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)
74 static int _nfs4_proc_open(struct nfs4_opendata *data);
75 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
76 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
77 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
78 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
79 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
80 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
81 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
82 struct nfs_fattr *fattr, struct iattr *sattr,
83 struct nfs4_state *state);
84 #ifdef CONFIG_NFS_V4_1
85 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
86 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
88 /* Prevent leaks of NFSv4 errors into userland */
89 static int nfs4_map_errors(int err)
94 case -NFS4ERR_RESOURCE:
96 case -NFS4ERR_WRONGSEC:
98 case -NFS4ERR_BADOWNER:
99 case -NFS4ERR_BADNAME:
101 case -NFS4ERR_SHARE_DENIED:
103 case -NFS4ERR_MINOR_VERS_MISMATCH:
104 return -EPROTONOSUPPORT;
105 case -NFS4ERR_ACCESS:
108 dprintk("%s could not handle NFSv4 error %d\n",
116 * This is our standard bitmap for GETATTR requests.
118 const u32 nfs4_fattr_bitmap[3] = {
120 | FATTR4_WORD0_CHANGE
123 | FATTR4_WORD0_FILEID,
125 | FATTR4_WORD1_NUMLINKS
127 | FATTR4_WORD1_OWNER_GROUP
128 | FATTR4_WORD1_RAWDEV
129 | FATTR4_WORD1_SPACE_USED
130 | FATTR4_WORD1_TIME_ACCESS
131 | FATTR4_WORD1_TIME_METADATA
132 | FATTR4_WORD1_TIME_MODIFY
135 static const u32 nfs4_pnfs_open_bitmap[3] = {
137 | FATTR4_WORD0_CHANGE
140 | FATTR4_WORD0_FILEID,
142 | FATTR4_WORD1_NUMLINKS
144 | FATTR4_WORD1_OWNER_GROUP
145 | FATTR4_WORD1_RAWDEV
146 | FATTR4_WORD1_SPACE_USED
147 | FATTR4_WORD1_TIME_ACCESS
148 | FATTR4_WORD1_TIME_METADATA
149 | FATTR4_WORD1_TIME_MODIFY,
150 FATTR4_WORD2_MDSTHRESHOLD
153 static const u32 nfs4_open_noattr_bitmap[3] = {
155 | FATTR4_WORD0_CHANGE
156 | FATTR4_WORD0_FILEID,
159 const u32 nfs4_statfs_bitmap[2] = {
160 FATTR4_WORD0_FILES_AVAIL
161 | FATTR4_WORD0_FILES_FREE
162 | FATTR4_WORD0_FILES_TOTAL,
163 FATTR4_WORD1_SPACE_AVAIL
164 | FATTR4_WORD1_SPACE_FREE
165 | FATTR4_WORD1_SPACE_TOTAL
168 const u32 nfs4_pathconf_bitmap[2] = {
170 | FATTR4_WORD0_MAXNAME,
174 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
175 | FATTR4_WORD0_MAXREAD
176 | FATTR4_WORD0_MAXWRITE
177 | FATTR4_WORD0_LEASE_TIME,
178 FATTR4_WORD1_TIME_DELTA
179 | FATTR4_WORD1_FS_LAYOUT_TYPES,
180 FATTR4_WORD2_LAYOUT_BLKSIZE
183 const u32 nfs4_fs_locations_bitmap[2] = {
185 | FATTR4_WORD0_CHANGE
188 | FATTR4_WORD0_FILEID
189 | FATTR4_WORD0_FS_LOCATIONS,
191 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER_GROUP
194 | FATTR4_WORD1_RAWDEV
195 | FATTR4_WORD1_SPACE_USED
196 | FATTR4_WORD1_TIME_ACCESS
197 | FATTR4_WORD1_TIME_METADATA
198 | FATTR4_WORD1_TIME_MODIFY
199 | FATTR4_WORD1_MOUNTED_ON_FILEID
202 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
203 struct nfs4_readdir_arg *readdir)
208 readdir->cookie = cookie;
209 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
214 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
219 * NFSv4 servers do not return entries for '.' and '..'
220 * Therefore, we fake these entries here. We let '.'
221 * have cookie 0 and '..' have cookie 1. Note that
222 * when talking to the server, we always send cookie 0
225 start = p = kmap_atomic(*readdir->pages);
228 *p++ = xdr_one; /* next */
229 *p++ = xdr_zero; /* cookie, first word */
230 *p++ = xdr_one; /* cookie, second word */
231 *p++ = xdr_one; /* entry len */
232 memcpy(p, ".\0\0\0", 4); /* entry */
234 *p++ = xdr_one; /* bitmap length */
235 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
236 *p++ = htonl(8); /* attribute buffer length */
237 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
240 *p++ = xdr_one; /* next */
241 *p++ = xdr_zero; /* cookie, first word */
242 *p++ = xdr_two; /* cookie, second word */
243 *p++ = xdr_two; /* entry len */
244 memcpy(p, "..\0\0", 4); /* entry */
246 *p++ = xdr_one; /* bitmap length */
247 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
248 *p++ = htonl(8); /* attribute buffer length */
249 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
251 readdir->pgbase = (char *)p - (char *)start;
252 readdir->count -= readdir->pgbase;
253 kunmap_atomic(start);
256 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
263 *timeout = NFS4_POLL_RETRY_MIN;
264 if (*timeout > NFS4_POLL_RETRY_MAX)
265 *timeout = NFS4_POLL_RETRY_MAX;
266 freezable_schedule_timeout_killable(*timeout);
267 if (fatal_signal_pending(current))
273 /* This is the error handling routine for processes that are allowed
276 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
278 struct nfs_client *clp = server->nfs_client;
279 struct nfs4_state *state = exception->state;
280 struct inode *inode = exception->inode;
283 exception->retry = 0;
287 case -NFS4ERR_OPENMODE:
288 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
289 nfs4_inode_return_delegation(inode);
290 exception->retry = 1;
295 nfs4_schedule_stateid_recovery(server, state);
296 goto wait_on_recovery;
297 case -NFS4ERR_DELEG_REVOKED:
298 case -NFS4ERR_ADMIN_REVOKED:
299 case -NFS4ERR_BAD_STATEID:
302 nfs_remove_bad_delegation(state->inode);
303 nfs4_schedule_stateid_recovery(server, state);
304 goto wait_on_recovery;
305 case -NFS4ERR_EXPIRED:
307 nfs4_schedule_stateid_recovery(server, state);
308 case -NFS4ERR_STALE_STATEID:
309 case -NFS4ERR_STALE_CLIENTID:
310 nfs4_schedule_lease_recovery(clp);
311 goto wait_on_recovery;
312 #if defined(CONFIG_NFS_V4_1)
313 case -NFS4ERR_BADSESSION:
314 case -NFS4ERR_BADSLOT:
315 case -NFS4ERR_BAD_HIGH_SLOT:
316 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
317 case -NFS4ERR_DEADSESSION:
318 case -NFS4ERR_SEQ_FALSE_RETRY:
319 case -NFS4ERR_SEQ_MISORDERED:
320 dprintk("%s ERROR: %d Reset session\n", __func__,
322 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
323 goto wait_on_recovery;
324 #endif /* defined(CONFIG_NFS_V4_1) */
325 case -NFS4ERR_FILE_OPEN:
326 if (exception->timeout > HZ) {
327 /* We have retried a decent amount, time to
336 ret = nfs4_delay(server->client, &exception->timeout);
339 case -NFS4ERR_RETRY_UNCACHED_REP:
340 case -NFS4ERR_OLD_STATEID:
341 exception->retry = 1;
343 case -NFS4ERR_BADOWNER:
344 /* The following works around a Linux server bug! */
345 case -NFS4ERR_BADNAME:
346 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
347 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
348 exception->retry = 1;
349 printk(KERN_WARNING "NFS: v4 server %s "
350 "does not accept raw "
352 "Reenabling the idmapper.\n",
353 server->nfs_client->cl_hostname);
356 /* We failed to handle the error */
357 return nfs4_map_errors(ret);
359 ret = nfs4_wait_clnt_recover(clp);
361 exception->retry = 1;
366 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
368 spin_lock(&clp->cl_lock);
369 if (time_before(clp->cl_last_renewal,timestamp))
370 clp->cl_last_renewal = timestamp;
371 spin_unlock(&clp->cl_lock);
374 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
376 do_renew_lease(server->nfs_client, timestamp);
379 #if defined(CONFIG_NFS_V4_1)
382 * nfs4_shrink_slot_table - free retired slots from the slot table
384 static void nfs4_shrink_slot_table(struct nfs4_slot_table *tbl, u32 newsize)
386 struct nfs4_slot **p;
387 if (newsize >= tbl->max_slots)
394 struct nfs4_slot *slot = *p;
403 * nfs4_free_slot - free a slot and efficiently update slot table.
405 * freeing a slot is trivially done by clearing its respective bit
407 * If the freed slotid equals highest_used_slotid we want to update it
408 * so that the server would be able to size down the slot table if needed,
409 * otherwise we know that the highest_used_slotid is still in use.
410 * When updating highest_used_slotid there may be "holes" in the bitmap
411 * so we need to scan down from highest_used_slotid to 0 looking for the now
412 * highest slotid in use.
413 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
415 * Must be called while holding tbl->slot_tbl_lock
418 nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *slot)
420 u32 slotid = slot->slot_nr;
422 /* clear used bit in bitmap */
423 __clear_bit(slotid, tbl->used_slots);
425 /* update highest_used_slotid when it is freed */
426 if (slotid == tbl->highest_used_slotid) {
427 u32 new_max = find_last_bit(tbl->used_slots, slotid);
428 if (new_max < slotid)
429 tbl->highest_used_slotid = new_max;
431 tbl->highest_used_slotid = NFS4_NO_SLOT;
432 nfs4_session_drain_complete(tbl->session, tbl);
435 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
436 slotid, tbl->highest_used_slotid);
439 bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
441 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
445 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
447 struct nfs4_session *session;
448 struct nfs4_slot_table *tbl;
451 /* just wake up the next guy waiting since
452 * we may have not consumed a slot after all */
453 dprintk("%s: No slot\n", __func__);
456 tbl = res->sr_slot->table;
457 session = tbl->session;
459 spin_lock(&tbl->slot_tbl_lock);
460 nfs4_free_slot(tbl, res->sr_slot);
461 if (!nfs4_session_draining(session))
462 rpc_wake_up_first(&tbl->slot_tbl_waitq,
463 nfs4_set_task_privileged, NULL);
464 spin_unlock(&tbl->slot_tbl_lock);
468 /* Update the client's idea of target_highest_slotid */
469 static void nfs41_set_target_slotid_locked(struct nfs4_slot_table *tbl,
470 u32 target_highest_slotid)
472 unsigned int max_slotid, i;
474 if (tbl->target_highest_slotid == target_highest_slotid)
476 tbl->target_highest_slotid = target_highest_slotid;
479 max_slotid = min(NFS4_MAX_SLOT_TABLE - 1, tbl->target_highest_slotid);
480 for (i = tbl->max_slotid + 1; i <= max_slotid; i++)
481 rpc_wake_up_next(&tbl->slot_tbl_waitq);
482 tbl->max_slotid = max_slotid;
485 void nfs41_set_target_slotid(struct nfs4_slot_table *tbl,
486 u32 target_highest_slotid)
488 spin_lock(&tbl->slot_tbl_lock);
489 nfs41_set_target_slotid_locked(tbl, target_highest_slotid);
490 spin_unlock(&tbl->slot_tbl_lock);
493 static void nfs41_set_server_slotid_locked(struct nfs4_slot_table *tbl,
496 if (tbl->server_highest_slotid == highest_slotid)
498 if (tbl->highest_used_slotid > highest_slotid)
500 /* Deallocate slots */
501 nfs4_shrink_slot_table(tbl, highest_slotid + 1);
502 tbl->server_highest_slotid = highest_slotid;
505 static void nfs41_update_target_slotid(struct nfs4_slot_table *tbl,
506 struct nfs4_slot *slot,
507 struct nfs4_sequence_res *res)
509 spin_lock(&tbl->slot_tbl_lock);
510 if (tbl->generation != slot->generation)
512 nfs41_set_server_slotid_locked(tbl, res->sr_highest_slotid);
513 nfs41_set_target_slotid_locked(tbl, res->sr_target_highest_slotid);
515 spin_unlock(&tbl->slot_tbl_lock);
518 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
520 struct nfs4_session *session;
521 struct nfs4_slot *slot;
522 unsigned long timestamp;
523 struct nfs_client *clp;
526 * sr_status remains 1 if an RPC level error occurred. The server
527 * may or may not have processed the sequence operation..
528 * Proceed as if the server received and processed the sequence
531 if (res->sr_status == 1)
532 res->sr_status = NFS_OK;
534 /* don't increment the sequence number if the task wasn't sent */
535 if (!RPC_WAS_SENT(task))
539 session = slot->table->session;
541 /* Check the SEQUENCE operation status */
542 switch (res->sr_status) {
544 /* Update the slot's sequence and clientid lease timer */
546 timestamp = slot->renewal_time;
548 do_renew_lease(clp, timestamp);
549 /* Check sequence flags */
550 if (res->sr_status_flags != 0)
551 nfs4_schedule_lease_recovery(clp);
552 nfs41_update_target_slotid(slot->table, slot, res);
555 /* The server detected a resend of the RPC call and
556 * returned NFS4ERR_DELAY as per Section 2.10.6.2
559 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
565 /* Just update the slot sequence no. */
569 /* The session may be reset by one of the error handlers. */
570 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
571 nfs41_sequence_free_slot(res);
574 if (!rpc_restart_call(task))
576 rpc_delay(task, NFS4_POLL_RETRY_MAX);
580 static int nfs4_sequence_done(struct rpc_task *task,
581 struct nfs4_sequence_res *res)
583 if (res->sr_slot == NULL)
585 return nfs41_sequence_done(task, res);
588 static struct nfs4_slot *nfs4_new_slot(struct nfs4_slot_table *tbl,
589 u32 slotid, u32 seq_init, gfp_t gfp_mask)
591 struct nfs4_slot *slot;
593 slot = kzalloc(sizeof(*slot), gfp_mask);
596 slot->slot_nr = slotid;
597 slot->seq_nr = seq_init;
602 static struct nfs4_slot *nfs4_find_or_create_slot(struct nfs4_slot_table *tbl,
603 u32 slotid, u32 seq_init, gfp_t gfp_mask)
605 struct nfs4_slot **p, *slot;
610 *p = nfs4_new_slot(tbl, tbl->max_slots,
617 if (slot->slot_nr == slotid)
621 return ERR_PTR(-ENOMEM);
625 * nfs4_alloc_slot - efficiently look for a free slot
627 * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap.
628 * If found, we mark the slot as used, update the highest_used_slotid,
629 * and respectively set up the sequence operation args.
631 * Note: must be called with under the slot_tbl_lock.
633 static struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
635 struct nfs4_slot *ret = ERR_PTR(-EBUSY);
638 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
639 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
640 tbl->max_slotid + 1);
641 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slotid + 1);
642 if (slotid > tbl->max_slotid)
644 ret = nfs4_find_or_create_slot(tbl, slotid, 1, GFP_NOWAIT);
647 __set_bit(slotid, tbl->used_slots);
648 if (slotid > tbl->highest_used_slotid ||
649 tbl->highest_used_slotid == NFS4_NO_SLOT)
650 tbl->highest_used_slotid = slotid;
651 ret->renewal_time = jiffies;
652 ret->generation = tbl->generation;
655 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
656 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
657 !IS_ERR(ret) ? ret->slot_nr : -1);
661 static void nfs41_init_sequence(struct nfs4_sequence_args *args,
662 struct nfs4_sequence_res *res, int cache_reply)
664 args->sa_slot = NULL;
665 args->sa_cache_this = 0;
667 args->sa_cache_this = 1;
671 int nfs41_setup_sequence(struct nfs4_session *session,
672 struct nfs4_sequence_args *args,
673 struct nfs4_sequence_res *res,
674 struct rpc_task *task)
676 struct nfs4_slot *slot;
677 struct nfs4_slot_table *tbl;
679 dprintk("--> %s\n", __func__);
680 /* slot already allocated? */
681 if (res->sr_slot != NULL)
684 tbl = &session->fc_slot_table;
686 task->tk_timeout = 0;
688 spin_lock(&tbl->slot_tbl_lock);
689 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
690 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
691 /* The state manager will wait until the slot table is empty */
692 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
693 spin_unlock(&tbl->slot_tbl_lock);
694 dprintk("%s session is draining\n", __func__);
698 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
699 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
700 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
701 spin_unlock(&tbl->slot_tbl_lock);
702 dprintk("%s enforce FIFO order\n", __func__);
706 slot = nfs4_alloc_slot(tbl);
708 /* If out of memory, try again in 1/4 second */
709 if (slot == ERR_PTR(-ENOMEM))
710 task->tk_timeout = HZ >> 2;
711 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
712 spin_unlock(&tbl->slot_tbl_lock);
713 dprintk("<-- %s: no free slots\n", __func__);
716 spin_unlock(&tbl->slot_tbl_lock);
718 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
720 args->sa_slot = slot;
722 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
723 slot->slot_nr, slot->seq_nr);
726 res->sr_status_flags = 0;
728 * sr_status is only set in decode_sequence, and so will remain
729 * set to 1 if an rpc level failure occurs.
734 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
736 int nfs4_setup_sequence(const struct nfs_server *server,
737 struct nfs4_sequence_args *args,
738 struct nfs4_sequence_res *res,
739 struct rpc_task *task)
741 struct nfs4_session *session = nfs4_get_session(server);
747 dprintk("--> %s clp %p session %p sr_slot %d\n",
748 __func__, session->clp, session, res->sr_slot ?
749 res->sr_slot->slot_nr : -1);
751 ret = nfs41_setup_sequence(session, args, res, task);
753 dprintk("<-- %s status=%d\n", __func__, ret);
757 struct nfs41_call_sync_data {
758 const struct nfs_server *seq_server;
759 struct nfs4_sequence_args *seq_args;
760 struct nfs4_sequence_res *seq_res;
763 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
765 struct nfs41_call_sync_data *data = calldata;
767 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
769 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
770 data->seq_res, task))
772 rpc_call_start(task);
775 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
777 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
778 nfs41_call_sync_prepare(task, calldata);
781 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
783 struct nfs41_call_sync_data *data = calldata;
785 nfs41_sequence_done(task, data->seq_res);
788 static const struct rpc_call_ops nfs41_call_sync_ops = {
789 .rpc_call_prepare = nfs41_call_sync_prepare,
790 .rpc_call_done = nfs41_call_sync_done,
793 static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
794 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
795 .rpc_call_done = nfs41_call_sync_done,
798 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
799 struct nfs_server *server,
800 struct rpc_message *msg,
801 struct nfs4_sequence_args *args,
802 struct nfs4_sequence_res *res,
806 struct rpc_task *task;
807 struct nfs41_call_sync_data data = {
808 .seq_server = server,
812 struct rpc_task_setup task_setup = {
815 .callback_ops = &nfs41_call_sync_ops,
816 .callback_data = &data
820 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
821 task = rpc_run_task(&task_setup);
825 ret = task->tk_status;
831 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
832 struct nfs_server *server,
833 struct rpc_message *msg,
834 struct nfs4_sequence_args *args,
835 struct nfs4_sequence_res *res,
838 nfs41_init_sequence(args, res, cache_reply);
839 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
844 void nfs41_init_sequence(struct nfs4_sequence_args *args,
845 struct nfs4_sequence_res *res, int cache_reply)
849 static int nfs4_sequence_done(struct rpc_task *task,
850 struct nfs4_sequence_res *res)
854 #endif /* CONFIG_NFS_V4_1 */
856 int _nfs4_call_sync(struct rpc_clnt *clnt,
857 struct nfs_server *server,
858 struct rpc_message *msg,
859 struct nfs4_sequence_args *args,
860 struct nfs4_sequence_res *res,
863 nfs41_init_sequence(args, res, cache_reply);
864 return rpc_call_sync(clnt, msg, 0);
868 int nfs4_call_sync(struct rpc_clnt *clnt,
869 struct nfs_server *server,
870 struct rpc_message *msg,
871 struct nfs4_sequence_args *args,
872 struct nfs4_sequence_res *res,
875 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
876 args, res, cache_reply);
879 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
881 struct nfs_inode *nfsi = NFS_I(dir);
883 spin_lock(&dir->i_lock);
884 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
885 if (!cinfo->atomic || cinfo->before != dir->i_version)
886 nfs_force_lookup_revalidate(dir);
887 dir->i_version = cinfo->after;
888 spin_unlock(&dir->i_lock);
891 struct nfs4_opendata {
893 struct nfs_openargs o_arg;
894 struct nfs_openres o_res;
895 struct nfs_open_confirmargs c_arg;
896 struct nfs_open_confirmres c_res;
897 struct nfs4_string owner_name;
898 struct nfs4_string group_name;
899 struct nfs_fattr f_attr;
901 struct dentry *dentry;
902 struct nfs4_state_owner *owner;
903 struct nfs4_state *state;
905 unsigned long timestamp;
906 unsigned int rpc_done : 1;
912 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
914 p->o_res.f_attr = &p->f_attr;
915 p->o_res.seqid = p->o_arg.seqid;
916 p->c_res.seqid = p->c_arg.seqid;
917 p->o_res.server = p->o_arg.server;
918 p->o_res.access_request = p->o_arg.access;
919 nfs_fattr_init(&p->f_attr);
920 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
923 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
924 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
925 const struct iattr *attrs,
928 struct dentry *parent = dget_parent(dentry);
929 struct inode *dir = parent->d_inode;
930 struct nfs_server *server = NFS_SERVER(dir);
931 struct nfs4_opendata *p;
933 p = kzalloc(sizeof(*p), gfp_mask);
936 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
937 if (p->o_arg.seqid == NULL)
939 nfs_sb_active(dentry->d_sb);
940 p->dentry = dget(dentry);
943 atomic_inc(&sp->so_count);
944 p->o_arg.fh = NFS_FH(dir);
945 p->o_arg.open_flags = flags;
946 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
947 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
948 * will return permission denied for all bits until close */
949 if (!(flags & O_EXCL)) {
950 /* ask server to check for all possible rights as results
952 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
953 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
955 p->o_arg.clientid = server->nfs_client->cl_clientid;
956 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
957 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
958 p->o_arg.name = &dentry->d_name;
959 p->o_arg.server = server;
960 p->o_arg.bitmask = server->attr_bitmask;
961 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
962 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
963 if (attrs != NULL && attrs->ia_valid != 0) {
966 p->o_arg.u.attrs = &p->attrs;
967 memcpy(&p->attrs, attrs, sizeof(p->attrs));
970 verf[1] = current->pid;
971 memcpy(p->o_arg.u.verifier.data, verf,
972 sizeof(p->o_arg.u.verifier.data));
974 p->c_arg.fh = &p->o_res.fh;
975 p->c_arg.stateid = &p->o_res.stateid;
976 p->c_arg.seqid = p->o_arg.seqid;
977 nfs4_init_opendata_res(p);
987 static void nfs4_opendata_free(struct kref *kref)
989 struct nfs4_opendata *p = container_of(kref,
990 struct nfs4_opendata, kref);
991 struct super_block *sb = p->dentry->d_sb;
993 nfs_free_seqid(p->o_arg.seqid);
994 if (p->state != NULL)
995 nfs4_put_open_state(p->state);
996 nfs4_put_state_owner(p->owner);
1000 nfs_fattr_free_names(&p->f_attr);
1004 static void nfs4_opendata_put(struct nfs4_opendata *p)
1007 kref_put(&p->kref, nfs4_opendata_free);
1010 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1014 ret = rpc_wait_for_completion_task(task);
1018 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1022 if (open_mode & (O_EXCL|O_TRUNC))
1024 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1026 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1027 && state->n_rdonly != 0;
1030 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1031 && state->n_wronly != 0;
1033 case FMODE_READ|FMODE_WRITE:
1034 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1035 && state->n_rdwr != 0;
1041 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
1043 if (delegation == NULL)
1045 if ((delegation->type & fmode) != fmode)
1047 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1049 nfs_mark_delegation_referenced(delegation);
1053 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1062 case FMODE_READ|FMODE_WRITE:
1065 nfs4_state_set_mode_locked(state, state->state | fmode);
1068 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1070 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1071 nfs4_stateid_copy(&state->stateid, stateid);
1072 nfs4_stateid_copy(&state->open_stateid, stateid);
1075 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1078 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1080 case FMODE_READ|FMODE_WRITE:
1081 set_bit(NFS_O_RDWR_STATE, &state->flags);
1085 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1087 write_seqlock(&state->seqlock);
1088 nfs_set_open_stateid_locked(state, stateid, fmode);
1089 write_sequnlock(&state->seqlock);
1092 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1095 * Protect the call to nfs4_state_set_mode_locked and
1096 * serialise the stateid update
1098 write_seqlock(&state->seqlock);
1099 if (deleg_stateid != NULL) {
1100 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1101 set_bit(NFS_DELEGATED_STATE, &state->flags);
1103 if (open_stateid != NULL)
1104 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1105 write_sequnlock(&state->seqlock);
1106 spin_lock(&state->owner->so_lock);
1107 update_open_stateflags(state, fmode);
1108 spin_unlock(&state->owner->so_lock);
1111 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1113 struct nfs_inode *nfsi = NFS_I(state->inode);
1114 struct nfs_delegation *deleg_cur;
1117 fmode &= (FMODE_READ|FMODE_WRITE);
1120 deleg_cur = rcu_dereference(nfsi->delegation);
1121 if (deleg_cur == NULL)
1124 spin_lock(&deleg_cur->lock);
1125 if (nfsi->delegation != deleg_cur ||
1126 (deleg_cur->type & fmode) != fmode)
1127 goto no_delegation_unlock;
1129 if (delegation == NULL)
1130 delegation = &deleg_cur->stateid;
1131 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1132 goto no_delegation_unlock;
1134 nfs_mark_delegation_referenced(deleg_cur);
1135 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1137 no_delegation_unlock:
1138 spin_unlock(&deleg_cur->lock);
1142 if (!ret && open_stateid != NULL) {
1143 __update_open_stateid(state, open_stateid, NULL, fmode);
1151 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1153 struct nfs_delegation *delegation;
1156 delegation = rcu_dereference(NFS_I(inode)->delegation);
1157 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1162 nfs4_inode_return_delegation(inode);
1165 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1167 struct nfs4_state *state = opendata->state;
1168 struct nfs_inode *nfsi = NFS_I(state->inode);
1169 struct nfs_delegation *delegation;
1170 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
1171 fmode_t fmode = opendata->o_arg.fmode;
1172 nfs4_stateid stateid;
1176 if (can_open_cached(state, fmode, open_mode)) {
1177 spin_lock(&state->owner->so_lock);
1178 if (can_open_cached(state, fmode, open_mode)) {
1179 update_open_stateflags(state, fmode);
1180 spin_unlock(&state->owner->so_lock);
1181 goto out_return_state;
1183 spin_unlock(&state->owner->so_lock);
1186 delegation = rcu_dereference(nfsi->delegation);
1187 if (!can_open_delegated(delegation, fmode)) {
1191 /* Save the delegation */
1192 nfs4_stateid_copy(&stateid, &delegation->stateid);
1194 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1199 /* Try to update the stateid using the delegation */
1200 if (update_open_stateid(state, NULL, &stateid, fmode))
1201 goto out_return_state;
1204 return ERR_PTR(ret);
1206 atomic_inc(&state->count);
1211 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1213 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1214 struct nfs_delegation *delegation;
1215 int delegation_flags = 0;
1218 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1220 delegation_flags = delegation->flags;
1222 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1223 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1224 "returning a delegation for "
1225 "OPEN(CLAIM_DELEGATE_CUR)\n",
1227 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1228 nfs_inode_set_delegation(state->inode,
1229 data->owner->so_cred,
1232 nfs_inode_reclaim_delegation(state->inode,
1233 data->owner->so_cred,
1238 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1239 * and update the nfs4_state.
1241 static struct nfs4_state *
1242 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1244 struct inode *inode = data->state->inode;
1245 struct nfs4_state *state = data->state;
1248 if (!data->rpc_done) {
1249 ret = data->rpc_status;
1254 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1255 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1256 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1260 state = nfs4_get_open_state(inode, data->owner);
1264 ret = nfs_refresh_inode(inode, &data->f_attr);
1268 if (data->o_res.delegation_type != 0)
1269 nfs4_opendata_check_deleg(data, state);
1270 update_open_stateid(state, &data->o_res.stateid, NULL,
1275 return ERR_PTR(ret);
1279 static struct nfs4_state *
1280 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1282 struct inode *inode;
1283 struct nfs4_state *state = NULL;
1286 if (!data->rpc_done) {
1287 state = nfs4_try_open_cached(data);
1292 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1294 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1295 ret = PTR_ERR(inode);
1299 state = nfs4_get_open_state(inode, data->owner);
1302 if (data->o_res.delegation_type != 0)
1303 nfs4_opendata_check_deleg(data, state);
1304 update_open_stateid(state, &data->o_res.stateid, NULL,
1312 return ERR_PTR(ret);
1315 static struct nfs4_state *
1316 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1318 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1319 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1320 return _nfs4_opendata_to_nfs4_state(data);
1323 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1325 struct nfs_inode *nfsi = NFS_I(state->inode);
1326 struct nfs_open_context *ctx;
1328 spin_lock(&state->inode->i_lock);
1329 list_for_each_entry(ctx, &nfsi->open_files, list) {
1330 if (ctx->state != state)
1332 get_nfs_open_context(ctx);
1333 spin_unlock(&state->inode->i_lock);
1336 spin_unlock(&state->inode->i_lock);
1337 return ERR_PTR(-ENOENT);
1340 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1342 struct nfs4_opendata *opendata;
1344 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1345 if (opendata == NULL)
1346 return ERR_PTR(-ENOMEM);
1347 opendata->state = state;
1348 atomic_inc(&state->count);
1352 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1354 struct nfs4_state *newstate;
1357 opendata->o_arg.open_flags = 0;
1358 opendata->o_arg.fmode = fmode;
1359 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1360 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1361 nfs4_init_opendata_res(opendata);
1362 ret = _nfs4_recover_proc_open(opendata);
1365 newstate = nfs4_opendata_to_nfs4_state(opendata);
1366 if (IS_ERR(newstate))
1367 return PTR_ERR(newstate);
1368 nfs4_close_state(newstate, fmode);
1373 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1375 struct nfs4_state *newstate;
1378 /* memory barrier prior to reading state->n_* */
1379 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1381 if (state->n_rdwr != 0) {
1382 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1383 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1386 if (newstate != state)
1389 if (state->n_wronly != 0) {
1390 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1391 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1394 if (newstate != state)
1397 if (state->n_rdonly != 0) {
1398 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1399 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1402 if (newstate != state)
1406 * We may have performed cached opens for all three recoveries.
1407 * Check if we need to update the current stateid.
1409 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1410 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1411 write_seqlock(&state->seqlock);
1412 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1413 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1414 write_sequnlock(&state->seqlock);
1421 * reclaim state on the server after a reboot.
1423 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1425 struct nfs_delegation *delegation;
1426 struct nfs4_opendata *opendata;
1427 fmode_t delegation_type = 0;
1430 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1431 if (IS_ERR(opendata))
1432 return PTR_ERR(opendata);
1433 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1434 opendata->o_arg.fh = NFS_FH(state->inode);
1436 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1437 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1438 delegation_type = delegation->type;
1440 opendata->o_arg.u.delegation_type = delegation_type;
1441 status = nfs4_open_recover(opendata, state);
1442 nfs4_opendata_put(opendata);
1446 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1448 struct nfs_server *server = NFS_SERVER(state->inode);
1449 struct nfs4_exception exception = { };
1452 err = _nfs4_do_open_reclaim(ctx, state);
1453 if (err != -NFS4ERR_DELAY)
1455 nfs4_handle_exception(server, err, &exception);
1456 } while (exception.retry);
1460 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1462 struct nfs_open_context *ctx;
1465 ctx = nfs4_state_find_open_context(state);
1467 return PTR_ERR(ctx);
1468 ret = nfs4_do_open_reclaim(ctx, state);
1469 put_nfs_open_context(ctx);
1473 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1475 struct nfs4_opendata *opendata;
1478 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1479 if (IS_ERR(opendata))
1480 return PTR_ERR(opendata);
1481 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1482 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1483 ret = nfs4_open_recover(opendata, state);
1484 nfs4_opendata_put(opendata);
1488 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1490 struct nfs4_exception exception = { };
1491 struct nfs_server *server = NFS_SERVER(state->inode);
1494 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1500 case -NFS4ERR_BADSESSION:
1501 case -NFS4ERR_BADSLOT:
1502 case -NFS4ERR_BAD_HIGH_SLOT:
1503 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1504 case -NFS4ERR_DEADSESSION:
1505 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1507 case -NFS4ERR_STALE_CLIENTID:
1508 case -NFS4ERR_STALE_STATEID:
1509 case -NFS4ERR_EXPIRED:
1510 /* Don't recall a delegation if it was lost */
1511 nfs4_schedule_lease_recovery(server->nfs_client);
1515 * The show must go on: exit, but mark the
1516 * stateid as needing recovery.
1518 case -NFS4ERR_DELEG_REVOKED:
1519 case -NFS4ERR_ADMIN_REVOKED:
1520 case -NFS4ERR_BAD_STATEID:
1521 nfs_inode_find_state_and_recover(state->inode,
1523 nfs4_schedule_stateid_recovery(server, state);
1526 * User RPCSEC_GSS context has expired.
1527 * We cannot recover this stateid now, so
1528 * skip it and allow recovery thread to
1535 err = nfs4_handle_exception(server, err, &exception);
1536 } while (exception.retry);
1541 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1543 struct nfs4_opendata *data = calldata;
1545 data->rpc_status = task->tk_status;
1546 if (data->rpc_status == 0) {
1547 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1548 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1549 renew_lease(data->o_res.server, data->timestamp);
1554 static void nfs4_open_confirm_release(void *calldata)
1556 struct nfs4_opendata *data = calldata;
1557 struct nfs4_state *state = NULL;
1559 /* If this request hasn't been cancelled, do nothing */
1560 if (data->cancelled == 0)
1562 /* In case of error, no cleanup! */
1563 if (!data->rpc_done)
1565 state = nfs4_opendata_to_nfs4_state(data);
1567 nfs4_close_state(state, data->o_arg.fmode);
1569 nfs4_opendata_put(data);
1572 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1573 .rpc_call_done = nfs4_open_confirm_done,
1574 .rpc_release = nfs4_open_confirm_release,
1578 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1580 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1582 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1583 struct rpc_task *task;
1584 struct rpc_message msg = {
1585 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1586 .rpc_argp = &data->c_arg,
1587 .rpc_resp = &data->c_res,
1588 .rpc_cred = data->owner->so_cred,
1590 struct rpc_task_setup task_setup_data = {
1591 .rpc_client = server->client,
1592 .rpc_message = &msg,
1593 .callback_ops = &nfs4_open_confirm_ops,
1594 .callback_data = data,
1595 .workqueue = nfsiod_workqueue,
1596 .flags = RPC_TASK_ASYNC,
1600 kref_get(&data->kref);
1602 data->rpc_status = 0;
1603 data->timestamp = jiffies;
1604 task = rpc_run_task(&task_setup_data);
1606 return PTR_ERR(task);
1607 status = nfs4_wait_for_completion_rpc_task(task);
1609 data->cancelled = 1;
1612 status = data->rpc_status;
1617 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1619 struct nfs4_opendata *data = calldata;
1620 struct nfs4_state_owner *sp = data->owner;
1622 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1625 * Check if we still need to send an OPEN call, or if we can use
1626 * a delegation instead.
1628 if (data->state != NULL) {
1629 struct nfs_delegation *delegation;
1631 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1634 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1635 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1636 can_open_delegated(delegation, data->o_arg.fmode))
1637 goto unlock_no_action;
1640 /* Update client id. */
1641 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1642 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1643 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1644 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1645 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1647 data->timestamp = jiffies;
1648 if (nfs4_setup_sequence(data->o_arg.server,
1649 &data->o_arg.seq_args,
1650 &data->o_res.seq_res,
1652 nfs_release_seqid(data->o_arg.seqid);
1654 rpc_call_start(task);
1659 task->tk_action = NULL;
1663 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1665 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1666 nfs4_open_prepare(task, calldata);
1669 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1671 struct nfs4_opendata *data = calldata;
1673 data->rpc_status = task->tk_status;
1675 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1678 if (task->tk_status == 0) {
1679 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1680 switch (data->o_res.f_attr->mode & S_IFMT) {
1684 data->rpc_status = -ELOOP;
1687 data->rpc_status = -EISDIR;
1690 data->rpc_status = -ENOTDIR;
1693 renew_lease(data->o_res.server, data->timestamp);
1694 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1695 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1700 static void nfs4_open_release(void *calldata)
1702 struct nfs4_opendata *data = calldata;
1703 struct nfs4_state *state = NULL;
1705 /* If this request hasn't been cancelled, do nothing */
1706 if (data->cancelled == 0)
1708 /* In case of error, no cleanup! */
1709 if (data->rpc_status != 0 || !data->rpc_done)
1711 /* In case we need an open_confirm, no cleanup! */
1712 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1714 state = nfs4_opendata_to_nfs4_state(data);
1716 nfs4_close_state(state, data->o_arg.fmode);
1718 nfs4_opendata_put(data);
1721 static const struct rpc_call_ops nfs4_open_ops = {
1722 .rpc_call_prepare = nfs4_open_prepare,
1723 .rpc_call_done = nfs4_open_done,
1724 .rpc_release = nfs4_open_release,
1727 static const struct rpc_call_ops nfs4_recover_open_ops = {
1728 .rpc_call_prepare = nfs4_recover_open_prepare,
1729 .rpc_call_done = nfs4_open_done,
1730 .rpc_release = nfs4_open_release,
1733 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1735 struct inode *dir = data->dir->d_inode;
1736 struct nfs_server *server = NFS_SERVER(dir);
1737 struct nfs_openargs *o_arg = &data->o_arg;
1738 struct nfs_openres *o_res = &data->o_res;
1739 struct rpc_task *task;
1740 struct rpc_message msg = {
1741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1744 .rpc_cred = data->owner->so_cred,
1746 struct rpc_task_setup task_setup_data = {
1747 .rpc_client = server->client,
1748 .rpc_message = &msg,
1749 .callback_ops = &nfs4_open_ops,
1750 .callback_data = data,
1751 .workqueue = nfsiod_workqueue,
1752 .flags = RPC_TASK_ASYNC,
1756 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1757 kref_get(&data->kref);
1759 data->rpc_status = 0;
1760 data->cancelled = 0;
1762 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1763 task = rpc_run_task(&task_setup_data);
1765 return PTR_ERR(task);
1766 status = nfs4_wait_for_completion_rpc_task(task);
1768 data->cancelled = 1;
1771 status = data->rpc_status;
1777 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1779 struct inode *dir = data->dir->d_inode;
1780 struct nfs_openres *o_res = &data->o_res;
1783 status = nfs4_run_open_task(data, 1);
1784 if (status != 0 || !data->rpc_done)
1787 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1789 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1790 status = _nfs4_proc_open_confirm(data);
1798 static int nfs4_opendata_access(struct rpc_cred *cred,
1799 struct nfs4_opendata *opendata,
1800 struct nfs4_state *state, fmode_t fmode)
1802 struct nfs_access_entry cache;
1805 /* access call failed or for some reason the server doesn't
1806 * support any access modes -- defer access call until later */
1807 if (opendata->o_res.access_supported == 0)
1811 /* don't check MAY_WRITE - a newly created file may not have
1812 * write mode bits, but POSIX allows the creating process to write */
1813 if (fmode & FMODE_READ)
1815 if (fmode & FMODE_EXEC)
1819 cache.jiffies = jiffies;
1820 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1821 nfs_access_add_cache(state->inode, &cache);
1823 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1826 /* even though OPEN succeeded, access is denied. Close the file */
1827 nfs4_close_state(state, fmode);
1832 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1834 static int _nfs4_proc_open(struct nfs4_opendata *data)
1836 struct inode *dir = data->dir->d_inode;
1837 struct nfs_server *server = NFS_SERVER(dir);
1838 struct nfs_openargs *o_arg = &data->o_arg;
1839 struct nfs_openres *o_res = &data->o_res;
1842 status = nfs4_run_open_task(data, 0);
1843 if (!data->rpc_done)
1846 if (status == -NFS4ERR_BADNAME &&
1847 !(o_arg->open_flags & O_CREAT))
1852 nfs_fattr_map_and_free_names(server, &data->f_attr);
1854 if (o_arg->open_flags & O_CREAT)
1855 update_changeattr(dir, &o_res->cinfo);
1856 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1857 server->caps &= ~NFS_CAP_POSIX_LOCK;
1858 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1859 status = _nfs4_proc_open_confirm(data);
1863 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1864 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1868 static int nfs4_recover_expired_lease(struct nfs_server *server)
1870 return nfs4_client_recover_expired_lease(server->nfs_client);
1875 * reclaim state on the server after a network partition.
1876 * Assumes caller holds the appropriate lock
1878 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1880 struct nfs4_opendata *opendata;
1883 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1884 if (IS_ERR(opendata))
1885 return PTR_ERR(opendata);
1886 ret = nfs4_open_recover(opendata, state);
1888 d_drop(ctx->dentry);
1889 nfs4_opendata_put(opendata);
1893 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1895 struct nfs_server *server = NFS_SERVER(state->inode);
1896 struct nfs4_exception exception = { };
1900 err = _nfs4_open_expired(ctx, state);
1904 case -NFS4ERR_GRACE:
1905 case -NFS4ERR_DELAY:
1906 nfs4_handle_exception(server, err, &exception);
1909 } while (exception.retry);
1914 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1916 struct nfs_open_context *ctx;
1919 ctx = nfs4_state_find_open_context(state);
1921 return PTR_ERR(ctx);
1922 ret = nfs4_do_open_expired(ctx, state);
1923 put_nfs_open_context(ctx);
1927 #if defined(CONFIG_NFS_V4_1)
1928 static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
1930 struct nfs_server *server = NFS_SERVER(state->inode);
1931 nfs4_stateid *stateid = &state->stateid;
1934 /* If a state reset has been done, test_stateid is unneeded */
1935 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1938 status = nfs41_test_stateid(server, stateid);
1939 if (status != NFS_OK) {
1940 /* Free the stateid unless the server explicitly
1941 * informs us the stateid is unrecognized. */
1942 if (status != -NFS4ERR_BAD_STATEID)
1943 nfs41_free_stateid(server, stateid);
1944 nfs_remove_bad_delegation(state->inode);
1946 write_seqlock(&state->seqlock);
1947 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1948 write_sequnlock(&state->seqlock);
1949 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1954 * nfs41_check_open_stateid - possibly free an open stateid
1956 * @state: NFSv4 state for an inode
1958 * Returns NFS_OK if recovery for this stateid is now finished.
1959 * Otherwise a negative NFS4ERR value is returned.
1961 static int nfs41_check_open_stateid(struct nfs4_state *state)
1963 struct nfs_server *server = NFS_SERVER(state->inode);
1964 nfs4_stateid *stateid = &state->open_stateid;
1967 /* If a state reset has been done, test_stateid is unneeded */
1968 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1969 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1970 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1971 return -NFS4ERR_BAD_STATEID;
1973 status = nfs41_test_stateid(server, stateid);
1974 if (status != NFS_OK) {
1975 /* Free the stateid unless the server explicitly
1976 * informs us the stateid is unrecognized. */
1977 if (status != -NFS4ERR_BAD_STATEID)
1978 nfs41_free_stateid(server, stateid);
1980 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1981 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1982 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1987 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1991 nfs41_clear_delegation_stateid(state);
1992 status = nfs41_check_open_stateid(state);
1993 if (status != NFS_OK)
1994 status = nfs4_open_expired(sp, state);
2000 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2001 * fields corresponding to attributes that were used to store the verifier.
2002 * Make sure we clobber those fields in the later setattr call
2004 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2006 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2007 !(sattr->ia_valid & ATTR_ATIME_SET))
2008 sattr->ia_valid |= ATTR_ATIME;
2010 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2011 !(sattr->ia_valid & ATTR_MTIME_SET))
2012 sattr->ia_valid |= ATTR_MTIME;
2016 * Returns a referenced nfs4_state
2018 static int _nfs4_do_open(struct inode *dir,
2019 struct dentry *dentry,
2022 struct iattr *sattr,
2023 struct rpc_cred *cred,
2024 struct nfs4_state **res,
2025 struct nfs4_threshold **ctx_th)
2027 struct nfs4_state_owner *sp;
2028 struct nfs4_state *state = NULL;
2029 struct nfs_server *server = NFS_SERVER(dir);
2030 struct nfs4_opendata *opendata;
2033 /* Protect against reboot recovery conflicts */
2035 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2037 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2040 status = nfs4_recover_expired_lease(server);
2042 goto err_put_state_owner;
2043 if (dentry->d_inode != NULL)
2044 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
2046 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
2047 if (opendata == NULL)
2048 goto err_put_state_owner;
2050 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2051 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2052 if (!opendata->f_attr.mdsthreshold)
2053 goto err_opendata_put;
2054 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2056 if (dentry->d_inode != NULL)
2057 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
2059 status = _nfs4_proc_open(opendata);
2061 goto err_opendata_put;
2063 state = nfs4_opendata_to_nfs4_state(opendata);
2064 status = PTR_ERR(state);
2066 goto err_opendata_put;
2067 if (server->caps & NFS_CAP_POSIX_LOCK)
2068 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2070 status = nfs4_opendata_access(cred, opendata, state, fmode);
2072 goto err_opendata_put;
2074 if (opendata->o_arg.open_flags & O_EXCL) {
2075 nfs4_exclusive_attrset(opendata, sattr);
2077 nfs_fattr_init(opendata->o_res.f_attr);
2078 status = nfs4_do_setattr(state->inode, cred,
2079 opendata->o_res.f_attr, sattr,
2082 nfs_setattr_update_inode(state->inode, sattr);
2083 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2086 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2087 *ctx_th = opendata->f_attr.mdsthreshold;
2089 kfree(opendata->f_attr.mdsthreshold);
2090 opendata->f_attr.mdsthreshold = NULL;
2092 nfs4_opendata_put(opendata);
2093 nfs4_put_state_owner(sp);
2097 kfree(opendata->f_attr.mdsthreshold);
2098 nfs4_opendata_put(opendata);
2099 err_put_state_owner:
2100 nfs4_put_state_owner(sp);
2107 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2108 struct dentry *dentry,
2111 struct iattr *sattr,
2112 struct rpc_cred *cred,
2113 struct nfs4_threshold **ctx_th)
2115 struct nfs4_exception exception = { };
2116 struct nfs4_state *res;
2119 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
2121 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2125 /* NOTE: BAD_SEQID means the server and client disagree about the
2126 * book-keeping w.r.t. state-changing operations
2127 * (OPEN/CLOSE/LOCK/LOCKU...)
2128 * It is actually a sign of a bug on the client or on the server.
2130 * If we receive a BAD_SEQID error in the particular case of
2131 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2132 * have unhashed the old state_owner for us, and that we can
2133 * therefore safely retry using a new one. We should still warn
2134 * the user though...
2136 if (status == -NFS4ERR_BAD_SEQID) {
2137 pr_warn_ratelimited("NFS: v4 server %s "
2138 " returned a bad sequence-id error!\n",
2139 NFS_SERVER(dir)->nfs_client->cl_hostname);
2140 exception.retry = 1;
2144 * BAD_STATEID on OPEN means that the server cancelled our
2145 * state before it received the OPEN_CONFIRM.
2146 * Recover by retrying the request as per the discussion
2147 * on Page 181 of RFC3530.
2149 if (status == -NFS4ERR_BAD_STATEID) {
2150 exception.retry = 1;
2153 if (status == -EAGAIN) {
2154 /* We must have found a delegation */
2155 exception.retry = 1;
2158 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2159 status, &exception));
2160 } while (exception.retry);
2164 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2165 struct nfs_fattr *fattr, struct iattr *sattr,
2166 struct nfs4_state *state)
2168 struct nfs_server *server = NFS_SERVER(inode);
2169 struct nfs_setattrargs arg = {
2170 .fh = NFS_FH(inode),
2173 .bitmask = server->attr_bitmask,
2175 struct nfs_setattrres res = {
2179 struct rpc_message msg = {
2180 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2185 unsigned long timestamp = jiffies;
2188 nfs_fattr_init(fattr);
2190 if (state != NULL) {
2191 struct nfs_lockowner lockowner = {
2192 .l_owner = current->files,
2193 .l_pid = current->tgid,
2195 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2197 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2199 /* Use that stateid */
2201 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2203 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2204 if (status == 0 && state != NULL)
2205 renew_lease(server, timestamp);
2209 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2210 struct nfs_fattr *fattr, struct iattr *sattr,
2211 struct nfs4_state *state)
2213 struct nfs_server *server = NFS_SERVER(inode);
2214 struct nfs4_exception exception = {
2220 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2222 case -NFS4ERR_OPENMODE:
2223 if (state && !(state->state & FMODE_WRITE)) {
2225 if (sattr->ia_valid & ATTR_OPEN)
2230 err = nfs4_handle_exception(server, err, &exception);
2231 } while (exception.retry);
2236 struct nfs4_closedata {
2237 struct inode *inode;
2238 struct nfs4_state *state;
2239 struct nfs_closeargs arg;
2240 struct nfs_closeres res;
2241 struct nfs_fattr fattr;
2242 unsigned long timestamp;
2247 static void nfs4_free_closedata(void *data)
2249 struct nfs4_closedata *calldata = data;
2250 struct nfs4_state_owner *sp = calldata->state->owner;
2251 struct super_block *sb = calldata->state->inode->i_sb;
2254 pnfs_roc_release(calldata->state->inode);
2255 nfs4_put_open_state(calldata->state);
2256 nfs_free_seqid(calldata->arg.seqid);
2257 nfs4_put_state_owner(sp);
2258 nfs_sb_deactive_async(sb);
2262 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2265 spin_lock(&state->owner->so_lock);
2266 if (!(fmode & FMODE_READ))
2267 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2268 if (!(fmode & FMODE_WRITE))
2269 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2270 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2271 spin_unlock(&state->owner->so_lock);
2274 static void nfs4_close_done(struct rpc_task *task, void *data)
2276 struct nfs4_closedata *calldata = data;
2277 struct nfs4_state *state = calldata->state;
2278 struct nfs_server *server = NFS_SERVER(calldata->inode);
2280 dprintk("%s: begin!\n", __func__);
2281 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2283 /* hmm. we are done with the inode, and in the process of freeing
2284 * the state_owner. we keep this around to process errors
2286 switch (task->tk_status) {
2289 pnfs_roc_set_barrier(state->inode,
2290 calldata->roc_barrier);
2291 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2292 renew_lease(server, calldata->timestamp);
2293 nfs4_close_clear_stateid_flags(state,
2294 calldata->arg.fmode);
2296 case -NFS4ERR_STALE_STATEID:
2297 case -NFS4ERR_OLD_STATEID:
2298 case -NFS4ERR_BAD_STATEID:
2299 case -NFS4ERR_EXPIRED:
2300 if (calldata->arg.fmode == 0)
2303 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2304 rpc_restart_call_prepare(task);
2306 nfs_release_seqid(calldata->arg.seqid);
2307 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2308 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2311 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2313 struct nfs4_closedata *calldata = data;
2314 struct nfs4_state *state = calldata->state;
2315 struct inode *inode = calldata->inode;
2318 dprintk("%s: begin!\n", __func__);
2319 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2322 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2323 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2324 spin_lock(&state->owner->so_lock);
2325 /* Calculate the change in open mode */
2326 if (state->n_rdwr == 0) {
2327 if (state->n_rdonly == 0) {
2328 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2329 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2330 calldata->arg.fmode &= ~FMODE_READ;
2332 if (state->n_wronly == 0) {
2333 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2334 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2335 calldata->arg.fmode &= ~FMODE_WRITE;
2338 spin_unlock(&state->owner->so_lock);
2341 /* Note: exit _without_ calling nfs4_close_done */
2342 task->tk_action = NULL;
2346 if (calldata->arg.fmode == 0) {
2347 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2348 if (calldata->roc &&
2349 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
2353 nfs_fattr_init(calldata->res.fattr);
2354 calldata->timestamp = jiffies;
2355 if (nfs4_setup_sequence(NFS_SERVER(inode),
2356 &calldata->arg.seq_args,
2357 &calldata->res.seq_res,
2359 nfs_release_seqid(calldata->arg.seqid);
2361 rpc_call_start(task);
2363 dprintk("%s: done!\n", __func__);
2366 static const struct rpc_call_ops nfs4_close_ops = {
2367 .rpc_call_prepare = nfs4_close_prepare,
2368 .rpc_call_done = nfs4_close_done,
2369 .rpc_release = nfs4_free_closedata,
2373 * It is possible for data to be read/written from a mem-mapped file
2374 * after the sys_close call (which hits the vfs layer as a flush).
2375 * This means that we can't safely call nfsv4 close on a file until
2376 * the inode is cleared. This in turn means that we are not good
2377 * NFSv4 citizens - we do not indicate to the server to update the file's
2378 * share state even when we are done with one of the three share
2379 * stateid's in the inode.
2381 * NOTE: Caller must be holding the sp->so_owner semaphore!
2383 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2385 struct nfs_server *server = NFS_SERVER(state->inode);
2386 struct nfs4_closedata *calldata;
2387 struct nfs4_state_owner *sp = state->owner;
2388 struct rpc_task *task;
2389 struct rpc_message msg = {
2390 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2391 .rpc_cred = state->owner->so_cred,
2393 struct rpc_task_setup task_setup_data = {
2394 .rpc_client = server->client,
2395 .rpc_message = &msg,
2396 .callback_ops = &nfs4_close_ops,
2397 .workqueue = nfsiod_workqueue,
2398 .flags = RPC_TASK_ASYNC,
2400 int status = -ENOMEM;
2402 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2403 if (calldata == NULL)
2405 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2406 calldata->inode = state->inode;
2407 calldata->state = state;
2408 calldata->arg.fh = NFS_FH(state->inode);
2409 calldata->arg.stateid = &state->open_stateid;
2410 /* Serialization for the sequence id */
2411 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2412 if (calldata->arg.seqid == NULL)
2413 goto out_free_calldata;
2414 calldata->arg.fmode = 0;
2415 calldata->arg.bitmask = server->cache_consistency_bitmask;
2416 calldata->res.fattr = &calldata->fattr;
2417 calldata->res.seqid = calldata->arg.seqid;
2418 calldata->res.server = server;
2419 calldata->roc = pnfs_roc(state->inode);
2420 nfs_sb_active(calldata->inode->i_sb);
2422 msg.rpc_argp = &calldata->arg;
2423 msg.rpc_resp = &calldata->res;
2424 task_setup_data.callback_data = calldata;
2425 task = rpc_run_task(&task_setup_data);
2427 return PTR_ERR(task);
2430 status = rpc_wait_for_completion_task(task);
2436 nfs4_put_open_state(state);
2437 nfs4_put_state_owner(sp);
2441 static struct inode *
2442 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2444 struct nfs4_state *state;
2446 /* Protect against concurrent sillydeletes */
2447 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2448 ctx->cred, &ctx->mdsthreshold);
2450 return ERR_CAST(state);
2452 return igrab(state->inode);
2455 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2457 if (ctx->state == NULL)
2460 nfs4_close_sync(ctx->state, ctx->mode);
2462 nfs4_close_state(ctx->state, ctx->mode);
2465 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2467 struct nfs4_server_caps_arg args = {
2470 struct nfs4_server_caps_res res = {};
2471 struct rpc_message msg = {
2472 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2478 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2480 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2481 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2482 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2483 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2484 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2485 NFS_CAP_CTIME|NFS_CAP_MTIME);
2486 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2487 server->caps |= NFS_CAP_ACLS;
2488 if (res.has_links != 0)
2489 server->caps |= NFS_CAP_HARDLINKS;
2490 if (res.has_symlinks != 0)
2491 server->caps |= NFS_CAP_SYMLINKS;
2492 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2493 server->caps |= NFS_CAP_FILEID;
2494 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2495 server->caps |= NFS_CAP_MODE;
2496 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2497 server->caps |= NFS_CAP_NLINK;
2498 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2499 server->caps |= NFS_CAP_OWNER;
2500 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2501 server->caps |= NFS_CAP_OWNER_GROUP;
2502 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2503 server->caps |= NFS_CAP_ATIME;
2504 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2505 server->caps |= NFS_CAP_CTIME;
2506 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2507 server->caps |= NFS_CAP_MTIME;
2509 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2510 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2511 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2512 server->acl_bitmask = res.acl_bitmask;
2513 server->fh_expire_type = res.fh_expire_type;
2519 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2521 struct nfs4_exception exception = { };
2524 err = nfs4_handle_exception(server,
2525 _nfs4_server_capabilities(server, fhandle),
2527 } while (exception.retry);
2531 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2532 struct nfs_fsinfo *info)
2534 struct nfs4_lookup_root_arg args = {
2535 .bitmask = nfs4_fattr_bitmap,
2537 struct nfs4_lookup_res res = {
2539 .fattr = info->fattr,
2542 struct rpc_message msg = {
2543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2548 nfs_fattr_init(info->fattr);
2549 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2552 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2553 struct nfs_fsinfo *info)
2555 struct nfs4_exception exception = { };
2558 err = _nfs4_lookup_root(server, fhandle, info);
2561 case -NFS4ERR_WRONGSEC:
2564 err = nfs4_handle_exception(server, err, &exception);
2566 } while (exception.retry);
2571 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2572 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2574 struct rpc_auth *auth;
2577 auth = rpcauth_create(flavor, server->client);
2582 ret = nfs4_lookup_root(server, fhandle, info);
2587 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2588 struct nfs_fsinfo *info)
2590 int i, len, status = 0;
2591 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2593 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2597 for (i = 0; i < len; i++) {
2598 /* AUTH_UNIX is the default flavor if none was specified,
2599 * thus has already been tried. */
2600 if (flav_array[i] == RPC_AUTH_UNIX)
2603 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2604 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2609 * -EACCESS could mean that the user doesn't have correct permissions
2610 * to access the mount. It could also mean that we tried to mount
2611 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2612 * existing mount programs don't handle -EACCES very well so it should
2613 * be mapped to -EPERM instead.
2615 if (status == -EACCES)
2621 * get the file handle for the "/" directory on the server
2623 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2624 struct nfs_fsinfo *info)
2626 int minor_version = server->nfs_client->cl_minorversion;
2627 int status = nfs4_lookup_root(server, fhandle, info);
2628 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2630 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2631 * by nfs4_map_errors() as this function exits.
2633 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2635 status = nfs4_server_capabilities(server, fhandle);
2637 status = nfs4_do_fsinfo(server, fhandle, info);
2638 return nfs4_map_errors(status);
2641 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2642 struct nfs_fsinfo *info)
2645 struct nfs_fattr *fattr = info->fattr;
2647 error = nfs4_server_capabilities(server, mntfh);
2649 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2653 error = nfs4_proc_getattr(server, mntfh, fattr);
2655 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2659 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2660 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2661 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2667 * Get locations and (maybe) other attributes of a referral.
2668 * Note that we'll actually follow the referral later when
2669 * we detect fsid mismatch in inode revalidation
2671 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2672 const struct qstr *name, struct nfs_fattr *fattr,
2673 struct nfs_fh *fhandle)
2675 int status = -ENOMEM;
2676 struct page *page = NULL;
2677 struct nfs4_fs_locations *locations = NULL;
2679 page = alloc_page(GFP_KERNEL);
2682 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2683 if (locations == NULL)
2686 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2689 /* Make sure server returned a different fsid for the referral */
2690 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2691 dprintk("%s: server did not return a different fsid for"
2692 " a referral at %s\n", __func__, name->name);
2696 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2697 nfs_fixup_referral_attributes(&locations->fattr);
2699 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2700 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2701 memset(fhandle, 0, sizeof(struct nfs_fh));
2709 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2711 struct nfs4_getattr_arg args = {
2713 .bitmask = server->attr_bitmask,
2715 struct nfs4_getattr_res res = {
2719 struct rpc_message msg = {
2720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2725 nfs_fattr_init(fattr);
2726 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2729 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2731 struct nfs4_exception exception = { };
2734 err = nfs4_handle_exception(server,
2735 _nfs4_proc_getattr(server, fhandle, fattr),
2737 } while (exception.retry);
2742 * The file is not closed if it is opened due to the a request to change
2743 * the size of the file. The open call will not be needed once the
2744 * VFS layer lookup-intents are implemented.
2746 * Close is called when the inode is destroyed.
2747 * If we haven't opened the file for O_WRONLY, we
2748 * need to in the size_change case to obtain a stateid.
2751 * Because OPEN is always done by name in nfsv4, it is
2752 * possible that we opened a different file by the same
2753 * name. We can recognize this race condition, but we
2754 * can't do anything about it besides returning an error.
2756 * This will be fixed with VFS changes (lookup-intent).
2759 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2760 struct iattr *sattr)
2762 struct inode *inode = dentry->d_inode;
2763 struct rpc_cred *cred = NULL;
2764 struct nfs4_state *state = NULL;
2767 if (pnfs_ld_layoutret_on_setattr(inode))
2768 pnfs_return_layout(inode);
2770 nfs_fattr_init(fattr);
2772 /* Deal with open(O_TRUNC) */
2773 if (sattr->ia_valid & ATTR_OPEN)
2774 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2776 /* Optimization: if the end result is no change, don't RPC */
2777 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2780 /* Search for an existing open(O_WRITE) file */
2781 if (sattr->ia_valid & ATTR_FILE) {
2782 struct nfs_open_context *ctx;
2784 ctx = nfs_file_open_context(sattr->ia_file);
2791 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2793 nfs_setattr_update_inode(inode, sattr);
2797 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2798 const struct qstr *name, struct nfs_fh *fhandle,
2799 struct nfs_fattr *fattr)
2801 struct nfs_server *server = NFS_SERVER(dir);
2803 struct nfs4_lookup_arg args = {
2804 .bitmask = server->attr_bitmask,
2805 .dir_fh = NFS_FH(dir),
2808 struct nfs4_lookup_res res = {
2813 struct rpc_message msg = {
2814 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2819 nfs_fattr_init(fattr);
2821 dprintk("NFS call lookup %s\n", name->name);
2822 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2823 dprintk("NFS reply lookup: %d\n", status);
2827 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2829 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2830 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2831 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2835 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2836 struct qstr *name, struct nfs_fh *fhandle,
2837 struct nfs_fattr *fattr)
2839 struct nfs4_exception exception = { };
2840 struct rpc_clnt *client = *clnt;
2843 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2845 case -NFS4ERR_BADNAME:
2848 case -NFS4ERR_MOVED:
2849 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
2851 case -NFS4ERR_WRONGSEC:
2853 if (client != *clnt)
2856 client = nfs4_create_sec_client(client, dir, name);
2858 return PTR_ERR(client);
2860 exception.retry = 1;
2863 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2865 } while (exception.retry);
2870 else if (client != *clnt)
2871 rpc_shutdown_client(client);
2876 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
2877 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2880 struct rpc_clnt *client = NFS_CLIENT(dir);
2882 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2883 if (client != NFS_CLIENT(dir)) {
2884 rpc_shutdown_client(client);
2885 nfs_fixup_secinfo_attributes(fattr);
2891 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2892 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2895 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2897 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2899 rpc_shutdown_client(client);
2900 return ERR_PTR(status);
2905 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2907 struct nfs_server *server = NFS_SERVER(inode);
2908 struct nfs4_accessargs args = {
2909 .fh = NFS_FH(inode),
2910 .bitmask = server->cache_consistency_bitmask,
2912 struct nfs4_accessres res = {
2915 struct rpc_message msg = {
2916 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2919 .rpc_cred = entry->cred,
2921 int mode = entry->mask;
2925 * Determine which access bits we want to ask for...
2927 if (mode & MAY_READ)
2928 args.access |= NFS4_ACCESS_READ;
2929 if (S_ISDIR(inode->i_mode)) {
2930 if (mode & MAY_WRITE)
2931 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2932 if (mode & MAY_EXEC)
2933 args.access |= NFS4_ACCESS_LOOKUP;
2935 if (mode & MAY_WRITE)
2936 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2937 if (mode & MAY_EXEC)
2938 args.access |= NFS4_ACCESS_EXECUTE;
2941 res.fattr = nfs_alloc_fattr();
2942 if (res.fattr == NULL)
2945 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2947 nfs_access_set_mask(entry, res.access);
2948 nfs_refresh_inode(inode, res.fattr);
2950 nfs_free_fattr(res.fattr);
2954 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2956 struct nfs4_exception exception = { };
2959 err = nfs4_handle_exception(NFS_SERVER(inode),
2960 _nfs4_proc_access(inode, entry),
2962 } while (exception.retry);
2967 * TODO: For the time being, we don't try to get any attributes
2968 * along with any of the zero-copy operations READ, READDIR,
2971 * In the case of the first three, we want to put the GETATTR
2972 * after the read-type operation -- this is because it is hard
2973 * to predict the length of a GETATTR response in v4, and thus
2974 * align the READ data correctly. This means that the GETATTR
2975 * may end up partially falling into the page cache, and we should
2976 * shift it into the 'tail' of the xdr_buf before processing.
2977 * To do this efficiently, we need to know the total length
2978 * of data received, which doesn't seem to be available outside
2981 * In the case of WRITE, we also want to put the GETATTR after
2982 * the operation -- in this case because we want to make sure
2983 * we get the post-operation mtime and size.
2985 * Both of these changes to the XDR layer would in fact be quite
2986 * minor, but I decided to leave them for a subsequent patch.
2988 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2989 unsigned int pgbase, unsigned int pglen)
2991 struct nfs4_readlink args = {
2992 .fh = NFS_FH(inode),
2997 struct nfs4_readlink_res res;
2998 struct rpc_message msg = {
2999 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3004 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3007 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3008 unsigned int pgbase, unsigned int pglen)
3010 struct nfs4_exception exception = { };
3013 err = nfs4_handle_exception(NFS_SERVER(inode),
3014 _nfs4_proc_readlink(inode, page, pgbase, pglen),
3016 } while (exception.retry);
3021 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3024 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3027 struct nfs_open_context *ctx;
3028 struct nfs4_state *state;
3031 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3033 return PTR_ERR(ctx);
3035 sattr->ia_mode &= ~current_umask();
3036 state = nfs4_do_open(dir, dentry, ctx->mode,
3037 flags, sattr, ctx->cred,
3038 &ctx->mdsthreshold);
3040 if (IS_ERR(state)) {
3041 status = PTR_ERR(state);
3044 d_add(dentry, igrab(state->inode));
3045 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
3048 put_nfs_open_context(ctx);
3052 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3054 struct nfs_server *server = NFS_SERVER(dir);
3055 struct nfs_removeargs args = {
3059 struct nfs_removeres res = {
3062 struct rpc_message msg = {
3063 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3069 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3071 update_changeattr(dir, &res.cinfo);
3075 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3077 struct nfs4_exception exception = { };
3080 err = nfs4_handle_exception(NFS_SERVER(dir),
3081 _nfs4_proc_remove(dir, name),
3083 } while (exception.retry);
3087 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3089 struct nfs_server *server = NFS_SERVER(dir);
3090 struct nfs_removeargs *args = msg->rpc_argp;
3091 struct nfs_removeres *res = msg->rpc_resp;
3093 res->server = server;
3094 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3095 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
3098 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3100 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
3101 &data->args.seq_args,
3105 rpc_call_start(task);
3108 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3110 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3112 if (!nfs4_sequence_done(task, &res->seq_res))
3114 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3116 update_changeattr(dir, &res->cinfo);
3120 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3122 struct nfs_server *server = NFS_SERVER(dir);
3123 struct nfs_renameargs *arg = msg->rpc_argp;
3124 struct nfs_renameres *res = msg->rpc_resp;
3126 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3127 res->server = server;
3128 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
3131 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3133 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3134 &data->args.seq_args,
3138 rpc_call_start(task);
3141 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3142 struct inode *new_dir)
3144 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3146 if (!nfs4_sequence_done(task, &res->seq_res))
3148 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3151 update_changeattr(old_dir, &res->old_cinfo);
3152 update_changeattr(new_dir, &res->new_cinfo);
3156 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3157 struct inode *new_dir, struct qstr *new_name)
3159 struct nfs_server *server = NFS_SERVER(old_dir);
3160 struct nfs_renameargs arg = {
3161 .old_dir = NFS_FH(old_dir),
3162 .new_dir = NFS_FH(new_dir),
3163 .old_name = old_name,
3164 .new_name = new_name,
3166 struct nfs_renameres res = {
3169 struct rpc_message msg = {
3170 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3174 int status = -ENOMEM;
3176 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3178 update_changeattr(old_dir, &res.old_cinfo);
3179 update_changeattr(new_dir, &res.new_cinfo);
3184 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3185 struct inode *new_dir, struct qstr *new_name)
3187 struct nfs4_exception exception = { };
3190 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3191 _nfs4_proc_rename(old_dir, old_name,
3194 } while (exception.retry);
3198 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3200 struct nfs_server *server = NFS_SERVER(inode);
3201 struct nfs4_link_arg arg = {
3202 .fh = NFS_FH(inode),
3203 .dir_fh = NFS_FH(dir),
3205 .bitmask = server->attr_bitmask,
3207 struct nfs4_link_res res = {
3210 struct rpc_message msg = {
3211 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3215 int status = -ENOMEM;
3217 res.fattr = nfs_alloc_fattr();
3218 if (res.fattr == NULL)
3221 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3223 update_changeattr(dir, &res.cinfo);
3224 nfs_post_op_update_inode(inode, res.fattr);
3227 nfs_free_fattr(res.fattr);
3231 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3233 struct nfs4_exception exception = { };
3236 err = nfs4_handle_exception(NFS_SERVER(inode),
3237 _nfs4_proc_link(inode, dir, name),
3239 } while (exception.retry);
3243 struct nfs4_createdata {
3244 struct rpc_message msg;
3245 struct nfs4_create_arg arg;
3246 struct nfs4_create_res res;
3248 struct nfs_fattr fattr;
3251 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3252 struct qstr *name, struct iattr *sattr, u32 ftype)
3254 struct nfs4_createdata *data;
3256 data = kzalloc(sizeof(*data), GFP_KERNEL);
3258 struct nfs_server *server = NFS_SERVER(dir);
3260 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3261 data->msg.rpc_argp = &data->arg;
3262 data->msg.rpc_resp = &data->res;
3263 data->arg.dir_fh = NFS_FH(dir);
3264 data->arg.server = server;
3265 data->arg.name = name;
3266 data->arg.attrs = sattr;
3267 data->arg.ftype = ftype;
3268 data->arg.bitmask = server->attr_bitmask;
3269 data->res.server = server;
3270 data->res.fh = &data->fh;
3271 data->res.fattr = &data->fattr;
3272 nfs_fattr_init(data->res.fattr);
3277 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3279 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3280 &data->arg.seq_args, &data->res.seq_res, 1);
3282 update_changeattr(dir, &data->res.dir_cinfo);
3283 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3288 static void nfs4_free_createdata(struct nfs4_createdata *data)
3293 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3294 struct page *page, unsigned int len, struct iattr *sattr)
3296 struct nfs4_createdata *data;
3297 int status = -ENAMETOOLONG;
3299 if (len > NFS4_MAXPATHLEN)
3303 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3307 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3308 data->arg.u.symlink.pages = &page;
3309 data->arg.u.symlink.len = len;
3311 status = nfs4_do_create(dir, dentry, data);
3313 nfs4_free_createdata(data);
3318 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3319 struct page *page, unsigned int len, struct iattr *sattr)
3321 struct nfs4_exception exception = { };
3324 err = nfs4_handle_exception(NFS_SERVER(dir),
3325 _nfs4_proc_symlink(dir, dentry, page,
3328 } while (exception.retry);
3332 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3333 struct iattr *sattr)
3335 struct nfs4_createdata *data;
3336 int status = -ENOMEM;
3338 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3342 status = nfs4_do_create(dir, dentry, data);
3344 nfs4_free_createdata(data);
3349 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3350 struct iattr *sattr)
3352 struct nfs4_exception exception = { };
3355 sattr->ia_mode &= ~current_umask();
3357 err = nfs4_handle_exception(NFS_SERVER(dir),
3358 _nfs4_proc_mkdir(dir, dentry, sattr),
3360 } while (exception.retry);
3364 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3365 u64 cookie, struct page **pages, unsigned int count, int plus)
3367 struct inode *dir = dentry->d_inode;
3368 struct nfs4_readdir_arg args = {
3373 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3376 struct nfs4_readdir_res res;
3377 struct rpc_message msg = {
3378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3385 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3386 dentry->d_parent->d_name.name,
3387 dentry->d_name.name,
3388 (unsigned long long)cookie);
3389 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3390 res.pgbase = args.pgbase;
3391 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3393 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3394 status += args.pgbase;
3397 nfs_invalidate_atime(dir);
3399 dprintk("%s: returns %d\n", __func__, status);
3403 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3404 u64 cookie, struct page **pages, unsigned int count, int plus)
3406 struct nfs4_exception exception = { };
3409 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3410 _nfs4_proc_readdir(dentry, cred, cookie,
3411 pages, count, plus),
3413 } while (exception.retry);
3417 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3418 struct iattr *sattr, dev_t rdev)
3420 struct nfs4_createdata *data;
3421 int mode = sattr->ia_mode;
3422 int status = -ENOMEM;
3424 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3429 data->arg.ftype = NF4FIFO;
3430 else if (S_ISBLK(mode)) {
3431 data->arg.ftype = NF4BLK;
3432 data->arg.u.device.specdata1 = MAJOR(rdev);
3433 data->arg.u.device.specdata2 = MINOR(rdev);
3435 else if (S_ISCHR(mode)) {
3436 data->arg.ftype = NF4CHR;
3437 data->arg.u.device.specdata1 = MAJOR(rdev);
3438 data->arg.u.device.specdata2 = MINOR(rdev);
3439 } else if (!S_ISSOCK(mode)) {
3444 status = nfs4_do_create(dir, dentry, data);
3446 nfs4_free_createdata(data);
3451 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3452 struct iattr *sattr, dev_t rdev)
3454 struct nfs4_exception exception = { };
3457 sattr->ia_mode &= ~current_umask();
3459 err = nfs4_handle_exception(NFS_SERVER(dir),
3460 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3462 } while (exception.retry);
3466 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3467 struct nfs_fsstat *fsstat)
3469 struct nfs4_statfs_arg args = {
3471 .bitmask = server->attr_bitmask,
3473 struct nfs4_statfs_res res = {
3476 struct rpc_message msg = {
3477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3482 nfs_fattr_init(fsstat->fattr);
3483 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3486 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3488 struct nfs4_exception exception = { };
3491 err = nfs4_handle_exception(server,
3492 _nfs4_proc_statfs(server, fhandle, fsstat),
3494 } while (exception.retry);
3498 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3499 struct nfs_fsinfo *fsinfo)
3501 struct nfs4_fsinfo_arg args = {
3503 .bitmask = server->attr_bitmask,
3505 struct nfs4_fsinfo_res res = {
3508 struct rpc_message msg = {
3509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3514 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3517 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3519 struct nfs4_exception exception = { };
3523 err = nfs4_handle_exception(server,
3524 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3526 } while (exception.retry);
3530 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3534 nfs_fattr_init(fsinfo->fattr);
3535 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3537 /* block layout checks this! */
3538 server->pnfs_blksize = fsinfo->blksize;
3539 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3545 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3546 struct nfs_pathconf *pathconf)
3548 struct nfs4_pathconf_arg args = {
3550 .bitmask = server->attr_bitmask,
3552 struct nfs4_pathconf_res res = {
3553 .pathconf = pathconf,
3555 struct rpc_message msg = {
3556 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3561 /* None of the pathconf attributes are mandatory to implement */
3562 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3563 memset(pathconf, 0, sizeof(*pathconf));
3567 nfs_fattr_init(pathconf->fattr);
3568 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3571 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3572 struct nfs_pathconf *pathconf)
3574 struct nfs4_exception exception = { };
3578 err = nfs4_handle_exception(server,
3579 _nfs4_proc_pathconf(server, fhandle, pathconf),
3581 } while (exception.retry);
3585 void __nfs4_read_done_cb(struct nfs_read_data *data)
3587 nfs_invalidate_atime(data->header->inode);
3590 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3592 struct nfs_server *server = NFS_SERVER(data->header->inode);
3594 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3595 rpc_restart_call_prepare(task);
3599 __nfs4_read_done_cb(data);
3600 if (task->tk_status > 0)
3601 renew_lease(server, data->timestamp);
3605 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3608 dprintk("--> %s\n", __func__);
3610 if (!nfs4_sequence_done(task, &data->res.seq_res))
3613 return data->read_done_cb ? data->read_done_cb(task, data) :
3614 nfs4_read_done_cb(task, data);
3617 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3619 data->timestamp = jiffies;
3620 data->read_done_cb = nfs4_read_done_cb;
3621 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3622 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3625 static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3627 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3628 &data->args.seq_args,
3632 rpc_call_start(task);
3635 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3637 struct inode *inode = data->header->inode;
3639 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3640 rpc_restart_call_prepare(task);
3643 if (task->tk_status >= 0) {
3644 renew_lease(NFS_SERVER(inode), data->timestamp);
3645 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
3650 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3652 if (!nfs4_sequence_done(task, &data->res.seq_res))
3654 return data->write_done_cb ? data->write_done_cb(task, data) :
3655 nfs4_write_done_cb(task, data);
3659 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3661 const struct nfs_pgio_header *hdr = data->header;
3663 /* Don't request attributes for pNFS or O_DIRECT writes */
3664 if (data->ds_clp != NULL || hdr->dreq != NULL)
3666 /* Otherwise, request attributes if and only if we don't hold
3669 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
3672 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3674 struct nfs_server *server = NFS_SERVER(data->header->inode);
3676 if (!nfs4_write_need_cache_consistency_data(data)) {
3677 data->args.bitmask = NULL;
3678 data->res.fattr = NULL;
3680 data->args.bitmask = server->cache_consistency_bitmask;
3682 if (!data->write_done_cb)
3683 data->write_done_cb = nfs4_write_done_cb;
3684 data->res.server = server;
3685 data->timestamp = jiffies;
3687 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3688 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3691 static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3693 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3694 &data->args.seq_args,
3698 rpc_call_start(task);
3701 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3703 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3704 &data->args.seq_args,
3708 rpc_call_start(task);
3711 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
3713 struct inode *inode = data->inode;
3715 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3716 rpc_restart_call_prepare(task);
3722 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
3724 if (!nfs4_sequence_done(task, &data->res.seq_res))
3726 return data->commit_done_cb(task, data);
3729 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
3731 struct nfs_server *server = NFS_SERVER(data->inode);
3733 if (data->commit_done_cb == NULL)
3734 data->commit_done_cb = nfs4_commit_done_cb;
3735 data->res.server = server;
3736 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3737 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3740 struct nfs4_renewdata {
3741 struct nfs_client *client;
3742 unsigned long timestamp;
3746 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3747 * standalone procedure for queueing an asynchronous RENEW.
3749 static void nfs4_renew_release(void *calldata)
3751 struct nfs4_renewdata *data = calldata;
3752 struct nfs_client *clp = data->client;
3754 if (atomic_read(&clp->cl_count) > 1)
3755 nfs4_schedule_state_renewal(clp);
3756 nfs_put_client(clp);
3760 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3762 struct nfs4_renewdata *data = calldata;
3763 struct nfs_client *clp = data->client;
3764 unsigned long timestamp = data->timestamp;
3766 if (task->tk_status < 0) {
3767 /* Unless we're shutting down, schedule state recovery! */
3768 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3770 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3771 nfs4_schedule_lease_recovery(clp);
3774 nfs4_schedule_path_down_recovery(clp);
3776 do_renew_lease(clp, timestamp);
3779 static const struct rpc_call_ops nfs4_renew_ops = {
3780 .rpc_call_done = nfs4_renew_done,
3781 .rpc_release = nfs4_renew_release,
3784 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3786 struct rpc_message msg = {
3787 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3791 struct nfs4_renewdata *data;
3793 if (renew_flags == 0)
3795 if (!atomic_inc_not_zero(&clp->cl_count))
3797 data = kmalloc(sizeof(*data), GFP_NOFS);
3801 data->timestamp = jiffies;
3802 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3803 &nfs4_renew_ops, data);
3806 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3808 struct rpc_message msg = {
3809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3813 unsigned long now = jiffies;
3816 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3819 do_renew_lease(clp, now);
3823 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3825 return (server->caps & NFS_CAP_ACLS)
3826 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3827 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3830 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3831 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
3834 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
3836 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3837 struct page **pages, unsigned int *pgbase)
3839 struct page *newpage, **spages;
3845 len = min_t(size_t, PAGE_SIZE, buflen);
3846 newpage = alloc_page(GFP_KERNEL);
3848 if (newpage == NULL)
3850 memcpy(page_address(newpage), buf, len);
3855 } while (buflen != 0);
3861 __free_page(spages[rc-1]);
3865 struct nfs4_cached_acl {
3871 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3873 struct nfs_inode *nfsi = NFS_I(inode);
3875 spin_lock(&inode->i_lock);
3876 kfree(nfsi->nfs4_acl);
3877 nfsi->nfs4_acl = acl;
3878 spin_unlock(&inode->i_lock);
3881 static void nfs4_zap_acl_attr(struct inode *inode)
3883 nfs4_set_cached_acl(inode, NULL);
3886 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3888 struct nfs_inode *nfsi = NFS_I(inode);
3889 struct nfs4_cached_acl *acl;
3892 spin_lock(&inode->i_lock);
3893 acl = nfsi->nfs4_acl;
3896 if (buf == NULL) /* user is just asking for length */
3898 if (acl->cached == 0)
3900 ret = -ERANGE; /* see getxattr(2) man page */
3901 if (acl->len > buflen)
3903 memcpy(buf, acl->data, acl->len);
3907 spin_unlock(&inode->i_lock);
3911 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3913 struct nfs4_cached_acl *acl;
3914 size_t buflen = sizeof(*acl) + acl_len;
3916 if (buflen <= PAGE_SIZE) {
3917 acl = kmalloc(buflen, GFP_KERNEL);
3921 _copy_from_pages(acl->data, pages, pgbase, acl_len);
3923 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3930 nfs4_set_cached_acl(inode, acl);
3934 * The getxattr API returns the required buffer length when called with a
3935 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3936 * the required buf. On a NULL buf, we send a page of data to the server
3937 * guessing that the ACL request can be serviced by a page. If so, we cache
3938 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3939 * the cache. If not so, we throw away the page, and cache the required
3940 * length. The next getxattr call will then produce another round trip to
3941 * the server, this time with the input buf of the required size.
3943 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3945 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3946 struct nfs_getaclargs args = {
3947 .fh = NFS_FH(inode),
3951 struct nfs_getaclres res = {
3954 struct rpc_message msg = {
3955 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3959 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3960 int ret = -ENOMEM, i;
3962 /* As long as we're doing a round trip to the server anyway,
3963 * let's be prepared for a page of acl data. */
3966 if (npages > ARRAY_SIZE(pages))
3969 for (i = 0; i < npages; i++) {
3970 pages[i] = alloc_page(GFP_KERNEL);
3975 /* for decoding across pages */
3976 res.acl_scratch = alloc_page(GFP_KERNEL);
3977 if (!res.acl_scratch)
3980 args.acl_len = npages * PAGE_SIZE;
3981 args.acl_pgbase = 0;
3983 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3984 __func__, buf, buflen, npages, args.acl_len);
3985 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3986 &msg, &args.seq_args, &res.seq_res, 0);
3990 /* Handle the case where the passed-in buffer is too short */
3991 if (res.acl_flags & NFS4_ACL_TRUNC) {
3992 /* Did the user only issue a request for the acl length? */
3998 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
4000 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
4004 for (i = 0; i < npages; i++)
4006 __free_page(pages[i]);
4007 if (res.acl_scratch)
4008 __free_page(res.acl_scratch);
4012 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4014 struct nfs4_exception exception = { };
4017 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4020 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4021 } while (exception.retry);
4025 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4027 struct nfs_server *server = NFS_SERVER(inode);
4030 if (!nfs4_server_supports_acls(server))
4032 ret = nfs_revalidate_inode(server, inode);
4035 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4036 nfs_zap_acl_cache(inode);
4037 ret = nfs4_read_cached_acl(inode, buf, buflen);
4039 /* -ENOENT is returned if there is no ACL or if there is an ACL
4040 * but no cached acl data, just the acl length */
4042 return nfs4_get_acl_uncached(inode, buf, buflen);
4045 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4047 struct nfs_server *server = NFS_SERVER(inode);
4048 struct page *pages[NFS4ACL_MAXPAGES];
4049 struct nfs_setaclargs arg = {
4050 .fh = NFS_FH(inode),
4054 struct nfs_setaclres res;
4055 struct rpc_message msg = {
4056 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4060 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4063 if (!nfs4_server_supports_acls(server))
4065 if (npages > ARRAY_SIZE(pages))
4067 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4070 nfs4_inode_return_delegation(inode);
4071 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4074 * Free each page after tx, so the only ref left is
4075 * held by the network stack
4078 put_page(pages[i-1]);
4081 * Acl update can result in inode attribute update.
4082 * so mark the attribute cache invalid.
4084 spin_lock(&inode->i_lock);
4085 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4086 spin_unlock(&inode->i_lock);
4087 nfs_access_zap_cache(inode);
4088 nfs_zap_acl_cache(inode);
4092 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4094 struct nfs4_exception exception = { };
4097 err = nfs4_handle_exception(NFS_SERVER(inode),
4098 __nfs4_proc_set_acl(inode, buf, buflen),
4100 } while (exception.retry);
4105 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
4107 struct nfs_client *clp = server->nfs_client;
4109 if (task->tk_status >= 0)
4111 switch(task->tk_status) {
4112 case -NFS4ERR_DELEG_REVOKED:
4113 case -NFS4ERR_ADMIN_REVOKED:
4114 case -NFS4ERR_BAD_STATEID:
4117 nfs_remove_bad_delegation(state->inode);
4118 case -NFS4ERR_OPENMODE:
4121 nfs4_schedule_stateid_recovery(server, state);
4122 goto wait_on_recovery;
4123 case -NFS4ERR_EXPIRED:
4125 nfs4_schedule_stateid_recovery(server, state);
4126 case -NFS4ERR_STALE_STATEID:
4127 case -NFS4ERR_STALE_CLIENTID:
4128 nfs4_schedule_lease_recovery(clp);
4129 goto wait_on_recovery;
4130 #if defined(CONFIG_NFS_V4_1)
4131 case -NFS4ERR_BADSESSION:
4132 case -NFS4ERR_BADSLOT:
4133 case -NFS4ERR_BAD_HIGH_SLOT:
4134 case -NFS4ERR_DEADSESSION:
4135 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4136 case -NFS4ERR_SEQ_FALSE_RETRY:
4137 case -NFS4ERR_SEQ_MISORDERED:
4138 dprintk("%s ERROR %d, Reset session\n", __func__,
4140 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4141 task->tk_status = 0;
4143 #endif /* CONFIG_NFS_V4_1 */
4144 case -NFS4ERR_DELAY:
4145 nfs_inc_server_stats(server, NFSIOS_DELAY);
4146 case -NFS4ERR_GRACE:
4148 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4149 task->tk_status = 0;
4151 case -NFS4ERR_RETRY_UNCACHED_REP:
4152 case -NFS4ERR_OLD_STATEID:
4153 task->tk_status = 0;
4156 task->tk_status = nfs4_map_errors(task->tk_status);
4159 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4160 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4161 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4162 task->tk_status = 0;
4166 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4167 nfs4_verifier *bootverf)
4171 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4172 /* An impossible timestamp guarantees this value
4173 * will never match a generated boot time. */
4175 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4177 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4178 verf[0] = (__be32)nn->boot_time.tv_sec;
4179 verf[1] = (__be32)nn->boot_time.tv_nsec;
4181 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4185 nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4186 char *buf, size_t len)
4188 unsigned int result;
4191 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4193 rpc_peeraddr2str(clp->cl_rpcclient,
4195 rpc_peeraddr2str(clp->cl_rpcclient,
4196 RPC_DISPLAY_PROTO));
4202 nfs4_init_uniform_client_string(const struct nfs_client *clp,
4203 char *buf, size_t len)
4205 char *nodename = clp->cl_rpcclient->cl_nodename;
4207 if (nfs4_client_id_uniquifier[0] != '\0')
4208 nodename = nfs4_client_id_uniquifier;
4209 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4210 clp->rpc_ops->version, clp->cl_minorversion,
4215 * nfs4_proc_setclientid - Negotiate client ID
4216 * @clp: state data structure
4217 * @program: RPC program for NFSv4 callback service
4218 * @port: IP port number for NFS4 callback service
4219 * @cred: RPC credential to use for this call
4220 * @res: where to place the result
4222 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4224 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4225 unsigned short port, struct rpc_cred *cred,
4226 struct nfs4_setclientid_res *res)
4228 nfs4_verifier sc_verifier;
4229 struct nfs4_setclientid setclientid = {
4230 .sc_verifier = &sc_verifier,
4232 .sc_cb_ident = clp->cl_cb_ident,
4234 struct rpc_message msg = {
4235 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4236 .rpc_argp = &setclientid,
4242 /* nfs_client_id4 */
4243 nfs4_init_boot_verifier(clp, &sc_verifier);
4244 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4245 setclientid.sc_name_len =
4246 nfs4_init_uniform_client_string(clp,
4247 setclientid.sc_name,
4248 sizeof(setclientid.sc_name));
4250 setclientid.sc_name_len =
4251 nfs4_init_nonuniform_client_string(clp,
4252 setclientid.sc_name,
4253 sizeof(setclientid.sc_name));
4256 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4257 sizeof(setclientid.sc_netid),
4258 rpc_peeraddr2str(clp->cl_rpcclient,
4259 RPC_DISPLAY_NETID));
4261 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4262 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4263 clp->cl_ipaddr, port >> 8, port & 255);
4265 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4266 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4267 setclientid.sc_name_len, setclientid.sc_name);
4268 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4269 dprintk("NFS reply setclientid: %d\n", status);
4274 * nfs4_proc_setclientid_confirm - Confirm client ID
4275 * @clp: state data structure
4276 * @res: result of a previous SETCLIENTID
4277 * @cred: RPC credential to use for this call
4279 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4281 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4282 struct nfs4_setclientid_res *arg,
4283 struct rpc_cred *cred)
4285 struct nfs_fsinfo fsinfo;
4286 struct rpc_message msg = {
4287 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4289 .rpc_resp = &fsinfo,
4295 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4296 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4299 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4301 spin_lock(&clp->cl_lock);
4302 clp->cl_lease_time = fsinfo.lease_time * HZ;
4303 clp->cl_last_renewal = now;
4304 spin_unlock(&clp->cl_lock);
4306 dprintk("NFS reply setclientid_confirm: %d\n", status);
4310 struct nfs4_delegreturndata {
4311 struct nfs4_delegreturnargs args;
4312 struct nfs4_delegreturnres res;
4314 nfs4_stateid stateid;
4315 unsigned long timestamp;
4316 struct nfs_fattr fattr;
4320 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4322 struct nfs4_delegreturndata *data = calldata;
4324 if (!nfs4_sequence_done(task, &data->res.seq_res))
4327 switch (task->tk_status) {
4328 case -NFS4ERR_STALE_STATEID:
4329 case -NFS4ERR_EXPIRED:
4331 renew_lease(data->res.server, data->timestamp);
4334 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4336 rpc_restart_call_prepare(task);
4340 data->rpc_status = task->tk_status;
4343 static void nfs4_delegreturn_release(void *calldata)
4348 #if defined(CONFIG_NFS_V4_1)
4349 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4351 struct nfs4_delegreturndata *d_data;
4353 d_data = (struct nfs4_delegreturndata *)data;
4355 if (nfs4_setup_sequence(d_data->res.server,
4356 &d_data->args.seq_args,
4357 &d_data->res.seq_res, task))
4359 rpc_call_start(task);
4361 #endif /* CONFIG_NFS_V4_1 */
4363 static const struct rpc_call_ops nfs4_delegreturn_ops = {
4364 #if defined(CONFIG_NFS_V4_1)
4365 .rpc_call_prepare = nfs4_delegreturn_prepare,
4366 #endif /* CONFIG_NFS_V4_1 */
4367 .rpc_call_done = nfs4_delegreturn_done,
4368 .rpc_release = nfs4_delegreturn_release,
4371 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4373 struct nfs4_delegreturndata *data;
4374 struct nfs_server *server = NFS_SERVER(inode);
4375 struct rpc_task *task;
4376 struct rpc_message msg = {
4377 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4380 struct rpc_task_setup task_setup_data = {
4381 .rpc_client = server->client,
4382 .rpc_message = &msg,
4383 .callback_ops = &nfs4_delegreturn_ops,
4384 .flags = RPC_TASK_ASYNC,
4388 data = kzalloc(sizeof(*data), GFP_NOFS);
4391 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4392 data->args.fhandle = &data->fh;
4393 data->args.stateid = &data->stateid;
4394 data->args.bitmask = server->cache_consistency_bitmask;
4395 nfs_copy_fh(&data->fh, NFS_FH(inode));
4396 nfs4_stateid_copy(&data->stateid, stateid);
4397 data->res.fattr = &data->fattr;
4398 data->res.server = server;
4399 nfs_fattr_init(data->res.fattr);
4400 data->timestamp = jiffies;
4401 data->rpc_status = 0;
4403 task_setup_data.callback_data = data;
4404 msg.rpc_argp = &data->args;
4405 msg.rpc_resp = &data->res;
4406 task = rpc_run_task(&task_setup_data);
4408 return PTR_ERR(task);
4411 status = nfs4_wait_for_completion_rpc_task(task);
4414 status = data->rpc_status;
4416 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4418 nfs_refresh_inode(inode, &data->fattr);
4424 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4426 struct nfs_server *server = NFS_SERVER(inode);
4427 struct nfs4_exception exception = { };
4430 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4432 case -NFS4ERR_STALE_STATEID:
4433 case -NFS4ERR_EXPIRED:
4437 err = nfs4_handle_exception(server, err, &exception);
4438 } while (exception.retry);
4442 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4443 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4446 * sleep, with exponential backoff, and retry the LOCK operation.
4448 static unsigned long
4449 nfs4_set_lock_task_retry(unsigned long timeout)
4451 freezable_schedule_timeout_killable(timeout);
4453 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4454 return NFS4_LOCK_MAXTIMEOUT;
4458 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4460 struct inode *inode = state->inode;
4461 struct nfs_server *server = NFS_SERVER(inode);
4462 struct nfs_client *clp = server->nfs_client;
4463 struct nfs_lockt_args arg = {
4464 .fh = NFS_FH(inode),
4467 struct nfs_lockt_res res = {
4470 struct rpc_message msg = {
4471 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4474 .rpc_cred = state->owner->so_cred,
4476 struct nfs4_lock_state *lsp;
4479 arg.lock_owner.clientid = clp->cl_clientid;
4480 status = nfs4_set_lock_state(state, request);
4483 lsp = request->fl_u.nfs4_fl.owner;
4484 arg.lock_owner.id = lsp->ls_seqid.owner_id;
4485 arg.lock_owner.s_dev = server->s_dev;
4486 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4489 request->fl_type = F_UNLCK;
4491 case -NFS4ERR_DENIED:
4494 request->fl_ops->fl_release_private(request);
4499 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4501 struct nfs4_exception exception = { };
4505 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4506 _nfs4_proc_getlk(state, cmd, request),
4508 } while (exception.retry);
4512 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4515 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4517 res = posix_lock_file_wait(file, fl);
4520 res = flock_lock_file_wait(file, fl);
4528 struct nfs4_unlockdata {
4529 struct nfs_locku_args arg;
4530 struct nfs_locku_res res;
4531 struct nfs4_lock_state *lsp;
4532 struct nfs_open_context *ctx;
4533 struct file_lock fl;
4534 const struct nfs_server *server;
4535 unsigned long timestamp;
4538 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4539 struct nfs_open_context *ctx,
4540 struct nfs4_lock_state *lsp,
4541 struct nfs_seqid *seqid)
4543 struct nfs4_unlockdata *p;
4544 struct inode *inode = lsp->ls_state->inode;
4546 p = kzalloc(sizeof(*p), GFP_NOFS);
4549 p->arg.fh = NFS_FH(inode);
4551 p->arg.seqid = seqid;
4552 p->res.seqid = seqid;
4553 p->arg.stateid = &lsp->ls_stateid;
4555 atomic_inc(&lsp->ls_count);
4556 /* Ensure we don't close file until we're done freeing locks! */
4557 p->ctx = get_nfs_open_context(ctx);
4558 memcpy(&p->fl, fl, sizeof(p->fl));
4559 p->server = NFS_SERVER(inode);
4563 static void nfs4_locku_release_calldata(void *data)
4565 struct nfs4_unlockdata *calldata = data;
4566 nfs_free_seqid(calldata->arg.seqid);
4567 nfs4_put_lock_state(calldata->lsp);
4568 put_nfs_open_context(calldata->ctx);
4572 static void nfs4_locku_done(struct rpc_task *task, void *data)
4574 struct nfs4_unlockdata *calldata = data;
4576 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4578 switch (task->tk_status) {
4580 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4581 &calldata->res.stateid);
4582 renew_lease(calldata->server, calldata->timestamp);
4584 case -NFS4ERR_BAD_STATEID:
4585 case -NFS4ERR_OLD_STATEID:
4586 case -NFS4ERR_STALE_STATEID:
4587 case -NFS4ERR_EXPIRED:
4590 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4591 rpc_restart_call_prepare(task);
4593 nfs_release_seqid(calldata->arg.seqid);
4596 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4598 struct nfs4_unlockdata *calldata = data;
4600 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4602 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
4603 /* Note: exit _without_ running nfs4_locku_done */
4604 task->tk_action = NULL;
4607 calldata->timestamp = jiffies;
4608 if (nfs4_setup_sequence(calldata->server,
4609 &calldata->arg.seq_args,
4610 &calldata->res.seq_res,
4612 nfs_release_seqid(calldata->arg.seqid);
4614 rpc_call_start(task);
4617 static const struct rpc_call_ops nfs4_locku_ops = {
4618 .rpc_call_prepare = nfs4_locku_prepare,
4619 .rpc_call_done = nfs4_locku_done,
4620 .rpc_release = nfs4_locku_release_calldata,
4623 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4624 struct nfs_open_context *ctx,
4625 struct nfs4_lock_state *lsp,
4626 struct nfs_seqid *seqid)
4628 struct nfs4_unlockdata *data;
4629 struct rpc_message msg = {
4630 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4631 .rpc_cred = ctx->cred,
4633 struct rpc_task_setup task_setup_data = {
4634 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4635 .rpc_message = &msg,
4636 .callback_ops = &nfs4_locku_ops,
4637 .workqueue = nfsiod_workqueue,
4638 .flags = RPC_TASK_ASYNC,
4641 /* Ensure this is an unlock - when canceling a lock, the
4642 * canceled lock is passed in, and it won't be an unlock.
4644 fl->fl_type = F_UNLCK;
4646 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4648 nfs_free_seqid(seqid);
4649 return ERR_PTR(-ENOMEM);
4652 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4653 msg.rpc_argp = &data->arg;
4654 msg.rpc_resp = &data->res;
4655 task_setup_data.callback_data = data;
4656 return rpc_run_task(&task_setup_data);
4659 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4661 struct nfs_inode *nfsi = NFS_I(state->inode);
4662 struct nfs_seqid *seqid;
4663 struct nfs4_lock_state *lsp;
4664 struct rpc_task *task;
4666 unsigned char fl_flags = request->fl_flags;
4668 status = nfs4_set_lock_state(state, request);
4669 /* Unlock _before_ we do the RPC call */
4670 request->fl_flags |= FL_EXISTS;
4671 down_read(&nfsi->rwsem);
4672 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4673 up_read(&nfsi->rwsem);
4676 up_read(&nfsi->rwsem);
4679 /* Is this a delegated lock? */
4680 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4682 lsp = request->fl_u.nfs4_fl.owner;
4683 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4687 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4688 status = PTR_ERR(task);
4691 status = nfs4_wait_for_completion_rpc_task(task);
4694 request->fl_flags = fl_flags;
4698 struct nfs4_lockdata {
4699 struct nfs_lock_args arg;
4700 struct nfs_lock_res res;
4701 struct nfs4_lock_state *lsp;
4702 struct nfs_open_context *ctx;
4703 struct file_lock fl;
4704 unsigned long timestamp;
4707 struct nfs_server *server;
4710 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4711 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4714 struct nfs4_lockdata *p;
4715 struct inode *inode = lsp->ls_state->inode;
4716 struct nfs_server *server = NFS_SERVER(inode);
4718 p = kzalloc(sizeof(*p), gfp_mask);
4722 p->arg.fh = NFS_FH(inode);
4724 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4725 if (p->arg.open_seqid == NULL)
4727 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4728 if (p->arg.lock_seqid == NULL)
4729 goto out_free_seqid;
4730 p->arg.lock_stateid = &lsp->ls_stateid;
4731 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4732 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4733 p->arg.lock_owner.s_dev = server->s_dev;
4734 p->res.lock_seqid = p->arg.lock_seqid;
4737 atomic_inc(&lsp->ls_count);
4738 p->ctx = get_nfs_open_context(ctx);
4739 memcpy(&p->fl, fl, sizeof(p->fl));
4742 nfs_free_seqid(p->arg.open_seqid);
4748 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4750 struct nfs4_lockdata *data = calldata;
4751 struct nfs4_state *state = data->lsp->ls_state;
4753 dprintk("%s: begin!\n", __func__);
4754 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4756 /* Do we need to do an open_to_lock_owner? */
4757 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4758 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4759 goto out_release_lock_seqid;
4760 data->arg.open_stateid = &state->stateid;
4761 data->arg.new_lock_owner = 1;
4762 data->res.open_seqid = data->arg.open_seqid;
4764 data->arg.new_lock_owner = 0;
4765 data->timestamp = jiffies;
4766 if (nfs4_setup_sequence(data->server,
4767 &data->arg.seq_args,
4770 rpc_call_start(task);
4773 nfs_release_seqid(data->arg.open_seqid);
4774 out_release_lock_seqid:
4775 nfs_release_seqid(data->arg.lock_seqid);
4776 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
4779 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4781 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4782 nfs4_lock_prepare(task, calldata);
4785 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4787 struct nfs4_lockdata *data = calldata;
4789 dprintk("%s: begin!\n", __func__);
4791 if (!nfs4_sequence_done(task, &data->res.seq_res))
4794 data->rpc_status = task->tk_status;
4795 if (data->arg.new_lock_owner != 0) {
4796 if (data->rpc_status == 0)
4797 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4801 if (data->rpc_status == 0) {
4802 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4803 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
4804 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4807 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4810 static void nfs4_lock_release(void *calldata)
4812 struct nfs4_lockdata *data = calldata;
4814 dprintk("%s: begin!\n", __func__);
4815 nfs_free_seqid(data->arg.open_seqid);
4816 if (data->cancelled != 0) {
4817 struct rpc_task *task;
4818 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4819 data->arg.lock_seqid);
4821 rpc_put_task_async(task);
4822 dprintk("%s: cancelling lock!\n", __func__);
4824 nfs_free_seqid(data->arg.lock_seqid);
4825 nfs4_put_lock_state(data->lsp);
4826 put_nfs_open_context(data->ctx);
4828 dprintk("%s: done!\n", __func__);
4831 static const struct rpc_call_ops nfs4_lock_ops = {
4832 .rpc_call_prepare = nfs4_lock_prepare,
4833 .rpc_call_done = nfs4_lock_done,
4834 .rpc_release = nfs4_lock_release,
4837 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4838 .rpc_call_prepare = nfs4_recover_lock_prepare,
4839 .rpc_call_done = nfs4_lock_done,
4840 .rpc_release = nfs4_lock_release,
4843 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4846 case -NFS4ERR_ADMIN_REVOKED:
4847 case -NFS4ERR_BAD_STATEID:
4848 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4849 if (new_lock_owner != 0 ||
4850 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
4851 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4853 case -NFS4ERR_STALE_STATEID:
4854 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4855 case -NFS4ERR_EXPIRED:
4856 nfs4_schedule_lease_recovery(server->nfs_client);
4860 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4862 struct nfs4_lockdata *data;
4863 struct rpc_task *task;
4864 struct rpc_message msg = {
4865 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4866 .rpc_cred = state->owner->so_cred,
4868 struct rpc_task_setup task_setup_data = {
4869 .rpc_client = NFS_CLIENT(state->inode),
4870 .rpc_message = &msg,
4871 .callback_ops = &nfs4_lock_ops,
4872 .workqueue = nfsiod_workqueue,
4873 .flags = RPC_TASK_ASYNC,
4877 dprintk("%s: begin!\n", __func__);
4878 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4879 fl->fl_u.nfs4_fl.owner,
4880 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4884 data->arg.block = 1;
4885 if (recovery_type > NFS_LOCK_NEW) {
4886 if (recovery_type == NFS_LOCK_RECLAIM)
4887 data->arg.reclaim = NFS_LOCK_RECLAIM;
4888 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4890 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4891 msg.rpc_argp = &data->arg;
4892 msg.rpc_resp = &data->res;
4893 task_setup_data.callback_data = data;
4894 task = rpc_run_task(&task_setup_data);
4896 return PTR_ERR(task);
4897 ret = nfs4_wait_for_completion_rpc_task(task);
4899 ret = data->rpc_status;
4901 nfs4_handle_setlk_error(data->server, data->lsp,
4902 data->arg.new_lock_owner, ret);
4904 data->cancelled = 1;
4906 dprintk("%s: done, ret = %d!\n", __func__, ret);
4910 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4912 struct nfs_server *server = NFS_SERVER(state->inode);
4913 struct nfs4_exception exception = {
4914 .inode = state->inode,
4919 /* Cache the lock if possible... */
4920 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4922 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4923 if (err != -NFS4ERR_DELAY)
4925 nfs4_handle_exception(server, err, &exception);
4926 } while (exception.retry);
4930 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4932 struct nfs_server *server = NFS_SERVER(state->inode);
4933 struct nfs4_exception exception = {
4934 .inode = state->inode,
4938 err = nfs4_set_lock_state(state, request);
4942 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4944 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4948 case -NFS4ERR_GRACE:
4949 case -NFS4ERR_DELAY:
4950 nfs4_handle_exception(server, err, &exception);
4953 } while (exception.retry);
4958 #if defined(CONFIG_NFS_V4_1)
4960 * nfs41_check_expired_locks - possibly free a lock stateid
4962 * @state: NFSv4 state for an inode
4964 * Returns NFS_OK if recovery for this stateid is now finished.
4965 * Otherwise a negative NFS4ERR value is returned.
4967 static int nfs41_check_expired_locks(struct nfs4_state *state)
4969 int status, ret = -NFS4ERR_BAD_STATEID;
4970 struct nfs4_lock_state *lsp;
4971 struct nfs_server *server = NFS_SERVER(state->inode);
4973 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4974 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
4975 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4976 if (status != NFS_OK) {
4977 /* Free the stateid unless the server
4978 * informs us the stateid is unrecognized. */
4979 if (status != -NFS4ERR_BAD_STATEID)
4980 nfs41_free_stateid(server,
4982 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
4991 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4993 int status = NFS_OK;
4995 if (test_bit(LK_STATE_IN_USE, &state->flags))
4996 status = nfs41_check_expired_locks(state);
4997 if (status != NFS_OK)
4998 status = nfs4_lock_expired(state, request);
5003 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5005 struct nfs_inode *nfsi = NFS_I(state->inode);
5006 unsigned char fl_flags = request->fl_flags;
5007 int status = -ENOLCK;
5009 if ((fl_flags & FL_POSIX) &&
5010 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5012 /* Is this a delegated open? */
5013 status = nfs4_set_lock_state(state, request);
5016 request->fl_flags |= FL_ACCESS;
5017 status = do_vfs_lock(request->fl_file, request);
5020 down_read(&nfsi->rwsem);
5021 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
5022 /* Yes: cache locks! */
5023 /* ...but avoid races with delegation recall... */
5024 request->fl_flags = fl_flags & ~FL_SLEEP;
5025 status = do_vfs_lock(request->fl_file, request);
5028 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
5031 /* Note: we always want to sleep here! */
5032 request->fl_flags = fl_flags | FL_SLEEP;
5033 if (do_vfs_lock(request->fl_file, request) < 0)
5034 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5035 "manager!\n", __func__);
5037 up_read(&nfsi->rwsem);
5039 request->fl_flags = fl_flags;
5043 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5045 struct nfs4_exception exception = {
5047 .inode = state->inode,
5052 err = _nfs4_proc_setlk(state, cmd, request);
5053 if (err == -NFS4ERR_DENIED)
5055 err = nfs4_handle_exception(NFS_SERVER(state->inode),
5057 } while (exception.retry);
5062 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5064 struct nfs_open_context *ctx;
5065 struct nfs4_state *state;
5066 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5069 /* verify open state */
5070 ctx = nfs_file_open_context(filp);
5073 if (request->fl_start < 0 || request->fl_end < 0)
5076 if (IS_GETLK(cmd)) {
5078 return nfs4_proc_getlk(state, F_GETLK, request);
5082 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5085 if (request->fl_type == F_UNLCK) {
5087 return nfs4_proc_unlck(state, cmd, request);
5094 * Don't rely on the VFS having checked the file open mode,
5095 * since it won't do this for flock() locks.
5097 switch (request->fl_type) {
5099 if (!(filp->f_mode & FMODE_READ))
5103 if (!(filp->f_mode & FMODE_WRITE))
5108 status = nfs4_proc_setlk(state, cmd, request);
5109 if ((status != -EAGAIN) || IS_SETLK(cmd))
5111 timeout = nfs4_set_lock_task_retry(timeout);
5112 status = -ERESTARTSYS;
5115 } while(status < 0);
5119 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5121 struct nfs_server *server = NFS_SERVER(state->inode);
5122 struct nfs4_exception exception = { };
5125 err = nfs4_set_lock_state(state, fl);
5129 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
5132 printk(KERN_ERR "NFS: %s: unhandled error "
5133 "%d.\n", __func__, err);
5137 case -NFS4ERR_EXPIRED:
5138 nfs4_schedule_stateid_recovery(server, state);
5139 case -NFS4ERR_STALE_CLIENTID:
5140 case -NFS4ERR_STALE_STATEID:
5141 nfs4_schedule_lease_recovery(server->nfs_client);
5143 case -NFS4ERR_BADSESSION:
5144 case -NFS4ERR_BADSLOT:
5145 case -NFS4ERR_BAD_HIGH_SLOT:
5146 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5147 case -NFS4ERR_DEADSESSION:
5148 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
5152 * The show must go on: exit, but mark the
5153 * stateid as needing recovery.
5155 case -NFS4ERR_DELEG_REVOKED:
5156 case -NFS4ERR_ADMIN_REVOKED:
5157 case -NFS4ERR_BAD_STATEID:
5158 case -NFS4ERR_OPENMODE:
5159 nfs4_schedule_stateid_recovery(server, state);
5164 * User RPCSEC_GSS context has expired.
5165 * We cannot recover this stateid now, so
5166 * skip it and allow recovery thread to
5172 case -NFS4ERR_DENIED:
5173 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5176 case -NFS4ERR_DELAY:
5179 err = nfs4_handle_exception(server, err, &exception);
5180 } while (exception.retry);
5185 struct nfs_release_lockowner_data {
5186 struct nfs4_lock_state *lsp;
5187 struct nfs_server *server;
5188 struct nfs_release_lockowner_args args;
5191 static void nfs4_release_lockowner_release(void *calldata)
5193 struct nfs_release_lockowner_data *data = calldata;
5194 nfs4_free_lock_state(data->server, data->lsp);
5198 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5199 .rpc_release = nfs4_release_lockowner_release,
5202 int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
5204 struct nfs_server *server = lsp->ls_state->owner->so_server;
5205 struct nfs_release_lockowner_data *data;
5206 struct rpc_message msg = {
5207 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5210 if (server->nfs_client->cl_mvops->minor_version != 0)
5212 data = kmalloc(sizeof(*data), GFP_NOFS);
5216 data->server = server;
5217 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5218 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5219 data->args.lock_owner.s_dev = server->s_dev;
5220 msg.rpc_argp = &data->args;
5221 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5225 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5227 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5228 const void *buf, size_t buflen,
5229 int flags, int type)
5231 if (strcmp(key, "") != 0)
5234 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5237 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5238 void *buf, size_t buflen, int type)
5240 if (strcmp(key, "") != 0)
5243 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5246 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5247 size_t list_len, const char *name,
5248 size_t name_len, int type)
5250 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5252 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5255 if (list && len <= list_len)
5256 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5261 * nfs_fhget will use either the mounted_on_fileid or the fileid
5263 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5265 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5266 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5267 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
5268 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
5271 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
5272 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
5273 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5277 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5278 const struct qstr *name,
5279 struct nfs4_fs_locations *fs_locations,
5282 struct nfs_server *server = NFS_SERVER(dir);
5284 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
5286 struct nfs4_fs_locations_arg args = {
5287 .dir_fh = NFS_FH(dir),
5292 struct nfs4_fs_locations_res res = {
5293 .fs_locations = fs_locations,
5295 struct rpc_message msg = {
5296 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5302 dprintk("%s: start\n", __func__);
5304 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5305 * is not supported */
5306 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5307 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5309 bitmask[0] |= FATTR4_WORD0_FILEID;
5311 nfs_fattr_init(&fs_locations->fattr);
5312 fs_locations->server = server;
5313 fs_locations->nlocations = 0;
5314 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
5315 dprintk("%s: returned status = %d\n", __func__, status);
5319 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5320 const struct qstr *name,
5321 struct nfs4_fs_locations *fs_locations,
5324 struct nfs4_exception exception = { };
5327 err = nfs4_handle_exception(NFS_SERVER(dir),
5328 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
5330 } while (exception.retry);
5334 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5337 struct nfs4_secinfo_arg args = {
5338 .dir_fh = NFS_FH(dir),
5341 struct nfs4_secinfo_res res = {
5344 struct rpc_message msg = {
5345 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5350 dprintk("NFS call secinfo %s\n", name->name);
5351 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5352 dprintk("NFS reply secinfo: %d\n", status);
5356 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5357 struct nfs4_secinfo_flavors *flavors)
5359 struct nfs4_exception exception = { };
5362 err = nfs4_handle_exception(NFS_SERVER(dir),
5363 _nfs4_proc_secinfo(dir, name, flavors),
5365 } while (exception.retry);
5369 #ifdef CONFIG_NFS_V4_1
5371 * Check the exchange flags returned by the server for invalid flags, having
5372 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5375 static int nfs4_check_cl_exchange_flags(u32 flags)
5377 if (flags & ~EXCHGID4_FLAG_MASK_R)
5379 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5380 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5382 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5386 return -NFS4ERR_INVAL;
5390 nfs41_same_server_scope(struct nfs41_server_scope *a,
5391 struct nfs41_server_scope *b)
5393 if (a->server_scope_sz == b->server_scope_sz &&
5394 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5401 * nfs4_proc_bind_conn_to_session()
5403 * The 4.1 client currently uses the same TCP connection for the
5404 * fore and backchannel.
5406 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
5409 struct nfs41_bind_conn_to_session_res res;
5410 struct rpc_message msg = {
5412 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5418 dprintk("--> %s\n", __func__);
5420 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5421 if (unlikely(res.session == NULL)) {
5426 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5428 if (memcmp(res.session->sess_id.data,
5429 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5430 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5434 if (res.dir != NFS4_CDFS4_BOTH) {
5435 dprintk("NFS: %s: Unexpected direction from server\n",
5440 if (res.use_conn_in_rdma_mode) {
5441 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5450 dprintk("<-- %s status= %d\n", __func__, status);
5455 * nfs4_proc_exchange_id()
5457 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5459 * Since the clientid has expired, all compounds using sessions
5460 * associated with the stale clientid will be returning
5461 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5462 * be in some phase of session reset.
5464 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
5466 nfs4_verifier verifier;
5467 struct nfs41_exchange_id_args args = {
5468 .verifier = &verifier,
5470 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
5472 struct nfs41_exchange_id_res res = {
5476 struct rpc_message msg = {
5477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5483 nfs4_init_boot_verifier(clp, &verifier);
5484 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5486 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5487 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5488 args.id_len, args.id);
5490 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
5492 if (unlikely(res.server_owner == NULL)) {
5497 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
5499 if (unlikely(res.server_scope == NULL)) {
5501 goto out_server_owner;
5504 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
5505 if (unlikely(res.impl_id == NULL)) {
5507 goto out_server_scope;
5510 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5512 status = nfs4_check_cl_exchange_flags(res.flags);
5515 clp->cl_clientid = res.clientid;
5516 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5517 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5518 clp->cl_seqid = res.seqid;
5520 kfree(clp->cl_serverowner);
5521 clp->cl_serverowner = res.server_owner;
5522 res.server_owner = NULL;
5524 /* use the most recent implementation id */
5525 kfree(clp->cl_implid);
5526 clp->cl_implid = res.impl_id;
5528 if (clp->cl_serverscope != NULL &&
5529 !nfs41_same_server_scope(clp->cl_serverscope,
5530 res.server_scope)) {
5531 dprintk("%s: server_scope mismatch detected\n",
5533 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5534 kfree(clp->cl_serverscope);
5535 clp->cl_serverscope = NULL;
5538 if (clp->cl_serverscope == NULL) {
5539 clp->cl_serverscope = res.server_scope;
5546 kfree(res.server_owner);
5548 kfree(res.server_scope);
5550 if (clp->cl_implid != NULL)
5551 dprintk("NFS reply exchange_id: Server Implementation ID: "
5552 "domain: %s, name: %s, date: %llu,%u\n",
5553 clp->cl_implid->domain, clp->cl_implid->name,
5554 clp->cl_implid->date.seconds,
5555 clp->cl_implid->date.nseconds);
5556 dprintk("NFS reply exchange_id: %d\n", status);
5560 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5561 struct rpc_cred *cred)
5563 struct rpc_message msg = {
5564 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5570 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5572 dprintk("NFS: Got error %d from the server %s on "
5573 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5577 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5578 struct rpc_cred *cred)
5583 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5584 ret = _nfs4_proc_destroy_clientid(clp, cred);
5586 case -NFS4ERR_DELAY:
5587 case -NFS4ERR_CLIENTID_BUSY:
5597 int nfs4_destroy_clientid(struct nfs_client *clp)
5599 struct rpc_cred *cred;
5602 if (clp->cl_mvops->minor_version < 1)
5604 if (clp->cl_exchange_flags == 0)
5606 if (clp->cl_preserve_clid)
5608 cred = nfs4_get_exchange_id_cred(clp);
5609 ret = nfs4_proc_destroy_clientid(clp, cred);
5614 case -NFS4ERR_STALE_CLIENTID:
5615 clp->cl_exchange_flags = 0;
5621 struct nfs4_get_lease_time_data {
5622 struct nfs4_get_lease_time_args *args;
5623 struct nfs4_get_lease_time_res *res;
5624 struct nfs_client *clp;
5627 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5631 struct nfs4_get_lease_time_data *data =
5632 (struct nfs4_get_lease_time_data *)calldata;
5634 dprintk("--> %s\n", __func__);
5635 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5636 /* just setup sequence, do not trigger session recovery
5637 since we're invoked within one */
5638 ret = nfs41_setup_sequence(data->clp->cl_session,
5639 &data->args->la_seq_args,
5640 &data->res->lr_seq_res, task);
5643 rpc_call_start(task);
5644 dprintk("<-- %s\n", __func__);
5648 * Called from nfs4_state_manager thread for session setup, so don't recover
5649 * from sequence operation or clientid errors.
5651 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5653 struct nfs4_get_lease_time_data *data =
5654 (struct nfs4_get_lease_time_data *)calldata;
5656 dprintk("--> %s\n", __func__);
5657 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5659 switch (task->tk_status) {
5660 case -NFS4ERR_DELAY:
5661 case -NFS4ERR_GRACE:
5662 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5663 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5664 task->tk_status = 0;
5666 case -NFS4ERR_RETRY_UNCACHED_REP:
5667 rpc_restart_call_prepare(task);
5670 dprintk("<-- %s\n", __func__);
5673 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5674 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5675 .rpc_call_done = nfs4_get_lease_time_done,
5678 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5680 struct rpc_task *task;
5681 struct nfs4_get_lease_time_args args;
5682 struct nfs4_get_lease_time_res res = {
5683 .lr_fsinfo = fsinfo,
5685 struct nfs4_get_lease_time_data data = {
5690 struct rpc_message msg = {
5691 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5695 struct rpc_task_setup task_setup = {
5696 .rpc_client = clp->cl_rpcclient,
5697 .rpc_message = &msg,
5698 .callback_ops = &nfs4_get_lease_time_ops,
5699 .callback_data = &data,
5700 .flags = RPC_TASK_TIMEOUT,
5704 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5705 dprintk("--> %s\n", __func__);
5706 task = rpc_run_task(&task_setup);
5709 status = PTR_ERR(task);
5711 status = task->tk_status;
5714 dprintk("<-- %s return %d\n", __func__, status);
5719 static int nfs4_grow_slot_table(struct nfs4_slot_table *tbl,
5720 u32 max_reqs, u32 ivalue)
5722 if (max_reqs <= tbl->max_slots)
5724 if (!IS_ERR(nfs4_find_or_create_slot(tbl, max_reqs - 1, ivalue, GFP_NOFS)))
5729 static void nfs4_reset_slot_table(struct nfs4_slot_table *tbl,
5730 u32 server_highest_slotid,
5733 struct nfs4_slot **p;
5735 nfs4_shrink_slot_table(tbl, server_highest_slotid + 1);
5738 (*p)->seq_nr = ivalue;
5741 tbl->highest_used_slotid = NFS4_NO_SLOT;
5742 tbl->target_highest_slotid = server_highest_slotid;
5743 tbl->server_highest_slotid = server_highest_slotid;
5744 tbl->max_slotid = server_highest_slotid;
5748 * (re)Initialise a slot table
5750 static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl,
5751 u32 max_reqs, u32 ivalue)
5755 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5756 max_reqs, tbl->max_slots);
5758 if (max_reqs > NFS4_MAX_SLOT_TABLE)
5759 max_reqs = NFS4_MAX_SLOT_TABLE;
5761 ret = nfs4_grow_slot_table(tbl, max_reqs, ivalue);
5765 spin_lock(&tbl->slot_tbl_lock);
5766 nfs4_reset_slot_table(tbl, max_reqs - 1, ivalue);
5767 spin_unlock(&tbl->slot_tbl_lock);
5769 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5770 tbl, tbl->slots, tbl->max_slots);
5772 dprintk("<-- %s: return %d\n", __func__, ret);
5776 /* Destroy the slot table */
5777 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5779 nfs4_shrink_slot_table(&session->fc_slot_table, 0);
5780 nfs4_shrink_slot_table(&session->bc_slot_table, 0);
5784 * Initialize or reset the forechannel and backchannel tables
5786 static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
5788 struct nfs4_slot_table *tbl;
5791 dprintk("--> %s\n", __func__);
5793 tbl = &ses->fc_slot_table;
5795 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5796 if (status) /* -ENOMEM */
5799 tbl = &ses->bc_slot_table;
5801 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5802 if (status && tbl->slots == NULL)
5803 /* Fore and back channel share a connection so get
5804 * both slot tables or neither */
5805 nfs4_destroy_slot_tables(ses);
5809 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5811 struct nfs4_session *session;
5812 struct nfs4_slot_table *tbl;
5814 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5818 tbl = &session->fc_slot_table;
5819 tbl->highest_used_slotid = NFS4_NO_SLOT;
5820 spin_lock_init(&tbl->slot_tbl_lock);
5821 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5822 init_completion(&tbl->complete);
5824 tbl = &session->bc_slot_table;
5825 tbl->highest_used_slotid = NFS4_NO_SLOT;
5826 spin_lock_init(&tbl->slot_tbl_lock);
5827 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5828 init_completion(&tbl->complete);
5830 session->session_state = 1<<NFS4_SESSION_INITING;
5836 void nfs4_destroy_session(struct nfs4_session *session)
5838 struct rpc_xprt *xprt;
5839 struct rpc_cred *cred;
5841 cred = nfs4_get_exchange_id_cred(session->clp);
5842 nfs4_proc_destroy_session(session, cred);
5847 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5849 dprintk("%s Destroy backchannel for xprt %p\n",
5851 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
5852 nfs4_destroy_slot_tables(session);
5857 * Initialize the values to be used by the client in CREATE_SESSION
5858 * If nfs4_init_session set the fore channel request and response sizes,
5861 * Set the back channel max_resp_sz_cached to zero to force the client to
5862 * always set csa_cachethis to FALSE because the current implementation
5863 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5865 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5867 struct nfs4_session *session = args->client->cl_session;
5868 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5869 mxresp_sz = session->fc_target_max_resp_sz;
5872 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5874 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5875 /* Fore channel attributes */
5876 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5877 args->fc_attrs.max_resp_sz = mxresp_sz;
5878 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5879 args->fc_attrs.max_reqs = max_session_slots;
5881 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5882 "max_ops=%u max_reqs=%u\n",
5884 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5885 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5887 /* Back channel attributes */
5888 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5889 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5890 args->bc_attrs.max_resp_sz_cached = 0;
5891 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5892 args->bc_attrs.max_reqs = 1;
5894 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5895 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5897 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5898 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5899 args->bc_attrs.max_reqs);
5902 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5904 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5905 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5907 if (rcvd->max_resp_sz > sent->max_resp_sz)
5910 * Our requested max_ops is the minimum we need; we're not
5911 * prepared to break up compounds into smaller pieces than that.
5912 * So, no point even trying to continue if the server won't
5915 if (rcvd->max_ops < sent->max_ops)
5917 if (rcvd->max_reqs == 0)
5919 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5920 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5924 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5926 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5927 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5929 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5931 if (rcvd->max_resp_sz < sent->max_resp_sz)
5933 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5935 /* These would render the backchannel useless: */
5936 if (rcvd->max_ops != sent->max_ops)
5938 if (rcvd->max_reqs != sent->max_reqs)
5943 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5944 struct nfs4_session *session)
5948 ret = nfs4_verify_fore_channel_attrs(args, session);
5951 return nfs4_verify_back_channel_attrs(args, session);
5954 static int _nfs4_proc_create_session(struct nfs_client *clp,
5955 struct rpc_cred *cred)
5957 struct nfs4_session *session = clp->cl_session;
5958 struct nfs41_create_session_args args = {
5960 .cb_program = NFS4_CALLBACK,
5962 struct nfs41_create_session_res res = {
5965 struct rpc_message msg = {
5966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5973 nfs4_init_channel_attrs(&args);
5974 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5976 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5979 /* Verify the session's negotiated channel_attrs values */
5980 status = nfs4_verify_channel_attrs(&args, session);
5981 /* Increment the clientid slot sequence id */
5989 * Issues a CREATE_SESSION operation to the server.
5990 * It is the responsibility of the caller to verify the session is
5991 * expired before calling this routine.
5993 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
5997 struct nfs4_session *session = clp->cl_session;
5999 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6001 status = _nfs4_proc_create_session(clp, cred);
6005 /* Init or reset the session slot tables */
6006 status = nfs4_setup_session_slot_tables(session);
6007 dprintk("slot table setup returned %d\n", status);
6011 ptr = (unsigned *)&session->sess_id.data[0];
6012 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6013 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
6015 dprintk("<-- %s\n", __func__);
6020 * Issue the over-the-wire RPC DESTROY_SESSION.
6021 * The caller must serialize access to this routine.
6023 int nfs4_proc_destroy_session(struct nfs4_session *session,
6024 struct rpc_cred *cred)
6026 struct rpc_message msg = {
6027 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6028 .rpc_argp = session,
6033 dprintk("--> nfs4_proc_destroy_session\n");
6035 /* session is still being setup */
6036 if (session->clp->cl_cons_state != NFS_CS_READY)
6039 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6042 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
6043 "Session has been destroyed regardless...\n", status);
6045 dprintk("<-- nfs4_proc_destroy_session\n");
6050 * With sessions, the client is not marked ready until after a
6051 * successful EXCHANGE_ID and CREATE_SESSION.
6053 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
6054 * other versions of NFS can be tried.
6056 static int nfs41_check_session_ready(struct nfs_client *clp)
6060 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
6061 ret = nfs4_client_recover_expired_lease(clp);
6065 if (clp->cl_cons_state < NFS_CS_READY)
6066 return -EPROTONOSUPPORT;
6071 int nfs4_init_session(struct nfs_server *server)
6073 struct nfs_client *clp = server->nfs_client;
6074 struct nfs4_session *session;
6075 unsigned int target_max_rqst_sz = NFS_MAX_FILE_IO_SIZE;
6076 unsigned int target_max_resp_sz = NFS_MAX_FILE_IO_SIZE;
6078 if (!nfs4_has_session(clp))
6081 if (server->rsize != 0)
6082 target_max_resp_sz = server->rsize;
6083 target_max_resp_sz += nfs41_maxread_overhead;
6085 if (server->wsize != 0)
6086 target_max_rqst_sz = server->wsize;
6087 target_max_rqst_sz += nfs41_maxwrite_overhead;
6089 session = clp->cl_session;
6090 spin_lock(&clp->cl_lock);
6091 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6092 /* Initialise targets and channel attributes */
6093 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6094 session->fc_attrs.max_rqst_sz = target_max_rqst_sz;
6095 session->fc_target_max_resp_sz = target_max_resp_sz;
6096 session->fc_attrs.max_resp_sz = target_max_resp_sz;
6098 /* Just adjust the targets */
6099 if (target_max_rqst_sz > session->fc_target_max_rqst_sz) {
6100 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6101 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6103 if (target_max_resp_sz > session->fc_target_max_resp_sz) {
6104 session->fc_target_max_resp_sz = target_max_resp_sz;
6105 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6108 spin_unlock(&clp->cl_lock);
6110 if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
6111 nfs4_schedule_lease_recovery(clp);
6113 return nfs41_check_session_ready(clp);
6116 int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
6118 struct nfs4_session *session = clp->cl_session;
6121 spin_lock(&clp->cl_lock);
6122 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6124 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
6125 * DS lease to be equal to the MDS lease.
6127 clp->cl_lease_time = lease_time;
6128 clp->cl_last_renewal = jiffies;
6130 spin_unlock(&clp->cl_lock);
6132 ret = nfs41_check_session_ready(clp);
6135 /* Test for the DS role */
6136 if (!is_ds_client(clp))
6140 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
6144 * Renew the cl_session lease.
6146 struct nfs4_sequence_data {
6147 struct nfs_client *clp;
6148 struct nfs4_sequence_args args;
6149 struct nfs4_sequence_res res;
6152 static void nfs41_sequence_release(void *data)
6154 struct nfs4_sequence_data *calldata = data;
6155 struct nfs_client *clp = calldata->clp;
6157 if (atomic_read(&clp->cl_count) > 1)
6158 nfs4_schedule_state_renewal(clp);
6159 nfs_put_client(clp);
6163 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6165 switch(task->tk_status) {
6166 case -NFS4ERR_DELAY:
6167 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6170 nfs4_schedule_lease_recovery(clp);
6175 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
6177 struct nfs4_sequence_data *calldata = data;
6178 struct nfs_client *clp = calldata->clp;
6180 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6183 if (task->tk_status < 0) {
6184 dprintk("%s ERROR %d\n", __func__, task->tk_status);
6185 if (atomic_read(&clp->cl_count) == 1)
6188 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6189 rpc_restart_call_prepare(task);
6193 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
6195 dprintk("<-- %s\n", __func__);
6198 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6200 struct nfs4_sequence_data *calldata = data;
6201 struct nfs_client *clp = calldata->clp;
6202 struct nfs4_sequence_args *args;
6203 struct nfs4_sequence_res *res;
6205 args = task->tk_msg.rpc_argp;
6206 res = task->tk_msg.rpc_resp;
6208 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
6210 rpc_call_start(task);
6213 static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
6215 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6216 nfs41_sequence_prepare(task, data);
6219 static const struct rpc_call_ops nfs41_sequence_ops = {
6220 .rpc_call_done = nfs41_sequence_call_done,
6221 .rpc_call_prepare = nfs41_sequence_prepare,
6222 .rpc_release = nfs41_sequence_release,
6225 static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
6226 .rpc_call_done = nfs41_sequence_call_done,
6227 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
6228 .rpc_release = nfs41_sequence_release,
6231 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
6232 const struct rpc_call_ops *seq_ops)
6234 struct nfs4_sequence_data *calldata;
6235 struct rpc_message msg = {
6236 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6239 struct rpc_task_setup task_setup_data = {
6240 .rpc_client = clp->cl_rpcclient,
6241 .rpc_message = &msg,
6242 .callback_ops = seq_ops,
6243 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6246 if (!atomic_inc_not_zero(&clp->cl_count))
6247 return ERR_PTR(-EIO);
6248 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6249 if (calldata == NULL) {
6250 nfs_put_client(clp);
6251 return ERR_PTR(-ENOMEM);
6253 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
6254 msg.rpc_argp = &calldata->args;
6255 msg.rpc_resp = &calldata->res;
6256 calldata->clp = clp;
6257 task_setup_data.callback_data = calldata;
6259 return rpc_run_task(&task_setup_data);
6262 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
6264 struct rpc_task *task;
6267 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6269 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
6271 ret = PTR_ERR(task);
6273 rpc_put_task_async(task);
6274 dprintk("<-- %s status=%d\n", __func__, ret);
6278 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6280 struct rpc_task *task;
6283 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
6285 ret = PTR_ERR(task);
6288 ret = rpc_wait_for_completion_task(task);
6290 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6292 if (task->tk_status == 0)
6293 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
6294 ret = task->tk_status;
6298 dprintk("<-- %s status=%d\n", __func__, ret);
6302 struct nfs4_reclaim_complete_data {
6303 struct nfs_client *clp;
6304 struct nfs41_reclaim_complete_args arg;
6305 struct nfs41_reclaim_complete_res res;
6308 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6310 struct nfs4_reclaim_complete_data *calldata = data;
6312 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6313 if (nfs41_setup_sequence(calldata->clp->cl_session,
6314 &calldata->arg.seq_args,
6315 &calldata->res.seq_res, task))
6318 rpc_call_start(task);
6321 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6323 switch(task->tk_status) {
6325 case -NFS4ERR_COMPLETE_ALREADY:
6326 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6328 case -NFS4ERR_DELAY:
6329 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6331 case -NFS4ERR_RETRY_UNCACHED_REP:
6334 nfs4_schedule_lease_recovery(clp);
6339 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6341 struct nfs4_reclaim_complete_data *calldata = data;
6342 struct nfs_client *clp = calldata->clp;
6343 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6345 dprintk("--> %s\n", __func__);
6346 if (!nfs41_sequence_done(task, res))
6349 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6350 rpc_restart_call_prepare(task);
6353 dprintk("<-- %s\n", __func__);
6356 static void nfs4_free_reclaim_complete_data(void *data)
6358 struct nfs4_reclaim_complete_data *calldata = data;
6363 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6364 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6365 .rpc_call_done = nfs4_reclaim_complete_done,
6366 .rpc_release = nfs4_free_reclaim_complete_data,
6370 * Issue a global reclaim complete.
6372 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6374 struct nfs4_reclaim_complete_data *calldata;
6375 struct rpc_task *task;
6376 struct rpc_message msg = {
6377 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6379 struct rpc_task_setup task_setup_data = {
6380 .rpc_client = clp->cl_rpcclient,
6381 .rpc_message = &msg,
6382 .callback_ops = &nfs4_reclaim_complete_call_ops,
6383 .flags = RPC_TASK_ASYNC,
6385 int status = -ENOMEM;
6387 dprintk("--> %s\n", __func__);
6388 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6389 if (calldata == NULL)
6391 calldata->clp = clp;
6392 calldata->arg.one_fs = 0;
6394 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
6395 msg.rpc_argp = &calldata->arg;
6396 msg.rpc_resp = &calldata->res;
6397 task_setup_data.callback_data = calldata;
6398 task = rpc_run_task(&task_setup_data);
6400 status = PTR_ERR(task);
6403 status = nfs4_wait_for_completion_rpc_task(task);
6405 status = task->tk_status;
6409 dprintk("<-- %s status=%d\n", __func__, status);
6414 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6416 struct nfs4_layoutget *lgp = calldata;
6417 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6419 dprintk("--> %s\n", __func__);
6420 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6421 * right now covering the LAYOUTGET we are about to send.
6422 * However, that is not so catastrophic, and there seems
6423 * to be no way to prevent it completely.
6425 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
6426 &lgp->res.seq_res, task))
6428 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6429 NFS_I(lgp->args.inode)->layout,
6430 lgp->args.ctx->state)) {
6431 rpc_exit(task, NFS4_OK);
6434 rpc_call_start(task);
6437 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6439 struct nfs4_layoutget *lgp = calldata;
6440 struct inode *inode = lgp->args.inode;
6441 struct nfs_server *server = NFS_SERVER(inode);
6442 struct pnfs_layout_hdr *lo;
6443 struct nfs4_state *state = NULL;
6445 dprintk("--> %s\n", __func__);
6447 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6450 switch (task->tk_status) {
6453 case -NFS4ERR_LAYOUTTRYLATER:
6454 case -NFS4ERR_RECALLCONFLICT:
6455 task->tk_status = -NFS4ERR_DELAY;
6457 case -NFS4ERR_EXPIRED:
6458 case -NFS4ERR_BAD_STATEID:
6459 spin_lock(&inode->i_lock);
6460 lo = NFS_I(inode)->layout;
6461 if (!lo || list_empty(&lo->plh_segs)) {
6462 spin_unlock(&inode->i_lock);
6463 /* If the open stateid was bad, then recover it. */
6464 state = lgp->args.ctx->state;
6468 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6469 spin_unlock(&inode->i_lock);
6470 /* Mark the bad layout state as invalid, then
6471 * retry using the open stateid. */
6472 pnfs_free_lseg_list(&head);
6475 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6476 rpc_restart_call_prepare(task);
6478 dprintk("<-- %s\n", __func__);
6481 static size_t max_response_pages(struct nfs_server *server)
6483 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6484 return nfs_page_array_len(0, max_resp_sz);
6487 static void nfs4_free_pages(struct page **pages, size_t size)
6494 for (i = 0; i < size; i++) {
6497 __free_page(pages[i]);
6502 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6504 struct page **pages;
6507 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6509 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6513 for (i = 0; i < size; i++) {
6514 pages[i] = alloc_page(gfp_flags);
6516 dprintk("%s: failed to allocate page\n", __func__);
6517 nfs4_free_pages(pages, size);
6525 static void nfs4_layoutget_release(void *calldata)
6527 struct nfs4_layoutget *lgp = calldata;
6528 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6529 size_t max_pages = max_response_pages(server);
6531 dprintk("--> %s\n", __func__);
6532 nfs4_free_pages(lgp->args.layout.pages, max_pages);
6533 put_nfs_open_context(lgp->args.ctx);
6535 dprintk("<-- %s\n", __func__);
6538 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6539 .rpc_call_prepare = nfs4_layoutget_prepare,
6540 .rpc_call_done = nfs4_layoutget_done,
6541 .rpc_release = nfs4_layoutget_release,
6544 struct pnfs_layout_segment *
6545 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
6547 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6548 size_t max_pages = max_response_pages(server);
6549 struct rpc_task *task;
6550 struct rpc_message msg = {
6551 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6552 .rpc_argp = &lgp->args,
6553 .rpc_resp = &lgp->res,
6555 struct rpc_task_setup task_setup_data = {
6556 .rpc_client = server->client,
6557 .rpc_message = &msg,
6558 .callback_ops = &nfs4_layoutget_call_ops,
6559 .callback_data = lgp,
6560 .flags = RPC_TASK_ASYNC,
6562 struct pnfs_layout_segment *lseg = NULL;
6565 dprintk("--> %s\n", __func__);
6567 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6568 if (!lgp->args.layout.pages) {
6569 nfs4_layoutget_release(lgp);
6570 return ERR_PTR(-ENOMEM);
6572 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6574 lgp->res.layoutp = &lgp->args.layout;
6575 lgp->res.seq_res.sr_slot = NULL;
6576 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
6577 task = rpc_run_task(&task_setup_data);
6579 return ERR_CAST(task);
6580 status = nfs4_wait_for_completion_rpc_task(task);
6582 status = task->tk_status;
6584 lseg = pnfs_layout_process(lgp);
6586 dprintk("<-- %s status=%d\n", __func__, status);
6588 return ERR_PTR(status);
6593 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6595 struct nfs4_layoutreturn *lrp = calldata;
6597 dprintk("--> %s\n", __func__);
6598 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
6599 &lrp->res.seq_res, task))
6601 rpc_call_start(task);
6604 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6606 struct nfs4_layoutreturn *lrp = calldata;
6607 struct nfs_server *server;
6609 dprintk("--> %s\n", __func__);
6611 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6614 server = NFS_SERVER(lrp->args.inode);
6615 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6616 rpc_restart_call_prepare(task);
6619 dprintk("<-- %s\n", __func__);
6622 static void nfs4_layoutreturn_release(void *calldata)
6624 struct nfs4_layoutreturn *lrp = calldata;
6625 struct pnfs_layout_hdr *lo = lrp->args.layout;
6627 dprintk("--> %s\n", __func__);
6628 spin_lock(&lo->plh_inode->i_lock);
6629 if (lrp->res.lrs_present)
6630 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6631 lo->plh_block_lgets--;
6632 spin_unlock(&lo->plh_inode->i_lock);
6633 pnfs_put_layout_hdr(lrp->args.layout);
6635 dprintk("<-- %s\n", __func__);
6638 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6639 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6640 .rpc_call_done = nfs4_layoutreturn_done,
6641 .rpc_release = nfs4_layoutreturn_release,
6644 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6646 struct rpc_task *task;
6647 struct rpc_message msg = {
6648 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6649 .rpc_argp = &lrp->args,
6650 .rpc_resp = &lrp->res,
6652 struct rpc_task_setup task_setup_data = {
6653 .rpc_client = lrp->clp->cl_rpcclient,
6654 .rpc_message = &msg,
6655 .callback_ops = &nfs4_layoutreturn_call_ops,
6656 .callback_data = lrp,
6660 dprintk("--> %s\n", __func__);
6661 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
6662 task = rpc_run_task(&task_setup_data);
6664 return PTR_ERR(task);
6665 status = task->tk_status;
6666 dprintk("<-- %s status=%d\n", __func__, status);
6672 * Retrieve the list of Data Server devices from the MDS.
6674 static int _nfs4_getdevicelist(struct nfs_server *server,
6675 const struct nfs_fh *fh,
6676 struct pnfs_devicelist *devlist)
6678 struct nfs4_getdevicelist_args args = {
6680 .layoutclass = server->pnfs_curr_ld->id,
6682 struct nfs4_getdevicelist_res res = {
6685 struct rpc_message msg = {
6686 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6692 dprintk("--> %s\n", __func__);
6693 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6695 dprintk("<-- %s status=%d\n", __func__, status);
6699 int nfs4_proc_getdevicelist(struct nfs_server *server,
6700 const struct nfs_fh *fh,
6701 struct pnfs_devicelist *devlist)
6703 struct nfs4_exception exception = { };
6707 err = nfs4_handle_exception(server,
6708 _nfs4_getdevicelist(server, fh, devlist),
6710 } while (exception.retry);
6712 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6713 err, devlist->num_devs);
6717 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6720 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6722 struct nfs4_getdeviceinfo_args args = {
6725 struct nfs4_getdeviceinfo_res res = {
6728 struct rpc_message msg = {
6729 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6735 dprintk("--> %s\n", __func__);
6736 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6737 dprintk("<-- %s status=%d\n", __func__, status);
6742 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6744 struct nfs4_exception exception = { };
6748 err = nfs4_handle_exception(server,
6749 _nfs4_proc_getdeviceinfo(server, pdev),
6751 } while (exception.retry);
6754 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6756 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6758 struct nfs4_layoutcommit_data *data = calldata;
6759 struct nfs_server *server = NFS_SERVER(data->args.inode);
6761 if (nfs4_setup_sequence(server, &data->args.seq_args,
6762 &data->res.seq_res, task))
6764 rpc_call_start(task);
6768 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6770 struct nfs4_layoutcommit_data *data = calldata;
6771 struct nfs_server *server = NFS_SERVER(data->args.inode);
6773 if (!nfs4_sequence_done(task, &data->res.seq_res))
6776 switch (task->tk_status) { /* Just ignore these failures */
6777 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6778 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6779 case -NFS4ERR_BADLAYOUT: /* no layout */
6780 case -NFS4ERR_GRACE: /* loca_recalim always false */
6781 task->tk_status = 0;
6784 nfs_post_op_update_inode_force_wcc(data->args.inode,
6788 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6789 rpc_restart_call_prepare(task);
6795 static void nfs4_layoutcommit_release(void *calldata)
6797 struct nfs4_layoutcommit_data *data = calldata;
6798 struct pnfs_layout_segment *lseg, *tmp;
6799 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
6801 pnfs_cleanup_layoutcommit(data);
6802 /* Matched by references in pnfs_set_layoutcommit */
6803 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6804 list_del_init(&lseg->pls_lc_list);
6805 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6807 pnfs_put_lseg(lseg);
6810 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6811 smp_mb__after_clear_bit();
6812 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6814 put_rpccred(data->cred);
6818 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6819 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6820 .rpc_call_done = nfs4_layoutcommit_done,
6821 .rpc_release = nfs4_layoutcommit_release,
6825 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6827 struct rpc_message msg = {
6828 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6829 .rpc_argp = &data->args,
6830 .rpc_resp = &data->res,
6831 .rpc_cred = data->cred,
6833 struct rpc_task_setup task_setup_data = {
6834 .task = &data->task,
6835 .rpc_client = NFS_CLIENT(data->args.inode),
6836 .rpc_message = &msg,
6837 .callback_ops = &nfs4_layoutcommit_ops,
6838 .callback_data = data,
6839 .flags = RPC_TASK_ASYNC,
6841 struct rpc_task *task;
6844 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6845 "lbw: %llu inode %lu\n",
6846 data->task.tk_pid, sync,
6847 data->args.lastbytewritten,
6848 data->args.inode->i_ino);
6850 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6851 task = rpc_run_task(&task_setup_data);
6853 return PTR_ERR(task);
6856 status = nfs4_wait_for_completion_rpc_task(task);
6859 status = task->tk_status;
6861 dprintk("%s: status %d\n", __func__, status);
6867 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6868 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6870 struct nfs41_secinfo_no_name_args args = {
6871 .style = SECINFO_STYLE_CURRENT_FH,
6873 struct nfs4_secinfo_res res = {
6876 struct rpc_message msg = {
6877 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6881 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6885 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6886 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6888 struct nfs4_exception exception = { };
6891 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6894 case -NFS4ERR_WRONGSEC:
6895 case -NFS4ERR_NOTSUPP:
6898 err = nfs4_handle_exception(server, err, &exception);
6900 } while (exception.retry);
6906 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6907 struct nfs_fsinfo *info)
6911 rpc_authflavor_t flavor;
6912 struct nfs4_secinfo_flavors *flavors;
6914 page = alloc_page(GFP_KERNEL);
6920 flavors = page_address(page);
6921 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6924 * Fall back on "guess and check" method if
6925 * the server doesn't support SECINFO_NO_NAME
6927 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6928 err = nfs4_find_root_sec(server, fhandle, info);
6934 flavor = nfs_find_best_sec(flavors);
6936 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6946 static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6949 struct nfs41_test_stateid_args args = {
6952 struct nfs41_test_stateid_res res;
6953 struct rpc_message msg = {
6954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6959 dprintk("NFS call test_stateid %p\n", stateid);
6960 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6961 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6962 if (status != NFS_OK) {
6963 dprintk("NFS reply test_stateid: failed, %d\n", status);
6966 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
6971 * nfs41_test_stateid - perform a TEST_STATEID operation
6973 * @server: server / transport on which to perform the operation
6974 * @stateid: state ID to test
6976 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6977 * Otherwise a negative NFS4ERR value is returned if the operation
6978 * failed or the state ID is not currently valid.
6980 static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6982 struct nfs4_exception exception = { };
6985 err = _nfs41_test_stateid(server, stateid);
6986 if (err != -NFS4ERR_DELAY)
6988 nfs4_handle_exception(server, err, &exception);
6989 } while (exception.retry);
6993 static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6995 struct nfs41_free_stateid_args args = {
6998 struct nfs41_free_stateid_res res;
6999 struct rpc_message msg = {
7000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
7006 dprintk("NFS call free_stateid %p\n", stateid);
7007 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
7008 status = nfs4_call_sync_sequence(server->client, server, &msg,
7009 &args.seq_args, &res.seq_res, 1);
7010 dprintk("NFS reply free_stateid: %d\n", status);
7015 * nfs41_free_stateid - perform a FREE_STATEID operation
7017 * @server: server / transport on which to perform the operation
7018 * @stateid: state ID to release
7020 * Returns NFS_OK if the server freed "stateid". Otherwise a
7021 * negative NFS4ERR value is returned.
7023 static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
7025 struct nfs4_exception exception = { };
7028 err = _nfs4_free_stateid(server, stateid);
7029 if (err != -NFS4ERR_DELAY)
7031 nfs4_handle_exception(server, err, &exception);
7032 } while (exception.retry);
7036 static bool nfs41_match_stateid(const nfs4_stateid *s1,
7037 const nfs4_stateid *s2)
7039 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
7042 if (s1->seqid == s2->seqid)
7044 if (s1->seqid == 0 || s2->seqid == 0)
7050 #endif /* CONFIG_NFS_V4_1 */
7052 static bool nfs4_match_stateid(const nfs4_stateid *s1,
7053 const nfs4_stateid *s2)
7055 return nfs4_stateid_match(s1, s2);
7059 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
7060 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7061 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7062 .recover_open = nfs4_open_reclaim,
7063 .recover_lock = nfs4_lock_reclaim,
7064 .establish_clid = nfs4_init_clientid,
7065 .get_clid_cred = nfs4_get_setclientid_cred,
7066 .detect_trunking = nfs40_discover_server_trunking,
7069 #if defined(CONFIG_NFS_V4_1)
7070 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
7071 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7072 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7073 .recover_open = nfs4_open_reclaim,
7074 .recover_lock = nfs4_lock_reclaim,
7075 .establish_clid = nfs41_init_clientid,
7076 .get_clid_cred = nfs4_get_exchange_id_cred,
7077 .reclaim_complete = nfs41_proc_reclaim_complete,
7078 .detect_trunking = nfs41_discover_server_trunking,
7080 #endif /* CONFIG_NFS_V4_1 */
7082 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
7083 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7084 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
7085 .recover_open = nfs4_open_expired,
7086 .recover_lock = nfs4_lock_expired,
7087 .establish_clid = nfs4_init_clientid,
7088 .get_clid_cred = nfs4_get_setclientid_cred,
7091 #if defined(CONFIG_NFS_V4_1)
7092 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
7093 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7094 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
7095 .recover_open = nfs41_open_expired,
7096 .recover_lock = nfs41_lock_expired,
7097 .establish_clid = nfs41_init_clientid,
7098 .get_clid_cred = nfs4_get_exchange_id_cred,
7100 #endif /* CONFIG_NFS_V4_1 */
7102 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
7103 .sched_state_renewal = nfs4_proc_async_renew,
7104 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
7105 .renew_lease = nfs4_proc_renew,
7108 #if defined(CONFIG_NFS_V4_1)
7109 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
7110 .sched_state_renewal = nfs41_proc_async_sequence,
7111 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
7112 .renew_lease = nfs4_proc_sequence,
7116 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7118 .call_sync = _nfs4_call_sync,
7119 .match_stateid = nfs4_match_stateid,
7120 .find_root_sec = nfs4_find_root_sec,
7121 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7122 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7123 .state_renewal_ops = &nfs40_state_renewal_ops,
7126 #if defined(CONFIG_NFS_V4_1)
7127 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7129 .call_sync = _nfs4_call_sync_session,
7130 .match_stateid = nfs41_match_stateid,
7131 .find_root_sec = nfs41_find_root_sec,
7132 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7133 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7134 .state_renewal_ops = &nfs41_state_renewal_ops,
7138 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7139 [0] = &nfs_v4_0_minor_ops,
7140 #if defined(CONFIG_NFS_V4_1)
7141 [1] = &nfs_v4_1_minor_ops,
7145 const struct inode_operations nfs4_dir_inode_operations = {
7146 .create = nfs_create,
7147 .lookup = nfs_lookup,
7148 .atomic_open = nfs_atomic_open,
7150 .unlink = nfs_unlink,
7151 .symlink = nfs_symlink,
7155 .rename = nfs_rename,
7156 .permission = nfs_permission,
7157 .getattr = nfs_getattr,
7158 .setattr = nfs_setattr,
7159 .getxattr = generic_getxattr,
7160 .setxattr = generic_setxattr,
7161 .listxattr = generic_listxattr,
7162 .removexattr = generic_removexattr,
7165 static const struct inode_operations nfs4_file_inode_operations = {
7166 .permission = nfs_permission,
7167 .getattr = nfs_getattr,
7168 .setattr = nfs_setattr,
7169 .getxattr = generic_getxattr,
7170 .setxattr = generic_setxattr,
7171 .listxattr = generic_listxattr,
7172 .removexattr = generic_removexattr,
7175 const struct nfs_rpc_ops nfs_v4_clientops = {
7176 .version = 4, /* protocol version */
7177 .dentry_ops = &nfs4_dentry_operations,
7178 .dir_inode_ops = &nfs4_dir_inode_operations,
7179 .file_inode_ops = &nfs4_file_inode_operations,
7180 .file_ops = &nfs4_file_operations,
7181 .getroot = nfs4_proc_get_root,
7182 .submount = nfs4_submount,
7183 .try_mount = nfs4_try_mount,
7184 .getattr = nfs4_proc_getattr,
7185 .setattr = nfs4_proc_setattr,
7186 .lookup = nfs4_proc_lookup,
7187 .access = nfs4_proc_access,
7188 .readlink = nfs4_proc_readlink,
7189 .create = nfs4_proc_create,
7190 .remove = nfs4_proc_remove,
7191 .unlink_setup = nfs4_proc_unlink_setup,
7192 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
7193 .unlink_done = nfs4_proc_unlink_done,
7194 .rename = nfs4_proc_rename,
7195 .rename_setup = nfs4_proc_rename_setup,
7196 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
7197 .rename_done = nfs4_proc_rename_done,
7198 .link = nfs4_proc_link,
7199 .symlink = nfs4_proc_symlink,
7200 .mkdir = nfs4_proc_mkdir,
7201 .rmdir = nfs4_proc_remove,
7202 .readdir = nfs4_proc_readdir,
7203 .mknod = nfs4_proc_mknod,
7204 .statfs = nfs4_proc_statfs,
7205 .fsinfo = nfs4_proc_fsinfo,
7206 .pathconf = nfs4_proc_pathconf,
7207 .set_capabilities = nfs4_server_capabilities,
7208 .decode_dirent = nfs4_decode_dirent,
7209 .read_setup = nfs4_proc_read_setup,
7210 .read_pageio_init = pnfs_pageio_init_read,
7211 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
7212 .read_done = nfs4_read_done,
7213 .write_setup = nfs4_proc_write_setup,
7214 .write_pageio_init = pnfs_pageio_init_write,
7215 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
7216 .write_done = nfs4_write_done,
7217 .commit_setup = nfs4_proc_commit_setup,
7218 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
7219 .commit_done = nfs4_commit_done,
7220 .lock = nfs4_proc_lock,
7221 .clear_acl_cache = nfs4_zap_acl_attr,
7222 .close_context = nfs4_close_context,
7223 .open_context = nfs4_atomic_open,
7224 .have_delegation = nfs4_have_delegation,
7225 .return_delegation = nfs4_inode_return_delegation,
7226 .alloc_client = nfs4_alloc_client,
7227 .init_client = nfs4_init_client,
7228 .free_client = nfs4_free_client,
7229 .create_server = nfs4_create_server,
7230 .clone_server = nfs_clone_server,
7233 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7234 .prefix = XATTR_NAME_NFSV4_ACL,
7235 .list = nfs4_xattr_list_nfs4_acl,
7236 .get = nfs4_xattr_get_nfs4_acl,
7237 .set = nfs4_xattr_set_nfs4_acl,
7240 const struct xattr_handler *nfs4_xattr_handlers[] = {
7241 &nfs4_xattr_nfs4_acl_handler,