4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/nfs_idmap.h>
55 #include <linux/sunrpc/bc_xprt.h>
56 #include <linux/xattr.h>
57 #include <linux/utsname.h>
58 #include <linux/freezer.h>
61 #include "delegation.h"
68 #define NFSDBG_FACILITY NFSDBG_PROC
70 #define NFS4_POLL_RETRY_MIN (HZ/10)
71 #define NFS4_POLL_RETRY_MAX (15*HZ)
73 #define NFS4_MAX_LOOP_ON_RECOVER (10)
76 static int _nfs4_proc_open(struct nfs4_opendata *data);
77 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
78 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
79 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
80 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
81 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
82 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
83 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
84 struct nfs_fattr *fattr, struct iattr *sattr,
85 struct nfs4_state *state);
86 #ifdef CONFIG_NFS_V4_1
87 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
88 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
90 /* Prevent leaks of NFSv4 errors into userland */
91 static int nfs4_map_errors(int err)
96 case -NFS4ERR_RESOURCE:
98 case -NFS4ERR_WRONGSEC:
100 case -NFS4ERR_BADOWNER:
101 case -NFS4ERR_BADNAME:
103 case -NFS4ERR_SHARE_DENIED:
105 case -NFS4ERR_MINOR_VERS_MISMATCH:
106 return -EPROTONOSUPPORT;
107 case -NFS4ERR_ACCESS:
110 dprintk("%s could not handle NFSv4 error %d\n",
118 * This is our standard bitmap for GETATTR requests.
120 const u32 nfs4_fattr_bitmap[3] = {
122 | FATTR4_WORD0_CHANGE
125 | FATTR4_WORD0_FILEID,
127 | FATTR4_WORD1_NUMLINKS
129 | FATTR4_WORD1_OWNER_GROUP
130 | FATTR4_WORD1_RAWDEV
131 | FATTR4_WORD1_SPACE_USED
132 | FATTR4_WORD1_TIME_ACCESS
133 | FATTR4_WORD1_TIME_METADATA
134 | FATTR4_WORD1_TIME_MODIFY
137 static const u32 nfs4_pnfs_open_bitmap[3] = {
139 | FATTR4_WORD0_CHANGE
142 | FATTR4_WORD0_FILEID,
144 | FATTR4_WORD1_NUMLINKS
146 | FATTR4_WORD1_OWNER_GROUP
147 | FATTR4_WORD1_RAWDEV
148 | FATTR4_WORD1_SPACE_USED
149 | FATTR4_WORD1_TIME_ACCESS
150 | FATTR4_WORD1_TIME_METADATA
151 | FATTR4_WORD1_TIME_MODIFY,
152 FATTR4_WORD2_MDSTHRESHOLD
155 static const u32 nfs4_open_noattr_bitmap[3] = {
157 | FATTR4_WORD0_CHANGE
158 | FATTR4_WORD0_FILEID,
161 const u32 nfs4_statfs_bitmap[2] = {
162 FATTR4_WORD0_FILES_AVAIL
163 | FATTR4_WORD0_FILES_FREE
164 | FATTR4_WORD0_FILES_TOTAL,
165 FATTR4_WORD1_SPACE_AVAIL
166 | FATTR4_WORD1_SPACE_FREE
167 | FATTR4_WORD1_SPACE_TOTAL
170 const u32 nfs4_pathconf_bitmap[2] = {
172 | FATTR4_WORD0_MAXNAME,
176 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
177 | FATTR4_WORD0_MAXREAD
178 | FATTR4_WORD0_MAXWRITE
179 | FATTR4_WORD0_LEASE_TIME,
180 FATTR4_WORD1_TIME_DELTA
181 | FATTR4_WORD1_FS_LAYOUT_TYPES,
182 FATTR4_WORD2_LAYOUT_BLKSIZE
185 const u32 nfs4_fs_locations_bitmap[2] = {
187 | FATTR4_WORD0_CHANGE
190 | FATTR4_WORD0_FILEID
191 | FATTR4_WORD0_FS_LOCATIONS,
193 | FATTR4_WORD1_NUMLINKS
195 | FATTR4_WORD1_OWNER_GROUP
196 | FATTR4_WORD1_RAWDEV
197 | FATTR4_WORD1_SPACE_USED
198 | FATTR4_WORD1_TIME_ACCESS
199 | FATTR4_WORD1_TIME_METADATA
200 | FATTR4_WORD1_TIME_MODIFY
201 | FATTR4_WORD1_MOUNTED_ON_FILEID
204 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
205 struct nfs4_readdir_arg *readdir)
210 readdir->cookie = cookie;
211 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
216 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
221 * NFSv4 servers do not return entries for '.' and '..'
222 * Therefore, we fake these entries here. We let '.'
223 * have cookie 0 and '..' have cookie 1. Note that
224 * when talking to the server, we always send cookie 0
227 start = p = kmap_atomic(*readdir->pages);
230 *p++ = xdr_one; /* next */
231 *p++ = xdr_zero; /* cookie, first word */
232 *p++ = xdr_one; /* cookie, second word */
233 *p++ = xdr_one; /* entry len */
234 memcpy(p, ".\0\0\0", 4); /* entry */
236 *p++ = xdr_one; /* bitmap length */
237 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
238 *p++ = htonl(8); /* attribute buffer length */
239 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
242 *p++ = xdr_one; /* next */
243 *p++ = xdr_zero; /* cookie, first word */
244 *p++ = xdr_two; /* cookie, second word */
245 *p++ = xdr_two; /* entry len */
246 memcpy(p, "..\0\0", 4); /* entry */
248 *p++ = xdr_one; /* bitmap length */
249 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
250 *p++ = htonl(8); /* attribute buffer length */
251 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
253 readdir->pgbase = (char *)p - (char *)start;
254 readdir->count -= readdir->pgbase;
255 kunmap_atomic(start);
258 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
264 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
265 nfs_wait_bit_killable, TASK_KILLABLE);
269 if (clp->cl_cons_state < 0)
270 return clp->cl_cons_state;
274 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
281 *timeout = NFS4_POLL_RETRY_MIN;
282 if (*timeout > NFS4_POLL_RETRY_MAX)
283 *timeout = NFS4_POLL_RETRY_MAX;
284 freezable_schedule_timeout_killable(*timeout);
285 if (fatal_signal_pending(current))
291 /* This is the error handling routine for processes that are allowed
294 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
296 struct nfs_client *clp = server->nfs_client;
297 struct nfs4_state *state = exception->state;
298 struct inode *inode = exception->inode;
301 exception->retry = 0;
305 case -NFS4ERR_OPENMODE:
306 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
307 nfs4_inode_return_delegation(inode);
308 exception->retry = 1;
313 nfs4_schedule_stateid_recovery(server, state);
314 goto wait_on_recovery;
315 case -NFS4ERR_DELEG_REVOKED:
316 case -NFS4ERR_ADMIN_REVOKED:
317 case -NFS4ERR_BAD_STATEID:
320 nfs_remove_bad_delegation(state->inode);
321 nfs4_schedule_stateid_recovery(server, state);
322 goto wait_on_recovery;
323 case -NFS4ERR_EXPIRED:
325 nfs4_schedule_stateid_recovery(server, state);
326 case -NFS4ERR_STALE_STATEID:
327 case -NFS4ERR_STALE_CLIENTID:
328 nfs4_schedule_lease_recovery(clp);
329 goto wait_on_recovery;
330 #if defined(CONFIG_NFS_V4_1)
331 case -NFS4ERR_BADSESSION:
332 case -NFS4ERR_BADSLOT:
333 case -NFS4ERR_BAD_HIGH_SLOT:
334 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
335 case -NFS4ERR_DEADSESSION:
336 case -NFS4ERR_SEQ_FALSE_RETRY:
337 case -NFS4ERR_SEQ_MISORDERED:
338 dprintk("%s ERROR: %d Reset session\n", __func__,
340 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
341 goto wait_on_recovery;
342 #endif /* defined(CONFIG_NFS_V4_1) */
343 case -NFS4ERR_FILE_OPEN:
344 if (exception->timeout > HZ) {
345 /* We have retried a decent amount, time to
354 ret = nfs4_delay(server->client, &exception->timeout);
357 case -NFS4ERR_RETRY_UNCACHED_REP:
358 case -NFS4ERR_OLD_STATEID:
359 exception->retry = 1;
361 case -NFS4ERR_BADOWNER:
362 /* The following works around a Linux server bug! */
363 case -NFS4ERR_BADNAME:
364 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
365 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
366 exception->retry = 1;
367 printk(KERN_WARNING "NFS: v4 server %s "
368 "does not accept raw "
370 "Reenabling the idmapper.\n",
371 server->nfs_client->cl_hostname);
374 /* We failed to handle the error */
375 return nfs4_map_errors(ret);
377 ret = nfs4_wait_clnt_recover(clp);
379 exception->retry = 1;
384 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
386 spin_lock(&clp->cl_lock);
387 if (time_before(clp->cl_last_renewal,timestamp))
388 clp->cl_last_renewal = timestamp;
389 spin_unlock(&clp->cl_lock);
392 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
394 do_renew_lease(server->nfs_client, timestamp);
397 #if defined(CONFIG_NFS_V4_1)
400 * nfs4_free_slot - free a slot and efficiently update slot table.
402 * freeing a slot is trivially done by clearing its respective bit
404 * If the freed slotid equals highest_used_slotid we want to update it
405 * so that the server would be able to size down the slot table if needed,
406 * otherwise we know that the highest_used_slotid is still in use.
407 * When updating highest_used_slotid there may be "holes" in the bitmap
408 * so we need to scan down from highest_used_slotid to 0 looking for the now
409 * highest slotid in use.
410 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
412 * Must be called while holding tbl->slot_tbl_lock
415 nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
417 /* clear used bit in bitmap */
418 __clear_bit(slotid, tbl->used_slots);
420 /* update highest_used_slotid when it is freed */
421 if (slotid == tbl->highest_used_slotid) {
422 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
423 if (slotid < tbl->max_slots)
424 tbl->highest_used_slotid = slotid;
426 tbl->highest_used_slotid = NFS4_NO_SLOT;
428 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
429 slotid, tbl->highest_used_slotid);
432 bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
434 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
439 * Signal state manager thread if session fore channel is drained
441 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
443 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
444 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
445 nfs4_set_task_privileged, NULL);
449 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
452 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
453 complete(&ses->fc_slot_table.complete);
457 * Signal state manager thread if session back channel is drained
459 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
461 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
462 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
464 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
465 complete(&ses->bc_slot_table.complete);
468 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
470 struct nfs4_slot_table *tbl;
472 tbl = &res->sr_session->fc_slot_table;
474 /* just wake up the next guy waiting since
475 * we may have not consumed a slot after all */
476 dprintk("%s: No slot\n", __func__);
480 spin_lock(&tbl->slot_tbl_lock);
481 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
482 nfs4_check_drain_fc_complete(res->sr_session);
483 spin_unlock(&tbl->slot_tbl_lock);
487 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
489 unsigned long timestamp;
490 struct nfs_client *clp;
493 * sr_status remains 1 if an RPC level error occurred. The server
494 * may or may not have processed the sequence operation..
495 * Proceed as if the server received and processed the sequence
498 if (res->sr_status == 1)
499 res->sr_status = NFS_OK;
501 /* don't increment the sequence number if the task wasn't sent */
502 if (!RPC_WAS_SENT(task))
505 /* Check the SEQUENCE operation status */
506 switch (res->sr_status) {
508 /* Update the slot's sequence and clientid lease timer */
509 ++res->sr_slot->seq_nr;
510 timestamp = res->sr_renewal_time;
511 clp = res->sr_session->clp;
512 do_renew_lease(clp, timestamp);
513 /* Check sequence flags */
514 if (res->sr_status_flags != 0)
515 nfs4_schedule_lease_recovery(clp);
518 /* The server detected a resend of the RPC call and
519 * returned NFS4ERR_DELAY as per Section 2.10.6.2
522 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
524 res->sr_slot - res->sr_session->fc_slot_table.slots,
525 res->sr_slot->seq_nr);
528 /* Just update the slot sequence no. */
529 ++res->sr_slot->seq_nr;
532 /* The session may be reset by one of the error handlers. */
533 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
534 nfs41_sequence_free_slot(res);
537 if (!rpc_restart_call(task))
539 rpc_delay(task, NFS4_POLL_RETRY_MAX);
543 static int nfs4_sequence_done(struct rpc_task *task,
544 struct nfs4_sequence_res *res)
546 if (res->sr_session == NULL)
548 return nfs41_sequence_done(task, res);
552 * nfs4_find_slot - efficiently look for a free slot
554 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
555 * If found, we mark the slot as used, update the highest_used_slotid,
556 * and respectively set up the sequence operation args.
557 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
559 * Note: must be called with under the slot_tbl_lock.
562 nfs4_find_slot(struct nfs4_slot_table *tbl)
565 u32 ret_id = NFS4_NO_SLOT;
567 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
568 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
570 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
571 if (slotid >= tbl->max_slots)
573 __set_bit(slotid, tbl->used_slots);
574 if (slotid > tbl->highest_used_slotid ||
575 tbl->highest_used_slotid == NFS4_NO_SLOT)
576 tbl->highest_used_slotid = slotid;
579 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
580 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
584 static void nfs41_init_sequence(struct nfs4_sequence_args *args,
585 struct nfs4_sequence_res *res, int cache_reply)
587 args->sa_session = NULL;
588 args->sa_cache_this = 0;
590 args->sa_cache_this = 1;
591 res->sr_session = NULL;
595 int nfs41_setup_sequence(struct nfs4_session *session,
596 struct nfs4_sequence_args *args,
597 struct nfs4_sequence_res *res,
598 struct rpc_task *task)
600 struct nfs4_slot *slot;
601 struct nfs4_slot_table *tbl;
604 dprintk("--> %s\n", __func__);
605 /* slot already allocated? */
606 if (res->sr_slot != NULL)
609 tbl = &session->fc_slot_table;
611 spin_lock(&tbl->slot_tbl_lock);
612 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
613 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
614 /* The state manager will wait until the slot table is empty */
615 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
616 spin_unlock(&tbl->slot_tbl_lock);
617 dprintk("%s session is draining\n", __func__);
621 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
622 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
623 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
624 spin_unlock(&tbl->slot_tbl_lock);
625 dprintk("%s enforce FIFO order\n", __func__);
629 slotid = nfs4_find_slot(tbl);
630 if (slotid == NFS4_NO_SLOT) {
631 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
632 spin_unlock(&tbl->slot_tbl_lock);
633 dprintk("<-- %s: no free slots\n", __func__);
636 spin_unlock(&tbl->slot_tbl_lock);
638 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
639 slot = tbl->slots + slotid;
640 args->sa_session = session;
641 args->sa_slotid = slotid;
643 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
645 res->sr_session = session;
647 res->sr_renewal_time = jiffies;
648 res->sr_status_flags = 0;
650 * sr_status is only set in decode_sequence, and so will remain
651 * set to 1 if an rpc level failure occurs.
656 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
658 int nfs4_setup_sequence(const struct nfs_server *server,
659 struct nfs4_sequence_args *args,
660 struct nfs4_sequence_res *res,
661 struct rpc_task *task)
663 struct nfs4_session *session = nfs4_get_session(server);
669 dprintk("--> %s clp %p session %p sr_slot %td\n",
670 __func__, session->clp, session, res->sr_slot ?
671 res->sr_slot - session->fc_slot_table.slots : -1);
673 ret = nfs41_setup_sequence(session, args, res, task);
675 dprintk("<-- %s status=%d\n", __func__, ret);
679 struct nfs41_call_sync_data {
680 const struct nfs_server *seq_server;
681 struct nfs4_sequence_args *seq_args;
682 struct nfs4_sequence_res *seq_res;
685 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
687 struct nfs41_call_sync_data *data = calldata;
689 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
691 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
692 data->seq_res, task))
694 rpc_call_start(task);
697 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
699 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
700 nfs41_call_sync_prepare(task, calldata);
703 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
705 struct nfs41_call_sync_data *data = calldata;
707 nfs41_sequence_done(task, data->seq_res);
710 static const struct rpc_call_ops nfs41_call_sync_ops = {
711 .rpc_call_prepare = nfs41_call_sync_prepare,
712 .rpc_call_done = nfs41_call_sync_done,
715 static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
716 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
717 .rpc_call_done = nfs41_call_sync_done,
720 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
721 struct nfs_server *server,
722 struct rpc_message *msg,
723 struct nfs4_sequence_args *args,
724 struct nfs4_sequence_res *res,
728 struct rpc_task *task;
729 struct nfs41_call_sync_data data = {
730 .seq_server = server,
734 struct rpc_task_setup task_setup = {
737 .callback_ops = &nfs41_call_sync_ops,
738 .callback_data = &data
742 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
743 task = rpc_run_task(&task_setup);
747 ret = task->tk_status;
753 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
754 struct nfs_server *server,
755 struct rpc_message *msg,
756 struct nfs4_sequence_args *args,
757 struct nfs4_sequence_res *res,
760 nfs41_init_sequence(args, res, cache_reply);
761 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
766 void nfs41_init_sequence(struct nfs4_sequence_args *args,
767 struct nfs4_sequence_res *res, int cache_reply)
771 static int nfs4_sequence_done(struct rpc_task *task,
772 struct nfs4_sequence_res *res)
776 #endif /* CONFIG_NFS_V4_1 */
778 int _nfs4_call_sync(struct rpc_clnt *clnt,
779 struct nfs_server *server,
780 struct rpc_message *msg,
781 struct nfs4_sequence_args *args,
782 struct nfs4_sequence_res *res,
785 nfs41_init_sequence(args, res, cache_reply);
786 return rpc_call_sync(clnt, msg, 0);
790 int nfs4_call_sync(struct rpc_clnt *clnt,
791 struct nfs_server *server,
792 struct rpc_message *msg,
793 struct nfs4_sequence_args *args,
794 struct nfs4_sequence_res *res,
797 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
798 args, res, cache_reply);
801 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
803 struct nfs_inode *nfsi = NFS_I(dir);
805 spin_lock(&dir->i_lock);
806 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
807 if (!cinfo->atomic || cinfo->before != dir->i_version)
808 nfs_force_lookup_revalidate(dir);
809 dir->i_version = cinfo->after;
810 spin_unlock(&dir->i_lock);
813 struct nfs4_opendata {
815 struct nfs_openargs o_arg;
816 struct nfs_openres o_res;
817 struct nfs_open_confirmargs c_arg;
818 struct nfs_open_confirmres c_res;
819 struct nfs4_string owner_name;
820 struct nfs4_string group_name;
821 struct nfs_fattr f_attr;
823 struct dentry *dentry;
824 struct nfs4_state_owner *owner;
825 struct nfs4_state *state;
827 unsigned long timestamp;
828 unsigned int rpc_done : 1;
834 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
836 p->o_res.f_attr = &p->f_attr;
837 p->o_res.seqid = p->o_arg.seqid;
838 p->c_res.seqid = p->c_arg.seqid;
839 p->o_res.server = p->o_arg.server;
840 p->o_res.access_request = p->o_arg.access;
841 nfs_fattr_init(&p->f_attr);
842 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
845 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
846 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
847 const struct iattr *attrs,
850 struct dentry *parent = dget_parent(dentry);
851 struct inode *dir = parent->d_inode;
852 struct nfs_server *server = NFS_SERVER(dir);
853 struct nfs4_opendata *p;
855 p = kzalloc(sizeof(*p), gfp_mask);
858 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
859 if (p->o_arg.seqid == NULL)
861 nfs_sb_active(dentry->d_sb);
862 p->dentry = dget(dentry);
865 atomic_inc(&sp->so_count);
866 p->o_arg.fh = NFS_FH(dir);
867 p->o_arg.open_flags = flags;
868 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
869 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
870 * will return permission denied for all bits until close */
871 if (!(flags & O_EXCL)) {
872 /* ask server to check for all possible rights as results
874 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
875 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
877 p->o_arg.clientid = server->nfs_client->cl_clientid;
878 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
879 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
880 p->o_arg.name = &dentry->d_name;
881 p->o_arg.server = server;
882 p->o_arg.bitmask = server->attr_bitmask;
883 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
884 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
885 if (attrs != NULL && attrs->ia_valid != 0) {
888 p->o_arg.u.attrs = &p->attrs;
889 memcpy(&p->attrs, attrs, sizeof(p->attrs));
892 verf[1] = current->pid;
893 memcpy(p->o_arg.u.verifier.data, verf,
894 sizeof(p->o_arg.u.verifier.data));
896 p->c_arg.fh = &p->o_res.fh;
897 p->c_arg.stateid = &p->o_res.stateid;
898 p->c_arg.seqid = p->o_arg.seqid;
899 nfs4_init_opendata_res(p);
909 static void nfs4_opendata_free(struct kref *kref)
911 struct nfs4_opendata *p = container_of(kref,
912 struct nfs4_opendata, kref);
913 struct super_block *sb = p->dentry->d_sb;
915 nfs_free_seqid(p->o_arg.seqid);
916 if (p->state != NULL)
917 nfs4_put_open_state(p->state);
918 nfs4_put_state_owner(p->owner);
922 nfs_fattr_free_names(&p->f_attr);
926 static void nfs4_opendata_put(struct nfs4_opendata *p)
929 kref_put(&p->kref, nfs4_opendata_free);
932 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
936 ret = rpc_wait_for_completion_task(task);
940 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
944 if (open_mode & (O_EXCL|O_TRUNC))
946 switch (mode & (FMODE_READ|FMODE_WRITE)) {
948 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
949 && state->n_rdonly != 0;
952 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
953 && state->n_wronly != 0;
955 case FMODE_READ|FMODE_WRITE:
956 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
957 && state->n_rdwr != 0;
963 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
965 if (delegation == NULL)
967 if ((delegation->type & fmode) != fmode)
969 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
971 nfs_mark_delegation_referenced(delegation);
975 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
984 case FMODE_READ|FMODE_WRITE:
987 nfs4_state_set_mode_locked(state, state->state | fmode);
990 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
992 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
993 nfs4_stateid_copy(&state->stateid, stateid);
994 nfs4_stateid_copy(&state->open_stateid, stateid);
997 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1000 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1002 case FMODE_READ|FMODE_WRITE:
1003 set_bit(NFS_O_RDWR_STATE, &state->flags);
1007 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1009 write_seqlock(&state->seqlock);
1010 nfs_set_open_stateid_locked(state, stateid, fmode);
1011 write_sequnlock(&state->seqlock);
1014 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1017 * Protect the call to nfs4_state_set_mode_locked and
1018 * serialise the stateid update
1020 write_seqlock(&state->seqlock);
1021 if (deleg_stateid != NULL) {
1022 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1023 set_bit(NFS_DELEGATED_STATE, &state->flags);
1025 if (open_stateid != NULL)
1026 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1027 write_sequnlock(&state->seqlock);
1028 spin_lock(&state->owner->so_lock);
1029 update_open_stateflags(state, fmode);
1030 spin_unlock(&state->owner->so_lock);
1033 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1035 struct nfs_inode *nfsi = NFS_I(state->inode);
1036 struct nfs_delegation *deleg_cur;
1039 fmode &= (FMODE_READ|FMODE_WRITE);
1042 deleg_cur = rcu_dereference(nfsi->delegation);
1043 if (deleg_cur == NULL)
1046 spin_lock(&deleg_cur->lock);
1047 if (nfsi->delegation != deleg_cur ||
1048 (deleg_cur->type & fmode) != fmode)
1049 goto no_delegation_unlock;
1051 if (delegation == NULL)
1052 delegation = &deleg_cur->stateid;
1053 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1054 goto no_delegation_unlock;
1056 nfs_mark_delegation_referenced(deleg_cur);
1057 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1059 no_delegation_unlock:
1060 spin_unlock(&deleg_cur->lock);
1064 if (!ret && open_stateid != NULL) {
1065 __update_open_stateid(state, open_stateid, NULL, fmode);
1073 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1075 struct nfs_delegation *delegation;
1078 delegation = rcu_dereference(NFS_I(inode)->delegation);
1079 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1084 nfs4_inode_return_delegation(inode);
1087 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1089 struct nfs4_state *state = opendata->state;
1090 struct nfs_inode *nfsi = NFS_I(state->inode);
1091 struct nfs_delegation *delegation;
1092 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
1093 fmode_t fmode = opendata->o_arg.fmode;
1094 nfs4_stateid stateid;
1098 if (can_open_cached(state, fmode, open_mode)) {
1099 spin_lock(&state->owner->so_lock);
1100 if (can_open_cached(state, fmode, open_mode)) {
1101 update_open_stateflags(state, fmode);
1102 spin_unlock(&state->owner->so_lock);
1103 goto out_return_state;
1105 spin_unlock(&state->owner->so_lock);
1108 delegation = rcu_dereference(nfsi->delegation);
1109 if (!can_open_delegated(delegation, fmode)) {
1113 /* Save the delegation */
1114 nfs4_stateid_copy(&stateid, &delegation->stateid);
1116 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1121 /* Try to update the stateid using the delegation */
1122 if (update_open_stateid(state, NULL, &stateid, fmode))
1123 goto out_return_state;
1126 return ERR_PTR(ret);
1128 atomic_inc(&state->count);
1133 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1135 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1136 struct nfs_delegation *delegation;
1137 int delegation_flags = 0;
1140 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1142 delegation_flags = delegation->flags;
1144 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1145 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1146 "returning a delegation for "
1147 "OPEN(CLAIM_DELEGATE_CUR)\n",
1149 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1150 nfs_inode_set_delegation(state->inode,
1151 data->owner->so_cred,
1154 nfs_inode_reclaim_delegation(state->inode,
1155 data->owner->so_cred,
1160 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1161 * and update the nfs4_state.
1163 static struct nfs4_state *
1164 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1166 struct inode *inode = data->state->inode;
1167 struct nfs4_state *state = data->state;
1170 if (!data->rpc_done) {
1171 ret = data->rpc_status;
1176 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1177 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1178 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1182 state = nfs4_get_open_state(inode, data->owner);
1186 ret = nfs_refresh_inode(inode, &data->f_attr);
1190 if (data->o_res.delegation_type != 0)
1191 nfs4_opendata_check_deleg(data, state);
1192 update_open_stateid(state, &data->o_res.stateid, NULL,
1197 return ERR_PTR(ret);
1201 static struct nfs4_state *
1202 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1204 struct inode *inode;
1205 struct nfs4_state *state = NULL;
1208 if (!data->rpc_done) {
1209 state = nfs4_try_open_cached(data);
1214 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1216 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1217 ret = PTR_ERR(inode);
1221 state = nfs4_get_open_state(inode, data->owner);
1224 if (data->o_res.delegation_type != 0)
1225 nfs4_opendata_check_deleg(data, state);
1226 update_open_stateid(state, &data->o_res.stateid, NULL,
1234 return ERR_PTR(ret);
1237 static struct nfs4_state *
1238 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1240 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1241 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1242 return _nfs4_opendata_to_nfs4_state(data);
1245 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1247 struct nfs_inode *nfsi = NFS_I(state->inode);
1248 struct nfs_open_context *ctx;
1250 spin_lock(&state->inode->i_lock);
1251 list_for_each_entry(ctx, &nfsi->open_files, list) {
1252 if (ctx->state != state)
1254 get_nfs_open_context(ctx);
1255 spin_unlock(&state->inode->i_lock);
1258 spin_unlock(&state->inode->i_lock);
1259 return ERR_PTR(-ENOENT);
1262 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1264 struct nfs4_opendata *opendata;
1266 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1267 if (opendata == NULL)
1268 return ERR_PTR(-ENOMEM);
1269 opendata->state = state;
1270 atomic_inc(&state->count);
1274 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1276 struct nfs4_state *newstate;
1279 opendata->o_arg.open_flags = 0;
1280 opendata->o_arg.fmode = fmode;
1281 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1282 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1283 nfs4_init_opendata_res(opendata);
1284 ret = _nfs4_recover_proc_open(opendata);
1287 newstate = nfs4_opendata_to_nfs4_state(opendata);
1288 if (IS_ERR(newstate))
1289 return PTR_ERR(newstate);
1290 nfs4_close_state(newstate, fmode);
1295 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1297 struct nfs4_state *newstate;
1300 /* memory barrier prior to reading state->n_* */
1301 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1303 if (state->n_rdwr != 0) {
1304 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1305 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1308 if (newstate != state)
1311 if (state->n_wronly != 0) {
1312 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1313 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1316 if (newstate != state)
1319 if (state->n_rdonly != 0) {
1320 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1321 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1324 if (newstate != state)
1328 * We may have performed cached opens for all three recoveries.
1329 * Check if we need to update the current stateid.
1331 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1332 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1333 write_seqlock(&state->seqlock);
1334 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1335 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1336 write_sequnlock(&state->seqlock);
1343 * reclaim state on the server after a reboot.
1345 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1347 struct nfs_delegation *delegation;
1348 struct nfs4_opendata *opendata;
1349 fmode_t delegation_type = 0;
1352 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1353 if (IS_ERR(opendata))
1354 return PTR_ERR(opendata);
1355 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1356 opendata->o_arg.fh = NFS_FH(state->inode);
1358 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1359 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1360 delegation_type = delegation->type;
1362 opendata->o_arg.u.delegation_type = delegation_type;
1363 status = nfs4_open_recover(opendata, state);
1364 nfs4_opendata_put(opendata);
1368 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1370 struct nfs_server *server = NFS_SERVER(state->inode);
1371 struct nfs4_exception exception = { };
1374 err = _nfs4_do_open_reclaim(ctx, state);
1375 if (err != -NFS4ERR_DELAY)
1377 nfs4_handle_exception(server, err, &exception);
1378 } while (exception.retry);
1382 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1384 struct nfs_open_context *ctx;
1387 ctx = nfs4_state_find_open_context(state);
1389 return PTR_ERR(ctx);
1390 ret = nfs4_do_open_reclaim(ctx, state);
1391 put_nfs_open_context(ctx);
1395 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1397 struct nfs4_opendata *opendata;
1400 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1401 if (IS_ERR(opendata))
1402 return PTR_ERR(opendata);
1403 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1404 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1405 ret = nfs4_open_recover(opendata, state);
1406 nfs4_opendata_put(opendata);
1410 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1412 struct nfs4_exception exception = { };
1413 struct nfs_server *server = NFS_SERVER(state->inode);
1416 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1422 case -NFS4ERR_BADSESSION:
1423 case -NFS4ERR_BADSLOT:
1424 case -NFS4ERR_BAD_HIGH_SLOT:
1425 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1426 case -NFS4ERR_DEADSESSION:
1427 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1429 case -NFS4ERR_STALE_CLIENTID:
1430 case -NFS4ERR_STALE_STATEID:
1431 case -NFS4ERR_EXPIRED:
1432 /* Don't recall a delegation if it was lost */
1433 nfs4_schedule_lease_recovery(server->nfs_client);
1437 * The show must go on: exit, but mark the
1438 * stateid as needing recovery.
1440 case -NFS4ERR_DELEG_REVOKED:
1441 case -NFS4ERR_ADMIN_REVOKED:
1442 case -NFS4ERR_BAD_STATEID:
1443 nfs_inode_find_state_and_recover(state->inode,
1445 nfs4_schedule_stateid_recovery(server, state);
1448 * User RPCSEC_GSS context has expired.
1449 * We cannot recover this stateid now, so
1450 * skip it and allow recovery thread to
1457 err = nfs4_handle_exception(server, err, &exception);
1458 } while (exception.retry);
1463 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1465 struct nfs4_opendata *data = calldata;
1467 data->rpc_status = task->tk_status;
1468 if (data->rpc_status == 0) {
1469 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1470 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1471 renew_lease(data->o_res.server, data->timestamp);
1476 static void nfs4_open_confirm_release(void *calldata)
1478 struct nfs4_opendata *data = calldata;
1479 struct nfs4_state *state = NULL;
1481 /* If this request hasn't been cancelled, do nothing */
1482 if (data->cancelled == 0)
1484 /* In case of error, no cleanup! */
1485 if (!data->rpc_done)
1487 state = nfs4_opendata_to_nfs4_state(data);
1489 nfs4_close_state(state, data->o_arg.fmode);
1491 nfs4_opendata_put(data);
1494 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1495 .rpc_call_done = nfs4_open_confirm_done,
1496 .rpc_release = nfs4_open_confirm_release,
1500 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1502 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1504 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1505 struct rpc_task *task;
1506 struct rpc_message msg = {
1507 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1508 .rpc_argp = &data->c_arg,
1509 .rpc_resp = &data->c_res,
1510 .rpc_cred = data->owner->so_cred,
1512 struct rpc_task_setup task_setup_data = {
1513 .rpc_client = server->client,
1514 .rpc_message = &msg,
1515 .callback_ops = &nfs4_open_confirm_ops,
1516 .callback_data = data,
1517 .workqueue = nfsiod_workqueue,
1518 .flags = RPC_TASK_ASYNC,
1522 kref_get(&data->kref);
1524 data->rpc_status = 0;
1525 data->timestamp = jiffies;
1526 task = rpc_run_task(&task_setup_data);
1528 return PTR_ERR(task);
1529 status = nfs4_wait_for_completion_rpc_task(task);
1531 data->cancelled = 1;
1534 status = data->rpc_status;
1539 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1541 struct nfs4_opendata *data = calldata;
1542 struct nfs4_state_owner *sp = data->owner;
1544 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1547 * Check if we still need to send an OPEN call, or if we can use
1548 * a delegation instead.
1550 if (data->state != NULL) {
1551 struct nfs_delegation *delegation;
1553 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1556 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1557 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1558 can_open_delegated(delegation, data->o_arg.fmode))
1559 goto unlock_no_action;
1562 /* Update client id. */
1563 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1564 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1565 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1566 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1567 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1569 data->timestamp = jiffies;
1570 if (nfs4_setup_sequence(data->o_arg.server,
1571 &data->o_arg.seq_args,
1572 &data->o_res.seq_res,
1574 nfs_release_seqid(data->o_arg.seqid);
1576 rpc_call_start(task);
1581 task->tk_action = NULL;
1585 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1587 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1588 nfs4_open_prepare(task, calldata);
1591 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1593 struct nfs4_opendata *data = calldata;
1595 data->rpc_status = task->tk_status;
1597 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1600 if (task->tk_status == 0) {
1601 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1602 switch (data->o_res.f_attr->mode & S_IFMT) {
1606 data->rpc_status = -ELOOP;
1609 data->rpc_status = -EISDIR;
1612 data->rpc_status = -ENOTDIR;
1615 renew_lease(data->o_res.server, data->timestamp);
1616 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1617 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1622 static void nfs4_open_release(void *calldata)
1624 struct nfs4_opendata *data = calldata;
1625 struct nfs4_state *state = NULL;
1627 /* If this request hasn't been cancelled, do nothing */
1628 if (data->cancelled == 0)
1630 /* In case of error, no cleanup! */
1631 if (data->rpc_status != 0 || !data->rpc_done)
1633 /* In case we need an open_confirm, no cleanup! */
1634 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1636 state = nfs4_opendata_to_nfs4_state(data);
1638 nfs4_close_state(state, data->o_arg.fmode);
1640 nfs4_opendata_put(data);
1643 static const struct rpc_call_ops nfs4_open_ops = {
1644 .rpc_call_prepare = nfs4_open_prepare,
1645 .rpc_call_done = nfs4_open_done,
1646 .rpc_release = nfs4_open_release,
1649 static const struct rpc_call_ops nfs4_recover_open_ops = {
1650 .rpc_call_prepare = nfs4_recover_open_prepare,
1651 .rpc_call_done = nfs4_open_done,
1652 .rpc_release = nfs4_open_release,
1655 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1657 struct inode *dir = data->dir->d_inode;
1658 struct nfs_server *server = NFS_SERVER(dir);
1659 struct nfs_openargs *o_arg = &data->o_arg;
1660 struct nfs_openres *o_res = &data->o_res;
1661 struct rpc_task *task;
1662 struct rpc_message msg = {
1663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1666 .rpc_cred = data->owner->so_cred,
1668 struct rpc_task_setup task_setup_data = {
1669 .rpc_client = server->client,
1670 .rpc_message = &msg,
1671 .callback_ops = &nfs4_open_ops,
1672 .callback_data = data,
1673 .workqueue = nfsiod_workqueue,
1674 .flags = RPC_TASK_ASYNC,
1678 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1679 kref_get(&data->kref);
1681 data->rpc_status = 0;
1682 data->cancelled = 0;
1684 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1685 task = rpc_run_task(&task_setup_data);
1687 return PTR_ERR(task);
1688 status = nfs4_wait_for_completion_rpc_task(task);
1690 data->cancelled = 1;
1693 status = data->rpc_status;
1699 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1701 struct inode *dir = data->dir->d_inode;
1702 struct nfs_openres *o_res = &data->o_res;
1705 status = nfs4_run_open_task(data, 1);
1706 if (status != 0 || !data->rpc_done)
1709 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1711 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1712 status = _nfs4_proc_open_confirm(data);
1720 static int nfs4_opendata_access(struct rpc_cred *cred,
1721 struct nfs4_opendata *opendata,
1722 struct nfs4_state *state, fmode_t fmode)
1724 struct nfs_access_entry cache;
1727 /* access call failed or for some reason the server doesn't
1728 * support any access modes -- defer access call until later */
1729 if (opendata->o_res.access_supported == 0)
1733 /* don't check MAY_WRITE - a newly created file may not have
1734 * write mode bits, but POSIX allows the creating process to write */
1735 if (fmode & FMODE_READ)
1737 if (fmode & FMODE_EXEC)
1741 cache.jiffies = jiffies;
1742 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1743 nfs_access_add_cache(state->inode, &cache);
1745 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1748 /* even though OPEN succeeded, access is denied. Close the file */
1749 nfs4_close_state(state, fmode);
1754 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1756 static int _nfs4_proc_open(struct nfs4_opendata *data)
1758 struct inode *dir = data->dir->d_inode;
1759 struct nfs_server *server = NFS_SERVER(dir);
1760 struct nfs_openargs *o_arg = &data->o_arg;
1761 struct nfs_openres *o_res = &data->o_res;
1764 status = nfs4_run_open_task(data, 0);
1765 if (!data->rpc_done)
1768 if (status == -NFS4ERR_BADNAME &&
1769 !(o_arg->open_flags & O_CREAT))
1774 nfs_fattr_map_and_free_names(server, &data->f_attr);
1776 if (o_arg->open_flags & O_CREAT)
1777 update_changeattr(dir, &o_res->cinfo);
1778 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1779 server->caps &= ~NFS_CAP_POSIX_LOCK;
1780 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1781 status = _nfs4_proc_open_confirm(data);
1785 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1786 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1790 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1795 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1796 ret = nfs4_wait_clnt_recover(clp);
1799 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1800 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1802 nfs4_schedule_state_manager(clp);
1808 static int nfs4_recover_expired_lease(struct nfs_server *server)
1810 return nfs4_client_recover_expired_lease(server->nfs_client);
1815 * reclaim state on the server after a network partition.
1816 * Assumes caller holds the appropriate lock
1818 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1820 struct nfs4_opendata *opendata;
1823 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1824 if (IS_ERR(opendata))
1825 return PTR_ERR(opendata);
1826 ret = nfs4_open_recover(opendata, state);
1828 d_drop(ctx->dentry);
1829 nfs4_opendata_put(opendata);
1833 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1835 struct nfs_server *server = NFS_SERVER(state->inode);
1836 struct nfs4_exception exception = { };
1840 err = _nfs4_open_expired(ctx, state);
1844 case -NFS4ERR_GRACE:
1845 case -NFS4ERR_DELAY:
1846 nfs4_handle_exception(server, err, &exception);
1849 } while (exception.retry);
1854 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1856 struct nfs_open_context *ctx;
1859 ctx = nfs4_state_find_open_context(state);
1861 return PTR_ERR(ctx);
1862 ret = nfs4_do_open_expired(ctx, state);
1863 put_nfs_open_context(ctx);
1867 #if defined(CONFIG_NFS_V4_1)
1868 static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
1870 struct nfs_server *server = NFS_SERVER(state->inode);
1871 nfs4_stateid *stateid = &state->stateid;
1874 /* If a state reset has been done, test_stateid is unneeded */
1875 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1878 status = nfs41_test_stateid(server, stateid);
1879 if (status != NFS_OK) {
1880 /* Free the stateid unless the server explicitly
1881 * informs us the stateid is unrecognized. */
1882 if (status != -NFS4ERR_BAD_STATEID)
1883 nfs41_free_stateid(server, stateid);
1884 nfs_remove_bad_delegation(state->inode);
1886 write_seqlock(&state->seqlock);
1887 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1888 write_sequnlock(&state->seqlock);
1889 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1894 * nfs41_check_open_stateid - possibly free an open stateid
1896 * @state: NFSv4 state for an inode
1898 * Returns NFS_OK if recovery for this stateid is now finished.
1899 * Otherwise a negative NFS4ERR value is returned.
1901 static int nfs41_check_open_stateid(struct nfs4_state *state)
1903 struct nfs_server *server = NFS_SERVER(state->inode);
1904 nfs4_stateid *stateid = &state->open_stateid;
1907 /* If a state reset has been done, test_stateid is unneeded */
1908 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1909 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1910 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1911 return -NFS4ERR_BAD_STATEID;
1913 status = nfs41_test_stateid(server, stateid);
1914 if (status != NFS_OK) {
1915 /* Free the stateid unless the server explicitly
1916 * informs us the stateid is unrecognized. */
1917 if (status != -NFS4ERR_BAD_STATEID)
1918 nfs41_free_stateid(server, stateid);
1920 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1921 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1922 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1927 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1931 nfs41_clear_delegation_stateid(state);
1932 status = nfs41_check_open_stateid(state);
1933 if (status != NFS_OK)
1934 status = nfs4_open_expired(sp, state);
1940 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1941 * fields corresponding to attributes that were used to store the verifier.
1942 * Make sure we clobber those fields in the later setattr call
1944 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1946 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1947 !(sattr->ia_valid & ATTR_ATIME_SET))
1948 sattr->ia_valid |= ATTR_ATIME;
1950 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1951 !(sattr->ia_valid & ATTR_MTIME_SET))
1952 sattr->ia_valid |= ATTR_MTIME;
1956 * Returns a referenced nfs4_state
1958 static int _nfs4_do_open(struct inode *dir,
1959 struct dentry *dentry,
1962 struct iattr *sattr,
1963 struct rpc_cred *cred,
1964 struct nfs4_state **res,
1965 struct nfs4_threshold **ctx_th)
1967 struct nfs4_state_owner *sp;
1968 struct nfs4_state *state = NULL;
1969 struct nfs_server *server = NFS_SERVER(dir);
1970 struct nfs4_opendata *opendata;
1973 /* Protect against reboot recovery conflicts */
1975 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1977 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1980 status = nfs4_recover_expired_lease(server);
1982 goto err_put_state_owner;
1983 if (dentry->d_inode != NULL)
1984 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1986 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1987 if (opendata == NULL)
1988 goto err_put_state_owner;
1990 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1991 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1992 if (!opendata->f_attr.mdsthreshold)
1993 goto err_opendata_put;
1994 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
1996 if (dentry->d_inode != NULL)
1997 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1999 status = _nfs4_proc_open(opendata);
2001 goto err_opendata_put;
2003 state = nfs4_opendata_to_nfs4_state(opendata);
2004 status = PTR_ERR(state);
2006 goto err_opendata_put;
2007 if (server->caps & NFS_CAP_POSIX_LOCK)
2008 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2010 status = nfs4_opendata_access(cred, opendata, state, fmode);
2012 goto err_opendata_put;
2014 if (opendata->o_arg.open_flags & O_EXCL) {
2015 nfs4_exclusive_attrset(opendata, sattr);
2017 nfs_fattr_init(opendata->o_res.f_attr);
2018 status = nfs4_do_setattr(state->inode, cred,
2019 opendata->o_res.f_attr, sattr,
2022 nfs_setattr_update_inode(state->inode, sattr);
2023 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2026 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2027 *ctx_th = opendata->f_attr.mdsthreshold;
2029 kfree(opendata->f_attr.mdsthreshold);
2030 opendata->f_attr.mdsthreshold = NULL;
2032 nfs4_opendata_put(opendata);
2033 nfs4_put_state_owner(sp);
2037 kfree(opendata->f_attr.mdsthreshold);
2038 nfs4_opendata_put(opendata);
2039 err_put_state_owner:
2040 nfs4_put_state_owner(sp);
2047 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2048 struct dentry *dentry,
2051 struct iattr *sattr,
2052 struct rpc_cred *cred,
2053 struct nfs4_threshold **ctx_th)
2055 struct nfs4_exception exception = { };
2056 struct nfs4_state *res;
2059 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
2061 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2065 /* NOTE: BAD_SEQID means the server and client disagree about the
2066 * book-keeping w.r.t. state-changing operations
2067 * (OPEN/CLOSE/LOCK/LOCKU...)
2068 * It is actually a sign of a bug on the client or on the server.
2070 * If we receive a BAD_SEQID error in the particular case of
2071 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2072 * have unhashed the old state_owner for us, and that we can
2073 * therefore safely retry using a new one. We should still warn
2074 * the user though...
2076 if (status == -NFS4ERR_BAD_SEQID) {
2077 pr_warn_ratelimited("NFS: v4 server %s "
2078 " returned a bad sequence-id error!\n",
2079 NFS_SERVER(dir)->nfs_client->cl_hostname);
2080 exception.retry = 1;
2084 * BAD_STATEID on OPEN means that the server cancelled our
2085 * state before it received the OPEN_CONFIRM.
2086 * Recover by retrying the request as per the discussion
2087 * on Page 181 of RFC3530.
2089 if (status == -NFS4ERR_BAD_STATEID) {
2090 exception.retry = 1;
2093 if (status == -EAGAIN) {
2094 /* We must have found a delegation */
2095 exception.retry = 1;
2098 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2099 status, &exception));
2100 } while (exception.retry);
2104 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2105 struct nfs_fattr *fattr, struct iattr *sattr,
2106 struct nfs4_state *state)
2108 struct nfs_server *server = NFS_SERVER(inode);
2109 struct nfs_setattrargs arg = {
2110 .fh = NFS_FH(inode),
2113 .bitmask = server->attr_bitmask,
2115 struct nfs_setattrres res = {
2119 struct rpc_message msg = {
2120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2125 unsigned long timestamp = jiffies;
2128 nfs_fattr_init(fattr);
2130 if (state != NULL) {
2131 struct nfs_lockowner lockowner = {
2132 .l_owner = current->files,
2133 .l_pid = current->tgid,
2135 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2137 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2139 /* Use that stateid */
2141 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2143 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2144 if (status == 0 && state != NULL)
2145 renew_lease(server, timestamp);
2149 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2150 struct nfs_fattr *fattr, struct iattr *sattr,
2151 struct nfs4_state *state)
2153 struct nfs_server *server = NFS_SERVER(inode);
2154 struct nfs4_exception exception = {
2160 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2162 case -NFS4ERR_OPENMODE:
2163 if (state && !(state->state & FMODE_WRITE)) {
2165 if (sattr->ia_valid & ATTR_OPEN)
2170 err = nfs4_handle_exception(server, err, &exception);
2171 } while (exception.retry);
2176 struct nfs4_closedata {
2177 struct inode *inode;
2178 struct nfs4_state *state;
2179 struct nfs_closeargs arg;
2180 struct nfs_closeres res;
2181 struct nfs_fattr fattr;
2182 unsigned long timestamp;
2187 static void nfs4_free_closedata(void *data)
2189 struct nfs4_closedata *calldata = data;
2190 struct nfs4_state_owner *sp = calldata->state->owner;
2191 struct super_block *sb = calldata->state->inode->i_sb;
2194 pnfs_roc_release(calldata->state->inode);
2195 nfs4_put_open_state(calldata->state);
2196 nfs_free_seqid(calldata->arg.seqid);
2197 nfs4_put_state_owner(sp);
2198 nfs_sb_deactive_async(sb);
2202 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2205 spin_lock(&state->owner->so_lock);
2206 if (!(fmode & FMODE_READ))
2207 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2208 if (!(fmode & FMODE_WRITE))
2209 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2210 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2211 spin_unlock(&state->owner->so_lock);
2214 static void nfs4_close_done(struct rpc_task *task, void *data)
2216 struct nfs4_closedata *calldata = data;
2217 struct nfs4_state *state = calldata->state;
2218 struct nfs_server *server = NFS_SERVER(calldata->inode);
2220 dprintk("%s: begin!\n", __func__);
2221 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2223 /* hmm. we are done with the inode, and in the process of freeing
2224 * the state_owner. we keep this around to process errors
2226 switch (task->tk_status) {
2229 pnfs_roc_set_barrier(state->inode,
2230 calldata->roc_barrier);
2231 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2232 renew_lease(server, calldata->timestamp);
2233 nfs4_close_clear_stateid_flags(state,
2234 calldata->arg.fmode);
2236 case -NFS4ERR_STALE_STATEID:
2237 case -NFS4ERR_OLD_STATEID:
2238 case -NFS4ERR_BAD_STATEID:
2239 case -NFS4ERR_EXPIRED:
2240 if (calldata->arg.fmode == 0)
2243 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2244 rpc_restart_call_prepare(task);
2246 nfs_release_seqid(calldata->arg.seqid);
2247 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2248 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2251 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2253 struct nfs4_closedata *calldata = data;
2254 struct nfs4_state *state = calldata->state;
2255 struct inode *inode = calldata->inode;
2258 dprintk("%s: begin!\n", __func__);
2259 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2262 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2263 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2264 spin_lock(&state->owner->so_lock);
2265 /* Calculate the change in open mode */
2266 if (state->n_rdwr == 0) {
2267 if (state->n_rdonly == 0) {
2268 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2269 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2270 calldata->arg.fmode &= ~FMODE_READ;
2272 if (state->n_wronly == 0) {
2273 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2274 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2275 calldata->arg.fmode &= ~FMODE_WRITE;
2278 spin_unlock(&state->owner->so_lock);
2281 /* Note: exit _without_ calling nfs4_close_done */
2282 task->tk_action = NULL;
2286 if (calldata->arg.fmode == 0) {
2287 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2288 if (calldata->roc &&
2289 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
2293 nfs_fattr_init(calldata->res.fattr);
2294 calldata->timestamp = jiffies;
2295 if (nfs4_setup_sequence(NFS_SERVER(inode),
2296 &calldata->arg.seq_args,
2297 &calldata->res.seq_res,
2299 nfs_release_seqid(calldata->arg.seqid);
2301 rpc_call_start(task);
2303 dprintk("%s: done!\n", __func__);
2306 static const struct rpc_call_ops nfs4_close_ops = {
2307 .rpc_call_prepare = nfs4_close_prepare,
2308 .rpc_call_done = nfs4_close_done,
2309 .rpc_release = nfs4_free_closedata,
2313 * It is possible for data to be read/written from a mem-mapped file
2314 * after the sys_close call (which hits the vfs layer as a flush).
2315 * This means that we can't safely call nfsv4 close on a file until
2316 * the inode is cleared. This in turn means that we are not good
2317 * NFSv4 citizens - we do not indicate to the server to update the file's
2318 * share state even when we are done with one of the three share
2319 * stateid's in the inode.
2321 * NOTE: Caller must be holding the sp->so_owner semaphore!
2323 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2325 struct nfs_server *server = NFS_SERVER(state->inode);
2326 struct nfs4_closedata *calldata;
2327 struct nfs4_state_owner *sp = state->owner;
2328 struct rpc_task *task;
2329 struct rpc_message msg = {
2330 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2331 .rpc_cred = state->owner->so_cred,
2333 struct rpc_task_setup task_setup_data = {
2334 .rpc_client = server->client,
2335 .rpc_message = &msg,
2336 .callback_ops = &nfs4_close_ops,
2337 .workqueue = nfsiod_workqueue,
2338 .flags = RPC_TASK_ASYNC,
2340 int status = -ENOMEM;
2342 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2343 if (calldata == NULL)
2345 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2346 calldata->inode = state->inode;
2347 calldata->state = state;
2348 calldata->arg.fh = NFS_FH(state->inode);
2349 calldata->arg.stateid = &state->open_stateid;
2350 /* Serialization for the sequence id */
2351 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2352 if (calldata->arg.seqid == NULL)
2353 goto out_free_calldata;
2354 calldata->arg.fmode = 0;
2355 calldata->arg.bitmask = server->cache_consistency_bitmask;
2356 calldata->res.fattr = &calldata->fattr;
2357 calldata->res.seqid = calldata->arg.seqid;
2358 calldata->res.server = server;
2359 calldata->roc = pnfs_roc(state->inode);
2360 nfs_sb_active(calldata->inode->i_sb);
2362 msg.rpc_argp = &calldata->arg;
2363 msg.rpc_resp = &calldata->res;
2364 task_setup_data.callback_data = calldata;
2365 task = rpc_run_task(&task_setup_data);
2367 return PTR_ERR(task);
2370 status = rpc_wait_for_completion_task(task);
2376 nfs4_put_open_state(state);
2377 nfs4_put_state_owner(sp);
2381 static struct inode *
2382 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2384 struct nfs4_state *state;
2386 /* Protect against concurrent sillydeletes */
2387 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2388 ctx->cred, &ctx->mdsthreshold);
2390 return ERR_CAST(state);
2392 return igrab(state->inode);
2395 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2397 if (ctx->state == NULL)
2400 nfs4_close_sync(ctx->state, ctx->mode);
2402 nfs4_close_state(ctx->state, ctx->mode);
2405 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2407 struct nfs4_server_caps_arg args = {
2410 struct nfs4_server_caps_res res = {};
2411 struct rpc_message msg = {
2412 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2418 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2420 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2421 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2422 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2423 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2424 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2425 NFS_CAP_CTIME|NFS_CAP_MTIME);
2426 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2427 server->caps |= NFS_CAP_ACLS;
2428 if (res.has_links != 0)
2429 server->caps |= NFS_CAP_HARDLINKS;
2430 if (res.has_symlinks != 0)
2431 server->caps |= NFS_CAP_SYMLINKS;
2432 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2433 server->caps |= NFS_CAP_FILEID;
2434 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2435 server->caps |= NFS_CAP_MODE;
2436 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2437 server->caps |= NFS_CAP_NLINK;
2438 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2439 server->caps |= NFS_CAP_OWNER;
2440 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2441 server->caps |= NFS_CAP_OWNER_GROUP;
2442 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2443 server->caps |= NFS_CAP_ATIME;
2444 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2445 server->caps |= NFS_CAP_CTIME;
2446 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2447 server->caps |= NFS_CAP_MTIME;
2449 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2450 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2451 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2452 server->acl_bitmask = res.acl_bitmask;
2453 server->fh_expire_type = res.fh_expire_type;
2459 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2461 struct nfs4_exception exception = { };
2464 err = nfs4_handle_exception(server,
2465 _nfs4_server_capabilities(server, fhandle),
2467 } while (exception.retry);
2471 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2472 struct nfs_fsinfo *info)
2474 struct nfs4_lookup_root_arg args = {
2475 .bitmask = nfs4_fattr_bitmap,
2477 struct nfs4_lookup_res res = {
2479 .fattr = info->fattr,
2482 struct rpc_message msg = {
2483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2488 nfs_fattr_init(info->fattr);
2489 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2492 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2493 struct nfs_fsinfo *info)
2495 struct nfs4_exception exception = { };
2498 err = _nfs4_lookup_root(server, fhandle, info);
2501 case -NFS4ERR_WRONGSEC:
2504 err = nfs4_handle_exception(server, err, &exception);
2506 } while (exception.retry);
2511 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2512 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2514 struct rpc_auth *auth;
2517 auth = rpcauth_create(flavor, server->client);
2522 ret = nfs4_lookup_root(server, fhandle, info);
2527 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2528 struct nfs_fsinfo *info)
2530 int i, len, status = 0;
2531 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2533 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2537 for (i = 0; i < len; i++) {
2538 /* AUTH_UNIX is the default flavor if none was specified,
2539 * thus has already been tried. */
2540 if (flav_array[i] == RPC_AUTH_UNIX)
2543 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2544 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2549 * -EACCESS could mean that the user doesn't have correct permissions
2550 * to access the mount. It could also mean that we tried to mount
2551 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2552 * existing mount programs don't handle -EACCES very well so it should
2553 * be mapped to -EPERM instead.
2555 if (status == -EACCES)
2561 * get the file handle for the "/" directory on the server
2563 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2564 struct nfs_fsinfo *info)
2566 int minor_version = server->nfs_client->cl_minorversion;
2567 int status = nfs4_lookup_root(server, fhandle, info);
2568 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2570 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2571 * by nfs4_map_errors() as this function exits.
2573 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2575 status = nfs4_server_capabilities(server, fhandle);
2577 status = nfs4_do_fsinfo(server, fhandle, info);
2578 return nfs4_map_errors(status);
2581 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2582 struct nfs_fsinfo *info)
2585 struct nfs_fattr *fattr = info->fattr;
2587 error = nfs4_server_capabilities(server, mntfh);
2589 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2593 error = nfs4_proc_getattr(server, mntfh, fattr);
2595 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2599 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2600 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2601 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2607 * Get locations and (maybe) other attributes of a referral.
2608 * Note that we'll actually follow the referral later when
2609 * we detect fsid mismatch in inode revalidation
2611 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2612 const struct qstr *name, struct nfs_fattr *fattr,
2613 struct nfs_fh *fhandle)
2615 int status = -ENOMEM;
2616 struct page *page = NULL;
2617 struct nfs4_fs_locations *locations = NULL;
2619 page = alloc_page(GFP_KERNEL);
2622 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2623 if (locations == NULL)
2626 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2629 /* Make sure server returned a different fsid for the referral */
2630 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2631 dprintk("%s: server did not return a different fsid for"
2632 " a referral at %s\n", __func__, name->name);
2636 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2637 nfs_fixup_referral_attributes(&locations->fattr);
2639 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2640 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2641 memset(fhandle, 0, sizeof(struct nfs_fh));
2649 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2651 struct nfs4_getattr_arg args = {
2653 .bitmask = server->attr_bitmask,
2655 struct nfs4_getattr_res res = {
2659 struct rpc_message msg = {
2660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2665 nfs_fattr_init(fattr);
2666 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2669 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2671 struct nfs4_exception exception = { };
2674 err = nfs4_handle_exception(server,
2675 _nfs4_proc_getattr(server, fhandle, fattr),
2677 } while (exception.retry);
2682 * The file is not closed if it is opened due to the a request to change
2683 * the size of the file. The open call will not be needed once the
2684 * VFS layer lookup-intents are implemented.
2686 * Close is called when the inode is destroyed.
2687 * If we haven't opened the file for O_WRONLY, we
2688 * need to in the size_change case to obtain a stateid.
2691 * Because OPEN is always done by name in nfsv4, it is
2692 * possible that we opened a different file by the same
2693 * name. We can recognize this race condition, but we
2694 * can't do anything about it besides returning an error.
2696 * This will be fixed with VFS changes (lookup-intent).
2699 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2700 struct iattr *sattr)
2702 struct inode *inode = dentry->d_inode;
2703 struct rpc_cred *cred = NULL;
2704 struct nfs4_state *state = NULL;
2707 if (pnfs_ld_layoutret_on_setattr(inode))
2708 pnfs_return_layout(inode);
2710 nfs_fattr_init(fattr);
2712 /* Deal with open(O_TRUNC) */
2713 if (sattr->ia_valid & ATTR_OPEN)
2714 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2716 /* Optimization: if the end result is no change, don't RPC */
2717 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2720 /* Search for an existing open(O_WRITE) file */
2721 if (sattr->ia_valid & ATTR_FILE) {
2722 struct nfs_open_context *ctx;
2724 ctx = nfs_file_open_context(sattr->ia_file);
2731 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2733 nfs_setattr_update_inode(inode, sattr);
2737 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2738 const struct qstr *name, struct nfs_fh *fhandle,
2739 struct nfs_fattr *fattr)
2741 struct nfs_server *server = NFS_SERVER(dir);
2743 struct nfs4_lookup_arg args = {
2744 .bitmask = server->attr_bitmask,
2745 .dir_fh = NFS_FH(dir),
2748 struct nfs4_lookup_res res = {
2753 struct rpc_message msg = {
2754 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2759 nfs_fattr_init(fattr);
2761 dprintk("NFS call lookup %s\n", name->name);
2762 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2763 dprintk("NFS reply lookup: %d\n", status);
2767 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2769 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2770 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2771 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2775 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2776 struct qstr *name, struct nfs_fh *fhandle,
2777 struct nfs_fattr *fattr)
2779 struct nfs4_exception exception = { };
2780 struct rpc_clnt *client = *clnt;
2783 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2785 case -NFS4ERR_BADNAME:
2788 case -NFS4ERR_MOVED:
2789 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
2791 case -NFS4ERR_WRONGSEC:
2793 if (client != *clnt)
2796 client = nfs4_create_sec_client(client, dir, name);
2798 return PTR_ERR(client);
2800 exception.retry = 1;
2803 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2805 } while (exception.retry);
2810 else if (client != *clnt)
2811 rpc_shutdown_client(client);
2816 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
2817 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2820 struct rpc_clnt *client = NFS_CLIENT(dir);
2822 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2823 if (client != NFS_CLIENT(dir)) {
2824 rpc_shutdown_client(client);
2825 nfs_fixup_secinfo_attributes(fattr);
2831 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2832 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2835 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2837 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2839 rpc_shutdown_client(client);
2840 return ERR_PTR(status);
2845 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2847 struct nfs_server *server = NFS_SERVER(inode);
2848 struct nfs4_accessargs args = {
2849 .fh = NFS_FH(inode),
2850 .bitmask = server->cache_consistency_bitmask,
2852 struct nfs4_accessres res = {
2855 struct rpc_message msg = {
2856 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2859 .rpc_cred = entry->cred,
2861 int mode = entry->mask;
2865 * Determine which access bits we want to ask for...
2867 if (mode & MAY_READ)
2868 args.access |= NFS4_ACCESS_READ;
2869 if (S_ISDIR(inode->i_mode)) {
2870 if (mode & MAY_WRITE)
2871 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2872 if (mode & MAY_EXEC)
2873 args.access |= NFS4_ACCESS_LOOKUP;
2875 if (mode & MAY_WRITE)
2876 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2877 if (mode & MAY_EXEC)
2878 args.access |= NFS4_ACCESS_EXECUTE;
2881 res.fattr = nfs_alloc_fattr();
2882 if (res.fattr == NULL)
2885 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2887 nfs_access_set_mask(entry, res.access);
2888 nfs_refresh_inode(inode, res.fattr);
2890 nfs_free_fattr(res.fattr);
2894 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2896 struct nfs4_exception exception = { };
2899 err = nfs4_handle_exception(NFS_SERVER(inode),
2900 _nfs4_proc_access(inode, entry),
2902 } while (exception.retry);
2907 * TODO: For the time being, we don't try to get any attributes
2908 * along with any of the zero-copy operations READ, READDIR,
2911 * In the case of the first three, we want to put the GETATTR
2912 * after the read-type operation -- this is because it is hard
2913 * to predict the length of a GETATTR response in v4, and thus
2914 * align the READ data correctly. This means that the GETATTR
2915 * may end up partially falling into the page cache, and we should
2916 * shift it into the 'tail' of the xdr_buf before processing.
2917 * To do this efficiently, we need to know the total length
2918 * of data received, which doesn't seem to be available outside
2921 * In the case of WRITE, we also want to put the GETATTR after
2922 * the operation -- in this case because we want to make sure
2923 * we get the post-operation mtime and size.
2925 * Both of these changes to the XDR layer would in fact be quite
2926 * minor, but I decided to leave them for a subsequent patch.
2928 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2929 unsigned int pgbase, unsigned int pglen)
2931 struct nfs4_readlink args = {
2932 .fh = NFS_FH(inode),
2937 struct nfs4_readlink_res res;
2938 struct rpc_message msg = {
2939 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2944 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2947 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2948 unsigned int pgbase, unsigned int pglen)
2950 struct nfs4_exception exception = { };
2953 err = nfs4_handle_exception(NFS_SERVER(inode),
2954 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2956 } while (exception.retry);
2961 * This is just for mknod. open(O_CREAT) will always do ->open_context().
2964 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2967 struct nfs_open_context *ctx;
2968 struct nfs4_state *state;
2971 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2973 return PTR_ERR(ctx);
2975 sattr->ia_mode &= ~current_umask();
2976 state = nfs4_do_open(dir, dentry, ctx->mode,
2977 flags, sattr, ctx->cred,
2978 &ctx->mdsthreshold);
2980 if (IS_ERR(state)) {
2981 status = PTR_ERR(state);
2984 d_add(dentry, igrab(state->inode));
2985 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2988 put_nfs_open_context(ctx);
2992 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2994 struct nfs_server *server = NFS_SERVER(dir);
2995 struct nfs_removeargs args = {
2999 struct nfs_removeres res = {
3002 struct rpc_message msg = {
3003 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3009 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3011 update_changeattr(dir, &res.cinfo);
3015 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3017 struct nfs4_exception exception = { };
3020 err = nfs4_handle_exception(NFS_SERVER(dir),
3021 _nfs4_proc_remove(dir, name),
3023 } while (exception.retry);
3027 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3029 struct nfs_server *server = NFS_SERVER(dir);
3030 struct nfs_removeargs *args = msg->rpc_argp;
3031 struct nfs_removeres *res = msg->rpc_resp;
3033 res->server = server;
3034 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3035 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
3038 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3040 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
3041 &data->args.seq_args,
3045 rpc_call_start(task);
3048 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3050 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3052 if (!nfs4_sequence_done(task, &res->seq_res))
3054 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3056 update_changeattr(dir, &res->cinfo);
3060 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3062 struct nfs_server *server = NFS_SERVER(dir);
3063 struct nfs_renameargs *arg = msg->rpc_argp;
3064 struct nfs_renameres *res = msg->rpc_resp;
3066 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3067 res->server = server;
3068 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
3071 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3073 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3074 &data->args.seq_args,
3078 rpc_call_start(task);
3081 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3082 struct inode *new_dir)
3084 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3086 if (!nfs4_sequence_done(task, &res->seq_res))
3088 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3091 update_changeattr(old_dir, &res->old_cinfo);
3092 update_changeattr(new_dir, &res->new_cinfo);
3096 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3097 struct inode *new_dir, struct qstr *new_name)
3099 struct nfs_server *server = NFS_SERVER(old_dir);
3100 struct nfs_renameargs arg = {
3101 .old_dir = NFS_FH(old_dir),
3102 .new_dir = NFS_FH(new_dir),
3103 .old_name = old_name,
3104 .new_name = new_name,
3106 struct nfs_renameres res = {
3109 struct rpc_message msg = {
3110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3114 int status = -ENOMEM;
3116 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3118 update_changeattr(old_dir, &res.old_cinfo);
3119 update_changeattr(new_dir, &res.new_cinfo);
3124 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3125 struct inode *new_dir, struct qstr *new_name)
3127 struct nfs4_exception exception = { };
3130 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3131 _nfs4_proc_rename(old_dir, old_name,
3134 } while (exception.retry);
3138 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3140 struct nfs_server *server = NFS_SERVER(inode);
3141 struct nfs4_link_arg arg = {
3142 .fh = NFS_FH(inode),
3143 .dir_fh = NFS_FH(dir),
3145 .bitmask = server->attr_bitmask,
3147 struct nfs4_link_res res = {
3150 struct rpc_message msg = {
3151 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3155 int status = -ENOMEM;
3157 res.fattr = nfs_alloc_fattr();
3158 if (res.fattr == NULL)
3161 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3163 update_changeattr(dir, &res.cinfo);
3164 nfs_post_op_update_inode(inode, res.fattr);
3167 nfs_free_fattr(res.fattr);
3171 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3173 struct nfs4_exception exception = { };
3176 err = nfs4_handle_exception(NFS_SERVER(inode),
3177 _nfs4_proc_link(inode, dir, name),
3179 } while (exception.retry);
3183 struct nfs4_createdata {
3184 struct rpc_message msg;
3185 struct nfs4_create_arg arg;
3186 struct nfs4_create_res res;
3188 struct nfs_fattr fattr;
3191 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3192 struct qstr *name, struct iattr *sattr, u32 ftype)
3194 struct nfs4_createdata *data;
3196 data = kzalloc(sizeof(*data), GFP_KERNEL);
3198 struct nfs_server *server = NFS_SERVER(dir);
3200 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3201 data->msg.rpc_argp = &data->arg;
3202 data->msg.rpc_resp = &data->res;
3203 data->arg.dir_fh = NFS_FH(dir);
3204 data->arg.server = server;
3205 data->arg.name = name;
3206 data->arg.attrs = sattr;
3207 data->arg.ftype = ftype;
3208 data->arg.bitmask = server->attr_bitmask;
3209 data->res.server = server;
3210 data->res.fh = &data->fh;
3211 data->res.fattr = &data->fattr;
3212 nfs_fattr_init(data->res.fattr);
3217 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3219 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3220 &data->arg.seq_args, &data->res.seq_res, 1);
3222 update_changeattr(dir, &data->res.dir_cinfo);
3223 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3228 static void nfs4_free_createdata(struct nfs4_createdata *data)
3233 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3234 struct page *page, unsigned int len, struct iattr *sattr)
3236 struct nfs4_createdata *data;
3237 int status = -ENAMETOOLONG;
3239 if (len > NFS4_MAXPATHLEN)
3243 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3247 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3248 data->arg.u.symlink.pages = &page;
3249 data->arg.u.symlink.len = len;
3251 status = nfs4_do_create(dir, dentry, data);
3253 nfs4_free_createdata(data);
3258 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3259 struct page *page, unsigned int len, struct iattr *sattr)
3261 struct nfs4_exception exception = { };
3264 err = nfs4_handle_exception(NFS_SERVER(dir),
3265 _nfs4_proc_symlink(dir, dentry, page,
3268 } while (exception.retry);
3272 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3273 struct iattr *sattr)
3275 struct nfs4_createdata *data;
3276 int status = -ENOMEM;
3278 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3282 status = nfs4_do_create(dir, dentry, data);
3284 nfs4_free_createdata(data);
3289 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3290 struct iattr *sattr)
3292 struct nfs4_exception exception = { };
3295 sattr->ia_mode &= ~current_umask();
3297 err = nfs4_handle_exception(NFS_SERVER(dir),
3298 _nfs4_proc_mkdir(dir, dentry, sattr),
3300 } while (exception.retry);
3304 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3305 u64 cookie, struct page **pages, unsigned int count, int plus)
3307 struct inode *dir = dentry->d_inode;
3308 struct nfs4_readdir_arg args = {
3313 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3316 struct nfs4_readdir_res res;
3317 struct rpc_message msg = {
3318 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3325 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3326 dentry->d_parent->d_name.name,
3327 dentry->d_name.name,
3328 (unsigned long long)cookie);
3329 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3330 res.pgbase = args.pgbase;
3331 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3333 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3334 status += args.pgbase;
3337 nfs_invalidate_atime(dir);
3339 dprintk("%s: returns %d\n", __func__, status);
3343 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3344 u64 cookie, struct page **pages, unsigned int count, int plus)
3346 struct nfs4_exception exception = { };
3349 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3350 _nfs4_proc_readdir(dentry, cred, cookie,
3351 pages, count, plus),
3353 } while (exception.retry);
3357 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3358 struct iattr *sattr, dev_t rdev)
3360 struct nfs4_createdata *data;
3361 int mode = sattr->ia_mode;
3362 int status = -ENOMEM;
3364 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3369 data->arg.ftype = NF4FIFO;
3370 else if (S_ISBLK(mode)) {
3371 data->arg.ftype = NF4BLK;
3372 data->arg.u.device.specdata1 = MAJOR(rdev);
3373 data->arg.u.device.specdata2 = MINOR(rdev);
3375 else if (S_ISCHR(mode)) {
3376 data->arg.ftype = NF4CHR;
3377 data->arg.u.device.specdata1 = MAJOR(rdev);
3378 data->arg.u.device.specdata2 = MINOR(rdev);
3379 } else if (!S_ISSOCK(mode)) {
3384 status = nfs4_do_create(dir, dentry, data);
3386 nfs4_free_createdata(data);
3391 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3392 struct iattr *sattr, dev_t rdev)
3394 struct nfs4_exception exception = { };
3397 sattr->ia_mode &= ~current_umask();
3399 err = nfs4_handle_exception(NFS_SERVER(dir),
3400 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3402 } while (exception.retry);
3406 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3407 struct nfs_fsstat *fsstat)
3409 struct nfs4_statfs_arg args = {
3411 .bitmask = server->attr_bitmask,
3413 struct nfs4_statfs_res res = {
3416 struct rpc_message msg = {
3417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3422 nfs_fattr_init(fsstat->fattr);
3423 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3426 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3428 struct nfs4_exception exception = { };
3431 err = nfs4_handle_exception(server,
3432 _nfs4_proc_statfs(server, fhandle, fsstat),
3434 } while (exception.retry);
3438 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3439 struct nfs_fsinfo *fsinfo)
3441 struct nfs4_fsinfo_arg args = {
3443 .bitmask = server->attr_bitmask,
3445 struct nfs4_fsinfo_res res = {
3448 struct rpc_message msg = {
3449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3454 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3457 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3459 struct nfs4_exception exception = { };
3463 err = nfs4_handle_exception(server,
3464 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3466 } while (exception.retry);
3470 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3474 nfs_fattr_init(fsinfo->fattr);
3475 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3477 /* block layout checks this! */
3478 server->pnfs_blksize = fsinfo->blksize;
3479 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3485 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3486 struct nfs_pathconf *pathconf)
3488 struct nfs4_pathconf_arg args = {
3490 .bitmask = server->attr_bitmask,
3492 struct nfs4_pathconf_res res = {
3493 .pathconf = pathconf,
3495 struct rpc_message msg = {
3496 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3501 /* None of the pathconf attributes are mandatory to implement */
3502 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3503 memset(pathconf, 0, sizeof(*pathconf));
3507 nfs_fattr_init(pathconf->fattr);
3508 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3511 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3512 struct nfs_pathconf *pathconf)
3514 struct nfs4_exception exception = { };
3518 err = nfs4_handle_exception(server,
3519 _nfs4_proc_pathconf(server, fhandle, pathconf),
3521 } while (exception.retry);
3525 void __nfs4_read_done_cb(struct nfs_read_data *data)
3527 nfs_invalidate_atime(data->header->inode);
3530 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3532 struct nfs_server *server = NFS_SERVER(data->header->inode);
3534 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3535 rpc_restart_call_prepare(task);
3539 __nfs4_read_done_cb(data);
3540 if (task->tk_status > 0)
3541 renew_lease(server, data->timestamp);
3545 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3548 dprintk("--> %s\n", __func__);
3550 if (!nfs4_sequence_done(task, &data->res.seq_res))
3553 return data->read_done_cb ? data->read_done_cb(task, data) :
3554 nfs4_read_done_cb(task, data);
3557 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3559 data->timestamp = jiffies;
3560 data->read_done_cb = nfs4_read_done_cb;
3561 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3562 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3565 static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3567 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3568 &data->args.seq_args,
3572 rpc_call_start(task);
3575 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3577 struct inode *inode = data->header->inode;
3579 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3580 rpc_restart_call_prepare(task);
3583 if (task->tk_status >= 0) {
3584 renew_lease(NFS_SERVER(inode), data->timestamp);
3585 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
3590 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3592 if (!nfs4_sequence_done(task, &data->res.seq_res))
3594 return data->write_done_cb ? data->write_done_cb(task, data) :
3595 nfs4_write_done_cb(task, data);
3599 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3601 const struct nfs_pgio_header *hdr = data->header;
3603 /* Don't request attributes for pNFS or O_DIRECT writes */
3604 if (data->ds_clp != NULL || hdr->dreq != NULL)
3606 /* Otherwise, request attributes if and only if we don't hold
3609 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
3612 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3614 struct nfs_server *server = NFS_SERVER(data->header->inode);
3616 if (!nfs4_write_need_cache_consistency_data(data)) {
3617 data->args.bitmask = NULL;
3618 data->res.fattr = NULL;
3620 data->args.bitmask = server->cache_consistency_bitmask;
3622 if (!data->write_done_cb)
3623 data->write_done_cb = nfs4_write_done_cb;
3624 data->res.server = server;
3625 data->timestamp = jiffies;
3627 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3628 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3631 static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3633 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3634 &data->args.seq_args,
3638 rpc_call_start(task);
3641 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3643 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3644 &data->args.seq_args,
3648 rpc_call_start(task);
3651 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
3653 struct inode *inode = data->inode;
3655 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3656 rpc_restart_call_prepare(task);
3662 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
3664 if (!nfs4_sequence_done(task, &data->res.seq_res))
3666 return data->commit_done_cb(task, data);
3669 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
3671 struct nfs_server *server = NFS_SERVER(data->inode);
3673 if (data->commit_done_cb == NULL)
3674 data->commit_done_cb = nfs4_commit_done_cb;
3675 data->res.server = server;
3676 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3677 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3680 struct nfs4_renewdata {
3681 struct nfs_client *client;
3682 unsigned long timestamp;
3686 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3687 * standalone procedure for queueing an asynchronous RENEW.
3689 static void nfs4_renew_release(void *calldata)
3691 struct nfs4_renewdata *data = calldata;
3692 struct nfs_client *clp = data->client;
3694 if (atomic_read(&clp->cl_count) > 1)
3695 nfs4_schedule_state_renewal(clp);
3696 nfs_put_client(clp);
3700 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3702 struct nfs4_renewdata *data = calldata;
3703 struct nfs_client *clp = data->client;
3704 unsigned long timestamp = data->timestamp;
3706 if (task->tk_status < 0) {
3707 /* Unless we're shutting down, schedule state recovery! */
3708 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3710 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3711 nfs4_schedule_lease_recovery(clp);
3714 nfs4_schedule_path_down_recovery(clp);
3716 do_renew_lease(clp, timestamp);
3719 static const struct rpc_call_ops nfs4_renew_ops = {
3720 .rpc_call_done = nfs4_renew_done,
3721 .rpc_release = nfs4_renew_release,
3724 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3726 struct rpc_message msg = {
3727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3731 struct nfs4_renewdata *data;
3733 if (renew_flags == 0)
3735 if (!atomic_inc_not_zero(&clp->cl_count))
3737 data = kmalloc(sizeof(*data), GFP_NOFS);
3741 data->timestamp = jiffies;
3742 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3743 &nfs4_renew_ops, data);
3746 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3748 struct rpc_message msg = {
3749 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3753 unsigned long now = jiffies;
3756 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3759 do_renew_lease(clp, now);
3763 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3765 return (server->caps & NFS_CAP_ACLS)
3766 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3767 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3770 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3771 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
3774 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
3776 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3777 struct page **pages, unsigned int *pgbase)
3779 struct page *newpage, **spages;
3785 len = min_t(size_t, PAGE_SIZE, buflen);
3786 newpage = alloc_page(GFP_KERNEL);
3788 if (newpage == NULL)
3790 memcpy(page_address(newpage), buf, len);
3795 } while (buflen != 0);
3801 __free_page(spages[rc-1]);
3805 struct nfs4_cached_acl {
3811 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3813 struct nfs_inode *nfsi = NFS_I(inode);
3815 spin_lock(&inode->i_lock);
3816 kfree(nfsi->nfs4_acl);
3817 nfsi->nfs4_acl = acl;
3818 spin_unlock(&inode->i_lock);
3821 static void nfs4_zap_acl_attr(struct inode *inode)
3823 nfs4_set_cached_acl(inode, NULL);
3826 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3828 struct nfs_inode *nfsi = NFS_I(inode);
3829 struct nfs4_cached_acl *acl;
3832 spin_lock(&inode->i_lock);
3833 acl = nfsi->nfs4_acl;
3836 if (buf == NULL) /* user is just asking for length */
3838 if (acl->cached == 0)
3840 ret = -ERANGE; /* see getxattr(2) man page */
3841 if (acl->len > buflen)
3843 memcpy(buf, acl->data, acl->len);
3847 spin_unlock(&inode->i_lock);
3851 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3853 struct nfs4_cached_acl *acl;
3854 size_t buflen = sizeof(*acl) + acl_len;
3856 if (buflen <= PAGE_SIZE) {
3857 acl = kmalloc(buflen, GFP_KERNEL);
3861 _copy_from_pages(acl->data, pages, pgbase, acl_len);
3863 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3870 nfs4_set_cached_acl(inode, acl);
3874 * The getxattr API returns the required buffer length when called with a
3875 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3876 * the required buf. On a NULL buf, we send a page of data to the server
3877 * guessing that the ACL request can be serviced by a page. If so, we cache
3878 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3879 * the cache. If not so, we throw away the page, and cache the required
3880 * length. The next getxattr call will then produce another round trip to
3881 * the server, this time with the input buf of the required size.
3883 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3885 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3886 struct nfs_getaclargs args = {
3887 .fh = NFS_FH(inode),
3891 struct nfs_getaclres res = {
3894 struct rpc_message msg = {
3895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3899 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3900 int ret = -ENOMEM, i;
3902 /* As long as we're doing a round trip to the server anyway,
3903 * let's be prepared for a page of acl data. */
3906 if (npages > ARRAY_SIZE(pages))
3909 for (i = 0; i < npages; i++) {
3910 pages[i] = alloc_page(GFP_KERNEL);
3915 /* for decoding across pages */
3916 res.acl_scratch = alloc_page(GFP_KERNEL);
3917 if (!res.acl_scratch)
3920 args.acl_len = npages * PAGE_SIZE;
3921 args.acl_pgbase = 0;
3923 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3924 __func__, buf, buflen, npages, args.acl_len);
3925 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3926 &msg, &args.seq_args, &res.seq_res, 0);
3930 /* Handle the case where the passed-in buffer is too short */
3931 if (res.acl_flags & NFS4_ACL_TRUNC) {
3932 /* Did the user only issue a request for the acl length? */
3938 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3940 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3944 for (i = 0; i < npages; i++)
3946 __free_page(pages[i]);
3947 if (res.acl_scratch)
3948 __free_page(res.acl_scratch);
3952 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3954 struct nfs4_exception exception = { };
3957 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3960 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3961 } while (exception.retry);
3965 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3967 struct nfs_server *server = NFS_SERVER(inode);
3970 if (!nfs4_server_supports_acls(server))
3972 ret = nfs_revalidate_inode(server, inode);
3975 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3976 nfs_zap_acl_cache(inode);
3977 ret = nfs4_read_cached_acl(inode, buf, buflen);
3979 /* -ENOENT is returned if there is no ACL or if there is an ACL
3980 * but no cached acl data, just the acl length */
3982 return nfs4_get_acl_uncached(inode, buf, buflen);
3985 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3987 struct nfs_server *server = NFS_SERVER(inode);
3988 struct page *pages[NFS4ACL_MAXPAGES];
3989 struct nfs_setaclargs arg = {
3990 .fh = NFS_FH(inode),
3994 struct nfs_setaclres res;
3995 struct rpc_message msg = {
3996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4000 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4003 if (!nfs4_server_supports_acls(server))
4005 if (npages > ARRAY_SIZE(pages))
4007 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4010 nfs4_inode_return_delegation(inode);
4011 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4014 * Free each page after tx, so the only ref left is
4015 * held by the network stack
4018 put_page(pages[i-1]);
4021 * Acl update can result in inode attribute update.
4022 * so mark the attribute cache invalid.
4024 spin_lock(&inode->i_lock);
4025 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4026 spin_unlock(&inode->i_lock);
4027 nfs_access_zap_cache(inode);
4028 nfs_zap_acl_cache(inode);
4032 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4034 struct nfs4_exception exception = { };
4037 err = nfs4_handle_exception(NFS_SERVER(inode),
4038 __nfs4_proc_set_acl(inode, buf, buflen),
4040 } while (exception.retry);
4045 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
4047 struct nfs_client *clp = server->nfs_client;
4049 if (task->tk_status >= 0)
4051 switch(task->tk_status) {
4052 case -NFS4ERR_DELEG_REVOKED:
4053 case -NFS4ERR_ADMIN_REVOKED:
4054 case -NFS4ERR_BAD_STATEID:
4057 nfs_remove_bad_delegation(state->inode);
4058 case -NFS4ERR_OPENMODE:
4061 nfs4_schedule_stateid_recovery(server, state);
4062 goto wait_on_recovery;
4063 case -NFS4ERR_EXPIRED:
4065 nfs4_schedule_stateid_recovery(server, state);
4066 case -NFS4ERR_STALE_STATEID:
4067 case -NFS4ERR_STALE_CLIENTID:
4068 nfs4_schedule_lease_recovery(clp);
4069 goto wait_on_recovery;
4070 #if defined(CONFIG_NFS_V4_1)
4071 case -NFS4ERR_BADSESSION:
4072 case -NFS4ERR_BADSLOT:
4073 case -NFS4ERR_BAD_HIGH_SLOT:
4074 case -NFS4ERR_DEADSESSION:
4075 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4076 case -NFS4ERR_SEQ_FALSE_RETRY:
4077 case -NFS4ERR_SEQ_MISORDERED:
4078 dprintk("%s ERROR %d, Reset session\n", __func__,
4080 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4081 task->tk_status = 0;
4083 #endif /* CONFIG_NFS_V4_1 */
4084 case -NFS4ERR_DELAY:
4085 nfs_inc_server_stats(server, NFSIOS_DELAY);
4086 case -NFS4ERR_GRACE:
4088 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4089 task->tk_status = 0;
4091 case -NFS4ERR_RETRY_UNCACHED_REP:
4092 case -NFS4ERR_OLD_STATEID:
4093 task->tk_status = 0;
4096 task->tk_status = nfs4_map_errors(task->tk_status);
4099 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4100 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4101 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4102 task->tk_status = 0;
4106 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4107 nfs4_verifier *bootverf)
4111 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4112 /* An impossible timestamp guarantees this value
4113 * will never match a generated boot time. */
4115 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4117 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4118 verf[0] = (__be32)nn->boot_time.tv_sec;
4119 verf[1] = (__be32)nn->boot_time.tv_nsec;
4121 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4125 nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4126 char *buf, size_t len)
4128 unsigned int result;
4131 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4133 rpc_peeraddr2str(clp->cl_rpcclient,
4135 rpc_peeraddr2str(clp->cl_rpcclient,
4136 RPC_DISPLAY_PROTO));
4142 nfs4_init_uniform_client_string(const struct nfs_client *clp,
4143 char *buf, size_t len)
4145 char *nodename = clp->cl_rpcclient->cl_nodename;
4147 if (nfs4_client_id_uniquifier[0] != '\0')
4148 nodename = nfs4_client_id_uniquifier;
4149 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4150 clp->rpc_ops->version, clp->cl_minorversion,
4155 * nfs4_proc_setclientid - Negotiate client ID
4156 * @clp: state data structure
4157 * @program: RPC program for NFSv4 callback service
4158 * @port: IP port number for NFS4 callback service
4159 * @cred: RPC credential to use for this call
4160 * @res: where to place the result
4162 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4164 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4165 unsigned short port, struct rpc_cred *cred,
4166 struct nfs4_setclientid_res *res)
4168 nfs4_verifier sc_verifier;
4169 struct nfs4_setclientid setclientid = {
4170 .sc_verifier = &sc_verifier,
4172 .sc_cb_ident = clp->cl_cb_ident,
4174 struct rpc_message msg = {
4175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4176 .rpc_argp = &setclientid,
4182 /* nfs_client_id4 */
4183 nfs4_init_boot_verifier(clp, &sc_verifier);
4184 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4185 setclientid.sc_name_len =
4186 nfs4_init_uniform_client_string(clp,
4187 setclientid.sc_name,
4188 sizeof(setclientid.sc_name));
4190 setclientid.sc_name_len =
4191 nfs4_init_nonuniform_client_string(clp,
4192 setclientid.sc_name,
4193 sizeof(setclientid.sc_name));
4196 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4197 sizeof(setclientid.sc_netid),
4198 rpc_peeraddr2str(clp->cl_rpcclient,
4199 RPC_DISPLAY_NETID));
4201 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4202 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4203 clp->cl_ipaddr, port >> 8, port & 255);
4205 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4206 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4207 setclientid.sc_name_len, setclientid.sc_name);
4208 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4209 dprintk("NFS reply setclientid: %d\n", status);
4214 * nfs4_proc_setclientid_confirm - Confirm client ID
4215 * @clp: state data structure
4216 * @res: result of a previous SETCLIENTID
4217 * @cred: RPC credential to use for this call
4219 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4221 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4222 struct nfs4_setclientid_res *arg,
4223 struct rpc_cred *cred)
4225 struct nfs_fsinfo fsinfo;
4226 struct rpc_message msg = {
4227 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4229 .rpc_resp = &fsinfo,
4235 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4236 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4239 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4241 spin_lock(&clp->cl_lock);
4242 clp->cl_lease_time = fsinfo.lease_time * HZ;
4243 clp->cl_last_renewal = now;
4244 spin_unlock(&clp->cl_lock);
4246 dprintk("NFS reply setclientid_confirm: %d\n", status);
4250 struct nfs4_delegreturndata {
4251 struct nfs4_delegreturnargs args;
4252 struct nfs4_delegreturnres res;
4254 nfs4_stateid stateid;
4255 unsigned long timestamp;
4256 struct nfs_fattr fattr;
4260 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4262 struct nfs4_delegreturndata *data = calldata;
4264 if (!nfs4_sequence_done(task, &data->res.seq_res))
4267 switch (task->tk_status) {
4268 case -NFS4ERR_STALE_STATEID:
4269 case -NFS4ERR_EXPIRED:
4271 renew_lease(data->res.server, data->timestamp);
4274 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4276 rpc_restart_call_prepare(task);
4280 data->rpc_status = task->tk_status;
4283 static void nfs4_delegreturn_release(void *calldata)
4288 #if defined(CONFIG_NFS_V4_1)
4289 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4291 struct nfs4_delegreturndata *d_data;
4293 d_data = (struct nfs4_delegreturndata *)data;
4295 if (nfs4_setup_sequence(d_data->res.server,
4296 &d_data->args.seq_args,
4297 &d_data->res.seq_res, task))
4299 rpc_call_start(task);
4301 #endif /* CONFIG_NFS_V4_1 */
4303 static const struct rpc_call_ops nfs4_delegreturn_ops = {
4304 #if defined(CONFIG_NFS_V4_1)
4305 .rpc_call_prepare = nfs4_delegreturn_prepare,
4306 #endif /* CONFIG_NFS_V4_1 */
4307 .rpc_call_done = nfs4_delegreturn_done,
4308 .rpc_release = nfs4_delegreturn_release,
4311 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4313 struct nfs4_delegreturndata *data;
4314 struct nfs_server *server = NFS_SERVER(inode);
4315 struct rpc_task *task;
4316 struct rpc_message msg = {
4317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4320 struct rpc_task_setup task_setup_data = {
4321 .rpc_client = server->client,
4322 .rpc_message = &msg,
4323 .callback_ops = &nfs4_delegreturn_ops,
4324 .flags = RPC_TASK_ASYNC,
4328 data = kzalloc(sizeof(*data), GFP_NOFS);
4331 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4332 data->args.fhandle = &data->fh;
4333 data->args.stateid = &data->stateid;
4334 data->args.bitmask = server->cache_consistency_bitmask;
4335 nfs_copy_fh(&data->fh, NFS_FH(inode));
4336 nfs4_stateid_copy(&data->stateid, stateid);
4337 data->res.fattr = &data->fattr;
4338 data->res.server = server;
4339 nfs_fattr_init(data->res.fattr);
4340 data->timestamp = jiffies;
4341 data->rpc_status = 0;
4343 task_setup_data.callback_data = data;
4344 msg.rpc_argp = &data->args;
4345 msg.rpc_resp = &data->res;
4346 task = rpc_run_task(&task_setup_data);
4348 return PTR_ERR(task);
4351 status = nfs4_wait_for_completion_rpc_task(task);
4354 status = data->rpc_status;
4356 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4358 nfs_refresh_inode(inode, &data->fattr);
4364 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4366 struct nfs_server *server = NFS_SERVER(inode);
4367 struct nfs4_exception exception = { };
4370 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4372 case -NFS4ERR_STALE_STATEID:
4373 case -NFS4ERR_EXPIRED:
4377 err = nfs4_handle_exception(server, err, &exception);
4378 } while (exception.retry);
4382 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4383 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4386 * sleep, with exponential backoff, and retry the LOCK operation.
4388 static unsigned long
4389 nfs4_set_lock_task_retry(unsigned long timeout)
4391 freezable_schedule_timeout_killable(timeout);
4393 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4394 return NFS4_LOCK_MAXTIMEOUT;
4398 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4400 struct inode *inode = state->inode;
4401 struct nfs_server *server = NFS_SERVER(inode);
4402 struct nfs_client *clp = server->nfs_client;
4403 struct nfs_lockt_args arg = {
4404 .fh = NFS_FH(inode),
4407 struct nfs_lockt_res res = {
4410 struct rpc_message msg = {
4411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4414 .rpc_cred = state->owner->so_cred,
4416 struct nfs4_lock_state *lsp;
4419 arg.lock_owner.clientid = clp->cl_clientid;
4420 status = nfs4_set_lock_state(state, request);
4423 lsp = request->fl_u.nfs4_fl.owner;
4424 arg.lock_owner.id = lsp->ls_seqid.owner_id;
4425 arg.lock_owner.s_dev = server->s_dev;
4426 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4429 request->fl_type = F_UNLCK;
4431 case -NFS4ERR_DENIED:
4434 request->fl_ops->fl_release_private(request);
4439 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4441 struct nfs4_exception exception = { };
4445 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4446 _nfs4_proc_getlk(state, cmd, request),
4448 } while (exception.retry);
4452 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4455 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4457 res = posix_lock_file_wait(file, fl);
4460 res = flock_lock_file_wait(file, fl);
4468 struct nfs4_unlockdata {
4469 struct nfs_locku_args arg;
4470 struct nfs_locku_res res;
4471 struct nfs4_lock_state *lsp;
4472 struct nfs_open_context *ctx;
4473 struct file_lock fl;
4474 const struct nfs_server *server;
4475 unsigned long timestamp;
4478 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4479 struct nfs_open_context *ctx,
4480 struct nfs4_lock_state *lsp,
4481 struct nfs_seqid *seqid)
4483 struct nfs4_unlockdata *p;
4484 struct inode *inode = lsp->ls_state->inode;
4486 p = kzalloc(sizeof(*p), GFP_NOFS);
4489 p->arg.fh = NFS_FH(inode);
4491 p->arg.seqid = seqid;
4492 p->res.seqid = seqid;
4493 p->arg.stateid = &lsp->ls_stateid;
4495 atomic_inc(&lsp->ls_count);
4496 /* Ensure we don't close file until we're done freeing locks! */
4497 p->ctx = get_nfs_open_context(ctx);
4498 memcpy(&p->fl, fl, sizeof(p->fl));
4499 p->server = NFS_SERVER(inode);
4503 static void nfs4_locku_release_calldata(void *data)
4505 struct nfs4_unlockdata *calldata = data;
4506 nfs_free_seqid(calldata->arg.seqid);
4507 nfs4_put_lock_state(calldata->lsp);
4508 put_nfs_open_context(calldata->ctx);
4512 static void nfs4_locku_done(struct rpc_task *task, void *data)
4514 struct nfs4_unlockdata *calldata = data;
4516 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4518 switch (task->tk_status) {
4520 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4521 &calldata->res.stateid);
4522 renew_lease(calldata->server, calldata->timestamp);
4524 case -NFS4ERR_BAD_STATEID:
4525 case -NFS4ERR_OLD_STATEID:
4526 case -NFS4ERR_STALE_STATEID:
4527 case -NFS4ERR_EXPIRED:
4530 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4531 rpc_restart_call_prepare(task);
4533 nfs_release_seqid(calldata->arg.seqid);
4536 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4538 struct nfs4_unlockdata *calldata = data;
4540 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4542 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
4543 /* Note: exit _without_ running nfs4_locku_done */
4544 task->tk_action = NULL;
4547 calldata->timestamp = jiffies;
4548 if (nfs4_setup_sequence(calldata->server,
4549 &calldata->arg.seq_args,
4550 &calldata->res.seq_res,
4552 nfs_release_seqid(calldata->arg.seqid);
4554 rpc_call_start(task);
4557 static const struct rpc_call_ops nfs4_locku_ops = {
4558 .rpc_call_prepare = nfs4_locku_prepare,
4559 .rpc_call_done = nfs4_locku_done,
4560 .rpc_release = nfs4_locku_release_calldata,
4563 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4564 struct nfs_open_context *ctx,
4565 struct nfs4_lock_state *lsp,
4566 struct nfs_seqid *seqid)
4568 struct nfs4_unlockdata *data;
4569 struct rpc_message msg = {
4570 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4571 .rpc_cred = ctx->cred,
4573 struct rpc_task_setup task_setup_data = {
4574 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4575 .rpc_message = &msg,
4576 .callback_ops = &nfs4_locku_ops,
4577 .workqueue = nfsiod_workqueue,
4578 .flags = RPC_TASK_ASYNC,
4581 /* Ensure this is an unlock - when canceling a lock, the
4582 * canceled lock is passed in, and it won't be an unlock.
4584 fl->fl_type = F_UNLCK;
4586 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4588 nfs_free_seqid(seqid);
4589 return ERR_PTR(-ENOMEM);
4592 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4593 msg.rpc_argp = &data->arg;
4594 msg.rpc_resp = &data->res;
4595 task_setup_data.callback_data = data;
4596 return rpc_run_task(&task_setup_data);
4599 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4601 struct nfs_inode *nfsi = NFS_I(state->inode);
4602 struct nfs_seqid *seqid;
4603 struct nfs4_lock_state *lsp;
4604 struct rpc_task *task;
4606 unsigned char fl_flags = request->fl_flags;
4608 status = nfs4_set_lock_state(state, request);
4609 /* Unlock _before_ we do the RPC call */
4610 request->fl_flags |= FL_EXISTS;
4611 down_read(&nfsi->rwsem);
4612 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4613 up_read(&nfsi->rwsem);
4616 up_read(&nfsi->rwsem);
4619 /* Is this a delegated lock? */
4620 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4622 lsp = request->fl_u.nfs4_fl.owner;
4623 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4627 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4628 status = PTR_ERR(task);
4631 status = nfs4_wait_for_completion_rpc_task(task);
4634 request->fl_flags = fl_flags;
4638 struct nfs4_lockdata {
4639 struct nfs_lock_args arg;
4640 struct nfs_lock_res res;
4641 struct nfs4_lock_state *lsp;
4642 struct nfs_open_context *ctx;
4643 struct file_lock fl;
4644 unsigned long timestamp;
4647 struct nfs_server *server;
4650 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4651 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4654 struct nfs4_lockdata *p;
4655 struct inode *inode = lsp->ls_state->inode;
4656 struct nfs_server *server = NFS_SERVER(inode);
4658 p = kzalloc(sizeof(*p), gfp_mask);
4662 p->arg.fh = NFS_FH(inode);
4664 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4665 if (p->arg.open_seqid == NULL)
4667 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4668 if (p->arg.lock_seqid == NULL)
4669 goto out_free_seqid;
4670 p->arg.lock_stateid = &lsp->ls_stateid;
4671 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4672 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4673 p->arg.lock_owner.s_dev = server->s_dev;
4674 p->res.lock_seqid = p->arg.lock_seqid;
4677 atomic_inc(&lsp->ls_count);
4678 p->ctx = get_nfs_open_context(ctx);
4679 memcpy(&p->fl, fl, sizeof(p->fl));
4682 nfs_free_seqid(p->arg.open_seqid);
4688 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4690 struct nfs4_lockdata *data = calldata;
4691 struct nfs4_state *state = data->lsp->ls_state;
4693 dprintk("%s: begin!\n", __func__);
4694 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4696 /* Do we need to do an open_to_lock_owner? */
4697 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4698 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4699 goto out_release_lock_seqid;
4700 data->arg.open_stateid = &state->stateid;
4701 data->arg.new_lock_owner = 1;
4702 data->res.open_seqid = data->arg.open_seqid;
4704 data->arg.new_lock_owner = 0;
4705 data->timestamp = jiffies;
4706 if (nfs4_setup_sequence(data->server,
4707 &data->arg.seq_args,
4710 rpc_call_start(task);
4713 nfs_release_seqid(data->arg.open_seqid);
4714 out_release_lock_seqid:
4715 nfs_release_seqid(data->arg.lock_seqid);
4716 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
4719 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4721 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4722 nfs4_lock_prepare(task, calldata);
4725 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4727 struct nfs4_lockdata *data = calldata;
4729 dprintk("%s: begin!\n", __func__);
4731 if (!nfs4_sequence_done(task, &data->res.seq_res))
4734 data->rpc_status = task->tk_status;
4735 if (data->arg.new_lock_owner != 0) {
4736 if (data->rpc_status == 0)
4737 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4741 if (data->rpc_status == 0) {
4742 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4743 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
4744 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4747 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4750 static void nfs4_lock_release(void *calldata)
4752 struct nfs4_lockdata *data = calldata;
4754 dprintk("%s: begin!\n", __func__);
4755 nfs_free_seqid(data->arg.open_seqid);
4756 if (data->cancelled != 0) {
4757 struct rpc_task *task;
4758 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4759 data->arg.lock_seqid);
4761 rpc_put_task_async(task);
4762 dprintk("%s: cancelling lock!\n", __func__);
4764 nfs_free_seqid(data->arg.lock_seqid);
4765 nfs4_put_lock_state(data->lsp);
4766 put_nfs_open_context(data->ctx);
4768 dprintk("%s: done!\n", __func__);
4771 static const struct rpc_call_ops nfs4_lock_ops = {
4772 .rpc_call_prepare = nfs4_lock_prepare,
4773 .rpc_call_done = nfs4_lock_done,
4774 .rpc_release = nfs4_lock_release,
4777 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4778 .rpc_call_prepare = nfs4_recover_lock_prepare,
4779 .rpc_call_done = nfs4_lock_done,
4780 .rpc_release = nfs4_lock_release,
4783 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4786 case -NFS4ERR_ADMIN_REVOKED:
4787 case -NFS4ERR_BAD_STATEID:
4788 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4789 if (new_lock_owner != 0 ||
4790 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
4791 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4793 case -NFS4ERR_STALE_STATEID:
4794 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4795 case -NFS4ERR_EXPIRED:
4796 nfs4_schedule_lease_recovery(server->nfs_client);
4800 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4802 struct nfs4_lockdata *data;
4803 struct rpc_task *task;
4804 struct rpc_message msg = {
4805 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4806 .rpc_cred = state->owner->so_cred,
4808 struct rpc_task_setup task_setup_data = {
4809 .rpc_client = NFS_CLIENT(state->inode),
4810 .rpc_message = &msg,
4811 .callback_ops = &nfs4_lock_ops,
4812 .workqueue = nfsiod_workqueue,
4813 .flags = RPC_TASK_ASYNC,
4817 dprintk("%s: begin!\n", __func__);
4818 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4819 fl->fl_u.nfs4_fl.owner,
4820 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4824 data->arg.block = 1;
4825 if (recovery_type > NFS_LOCK_NEW) {
4826 if (recovery_type == NFS_LOCK_RECLAIM)
4827 data->arg.reclaim = NFS_LOCK_RECLAIM;
4828 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4830 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4831 msg.rpc_argp = &data->arg;
4832 msg.rpc_resp = &data->res;
4833 task_setup_data.callback_data = data;
4834 task = rpc_run_task(&task_setup_data);
4836 return PTR_ERR(task);
4837 ret = nfs4_wait_for_completion_rpc_task(task);
4839 ret = data->rpc_status;
4841 nfs4_handle_setlk_error(data->server, data->lsp,
4842 data->arg.new_lock_owner, ret);
4844 data->cancelled = 1;
4846 dprintk("%s: done, ret = %d!\n", __func__, ret);
4850 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4852 struct nfs_server *server = NFS_SERVER(state->inode);
4853 struct nfs4_exception exception = {
4854 .inode = state->inode,
4859 /* Cache the lock if possible... */
4860 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4862 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4863 if (err != -NFS4ERR_DELAY)
4865 nfs4_handle_exception(server, err, &exception);
4866 } while (exception.retry);
4870 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4872 struct nfs_server *server = NFS_SERVER(state->inode);
4873 struct nfs4_exception exception = {
4874 .inode = state->inode,
4878 err = nfs4_set_lock_state(state, request);
4882 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4884 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4888 case -NFS4ERR_GRACE:
4889 case -NFS4ERR_DELAY:
4890 nfs4_handle_exception(server, err, &exception);
4893 } while (exception.retry);
4898 #if defined(CONFIG_NFS_V4_1)
4900 * nfs41_check_expired_locks - possibly free a lock stateid
4902 * @state: NFSv4 state for an inode
4904 * Returns NFS_OK if recovery for this stateid is now finished.
4905 * Otherwise a negative NFS4ERR value is returned.
4907 static int nfs41_check_expired_locks(struct nfs4_state *state)
4909 int status, ret = -NFS4ERR_BAD_STATEID;
4910 struct nfs4_lock_state *lsp;
4911 struct nfs_server *server = NFS_SERVER(state->inode);
4913 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4914 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
4915 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4916 if (status != NFS_OK) {
4917 /* Free the stateid unless the server
4918 * informs us the stateid is unrecognized. */
4919 if (status != -NFS4ERR_BAD_STATEID)
4920 nfs41_free_stateid(server,
4922 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
4931 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4933 int status = NFS_OK;
4935 if (test_bit(LK_STATE_IN_USE, &state->flags))
4936 status = nfs41_check_expired_locks(state);
4937 if (status != NFS_OK)
4938 status = nfs4_lock_expired(state, request);
4943 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4945 struct nfs_inode *nfsi = NFS_I(state->inode);
4946 unsigned char fl_flags = request->fl_flags;
4947 int status = -ENOLCK;
4949 if ((fl_flags & FL_POSIX) &&
4950 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4952 /* Is this a delegated open? */
4953 status = nfs4_set_lock_state(state, request);
4956 request->fl_flags |= FL_ACCESS;
4957 status = do_vfs_lock(request->fl_file, request);
4960 down_read(&nfsi->rwsem);
4961 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4962 /* Yes: cache locks! */
4963 /* ...but avoid races with delegation recall... */
4964 request->fl_flags = fl_flags & ~FL_SLEEP;
4965 status = do_vfs_lock(request->fl_file, request);
4968 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4971 /* Note: we always want to sleep here! */
4972 request->fl_flags = fl_flags | FL_SLEEP;
4973 if (do_vfs_lock(request->fl_file, request) < 0)
4974 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4975 "manager!\n", __func__);
4977 up_read(&nfsi->rwsem);
4979 request->fl_flags = fl_flags;
4983 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4985 struct nfs4_exception exception = {
4987 .inode = state->inode,
4992 err = _nfs4_proc_setlk(state, cmd, request);
4993 if (err == -NFS4ERR_DENIED)
4995 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4997 } while (exception.retry);
5002 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5004 struct nfs_open_context *ctx;
5005 struct nfs4_state *state;
5006 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5009 /* verify open state */
5010 ctx = nfs_file_open_context(filp);
5013 if (request->fl_start < 0 || request->fl_end < 0)
5016 if (IS_GETLK(cmd)) {
5018 return nfs4_proc_getlk(state, F_GETLK, request);
5022 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5025 if (request->fl_type == F_UNLCK) {
5027 return nfs4_proc_unlck(state, cmd, request);
5034 * Don't rely on the VFS having checked the file open mode,
5035 * since it won't do this for flock() locks.
5037 switch (request->fl_type) {
5039 if (!(filp->f_mode & FMODE_READ))
5043 if (!(filp->f_mode & FMODE_WRITE))
5048 status = nfs4_proc_setlk(state, cmd, request);
5049 if ((status != -EAGAIN) || IS_SETLK(cmd))
5051 timeout = nfs4_set_lock_task_retry(timeout);
5052 status = -ERESTARTSYS;
5055 } while(status < 0);
5059 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5061 struct nfs_server *server = NFS_SERVER(state->inode);
5062 struct nfs4_exception exception = { };
5065 err = nfs4_set_lock_state(state, fl);
5069 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
5072 printk(KERN_ERR "NFS: %s: unhandled error "
5073 "%d.\n", __func__, err);
5077 case -NFS4ERR_EXPIRED:
5078 nfs4_schedule_stateid_recovery(server, state);
5079 case -NFS4ERR_STALE_CLIENTID:
5080 case -NFS4ERR_STALE_STATEID:
5081 nfs4_schedule_lease_recovery(server->nfs_client);
5083 case -NFS4ERR_BADSESSION:
5084 case -NFS4ERR_BADSLOT:
5085 case -NFS4ERR_BAD_HIGH_SLOT:
5086 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5087 case -NFS4ERR_DEADSESSION:
5088 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
5092 * The show must go on: exit, but mark the
5093 * stateid as needing recovery.
5095 case -NFS4ERR_DELEG_REVOKED:
5096 case -NFS4ERR_ADMIN_REVOKED:
5097 case -NFS4ERR_BAD_STATEID:
5098 case -NFS4ERR_OPENMODE:
5099 nfs4_schedule_stateid_recovery(server, state);
5104 * User RPCSEC_GSS context has expired.
5105 * We cannot recover this stateid now, so
5106 * skip it and allow recovery thread to
5112 case -NFS4ERR_DENIED:
5113 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5116 case -NFS4ERR_DELAY:
5119 err = nfs4_handle_exception(server, err, &exception);
5120 } while (exception.retry);
5125 struct nfs_release_lockowner_data {
5126 struct nfs4_lock_state *lsp;
5127 struct nfs_server *server;
5128 struct nfs_release_lockowner_args args;
5131 static void nfs4_release_lockowner_release(void *calldata)
5133 struct nfs_release_lockowner_data *data = calldata;
5134 nfs4_free_lock_state(data->server, data->lsp);
5138 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5139 .rpc_release = nfs4_release_lockowner_release,
5142 int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
5144 struct nfs_server *server = lsp->ls_state->owner->so_server;
5145 struct nfs_release_lockowner_data *data;
5146 struct rpc_message msg = {
5147 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5150 if (server->nfs_client->cl_mvops->minor_version != 0)
5152 data = kmalloc(sizeof(*data), GFP_NOFS);
5156 data->server = server;
5157 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5158 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5159 data->args.lock_owner.s_dev = server->s_dev;
5160 msg.rpc_argp = &data->args;
5161 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5165 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5167 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5168 const void *buf, size_t buflen,
5169 int flags, int type)
5171 if (strcmp(key, "") != 0)
5174 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5177 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5178 void *buf, size_t buflen, int type)
5180 if (strcmp(key, "") != 0)
5183 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5186 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5187 size_t list_len, const char *name,
5188 size_t name_len, int type)
5190 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5192 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5195 if (list && len <= list_len)
5196 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5201 * nfs_fhget will use either the mounted_on_fileid or the fileid
5203 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5205 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5206 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5207 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
5208 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
5211 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
5212 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
5213 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5217 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5218 const struct qstr *name,
5219 struct nfs4_fs_locations *fs_locations,
5222 struct nfs_server *server = NFS_SERVER(dir);
5224 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
5226 struct nfs4_fs_locations_arg args = {
5227 .dir_fh = NFS_FH(dir),
5232 struct nfs4_fs_locations_res res = {
5233 .fs_locations = fs_locations,
5235 struct rpc_message msg = {
5236 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5242 dprintk("%s: start\n", __func__);
5244 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5245 * is not supported */
5246 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5247 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5249 bitmask[0] |= FATTR4_WORD0_FILEID;
5251 nfs_fattr_init(&fs_locations->fattr);
5252 fs_locations->server = server;
5253 fs_locations->nlocations = 0;
5254 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
5255 dprintk("%s: returned status = %d\n", __func__, status);
5259 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5260 const struct qstr *name,
5261 struct nfs4_fs_locations *fs_locations,
5264 struct nfs4_exception exception = { };
5267 err = nfs4_handle_exception(NFS_SERVER(dir),
5268 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
5270 } while (exception.retry);
5274 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5277 struct nfs4_secinfo_arg args = {
5278 .dir_fh = NFS_FH(dir),
5281 struct nfs4_secinfo_res res = {
5284 struct rpc_message msg = {
5285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5290 dprintk("NFS call secinfo %s\n", name->name);
5291 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5292 dprintk("NFS reply secinfo: %d\n", status);
5296 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5297 struct nfs4_secinfo_flavors *flavors)
5299 struct nfs4_exception exception = { };
5302 err = nfs4_handle_exception(NFS_SERVER(dir),
5303 _nfs4_proc_secinfo(dir, name, flavors),
5305 } while (exception.retry);
5309 #ifdef CONFIG_NFS_V4_1
5311 * Check the exchange flags returned by the server for invalid flags, having
5312 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5315 static int nfs4_check_cl_exchange_flags(u32 flags)
5317 if (flags & ~EXCHGID4_FLAG_MASK_R)
5319 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5320 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5322 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5326 return -NFS4ERR_INVAL;
5330 nfs41_same_server_scope(struct nfs41_server_scope *a,
5331 struct nfs41_server_scope *b)
5333 if (a->server_scope_sz == b->server_scope_sz &&
5334 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5341 * nfs4_proc_bind_conn_to_session()
5343 * The 4.1 client currently uses the same TCP connection for the
5344 * fore and backchannel.
5346 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
5349 struct nfs41_bind_conn_to_session_res res;
5350 struct rpc_message msg = {
5352 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5358 dprintk("--> %s\n", __func__);
5360 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5361 if (unlikely(res.session == NULL)) {
5366 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5368 if (memcmp(res.session->sess_id.data,
5369 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5370 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5374 if (res.dir != NFS4_CDFS4_BOTH) {
5375 dprintk("NFS: %s: Unexpected direction from server\n",
5380 if (res.use_conn_in_rdma_mode) {
5381 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5390 dprintk("<-- %s status= %d\n", __func__, status);
5395 * nfs4_proc_exchange_id()
5397 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5399 * Since the clientid has expired, all compounds using sessions
5400 * associated with the stale clientid will be returning
5401 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5402 * be in some phase of session reset.
5404 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
5406 nfs4_verifier verifier;
5407 struct nfs41_exchange_id_args args = {
5408 .verifier = &verifier,
5410 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
5412 struct nfs41_exchange_id_res res = {
5416 struct rpc_message msg = {
5417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5423 nfs4_init_boot_verifier(clp, &verifier);
5424 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5426 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5427 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5428 args.id_len, args.id);
5430 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
5432 if (unlikely(res.server_owner == NULL)) {
5437 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
5439 if (unlikely(res.server_scope == NULL)) {
5441 goto out_server_owner;
5444 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
5445 if (unlikely(res.impl_id == NULL)) {
5447 goto out_server_scope;
5450 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5452 status = nfs4_check_cl_exchange_flags(res.flags);
5455 clp->cl_clientid = res.clientid;
5456 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5457 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5458 clp->cl_seqid = res.seqid;
5460 kfree(clp->cl_serverowner);
5461 clp->cl_serverowner = res.server_owner;
5462 res.server_owner = NULL;
5464 /* use the most recent implementation id */
5465 kfree(clp->cl_implid);
5466 clp->cl_implid = res.impl_id;
5468 if (clp->cl_serverscope != NULL &&
5469 !nfs41_same_server_scope(clp->cl_serverscope,
5470 res.server_scope)) {
5471 dprintk("%s: server_scope mismatch detected\n",
5473 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5474 kfree(clp->cl_serverscope);
5475 clp->cl_serverscope = NULL;
5478 if (clp->cl_serverscope == NULL) {
5479 clp->cl_serverscope = res.server_scope;
5486 kfree(res.server_owner);
5488 kfree(res.server_scope);
5490 if (clp->cl_implid != NULL)
5491 dprintk("NFS reply exchange_id: Server Implementation ID: "
5492 "domain: %s, name: %s, date: %llu,%u\n",
5493 clp->cl_implid->domain, clp->cl_implid->name,
5494 clp->cl_implid->date.seconds,
5495 clp->cl_implid->date.nseconds);
5496 dprintk("NFS reply exchange_id: %d\n", status);
5500 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5501 struct rpc_cred *cred)
5503 struct rpc_message msg = {
5504 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5510 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5512 dprintk("NFS: Got error %d from the server %s on "
5513 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5517 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5518 struct rpc_cred *cred)
5523 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5524 ret = _nfs4_proc_destroy_clientid(clp, cred);
5526 case -NFS4ERR_DELAY:
5527 case -NFS4ERR_CLIENTID_BUSY:
5537 int nfs4_destroy_clientid(struct nfs_client *clp)
5539 struct rpc_cred *cred;
5542 if (clp->cl_mvops->minor_version < 1)
5544 if (clp->cl_exchange_flags == 0)
5546 if (clp->cl_preserve_clid)
5548 cred = nfs4_get_exchange_id_cred(clp);
5549 ret = nfs4_proc_destroy_clientid(clp, cred);
5554 case -NFS4ERR_STALE_CLIENTID:
5555 clp->cl_exchange_flags = 0;
5561 struct nfs4_get_lease_time_data {
5562 struct nfs4_get_lease_time_args *args;
5563 struct nfs4_get_lease_time_res *res;
5564 struct nfs_client *clp;
5567 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5571 struct nfs4_get_lease_time_data *data =
5572 (struct nfs4_get_lease_time_data *)calldata;
5574 dprintk("--> %s\n", __func__);
5575 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5576 /* just setup sequence, do not trigger session recovery
5577 since we're invoked within one */
5578 ret = nfs41_setup_sequence(data->clp->cl_session,
5579 &data->args->la_seq_args,
5580 &data->res->lr_seq_res, task);
5583 rpc_call_start(task);
5584 dprintk("<-- %s\n", __func__);
5588 * Called from nfs4_state_manager thread for session setup, so don't recover
5589 * from sequence operation or clientid errors.
5591 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5593 struct nfs4_get_lease_time_data *data =
5594 (struct nfs4_get_lease_time_data *)calldata;
5596 dprintk("--> %s\n", __func__);
5597 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5599 switch (task->tk_status) {
5600 case -NFS4ERR_DELAY:
5601 case -NFS4ERR_GRACE:
5602 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5603 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5604 task->tk_status = 0;
5606 case -NFS4ERR_RETRY_UNCACHED_REP:
5607 rpc_restart_call_prepare(task);
5610 dprintk("<-- %s\n", __func__);
5613 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5614 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5615 .rpc_call_done = nfs4_get_lease_time_done,
5618 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5620 struct rpc_task *task;
5621 struct nfs4_get_lease_time_args args;
5622 struct nfs4_get_lease_time_res res = {
5623 .lr_fsinfo = fsinfo,
5625 struct nfs4_get_lease_time_data data = {
5630 struct rpc_message msg = {
5631 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5635 struct rpc_task_setup task_setup = {
5636 .rpc_client = clp->cl_rpcclient,
5637 .rpc_message = &msg,
5638 .callback_ops = &nfs4_get_lease_time_ops,
5639 .callback_data = &data,
5640 .flags = RPC_TASK_TIMEOUT,
5644 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5645 dprintk("--> %s\n", __func__);
5646 task = rpc_run_task(&task_setup);
5649 status = PTR_ERR(task);
5651 status = task->tk_status;
5654 dprintk("<-- %s return %d\n", __func__, status);
5659 static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5661 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5664 static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5665 struct nfs4_slot *new,
5669 struct nfs4_slot *old = NULL;
5672 spin_lock(&tbl->slot_tbl_lock);
5676 tbl->max_slots = max_slots;
5678 tbl->highest_used_slotid = NFS4_NO_SLOT;
5679 for (i = 0; i < tbl->max_slots; i++)
5680 tbl->slots[i].seq_nr = ivalue;
5681 spin_unlock(&tbl->slot_tbl_lock);
5686 * (re)Initialise a slot table
5688 static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5691 struct nfs4_slot *new = NULL;
5694 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5695 max_reqs, tbl->max_slots);
5697 /* Does the newly negotiated max_reqs match the existing slot table? */
5698 if (max_reqs != tbl->max_slots) {
5699 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
5705 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
5706 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5707 tbl, tbl->slots, tbl->max_slots);
5709 dprintk("<-- %s: return %d\n", __func__, ret);
5713 /* Destroy the slot table */
5714 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5716 if (session->fc_slot_table.slots != NULL) {
5717 kfree(session->fc_slot_table.slots);
5718 session->fc_slot_table.slots = NULL;
5720 if (session->bc_slot_table.slots != NULL) {
5721 kfree(session->bc_slot_table.slots);
5722 session->bc_slot_table.slots = NULL;
5728 * Initialize or reset the forechannel and backchannel tables
5730 static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
5732 struct nfs4_slot_table *tbl;
5735 dprintk("--> %s\n", __func__);
5737 tbl = &ses->fc_slot_table;
5738 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5739 if (status) /* -ENOMEM */
5742 tbl = &ses->bc_slot_table;
5743 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5744 if (status && tbl->slots == NULL)
5745 /* Fore and back channel share a connection so get
5746 * both slot tables or neither */
5747 nfs4_destroy_slot_tables(ses);
5751 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5753 struct nfs4_session *session;
5754 struct nfs4_slot_table *tbl;
5756 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5760 tbl = &session->fc_slot_table;
5761 tbl->highest_used_slotid = NFS4_NO_SLOT;
5762 spin_lock_init(&tbl->slot_tbl_lock);
5763 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5764 init_completion(&tbl->complete);
5766 tbl = &session->bc_slot_table;
5767 tbl->highest_used_slotid = NFS4_NO_SLOT;
5768 spin_lock_init(&tbl->slot_tbl_lock);
5769 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5770 init_completion(&tbl->complete);
5772 session->session_state = 1<<NFS4_SESSION_INITING;
5778 void nfs4_destroy_session(struct nfs4_session *session)
5780 struct rpc_xprt *xprt;
5781 struct rpc_cred *cred;
5783 cred = nfs4_get_exchange_id_cred(session->clp);
5784 nfs4_proc_destroy_session(session, cred);
5789 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5791 dprintk("%s Destroy backchannel for xprt %p\n",
5793 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
5794 nfs4_destroy_slot_tables(session);
5799 * Initialize the values to be used by the client in CREATE_SESSION
5800 * If nfs4_init_session set the fore channel request and response sizes,
5803 * Set the back channel max_resp_sz_cached to zero to force the client to
5804 * always set csa_cachethis to FALSE because the current implementation
5805 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5807 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5809 struct nfs4_session *session = args->client->cl_session;
5810 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5811 mxresp_sz = session->fc_target_max_resp_sz;
5814 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5816 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5817 /* Fore channel attributes */
5818 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5819 args->fc_attrs.max_resp_sz = mxresp_sz;
5820 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5821 args->fc_attrs.max_reqs = max_session_slots;
5823 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5824 "max_ops=%u max_reqs=%u\n",
5826 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5827 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5829 /* Back channel attributes */
5830 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5831 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5832 args->bc_attrs.max_resp_sz_cached = 0;
5833 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5834 args->bc_attrs.max_reqs = 1;
5836 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5837 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5839 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5840 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5841 args->bc_attrs.max_reqs);
5844 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5846 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5847 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5849 if (rcvd->max_resp_sz > sent->max_resp_sz)
5852 * Our requested max_ops is the minimum we need; we're not
5853 * prepared to break up compounds into smaller pieces than that.
5854 * So, no point even trying to continue if the server won't
5857 if (rcvd->max_ops < sent->max_ops)
5859 if (rcvd->max_reqs == 0)
5861 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5862 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5866 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5868 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5869 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5871 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5873 if (rcvd->max_resp_sz < sent->max_resp_sz)
5875 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5877 /* These would render the backchannel useless: */
5878 if (rcvd->max_ops != sent->max_ops)
5880 if (rcvd->max_reqs != sent->max_reqs)
5885 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5886 struct nfs4_session *session)
5890 ret = nfs4_verify_fore_channel_attrs(args, session);
5893 return nfs4_verify_back_channel_attrs(args, session);
5896 static int _nfs4_proc_create_session(struct nfs_client *clp,
5897 struct rpc_cred *cred)
5899 struct nfs4_session *session = clp->cl_session;
5900 struct nfs41_create_session_args args = {
5902 .cb_program = NFS4_CALLBACK,
5904 struct nfs41_create_session_res res = {
5907 struct rpc_message msg = {
5908 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5915 nfs4_init_channel_attrs(&args);
5916 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5918 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5921 /* Verify the session's negotiated channel_attrs values */
5922 status = nfs4_verify_channel_attrs(&args, session);
5923 /* Increment the clientid slot sequence id */
5931 * Issues a CREATE_SESSION operation to the server.
5932 * It is the responsibility of the caller to verify the session is
5933 * expired before calling this routine.
5935 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
5939 struct nfs4_session *session = clp->cl_session;
5941 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5943 status = _nfs4_proc_create_session(clp, cred);
5947 /* Init or reset the session slot tables */
5948 status = nfs4_setup_session_slot_tables(session);
5949 dprintk("slot table setup returned %d\n", status);
5953 ptr = (unsigned *)&session->sess_id.data[0];
5954 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5955 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5957 dprintk("<-- %s\n", __func__);
5962 * Issue the over-the-wire RPC DESTROY_SESSION.
5963 * The caller must serialize access to this routine.
5965 int nfs4_proc_destroy_session(struct nfs4_session *session,
5966 struct rpc_cred *cred)
5968 struct rpc_message msg = {
5969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5970 .rpc_argp = session,
5975 dprintk("--> nfs4_proc_destroy_session\n");
5977 /* session is still being setup */
5978 if (session->clp->cl_cons_state != NFS_CS_READY)
5981 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5984 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
5985 "Session has been destroyed regardless...\n", status);
5987 dprintk("<-- nfs4_proc_destroy_session\n");
5992 * With sessions, the client is not marked ready until after a
5993 * successful EXCHANGE_ID and CREATE_SESSION.
5995 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5996 * other versions of NFS can be tried.
5998 static int nfs41_check_session_ready(struct nfs_client *clp)
6002 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
6003 ret = nfs4_client_recover_expired_lease(clp);
6007 if (clp->cl_cons_state < NFS_CS_READY)
6008 return -EPROTONOSUPPORT;
6013 int nfs4_init_session(struct nfs_server *server)
6015 struct nfs_client *clp = server->nfs_client;
6016 struct nfs4_session *session;
6017 unsigned int target_max_rqst_sz = NFS_MAX_FILE_IO_SIZE;
6018 unsigned int target_max_resp_sz = NFS_MAX_FILE_IO_SIZE;
6020 if (!nfs4_has_session(clp))
6023 if (server->rsize != 0)
6024 target_max_resp_sz = server->rsize;
6025 target_max_resp_sz += nfs41_maxread_overhead;
6027 if (server->wsize != 0)
6028 target_max_rqst_sz = server->wsize;
6029 target_max_rqst_sz += nfs41_maxwrite_overhead;
6031 session = clp->cl_session;
6032 spin_lock(&clp->cl_lock);
6033 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6034 /* Initialise targets and channel attributes */
6035 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6036 session->fc_attrs.max_rqst_sz = target_max_rqst_sz;
6037 session->fc_target_max_resp_sz = target_max_resp_sz;
6038 session->fc_attrs.max_resp_sz = target_max_resp_sz;
6040 /* Just adjust the targets */
6041 if (target_max_rqst_sz > session->fc_target_max_rqst_sz) {
6042 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6043 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6045 if (target_max_resp_sz > session->fc_target_max_resp_sz) {
6046 session->fc_target_max_resp_sz = target_max_resp_sz;
6047 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6050 spin_unlock(&clp->cl_lock);
6052 if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
6053 nfs4_schedule_lease_recovery(clp);
6055 return nfs41_check_session_ready(clp);
6058 int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
6060 struct nfs4_session *session = clp->cl_session;
6063 spin_lock(&clp->cl_lock);
6064 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6066 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
6067 * DS lease to be equal to the MDS lease.
6069 clp->cl_lease_time = lease_time;
6070 clp->cl_last_renewal = jiffies;
6072 spin_unlock(&clp->cl_lock);
6074 ret = nfs41_check_session_ready(clp);
6077 /* Test for the DS role */
6078 if (!is_ds_client(clp))
6082 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
6086 * Renew the cl_session lease.
6088 struct nfs4_sequence_data {
6089 struct nfs_client *clp;
6090 struct nfs4_sequence_args args;
6091 struct nfs4_sequence_res res;
6094 static void nfs41_sequence_release(void *data)
6096 struct nfs4_sequence_data *calldata = data;
6097 struct nfs_client *clp = calldata->clp;
6099 if (atomic_read(&clp->cl_count) > 1)
6100 nfs4_schedule_state_renewal(clp);
6101 nfs_put_client(clp);
6105 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6107 switch(task->tk_status) {
6108 case -NFS4ERR_DELAY:
6109 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6112 nfs4_schedule_lease_recovery(clp);
6117 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
6119 struct nfs4_sequence_data *calldata = data;
6120 struct nfs_client *clp = calldata->clp;
6122 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6125 if (task->tk_status < 0) {
6126 dprintk("%s ERROR %d\n", __func__, task->tk_status);
6127 if (atomic_read(&clp->cl_count) == 1)
6130 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6131 rpc_restart_call_prepare(task);
6135 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
6137 dprintk("<-- %s\n", __func__);
6140 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6142 struct nfs4_sequence_data *calldata = data;
6143 struct nfs_client *clp = calldata->clp;
6144 struct nfs4_sequence_args *args;
6145 struct nfs4_sequence_res *res;
6147 args = task->tk_msg.rpc_argp;
6148 res = task->tk_msg.rpc_resp;
6150 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
6152 rpc_call_start(task);
6155 static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
6157 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6158 nfs41_sequence_prepare(task, data);
6161 static const struct rpc_call_ops nfs41_sequence_ops = {
6162 .rpc_call_done = nfs41_sequence_call_done,
6163 .rpc_call_prepare = nfs41_sequence_prepare,
6164 .rpc_release = nfs41_sequence_release,
6167 static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
6168 .rpc_call_done = nfs41_sequence_call_done,
6169 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
6170 .rpc_release = nfs41_sequence_release,
6173 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
6174 const struct rpc_call_ops *seq_ops)
6176 struct nfs4_sequence_data *calldata;
6177 struct rpc_message msg = {
6178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6181 struct rpc_task_setup task_setup_data = {
6182 .rpc_client = clp->cl_rpcclient,
6183 .rpc_message = &msg,
6184 .callback_ops = seq_ops,
6185 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6188 if (!atomic_inc_not_zero(&clp->cl_count))
6189 return ERR_PTR(-EIO);
6190 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6191 if (calldata == NULL) {
6192 nfs_put_client(clp);
6193 return ERR_PTR(-ENOMEM);
6195 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
6196 msg.rpc_argp = &calldata->args;
6197 msg.rpc_resp = &calldata->res;
6198 calldata->clp = clp;
6199 task_setup_data.callback_data = calldata;
6201 return rpc_run_task(&task_setup_data);
6204 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
6206 struct rpc_task *task;
6209 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6211 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
6213 ret = PTR_ERR(task);
6215 rpc_put_task_async(task);
6216 dprintk("<-- %s status=%d\n", __func__, ret);
6220 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6222 struct rpc_task *task;
6225 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
6227 ret = PTR_ERR(task);
6230 ret = rpc_wait_for_completion_task(task);
6232 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6234 if (task->tk_status == 0)
6235 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
6236 ret = task->tk_status;
6240 dprintk("<-- %s status=%d\n", __func__, ret);
6244 struct nfs4_reclaim_complete_data {
6245 struct nfs_client *clp;
6246 struct nfs41_reclaim_complete_args arg;
6247 struct nfs41_reclaim_complete_res res;
6250 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6252 struct nfs4_reclaim_complete_data *calldata = data;
6254 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6255 if (nfs41_setup_sequence(calldata->clp->cl_session,
6256 &calldata->arg.seq_args,
6257 &calldata->res.seq_res, task))
6260 rpc_call_start(task);
6263 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6265 switch(task->tk_status) {
6267 case -NFS4ERR_COMPLETE_ALREADY:
6268 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6270 case -NFS4ERR_DELAY:
6271 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6273 case -NFS4ERR_RETRY_UNCACHED_REP:
6276 nfs4_schedule_lease_recovery(clp);
6281 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6283 struct nfs4_reclaim_complete_data *calldata = data;
6284 struct nfs_client *clp = calldata->clp;
6285 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6287 dprintk("--> %s\n", __func__);
6288 if (!nfs41_sequence_done(task, res))
6291 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6292 rpc_restart_call_prepare(task);
6295 dprintk("<-- %s\n", __func__);
6298 static void nfs4_free_reclaim_complete_data(void *data)
6300 struct nfs4_reclaim_complete_data *calldata = data;
6305 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6306 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6307 .rpc_call_done = nfs4_reclaim_complete_done,
6308 .rpc_release = nfs4_free_reclaim_complete_data,
6312 * Issue a global reclaim complete.
6314 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6316 struct nfs4_reclaim_complete_data *calldata;
6317 struct rpc_task *task;
6318 struct rpc_message msg = {
6319 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6321 struct rpc_task_setup task_setup_data = {
6322 .rpc_client = clp->cl_rpcclient,
6323 .rpc_message = &msg,
6324 .callback_ops = &nfs4_reclaim_complete_call_ops,
6325 .flags = RPC_TASK_ASYNC,
6327 int status = -ENOMEM;
6329 dprintk("--> %s\n", __func__);
6330 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6331 if (calldata == NULL)
6333 calldata->clp = clp;
6334 calldata->arg.one_fs = 0;
6336 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
6337 msg.rpc_argp = &calldata->arg;
6338 msg.rpc_resp = &calldata->res;
6339 task_setup_data.callback_data = calldata;
6340 task = rpc_run_task(&task_setup_data);
6342 status = PTR_ERR(task);
6345 status = nfs4_wait_for_completion_rpc_task(task);
6347 status = task->tk_status;
6351 dprintk("<-- %s status=%d\n", __func__, status);
6356 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6358 struct nfs4_layoutget *lgp = calldata;
6359 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6361 dprintk("--> %s\n", __func__);
6362 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6363 * right now covering the LAYOUTGET we are about to send.
6364 * However, that is not so catastrophic, and there seems
6365 * to be no way to prevent it completely.
6367 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
6368 &lgp->res.seq_res, task))
6370 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6371 NFS_I(lgp->args.inode)->layout,
6372 lgp->args.ctx->state)) {
6373 rpc_exit(task, NFS4_OK);
6376 rpc_call_start(task);
6379 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6381 struct nfs4_layoutget *lgp = calldata;
6382 struct inode *inode = lgp->args.inode;
6383 struct nfs_server *server = NFS_SERVER(inode);
6384 struct pnfs_layout_hdr *lo;
6385 struct nfs4_state *state = NULL;
6387 dprintk("--> %s\n", __func__);
6389 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6392 switch (task->tk_status) {
6395 case -NFS4ERR_LAYOUTTRYLATER:
6396 case -NFS4ERR_RECALLCONFLICT:
6397 task->tk_status = -NFS4ERR_DELAY;
6399 case -NFS4ERR_EXPIRED:
6400 case -NFS4ERR_BAD_STATEID:
6401 spin_lock(&inode->i_lock);
6402 lo = NFS_I(inode)->layout;
6403 if (!lo || list_empty(&lo->plh_segs)) {
6404 spin_unlock(&inode->i_lock);
6405 /* If the open stateid was bad, then recover it. */
6406 state = lgp->args.ctx->state;
6410 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6411 spin_unlock(&inode->i_lock);
6412 /* Mark the bad layout state as invalid, then
6413 * retry using the open stateid. */
6414 pnfs_free_lseg_list(&head);
6417 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6418 rpc_restart_call_prepare(task);
6420 dprintk("<-- %s\n", __func__);
6423 static size_t max_response_pages(struct nfs_server *server)
6425 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6426 return nfs_page_array_len(0, max_resp_sz);
6429 static void nfs4_free_pages(struct page **pages, size_t size)
6436 for (i = 0; i < size; i++) {
6439 __free_page(pages[i]);
6444 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6446 struct page **pages;
6449 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6451 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6455 for (i = 0; i < size; i++) {
6456 pages[i] = alloc_page(gfp_flags);
6458 dprintk("%s: failed to allocate page\n", __func__);
6459 nfs4_free_pages(pages, size);
6467 static void nfs4_layoutget_release(void *calldata)
6469 struct nfs4_layoutget *lgp = calldata;
6470 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6471 size_t max_pages = max_response_pages(server);
6473 dprintk("--> %s\n", __func__);
6474 nfs4_free_pages(lgp->args.layout.pages, max_pages);
6475 put_nfs_open_context(lgp->args.ctx);
6477 dprintk("<-- %s\n", __func__);
6480 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6481 .rpc_call_prepare = nfs4_layoutget_prepare,
6482 .rpc_call_done = nfs4_layoutget_done,
6483 .rpc_release = nfs4_layoutget_release,
6486 struct pnfs_layout_segment *
6487 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
6489 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6490 size_t max_pages = max_response_pages(server);
6491 struct rpc_task *task;
6492 struct rpc_message msg = {
6493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6494 .rpc_argp = &lgp->args,
6495 .rpc_resp = &lgp->res,
6497 struct rpc_task_setup task_setup_data = {
6498 .rpc_client = server->client,
6499 .rpc_message = &msg,
6500 .callback_ops = &nfs4_layoutget_call_ops,
6501 .callback_data = lgp,
6502 .flags = RPC_TASK_ASYNC,
6504 struct pnfs_layout_segment *lseg = NULL;
6507 dprintk("--> %s\n", __func__);
6509 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6510 if (!lgp->args.layout.pages) {
6511 nfs4_layoutget_release(lgp);
6512 return ERR_PTR(-ENOMEM);
6514 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6516 lgp->res.layoutp = &lgp->args.layout;
6517 lgp->res.seq_res.sr_slot = NULL;
6518 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
6519 task = rpc_run_task(&task_setup_data);
6521 return ERR_CAST(task);
6522 status = nfs4_wait_for_completion_rpc_task(task);
6524 status = task->tk_status;
6526 lseg = pnfs_layout_process(lgp);
6528 dprintk("<-- %s status=%d\n", __func__, status);
6530 return ERR_PTR(status);
6535 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6537 struct nfs4_layoutreturn *lrp = calldata;
6539 dprintk("--> %s\n", __func__);
6540 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
6541 &lrp->res.seq_res, task))
6543 rpc_call_start(task);
6546 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6548 struct nfs4_layoutreturn *lrp = calldata;
6549 struct nfs_server *server;
6551 dprintk("--> %s\n", __func__);
6553 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6556 server = NFS_SERVER(lrp->args.inode);
6557 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6558 rpc_restart_call_prepare(task);
6561 dprintk("<-- %s\n", __func__);
6564 static void nfs4_layoutreturn_release(void *calldata)
6566 struct nfs4_layoutreturn *lrp = calldata;
6567 struct pnfs_layout_hdr *lo = lrp->args.layout;
6569 dprintk("--> %s\n", __func__);
6570 spin_lock(&lo->plh_inode->i_lock);
6571 if (lrp->res.lrs_present)
6572 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6573 lo->plh_block_lgets--;
6574 spin_unlock(&lo->plh_inode->i_lock);
6575 pnfs_put_layout_hdr(lrp->args.layout);
6577 dprintk("<-- %s\n", __func__);
6580 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6581 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6582 .rpc_call_done = nfs4_layoutreturn_done,
6583 .rpc_release = nfs4_layoutreturn_release,
6586 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6588 struct rpc_task *task;
6589 struct rpc_message msg = {
6590 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6591 .rpc_argp = &lrp->args,
6592 .rpc_resp = &lrp->res,
6594 struct rpc_task_setup task_setup_data = {
6595 .rpc_client = lrp->clp->cl_rpcclient,
6596 .rpc_message = &msg,
6597 .callback_ops = &nfs4_layoutreturn_call_ops,
6598 .callback_data = lrp,
6602 dprintk("--> %s\n", __func__);
6603 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
6604 task = rpc_run_task(&task_setup_data);
6606 return PTR_ERR(task);
6607 status = task->tk_status;
6608 dprintk("<-- %s status=%d\n", __func__, status);
6614 * Retrieve the list of Data Server devices from the MDS.
6616 static int _nfs4_getdevicelist(struct nfs_server *server,
6617 const struct nfs_fh *fh,
6618 struct pnfs_devicelist *devlist)
6620 struct nfs4_getdevicelist_args args = {
6622 .layoutclass = server->pnfs_curr_ld->id,
6624 struct nfs4_getdevicelist_res res = {
6627 struct rpc_message msg = {
6628 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6634 dprintk("--> %s\n", __func__);
6635 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6637 dprintk("<-- %s status=%d\n", __func__, status);
6641 int nfs4_proc_getdevicelist(struct nfs_server *server,
6642 const struct nfs_fh *fh,
6643 struct pnfs_devicelist *devlist)
6645 struct nfs4_exception exception = { };
6649 err = nfs4_handle_exception(server,
6650 _nfs4_getdevicelist(server, fh, devlist),
6652 } while (exception.retry);
6654 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6655 err, devlist->num_devs);
6659 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6662 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6664 struct nfs4_getdeviceinfo_args args = {
6667 struct nfs4_getdeviceinfo_res res = {
6670 struct rpc_message msg = {
6671 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6677 dprintk("--> %s\n", __func__);
6678 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6679 dprintk("<-- %s status=%d\n", __func__, status);
6684 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6686 struct nfs4_exception exception = { };
6690 err = nfs4_handle_exception(server,
6691 _nfs4_proc_getdeviceinfo(server, pdev),
6693 } while (exception.retry);
6696 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6698 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6700 struct nfs4_layoutcommit_data *data = calldata;
6701 struct nfs_server *server = NFS_SERVER(data->args.inode);
6703 if (nfs4_setup_sequence(server, &data->args.seq_args,
6704 &data->res.seq_res, task))
6706 rpc_call_start(task);
6710 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6712 struct nfs4_layoutcommit_data *data = calldata;
6713 struct nfs_server *server = NFS_SERVER(data->args.inode);
6715 if (!nfs4_sequence_done(task, &data->res.seq_res))
6718 switch (task->tk_status) { /* Just ignore these failures */
6719 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6720 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6721 case -NFS4ERR_BADLAYOUT: /* no layout */
6722 case -NFS4ERR_GRACE: /* loca_recalim always false */
6723 task->tk_status = 0;
6726 nfs_post_op_update_inode_force_wcc(data->args.inode,
6730 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6731 rpc_restart_call_prepare(task);
6737 static void nfs4_layoutcommit_release(void *calldata)
6739 struct nfs4_layoutcommit_data *data = calldata;
6740 struct pnfs_layout_segment *lseg, *tmp;
6741 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
6743 pnfs_cleanup_layoutcommit(data);
6744 /* Matched by references in pnfs_set_layoutcommit */
6745 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6746 list_del_init(&lseg->pls_lc_list);
6747 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6749 pnfs_put_lseg(lseg);
6752 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6753 smp_mb__after_clear_bit();
6754 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6756 put_rpccred(data->cred);
6760 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6761 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6762 .rpc_call_done = nfs4_layoutcommit_done,
6763 .rpc_release = nfs4_layoutcommit_release,
6767 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6769 struct rpc_message msg = {
6770 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6771 .rpc_argp = &data->args,
6772 .rpc_resp = &data->res,
6773 .rpc_cred = data->cred,
6775 struct rpc_task_setup task_setup_data = {
6776 .task = &data->task,
6777 .rpc_client = NFS_CLIENT(data->args.inode),
6778 .rpc_message = &msg,
6779 .callback_ops = &nfs4_layoutcommit_ops,
6780 .callback_data = data,
6781 .flags = RPC_TASK_ASYNC,
6783 struct rpc_task *task;
6786 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6787 "lbw: %llu inode %lu\n",
6788 data->task.tk_pid, sync,
6789 data->args.lastbytewritten,
6790 data->args.inode->i_ino);
6792 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6793 task = rpc_run_task(&task_setup_data);
6795 return PTR_ERR(task);
6798 status = nfs4_wait_for_completion_rpc_task(task);
6801 status = task->tk_status;
6803 dprintk("%s: status %d\n", __func__, status);
6809 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6810 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6812 struct nfs41_secinfo_no_name_args args = {
6813 .style = SECINFO_STYLE_CURRENT_FH,
6815 struct nfs4_secinfo_res res = {
6818 struct rpc_message msg = {
6819 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6823 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6827 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6828 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6830 struct nfs4_exception exception = { };
6833 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6836 case -NFS4ERR_WRONGSEC:
6837 case -NFS4ERR_NOTSUPP:
6840 err = nfs4_handle_exception(server, err, &exception);
6842 } while (exception.retry);
6848 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6849 struct nfs_fsinfo *info)
6853 rpc_authflavor_t flavor;
6854 struct nfs4_secinfo_flavors *flavors;
6856 page = alloc_page(GFP_KERNEL);
6862 flavors = page_address(page);
6863 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6866 * Fall back on "guess and check" method if
6867 * the server doesn't support SECINFO_NO_NAME
6869 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6870 err = nfs4_find_root_sec(server, fhandle, info);
6876 flavor = nfs_find_best_sec(flavors);
6878 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6888 static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6891 struct nfs41_test_stateid_args args = {
6894 struct nfs41_test_stateid_res res;
6895 struct rpc_message msg = {
6896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6901 dprintk("NFS call test_stateid %p\n", stateid);
6902 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6903 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6904 if (status != NFS_OK) {
6905 dprintk("NFS reply test_stateid: failed, %d\n", status);
6908 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
6913 * nfs41_test_stateid - perform a TEST_STATEID operation
6915 * @server: server / transport on which to perform the operation
6916 * @stateid: state ID to test
6918 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6919 * Otherwise a negative NFS4ERR value is returned if the operation
6920 * failed or the state ID is not currently valid.
6922 static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6924 struct nfs4_exception exception = { };
6927 err = _nfs41_test_stateid(server, stateid);
6928 if (err != -NFS4ERR_DELAY)
6930 nfs4_handle_exception(server, err, &exception);
6931 } while (exception.retry);
6935 static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6937 struct nfs41_free_stateid_args args = {
6940 struct nfs41_free_stateid_res res;
6941 struct rpc_message msg = {
6942 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6948 dprintk("NFS call free_stateid %p\n", stateid);
6949 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6950 status = nfs4_call_sync_sequence(server->client, server, &msg,
6951 &args.seq_args, &res.seq_res, 1);
6952 dprintk("NFS reply free_stateid: %d\n", status);
6957 * nfs41_free_stateid - perform a FREE_STATEID operation
6959 * @server: server / transport on which to perform the operation
6960 * @stateid: state ID to release
6962 * Returns NFS_OK if the server freed "stateid". Otherwise a
6963 * negative NFS4ERR value is returned.
6965 static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6967 struct nfs4_exception exception = { };
6970 err = _nfs4_free_stateid(server, stateid);
6971 if (err != -NFS4ERR_DELAY)
6973 nfs4_handle_exception(server, err, &exception);
6974 } while (exception.retry);
6978 static bool nfs41_match_stateid(const nfs4_stateid *s1,
6979 const nfs4_stateid *s2)
6981 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
6984 if (s1->seqid == s2->seqid)
6986 if (s1->seqid == 0 || s2->seqid == 0)
6992 #endif /* CONFIG_NFS_V4_1 */
6994 static bool nfs4_match_stateid(const nfs4_stateid *s1,
6995 const nfs4_stateid *s2)
6997 return nfs4_stateid_match(s1, s2);
7001 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
7002 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7003 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7004 .recover_open = nfs4_open_reclaim,
7005 .recover_lock = nfs4_lock_reclaim,
7006 .establish_clid = nfs4_init_clientid,
7007 .get_clid_cred = nfs4_get_setclientid_cred,
7008 .detect_trunking = nfs40_discover_server_trunking,
7011 #if defined(CONFIG_NFS_V4_1)
7012 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
7013 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7014 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7015 .recover_open = nfs4_open_reclaim,
7016 .recover_lock = nfs4_lock_reclaim,
7017 .establish_clid = nfs41_init_clientid,
7018 .get_clid_cred = nfs4_get_exchange_id_cred,
7019 .reclaim_complete = nfs41_proc_reclaim_complete,
7020 .detect_trunking = nfs41_discover_server_trunking,
7022 #endif /* CONFIG_NFS_V4_1 */
7024 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
7025 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7026 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
7027 .recover_open = nfs4_open_expired,
7028 .recover_lock = nfs4_lock_expired,
7029 .establish_clid = nfs4_init_clientid,
7030 .get_clid_cred = nfs4_get_setclientid_cred,
7033 #if defined(CONFIG_NFS_V4_1)
7034 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
7035 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7036 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
7037 .recover_open = nfs41_open_expired,
7038 .recover_lock = nfs41_lock_expired,
7039 .establish_clid = nfs41_init_clientid,
7040 .get_clid_cred = nfs4_get_exchange_id_cred,
7042 #endif /* CONFIG_NFS_V4_1 */
7044 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
7045 .sched_state_renewal = nfs4_proc_async_renew,
7046 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
7047 .renew_lease = nfs4_proc_renew,
7050 #if defined(CONFIG_NFS_V4_1)
7051 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
7052 .sched_state_renewal = nfs41_proc_async_sequence,
7053 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
7054 .renew_lease = nfs4_proc_sequence,
7058 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7060 .call_sync = _nfs4_call_sync,
7061 .match_stateid = nfs4_match_stateid,
7062 .find_root_sec = nfs4_find_root_sec,
7063 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7064 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7065 .state_renewal_ops = &nfs40_state_renewal_ops,
7068 #if defined(CONFIG_NFS_V4_1)
7069 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7071 .call_sync = _nfs4_call_sync_session,
7072 .match_stateid = nfs41_match_stateid,
7073 .find_root_sec = nfs41_find_root_sec,
7074 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7075 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7076 .state_renewal_ops = &nfs41_state_renewal_ops,
7080 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7081 [0] = &nfs_v4_0_minor_ops,
7082 #if defined(CONFIG_NFS_V4_1)
7083 [1] = &nfs_v4_1_minor_ops,
7087 const struct inode_operations nfs4_dir_inode_operations = {
7088 .create = nfs_create,
7089 .lookup = nfs_lookup,
7090 .atomic_open = nfs_atomic_open,
7092 .unlink = nfs_unlink,
7093 .symlink = nfs_symlink,
7097 .rename = nfs_rename,
7098 .permission = nfs_permission,
7099 .getattr = nfs_getattr,
7100 .setattr = nfs_setattr,
7101 .getxattr = generic_getxattr,
7102 .setxattr = generic_setxattr,
7103 .listxattr = generic_listxattr,
7104 .removexattr = generic_removexattr,
7107 static const struct inode_operations nfs4_file_inode_operations = {
7108 .permission = nfs_permission,
7109 .getattr = nfs_getattr,
7110 .setattr = nfs_setattr,
7111 .getxattr = generic_getxattr,
7112 .setxattr = generic_setxattr,
7113 .listxattr = generic_listxattr,
7114 .removexattr = generic_removexattr,
7117 const struct nfs_rpc_ops nfs_v4_clientops = {
7118 .version = 4, /* protocol version */
7119 .dentry_ops = &nfs4_dentry_operations,
7120 .dir_inode_ops = &nfs4_dir_inode_operations,
7121 .file_inode_ops = &nfs4_file_inode_operations,
7122 .file_ops = &nfs4_file_operations,
7123 .getroot = nfs4_proc_get_root,
7124 .submount = nfs4_submount,
7125 .try_mount = nfs4_try_mount,
7126 .getattr = nfs4_proc_getattr,
7127 .setattr = nfs4_proc_setattr,
7128 .lookup = nfs4_proc_lookup,
7129 .access = nfs4_proc_access,
7130 .readlink = nfs4_proc_readlink,
7131 .create = nfs4_proc_create,
7132 .remove = nfs4_proc_remove,
7133 .unlink_setup = nfs4_proc_unlink_setup,
7134 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
7135 .unlink_done = nfs4_proc_unlink_done,
7136 .rename = nfs4_proc_rename,
7137 .rename_setup = nfs4_proc_rename_setup,
7138 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
7139 .rename_done = nfs4_proc_rename_done,
7140 .link = nfs4_proc_link,
7141 .symlink = nfs4_proc_symlink,
7142 .mkdir = nfs4_proc_mkdir,
7143 .rmdir = nfs4_proc_remove,
7144 .readdir = nfs4_proc_readdir,
7145 .mknod = nfs4_proc_mknod,
7146 .statfs = nfs4_proc_statfs,
7147 .fsinfo = nfs4_proc_fsinfo,
7148 .pathconf = nfs4_proc_pathconf,
7149 .set_capabilities = nfs4_server_capabilities,
7150 .decode_dirent = nfs4_decode_dirent,
7151 .read_setup = nfs4_proc_read_setup,
7152 .read_pageio_init = pnfs_pageio_init_read,
7153 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
7154 .read_done = nfs4_read_done,
7155 .write_setup = nfs4_proc_write_setup,
7156 .write_pageio_init = pnfs_pageio_init_write,
7157 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
7158 .write_done = nfs4_write_done,
7159 .commit_setup = nfs4_proc_commit_setup,
7160 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
7161 .commit_done = nfs4_commit_done,
7162 .lock = nfs4_proc_lock,
7163 .clear_acl_cache = nfs4_zap_acl_attr,
7164 .close_context = nfs4_close_context,
7165 .open_context = nfs4_atomic_open,
7166 .have_delegation = nfs4_have_delegation,
7167 .return_delegation = nfs4_inode_return_delegation,
7168 .alloc_client = nfs4_alloc_client,
7169 .init_client = nfs4_init_client,
7170 .free_client = nfs4_free_client,
7171 .create_server = nfs4_create_server,
7172 .clone_server = nfs_clone_server,
7175 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7176 .prefix = XATTR_NAME_NFSV4_ACL,
7177 .list = nfs4_xattr_list_nfs4_acl,
7178 .get = nfs4_xattr_get_nfs4_acl,
7179 .set = nfs4_xattr_set_nfs4_acl,
7182 const struct xattr_handler *nfs4_xattr_handlers[] = {
7183 &nfs4_xattr_nfs4_acl_handler,