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_shrink_slot_table - free retired slots from the slot table
402 static void nfs4_shrink_slot_table(struct nfs4_slot_table *tbl, u32 newsize)
404 struct nfs4_slot **p;
405 if (newsize >= tbl->max_slots)
412 struct nfs4_slot *slot = *p;
421 * nfs4_free_slot - free a slot and efficiently update slot table.
423 * freeing a slot is trivially done by clearing its respective bit
425 * If the freed slotid equals highest_used_slotid we want to update it
426 * so that the server would be able to size down the slot table if needed,
427 * otherwise we know that the highest_used_slotid is still in use.
428 * When updating highest_used_slotid there may be "holes" in the bitmap
429 * so we need to scan down from highest_used_slotid to 0 looking for the now
430 * highest slotid in use.
431 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
433 * Must be called while holding tbl->slot_tbl_lock
436 nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
438 u32 slotid = slot->slot_nr;
440 /* clear used bit in bitmap */
441 __clear_bit(slotid, tbl->used_slots);
443 /* update highest_used_slotid when it is freed */
444 if (slotid == tbl->highest_used_slotid) {
445 u32 new_max = find_last_bit(tbl->used_slots, slotid);
446 if (new_max < slotid)
447 tbl->highest_used_slotid = new_max;
449 tbl->highest_used_slotid = NFS4_NO_SLOT;
451 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
452 slotid, tbl->highest_used_slotid);
455 bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
457 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
462 * Signal state manager thread if session fore channel is drained
464 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
466 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
467 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
468 nfs4_set_task_privileged, NULL);
472 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
475 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
476 complete(&ses->fc_slot_table.complete);
480 * Signal state manager thread if session back channel is drained
482 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
484 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
485 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
487 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
488 complete(&ses->bc_slot_table.complete);
491 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
493 struct nfs4_session *session;
494 struct nfs4_slot_table *tbl;
497 /* just wake up the next guy waiting since
498 * we may have not consumed a slot after all */
499 dprintk("%s: No slot\n", __func__);
502 tbl = res->sr_slot->table;
503 session = tbl->session;
505 spin_lock(&tbl->slot_tbl_lock);
506 nfs4_free_slot(tbl, res->sr_slot);
507 nfs4_check_drain_fc_complete(session);
508 spin_unlock(&tbl->slot_tbl_lock);
512 /* Update the client's idea of target_highest_slotid */
513 static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
514 u32 target_highest_slotid)
516 unsigned int max_slotid, i;
518 if (tbl->target_highest_slotid == target_highest_slotid)
520 tbl->target_highest_slotid = target_highest_slotid;
523 max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, tbl->target_highest_slotid);
524 for (i = tbl->max_slotid + 1; i <= max_slotid; i++)
525 rpc_wake_up_next(&tbl->slot_tbl_waitq);
526 tbl->max_slotid = max_slotid;
529 void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
530 u32 target_highest_slotid)
532 spin_lock(&tbl->slot_tbl_lock);
533 nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
534 spin_unlock(&tbl->slot_tbl_lock);
537 static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
540 if (tbl->server_highest_slotid == highest_slotid)
542 if (tbl->highest_used_slotid > highest_slotid)
544 /* Deallocate slots */
545 nfs4_shrink_slot_table(tbl, highest_slotid + 1);
546 tbl->server_highest_slotid = highest_slotid;
549 static void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
550 struct nfs4_slot *slot,
551 struct nfs4_sequence_res *res)
553 spin_lock(&tbl->slot_tbl_lock);
554 if (tbl->generation != slot->generation)
556 nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
557 nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
559 spin_unlock(&tbl->slot_tbl_lock);
562 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
564 struct nfs4_session *session;
565 struct nfs4_slot *slot;
566 unsigned long timestamp;
567 struct nfs_client *clp;
570 * sr_status remains 1 if an RPC level error occurred. The server
571 * may or may not have processed the sequence operation..
572 * Proceed as if the server received and processed the sequence
575 if (res->sr_status == 1)
576 res->sr_status = NFS_OK;
578 /* don't increment the sequence number if the task wasn't sent */
579 if (!RPC_WAS_SENT(task))
583 session = slot->table->session;
585 /* Check the SEQUENCE operation status */
586 switch (res->sr_status) {
588 /* Update the slot's sequence and clientid lease timer */
590 timestamp = slot->renewal_time;
592 do_renew_lease(clp, timestamp);
593 /* Check sequence flags */
594 if (res->sr_status_flags != 0)
595 nfs4_schedule_lease_recovery(clp);
596 nfs41_update_target_slotid(slot->table, slot, res);
599 /* The server detected a resend of the RPC call and
600 * returned NFS4ERR_DELAY as per Section 2.10.6.2
603 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
609 /* Just update the slot sequence no. */
613 /* The session may be reset by one of the error handlers. */
614 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
615 nfs41_sequence_free_slot(res);
618 if (!rpc_restart_call(task))
620 rpc_delay(task, NFS4_POLL_RETRY_MAX);
624 static int nfs4_sequence_done(struct rpc_task *task,
625 struct nfs4_sequence_res *res)
627 if (res->sr_slot == NULL)
629 return nfs41_sequence_done(task, res);
632 static struct nfs4_slot *nfs4_new_slot(struct nfs4_slot_table *tbl,
633 u32 slotid, u32 seq_init, gfp_t gfp_mask)
635 struct nfs4_slot *slot;
637 slot = kzalloc(sizeof(*slot), gfp_mask);
640 slot->slot_nr = slotid;
641 slot->seq_nr = seq_init;
646 static struct nfs4_slot *nfs4_find_or_create_slot(struct nfs4_slot_table *tbl,
647 u32 slotid, u32 seq_init, gfp_t gfp_mask)
649 struct nfs4_slot **p, *slot;
654 *p = nfs4_new_slot(tbl, tbl->max_slots,
661 if (slot->slot_nr == slotid)
665 return ERR_PTR(-ENOMEM);
669 * nfs4_alloc_slot - efficiently look for a free slot
671 * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap.
672 * If found, we mark the slot as used, update the highest_used_slotid,
673 * and respectively set up the sequence operation args.
675 * Note: must be called with under the slot_tbl_lock.
677 static struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
679 struct nfs4_slot *ret = ERR_PTR(-EBUSY);
682 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
683 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
684 tbl->max_slotid + 1);
685 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slotid + 1);
686 if (slotid > tbl->max_slotid)
688 ret = nfs4_find_or_create_slot(tbl, slotid, 1, GFP_NOWAIT);
691 __set_bit(slotid, tbl->used_slots);
692 if (slotid > tbl->highest_used_slotid ||
693 tbl->highest_used_slotid == NFS4_NO_SLOT)
694 tbl->highest_used_slotid = slotid;
695 ret->renewal_time = jiffies;
696 ret->generation = tbl->generation;
699 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
700 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
701 !IS_ERR(ret) ? ret->slot_nr : -1);
705 static void nfs41_init_sequence(struct nfs4_sequence_args *args,
706 struct nfs4_sequence_res *res, int cache_reply)
708 args->sa_slot = NULL;
709 args->sa_cache_this = 0;
711 args->sa_cache_this = 1;
715 int nfs41_setup_sequence(struct nfs4_session *session,
716 struct nfs4_sequence_args *args,
717 struct nfs4_sequence_res *res,
718 struct rpc_task *task)
720 struct nfs4_slot *slot;
721 struct nfs4_slot_table *tbl;
723 dprintk("--> %s\n", __func__);
724 /* slot already allocated? */
725 if (res->sr_slot != NULL)
728 tbl = &session->fc_slot_table;
730 task->tk_timeout = 0;
732 spin_lock(&tbl->slot_tbl_lock);
733 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
734 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
735 /* The state manager will wait until the slot table is empty */
736 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
737 spin_unlock(&tbl->slot_tbl_lock);
738 dprintk("%s session is draining\n", __func__);
742 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
743 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
744 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
745 spin_unlock(&tbl->slot_tbl_lock);
746 dprintk("%s enforce FIFO order\n", __func__);
750 slot = nfs4_alloc_slot(tbl);
752 /* If out of memory, try again in 1/4 second */
753 if (slot == ERR_PTR(-ENOMEM))
754 task->tk_timeout = HZ >> 2;
755 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
756 spin_unlock(&tbl->slot_tbl_lock);
757 dprintk("<-- %s: no free slots\n", __func__);
760 spin_unlock(&tbl->slot_tbl_lock);
762 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
764 args->sa_slot = slot;
766 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
767 slot->slot_nr, slot->seq_nr);
770 res->sr_status_flags = 0;
772 * sr_status is only set in decode_sequence, and so will remain
773 * set to 1 if an rpc level failure occurs.
778 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
780 int nfs4_setup_sequence(const struct nfs_server *server,
781 struct nfs4_sequence_args *args,
782 struct nfs4_sequence_res *res,
783 struct rpc_task *task)
785 struct nfs4_session *session = nfs4_get_session(server);
791 dprintk("--> %s clp %p session %p sr_slot %d\n",
792 __func__, session->clp, session, res->sr_slot ?
793 res->sr_slot->slot_nr : -1);
795 ret = nfs41_setup_sequence(session, args, res, task);
797 dprintk("<-- %s status=%d\n", __func__, ret);
801 struct nfs41_call_sync_data {
802 const struct nfs_server *seq_server;
803 struct nfs4_sequence_args *seq_args;
804 struct nfs4_sequence_res *seq_res;
807 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
809 struct nfs41_call_sync_data *data = calldata;
811 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
813 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
814 data->seq_res, task))
816 rpc_call_start(task);
819 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
821 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
822 nfs41_call_sync_prepare(task, calldata);
825 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
827 struct nfs41_call_sync_data *data = calldata;
829 nfs41_sequence_done(task, data->seq_res);
832 static const struct rpc_call_ops nfs41_call_sync_ops = {
833 .rpc_call_prepare = nfs41_call_sync_prepare,
834 .rpc_call_done = nfs41_call_sync_done,
837 static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
838 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
839 .rpc_call_done = nfs41_call_sync_done,
842 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
843 struct nfs_server *server,
844 struct rpc_message *msg,
845 struct nfs4_sequence_args *args,
846 struct nfs4_sequence_res *res,
850 struct rpc_task *task;
851 struct nfs41_call_sync_data data = {
852 .seq_server = server,
856 struct rpc_task_setup task_setup = {
859 .callback_ops = &nfs41_call_sync_ops,
860 .callback_data = &data
864 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
865 task = rpc_run_task(&task_setup);
869 ret = task->tk_status;
875 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
876 struct nfs_server *server,
877 struct rpc_message *msg,
878 struct nfs4_sequence_args *args,
879 struct nfs4_sequence_res *res,
882 nfs41_init_sequence(args, res, cache_reply);
883 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
888 void nfs41_init_sequence(struct nfs4_sequence_args *args,
889 struct nfs4_sequence_res *res, int cache_reply)
893 static int nfs4_sequence_done(struct rpc_task *task,
894 struct nfs4_sequence_res *res)
898 #endif /* CONFIG_NFS_V4_1 */
900 int _nfs4_call_sync(struct rpc_clnt *clnt,
901 struct nfs_server *server,
902 struct rpc_message *msg,
903 struct nfs4_sequence_args *args,
904 struct nfs4_sequence_res *res,
907 nfs41_init_sequence(args, res, cache_reply);
908 return rpc_call_sync(clnt, msg, 0);
912 int nfs4_call_sync(struct rpc_clnt *clnt,
913 struct nfs_server *server,
914 struct rpc_message *msg,
915 struct nfs4_sequence_args *args,
916 struct nfs4_sequence_res *res,
919 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
920 args, res, cache_reply);
923 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
925 struct nfs_inode *nfsi = NFS_I(dir);
927 spin_lock(&dir->i_lock);
928 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
929 if (!cinfo->atomic || cinfo->before != dir->i_version)
930 nfs_force_lookup_revalidate(dir);
931 dir->i_version = cinfo->after;
932 spin_unlock(&dir->i_lock);
935 struct nfs4_opendata {
937 struct nfs_openargs o_arg;
938 struct nfs_openres o_res;
939 struct nfs_open_confirmargs c_arg;
940 struct nfs_open_confirmres c_res;
941 struct nfs4_string owner_name;
942 struct nfs4_string group_name;
943 struct nfs_fattr f_attr;
945 struct dentry *dentry;
946 struct nfs4_state_owner *owner;
947 struct nfs4_state *state;
949 unsigned long timestamp;
950 unsigned int rpc_done : 1;
956 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
958 p->o_res.f_attr = &p->f_attr;
959 p->o_res.seqid = p->o_arg.seqid;
960 p->c_res.seqid = p->c_arg.seqid;
961 p->o_res.server = p->o_arg.server;
962 p->o_res.access_request = p->o_arg.access;
963 nfs_fattr_init(&p->f_attr);
964 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
967 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
968 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
969 const struct iattr *attrs,
972 struct dentry *parent = dget_parent(dentry);
973 struct inode *dir = parent->d_inode;
974 struct nfs_server *server = NFS_SERVER(dir);
975 struct nfs4_opendata *p;
977 p = kzalloc(sizeof(*p), gfp_mask);
980 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
981 if (p->o_arg.seqid == NULL)
983 nfs_sb_active(dentry->d_sb);
984 p->dentry = dget(dentry);
987 atomic_inc(&sp->so_count);
988 p->o_arg.fh = NFS_FH(dir);
989 p->o_arg.open_flags = flags;
990 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
991 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
992 * will return permission denied for all bits until close */
993 if (!(flags & O_EXCL)) {
994 /* ask server to check for all possible rights as results
996 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
997 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
999 p->o_arg.clientid = server->nfs_client->cl_clientid;
1000 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1001 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1002 p->o_arg.name = &dentry->d_name;
1003 p->o_arg.server = server;
1004 p->o_arg.bitmask = server->attr_bitmask;
1005 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1006 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
1007 if (attrs != NULL && attrs->ia_valid != 0) {
1010 p->o_arg.u.attrs = &p->attrs;
1011 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1014 verf[1] = current->pid;
1015 memcpy(p->o_arg.u.verifier.data, verf,
1016 sizeof(p->o_arg.u.verifier.data));
1018 p->c_arg.fh = &p->o_res.fh;
1019 p->c_arg.stateid = &p->o_res.stateid;
1020 p->c_arg.seqid = p->o_arg.seqid;
1021 nfs4_init_opendata_res(p);
1022 kref_init(&p->kref);
1031 static void nfs4_opendata_free(struct kref *kref)
1033 struct nfs4_opendata *p = container_of(kref,
1034 struct nfs4_opendata, kref);
1035 struct super_block *sb = p->dentry->d_sb;
1037 nfs_free_seqid(p->o_arg.seqid);
1038 if (p->state != NULL)
1039 nfs4_put_open_state(p->state);
1040 nfs4_put_state_owner(p->owner);
1043 nfs_sb_deactive(sb);
1044 nfs_fattr_free_names(&p->f_attr);
1048 static void nfs4_opendata_put(struct nfs4_opendata *p)
1051 kref_put(&p->kref, nfs4_opendata_free);
1054 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1058 ret = rpc_wait_for_completion_task(task);
1062 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1066 if (open_mode & (O_EXCL|O_TRUNC))
1068 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1070 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1071 && state->n_rdonly != 0;
1074 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1075 && state->n_wronly != 0;
1077 case FMODE_READ|FMODE_WRITE:
1078 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1079 && state->n_rdwr != 0;
1085 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
1087 if (delegation == NULL)
1089 if ((delegation->type & fmode) != fmode)
1091 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1093 nfs_mark_delegation_referenced(delegation);
1097 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1106 case FMODE_READ|FMODE_WRITE:
1109 nfs4_state_set_mode_locked(state, state->state | fmode);
1112 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1114 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1115 nfs4_stateid_copy(&state->stateid, stateid);
1116 nfs4_stateid_copy(&state->open_stateid, stateid);
1119 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1122 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1124 case FMODE_READ|FMODE_WRITE:
1125 set_bit(NFS_O_RDWR_STATE, &state->flags);
1129 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1131 write_seqlock(&state->seqlock);
1132 nfs_set_open_stateid_locked(state, stateid, fmode);
1133 write_sequnlock(&state->seqlock);
1136 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1139 * Protect the call to nfs4_state_set_mode_locked and
1140 * serialise the stateid update
1142 write_seqlock(&state->seqlock);
1143 if (deleg_stateid != NULL) {
1144 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1145 set_bit(NFS_DELEGATED_STATE, &state->flags);
1147 if (open_stateid != NULL)
1148 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1149 write_sequnlock(&state->seqlock);
1150 spin_lock(&state->owner->so_lock);
1151 update_open_stateflags(state, fmode);
1152 spin_unlock(&state->owner->so_lock);
1155 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1157 struct nfs_inode *nfsi = NFS_I(state->inode);
1158 struct nfs_delegation *deleg_cur;
1161 fmode &= (FMODE_READ|FMODE_WRITE);
1164 deleg_cur = rcu_dereference(nfsi->delegation);
1165 if (deleg_cur == NULL)
1168 spin_lock(&deleg_cur->lock);
1169 if (nfsi->delegation != deleg_cur ||
1170 (deleg_cur->type & fmode) != fmode)
1171 goto no_delegation_unlock;
1173 if (delegation == NULL)
1174 delegation = &deleg_cur->stateid;
1175 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1176 goto no_delegation_unlock;
1178 nfs_mark_delegation_referenced(deleg_cur);
1179 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1181 no_delegation_unlock:
1182 spin_unlock(&deleg_cur->lock);
1186 if (!ret && open_stateid != NULL) {
1187 __update_open_stateid(state, open_stateid, NULL, fmode);
1195 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1197 struct nfs_delegation *delegation;
1200 delegation = rcu_dereference(NFS_I(inode)->delegation);
1201 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1206 nfs4_inode_return_delegation(inode);
1209 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1211 struct nfs4_state *state = opendata->state;
1212 struct nfs_inode *nfsi = NFS_I(state->inode);
1213 struct nfs_delegation *delegation;
1214 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
1215 fmode_t fmode = opendata->o_arg.fmode;
1216 nfs4_stateid stateid;
1220 if (can_open_cached(state, fmode, open_mode)) {
1221 spin_lock(&state->owner->so_lock);
1222 if (can_open_cached(state, fmode, open_mode)) {
1223 update_open_stateflags(state, fmode);
1224 spin_unlock(&state->owner->so_lock);
1225 goto out_return_state;
1227 spin_unlock(&state->owner->so_lock);
1230 delegation = rcu_dereference(nfsi->delegation);
1231 if (!can_open_delegated(delegation, fmode)) {
1235 /* Save the delegation */
1236 nfs4_stateid_copy(&stateid, &delegation->stateid);
1238 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1243 /* Try to update the stateid using the delegation */
1244 if (update_open_stateid(state, NULL, &stateid, fmode))
1245 goto out_return_state;
1248 return ERR_PTR(ret);
1250 atomic_inc(&state->count);
1255 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1257 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1258 struct nfs_delegation *delegation;
1259 int delegation_flags = 0;
1262 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1264 delegation_flags = delegation->flags;
1266 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1267 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1268 "returning a delegation for "
1269 "OPEN(CLAIM_DELEGATE_CUR)\n",
1271 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1272 nfs_inode_set_delegation(state->inode,
1273 data->owner->so_cred,
1276 nfs_inode_reclaim_delegation(state->inode,
1277 data->owner->so_cred,
1282 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1283 * and update the nfs4_state.
1285 static struct nfs4_state *
1286 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1288 struct inode *inode = data->state->inode;
1289 struct nfs4_state *state = data->state;
1292 if (!data->rpc_done) {
1293 ret = data->rpc_status;
1298 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1299 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1300 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1304 state = nfs4_get_open_state(inode, data->owner);
1308 ret = nfs_refresh_inode(inode, &data->f_attr);
1312 if (data->o_res.delegation_type != 0)
1313 nfs4_opendata_check_deleg(data, state);
1314 update_open_stateid(state, &data->o_res.stateid, NULL,
1319 return ERR_PTR(ret);
1323 static struct nfs4_state *
1324 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1326 struct inode *inode;
1327 struct nfs4_state *state = NULL;
1330 if (!data->rpc_done) {
1331 state = nfs4_try_open_cached(data);
1336 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1338 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1339 ret = PTR_ERR(inode);
1343 state = nfs4_get_open_state(inode, data->owner);
1346 if (data->o_res.delegation_type != 0)
1347 nfs4_opendata_check_deleg(data, state);
1348 update_open_stateid(state, &data->o_res.stateid, NULL,
1356 return ERR_PTR(ret);
1359 static struct nfs4_state *
1360 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1362 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1363 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1364 return _nfs4_opendata_to_nfs4_state(data);
1367 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1369 struct nfs_inode *nfsi = NFS_I(state->inode);
1370 struct nfs_open_context *ctx;
1372 spin_lock(&state->inode->i_lock);
1373 list_for_each_entry(ctx, &nfsi->open_files, list) {
1374 if (ctx->state != state)
1376 get_nfs_open_context(ctx);
1377 spin_unlock(&state->inode->i_lock);
1380 spin_unlock(&state->inode->i_lock);
1381 return ERR_PTR(-ENOENT);
1384 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1386 struct nfs4_opendata *opendata;
1388 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1389 if (opendata == NULL)
1390 return ERR_PTR(-ENOMEM);
1391 opendata->state = state;
1392 atomic_inc(&state->count);
1396 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1398 struct nfs4_state *newstate;
1401 opendata->o_arg.open_flags = 0;
1402 opendata->o_arg.fmode = fmode;
1403 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1404 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1405 nfs4_init_opendata_res(opendata);
1406 ret = _nfs4_recover_proc_open(opendata);
1409 newstate = nfs4_opendata_to_nfs4_state(opendata);
1410 if (IS_ERR(newstate))
1411 return PTR_ERR(newstate);
1412 nfs4_close_state(newstate, fmode);
1417 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1419 struct nfs4_state *newstate;
1422 /* memory barrier prior to reading state->n_* */
1423 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1425 if (state->n_rdwr != 0) {
1426 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1427 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1430 if (newstate != state)
1433 if (state->n_wronly != 0) {
1434 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1435 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1438 if (newstate != state)
1441 if (state->n_rdonly != 0) {
1442 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1443 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1446 if (newstate != state)
1450 * We may have performed cached opens for all three recoveries.
1451 * Check if we need to update the current stateid.
1453 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1454 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1455 write_seqlock(&state->seqlock);
1456 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1457 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1458 write_sequnlock(&state->seqlock);
1465 * reclaim state on the server after a reboot.
1467 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1469 struct nfs_delegation *delegation;
1470 struct nfs4_opendata *opendata;
1471 fmode_t delegation_type = 0;
1474 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1475 if (IS_ERR(opendata))
1476 return PTR_ERR(opendata);
1477 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1478 opendata->o_arg.fh = NFS_FH(state->inode);
1480 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1481 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1482 delegation_type = delegation->type;
1484 opendata->o_arg.u.delegation_type = delegation_type;
1485 status = nfs4_open_recover(opendata, state);
1486 nfs4_opendata_put(opendata);
1490 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1492 struct nfs_server *server = NFS_SERVER(state->inode);
1493 struct nfs4_exception exception = { };
1496 err = _nfs4_do_open_reclaim(ctx, state);
1497 if (err != -NFS4ERR_DELAY)
1499 nfs4_handle_exception(server, err, &exception);
1500 } while (exception.retry);
1504 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1506 struct nfs_open_context *ctx;
1509 ctx = nfs4_state_find_open_context(state);
1511 return PTR_ERR(ctx);
1512 ret = nfs4_do_open_reclaim(ctx, state);
1513 put_nfs_open_context(ctx);
1517 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1519 struct nfs4_opendata *opendata;
1522 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1523 if (IS_ERR(opendata))
1524 return PTR_ERR(opendata);
1525 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1526 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1527 ret = nfs4_open_recover(opendata, state);
1528 nfs4_opendata_put(opendata);
1532 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1534 struct nfs4_exception exception = { };
1535 struct nfs_server *server = NFS_SERVER(state->inode);
1538 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1544 case -NFS4ERR_BADSESSION:
1545 case -NFS4ERR_BADSLOT:
1546 case -NFS4ERR_BAD_HIGH_SLOT:
1547 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1548 case -NFS4ERR_DEADSESSION:
1549 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1551 case -NFS4ERR_STALE_CLIENTID:
1552 case -NFS4ERR_STALE_STATEID:
1553 case -NFS4ERR_EXPIRED:
1554 /* Don't recall a delegation if it was lost */
1555 nfs4_schedule_lease_recovery(server->nfs_client);
1559 * The show must go on: exit, but mark the
1560 * stateid as needing recovery.
1562 case -NFS4ERR_DELEG_REVOKED:
1563 case -NFS4ERR_ADMIN_REVOKED:
1564 case -NFS4ERR_BAD_STATEID:
1565 nfs_inode_find_state_and_recover(state->inode,
1567 nfs4_schedule_stateid_recovery(server, state);
1570 * User RPCSEC_GSS context has expired.
1571 * We cannot recover this stateid now, so
1572 * skip it and allow recovery thread to
1579 err = nfs4_handle_exception(server, err, &exception);
1580 } while (exception.retry);
1585 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1587 struct nfs4_opendata *data = calldata;
1589 data->rpc_status = task->tk_status;
1590 if (data->rpc_status == 0) {
1591 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1592 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1593 renew_lease(data->o_res.server, data->timestamp);
1598 static void nfs4_open_confirm_release(void *calldata)
1600 struct nfs4_opendata *data = calldata;
1601 struct nfs4_state *state = NULL;
1603 /* If this request hasn't been cancelled, do nothing */
1604 if (data->cancelled == 0)
1606 /* In case of error, no cleanup! */
1607 if (!data->rpc_done)
1609 state = nfs4_opendata_to_nfs4_state(data);
1611 nfs4_close_state(state, data->o_arg.fmode);
1613 nfs4_opendata_put(data);
1616 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1617 .rpc_call_done = nfs4_open_confirm_done,
1618 .rpc_release = nfs4_open_confirm_release,
1622 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1624 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1626 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1627 struct rpc_task *task;
1628 struct rpc_message msg = {
1629 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1630 .rpc_argp = &data->c_arg,
1631 .rpc_resp = &data->c_res,
1632 .rpc_cred = data->owner->so_cred,
1634 struct rpc_task_setup task_setup_data = {
1635 .rpc_client = server->client,
1636 .rpc_message = &msg,
1637 .callback_ops = &nfs4_open_confirm_ops,
1638 .callback_data = data,
1639 .workqueue = nfsiod_workqueue,
1640 .flags = RPC_TASK_ASYNC,
1644 kref_get(&data->kref);
1646 data->rpc_status = 0;
1647 data->timestamp = jiffies;
1648 task = rpc_run_task(&task_setup_data);
1650 return PTR_ERR(task);
1651 status = nfs4_wait_for_completion_rpc_task(task);
1653 data->cancelled = 1;
1656 status = data->rpc_status;
1661 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1663 struct nfs4_opendata *data = calldata;
1664 struct nfs4_state_owner *sp = data->owner;
1666 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1669 * Check if we still need to send an OPEN call, or if we can use
1670 * a delegation instead.
1672 if (data->state != NULL) {
1673 struct nfs_delegation *delegation;
1675 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1678 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1679 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1680 can_open_delegated(delegation, data->o_arg.fmode))
1681 goto unlock_no_action;
1684 /* Update client id. */
1685 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1686 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1687 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1688 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1689 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1691 data->timestamp = jiffies;
1692 if (nfs4_setup_sequence(data->o_arg.server,
1693 &data->o_arg.seq_args,
1694 &data->o_res.seq_res,
1696 nfs_release_seqid(data->o_arg.seqid);
1698 rpc_call_start(task);
1703 task->tk_action = NULL;
1707 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1709 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1710 nfs4_open_prepare(task, calldata);
1713 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1715 struct nfs4_opendata *data = calldata;
1717 data->rpc_status = task->tk_status;
1719 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1722 if (task->tk_status == 0) {
1723 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1724 switch (data->o_res.f_attr->mode & S_IFMT) {
1728 data->rpc_status = -ELOOP;
1731 data->rpc_status = -EISDIR;
1734 data->rpc_status = -ENOTDIR;
1737 renew_lease(data->o_res.server, data->timestamp);
1738 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1739 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1744 static void nfs4_open_release(void *calldata)
1746 struct nfs4_opendata *data = calldata;
1747 struct nfs4_state *state = NULL;
1749 /* If this request hasn't been cancelled, do nothing */
1750 if (data->cancelled == 0)
1752 /* In case of error, no cleanup! */
1753 if (data->rpc_status != 0 || !data->rpc_done)
1755 /* In case we need an open_confirm, no cleanup! */
1756 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1758 state = nfs4_opendata_to_nfs4_state(data);
1760 nfs4_close_state(state, data->o_arg.fmode);
1762 nfs4_opendata_put(data);
1765 static const struct rpc_call_ops nfs4_open_ops = {
1766 .rpc_call_prepare = nfs4_open_prepare,
1767 .rpc_call_done = nfs4_open_done,
1768 .rpc_release = nfs4_open_release,
1771 static const struct rpc_call_ops nfs4_recover_open_ops = {
1772 .rpc_call_prepare = nfs4_recover_open_prepare,
1773 .rpc_call_done = nfs4_open_done,
1774 .rpc_release = nfs4_open_release,
1777 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1779 struct inode *dir = data->dir->d_inode;
1780 struct nfs_server *server = NFS_SERVER(dir);
1781 struct nfs_openargs *o_arg = &data->o_arg;
1782 struct nfs_openres *o_res = &data->o_res;
1783 struct rpc_task *task;
1784 struct rpc_message msg = {
1785 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1788 .rpc_cred = data->owner->so_cred,
1790 struct rpc_task_setup task_setup_data = {
1791 .rpc_client = server->client,
1792 .rpc_message = &msg,
1793 .callback_ops = &nfs4_open_ops,
1794 .callback_data = data,
1795 .workqueue = nfsiod_workqueue,
1796 .flags = RPC_TASK_ASYNC,
1800 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1801 kref_get(&data->kref);
1803 data->rpc_status = 0;
1804 data->cancelled = 0;
1806 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1807 task = rpc_run_task(&task_setup_data);
1809 return PTR_ERR(task);
1810 status = nfs4_wait_for_completion_rpc_task(task);
1812 data->cancelled = 1;
1815 status = data->rpc_status;
1821 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1823 struct inode *dir = data->dir->d_inode;
1824 struct nfs_openres *o_res = &data->o_res;
1827 status = nfs4_run_open_task(data, 1);
1828 if (status != 0 || !data->rpc_done)
1831 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1833 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1834 status = _nfs4_proc_open_confirm(data);
1842 static int nfs4_opendata_access(struct rpc_cred *cred,
1843 struct nfs4_opendata *opendata,
1844 struct nfs4_state *state, fmode_t fmode)
1846 struct nfs_access_entry cache;
1849 /* access call failed or for some reason the server doesn't
1850 * support any access modes -- defer access call until later */
1851 if (opendata->o_res.access_supported == 0)
1855 /* don't check MAY_WRITE - a newly created file may not have
1856 * write mode bits, but POSIX allows the creating process to write */
1857 if (fmode & FMODE_READ)
1859 if (fmode & FMODE_EXEC)
1863 cache.jiffies = jiffies;
1864 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1865 nfs_access_add_cache(state->inode, &cache);
1867 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1870 /* even though OPEN succeeded, access is denied. Close the file */
1871 nfs4_close_state(state, fmode);
1876 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1878 static int _nfs4_proc_open(struct nfs4_opendata *data)
1880 struct inode *dir = data->dir->d_inode;
1881 struct nfs_server *server = NFS_SERVER(dir);
1882 struct nfs_openargs *o_arg = &data->o_arg;
1883 struct nfs_openres *o_res = &data->o_res;
1886 status = nfs4_run_open_task(data, 0);
1887 if (!data->rpc_done)
1890 if (status == -NFS4ERR_BADNAME &&
1891 !(o_arg->open_flags & O_CREAT))
1896 nfs_fattr_map_and_free_names(server, &data->f_attr);
1898 if (o_arg->open_flags & O_CREAT)
1899 update_changeattr(dir, &o_res->cinfo);
1900 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1901 server->caps &= ~NFS_CAP_POSIX_LOCK;
1902 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1903 status = _nfs4_proc_open_confirm(data);
1907 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1908 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1912 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1917 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1918 ret = nfs4_wait_clnt_recover(clp);
1921 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1922 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1924 nfs4_schedule_state_manager(clp);
1930 static int nfs4_recover_expired_lease(struct nfs_server *server)
1932 return nfs4_client_recover_expired_lease(server->nfs_client);
1937 * reclaim state on the server after a network partition.
1938 * Assumes caller holds the appropriate lock
1940 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1942 struct nfs4_opendata *opendata;
1945 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1946 if (IS_ERR(opendata))
1947 return PTR_ERR(opendata);
1948 ret = nfs4_open_recover(opendata, state);
1950 d_drop(ctx->dentry);
1951 nfs4_opendata_put(opendata);
1955 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1957 struct nfs_server *server = NFS_SERVER(state->inode);
1958 struct nfs4_exception exception = { };
1962 err = _nfs4_open_expired(ctx, state);
1966 case -NFS4ERR_GRACE:
1967 case -NFS4ERR_DELAY:
1968 nfs4_handle_exception(server, err, &exception);
1971 } while (exception.retry);
1976 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1978 struct nfs_open_context *ctx;
1981 ctx = nfs4_state_find_open_context(state);
1983 return PTR_ERR(ctx);
1984 ret = nfs4_do_open_expired(ctx, state);
1985 put_nfs_open_context(ctx);
1989 #if defined(CONFIG_NFS_V4_1)
1990 static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
1992 struct nfs_server *server = NFS_SERVER(state->inode);
1993 nfs4_stateid *stateid = &state->stateid;
1996 /* If a state reset has been done, test_stateid is unneeded */
1997 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2000 status = nfs41_test_stateid(server, stateid);
2001 if (status != NFS_OK) {
2002 /* Free the stateid unless the server explicitly
2003 * informs us the stateid is unrecognized. */
2004 if (status != -NFS4ERR_BAD_STATEID)
2005 nfs41_free_stateid(server, stateid);
2006 nfs_remove_bad_delegation(state->inode);
2008 write_seqlock(&state->seqlock);
2009 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2010 write_sequnlock(&state->seqlock);
2011 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2016 * nfs41_check_open_stateid - possibly free an open stateid
2018 * @state: NFSv4 state for an inode
2020 * Returns NFS_OK if recovery for this stateid is now finished.
2021 * Otherwise a negative NFS4ERR value is returned.
2023 static int nfs41_check_open_stateid(struct nfs4_state *state)
2025 struct nfs_server *server = NFS_SERVER(state->inode);
2026 nfs4_stateid *stateid = &state->open_stateid;
2029 /* If a state reset has been done, test_stateid is unneeded */
2030 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2031 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2032 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2033 return -NFS4ERR_BAD_STATEID;
2035 status = nfs41_test_stateid(server, stateid);
2036 if (status != NFS_OK) {
2037 /* Free the stateid unless the server explicitly
2038 * informs us the stateid is unrecognized. */
2039 if (status != -NFS4ERR_BAD_STATEID)
2040 nfs41_free_stateid(server, stateid);
2042 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2043 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2044 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2049 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2053 nfs41_clear_delegation_stateid(state);
2054 status = nfs41_check_open_stateid(state);
2055 if (status != NFS_OK)
2056 status = nfs4_open_expired(sp, state);
2062 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2063 * fields corresponding to attributes that were used to store the verifier.
2064 * Make sure we clobber those fields in the later setattr call
2066 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2068 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2069 !(sattr->ia_valid & ATTR_ATIME_SET))
2070 sattr->ia_valid |= ATTR_ATIME;
2072 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2073 !(sattr->ia_valid & ATTR_MTIME_SET))
2074 sattr->ia_valid |= ATTR_MTIME;
2078 * Returns a referenced nfs4_state
2080 static int _nfs4_do_open(struct inode *dir,
2081 struct dentry *dentry,
2084 struct iattr *sattr,
2085 struct rpc_cred *cred,
2086 struct nfs4_state **res,
2087 struct nfs4_threshold **ctx_th)
2089 struct nfs4_state_owner *sp;
2090 struct nfs4_state *state = NULL;
2091 struct nfs_server *server = NFS_SERVER(dir);
2092 struct nfs4_opendata *opendata;
2095 /* Protect against reboot recovery conflicts */
2097 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2099 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2102 status = nfs4_recover_expired_lease(server);
2104 goto err_put_state_owner;
2105 if (dentry->d_inode != NULL)
2106 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
2108 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
2109 if (opendata == NULL)
2110 goto err_put_state_owner;
2112 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2113 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2114 if (!opendata->f_attr.mdsthreshold)
2115 goto err_opendata_put;
2116 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2118 if (dentry->d_inode != NULL)
2119 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
2121 status = _nfs4_proc_open(opendata);
2123 goto err_opendata_put;
2125 state = nfs4_opendata_to_nfs4_state(opendata);
2126 status = PTR_ERR(state);
2128 goto err_opendata_put;
2129 if (server->caps & NFS_CAP_POSIX_LOCK)
2130 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2132 status = nfs4_opendata_access(cred, opendata, state, fmode);
2134 goto err_opendata_put;
2136 if (opendata->o_arg.open_flags & O_EXCL) {
2137 nfs4_exclusive_attrset(opendata, sattr);
2139 nfs_fattr_init(opendata->o_res.f_attr);
2140 status = nfs4_do_setattr(state->inode, cred,
2141 opendata->o_res.f_attr, sattr,
2144 nfs_setattr_update_inode(state->inode, sattr);
2145 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2148 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2149 *ctx_th = opendata->f_attr.mdsthreshold;
2151 kfree(opendata->f_attr.mdsthreshold);
2152 opendata->f_attr.mdsthreshold = NULL;
2154 nfs4_opendata_put(opendata);
2155 nfs4_put_state_owner(sp);
2159 kfree(opendata->f_attr.mdsthreshold);
2160 nfs4_opendata_put(opendata);
2161 err_put_state_owner:
2162 nfs4_put_state_owner(sp);
2169 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2170 struct dentry *dentry,
2173 struct iattr *sattr,
2174 struct rpc_cred *cred,
2175 struct nfs4_threshold **ctx_th)
2177 struct nfs4_exception exception = { };
2178 struct nfs4_state *res;
2181 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
2183 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2187 /* NOTE: BAD_SEQID means the server and client disagree about the
2188 * book-keeping w.r.t. state-changing operations
2189 * (OPEN/CLOSE/LOCK/LOCKU...)
2190 * It is actually a sign of a bug on the client or on the server.
2192 * If we receive a BAD_SEQID error in the particular case of
2193 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2194 * have unhashed the old state_owner for us, and that we can
2195 * therefore safely retry using a new one. We should still warn
2196 * the user though...
2198 if (status == -NFS4ERR_BAD_SEQID) {
2199 pr_warn_ratelimited("NFS: v4 server %s "
2200 " returned a bad sequence-id error!\n",
2201 NFS_SERVER(dir)->nfs_client->cl_hostname);
2202 exception.retry = 1;
2206 * BAD_STATEID on OPEN means that the server cancelled our
2207 * state before it received the OPEN_CONFIRM.
2208 * Recover by retrying the request as per the discussion
2209 * on Page 181 of RFC3530.
2211 if (status == -NFS4ERR_BAD_STATEID) {
2212 exception.retry = 1;
2215 if (status == -EAGAIN) {
2216 /* We must have found a delegation */
2217 exception.retry = 1;
2220 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2221 status, &exception));
2222 } while (exception.retry);
2226 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2227 struct nfs_fattr *fattr, struct iattr *sattr,
2228 struct nfs4_state *state)
2230 struct nfs_server *server = NFS_SERVER(inode);
2231 struct nfs_setattrargs arg = {
2232 .fh = NFS_FH(inode),
2235 .bitmask = server->attr_bitmask,
2237 struct nfs_setattrres res = {
2241 struct rpc_message msg = {
2242 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2247 unsigned long timestamp = jiffies;
2250 nfs_fattr_init(fattr);
2252 if (state != NULL) {
2253 struct nfs_lockowner lockowner = {
2254 .l_owner = current->files,
2255 .l_pid = current->tgid,
2257 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2259 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2261 /* Use that stateid */
2263 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2265 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2266 if (status == 0 && state != NULL)
2267 renew_lease(server, timestamp);
2271 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2272 struct nfs_fattr *fattr, struct iattr *sattr,
2273 struct nfs4_state *state)
2275 struct nfs_server *server = NFS_SERVER(inode);
2276 struct nfs4_exception exception = {
2282 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2284 case -NFS4ERR_OPENMODE:
2285 if (state && !(state->state & FMODE_WRITE)) {
2287 if (sattr->ia_valid & ATTR_OPEN)
2292 err = nfs4_handle_exception(server, err, &exception);
2293 } while (exception.retry);
2298 struct nfs4_closedata {
2299 struct inode *inode;
2300 struct nfs4_state *state;
2301 struct nfs_closeargs arg;
2302 struct nfs_closeres res;
2303 struct nfs_fattr fattr;
2304 unsigned long timestamp;
2309 static void nfs4_free_closedata(void *data)
2311 struct nfs4_closedata *calldata = data;
2312 struct nfs4_state_owner *sp = calldata->state->owner;
2313 struct super_block *sb = calldata->state->inode->i_sb;
2316 pnfs_roc_release(calldata->state->inode);
2317 nfs4_put_open_state(calldata->state);
2318 nfs_free_seqid(calldata->arg.seqid);
2319 nfs4_put_state_owner(sp);
2320 nfs_sb_deactive_async(sb);
2324 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2327 spin_lock(&state->owner->so_lock);
2328 if (!(fmode & FMODE_READ))
2329 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2330 if (!(fmode & FMODE_WRITE))
2331 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2332 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2333 spin_unlock(&state->owner->so_lock);
2336 static void nfs4_close_done(struct rpc_task *task, void *data)
2338 struct nfs4_closedata *calldata = data;
2339 struct nfs4_state *state = calldata->state;
2340 struct nfs_server *server = NFS_SERVER(calldata->inode);
2342 dprintk("%s: begin!\n", __func__);
2343 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2345 /* hmm. we are done with the inode, and in the process of freeing
2346 * the state_owner. we keep this around to process errors
2348 switch (task->tk_status) {
2351 pnfs_roc_set_barrier(state->inode,
2352 calldata->roc_barrier);
2353 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2354 renew_lease(server, calldata->timestamp);
2355 nfs4_close_clear_stateid_flags(state,
2356 calldata->arg.fmode);
2358 case -NFS4ERR_STALE_STATEID:
2359 case -NFS4ERR_OLD_STATEID:
2360 case -NFS4ERR_BAD_STATEID:
2361 case -NFS4ERR_EXPIRED:
2362 if (calldata->arg.fmode == 0)
2365 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2366 rpc_restart_call_prepare(task);
2368 nfs_release_seqid(calldata->arg.seqid);
2369 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2370 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2373 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2375 struct nfs4_closedata *calldata = data;
2376 struct nfs4_state *state = calldata->state;
2377 struct inode *inode = calldata->inode;
2380 dprintk("%s: begin!\n", __func__);
2381 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2384 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2385 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2386 spin_lock(&state->owner->so_lock);
2387 /* Calculate the change in open mode */
2388 if (state->n_rdwr == 0) {
2389 if (state->n_rdonly == 0) {
2390 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2391 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2392 calldata->arg.fmode &= ~FMODE_READ;
2394 if (state->n_wronly == 0) {
2395 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2396 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2397 calldata->arg.fmode &= ~FMODE_WRITE;
2400 spin_unlock(&state->owner->so_lock);
2403 /* Note: exit _without_ calling nfs4_close_done */
2404 task->tk_action = NULL;
2408 if (calldata->arg.fmode == 0) {
2409 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2410 if (calldata->roc &&
2411 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
2415 nfs_fattr_init(calldata->res.fattr);
2416 calldata->timestamp = jiffies;
2417 if (nfs4_setup_sequence(NFS_SERVER(inode),
2418 &calldata->arg.seq_args,
2419 &calldata->res.seq_res,
2421 nfs_release_seqid(calldata->arg.seqid);
2423 rpc_call_start(task);
2425 dprintk("%s: done!\n", __func__);
2428 static const struct rpc_call_ops nfs4_close_ops = {
2429 .rpc_call_prepare = nfs4_close_prepare,
2430 .rpc_call_done = nfs4_close_done,
2431 .rpc_release = nfs4_free_closedata,
2435 * It is possible for data to be read/written from a mem-mapped file
2436 * after the sys_close call (which hits the vfs layer as a flush).
2437 * This means that we can't safely call nfsv4 close on a file until
2438 * the inode is cleared. This in turn means that we are not good
2439 * NFSv4 citizens - we do not indicate to the server to update the file's
2440 * share state even when we are done with one of the three share
2441 * stateid's in the inode.
2443 * NOTE: Caller must be holding the sp->so_owner semaphore!
2445 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2447 struct nfs_server *server = NFS_SERVER(state->inode);
2448 struct nfs4_closedata *calldata;
2449 struct nfs4_state_owner *sp = state->owner;
2450 struct rpc_task *task;
2451 struct rpc_message msg = {
2452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2453 .rpc_cred = state->owner->so_cred,
2455 struct rpc_task_setup task_setup_data = {
2456 .rpc_client = server->client,
2457 .rpc_message = &msg,
2458 .callback_ops = &nfs4_close_ops,
2459 .workqueue = nfsiod_workqueue,
2460 .flags = RPC_TASK_ASYNC,
2462 int status = -ENOMEM;
2464 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2465 if (calldata == NULL)
2467 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2468 calldata->inode = state->inode;
2469 calldata->state = state;
2470 calldata->arg.fh = NFS_FH(state->inode);
2471 calldata->arg.stateid = &state->open_stateid;
2472 /* Serialization for the sequence id */
2473 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2474 if (calldata->arg.seqid == NULL)
2475 goto out_free_calldata;
2476 calldata->arg.fmode = 0;
2477 calldata->arg.bitmask = server->cache_consistency_bitmask;
2478 calldata->res.fattr = &calldata->fattr;
2479 calldata->res.seqid = calldata->arg.seqid;
2480 calldata->res.server = server;
2481 calldata->roc = pnfs_roc(state->inode);
2482 nfs_sb_active(calldata->inode->i_sb);
2484 msg.rpc_argp = &calldata->arg;
2485 msg.rpc_resp = &calldata->res;
2486 task_setup_data.callback_data = calldata;
2487 task = rpc_run_task(&task_setup_data);
2489 return PTR_ERR(task);
2492 status = rpc_wait_for_completion_task(task);
2498 nfs4_put_open_state(state);
2499 nfs4_put_state_owner(sp);
2503 static struct inode *
2504 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2506 struct nfs4_state *state;
2508 /* Protect against concurrent sillydeletes */
2509 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2510 ctx->cred, &ctx->mdsthreshold);
2512 return ERR_CAST(state);
2514 return igrab(state->inode);
2517 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2519 if (ctx->state == NULL)
2522 nfs4_close_sync(ctx->state, ctx->mode);
2524 nfs4_close_state(ctx->state, ctx->mode);
2527 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2529 struct nfs4_server_caps_arg args = {
2532 struct nfs4_server_caps_res res = {};
2533 struct rpc_message msg = {
2534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2540 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2542 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2543 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2544 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2545 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2546 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2547 NFS_CAP_CTIME|NFS_CAP_MTIME);
2548 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2549 server->caps |= NFS_CAP_ACLS;
2550 if (res.has_links != 0)
2551 server->caps |= NFS_CAP_HARDLINKS;
2552 if (res.has_symlinks != 0)
2553 server->caps |= NFS_CAP_SYMLINKS;
2554 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2555 server->caps |= NFS_CAP_FILEID;
2556 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2557 server->caps |= NFS_CAP_MODE;
2558 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2559 server->caps |= NFS_CAP_NLINK;
2560 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2561 server->caps |= NFS_CAP_OWNER;
2562 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2563 server->caps |= NFS_CAP_OWNER_GROUP;
2564 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2565 server->caps |= NFS_CAP_ATIME;
2566 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2567 server->caps |= NFS_CAP_CTIME;
2568 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2569 server->caps |= NFS_CAP_MTIME;
2571 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2572 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2573 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2574 server->acl_bitmask = res.acl_bitmask;
2575 server->fh_expire_type = res.fh_expire_type;
2581 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2583 struct nfs4_exception exception = { };
2586 err = nfs4_handle_exception(server,
2587 _nfs4_server_capabilities(server, fhandle),
2589 } while (exception.retry);
2593 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2594 struct nfs_fsinfo *info)
2596 struct nfs4_lookup_root_arg args = {
2597 .bitmask = nfs4_fattr_bitmap,
2599 struct nfs4_lookup_res res = {
2601 .fattr = info->fattr,
2604 struct rpc_message msg = {
2605 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2610 nfs_fattr_init(info->fattr);
2611 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2614 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2615 struct nfs_fsinfo *info)
2617 struct nfs4_exception exception = { };
2620 err = _nfs4_lookup_root(server, fhandle, info);
2623 case -NFS4ERR_WRONGSEC:
2626 err = nfs4_handle_exception(server, err, &exception);
2628 } while (exception.retry);
2633 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2634 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2636 struct rpc_auth *auth;
2639 auth = rpcauth_create(flavor, server->client);
2644 ret = nfs4_lookup_root(server, fhandle, info);
2649 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2650 struct nfs_fsinfo *info)
2652 int i, len, status = 0;
2653 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2655 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2659 for (i = 0; i < len; i++) {
2660 /* AUTH_UNIX is the default flavor if none was specified,
2661 * thus has already been tried. */
2662 if (flav_array[i] == RPC_AUTH_UNIX)
2665 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2666 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2671 * -EACCESS could mean that the user doesn't have correct permissions
2672 * to access the mount. It could also mean that we tried to mount
2673 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2674 * existing mount programs don't handle -EACCES very well so it should
2675 * be mapped to -EPERM instead.
2677 if (status == -EACCES)
2683 * get the file handle for the "/" directory on the server
2685 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2686 struct nfs_fsinfo *info)
2688 int minor_version = server->nfs_client->cl_minorversion;
2689 int status = nfs4_lookup_root(server, fhandle, info);
2690 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2692 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2693 * by nfs4_map_errors() as this function exits.
2695 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2697 status = nfs4_server_capabilities(server, fhandle);
2699 status = nfs4_do_fsinfo(server, fhandle, info);
2700 return nfs4_map_errors(status);
2703 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2704 struct nfs_fsinfo *info)
2707 struct nfs_fattr *fattr = info->fattr;
2709 error = nfs4_server_capabilities(server, mntfh);
2711 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2715 error = nfs4_proc_getattr(server, mntfh, fattr);
2717 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2721 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2722 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2723 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2729 * Get locations and (maybe) other attributes of a referral.
2730 * Note that we'll actually follow the referral later when
2731 * we detect fsid mismatch in inode revalidation
2733 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2734 const struct qstr *name, struct nfs_fattr *fattr,
2735 struct nfs_fh *fhandle)
2737 int status = -ENOMEM;
2738 struct page *page = NULL;
2739 struct nfs4_fs_locations *locations = NULL;
2741 page = alloc_page(GFP_KERNEL);
2744 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2745 if (locations == NULL)
2748 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2751 /* Make sure server returned a different fsid for the referral */
2752 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2753 dprintk("%s: server did not return a different fsid for"
2754 " a referral at %s\n", __func__, name->name);
2758 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2759 nfs_fixup_referral_attributes(&locations->fattr);
2761 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2762 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2763 memset(fhandle, 0, sizeof(struct nfs_fh));
2771 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2773 struct nfs4_getattr_arg args = {
2775 .bitmask = server->attr_bitmask,
2777 struct nfs4_getattr_res res = {
2781 struct rpc_message msg = {
2782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2787 nfs_fattr_init(fattr);
2788 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2791 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2793 struct nfs4_exception exception = { };
2796 err = nfs4_handle_exception(server,
2797 _nfs4_proc_getattr(server, fhandle, fattr),
2799 } while (exception.retry);
2804 * The file is not closed if it is opened due to the a request to change
2805 * the size of the file. The open call will not be needed once the
2806 * VFS layer lookup-intents are implemented.
2808 * Close is called when the inode is destroyed.
2809 * If we haven't opened the file for O_WRONLY, we
2810 * need to in the size_change case to obtain a stateid.
2813 * Because OPEN is always done by name in nfsv4, it is
2814 * possible that we opened a different file by the same
2815 * name. We can recognize this race condition, but we
2816 * can't do anything about it besides returning an error.
2818 * This will be fixed with VFS changes (lookup-intent).
2821 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2822 struct iattr *sattr)
2824 struct inode *inode = dentry->d_inode;
2825 struct rpc_cred *cred = NULL;
2826 struct nfs4_state *state = NULL;
2829 if (pnfs_ld_layoutret_on_setattr(inode))
2830 pnfs_return_layout(inode);
2832 nfs_fattr_init(fattr);
2834 /* Deal with open(O_TRUNC) */
2835 if (sattr->ia_valid & ATTR_OPEN)
2836 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2838 /* Optimization: if the end result is no change, don't RPC */
2839 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2842 /* Search for an existing open(O_WRITE) file */
2843 if (sattr->ia_valid & ATTR_FILE) {
2844 struct nfs_open_context *ctx;
2846 ctx = nfs_file_open_context(sattr->ia_file);
2853 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2855 nfs_setattr_update_inode(inode, sattr);
2859 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2860 const struct qstr *name, struct nfs_fh *fhandle,
2861 struct nfs_fattr *fattr)
2863 struct nfs_server *server = NFS_SERVER(dir);
2865 struct nfs4_lookup_arg args = {
2866 .bitmask = server->attr_bitmask,
2867 .dir_fh = NFS_FH(dir),
2870 struct nfs4_lookup_res res = {
2875 struct rpc_message msg = {
2876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2881 nfs_fattr_init(fattr);
2883 dprintk("NFS call lookup %s\n", name->name);
2884 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2885 dprintk("NFS reply lookup: %d\n", status);
2889 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2891 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2892 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2893 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2897 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2898 struct qstr *name, struct nfs_fh *fhandle,
2899 struct nfs_fattr *fattr)
2901 struct nfs4_exception exception = { };
2902 struct rpc_clnt *client = *clnt;
2905 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2907 case -NFS4ERR_BADNAME:
2910 case -NFS4ERR_MOVED:
2911 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
2913 case -NFS4ERR_WRONGSEC:
2915 if (client != *clnt)
2918 client = nfs4_create_sec_client(client, dir, name);
2920 return PTR_ERR(client);
2922 exception.retry = 1;
2925 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2927 } while (exception.retry);
2932 else if (client != *clnt)
2933 rpc_shutdown_client(client);
2938 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
2939 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2942 struct rpc_clnt *client = NFS_CLIENT(dir);
2944 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2945 if (client != NFS_CLIENT(dir)) {
2946 rpc_shutdown_client(client);
2947 nfs_fixup_secinfo_attributes(fattr);
2953 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2954 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2957 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2959 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2961 rpc_shutdown_client(client);
2962 return ERR_PTR(status);
2967 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2969 struct nfs_server *server = NFS_SERVER(inode);
2970 struct nfs4_accessargs args = {
2971 .fh = NFS_FH(inode),
2972 .bitmask = server->cache_consistency_bitmask,
2974 struct nfs4_accessres res = {
2977 struct rpc_message msg = {
2978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2981 .rpc_cred = entry->cred,
2983 int mode = entry->mask;
2987 * Determine which access bits we want to ask for...
2989 if (mode & MAY_READ)
2990 args.access |= NFS4_ACCESS_READ;
2991 if (S_ISDIR(inode->i_mode)) {
2992 if (mode & MAY_WRITE)
2993 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2994 if (mode & MAY_EXEC)
2995 args.access |= NFS4_ACCESS_LOOKUP;
2997 if (mode & MAY_WRITE)
2998 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2999 if (mode & MAY_EXEC)
3000 args.access |= NFS4_ACCESS_EXECUTE;
3003 res.fattr = nfs_alloc_fattr();
3004 if (res.fattr == NULL)
3007 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3009 nfs_access_set_mask(entry, res.access);
3010 nfs_refresh_inode(inode, res.fattr);
3012 nfs_free_fattr(res.fattr);
3016 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3018 struct nfs4_exception exception = { };
3021 err = nfs4_handle_exception(NFS_SERVER(inode),
3022 _nfs4_proc_access(inode, entry),
3024 } while (exception.retry);
3029 * TODO: For the time being, we don't try to get any attributes
3030 * along with any of the zero-copy operations READ, READDIR,
3033 * In the case of the first three, we want to put the GETATTR
3034 * after the read-type operation -- this is because it is hard
3035 * to predict the length of a GETATTR response in v4, and thus
3036 * align the READ data correctly. This means that the GETATTR
3037 * may end up partially falling into the page cache, and we should
3038 * shift it into the 'tail' of the xdr_buf before processing.
3039 * To do this efficiently, we need to know the total length
3040 * of data received, which doesn't seem to be available outside
3043 * In the case of WRITE, we also want to put the GETATTR after
3044 * the operation -- in this case because we want to make sure
3045 * we get the post-operation mtime and size.
3047 * Both of these changes to the XDR layer would in fact be quite
3048 * minor, but I decided to leave them for a subsequent patch.
3050 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3051 unsigned int pgbase, unsigned int pglen)
3053 struct nfs4_readlink args = {
3054 .fh = NFS_FH(inode),
3059 struct nfs4_readlink_res res;
3060 struct rpc_message msg = {
3061 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3066 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3069 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3070 unsigned int pgbase, unsigned int pglen)
3072 struct nfs4_exception exception = { };
3075 err = nfs4_handle_exception(NFS_SERVER(inode),
3076 _nfs4_proc_readlink(inode, page, pgbase, pglen),
3078 } while (exception.retry);
3083 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3086 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3089 struct nfs_open_context *ctx;
3090 struct nfs4_state *state;
3093 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3095 return PTR_ERR(ctx);
3097 sattr->ia_mode &= ~current_umask();
3098 state = nfs4_do_open(dir, dentry, ctx->mode,
3099 flags, sattr, ctx->cred,
3100 &ctx->mdsthreshold);
3102 if (IS_ERR(state)) {
3103 status = PTR_ERR(state);
3106 d_add(dentry, igrab(state->inode));
3107 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
3110 put_nfs_open_context(ctx);
3114 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3116 struct nfs_server *server = NFS_SERVER(dir);
3117 struct nfs_removeargs args = {
3121 struct nfs_removeres res = {
3124 struct rpc_message msg = {
3125 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3131 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3133 update_changeattr(dir, &res.cinfo);
3137 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3139 struct nfs4_exception exception = { };
3142 err = nfs4_handle_exception(NFS_SERVER(dir),
3143 _nfs4_proc_remove(dir, name),
3145 } while (exception.retry);
3149 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3151 struct nfs_server *server = NFS_SERVER(dir);
3152 struct nfs_removeargs *args = msg->rpc_argp;
3153 struct nfs_removeres *res = msg->rpc_resp;
3155 res->server = server;
3156 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3157 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
3160 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3162 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
3163 &data->args.seq_args,
3167 rpc_call_start(task);
3170 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3172 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3174 if (!nfs4_sequence_done(task, &res->seq_res))
3176 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3178 update_changeattr(dir, &res->cinfo);
3182 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3184 struct nfs_server *server = NFS_SERVER(dir);
3185 struct nfs_renameargs *arg = msg->rpc_argp;
3186 struct nfs_renameres *res = msg->rpc_resp;
3188 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3189 res->server = server;
3190 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
3193 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3195 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3196 &data->args.seq_args,
3200 rpc_call_start(task);
3203 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3204 struct inode *new_dir)
3206 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3208 if (!nfs4_sequence_done(task, &res->seq_res))
3210 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3213 update_changeattr(old_dir, &res->old_cinfo);
3214 update_changeattr(new_dir, &res->new_cinfo);
3218 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3219 struct inode *new_dir, struct qstr *new_name)
3221 struct nfs_server *server = NFS_SERVER(old_dir);
3222 struct nfs_renameargs arg = {
3223 .old_dir = NFS_FH(old_dir),
3224 .new_dir = NFS_FH(new_dir),
3225 .old_name = old_name,
3226 .new_name = new_name,
3228 struct nfs_renameres res = {
3231 struct rpc_message msg = {
3232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3236 int status = -ENOMEM;
3238 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3240 update_changeattr(old_dir, &res.old_cinfo);
3241 update_changeattr(new_dir, &res.new_cinfo);
3246 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3247 struct inode *new_dir, struct qstr *new_name)
3249 struct nfs4_exception exception = { };
3252 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3253 _nfs4_proc_rename(old_dir, old_name,
3256 } while (exception.retry);
3260 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3262 struct nfs_server *server = NFS_SERVER(inode);
3263 struct nfs4_link_arg arg = {
3264 .fh = NFS_FH(inode),
3265 .dir_fh = NFS_FH(dir),
3267 .bitmask = server->attr_bitmask,
3269 struct nfs4_link_res res = {
3272 struct rpc_message msg = {
3273 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3277 int status = -ENOMEM;
3279 res.fattr = nfs_alloc_fattr();
3280 if (res.fattr == NULL)
3283 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3285 update_changeattr(dir, &res.cinfo);
3286 nfs_post_op_update_inode(inode, res.fattr);
3289 nfs_free_fattr(res.fattr);
3293 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3295 struct nfs4_exception exception = { };
3298 err = nfs4_handle_exception(NFS_SERVER(inode),
3299 _nfs4_proc_link(inode, dir, name),
3301 } while (exception.retry);
3305 struct nfs4_createdata {
3306 struct rpc_message msg;
3307 struct nfs4_create_arg arg;
3308 struct nfs4_create_res res;
3310 struct nfs_fattr fattr;
3313 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3314 struct qstr *name, struct iattr *sattr, u32 ftype)
3316 struct nfs4_createdata *data;
3318 data = kzalloc(sizeof(*data), GFP_KERNEL);
3320 struct nfs_server *server = NFS_SERVER(dir);
3322 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3323 data->msg.rpc_argp = &data->arg;
3324 data->msg.rpc_resp = &data->res;
3325 data->arg.dir_fh = NFS_FH(dir);
3326 data->arg.server = server;
3327 data->arg.name = name;
3328 data->arg.attrs = sattr;
3329 data->arg.ftype = ftype;
3330 data->arg.bitmask = server->attr_bitmask;
3331 data->res.server = server;
3332 data->res.fh = &data->fh;
3333 data->res.fattr = &data->fattr;
3334 nfs_fattr_init(data->res.fattr);
3339 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3341 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3342 &data->arg.seq_args, &data->res.seq_res, 1);
3344 update_changeattr(dir, &data->res.dir_cinfo);
3345 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3350 static void nfs4_free_createdata(struct nfs4_createdata *data)
3355 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3356 struct page *page, unsigned int len, struct iattr *sattr)
3358 struct nfs4_createdata *data;
3359 int status = -ENAMETOOLONG;
3361 if (len > NFS4_MAXPATHLEN)
3365 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3369 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3370 data->arg.u.symlink.pages = &page;
3371 data->arg.u.symlink.len = len;
3373 status = nfs4_do_create(dir, dentry, data);
3375 nfs4_free_createdata(data);
3380 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3381 struct page *page, unsigned int len, struct iattr *sattr)
3383 struct nfs4_exception exception = { };
3386 err = nfs4_handle_exception(NFS_SERVER(dir),
3387 _nfs4_proc_symlink(dir, dentry, page,
3390 } while (exception.retry);
3394 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3395 struct iattr *sattr)
3397 struct nfs4_createdata *data;
3398 int status = -ENOMEM;
3400 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3404 status = nfs4_do_create(dir, dentry, data);
3406 nfs4_free_createdata(data);
3411 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3412 struct iattr *sattr)
3414 struct nfs4_exception exception = { };
3417 sattr->ia_mode &= ~current_umask();
3419 err = nfs4_handle_exception(NFS_SERVER(dir),
3420 _nfs4_proc_mkdir(dir, dentry, sattr),
3422 } while (exception.retry);
3426 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3427 u64 cookie, struct page **pages, unsigned int count, int plus)
3429 struct inode *dir = dentry->d_inode;
3430 struct nfs4_readdir_arg args = {
3435 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3438 struct nfs4_readdir_res res;
3439 struct rpc_message msg = {
3440 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3447 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3448 dentry->d_parent->d_name.name,
3449 dentry->d_name.name,
3450 (unsigned long long)cookie);
3451 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3452 res.pgbase = args.pgbase;
3453 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3455 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3456 status += args.pgbase;
3459 nfs_invalidate_atime(dir);
3461 dprintk("%s: returns %d\n", __func__, status);
3465 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3466 u64 cookie, struct page **pages, unsigned int count, int plus)
3468 struct nfs4_exception exception = { };
3471 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3472 _nfs4_proc_readdir(dentry, cred, cookie,
3473 pages, count, plus),
3475 } while (exception.retry);
3479 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3480 struct iattr *sattr, dev_t rdev)
3482 struct nfs4_createdata *data;
3483 int mode = sattr->ia_mode;
3484 int status = -ENOMEM;
3486 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3491 data->arg.ftype = NF4FIFO;
3492 else if (S_ISBLK(mode)) {
3493 data->arg.ftype = NF4BLK;
3494 data->arg.u.device.specdata1 = MAJOR(rdev);
3495 data->arg.u.device.specdata2 = MINOR(rdev);
3497 else if (S_ISCHR(mode)) {
3498 data->arg.ftype = NF4CHR;
3499 data->arg.u.device.specdata1 = MAJOR(rdev);
3500 data->arg.u.device.specdata2 = MINOR(rdev);
3501 } else if (!S_ISSOCK(mode)) {
3506 status = nfs4_do_create(dir, dentry, data);
3508 nfs4_free_createdata(data);
3513 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3514 struct iattr *sattr, dev_t rdev)
3516 struct nfs4_exception exception = { };
3519 sattr->ia_mode &= ~current_umask();
3521 err = nfs4_handle_exception(NFS_SERVER(dir),
3522 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3524 } while (exception.retry);
3528 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3529 struct nfs_fsstat *fsstat)
3531 struct nfs4_statfs_arg args = {
3533 .bitmask = server->attr_bitmask,
3535 struct nfs4_statfs_res res = {
3538 struct rpc_message msg = {
3539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3544 nfs_fattr_init(fsstat->fattr);
3545 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3548 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3550 struct nfs4_exception exception = { };
3553 err = nfs4_handle_exception(server,
3554 _nfs4_proc_statfs(server, fhandle, fsstat),
3556 } while (exception.retry);
3560 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3561 struct nfs_fsinfo *fsinfo)
3563 struct nfs4_fsinfo_arg args = {
3565 .bitmask = server->attr_bitmask,
3567 struct nfs4_fsinfo_res res = {
3570 struct rpc_message msg = {
3571 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3576 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3579 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3581 struct nfs4_exception exception = { };
3585 err = nfs4_handle_exception(server,
3586 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3588 } while (exception.retry);
3592 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3596 nfs_fattr_init(fsinfo->fattr);
3597 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3599 /* block layout checks this! */
3600 server->pnfs_blksize = fsinfo->blksize;
3601 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3607 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3608 struct nfs_pathconf *pathconf)
3610 struct nfs4_pathconf_arg args = {
3612 .bitmask = server->attr_bitmask,
3614 struct nfs4_pathconf_res res = {
3615 .pathconf = pathconf,
3617 struct rpc_message msg = {
3618 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3623 /* None of the pathconf attributes are mandatory to implement */
3624 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3625 memset(pathconf, 0, sizeof(*pathconf));
3629 nfs_fattr_init(pathconf->fattr);
3630 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3633 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3634 struct nfs_pathconf *pathconf)
3636 struct nfs4_exception exception = { };
3640 err = nfs4_handle_exception(server,
3641 _nfs4_proc_pathconf(server, fhandle, pathconf),
3643 } while (exception.retry);
3647 void __nfs4_read_done_cb(struct nfs_read_data *data)
3649 nfs_invalidate_atime(data->header->inode);
3652 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3654 struct nfs_server *server = NFS_SERVER(data->header->inode);
3656 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3657 rpc_restart_call_prepare(task);
3661 __nfs4_read_done_cb(data);
3662 if (task->tk_status > 0)
3663 renew_lease(server, data->timestamp);
3667 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3670 dprintk("--> %s\n", __func__);
3672 if (!nfs4_sequence_done(task, &data->res.seq_res))
3675 return data->read_done_cb ? data->read_done_cb(task, data) :
3676 nfs4_read_done_cb(task, data);
3679 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3681 data->timestamp = jiffies;
3682 data->read_done_cb = nfs4_read_done_cb;
3683 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3684 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3687 static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3689 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3690 &data->args.seq_args,
3694 rpc_call_start(task);
3697 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3699 struct inode *inode = data->header->inode;
3701 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3702 rpc_restart_call_prepare(task);
3705 if (task->tk_status >= 0) {
3706 renew_lease(NFS_SERVER(inode), data->timestamp);
3707 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
3712 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3714 if (!nfs4_sequence_done(task, &data->res.seq_res))
3716 return data->write_done_cb ? data->write_done_cb(task, data) :
3717 nfs4_write_done_cb(task, data);
3721 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3723 const struct nfs_pgio_header *hdr = data->header;
3725 /* Don't request attributes for pNFS or O_DIRECT writes */
3726 if (data->ds_clp != NULL || hdr->dreq != NULL)
3728 /* Otherwise, request attributes if and only if we don't hold
3731 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
3734 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3736 struct nfs_server *server = NFS_SERVER(data->header->inode);
3738 if (!nfs4_write_need_cache_consistency_data(data)) {
3739 data->args.bitmask = NULL;
3740 data->res.fattr = NULL;
3742 data->args.bitmask = server->cache_consistency_bitmask;
3744 if (!data->write_done_cb)
3745 data->write_done_cb = nfs4_write_done_cb;
3746 data->res.server = server;
3747 data->timestamp = jiffies;
3749 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3750 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3753 static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3755 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3756 &data->args.seq_args,
3760 rpc_call_start(task);
3763 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3765 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3766 &data->args.seq_args,
3770 rpc_call_start(task);
3773 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
3775 struct inode *inode = data->inode;
3777 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3778 rpc_restart_call_prepare(task);
3784 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
3786 if (!nfs4_sequence_done(task, &data->res.seq_res))
3788 return data->commit_done_cb(task, data);
3791 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
3793 struct nfs_server *server = NFS_SERVER(data->inode);
3795 if (data->commit_done_cb == NULL)
3796 data->commit_done_cb = nfs4_commit_done_cb;
3797 data->res.server = server;
3798 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3799 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3802 struct nfs4_renewdata {
3803 struct nfs_client *client;
3804 unsigned long timestamp;
3808 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3809 * standalone procedure for queueing an asynchronous RENEW.
3811 static void nfs4_renew_release(void *calldata)
3813 struct nfs4_renewdata *data = calldata;
3814 struct nfs_client *clp = data->client;
3816 if (atomic_read(&clp->cl_count) > 1)
3817 nfs4_schedule_state_renewal(clp);
3818 nfs_put_client(clp);
3822 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3824 struct nfs4_renewdata *data = calldata;
3825 struct nfs_client *clp = data->client;
3826 unsigned long timestamp = data->timestamp;
3828 if (task->tk_status < 0) {
3829 /* Unless we're shutting down, schedule state recovery! */
3830 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3832 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3833 nfs4_schedule_lease_recovery(clp);
3836 nfs4_schedule_path_down_recovery(clp);
3838 do_renew_lease(clp, timestamp);
3841 static const struct rpc_call_ops nfs4_renew_ops = {
3842 .rpc_call_done = nfs4_renew_done,
3843 .rpc_release = nfs4_renew_release,
3846 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3848 struct rpc_message msg = {
3849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3853 struct nfs4_renewdata *data;
3855 if (renew_flags == 0)
3857 if (!atomic_inc_not_zero(&clp->cl_count))
3859 data = kmalloc(sizeof(*data), GFP_NOFS);
3863 data->timestamp = jiffies;
3864 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3865 &nfs4_renew_ops, data);
3868 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3870 struct rpc_message msg = {
3871 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3875 unsigned long now = jiffies;
3878 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3881 do_renew_lease(clp, now);
3885 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3887 return (server->caps & NFS_CAP_ACLS)
3888 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3889 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3892 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3893 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
3896 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
3898 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3899 struct page **pages, unsigned int *pgbase)
3901 struct page *newpage, **spages;
3907 len = min_t(size_t, PAGE_SIZE, buflen);
3908 newpage = alloc_page(GFP_KERNEL);
3910 if (newpage == NULL)
3912 memcpy(page_address(newpage), buf, len);
3917 } while (buflen != 0);
3923 __free_page(spages[rc-1]);
3927 struct nfs4_cached_acl {
3933 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3935 struct nfs_inode *nfsi = NFS_I(inode);
3937 spin_lock(&inode->i_lock);
3938 kfree(nfsi->nfs4_acl);
3939 nfsi->nfs4_acl = acl;
3940 spin_unlock(&inode->i_lock);
3943 static void nfs4_zap_acl_attr(struct inode *inode)
3945 nfs4_set_cached_acl(inode, NULL);
3948 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3950 struct nfs_inode *nfsi = NFS_I(inode);
3951 struct nfs4_cached_acl *acl;
3954 spin_lock(&inode->i_lock);
3955 acl = nfsi->nfs4_acl;
3958 if (buf == NULL) /* user is just asking for length */
3960 if (acl->cached == 0)
3962 ret = -ERANGE; /* see getxattr(2) man page */
3963 if (acl->len > buflen)
3965 memcpy(buf, acl->data, acl->len);
3969 spin_unlock(&inode->i_lock);
3973 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3975 struct nfs4_cached_acl *acl;
3976 size_t buflen = sizeof(*acl) + acl_len;
3978 if (buflen <= PAGE_SIZE) {
3979 acl = kmalloc(buflen, GFP_KERNEL);
3983 _copy_from_pages(acl->data, pages, pgbase, acl_len);
3985 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3992 nfs4_set_cached_acl(inode, acl);
3996 * The getxattr API returns the required buffer length when called with a
3997 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3998 * the required buf. On a NULL buf, we send a page of data to the server
3999 * guessing that the ACL request can be serviced by a page. If so, we cache
4000 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4001 * the cache. If not so, we throw away the page, and cache the required
4002 * length. The next getxattr call will then produce another round trip to
4003 * the server, this time with the input buf of the required size.
4005 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4007 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
4008 struct nfs_getaclargs args = {
4009 .fh = NFS_FH(inode),
4013 struct nfs_getaclres res = {
4016 struct rpc_message msg = {
4017 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4021 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4022 int ret = -ENOMEM, i;
4024 /* As long as we're doing a round trip to the server anyway,
4025 * let's be prepared for a page of acl data. */
4028 if (npages > ARRAY_SIZE(pages))
4031 for (i = 0; i < npages; i++) {
4032 pages[i] = alloc_page(GFP_KERNEL);
4037 /* for decoding across pages */
4038 res.acl_scratch = alloc_page(GFP_KERNEL);
4039 if (!res.acl_scratch)
4042 args.acl_len = npages * PAGE_SIZE;
4043 args.acl_pgbase = 0;
4045 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
4046 __func__, buf, buflen, npages, args.acl_len);
4047 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4048 &msg, &args.seq_args, &res.seq_res, 0);
4052 /* Handle the case where the passed-in buffer is too short */
4053 if (res.acl_flags & NFS4_ACL_TRUNC) {
4054 /* Did the user only issue a request for the acl length? */
4060 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4062 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4066 for (i = 0; i < npages; i++)
4068 __free_page(pages[i]);
4069 if (res.acl_scratch)
4070 __free_page(res.acl_scratch);
4074 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4076 struct nfs4_exception exception = { };
4079 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4082 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4083 } while (exception.retry);
4087 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4089 struct nfs_server *server = NFS_SERVER(inode);
4092 if (!nfs4_server_supports_acls(server))
4094 ret = nfs_revalidate_inode(server, inode);
4097 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4098 nfs_zap_acl_cache(inode);
4099 ret = nfs4_read_cached_acl(inode, buf, buflen);
4101 /* -ENOENT is returned if there is no ACL or if there is an ACL
4102 * but no cached acl data, just the acl length */
4104 return nfs4_get_acl_uncached(inode, buf, buflen);
4107 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4109 struct nfs_server *server = NFS_SERVER(inode);
4110 struct page *pages[NFS4ACL_MAXPAGES];
4111 struct nfs_setaclargs arg = {
4112 .fh = NFS_FH(inode),
4116 struct nfs_setaclres res;
4117 struct rpc_message msg = {
4118 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4122 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4125 if (!nfs4_server_supports_acls(server))
4127 if (npages > ARRAY_SIZE(pages))
4129 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4132 nfs4_inode_return_delegation(inode);
4133 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4136 * Free each page after tx, so the only ref left is
4137 * held by the network stack
4140 put_page(pages[i-1]);
4143 * Acl update can result in inode attribute update.
4144 * so mark the attribute cache invalid.
4146 spin_lock(&inode->i_lock);
4147 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4148 spin_unlock(&inode->i_lock);
4149 nfs_access_zap_cache(inode);
4150 nfs_zap_acl_cache(inode);
4154 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4156 struct nfs4_exception exception = { };
4159 err = nfs4_handle_exception(NFS_SERVER(inode),
4160 __nfs4_proc_set_acl(inode, buf, buflen),
4162 } while (exception.retry);
4167 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
4169 struct nfs_client *clp = server->nfs_client;
4171 if (task->tk_status >= 0)
4173 switch(task->tk_status) {
4174 case -NFS4ERR_DELEG_REVOKED:
4175 case -NFS4ERR_ADMIN_REVOKED:
4176 case -NFS4ERR_BAD_STATEID:
4179 nfs_remove_bad_delegation(state->inode);
4180 case -NFS4ERR_OPENMODE:
4183 nfs4_schedule_stateid_recovery(server, state);
4184 goto wait_on_recovery;
4185 case -NFS4ERR_EXPIRED:
4187 nfs4_schedule_stateid_recovery(server, state);
4188 case -NFS4ERR_STALE_STATEID:
4189 case -NFS4ERR_STALE_CLIENTID:
4190 nfs4_schedule_lease_recovery(clp);
4191 goto wait_on_recovery;
4192 #if defined(CONFIG_NFS_V4_1)
4193 case -NFS4ERR_BADSESSION:
4194 case -NFS4ERR_BADSLOT:
4195 case -NFS4ERR_BAD_HIGH_SLOT:
4196 case -NFS4ERR_DEADSESSION:
4197 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4198 case -NFS4ERR_SEQ_FALSE_RETRY:
4199 case -NFS4ERR_SEQ_MISORDERED:
4200 dprintk("%s ERROR %d, Reset session\n", __func__,
4202 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4203 task->tk_status = 0;
4205 #endif /* CONFIG_NFS_V4_1 */
4206 case -NFS4ERR_DELAY:
4207 nfs_inc_server_stats(server, NFSIOS_DELAY);
4208 case -NFS4ERR_GRACE:
4210 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4211 task->tk_status = 0;
4213 case -NFS4ERR_RETRY_UNCACHED_REP:
4214 case -NFS4ERR_OLD_STATEID:
4215 task->tk_status = 0;
4218 task->tk_status = nfs4_map_errors(task->tk_status);
4221 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4222 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4223 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4224 task->tk_status = 0;
4228 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4229 nfs4_verifier *bootverf)
4233 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4234 /* An impossible timestamp guarantees this value
4235 * will never match a generated boot time. */
4237 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4239 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4240 verf[0] = (__be32)nn->boot_time.tv_sec;
4241 verf[1] = (__be32)nn->boot_time.tv_nsec;
4243 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4247 nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4248 char *buf, size_t len)
4250 unsigned int result;
4253 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4255 rpc_peeraddr2str(clp->cl_rpcclient,
4257 rpc_peeraddr2str(clp->cl_rpcclient,
4258 RPC_DISPLAY_PROTO));
4264 nfs4_init_uniform_client_string(const struct nfs_client *clp,
4265 char *buf, size_t len)
4267 char *nodename = clp->cl_rpcclient->cl_nodename;
4269 if (nfs4_client_id_uniquifier[0] != '\0')
4270 nodename = nfs4_client_id_uniquifier;
4271 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4272 clp->rpc_ops->version, clp->cl_minorversion,
4277 * nfs4_proc_setclientid - Negotiate client ID
4278 * @clp: state data structure
4279 * @program: RPC program for NFSv4 callback service
4280 * @port: IP port number for NFS4 callback service
4281 * @cred: RPC credential to use for this call
4282 * @res: where to place the result
4284 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4286 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4287 unsigned short port, struct rpc_cred *cred,
4288 struct nfs4_setclientid_res *res)
4290 nfs4_verifier sc_verifier;
4291 struct nfs4_setclientid setclientid = {
4292 .sc_verifier = &sc_verifier,
4294 .sc_cb_ident = clp->cl_cb_ident,
4296 struct rpc_message msg = {
4297 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4298 .rpc_argp = &setclientid,
4304 /* nfs_client_id4 */
4305 nfs4_init_boot_verifier(clp, &sc_verifier);
4306 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4307 setclientid.sc_name_len =
4308 nfs4_init_uniform_client_string(clp,
4309 setclientid.sc_name,
4310 sizeof(setclientid.sc_name));
4312 setclientid.sc_name_len =
4313 nfs4_init_nonuniform_client_string(clp,
4314 setclientid.sc_name,
4315 sizeof(setclientid.sc_name));
4318 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4319 sizeof(setclientid.sc_netid),
4320 rpc_peeraddr2str(clp->cl_rpcclient,
4321 RPC_DISPLAY_NETID));
4323 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4324 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4325 clp->cl_ipaddr, port >> 8, port & 255);
4327 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4328 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4329 setclientid.sc_name_len, setclientid.sc_name);
4330 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4331 dprintk("NFS reply setclientid: %d\n", status);
4336 * nfs4_proc_setclientid_confirm - Confirm client ID
4337 * @clp: state data structure
4338 * @res: result of a previous SETCLIENTID
4339 * @cred: RPC credential to use for this call
4341 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4343 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4344 struct nfs4_setclientid_res *arg,
4345 struct rpc_cred *cred)
4347 struct nfs_fsinfo fsinfo;
4348 struct rpc_message msg = {
4349 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4351 .rpc_resp = &fsinfo,
4357 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4358 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4361 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4363 spin_lock(&clp->cl_lock);
4364 clp->cl_lease_time = fsinfo.lease_time * HZ;
4365 clp->cl_last_renewal = now;
4366 spin_unlock(&clp->cl_lock);
4368 dprintk("NFS reply setclientid_confirm: %d\n", status);
4372 struct nfs4_delegreturndata {
4373 struct nfs4_delegreturnargs args;
4374 struct nfs4_delegreturnres res;
4376 nfs4_stateid stateid;
4377 unsigned long timestamp;
4378 struct nfs_fattr fattr;
4382 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4384 struct nfs4_delegreturndata *data = calldata;
4386 if (!nfs4_sequence_done(task, &data->res.seq_res))
4389 switch (task->tk_status) {
4390 case -NFS4ERR_STALE_STATEID:
4391 case -NFS4ERR_EXPIRED:
4393 renew_lease(data->res.server, data->timestamp);
4396 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4398 rpc_restart_call_prepare(task);
4402 data->rpc_status = task->tk_status;
4405 static void nfs4_delegreturn_release(void *calldata)
4410 #if defined(CONFIG_NFS_V4_1)
4411 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4413 struct nfs4_delegreturndata *d_data;
4415 d_data = (struct nfs4_delegreturndata *)data;
4417 if (nfs4_setup_sequence(d_data->res.server,
4418 &d_data->args.seq_args,
4419 &d_data->res.seq_res, task))
4421 rpc_call_start(task);
4423 #endif /* CONFIG_NFS_V4_1 */
4425 static const struct rpc_call_ops nfs4_delegreturn_ops = {
4426 #if defined(CONFIG_NFS_V4_1)
4427 .rpc_call_prepare = nfs4_delegreturn_prepare,
4428 #endif /* CONFIG_NFS_V4_1 */
4429 .rpc_call_done = nfs4_delegreturn_done,
4430 .rpc_release = nfs4_delegreturn_release,
4433 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4435 struct nfs4_delegreturndata *data;
4436 struct nfs_server *server = NFS_SERVER(inode);
4437 struct rpc_task *task;
4438 struct rpc_message msg = {
4439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4442 struct rpc_task_setup task_setup_data = {
4443 .rpc_client = server->client,
4444 .rpc_message = &msg,
4445 .callback_ops = &nfs4_delegreturn_ops,
4446 .flags = RPC_TASK_ASYNC,
4450 data = kzalloc(sizeof(*data), GFP_NOFS);
4453 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4454 data->args.fhandle = &data->fh;
4455 data->args.stateid = &data->stateid;
4456 data->args.bitmask = server->cache_consistency_bitmask;
4457 nfs_copy_fh(&data->fh, NFS_FH(inode));
4458 nfs4_stateid_copy(&data->stateid, stateid);
4459 data->res.fattr = &data->fattr;
4460 data->res.server = server;
4461 nfs_fattr_init(data->res.fattr);
4462 data->timestamp = jiffies;
4463 data->rpc_status = 0;
4465 task_setup_data.callback_data = data;
4466 msg.rpc_argp = &data->args;
4467 msg.rpc_resp = &data->res;
4468 task = rpc_run_task(&task_setup_data);
4470 return PTR_ERR(task);
4473 status = nfs4_wait_for_completion_rpc_task(task);
4476 status = data->rpc_status;
4478 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4480 nfs_refresh_inode(inode, &data->fattr);
4486 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4488 struct nfs_server *server = NFS_SERVER(inode);
4489 struct nfs4_exception exception = { };
4492 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4494 case -NFS4ERR_STALE_STATEID:
4495 case -NFS4ERR_EXPIRED:
4499 err = nfs4_handle_exception(server, err, &exception);
4500 } while (exception.retry);
4504 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4505 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4508 * sleep, with exponential backoff, and retry the LOCK operation.
4510 static unsigned long
4511 nfs4_set_lock_task_retry(unsigned long timeout)
4513 freezable_schedule_timeout_killable(timeout);
4515 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4516 return NFS4_LOCK_MAXTIMEOUT;
4520 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4522 struct inode *inode = state->inode;
4523 struct nfs_server *server = NFS_SERVER(inode);
4524 struct nfs_client *clp = server->nfs_client;
4525 struct nfs_lockt_args arg = {
4526 .fh = NFS_FH(inode),
4529 struct nfs_lockt_res res = {
4532 struct rpc_message msg = {
4533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4536 .rpc_cred = state->owner->so_cred,
4538 struct nfs4_lock_state *lsp;
4541 arg.lock_owner.clientid = clp->cl_clientid;
4542 status = nfs4_set_lock_state(state, request);
4545 lsp = request->fl_u.nfs4_fl.owner;
4546 arg.lock_owner.id = lsp->ls_seqid.owner_id;
4547 arg.lock_owner.s_dev = server->s_dev;
4548 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4551 request->fl_type = F_UNLCK;
4553 case -NFS4ERR_DENIED:
4556 request->fl_ops->fl_release_private(request);
4561 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4563 struct nfs4_exception exception = { };
4567 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4568 _nfs4_proc_getlk(state, cmd, request),
4570 } while (exception.retry);
4574 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4577 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4579 res = posix_lock_file_wait(file, fl);
4582 res = flock_lock_file_wait(file, fl);
4590 struct nfs4_unlockdata {
4591 struct nfs_locku_args arg;
4592 struct nfs_locku_res res;
4593 struct nfs4_lock_state *lsp;
4594 struct nfs_open_context *ctx;
4595 struct file_lock fl;
4596 const struct nfs_server *server;
4597 unsigned long timestamp;
4600 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4601 struct nfs_open_context *ctx,
4602 struct nfs4_lock_state *lsp,
4603 struct nfs_seqid *seqid)
4605 struct nfs4_unlockdata *p;
4606 struct inode *inode = lsp->ls_state->inode;
4608 p = kzalloc(sizeof(*p), GFP_NOFS);
4611 p->arg.fh = NFS_FH(inode);
4613 p->arg.seqid = seqid;
4614 p->res.seqid = seqid;
4615 p->arg.stateid = &lsp->ls_stateid;
4617 atomic_inc(&lsp->ls_count);
4618 /* Ensure we don't close file until we're done freeing locks! */
4619 p->ctx = get_nfs_open_context(ctx);
4620 memcpy(&p->fl, fl, sizeof(p->fl));
4621 p->server = NFS_SERVER(inode);
4625 static void nfs4_locku_release_calldata(void *data)
4627 struct nfs4_unlockdata *calldata = data;
4628 nfs_free_seqid(calldata->arg.seqid);
4629 nfs4_put_lock_state(calldata->lsp);
4630 put_nfs_open_context(calldata->ctx);
4634 static void nfs4_locku_done(struct rpc_task *task, void *data)
4636 struct nfs4_unlockdata *calldata = data;
4638 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4640 switch (task->tk_status) {
4642 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4643 &calldata->res.stateid);
4644 renew_lease(calldata->server, calldata->timestamp);
4646 case -NFS4ERR_BAD_STATEID:
4647 case -NFS4ERR_OLD_STATEID:
4648 case -NFS4ERR_STALE_STATEID:
4649 case -NFS4ERR_EXPIRED:
4652 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4653 rpc_restart_call_prepare(task);
4655 nfs_release_seqid(calldata->arg.seqid);
4658 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4660 struct nfs4_unlockdata *calldata = data;
4662 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4664 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
4665 /* Note: exit _without_ running nfs4_locku_done */
4666 task->tk_action = NULL;
4669 calldata->timestamp = jiffies;
4670 if (nfs4_setup_sequence(calldata->server,
4671 &calldata->arg.seq_args,
4672 &calldata->res.seq_res,
4674 nfs_release_seqid(calldata->arg.seqid);
4676 rpc_call_start(task);
4679 static const struct rpc_call_ops nfs4_locku_ops = {
4680 .rpc_call_prepare = nfs4_locku_prepare,
4681 .rpc_call_done = nfs4_locku_done,
4682 .rpc_release = nfs4_locku_release_calldata,
4685 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4686 struct nfs_open_context *ctx,
4687 struct nfs4_lock_state *lsp,
4688 struct nfs_seqid *seqid)
4690 struct nfs4_unlockdata *data;
4691 struct rpc_message msg = {
4692 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4693 .rpc_cred = ctx->cred,
4695 struct rpc_task_setup task_setup_data = {
4696 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4697 .rpc_message = &msg,
4698 .callback_ops = &nfs4_locku_ops,
4699 .workqueue = nfsiod_workqueue,
4700 .flags = RPC_TASK_ASYNC,
4703 /* Ensure this is an unlock - when canceling a lock, the
4704 * canceled lock is passed in, and it won't be an unlock.
4706 fl->fl_type = F_UNLCK;
4708 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4710 nfs_free_seqid(seqid);
4711 return ERR_PTR(-ENOMEM);
4714 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4715 msg.rpc_argp = &data->arg;
4716 msg.rpc_resp = &data->res;
4717 task_setup_data.callback_data = data;
4718 return rpc_run_task(&task_setup_data);
4721 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4723 struct nfs_inode *nfsi = NFS_I(state->inode);
4724 struct nfs_seqid *seqid;
4725 struct nfs4_lock_state *lsp;
4726 struct rpc_task *task;
4728 unsigned char fl_flags = request->fl_flags;
4730 status = nfs4_set_lock_state(state, request);
4731 /* Unlock _before_ we do the RPC call */
4732 request->fl_flags |= FL_EXISTS;
4733 down_read(&nfsi->rwsem);
4734 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4735 up_read(&nfsi->rwsem);
4738 up_read(&nfsi->rwsem);
4741 /* Is this a delegated lock? */
4742 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4744 lsp = request->fl_u.nfs4_fl.owner;
4745 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4749 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4750 status = PTR_ERR(task);
4753 status = nfs4_wait_for_completion_rpc_task(task);
4756 request->fl_flags = fl_flags;
4760 struct nfs4_lockdata {
4761 struct nfs_lock_args arg;
4762 struct nfs_lock_res res;
4763 struct nfs4_lock_state *lsp;
4764 struct nfs_open_context *ctx;
4765 struct file_lock fl;
4766 unsigned long timestamp;
4769 struct nfs_server *server;
4772 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4773 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4776 struct nfs4_lockdata *p;
4777 struct inode *inode = lsp->ls_state->inode;
4778 struct nfs_server *server = NFS_SERVER(inode);
4780 p = kzalloc(sizeof(*p), gfp_mask);
4784 p->arg.fh = NFS_FH(inode);
4786 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4787 if (p->arg.open_seqid == NULL)
4789 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4790 if (p->arg.lock_seqid == NULL)
4791 goto out_free_seqid;
4792 p->arg.lock_stateid = &lsp->ls_stateid;
4793 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4794 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4795 p->arg.lock_owner.s_dev = server->s_dev;
4796 p->res.lock_seqid = p->arg.lock_seqid;
4799 atomic_inc(&lsp->ls_count);
4800 p->ctx = get_nfs_open_context(ctx);
4801 memcpy(&p->fl, fl, sizeof(p->fl));
4804 nfs_free_seqid(p->arg.open_seqid);
4810 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4812 struct nfs4_lockdata *data = calldata;
4813 struct nfs4_state *state = data->lsp->ls_state;
4815 dprintk("%s: begin!\n", __func__);
4816 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4818 /* Do we need to do an open_to_lock_owner? */
4819 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4820 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4821 goto out_release_lock_seqid;
4822 data->arg.open_stateid = &state->stateid;
4823 data->arg.new_lock_owner = 1;
4824 data->res.open_seqid = data->arg.open_seqid;
4826 data->arg.new_lock_owner = 0;
4827 data->timestamp = jiffies;
4828 if (nfs4_setup_sequence(data->server,
4829 &data->arg.seq_args,
4832 rpc_call_start(task);
4835 nfs_release_seqid(data->arg.open_seqid);
4836 out_release_lock_seqid:
4837 nfs_release_seqid(data->arg.lock_seqid);
4838 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
4841 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4843 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4844 nfs4_lock_prepare(task, calldata);
4847 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4849 struct nfs4_lockdata *data = calldata;
4851 dprintk("%s: begin!\n", __func__);
4853 if (!nfs4_sequence_done(task, &data->res.seq_res))
4856 data->rpc_status = task->tk_status;
4857 if (data->arg.new_lock_owner != 0) {
4858 if (data->rpc_status == 0)
4859 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4863 if (data->rpc_status == 0) {
4864 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4865 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
4866 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4869 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4872 static void nfs4_lock_release(void *calldata)
4874 struct nfs4_lockdata *data = calldata;
4876 dprintk("%s: begin!\n", __func__);
4877 nfs_free_seqid(data->arg.open_seqid);
4878 if (data->cancelled != 0) {
4879 struct rpc_task *task;
4880 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4881 data->arg.lock_seqid);
4883 rpc_put_task_async(task);
4884 dprintk("%s: cancelling lock!\n", __func__);
4886 nfs_free_seqid(data->arg.lock_seqid);
4887 nfs4_put_lock_state(data->lsp);
4888 put_nfs_open_context(data->ctx);
4890 dprintk("%s: done!\n", __func__);
4893 static const struct rpc_call_ops nfs4_lock_ops = {
4894 .rpc_call_prepare = nfs4_lock_prepare,
4895 .rpc_call_done = nfs4_lock_done,
4896 .rpc_release = nfs4_lock_release,
4899 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4900 .rpc_call_prepare = nfs4_recover_lock_prepare,
4901 .rpc_call_done = nfs4_lock_done,
4902 .rpc_release = nfs4_lock_release,
4905 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4908 case -NFS4ERR_ADMIN_REVOKED:
4909 case -NFS4ERR_BAD_STATEID:
4910 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4911 if (new_lock_owner != 0 ||
4912 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
4913 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4915 case -NFS4ERR_STALE_STATEID:
4916 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4917 case -NFS4ERR_EXPIRED:
4918 nfs4_schedule_lease_recovery(server->nfs_client);
4922 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4924 struct nfs4_lockdata *data;
4925 struct rpc_task *task;
4926 struct rpc_message msg = {
4927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4928 .rpc_cred = state->owner->so_cred,
4930 struct rpc_task_setup task_setup_data = {
4931 .rpc_client = NFS_CLIENT(state->inode),
4932 .rpc_message = &msg,
4933 .callback_ops = &nfs4_lock_ops,
4934 .workqueue = nfsiod_workqueue,
4935 .flags = RPC_TASK_ASYNC,
4939 dprintk("%s: begin!\n", __func__);
4940 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4941 fl->fl_u.nfs4_fl.owner,
4942 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4946 data->arg.block = 1;
4947 if (recovery_type > NFS_LOCK_NEW) {
4948 if (recovery_type == NFS_LOCK_RECLAIM)
4949 data->arg.reclaim = NFS_LOCK_RECLAIM;
4950 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4952 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4953 msg.rpc_argp = &data->arg;
4954 msg.rpc_resp = &data->res;
4955 task_setup_data.callback_data = data;
4956 task = rpc_run_task(&task_setup_data);
4958 return PTR_ERR(task);
4959 ret = nfs4_wait_for_completion_rpc_task(task);
4961 ret = data->rpc_status;
4963 nfs4_handle_setlk_error(data->server, data->lsp,
4964 data->arg.new_lock_owner, ret);
4966 data->cancelled = 1;
4968 dprintk("%s: done, ret = %d!\n", __func__, ret);
4972 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4974 struct nfs_server *server = NFS_SERVER(state->inode);
4975 struct nfs4_exception exception = {
4976 .inode = state->inode,
4981 /* Cache the lock if possible... */
4982 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4984 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4985 if (err != -NFS4ERR_DELAY)
4987 nfs4_handle_exception(server, err, &exception);
4988 } while (exception.retry);
4992 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4994 struct nfs_server *server = NFS_SERVER(state->inode);
4995 struct nfs4_exception exception = {
4996 .inode = state->inode,
5000 err = nfs4_set_lock_state(state, request);
5004 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5006 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
5010 case -NFS4ERR_GRACE:
5011 case -NFS4ERR_DELAY:
5012 nfs4_handle_exception(server, err, &exception);
5015 } while (exception.retry);
5020 #if defined(CONFIG_NFS_V4_1)
5022 * nfs41_check_expired_locks - possibly free a lock stateid
5024 * @state: NFSv4 state for an inode
5026 * Returns NFS_OK if recovery for this stateid is now finished.
5027 * Otherwise a negative NFS4ERR value is returned.
5029 static int nfs41_check_expired_locks(struct nfs4_state *state)
5031 int status, ret = -NFS4ERR_BAD_STATEID;
5032 struct nfs4_lock_state *lsp;
5033 struct nfs_server *server = NFS_SERVER(state->inode);
5035 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
5036 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
5037 status = nfs41_test_stateid(server, &lsp->ls_stateid);
5038 if (status != NFS_OK) {
5039 /* Free the stateid unless the server
5040 * informs us the stateid is unrecognized. */
5041 if (status != -NFS4ERR_BAD_STATEID)
5042 nfs41_free_stateid(server,
5044 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5053 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5055 int status = NFS_OK;
5057 if (test_bit(LK_STATE_IN_USE, &state->flags))
5058 status = nfs41_check_expired_locks(state);
5059 if (status != NFS_OK)
5060 status = nfs4_lock_expired(state, request);
5065 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5067 struct nfs_inode *nfsi = NFS_I(state->inode);
5068 unsigned char fl_flags = request->fl_flags;
5069 int status = -ENOLCK;
5071 if ((fl_flags & FL_POSIX) &&
5072 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5074 /* Is this a delegated open? */
5075 status = nfs4_set_lock_state(state, request);
5078 request->fl_flags |= FL_ACCESS;
5079 status = do_vfs_lock(request->fl_file, request);
5082 down_read(&nfsi->rwsem);
5083 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
5084 /* Yes: cache locks! */
5085 /* ...but avoid races with delegation recall... */
5086 request->fl_flags = fl_flags & ~FL_SLEEP;
5087 status = do_vfs_lock(request->fl_file, request);
5090 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
5093 /* Note: we always want to sleep here! */
5094 request->fl_flags = fl_flags | FL_SLEEP;
5095 if (do_vfs_lock(request->fl_file, request) < 0)
5096 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5097 "manager!\n", __func__);
5099 up_read(&nfsi->rwsem);
5101 request->fl_flags = fl_flags;
5105 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5107 struct nfs4_exception exception = {
5109 .inode = state->inode,
5114 err = _nfs4_proc_setlk(state, cmd, request);
5115 if (err == -NFS4ERR_DENIED)
5117 err = nfs4_handle_exception(NFS_SERVER(state->inode),
5119 } while (exception.retry);
5124 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5126 struct nfs_open_context *ctx;
5127 struct nfs4_state *state;
5128 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5131 /* verify open state */
5132 ctx = nfs_file_open_context(filp);
5135 if (request->fl_start < 0 || request->fl_end < 0)
5138 if (IS_GETLK(cmd)) {
5140 return nfs4_proc_getlk(state, F_GETLK, request);
5144 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5147 if (request->fl_type == F_UNLCK) {
5149 return nfs4_proc_unlck(state, cmd, request);
5156 * Don't rely on the VFS having checked the file open mode,
5157 * since it won't do this for flock() locks.
5159 switch (request->fl_type) {
5161 if (!(filp->f_mode & FMODE_READ))
5165 if (!(filp->f_mode & FMODE_WRITE))
5170 status = nfs4_proc_setlk(state, cmd, request);
5171 if ((status != -EAGAIN) || IS_SETLK(cmd))
5173 timeout = nfs4_set_lock_task_retry(timeout);
5174 status = -ERESTARTSYS;
5177 } while(status < 0);
5181 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5183 struct nfs_server *server = NFS_SERVER(state->inode);
5184 struct nfs4_exception exception = { };
5187 err = nfs4_set_lock_state(state, fl);
5191 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
5194 printk(KERN_ERR "NFS: %s: unhandled error "
5195 "%d.\n", __func__, err);
5199 case -NFS4ERR_EXPIRED:
5200 nfs4_schedule_stateid_recovery(server, state);
5201 case -NFS4ERR_STALE_CLIENTID:
5202 case -NFS4ERR_STALE_STATEID:
5203 nfs4_schedule_lease_recovery(server->nfs_client);
5205 case -NFS4ERR_BADSESSION:
5206 case -NFS4ERR_BADSLOT:
5207 case -NFS4ERR_BAD_HIGH_SLOT:
5208 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5209 case -NFS4ERR_DEADSESSION:
5210 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
5214 * The show must go on: exit, but mark the
5215 * stateid as needing recovery.
5217 case -NFS4ERR_DELEG_REVOKED:
5218 case -NFS4ERR_ADMIN_REVOKED:
5219 case -NFS4ERR_BAD_STATEID:
5220 case -NFS4ERR_OPENMODE:
5221 nfs4_schedule_stateid_recovery(server, state);
5226 * User RPCSEC_GSS context has expired.
5227 * We cannot recover this stateid now, so
5228 * skip it and allow recovery thread to
5234 case -NFS4ERR_DENIED:
5235 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5238 case -NFS4ERR_DELAY:
5241 err = nfs4_handle_exception(server, err, &exception);
5242 } while (exception.retry);
5247 struct nfs_release_lockowner_data {
5248 struct nfs4_lock_state *lsp;
5249 struct nfs_server *server;
5250 struct nfs_release_lockowner_args args;
5253 static void nfs4_release_lockowner_release(void *calldata)
5255 struct nfs_release_lockowner_data *data = calldata;
5256 nfs4_free_lock_state(data->server, data->lsp);
5260 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5261 .rpc_release = nfs4_release_lockowner_release,
5264 int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
5266 struct nfs_server *server = lsp->ls_state->owner->so_server;
5267 struct nfs_release_lockowner_data *data;
5268 struct rpc_message msg = {
5269 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5272 if (server->nfs_client->cl_mvops->minor_version != 0)
5274 data = kmalloc(sizeof(*data), GFP_NOFS);
5278 data->server = server;
5279 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5280 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5281 data->args.lock_owner.s_dev = server->s_dev;
5282 msg.rpc_argp = &data->args;
5283 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5287 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5289 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5290 const void *buf, size_t buflen,
5291 int flags, int type)
5293 if (strcmp(key, "") != 0)
5296 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5299 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5300 void *buf, size_t buflen, int type)
5302 if (strcmp(key, "") != 0)
5305 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5308 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5309 size_t list_len, const char *name,
5310 size_t name_len, int type)
5312 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5314 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5317 if (list && len <= list_len)
5318 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5323 * nfs_fhget will use either the mounted_on_fileid or the fileid
5325 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5327 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5328 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5329 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
5330 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
5333 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
5334 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
5335 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5339 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5340 const struct qstr *name,
5341 struct nfs4_fs_locations *fs_locations,
5344 struct nfs_server *server = NFS_SERVER(dir);
5346 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
5348 struct nfs4_fs_locations_arg args = {
5349 .dir_fh = NFS_FH(dir),
5354 struct nfs4_fs_locations_res res = {
5355 .fs_locations = fs_locations,
5357 struct rpc_message msg = {
5358 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5364 dprintk("%s: start\n", __func__);
5366 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5367 * is not supported */
5368 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5369 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5371 bitmask[0] |= FATTR4_WORD0_FILEID;
5373 nfs_fattr_init(&fs_locations->fattr);
5374 fs_locations->server = server;
5375 fs_locations->nlocations = 0;
5376 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
5377 dprintk("%s: returned status = %d\n", __func__, status);
5381 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5382 const struct qstr *name,
5383 struct nfs4_fs_locations *fs_locations,
5386 struct nfs4_exception exception = { };
5389 err = nfs4_handle_exception(NFS_SERVER(dir),
5390 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
5392 } while (exception.retry);
5396 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5399 struct nfs4_secinfo_arg args = {
5400 .dir_fh = NFS_FH(dir),
5403 struct nfs4_secinfo_res res = {
5406 struct rpc_message msg = {
5407 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5412 dprintk("NFS call secinfo %s\n", name->name);
5413 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5414 dprintk("NFS reply secinfo: %d\n", status);
5418 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5419 struct nfs4_secinfo_flavors *flavors)
5421 struct nfs4_exception exception = { };
5424 err = nfs4_handle_exception(NFS_SERVER(dir),
5425 _nfs4_proc_secinfo(dir, name, flavors),
5427 } while (exception.retry);
5431 #ifdef CONFIG_NFS_V4_1
5433 * Check the exchange flags returned by the server for invalid flags, having
5434 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5437 static int nfs4_check_cl_exchange_flags(u32 flags)
5439 if (flags & ~EXCHGID4_FLAG_MASK_R)
5441 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5442 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5444 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5448 return -NFS4ERR_INVAL;
5452 nfs41_same_server_scope(struct nfs41_server_scope *a,
5453 struct nfs41_server_scope *b)
5455 if (a->server_scope_sz == b->server_scope_sz &&
5456 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5463 * nfs4_proc_bind_conn_to_session()
5465 * The 4.1 client currently uses the same TCP connection for the
5466 * fore and backchannel.
5468 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
5471 struct nfs41_bind_conn_to_session_res res;
5472 struct rpc_message msg = {
5474 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5480 dprintk("--> %s\n", __func__);
5482 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5483 if (unlikely(res.session == NULL)) {
5488 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5490 if (memcmp(res.session->sess_id.data,
5491 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5492 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5496 if (res.dir != NFS4_CDFS4_BOTH) {
5497 dprintk("NFS: %s: Unexpected direction from server\n",
5502 if (res.use_conn_in_rdma_mode) {
5503 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5512 dprintk("<-- %s status= %d\n", __func__, status);
5517 * nfs4_proc_exchange_id()
5519 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5521 * Since the clientid has expired, all compounds using sessions
5522 * associated with the stale clientid will be returning
5523 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5524 * be in some phase of session reset.
5526 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
5528 nfs4_verifier verifier;
5529 struct nfs41_exchange_id_args args = {
5530 .verifier = &verifier,
5532 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
5534 struct nfs41_exchange_id_res res = {
5538 struct rpc_message msg = {
5539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5545 nfs4_init_boot_verifier(clp, &verifier);
5546 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5548 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5549 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5550 args.id_len, args.id);
5552 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
5554 if (unlikely(res.server_owner == NULL)) {
5559 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
5561 if (unlikely(res.server_scope == NULL)) {
5563 goto out_server_owner;
5566 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
5567 if (unlikely(res.impl_id == NULL)) {
5569 goto out_server_scope;
5572 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5574 status = nfs4_check_cl_exchange_flags(res.flags);
5577 clp->cl_clientid = res.clientid;
5578 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5579 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5580 clp->cl_seqid = res.seqid;
5582 kfree(clp->cl_serverowner);
5583 clp->cl_serverowner = res.server_owner;
5584 res.server_owner = NULL;
5586 /* use the most recent implementation id */
5587 kfree(clp->cl_implid);
5588 clp->cl_implid = res.impl_id;
5590 if (clp->cl_serverscope != NULL &&
5591 !nfs41_same_server_scope(clp->cl_serverscope,
5592 res.server_scope)) {
5593 dprintk("%s: server_scope mismatch detected\n",
5595 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5596 kfree(clp->cl_serverscope);
5597 clp->cl_serverscope = NULL;
5600 if (clp->cl_serverscope == NULL) {
5601 clp->cl_serverscope = res.server_scope;
5608 kfree(res.server_owner);
5610 kfree(res.server_scope);
5612 if (clp->cl_implid != NULL)
5613 dprintk("NFS reply exchange_id: Server Implementation ID: "
5614 "domain: %s, name: %s, date: %llu,%u\n",
5615 clp->cl_implid->domain, clp->cl_implid->name,
5616 clp->cl_implid->date.seconds,
5617 clp->cl_implid->date.nseconds);
5618 dprintk("NFS reply exchange_id: %d\n", status);
5622 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5623 struct rpc_cred *cred)
5625 struct rpc_message msg = {
5626 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5632 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5634 dprintk("NFS: Got error %d from the server %s on "
5635 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5639 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5640 struct rpc_cred *cred)
5645 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5646 ret = _nfs4_proc_destroy_clientid(clp, cred);
5648 case -NFS4ERR_DELAY:
5649 case -NFS4ERR_CLIENTID_BUSY:
5659 int nfs4_destroy_clientid(struct nfs_client *clp)
5661 struct rpc_cred *cred;
5664 if (clp->cl_mvops->minor_version < 1)
5666 if (clp->cl_exchange_flags == 0)
5668 if (clp->cl_preserve_clid)
5670 cred = nfs4_get_exchange_id_cred(clp);
5671 ret = nfs4_proc_destroy_clientid(clp, cred);
5676 case -NFS4ERR_STALE_CLIENTID:
5677 clp->cl_exchange_flags = 0;
5683 struct nfs4_get_lease_time_data {
5684 struct nfs4_get_lease_time_args *args;
5685 struct nfs4_get_lease_time_res *res;
5686 struct nfs_client *clp;
5689 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5693 struct nfs4_get_lease_time_data *data =
5694 (struct nfs4_get_lease_time_data *)calldata;
5696 dprintk("--> %s\n", __func__);
5697 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5698 /* just setup sequence, do not trigger session recovery
5699 since we're invoked within one */
5700 ret = nfs41_setup_sequence(data->clp->cl_session,
5701 &data->args->la_seq_args,
5702 &data->res->lr_seq_res, task);
5705 rpc_call_start(task);
5706 dprintk("<-- %s\n", __func__);
5710 * Called from nfs4_state_manager thread for session setup, so don't recover
5711 * from sequence operation or clientid errors.
5713 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5715 struct nfs4_get_lease_time_data *data =
5716 (struct nfs4_get_lease_time_data *)calldata;
5718 dprintk("--> %s\n", __func__);
5719 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5721 switch (task->tk_status) {
5722 case -NFS4ERR_DELAY:
5723 case -NFS4ERR_GRACE:
5724 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5725 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5726 task->tk_status = 0;
5728 case -NFS4ERR_RETRY_UNCACHED_REP:
5729 rpc_restart_call_prepare(task);
5732 dprintk("<-- %s\n", __func__);
5735 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5736 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5737 .rpc_call_done = nfs4_get_lease_time_done,
5740 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5742 struct rpc_task *task;
5743 struct nfs4_get_lease_time_args args;
5744 struct nfs4_get_lease_time_res res = {
5745 .lr_fsinfo = fsinfo,
5747 struct nfs4_get_lease_time_data data = {
5752 struct rpc_message msg = {
5753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5757 struct rpc_task_setup task_setup = {
5758 .rpc_client = clp->cl_rpcclient,
5759 .rpc_message = &msg,
5760 .callback_ops = &nfs4_get_lease_time_ops,
5761 .callback_data = &data,
5762 .flags = RPC_TASK_TIMEOUT,
5766 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5767 dprintk("--> %s\n", __func__);
5768 task = rpc_run_task(&task_setup);
5771 status = PTR_ERR(task);
5773 status = task->tk_status;
5776 dprintk("<-- %s return %d\n", __func__, status);
5781 static int nfs4_grow_slot_table(struct nfs4_slot_table *tbl,
5782 u32 max_reqs, u32 ivalue)
5784 if (max_reqs <= tbl->max_slots)
5786 if (!IS_ERR(nfs4_find_or_create_slot(tbl, max_reqs - 1, ivalue, GFP_NOFS)))
5791 static void nfs4_reset_slot_table(struct nfs4_slot_table *tbl,
5792 u32 server_highest_slotid,
5795 struct nfs4_slot **p;
5797 nfs4_shrink_slot_table(tbl, server_highest_slotid + 1);
5800 (*p)->seq_nr = ivalue;
5803 tbl->highest_used_slotid = NFS4_NO_SLOT;
5804 tbl->target_highest_slotid = server_highest_slotid;
5805 tbl->server_highest_slotid = server_highest_slotid;
5806 tbl->max_slotid = server_highest_slotid;
5810 * (re)Initialise a slot table
5812 static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl,
5813 u32 max_reqs, u32 ivalue)
5817 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5818 max_reqs, tbl->max_slots);
5820 if (max_reqs > NFS4_MAX_SLOT_TABLE)
5821 max_reqs = NFS4_MAX_SLOT_TABLE;
5823 ret = nfs4_grow_slot_table(tbl, max_reqs, ivalue);
5827 spin_lock(&tbl->slot_tbl_lock);
5828 nfs4_reset_slot_table(tbl, max_reqs - 1, ivalue);
5829 spin_unlock(&tbl->slot_tbl_lock);
5831 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5832 tbl, tbl->slots, tbl->max_slots);
5834 dprintk("<-- %s: return %d\n", __func__, ret);
5838 /* Destroy the slot table */
5839 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5841 nfs4_shrink_slot_table(&session->fc_slot_table, 0);
5842 nfs4_shrink_slot_table(&session->bc_slot_table, 0);
5846 * Initialize or reset the forechannel and backchannel tables
5848 static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
5850 struct nfs4_slot_table *tbl;
5853 dprintk("--> %s\n", __func__);
5855 tbl = &ses->fc_slot_table;
5857 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5858 if (status) /* -ENOMEM */
5861 tbl = &ses->bc_slot_table;
5863 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5864 if (status && tbl->slots == NULL)
5865 /* Fore and back channel share a connection so get
5866 * both slot tables or neither */
5867 nfs4_destroy_slot_tables(ses);
5871 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5873 struct nfs4_session *session;
5874 struct nfs4_slot_table *tbl;
5876 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5880 tbl = &session->fc_slot_table;
5881 tbl->highest_used_slotid = NFS4_NO_SLOT;
5882 spin_lock_init(&tbl->slot_tbl_lock);
5883 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5884 init_completion(&tbl->complete);
5886 tbl = &session->bc_slot_table;
5887 tbl->highest_used_slotid = NFS4_NO_SLOT;
5888 spin_lock_init(&tbl->slot_tbl_lock);
5889 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5890 init_completion(&tbl->complete);
5892 session->session_state = 1<<NFS4_SESSION_INITING;
5898 void nfs4_destroy_session(struct nfs4_session *session)
5900 struct rpc_xprt *xprt;
5901 struct rpc_cred *cred;
5903 cred = nfs4_get_exchange_id_cred(session->clp);
5904 nfs4_proc_destroy_session(session, cred);
5909 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5911 dprintk("%s Destroy backchannel for xprt %p\n",
5913 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
5914 nfs4_destroy_slot_tables(session);
5919 * Initialize the values to be used by the client in CREATE_SESSION
5920 * If nfs4_init_session set the fore channel request and response sizes,
5923 * Set the back channel max_resp_sz_cached to zero to force the client to
5924 * always set csa_cachethis to FALSE because the current implementation
5925 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5927 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5929 struct nfs4_session *session = args->client->cl_session;
5930 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5931 mxresp_sz = session->fc_target_max_resp_sz;
5934 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5936 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5937 /* Fore channel attributes */
5938 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5939 args->fc_attrs.max_resp_sz = mxresp_sz;
5940 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5941 args->fc_attrs.max_reqs = max_session_slots;
5943 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5944 "max_ops=%u max_reqs=%u\n",
5946 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5947 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5949 /* Back channel attributes */
5950 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5951 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5952 args->bc_attrs.max_resp_sz_cached = 0;
5953 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5954 args->bc_attrs.max_reqs = 1;
5956 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5957 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5959 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5960 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5961 args->bc_attrs.max_reqs);
5964 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5966 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5967 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5969 if (rcvd->max_resp_sz > sent->max_resp_sz)
5972 * Our requested max_ops is the minimum we need; we're not
5973 * prepared to break up compounds into smaller pieces than that.
5974 * So, no point even trying to continue if the server won't
5977 if (rcvd->max_ops < sent->max_ops)
5979 if (rcvd->max_reqs == 0)
5981 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5982 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5986 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5988 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5989 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5991 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5993 if (rcvd->max_resp_sz < sent->max_resp_sz)
5995 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5997 /* These would render the backchannel useless: */
5998 if (rcvd->max_ops != sent->max_ops)
6000 if (rcvd->max_reqs != sent->max_reqs)
6005 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6006 struct nfs4_session *session)
6010 ret = nfs4_verify_fore_channel_attrs(args, session);
6013 return nfs4_verify_back_channel_attrs(args, session);
6016 static int _nfs4_proc_create_session(struct nfs_client *clp,
6017 struct rpc_cred *cred)
6019 struct nfs4_session *session = clp->cl_session;
6020 struct nfs41_create_session_args args = {
6022 .cb_program = NFS4_CALLBACK,
6024 struct nfs41_create_session_res res = {
6027 struct rpc_message msg = {
6028 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6035 nfs4_init_channel_attrs(&args);
6036 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
6038 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6041 /* Verify the session's negotiated channel_attrs values */
6042 status = nfs4_verify_channel_attrs(&args, session);
6043 /* Increment the clientid slot sequence id */
6051 * Issues a CREATE_SESSION operation to the server.
6052 * It is the responsibility of the caller to verify the session is
6053 * expired before calling this routine.
6055 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
6059 struct nfs4_session *session = clp->cl_session;
6061 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6063 status = _nfs4_proc_create_session(clp, cred);
6067 /* Init or reset the session slot tables */
6068 status = nfs4_setup_session_slot_tables(session);
6069 dprintk("slot table setup returned %d\n", status);
6073 ptr = (unsigned *)&session->sess_id.data[0];
6074 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6075 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
6077 dprintk("<-- %s\n", __func__);
6082 * Issue the over-the-wire RPC DESTROY_SESSION.
6083 * The caller must serialize access to this routine.
6085 int nfs4_proc_destroy_session(struct nfs4_session *session,
6086 struct rpc_cred *cred)
6088 struct rpc_message msg = {
6089 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6090 .rpc_argp = session,
6095 dprintk("--> nfs4_proc_destroy_session\n");
6097 /* session is still being setup */
6098 if (session->clp->cl_cons_state != NFS_CS_READY)
6101 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6104 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
6105 "Session has been destroyed regardless...\n", status);
6107 dprintk("<-- nfs4_proc_destroy_session\n");
6112 * With sessions, the client is not marked ready until after a
6113 * successful EXCHANGE_ID and CREATE_SESSION.
6115 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
6116 * other versions of NFS can be tried.
6118 static int nfs41_check_session_ready(struct nfs_client *clp)
6122 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
6123 ret = nfs4_client_recover_expired_lease(clp);
6127 if (clp->cl_cons_state < NFS_CS_READY)
6128 return -EPROTONOSUPPORT;
6133 int nfs4_init_session(struct nfs_server *server)
6135 struct nfs_client *clp = server->nfs_client;
6136 struct nfs4_session *session;
6137 unsigned int target_max_rqst_sz = NFS_MAX_FILE_IO_SIZE;
6138 unsigned int target_max_resp_sz = NFS_MAX_FILE_IO_SIZE;
6140 if (!nfs4_has_session(clp))
6143 if (server->rsize != 0)
6144 target_max_resp_sz = server->rsize;
6145 target_max_resp_sz += nfs41_maxread_overhead;
6147 if (server->wsize != 0)
6148 target_max_rqst_sz = server->wsize;
6149 target_max_rqst_sz += nfs41_maxwrite_overhead;
6151 session = clp->cl_session;
6152 spin_lock(&clp->cl_lock);
6153 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6154 /* Initialise targets and channel attributes */
6155 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6156 session->fc_attrs.max_rqst_sz = target_max_rqst_sz;
6157 session->fc_target_max_resp_sz = target_max_resp_sz;
6158 session->fc_attrs.max_resp_sz = target_max_resp_sz;
6160 /* Just adjust the targets */
6161 if (target_max_rqst_sz > session->fc_target_max_rqst_sz) {
6162 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6163 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6165 if (target_max_resp_sz > session->fc_target_max_resp_sz) {
6166 session->fc_target_max_resp_sz = target_max_resp_sz;
6167 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6170 spin_unlock(&clp->cl_lock);
6172 if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
6173 nfs4_schedule_lease_recovery(clp);
6175 return nfs41_check_session_ready(clp);
6178 int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
6180 struct nfs4_session *session = clp->cl_session;
6183 spin_lock(&clp->cl_lock);
6184 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6186 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
6187 * DS lease to be equal to the MDS lease.
6189 clp->cl_lease_time = lease_time;
6190 clp->cl_last_renewal = jiffies;
6192 spin_unlock(&clp->cl_lock);
6194 ret = nfs41_check_session_ready(clp);
6197 /* Test for the DS role */
6198 if (!is_ds_client(clp))
6202 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
6206 * Renew the cl_session lease.
6208 struct nfs4_sequence_data {
6209 struct nfs_client *clp;
6210 struct nfs4_sequence_args args;
6211 struct nfs4_sequence_res res;
6214 static void nfs41_sequence_release(void *data)
6216 struct nfs4_sequence_data *calldata = data;
6217 struct nfs_client *clp = calldata->clp;
6219 if (atomic_read(&clp->cl_count) > 1)
6220 nfs4_schedule_state_renewal(clp);
6221 nfs_put_client(clp);
6225 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6227 switch(task->tk_status) {
6228 case -NFS4ERR_DELAY:
6229 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6232 nfs4_schedule_lease_recovery(clp);
6237 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
6239 struct nfs4_sequence_data *calldata = data;
6240 struct nfs_client *clp = calldata->clp;
6242 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6245 if (task->tk_status < 0) {
6246 dprintk("%s ERROR %d\n", __func__, task->tk_status);
6247 if (atomic_read(&clp->cl_count) == 1)
6250 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6251 rpc_restart_call_prepare(task);
6255 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
6257 dprintk("<-- %s\n", __func__);
6260 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6262 struct nfs4_sequence_data *calldata = data;
6263 struct nfs_client *clp = calldata->clp;
6264 struct nfs4_sequence_args *args;
6265 struct nfs4_sequence_res *res;
6267 args = task->tk_msg.rpc_argp;
6268 res = task->tk_msg.rpc_resp;
6270 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
6272 rpc_call_start(task);
6275 static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
6277 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6278 nfs41_sequence_prepare(task, data);
6281 static const struct rpc_call_ops nfs41_sequence_ops = {
6282 .rpc_call_done = nfs41_sequence_call_done,
6283 .rpc_call_prepare = nfs41_sequence_prepare,
6284 .rpc_release = nfs41_sequence_release,
6287 static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
6288 .rpc_call_done = nfs41_sequence_call_done,
6289 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
6290 .rpc_release = nfs41_sequence_release,
6293 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
6294 const struct rpc_call_ops *seq_ops)
6296 struct nfs4_sequence_data *calldata;
6297 struct rpc_message msg = {
6298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6301 struct rpc_task_setup task_setup_data = {
6302 .rpc_client = clp->cl_rpcclient,
6303 .rpc_message = &msg,
6304 .callback_ops = seq_ops,
6305 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6308 if (!atomic_inc_not_zero(&clp->cl_count))
6309 return ERR_PTR(-EIO);
6310 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6311 if (calldata == NULL) {
6312 nfs_put_client(clp);
6313 return ERR_PTR(-ENOMEM);
6315 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
6316 msg.rpc_argp = &calldata->args;
6317 msg.rpc_resp = &calldata->res;
6318 calldata->clp = clp;
6319 task_setup_data.callback_data = calldata;
6321 return rpc_run_task(&task_setup_data);
6324 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
6326 struct rpc_task *task;
6329 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6331 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
6333 ret = PTR_ERR(task);
6335 rpc_put_task_async(task);
6336 dprintk("<-- %s status=%d\n", __func__, ret);
6340 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6342 struct rpc_task *task;
6345 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
6347 ret = PTR_ERR(task);
6350 ret = rpc_wait_for_completion_task(task);
6352 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6354 if (task->tk_status == 0)
6355 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
6356 ret = task->tk_status;
6360 dprintk("<-- %s status=%d\n", __func__, ret);
6364 struct nfs4_reclaim_complete_data {
6365 struct nfs_client *clp;
6366 struct nfs41_reclaim_complete_args arg;
6367 struct nfs41_reclaim_complete_res res;
6370 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6372 struct nfs4_reclaim_complete_data *calldata = data;
6374 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6375 if (nfs41_setup_sequence(calldata->clp->cl_session,
6376 &calldata->arg.seq_args,
6377 &calldata->res.seq_res, task))
6380 rpc_call_start(task);
6383 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6385 switch(task->tk_status) {
6387 case -NFS4ERR_COMPLETE_ALREADY:
6388 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6390 case -NFS4ERR_DELAY:
6391 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6393 case -NFS4ERR_RETRY_UNCACHED_REP:
6396 nfs4_schedule_lease_recovery(clp);
6401 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6403 struct nfs4_reclaim_complete_data *calldata = data;
6404 struct nfs_client *clp = calldata->clp;
6405 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6407 dprintk("--> %s\n", __func__);
6408 if (!nfs41_sequence_done(task, res))
6411 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6412 rpc_restart_call_prepare(task);
6415 dprintk("<-- %s\n", __func__);
6418 static void nfs4_free_reclaim_complete_data(void *data)
6420 struct nfs4_reclaim_complete_data *calldata = data;
6425 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6426 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6427 .rpc_call_done = nfs4_reclaim_complete_done,
6428 .rpc_release = nfs4_free_reclaim_complete_data,
6432 * Issue a global reclaim complete.
6434 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6436 struct nfs4_reclaim_complete_data *calldata;
6437 struct rpc_task *task;
6438 struct rpc_message msg = {
6439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6441 struct rpc_task_setup task_setup_data = {
6442 .rpc_client = clp->cl_rpcclient,
6443 .rpc_message = &msg,
6444 .callback_ops = &nfs4_reclaim_complete_call_ops,
6445 .flags = RPC_TASK_ASYNC,
6447 int status = -ENOMEM;
6449 dprintk("--> %s\n", __func__);
6450 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6451 if (calldata == NULL)
6453 calldata->clp = clp;
6454 calldata->arg.one_fs = 0;
6456 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
6457 msg.rpc_argp = &calldata->arg;
6458 msg.rpc_resp = &calldata->res;
6459 task_setup_data.callback_data = calldata;
6460 task = rpc_run_task(&task_setup_data);
6462 status = PTR_ERR(task);
6465 status = nfs4_wait_for_completion_rpc_task(task);
6467 status = task->tk_status;
6471 dprintk("<-- %s status=%d\n", __func__, status);
6476 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6478 struct nfs4_layoutget *lgp = calldata;
6479 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6481 dprintk("--> %s\n", __func__);
6482 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6483 * right now covering the LAYOUTGET we are about to send.
6484 * However, that is not so catastrophic, and there seems
6485 * to be no way to prevent it completely.
6487 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
6488 &lgp->res.seq_res, task))
6490 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6491 NFS_I(lgp->args.inode)->layout,
6492 lgp->args.ctx->state)) {
6493 rpc_exit(task, NFS4_OK);
6496 rpc_call_start(task);
6499 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6501 struct nfs4_layoutget *lgp = calldata;
6502 struct inode *inode = lgp->args.inode;
6503 struct nfs_server *server = NFS_SERVER(inode);
6504 struct pnfs_layout_hdr *lo;
6505 struct nfs4_state *state = NULL;
6507 dprintk("--> %s\n", __func__);
6509 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6512 switch (task->tk_status) {
6515 case -NFS4ERR_LAYOUTTRYLATER:
6516 case -NFS4ERR_RECALLCONFLICT:
6517 task->tk_status = -NFS4ERR_DELAY;
6519 case -NFS4ERR_EXPIRED:
6520 case -NFS4ERR_BAD_STATEID:
6521 spin_lock(&inode->i_lock);
6522 lo = NFS_I(inode)->layout;
6523 if (!lo || list_empty(&lo->plh_segs)) {
6524 spin_unlock(&inode->i_lock);
6525 /* If the open stateid was bad, then recover it. */
6526 state = lgp->args.ctx->state;
6530 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6531 spin_unlock(&inode->i_lock);
6532 /* Mark the bad layout state as invalid, then
6533 * retry using the open stateid. */
6534 pnfs_free_lseg_list(&head);
6537 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6538 rpc_restart_call_prepare(task);
6540 dprintk("<-- %s\n", __func__);
6543 static size_t max_response_pages(struct nfs_server *server)
6545 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6546 return nfs_page_array_len(0, max_resp_sz);
6549 static void nfs4_free_pages(struct page **pages, size_t size)
6556 for (i = 0; i < size; i++) {
6559 __free_page(pages[i]);
6564 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6566 struct page **pages;
6569 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6571 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6575 for (i = 0; i < size; i++) {
6576 pages[i] = alloc_page(gfp_flags);
6578 dprintk("%s: failed to allocate page\n", __func__);
6579 nfs4_free_pages(pages, size);
6587 static void nfs4_layoutget_release(void *calldata)
6589 struct nfs4_layoutget *lgp = calldata;
6590 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6591 size_t max_pages = max_response_pages(server);
6593 dprintk("--> %s\n", __func__);
6594 nfs4_free_pages(lgp->args.layout.pages, max_pages);
6595 put_nfs_open_context(lgp->args.ctx);
6597 dprintk("<-- %s\n", __func__);
6600 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6601 .rpc_call_prepare = nfs4_layoutget_prepare,
6602 .rpc_call_done = nfs4_layoutget_done,
6603 .rpc_release = nfs4_layoutget_release,
6606 struct pnfs_layout_segment *
6607 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
6609 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6610 size_t max_pages = max_response_pages(server);
6611 struct rpc_task *task;
6612 struct rpc_message msg = {
6613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6614 .rpc_argp = &lgp->args,
6615 .rpc_resp = &lgp->res,
6617 struct rpc_task_setup task_setup_data = {
6618 .rpc_client = server->client,
6619 .rpc_message = &msg,
6620 .callback_ops = &nfs4_layoutget_call_ops,
6621 .callback_data = lgp,
6622 .flags = RPC_TASK_ASYNC,
6624 struct pnfs_layout_segment *lseg = NULL;
6627 dprintk("--> %s\n", __func__);
6629 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6630 if (!lgp->args.layout.pages) {
6631 nfs4_layoutget_release(lgp);
6632 return ERR_PTR(-ENOMEM);
6634 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6636 lgp->res.layoutp = &lgp->args.layout;
6637 lgp->res.seq_res.sr_slot = NULL;
6638 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
6639 task = rpc_run_task(&task_setup_data);
6641 return ERR_CAST(task);
6642 status = nfs4_wait_for_completion_rpc_task(task);
6644 status = task->tk_status;
6646 lseg = pnfs_layout_process(lgp);
6648 dprintk("<-- %s status=%d\n", __func__, status);
6650 return ERR_PTR(status);
6655 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6657 struct nfs4_layoutreturn *lrp = calldata;
6659 dprintk("--> %s\n", __func__);
6660 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
6661 &lrp->res.seq_res, task))
6663 rpc_call_start(task);
6666 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6668 struct nfs4_layoutreturn *lrp = calldata;
6669 struct nfs_server *server;
6671 dprintk("--> %s\n", __func__);
6673 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6676 server = NFS_SERVER(lrp->args.inode);
6677 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6678 rpc_restart_call_prepare(task);
6681 dprintk("<-- %s\n", __func__);
6684 static void nfs4_layoutreturn_release(void *calldata)
6686 struct nfs4_layoutreturn *lrp = calldata;
6687 struct pnfs_layout_hdr *lo = lrp->args.layout;
6689 dprintk("--> %s\n", __func__);
6690 spin_lock(&lo->plh_inode->i_lock);
6691 if (lrp->res.lrs_present)
6692 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6693 lo->plh_block_lgets--;
6694 spin_unlock(&lo->plh_inode->i_lock);
6695 pnfs_put_layout_hdr(lrp->args.layout);
6697 dprintk("<-- %s\n", __func__);
6700 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6701 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6702 .rpc_call_done = nfs4_layoutreturn_done,
6703 .rpc_release = nfs4_layoutreturn_release,
6706 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6708 struct rpc_task *task;
6709 struct rpc_message msg = {
6710 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6711 .rpc_argp = &lrp->args,
6712 .rpc_resp = &lrp->res,
6714 struct rpc_task_setup task_setup_data = {
6715 .rpc_client = lrp->clp->cl_rpcclient,
6716 .rpc_message = &msg,
6717 .callback_ops = &nfs4_layoutreturn_call_ops,
6718 .callback_data = lrp,
6722 dprintk("--> %s\n", __func__);
6723 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
6724 task = rpc_run_task(&task_setup_data);
6726 return PTR_ERR(task);
6727 status = task->tk_status;
6728 dprintk("<-- %s status=%d\n", __func__, status);
6734 * Retrieve the list of Data Server devices from the MDS.
6736 static int _nfs4_getdevicelist(struct nfs_server *server,
6737 const struct nfs_fh *fh,
6738 struct pnfs_devicelist *devlist)
6740 struct nfs4_getdevicelist_args args = {
6742 .layoutclass = server->pnfs_curr_ld->id,
6744 struct nfs4_getdevicelist_res res = {
6747 struct rpc_message msg = {
6748 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6754 dprintk("--> %s\n", __func__);
6755 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6757 dprintk("<-- %s status=%d\n", __func__, status);
6761 int nfs4_proc_getdevicelist(struct nfs_server *server,
6762 const struct nfs_fh *fh,
6763 struct pnfs_devicelist *devlist)
6765 struct nfs4_exception exception = { };
6769 err = nfs4_handle_exception(server,
6770 _nfs4_getdevicelist(server, fh, devlist),
6772 } while (exception.retry);
6774 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6775 err, devlist->num_devs);
6779 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6782 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6784 struct nfs4_getdeviceinfo_args args = {
6787 struct nfs4_getdeviceinfo_res res = {
6790 struct rpc_message msg = {
6791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6797 dprintk("--> %s\n", __func__);
6798 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6799 dprintk("<-- %s status=%d\n", __func__, status);
6804 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6806 struct nfs4_exception exception = { };
6810 err = nfs4_handle_exception(server,
6811 _nfs4_proc_getdeviceinfo(server, pdev),
6813 } while (exception.retry);
6816 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6818 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6820 struct nfs4_layoutcommit_data *data = calldata;
6821 struct nfs_server *server = NFS_SERVER(data->args.inode);
6823 if (nfs4_setup_sequence(server, &data->args.seq_args,
6824 &data->res.seq_res, task))
6826 rpc_call_start(task);
6830 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6832 struct nfs4_layoutcommit_data *data = calldata;
6833 struct nfs_server *server = NFS_SERVER(data->args.inode);
6835 if (!nfs4_sequence_done(task, &data->res.seq_res))
6838 switch (task->tk_status) { /* Just ignore these failures */
6839 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6840 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6841 case -NFS4ERR_BADLAYOUT: /* no layout */
6842 case -NFS4ERR_GRACE: /* loca_recalim always false */
6843 task->tk_status = 0;
6846 nfs_post_op_update_inode_force_wcc(data->args.inode,
6850 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6851 rpc_restart_call_prepare(task);
6857 static void nfs4_layoutcommit_release(void *calldata)
6859 struct nfs4_layoutcommit_data *data = calldata;
6860 struct pnfs_layout_segment *lseg, *tmp;
6861 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
6863 pnfs_cleanup_layoutcommit(data);
6864 /* Matched by references in pnfs_set_layoutcommit */
6865 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6866 list_del_init(&lseg->pls_lc_list);
6867 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6869 pnfs_put_lseg(lseg);
6872 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6873 smp_mb__after_clear_bit();
6874 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6876 put_rpccred(data->cred);
6880 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6881 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6882 .rpc_call_done = nfs4_layoutcommit_done,
6883 .rpc_release = nfs4_layoutcommit_release,
6887 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6889 struct rpc_message msg = {
6890 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6891 .rpc_argp = &data->args,
6892 .rpc_resp = &data->res,
6893 .rpc_cred = data->cred,
6895 struct rpc_task_setup task_setup_data = {
6896 .task = &data->task,
6897 .rpc_client = NFS_CLIENT(data->args.inode),
6898 .rpc_message = &msg,
6899 .callback_ops = &nfs4_layoutcommit_ops,
6900 .callback_data = data,
6901 .flags = RPC_TASK_ASYNC,
6903 struct rpc_task *task;
6906 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6907 "lbw: %llu inode %lu\n",
6908 data->task.tk_pid, sync,
6909 data->args.lastbytewritten,
6910 data->args.inode->i_ino);
6912 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6913 task = rpc_run_task(&task_setup_data);
6915 return PTR_ERR(task);
6918 status = nfs4_wait_for_completion_rpc_task(task);
6921 status = task->tk_status;
6923 dprintk("%s: status %d\n", __func__, status);
6929 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6930 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6932 struct nfs41_secinfo_no_name_args args = {
6933 .style = SECINFO_STYLE_CURRENT_FH,
6935 struct nfs4_secinfo_res res = {
6938 struct rpc_message msg = {
6939 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6943 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6947 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6948 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6950 struct nfs4_exception exception = { };
6953 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6956 case -NFS4ERR_WRONGSEC:
6957 case -NFS4ERR_NOTSUPP:
6960 err = nfs4_handle_exception(server, err, &exception);
6962 } while (exception.retry);
6968 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6969 struct nfs_fsinfo *info)
6973 rpc_authflavor_t flavor;
6974 struct nfs4_secinfo_flavors *flavors;
6976 page = alloc_page(GFP_KERNEL);
6982 flavors = page_address(page);
6983 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6986 * Fall back on "guess and check" method if
6987 * the server doesn't support SECINFO_NO_NAME
6989 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6990 err = nfs4_find_root_sec(server, fhandle, info);
6996 flavor = nfs_find_best_sec(flavors);
6998 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
7008 static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
7011 struct nfs41_test_stateid_args args = {
7014 struct nfs41_test_stateid_res res;
7015 struct rpc_message msg = {
7016 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7021 dprintk("NFS call test_stateid %p\n", stateid);
7022 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
7023 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
7024 if (status != NFS_OK) {
7025 dprintk("NFS reply test_stateid: failed, %d\n", status);
7028 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
7033 * nfs41_test_stateid - perform a TEST_STATEID operation
7035 * @server: server / transport on which to perform the operation
7036 * @stateid: state ID to test
7038 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7039 * Otherwise a negative NFS4ERR value is returned if the operation
7040 * failed or the state ID is not currently valid.
7042 static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
7044 struct nfs4_exception exception = { };
7047 err = _nfs41_test_stateid(server, stateid);
7048 if (err != -NFS4ERR_DELAY)
7050 nfs4_handle_exception(server, err, &exception);
7051 } while (exception.retry);
7055 static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
7057 struct nfs41_free_stateid_args args = {
7060 struct nfs41_free_stateid_res res;
7061 struct rpc_message msg = {
7062 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
7068 dprintk("NFS call free_stateid %p\n", stateid);
7069 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
7070 status = nfs4_call_sync_sequence(server->client, server, &msg,
7071 &args.seq_args, &res.seq_res, 1);
7072 dprintk("NFS reply free_stateid: %d\n", status);
7077 * nfs41_free_stateid - perform a FREE_STATEID operation
7079 * @server: server / transport on which to perform the operation
7080 * @stateid: state ID to release
7082 * Returns NFS_OK if the server freed "stateid". Otherwise a
7083 * negative NFS4ERR value is returned.
7085 static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
7087 struct nfs4_exception exception = { };
7090 err = _nfs4_free_stateid(server, stateid);
7091 if (err != -NFS4ERR_DELAY)
7093 nfs4_handle_exception(server, err, &exception);
7094 } while (exception.retry);
7098 static bool nfs41_match_stateid(const nfs4_stateid *s1,
7099 const nfs4_stateid *s2)
7101 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
7104 if (s1->seqid == s2->seqid)
7106 if (s1->seqid == 0 || s2->seqid == 0)
7112 #endif /* CONFIG_NFS_V4_1 */
7114 static bool nfs4_match_stateid(const nfs4_stateid *s1,
7115 const nfs4_stateid *s2)
7117 return nfs4_stateid_match(s1, s2);
7121 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
7122 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7123 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7124 .recover_open = nfs4_open_reclaim,
7125 .recover_lock = nfs4_lock_reclaim,
7126 .establish_clid = nfs4_init_clientid,
7127 .get_clid_cred = nfs4_get_setclientid_cred,
7128 .detect_trunking = nfs40_discover_server_trunking,
7131 #if defined(CONFIG_NFS_V4_1)
7132 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
7133 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7134 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7135 .recover_open = nfs4_open_reclaim,
7136 .recover_lock = nfs4_lock_reclaim,
7137 .establish_clid = nfs41_init_clientid,
7138 .get_clid_cred = nfs4_get_exchange_id_cred,
7139 .reclaim_complete = nfs41_proc_reclaim_complete,
7140 .detect_trunking = nfs41_discover_server_trunking,
7142 #endif /* CONFIG_NFS_V4_1 */
7144 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
7145 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7146 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
7147 .recover_open = nfs4_open_expired,
7148 .recover_lock = nfs4_lock_expired,
7149 .establish_clid = nfs4_init_clientid,
7150 .get_clid_cred = nfs4_get_setclientid_cred,
7153 #if defined(CONFIG_NFS_V4_1)
7154 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
7155 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7156 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
7157 .recover_open = nfs41_open_expired,
7158 .recover_lock = nfs41_lock_expired,
7159 .establish_clid = nfs41_init_clientid,
7160 .get_clid_cred = nfs4_get_exchange_id_cred,
7162 #endif /* CONFIG_NFS_V4_1 */
7164 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
7165 .sched_state_renewal = nfs4_proc_async_renew,
7166 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
7167 .renew_lease = nfs4_proc_renew,
7170 #if defined(CONFIG_NFS_V4_1)
7171 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
7172 .sched_state_renewal = nfs41_proc_async_sequence,
7173 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
7174 .renew_lease = nfs4_proc_sequence,
7178 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7180 .call_sync = _nfs4_call_sync,
7181 .match_stateid = nfs4_match_stateid,
7182 .find_root_sec = nfs4_find_root_sec,
7183 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7184 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7185 .state_renewal_ops = &nfs40_state_renewal_ops,
7188 #if defined(CONFIG_NFS_V4_1)
7189 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7191 .call_sync = _nfs4_call_sync_session,
7192 .match_stateid = nfs41_match_stateid,
7193 .find_root_sec = nfs41_find_root_sec,
7194 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7195 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7196 .state_renewal_ops = &nfs41_state_renewal_ops,
7200 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7201 [0] = &nfs_v4_0_minor_ops,
7202 #if defined(CONFIG_NFS_V4_1)
7203 [1] = &nfs_v4_1_minor_ops,
7207 const struct inode_operations nfs4_dir_inode_operations = {
7208 .create = nfs_create,
7209 .lookup = nfs_lookup,
7210 .atomic_open = nfs_atomic_open,
7212 .unlink = nfs_unlink,
7213 .symlink = nfs_symlink,
7217 .rename = nfs_rename,
7218 .permission = nfs_permission,
7219 .getattr = nfs_getattr,
7220 .setattr = nfs_setattr,
7221 .getxattr = generic_getxattr,
7222 .setxattr = generic_setxattr,
7223 .listxattr = generic_listxattr,
7224 .removexattr = generic_removexattr,
7227 static const struct inode_operations nfs4_file_inode_operations = {
7228 .permission = nfs_permission,
7229 .getattr = nfs_getattr,
7230 .setattr = nfs_setattr,
7231 .getxattr = generic_getxattr,
7232 .setxattr = generic_setxattr,
7233 .listxattr = generic_listxattr,
7234 .removexattr = generic_removexattr,
7237 const struct nfs_rpc_ops nfs_v4_clientops = {
7238 .version = 4, /* protocol version */
7239 .dentry_ops = &nfs4_dentry_operations,
7240 .dir_inode_ops = &nfs4_dir_inode_operations,
7241 .file_inode_ops = &nfs4_file_inode_operations,
7242 .file_ops = &nfs4_file_operations,
7243 .getroot = nfs4_proc_get_root,
7244 .submount = nfs4_submount,
7245 .try_mount = nfs4_try_mount,
7246 .getattr = nfs4_proc_getattr,
7247 .setattr = nfs4_proc_setattr,
7248 .lookup = nfs4_proc_lookup,
7249 .access = nfs4_proc_access,
7250 .readlink = nfs4_proc_readlink,
7251 .create = nfs4_proc_create,
7252 .remove = nfs4_proc_remove,
7253 .unlink_setup = nfs4_proc_unlink_setup,
7254 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
7255 .unlink_done = nfs4_proc_unlink_done,
7256 .rename = nfs4_proc_rename,
7257 .rename_setup = nfs4_proc_rename_setup,
7258 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
7259 .rename_done = nfs4_proc_rename_done,
7260 .link = nfs4_proc_link,
7261 .symlink = nfs4_proc_symlink,
7262 .mkdir = nfs4_proc_mkdir,
7263 .rmdir = nfs4_proc_remove,
7264 .readdir = nfs4_proc_readdir,
7265 .mknod = nfs4_proc_mknod,
7266 .statfs = nfs4_proc_statfs,
7267 .fsinfo = nfs4_proc_fsinfo,
7268 .pathconf = nfs4_proc_pathconf,
7269 .set_capabilities = nfs4_server_capabilities,
7270 .decode_dirent = nfs4_decode_dirent,
7271 .read_setup = nfs4_proc_read_setup,
7272 .read_pageio_init = pnfs_pageio_init_read,
7273 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
7274 .read_done = nfs4_read_done,
7275 .write_setup = nfs4_proc_write_setup,
7276 .write_pageio_init = pnfs_pageio_init_write,
7277 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
7278 .write_done = nfs4_write_done,
7279 .commit_setup = nfs4_proc_commit_setup,
7280 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
7281 .commit_done = nfs4_commit_done,
7282 .lock = nfs4_proc_lock,
7283 .clear_acl_cache = nfs4_zap_acl_attr,
7284 .close_context = nfs4_close_context,
7285 .open_context = nfs4_atomic_open,
7286 .have_delegation = nfs4_have_delegation,
7287 .return_delegation = nfs4_inode_return_delegation,
7288 .alloc_client = nfs4_alloc_client,
7289 .init_client = nfs4_init_client,
7290 .free_client = nfs4_free_client,
7291 .create_server = nfs4_create_server,
7292 .clone_server = nfs_clone_server,
7295 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7296 .prefix = XATTR_NAME_NFSV4_ACL,
7297 .list = nfs4_xattr_list_nfs4_acl,
7298 .get = nfs4_xattr_get_nfs4_acl,
7299 .set = nfs4_xattr_set_nfs4_acl,
7302 const struct xattr_handler *nfs4_xattr_handlers[] = {
7303 &nfs4_xattr_nfs4_acl_handler,