4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/nfs_idmap.h>
55 #include <linux/sunrpc/bc_xprt.h>
56 #include <linux/xattr.h>
57 #include <linux/utsname.h>
58 #include <linux/freezer.h>
61 #include "delegation.h"
68 #define NFSDBG_FACILITY NFSDBG_PROC
70 #define NFS4_POLL_RETRY_MIN (HZ/10)
71 #define NFS4_POLL_RETRY_MAX (15*HZ)
73 #define NFS4_MAX_LOOP_ON_RECOVER (10)
76 static int _nfs4_proc_open(struct nfs4_opendata *data);
77 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
78 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
79 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
80 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
81 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
82 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
83 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
84 struct nfs_fattr *fattr, struct iattr *sattr,
85 struct nfs4_state *state);
86 #ifdef CONFIG_NFS_V4_1
87 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
88 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
90 /* Prevent leaks of NFSv4 errors into userland */
91 static int nfs4_map_errors(int err)
96 case -NFS4ERR_RESOURCE:
98 case -NFS4ERR_WRONGSEC:
100 case -NFS4ERR_BADOWNER:
101 case -NFS4ERR_BADNAME:
103 case -NFS4ERR_SHARE_DENIED:
105 case -NFS4ERR_MINOR_VERS_MISMATCH:
106 return -EPROTONOSUPPORT;
107 case -NFS4ERR_ACCESS:
110 dprintk("%s could not handle NFSv4 error %d\n",
118 * This is our standard bitmap for GETATTR requests.
120 const u32 nfs4_fattr_bitmap[3] = {
122 | FATTR4_WORD0_CHANGE
125 | FATTR4_WORD0_FILEID,
127 | FATTR4_WORD1_NUMLINKS
129 | FATTR4_WORD1_OWNER_GROUP
130 | FATTR4_WORD1_RAWDEV
131 | FATTR4_WORD1_SPACE_USED
132 | FATTR4_WORD1_TIME_ACCESS
133 | FATTR4_WORD1_TIME_METADATA
134 | FATTR4_WORD1_TIME_MODIFY
137 static const u32 nfs4_pnfs_open_bitmap[3] = {
139 | FATTR4_WORD0_CHANGE
142 | FATTR4_WORD0_FILEID,
144 | FATTR4_WORD1_NUMLINKS
146 | FATTR4_WORD1_OWNER_GROUP
147 | FATTR4_WORD1_RAWDEV
148 | FATTR4_WORD1_SPACE_USED
149 | FATTR4_WORD1_TIME_ACCESS
150 | FATTR4_WORD1_TIME_METADATA
151 | FATTR4_WORD1_TIME_MODIFY,
152 FATTR4_WORD2_MDSTHRESHOLD
155 static const u32 nfs4_open_noattr_bitmap[3] = {
157 | FATTR4_WORD0_CHANGE
158 | FATTR4_WORD0_FILEID,
161 const u32 nfs4_statfs_bitmap[2] = {
162 FATTR4_WORD0_FILES_AVAIL
163 | FATTR4_WORD0_FILES_FREE
164 | FATTR4_WORD0_FILES_TOTAL,
165 FATTR4_WORD1_SPACE_AVAIL
166 | FATTR4_WORD1_SPACE_FREE
167 | FATTR4_WORD1_SPACE_TOTAL
170 const u32 nfs4_pathconf_bitmap[2] = {
172 | FATTR4_WORD0_MAXNAME,
176 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
177 | FATTR4_WORD0_MAXREAD
178 | FATTR4_WORD0_MAXWRITE
179 | FATTR4_WORD0_LEASE_TIME,
180 FATTR4_WORD1_TIME_DELTA
181 | FATTR4_WORD1_FS_LAYOUT_TYPES,
182 FATTR4_WORD2_LAYOUT_BLKSIZE
185 const u32 nfs4_fs_locations_bitmap[2] = {
187 | FATTR4_WORD0_CHANGE
190 | FATTR4_WORD0_FILEID
191 | FATTR4_WORD0_FS_LOCATIONS,
193 | FATTR4_WORD1_NUMLINKS
195 | FATTR4_WORD1_OWNER_GROUP
196 | FATTR4_WORD1_RAWDEV
197 | FATTR4_WORD1_SPACE_USED
198 | FATTR4_WORD1_TIME_ACCESS
199 | FATTR4_WORD1_TIME_METADATA
200 | FATTR4_WORD1_TIME_MODIFY
201 | FATTR4_WORD1_MOUNTED_ON_FILEID
204 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
205 struct nfs4_readdir_arg *readdir)
209 BUG_ON(readdir->count < 80);
211 readdir->cookie = cookie;
212 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
217 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
222 * NFSv4 servers do not return entries for '.' and '..'
223 * Therefore, we fake these entries here. We let '.'
224 * have cookie 0 and '..' have cookie 1. Note that
225 * when talking to the server, we always send cookie 0
228 start = p = kmap_atomic(*readdir->pages);
231 *p++ = xdr_one; /* next */
232 *p++ = xdr_zero; /* cookie, first word */
233 *p++ = xdr_one; /* cookie, second word */
234 *p++ = xdr_one; /* entry len */
235 memcpy(p, ".\0\0\0", 4); /* entry */
237 *p++ = xdr_one; /* bitmap length */
238 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
239 *p++ = htonl(8); /* attribute buffer length */
240 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
243 *p++ = xdr_one; /* next */
244 *p++ = xdr_zero; /* cookie, first word */
245 *p++ = xdr_two; /* cookie, second word */
246 *p++ = xdr_two; /* entry len */
247 memcpy(p, "..\0\0", 4); /* entry */
249 *p++ = xdr_one; /* bitmap length */
250 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
251 *p++ = htonl(8); /* attribute buffer length */
252 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
254 readdir->pgbase = (char *)p - (char *)start;
255 readdir->count -= readdir->pgbase;
256 kunmap_atomic(start);
259 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
265 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
266 nfs_wait_bit_killable, TASK_KILLABLE);
270 if (clp->cl_cons_state < 0)
271 return clp->cl_cons_state;
275 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
282 *timeout = NFS4_POLL_RETRY_MIN;
283 if (*timeout > NFS4_POLL_RETRY_MAX)
284 *timeout = NFS4_POLL_RETRY_MAX;
285 freezable_schedule_timeout_killable(*timeout);
286 if (fatal_signal_pending(current))
292 /* This is the error handling routine for processes that are allowed
295 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
297 struct nfs_client *clp = server->nfs_client;
298 struct nfs4_state *state = exception->state;
299 struct inode *inode = exception->inode;
302 exception->retry = 0;
306 case -NFS4ERR_OPENMODE:
307 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
308 nfs4_inode_return_delegation(inode);
309 exception->retry = 1;
314 nfs4_schedule_stateid_recovery(server, state);
315 goto wait_on_recovery;
316 case -NFS4ERR_DELEG_REVOKED:
317 case -NFS4ERR_ADMIN_REVOKED:
318 case -NFS4ERR_BAD_STATEID:
321 nfs_remove_bad_delegation(state->inode);
322 nfs4_schedule_stateid_recovery(server, state);
323 goto wait_on_recovery;
324 case -NFS4ERR_EXPIRED:
326 nfs4_schedule_stateid_recovery(server, state);
327 case -NFS4ERR_STALE_STATEID:
328 case -NFS4ERR_STALE_CLIENTID:
329 nfs4_schedule_lease_recovery(clp);
330 goto wait_on_recovery;
331 #if defined(CONFIG_NFS_V4_1)
332 case -NFS4ERR_BADSESSION:
333 case -NFS4ERR_BADSLOT:
334 case -NFS4ERR_BAD_HIGH_SLOT:
335 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
336 case -NFS4ERR_DEADSESSION:
337 case -NFS4ERR_SEQ_FALSE_RETRY:
338 case -NFS4ERR_SEQ_MISORDERED:
339 dprintk("%s ERROR: %d Reset session\n", __func__,
341 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
342 exception->retry = 1;
344 #endif /* defined(CONFIG_NFS_V4_1) */
345 case -NFS4ERR_FILE_OPEN:
346 if (exception->timeout > HZ) {
347 /* We have retried a decent amount, time to
356 ret = nfs4_delay(server->client, &exception->timeout);
359 case -NFS4ERR_RETRY_UNCACHED_REP:
360 case -NFS4ERR_OLD_STATEID:
361 exception->retry = 1;
363 case -NFS4ERR_BADOWNER:
364 /* The following works around a Linux server bug! */
365 case -NFS4ERR_BADNAME:
366 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
367 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
368 exception->retry = 1;
369 printk(KERN_WARNING "NFS: v4 server %s "
370 "does not accept raw "
372 "Reenabling the idmapper.\n",
373 server->nfs_client->cl_hostname);
376 /* We failed to handle the error */
377 return nfs4_map_errors(ret);
379 ret = nfs4_wait_clnt_recover(clp);
381 exception->retry = 1;
386 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
388 spin_lock(&clp->cl_lock);
389 if (time_before(clp->cl_last_renewal,timestamp))
390 clp->cl_last_renewal = timestamp;
391 spin_unlock(&clp->cl_lock);
394 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
396 do_renew_lease(server->nfs_client, timestamp);
399 #if defined(CONFIG_NFS_V4_1)
402 * nfs4_free_slot - free a slot and efficiently update slot table.
404 * freeing a slot is trivially done by clearing its respective bit
406 * If the freed slotid equals highest_used_slotid we want to update it
407 * so that the server would be able to size down the slot table if needed,
408 * otherwise we know that the highest_used_slotid is still in use.
409 * When updating highest_used_slotid there may be "holes" in the bitmap
410 * so we need to scan down from highest_used_slotid to 0 looking for the now
411 * highest slotid in use.
412 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
414 * Must be called while holding tbl->slot_tbl_lock
417 nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
419 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
420 /* clear used bit in bitmap */
421 __clear_bit(slotid, tbl->used_slots);
423 /* update highest_used_slotid when it is freed */
424 if (slotid == tbl->highest_used_slotid) {
425 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
426 if (slotid < tbl->max_slots)
427 tbl->highest_used_slotid = slotid;
429 tbl->highest_used_slotid = NFS4_NO_SLOT;
431 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
432 slotid, tbl->highest_used_slotid);
435 bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
437 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
442 * Signal state manager thread if session fore channel is drained
444 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
446 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
447 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
448 nfs4_set_task_privileged, NULL);
452 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
455 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
456 complete(&ses->fc_slot_table.complete);
460 * Signal state manager thread if session back channel is drained
462 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
464 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
465 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
467 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
468 complete(&ses->bc_slot_table.complete);
471 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
473 struct nfs4_slot_table *tbl;
475 tbl = &res->sr_session->fc_slot_table;
477 /* just wake up the next guy waiting since
478 * we may have not consumed a slot after all */
479 dprintk("%s: No slot\n", __func__);
483 spin_lock(&tbl->slot_tbl_lock);
484 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
485 nfs4_check_drain_fc_complete(res->sr_session);
486 spin_unlock(&tbl->slot_tbl_lock);
490 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
492 unsigned long timestamp;
493 struct nfs_client *clp;
496 * sr_status remains 1 if an RPC level error occurred. The server
497 * may or may not have processed the sequence operation..
498 * Proceed as if the server received and processed the sequence
501 if (res->sr_status == 1)
502 res->sr_status = NFS_OK;
504 /* don't increment the sequence number if the task wasn't sent */
505 if (!RPC_WAS_SENT(task))
508 /* Check the SEQUENCE operation status */
509 switch (res->sr_status) {
511 /* Update the slot's sequence and clientid lease timer */
512 ++res->sr_slot->seq_nr;
513 timestamp = res->sr_renewal_time;
514 clp = res->sr_session->clp;
515 do_renew_lease(clp, timestamp);
516 /* Check sequence flags */
517 if (res->sr_status_flags != 0)
518 nfs4_schedule_lease_recovery(clp);
521 /* The server detected a resend of the RPC call and
522 * returned NFS4ERR_DELAY as per Section 2.10.6.2
525 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
527 res->sr_slot - res->sr_session->fc_slot_table.slots,
528 res->sr_slot->seq_nr);
531 /* Just update the slot sequence no. */
532 ++res->sr_slot->seq_nr;
535 /* The session may be reset by one of the error handlers. */
536 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
537 nfs41_sequence_free_slot(res);
540 if (!rpc_restart_call(task))
542 rpc_delay(task, NFS4_POLL_RETRY_MAX);
546 static int nfs4_sequence_done(struct rpc_task *task,
547 struct nfs4_sequence_res *res)
549 if (res->sr_session == NULL)
551 return nfs41_sequence_done(task, res);
555 * nfs4_find_slot - efficiently look for a free slot
557 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
558 * If found, we mark the slot as used, update the highest_used_slotid,
559 * and respectively set up the sequence operation args.
560 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
562 * Note: must be called with under the slot_tbl_lock.
565 nfs4_find_slot(struct nfs4_slot_table *tbl)
568 u32 ret_id = NFS4_NO_SLOT;
570 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
571 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
573 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
574 if (slotid >= tbl->max_slots)
576 __set_bit(slotid, tbl->used_slots);
577 if (slotid > tbl->highest_used_slotid ||
578 tbl->highest_used_slotid == NFS4_NO_SLOT)
579 tbl->highest_used_slotid = slotid;
582 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
583 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
587 static void nfs41_init_sequence(struct nfs4_sequence_args *args,
588 struct nfs4_sequence_res *res, int cache_reply)
590 args->sa_session = NULL;
591 args->sa_cache_this = 0;
593 args->sa_cache_this = 1;
594 res->sr_session = NULL;
598 int nfs41_setup_sequence(struct nfs4_session *session,
599 struct nfs4_sequence_args *args,
600 struct nfs4_sequence_res *res,
601 struct rpc_task *task)
603 struct nfs4_slot *slot;
604 struct nfs4_slot_table *tbl;
607 dprintk("--> %s\n", __func__);
608 /* slot already allocated? */
609 if (res->sr_slot != NULL)
612 tbl = &session->fc_slot_table;
614 spin_lock(&tbl->slot_tbl_lock);
615 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
616 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
617 /* The state manager will wait until the slot table is empty */
618 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
619 spin_unlock(&tbl->slot_tbl_lock);
620 dprintk("%s session is draining\n", __func__);
624 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
625 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
626 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
627 spin_unlock(&tbl->slot_tbl_lock);
628 dprintk("%s enforce FIFO order\n", __func__);
632 slotid = nfs4_find_slot(tbl);
633 if (slotid == NFS4_NO_SLOT) {
634 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
635 spin_unlock(&tbl->slot_tbl_lock);
636 dprintk("<-- %s: no free slots\n", __func__);
639 spin_unlock(&tbl->slot_tbl_lock);
641 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
642 slot = tbl->slots + slotid;
643 args->sa_session = session;
644 args->sa_slotid = slotid;
646 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
648 res->sr_session = session;
650 res->sr_renewal_time = jiffies;
651 res->sr_status_flags = 0;
653 * sr_status is only set in decode_sequence, and so will remain
654 * set to 1 if an rpc level failure occurs.
659 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
661 int nfs4_setup_sequence(const struct nfs_server *server,
662 struct nfs4_sequence_args *args,
663 struct nfs4_sequence_res *res,
664 struct rpc_task *task)
666 struct nfs4_session *session = nfs4_get_session(server);
672 dprintk("--> %s clp %p session %p sr_slot %td\n",
673 __func__, session->clp, session, res->sr_slot ?
674 res->sr_slot - session->fc_slot_table.slots : -1);
676 ret = nfs41_setup_sequence(session, args, res, task);
678 dprintk("<-- %s status=%d\n", __func__, ret);
682 struct nfs41_call_sync_data {
683 const struct nfs_server *seq_server;
684 struct nfs4_sequence_args *seq_args;
685 struct nfs4_sequence_res *seq_res;
688 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
690 struct nfs41_call_sync_data *data = calldata;
692 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
694 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
695 data->seq_res, task))
697 rpc_call_start(task);
700 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
702 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
703 nfs41_call_sync_prepare(task, calldata);
706 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
708 struct nfs41_call_sync_data *data = calldata;
710 nfs41_sequence_done(task, data->seq_res);
713 static const struct rpc_call_ops nfs41_call_sync_ops = {
714 .rpc_call_prepare = nfs41_call_sync_prepare,
715 .rpc_call_done = nfs41_call_sync_done,
718 static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
719 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
720 .rpc_call_done = nfs41_call_sync_done,
723 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
724 struct nfs_server *server,
725 struct rpc_message *msg,
726 struct nfs4_sequence_args *args,
727 struct nfs4_sequence_res *res,
731 struct rpc_task *task;
732 struct nfs41_call_sync_data data = {
733 .seq_server = server,
737 struct rpc_task_setup task_setup = {
740 .callback_ops = &nfs41_call_sync_ops,
741 .callback_data = &data
745 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
746 task = rpc_run_task(&task_setup);
750 ret = task->tk_status;
756 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
757 struct nfs_server *server,
758 struct rpc_message *msg,
759 struct nfs4_sequence_args *args,
760 struct nfs4_sequence_res *res,
763 nfs41_init_sequence(args, res, cache_reply);
764 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
769 void nfs41_init_sequence(struct nfs4_sequence_args *args,
770 struct nfs4_sequence_res *res, int cache_reply)
774 static int nfs4_sequence_done(struct rpc_task *task,
775 struct nfs4_sequence_res *res)
779 #endif /* CONFIG_NFS_V4_1 */
781 int _nfs4_call_sync(struct rpc_clnt *clnt,
782 struct nfs_server *server,
783 struct rpc_message *msg,
784 struct nfs4_sequence_args *args,
785 struct nfs4_sequence_res *res,
788 nfs41_init_sequence(args, res, cache_reply);
789 return rpc_call_sync(clnt, msg, 0);
793 int nfs4_call_sync(struct rpc_clnt *clnt,
794 struct nfs_server *server,
795 struct rpc_message *msg,
796 struct nfs4_sequence_args *args,
797 struct nfs4_sequence_res *res,
800 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
801 args, res, cache_reply);
804 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
806 struct nfs_inode *nfsi = NFS_I(dir);
808 spin_lock(&dir->i_lock);
809 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
810 if (!cinfo->atomic || cinfo->before != dir->i_version)
811 nfs_force_lookup_revalidate(dir);
812 dir->i_version = cinfo->after;
813 spin_unlock(&dir->i_lock);
816 struct nfs4_opendata {
818 struct nfs_openargs o_arg;
819 struct nfs_openres o_res;
820 struct nfs_open_confirmargs c_arg;
821 struct nfs_open_confirmres c_res;
822 struct nfs4_string owner_name;
823 struct nfs4_string group_name;
824 struct nfs_fattr f_attr;
826 struct dentry *dentry;
827 struct nfs4_state_owner *owner;
828 struct nfs4_state *state;
830 unsigned long timestamp;
831 unsigned int rpc_done : 1;
837 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
839 p->o_res.f_attr = &p->f_attr;
840 p->o_res.seqid = p->o_arg.seqid;
841 p->c_res.seqid = p->c_arg.seqid;
842 p->o_res.server = p->o_arg.server;
843 p->o_res.access_request = p->o_arg.access;
844 nfs_fattr_init(&p->f_attr);
845 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
848 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
849 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
850 const struct iattr *attrs,
853 struct dentry *parent = dget_parent(dentry);
854 struct inode *dir = parent->d_inode;
855 struct nfs_server *server = NFS_SERVER(dir);
856 struct nfs4_opendata *p;
858 p = kzalloc(sizeof(*p), gfp_mask);
861 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
862 if (p->o_arg.seqid == NULL)
864 nfs_sb_active(dentry->d_sb);
865 p->dentry = dget(dentry);
868 atomic_inc(&sp->so_count);
869 p->o_arg.fh = NFS_FH(dir);
870 p->o_arg.open_flags = flags;
871 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
872 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
873 * will return permission denied for all bits until close */
874 if (!(flags & O_EXCL)) {
875 /* ask server to check for all possible rights as results
877 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
878 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
880 p->o_arg.clientid = server->nfs_client->cl_clientid;
881 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
882 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
883 p->o_arg.name = &dentry->d_name;
884 p->o_arg.server = server;
885 p->o_arg.bitmask = server->attr_bitmask;
886 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
887 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
888 if (attrs != NULL && attrs->ia_valid != 0) {
891 p->o_arg.u.attrs = &p->attrs;
892 memcpy(&p->attrs, attrs, sizeof(p->attrs));
895 verf[1] = current->pid;
896 memcpy(p->o_arg.u.verifier.data, verf,
897 sizeof(p->o_arg.u.verifier.data));
899 p->c_arg.fh = &p->o_res.fh;
900 p->c_arg.stateid = &p->o_res.stateid;
901 p->c_arg.seqid = p->o_arg.seqid;
902 nfs4_init_opendata_res(p);
912 static void nfs4_opendata_free(struct kref *kref)
914 struct nfs4_opendata *p = container_of(kref,
915 struct nfs4_opendata, kref);
916 struct super_block *sb = p->dentry->d_sb;
918 nfs_free_seqid(p->o_arg.seqid);
919 if (p->state != NULL)
920 nfs4_put_open_state(p->state);
921 nfs4_put_state_owner(p->owner);
925 nfs_fattr_free_names(&p->f_attr);
929 static void nfs4_opendata_put(struct nfs4_opendata *p)
932 kref_put(&p->kref, nfs4_opendata_free);
935 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
939 ret = rpc_wait_for_completion_task(task);
943 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
947 if (open_mode & (O_EXCL|O_TRUNC))
949 switch (mode & (FMODE_READ|FMODE_WRITE)) {
951 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
952 && state->n_rdonly != 0;
955 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
956 && state->n_wronly != 0;
958 case FMODE_READ|FMODE_WRITE:
959 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
960 && state->n_rdwr != 0;
966 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
968 if (delegation == NULL)
970 if ((delegation->type & fmode) != fmode)
972 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
974 nfs_mark_delegation_referenced(delegation);
978 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
987 case FMODE_READ|FMODE_WRITE:
990 nfs4_state_set_mode_locked(state, state->state | fmode);
993 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
995 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
996 nfs4_stateid_copy(&state->stateid, stateid);
997 nfs4_stateid_copy(&state->open_stateid, stateid);
1000 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1003 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1005 case FMODE_READ|FMODE_WRITE:
1006 set_bit(NFS_O_RDWR_STATE, &state->flags);
1010 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1012 write_seqlock(&state->seqlock);
1013 nfs_set_open_stateid_locked(state, stateid, fmode);
1014 write_sequnlock(&state->seqlock);
1017 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1020 * Protect the call to nfs4_state_set_mode_locked and
1021 * serialise the stateid update
1023 write_seqlock(&state->seqlock);
1024 if (deleg_stateid != NULL) {
1025 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1026 set_bit(NFS_DELEGATED_STATE, &state->flags);
1028 if (open_stateid != NULL)
1029 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1030 write_sequnlock(&state->seqlock);
1031 spin_lock(&state->owner->so_lock);
1032 update_open_stateflags(state, fmode);
1033 spin_unlock(&state->owner->so_lock);
1036 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1038 struct nfs_inode *nfsi = NFS_I(state->inode);
1039 struct nfs_delegation *deleg_cur;
1042 fmode &= (FMODE_READ|FMODE_WRITE);
1045 deleg_cur = rcu_dereference(nfsi->delegation);
1046 if (deleg_cur == NULL)
1049 spin_lock(&deleg_cur->lock);
1050 if (nfsi->delegation != deleg_cur ||
1051 (deleg_cur->type & fmode) != fmode)
1052 goto no_delegation_unlock;
1054 if (delegation == NULL)
1055 delegation = &deleg_cur->stateid;
1056 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1057 goto no_delegation_unlock;
1059 nfs_mark_delegation_referenced(deleg_cur);
1060 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1062 no_delegation_unlock:
1063 spin_unlock(&deleg_cur->lock);
1067 if (!ret && open_stateid != NULL) {
1068 __update_open_stateid(state, open_stateid, NULL, fmode);
1076 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1078 struct nfs_delegation *delegation;
1081 delegation = rcu_dereference(NFS_I(inode)->delegation);
1082 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1087 nfs4_inode_return_delegation(inode);
1090 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1092 struct nfs4_state *state = opendata->state;
1093 struct nfs_inode *nfsi = NFS_I(state->inode);
1094 struct nfs_delegation *delegation;
1095 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
1096 fmode_t fmode = opendata->o_arg.fmode;
1097 nfs4_stateid stateid;
1101 if (can_open_cached(state, fmode, open_mode)) {
1102 spin_lock(&state->owner->so_lock);
1103 if (can_open_cached(state, fmode, open_mode)) {
1104 update_open_stateflags(state, fmode);
1105 spin_unlock(&state->owner->so_lock);
1106 goto out_return_state;
1108 spin_unlock(&state->owner->so_lock);
1111 delegation = rcu_dereference(nfsi->delegation);
1112 if (!can_open_delegated(delegation, fmode)) {
1116 /* Save the delegation */
1117 nfs4_stateid_copy(&stateid, &delegation->stateid);
1119 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1124 /* Try to update the stateid using the delegation */
1125 if (update_open_stateid(state, NULL, &stateid, fmode))
1126 goto out_return_state;
1129 return ERR_PTR(ret);
1131 atomic_inc(&state->count);
1136 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1138 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1139 struct nfs_delegation *delegation;
1140 int delegation_flags = 0;
1143 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1145 delegation_flags = delegation->flags;
1147 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1148 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1149 "returning a delegation for "
1150 "OPEN(CLAIM_DELEGATE_CUR)\n",
1152 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1153 nfs_inode_set_delegation(state->inode,
1154 data->owner->so_cred,
1157 nfs_inode_reclaim_delegation(state->inode,
1158 data->owner->so_cred,
1163 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1164 * and update the nfs4_state.
1166 static struct nfs4_state *
1167 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1169 struct inode *inode = data->state->inode;
1170 struct nfs4_state *state = data->state;
1173 if (!data->rpc_done) {
1174 ret = data->rpc_status;
1179 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1180 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1181 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1185 state = nfs4_get_open_state(inode, data->owner);
1189 ret = nfs_refresh_inode(inode, &data->f_attr);
1193 if (data->o_res.delegation_type != 0)
1194 nfs4_opendata_check_deleg(data, state);
1195 update_open_stateid(state, &data->o_res.stateid, NULL,
1200 return ERR_PTR(ret);
1204 static struct nfs4_state *
1205 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1207 struct inode *inode;
1208 struct nfs4_state *state = NULL;
1211 if (!data->rpc_done) {
1212 state = nfs4_try_open_cached(data);
1217 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1219 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1220 ret = PTR_ERR(inode);
1224 state = nfs4_get_open_state(inode, data->owner);
1227 if (data->o_res.delegation_type != 0)
1228 nfs4_opendata_check_deleg(data, state);
1229 update_open_stateid(state, &data->o_res.stateid, NULL,
1237 return ERR_PTR(ret);
1240 static struct nfs4_state *
1241 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1243 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1244 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1245 return _nfs4_opendata_to_nfs4_state(data);
1248 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1250 struct nfs_inode *nfsi = NFS_I(state->inode);
1251 struct nfs_open_context *ctx;
1253 spin_lock(&state->inode->i_lock);
1254 list_for_each_entry(ctx, &nfsi->open_files, list) {
1255 if (ctx->state != state)
1257 get_nfs_open_context(ctx);
1258 spin_unlock(&state->inode->i_lock);
1261 spin_unlock(&state->inode->i_lock);
1262 return ERR_PTR(-ENOENT);
1265 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1267 struct nfs4_opendata *opendata;
1269 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1270 if (opendata == NULL)
1271 return ERR_PTR(-ENOMEM);
1272 opendata->state = state;
1273 atomic_inc(&state->count);
1277 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1279 struct nfs4_state *newstate;
1282 opendata->o_arg.open_flags = 0;
1283 opendata->o_arg.fmode = fmode;
1284 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1285 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1286 nfs4_init_opendata_res(opendata);
1287 ret = _nfs4_recover_proc_open(opendata);
1290 newstate = nfs4_opendata_to_nfs4_state(opendata);
1291 if (IS_ERR(newstate))
1292 return PTR_ERR(newstate);
1293 nfs4_close_state(newstate, fmode);
1298 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1300 struct nfs4_state *newstate;
1303 /* memory barrier prior to reading state->n_* */
1304 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1306 if (state->n_rdwr != 0) {
1307 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1308 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1311 if (newstate != state)
1314 if (state->n_wronly != 0) {
1315 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1316 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1319 if (newstate != state)
1322 if (state->n_rdonly != 0) {
1323 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1324 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1327 if (newstate != state)
1331 * We may have performed cached opens for all three recoveries.
1332 * Check if we need to update the current stateid.
1334 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1335 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1336 write_seqlock(&state->seqlock);
1337 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1338 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1339 write_sequnlock(&state->seqlock);
1346 * reclaim state on the server after a reboot.
1348 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1350 struct nfs_delegation *delegation;
1351 struct nfs4_opendata *opendata;
1352 fmode_t delegation_type = 0;
1355 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1356 if (IS_ERR(opendata))
1357 return PTR_ERR(opendata);
1358 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1359 opendata->o_arg.fh = NFS_FH(state->inode);
1361 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1362 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1363 delegation_type = delegation->type;
1365 opendata->o_arg.u.delegation_type = delegation_type;
1366 status = nfs4_open_recover(opendata, state);
1367 nfs4_opendata_put(opendata);
1371 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1373 struct nfs_server *server = NFS_SERVER(state->inode);
1374 struct nfs4_exception exception = { };
1377 err = _nfs4_do_open_reclaim(ctx, state);
1378 if (err != -NFS4ERR_DELAY)
1380 nfs4_handle_exception(server, err, &exception);
1381 } while (exception.retry);
1385 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1387 struct nfs_open_context *ctx;
1390 ctx = nfs4_state_find_open_context(state);
1392 return PTR_ERR(ctx);
1393 ret = nfs4_do_open_reclaim(ctx, state);
1394 put_nfs_open_context(ctx);
1398 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1400 struct nfs4_opendata *opendata;
1403 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1404 if (IS_ERR(opendata))
1405 return PTR_ERR(opendata);
1406 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1407 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1408 ret = nfs4_open_recover(opendata, state);
1409 nfs4_opendata_put(opendata);
1413 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1415 struct nfs4_exception exception = { };
1416 struct nfs_server *server = NFS_SERVER(state->inode);
1419 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1425 case -NFS4ERR_BADSESSION:
1426 case -NFS4ERR_BADSLOT:
1427 case -NFS4ERR_BAD_HIGH_SLOT:
1428 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1429 case -NFS4ERR_DEADSESSION:
1430 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1432 case -NFS4ERR_STALE_CLIENTID:
1433 case -NFS4ERR_STALE_STATEID:
1434 case -NFS4ERR_EXPIRED:
1435 /* Don't recall a delegation if it was lost */
1436 nfs4_schedule_lease_recovery(server->nfs_client);
1440 * The show must go on: exit, but mark the
1441 * stateid as needing recovery.
1443 case -NFS4ERR_DELEG_REVOKED:
1444 case -NFS4ERR_ADMIN_REVOKED:
1445 case -NFS4ERR_BAD_STATEID:
1446 nfs_inode_find_state_and_recover(state->inode,
1448 nfs4_schedule_stateid_recovery(server, state);
1451 * User RPCSEC_GSS context has expired.
1452 * We cannot recover this stateid now, so
1453 * skip it and allow recovery thread to
1460 err = nfs4_handle_exception(server, err, &exception);
1461 } while (exception.retry);
1466 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1468 struct nfs4_opendata *data = calldata;
1470 data->rpc_status = task->tk_status;
1471 if (data->rpc_status == 0) {
1472 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1473 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1474 renew_lease(data->o_res.server, data->timestamp);
1479 static void nfs4_open_confirm_release(void *calldata)
1481 struct nfs4_opendata *data = calldata;
1482 struct nfs4_state *state = NULL;
1484 /* If this request hasn't been cancelled, do nothing */
1485 if (data->cancelled == 0)
1487 /* In case of error, no cleanup! */
1488 if (!data->rpc_done)
1490 state = nfs4_opendata_to_nfs4_state(data);
1492 nfs4_close_state(state, data->o_arg.fmode);
1494 nfs4_opendata_put(data);
1497 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1498 .rpc_call_done = nfs4_open_confirm_done,
1499 .rpc_release = nfs4_open_confirm_release,
1503 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1505 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1507 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1508 struct rpc_task *task;
1509 struct rpc_message msg = {
1510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1511 .rpc_argp = &data->c_arg,
1512 .rpc_resp = &data->c_res,
1513 .rpc_cred = data->owner->so_cred,
1515 struct rpc_task_setup task_setup_data = {
1516 .rpc_client = server->client,
1517 .rpc_message = &msg,
1518 .callback_ops = &nfs4_open_confirm_ops,
1519 .callback_data = data,
1520 .workqueue = nfsiod_workqueue,
1521 .flags = RPC_TASK_ASYNC,
1525 kref_get(&data->kref);
1527 data->rpc_status = 0;
1528 data->timestamp = jiffies;
1529 task = rpc_run_task(&task_setup_data);
1531 return PTR_ERR(task);
1532 status = nfs4_wait_for_completion_rpc_task(task);
1534 data->cancelled = 1;
1537 status = data->rpc_status;
1542 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1544 struct nfs4_opendata *data = calldata;
1545 struct nfs4_state_owner *sp = data->owner;
1547 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1550 * Check if we still need to send an OPEN call, or if we can use
1551 * a delegation instead.
1553 if (data->state != NULL) {
1554 struct nfs_delegation *delegation;
1556 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1559 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1560 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1561 can_open_delegated(delegation, data->o_arg.fmode))
1562 goto unlock_no_action;
1565 /* Update client id. */
1566 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1567 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1568 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1569 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1570 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1572 data->timestamp = jiffies;
1573 if (nfs4_setup_sequence(data->o_arg.server,
1574 &data->o_arg.seq_args,
1575 &data->o_res.seq_res, task))
1577 rpc_call_start(task);
1582 task->tk_action = NULL;
1586 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1588 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1589 nfs4_open_prepare(task, calldata);
1592 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1594 struct nfs4_opendata *data = calldata;
1596 data->rpc_status = task->tk_status;
1598 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1601 if (task->tk_status == 0) {
1602 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1603 switch (data->o_res.f_attr->mode & S_IFMT) {
1607 data->rpc_status = -ELOOP;
1610 data->rpc_status = -EISDIR;
1613 data->rpc_status = -ENOTDIR;
1616 renew_lease(data->o_res.server, data->timestamp);
1617 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1618 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1623 static void nfs4_open_release(void *calldata)
1625 struct nfs4_opendata *data = calldata;
1626 struct nfs4_state *state = NULL;
1628 /* If this request hasn't been cancelled, do nothing */
1629 if (data->cancelled == 0)
1631 /* In case of error, no cleanup! */
1632 if (data->rpc_status != 0 || !data->rpc_done)
1634 /* In case we need an open_confirm, no cleanup! */
1635 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1637 state = nfs4_opendata_to_nfs4_state(data);
1639 nfs4_close_state(state, data->o_arg.fmode);
1641 nfs4_opendata_put(data);
1644 static const struct rpc_call_ops nfs4_open_ops = {
1645 .rpc_call_prepare = nfs4_open_prepare,
1646 .rpc_call_done = nfs4_open_done,
1647 .rpc_release = nfs4_open_release,
1650 static const struct rpc_call_ops nfs4_recover_open_ops = {
1651 .rpc_call_prepare = nfs4_recover_open_prepare,
1652 .rpc_call_done = nfs4_open_done,
1653 .rpc_release = nfs4_open_release,
1656 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1658 struct inode *dir = data->dir->d_inode;
1659 struct nfs_server *server = NFS_SERVER(dir);
1660 struct nfs_openargs *o_arg = &data->o_arg;
1661 struct nfs_openres *o_res = &data->o_res;
1662 struct rpc_task *task;
1663 struct rpc_message msg = {
1664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1667 .rpc_cred = data->owner->so_cred,
1669 struct rpc_task_setup task_setup_data = {
1670 .rpc_client = server->client,
1671 .rpc_message = &msg,
1672 .callback_ops = &nfs4_open_ops,
1673 .callback_data = data,
1674 .workqueue = nfsiod_workqueue,
1675 .flags = RPC_TASK_ASYNC,
1679 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1680 kref_get(&data->kref);
1682 data->rpc_status = 0;
1683 data->cancelled = 0;
1685 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1686 task = rpc_run_task(&task_setup_data);
1688 return PTR_ERR(task);
1689 status = nfs4_wait_for_completion_rpc_task(task);
1691 data->cancelled = 1;
1694 status = data->rpc_status;
1700 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1702 struct inode *dir = data->dir->d_inode;
1703 struct nfs_openres *o_res = &data->o_res;
1706 status = nfs4_run_open_task(data, 1);
1707 if (status != 0 || !data->rpc_done)
1710 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1712 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1713 status = _nfs4_proc_open_confirm(data);
1721 static int nfs4_opendata_access(struct rpc_cred *cred,
1722 struct nfs4_opendata *opendata,
1723 struct nfs4_state *state, fmode_t fmode)
1725 struct nfs_access_entry cache;
1728 /* access call failed or for some reason the server doesn't
1729 * support any access modes -- defer access call until later */
1730 if (opendata->o_res.access_supported == 0)
1734 /* don't check MAY_WRITE - a newly created file may not have
1735 * write mode bits, but POSIX allows the creating process to write */
1736 if (fmode & FMODE_READ)
1738 if (fmode & FMODE_EXEC)
1742 cache.jiffies = jiffies;
1743 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1744 nfs_access_add_cache(state->inode, &cache);
1746 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1749 /* even though OPEN succeeded, access is denied. Close the file */
1750 nfs4_close_state(state, fmode);
1751 return -NFS4ERR_ACCESS;
1755 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1757 static int _nfs4_proc_open(struct nfs4_opendata *data)
1759 struct inode *dir = data->dir->d_inode;
1760 struct nfs_server *server = NFS_SERVER(dir);
1761 struct nfs_openargs *o_arg = &data->o_arg;
1762 struct nfs_openres *o_res = &data->o_res;
1765 status = nfs4_run_open_task(data, 0);
1766 if (!data->rpc_done)
1769 if (status == -NFS4ERR_BADNAME &&
1770 !(o_arg->open_flags & O_CREAT))
1775 nfs_fattr_map_and_free_names(server, &data->f_attr);
1777 if (o_arg->open_flags & O_CREAT)
1778 update_changeattr(dir, &o_res->cinfo);
1779 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1780 server->caps &= ~NFS_CAP_POSIX_LOCK;
1781 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1782 status = _nfs4_proc_open_confirm(data);
1786 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1787 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1791 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1796 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1797 ret = nfs4_wait_clnt_recover(clp);
1800 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1801 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1803 nfs4_schedule_state_manager(clp);
1809 static int nfs4_recover_expired_lease(struct nfs_server *server)
1811 return nfs4_client_recover_expired_lease(server->nfs_client);
1816 * reclaim state on the server after a network partition.
1817 * Assumes caller holds the appropriate lock
1819 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1821 struct nfs4_opendata *opendata;
1824 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1825 if (IS_ERR(opendata))
1826 return PTR_ERR(opendata);
1827 ret = nfs4_open_recover(opendata, state);
1829 d_drop(ctx->dentry);
1830 nfs4_opendata_put(opendata);
1834 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1836 struct nfs_server *server = NFS_SERVER(state->inode);
1837 struct nfs4_exception exception = { };
1841 err = _nfs4_open_expired(ctx, state);
1845 case -NFS4ERR_GRACE:
1846 case -NFS4ERR_DELAY:
1847 nfs4_handle_exception(server, err, &exception);
1850 } while (exception.retry);
1855 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1857 struct nfs_open_context *ctx;
1860 ctx = nfs4_state_find_open_context(state);
1862 return PTR_ERR(ctx);
1863 ret = nfs4_do_open_expired(ctx, state);
1864 put_nfs_open_context(ctx);
1868 #if defined(CONFIG_NFS_V4_1)
1869 static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
1871 struct nfs_server *server = NFS_SERVER(state->inode);
1872 nfs4_stateid *stateid = &state->stateid;
1875 /* If a state reset has been done, test_stateid is unneeded */
1876 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1879 status = nfs41_test_stateid(server, stateid);
1880 if (status != NFS_OK) {
1881 /* Free the stateid unless the server explicitly
1882 * informs us the stateid is unrecognized. */
1883 if (status != -NFS4ERR_BAD_STATEID)
1884 nfs41_free_stateid(server, stateid);
1885 nfs_remove_bad_delegation(state->inode);
1887 write_seqlock(&state->seqlock);
1888 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1889 write_sequnlock(&state->seqlock);
1890 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1895 * nfs41_check_open_stateid - possibly free an open stateid
1897 * @state: NFSv4 state for an inode
1899 * Returns NFS_OK if recovery for this stateid is now finished.
1900 * Otherwise a negative NFS4ERR value is returned.
1902 static int nfs41_check_open_stateid(struct nfs4_state *state)
1904 struct nfs_server *server = NFS_SERVER(state->inode);
1905 nfs4_stateid *stateid = &state->open_stateid;
1908 /* If a state reset has been done, test_stateid is unneeded */
1909 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1910 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1911 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1912 return -NFS4ERR_BAD_STATEID;
1914 status = nfs41_test_stateid(server, stateid);
1915 if (status != NFS_OK) {
1916 /* Free the stateid unless the server explicitly
1917 * informs us the stateid is unrecognized. */
1918 if (status != -NFS4ERR_BAD_STATEID)
1919 nfs41_free_stateid(server, stateid);
1921 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1922 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1923 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1928 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1932 nfs41_clear_delegation_stateid(state);
1933 status = nfs41_check_open_stateid(state);
1934 if (status != NFS_OK)
1935 status = nfs4_open_expired(sp, state);
1941 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1942 * fields corresponding to attributes that were used to store the verifier.
1943 * Make sure we clobber those fields in the later setattr call
1945 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1947 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1948 !(sattr->ia_valid & ATTR_ATIME_SET))
1949 sattr->ia_valid |= ATTR_ATIME;
1951 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1952 !(sattr->ia_valid & ATTR_MTIME_SET))
1953 sattr->ia_valid |= ATTR_MTIME;
1957 * Returns a referenced nfs4_state
1959 static int _nfs4_do_open(struct inode *dir,
1960 struct dentry *dentry,
1963 struct iattr *sattr,
1964 struct rpc_cred *cred,
1965 struct nfs4_state **res,
1966 struct nfs4_threshold **ctx_th)
1968 struct nfs4_state_owner *sp;
1969 struct nfs4_state *state = NULL;
1970 struct nfs_server *server = NFS_SERVER(dir);
1971 struct nfs4_opendata *opendata;
1974 /* Protect against reboot recovery conflicts */
1976 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1978 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1981 status = nfs4_recover_expired_lease(server);
1983 goto err_put_state_owner;
1984 if (dentry->d_inode != NULL)
1985 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1987 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1988 if (opendata == NULL)
1989 goto err_put_state_owner;
1991 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1992 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1993 if (!opendata->f_attr.mdsthreshold)
1994 goto err_opendata_put;
1995 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
1997 if (dentry->d_inode != NULL)
1998 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
2000 status = _nfs4_proc_open(opendata);
2002 goto err_opendata_put;
2004 state = nfs4_opendata_to_nfs4_state(opendata);
2005 status = PTR_ERR(state);
2007 goto err_opendata_put;
2008 if (server->caps & NFS_CAP_POSIX_LOCK)
2009 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2011 status = nfs4_opendata_access(cred, opendata, state, fmode);
2013 goto err_opendata_put;
2015 if (opendata->o_arg.open_flags & O_EXCL) {
2016 nfs4_exclusive_attrset(opendata, sattr);
2018 nfs_fattr_init(opendata->o_res.f_attr);
2019 status = nfs4_do_setattr(state->inode, cred,
2020 opendata->o_res.f_attr, sattr,
2023 nfs_setattr_update_inode(state->inode, sattr);
2024 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2027 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2028 *ctx_th = opendata->f_attr.mdsthreshold;
2030 kfree(opendata->f_attr.mdsthreshold);
2031 opendata->f_attr.mdsthreshold = NULL;
2033 nfs4_opendata_put(opendata);
2034 nfs4_put_state_owner(sp);
2038 kfree(opendata->f_attr.mdsthreshold);
2039 nfs4_opendata_put(opendata);
2040 err_put_state_owner:
2041 nfs4_put_state_owner(sp);
2048 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2049 struct dentry *dentry,
2052 struct iattr *sattr,
2053 struct rpc_cred *cred,
2054 struct nfs4_threshold **ctx_th)
2056 struct nfs4_exception exception = { };
2057 struct nfs4_state *res;
2060 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
2062 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2066 /* NOTE: BAD_SEQID means the server and client disagree about the
2067 * book-keeping w.r.t. state-changing operations
2068 * (OPEN/CLOSE/LOCK/LOCKU...)
2069 * It is actually a sign of a bug on the client or on the server.
2071 * If we receive a BAD_SEQID error in the particular case of
2072 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2073 * have unhashed the old state_owner for us, and that we can
2074 * therefore safely retry using a new one. We should still warn
2075 * the user though...
2077 if (status == -NFS4ERR_BAD_SEQID) {
2078 pr_warn_ratelimited("NFS: v4 server %s "
2079 " returned a bad sequence-id error!\n",
2080 NFS_SERVER(dir)->nfs_client->cl_hostname);
2081 exception.retry = 1;
2085 * BAD_STATEID on OPEN means that the server cancelled our
2086 * state before it received the OPEN_CONFIRM.
2087 * Recover by retrying the request as per the discussion
2088 * on Page 181 of RFC3530.
2090 if (status == -NFS4ERR_BAD_STATEID) {
2091 exception.retry = 1;
2094 if (status == -EAGAIN) {
2095 /* We must have found a delegation */
2096 exception.retry = 1;
2099 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2100 status, &exception));
2101 } while (exception.retry);
2105 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2106 struct nfs_fattr *fattr, struct iattr *sattr,
2107 struct nfs4_state *state)
2109 struct nfs_server *server = NFS_SERVER(inode);
2110 struct nfs_setattrargs arg = {
2111 .fh = NFS_FH(inode),
2114 .bitmask = server->attr_bitmask,
2116 struct nfs_setattrres res = {
2120 struct rpc_message msg = {
2121 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2126 unsigned long timestamp = jiffies;
2129 nfs_fattr_init(fattr);
2131 if (state != NULL) {
2132 struct nfs_lockowner lockowner = {
2133 .l_owner = current->files,
2134 .l_pid = current->tgid,
2136 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2138 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2140 /* Use that stateid */
2142 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2144 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2145 if (status == 0 && state != NULL)
2146 renew_lease(server, timestamp);
2150 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2151 struct nfs_fattr *fattr, struct iattr *sattr,
2152 struct nfs4_state *state)
2154 struct nfs_server *server = NFS_SERVER(inode);
2155 struct nfs4_exception exception = {
2161 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2163 case -NFS4ERR_OPENMODE:
2164 if (state && !(state->state & FMODE_WRITE)) {
2166 if (sattr->ia_valid & ATTR_OPEN)
2171 err = nfs4_handle_exception(server, err, &exception);
2172 } while (exception.retry);
2177 struct nfs4_closedata {
2178 struct inode *inode;
2179 struct nfs4_state *state;
2180 struct nfs_closeargs arg;
2181 struct nfs_closeres res;
2182 struct nfs_fattr fattr;
2183 unsigned long timestamp;
2188 static void nfs4_free_closedata(void *data)
2190 struct nfs4_closedata *calldata = data;
2191 struct nfs4_state_owner *sp = calldata->state->owner;
2192 struct super_block *sb = calldata->state->inode->i_sb;
2195 pnfs_roc_release(calldata->state->inode);
2196 nfs4_put_open_state(calldata->state);
2197 nfs_free_seqid(calldata->arg.seqid);
2198 nfs4_put_state_owner(sp);
2199 nfs_sb_deactive(sb);
2203 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2206 spin_lock(&state->owner->so_lock);
2207 if (!(fmode & FMODE_READ))
2208 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2209 if (!(fmode & FMODE_WRITE))
2210 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2211 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2212 spin_unlock(&state->owner->so_lock);
2215 static void nfs4_close_done(struct rpc_task *task, void *data)
2217 struct nfs4_closedata *calldata = data;
2218 struct nfs4_state *state = calldata->state;
2219 struct nfs_server *server = NFS_SERVER(calldata->inode);
2221 dprintk("%s: begin!\n", __func__);
2222 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2224 /* hmm. we are done with the inode, and in the process of freeing
2225 * the state_owner. we keep this around to process errors
2227 switch (task->tk_status) {
2230 pnfs_roc_set_barrier(state->inode,
2231 calldata->roc_barrier);
2232 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2233 renew_lease(server, calldata->timestamp);
2234 nfs4_close_clear_stateid_flags(state,
2235 calldata->arg.fmode);
2237 case -NFS4ERR_STALE_STATEID:
2238 case -NFS4ERR_OLD_STATEID:
2239 case -NFS4ERR_BAD_STATEID:
2240 case -NFS4ERR_EXPIRED:
2241 if (calldata->arg.fmode == 0)
2244 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2245 rpc_restart_call_prepare(task);
2247 nfs_release_seqid(calldata->arg.seqid);
2248 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2249 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2252 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2254 struct nfs4_closedata *calldata = data;
2255 struct nfs4_state *state = calldata->state;
2256 struct inode *inode = calldata->inode;
2259 dprintk("%s: begin!\n", __func__);
2260 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2263 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2264 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2265 spin_lock(&state->owner->so_lock);
2266 /* Calculate the change in open mode */
2267 if (state->n_rdwr == 0) {
2268 if (state->n_rdonly == 0) {
2269 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2270 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2271 calldata->arg.fmode &= ~FMODE_READ;
2273 if (state->n_wronly == 0) {
2274 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2275 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2276 calldata->arg.fmode &= ~FMODE_WRITE;
2279 spin_unlock(&state->owner->so_lock);
2282 /* Note: exit _without_ calling nfs4_close_done */
2283 task->tk_action = NULL;
2287 if (calldata->arg.fmode == 0) {
2288 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2289 if (calldata->roc &&
2290 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
2294 nfs_fattr_init(calldata->res.fattr);
2295 calldata->timestamp = jiffies;
2296 if (nfs4_setup_sequence(NFS_SERVER(inode),
2297 &calldata->arg.seq_args,
2298 &calldata->res.seq_res,
2301 rpc_call_start(task);
2303 dprintk("%s: done!\n", __func__);
2306 static const struct rpc_call_ops nfs4_close_ops = {
2307 .rpc_call_prepare = nfs4_close_prepare,
2308 .rpc_call_done = nfs4_close_done,
2309 .rpc_release = nfs4_free_closedata,
2313 * It is possible for data to be read/written from a mem-mapped file
2314 * after the sys_close call (which hits the vfs layer as a flush).
2315 * This means that we can't safely call nfsv4 close on a file until
2316 * the inode is cleared. This in turn means that we are not good
2317 * NFSv4 citizens - we do not indicate to the server to update the file's
2318 * share state even when we are done with one of the three share
2319 * stateid's in the inode.
2321 * NOTE: Caller must be holding the sp->so_owner semaphore!
2323 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2325 struct nfs_server *server = NFS_SERVER(state->inode);
2326 struct nfs4_closedata *calldata;
2327 struct nfs4_state_owner *sp = state->owner;
2328 struct rpc_task *task;
2329 struct rpc_message msg = {
2330 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2331 .rpc_cred = state->owner->so_cred,
2333 struct rpc_task_setup task_setup_data = {
2334 .rpc_client = server->client,
2335 .rpc_message = &msg,
2336 .callback_ops = &nfs4_close_ops,
2337 .workqueue = nfsiod_workqueue,
2338 .flags = RPC_TASK_ASYNC,
2340 int status = -ENOMEM;
2342 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2343 if (calldata == NULL)
2345 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2346 calldata->inode = state->inode;
2347 calldata->state = state;
2348 calldata->arg.fh = NFS_FH(state->inode);
2349 calldata->arg.stateid = &state->open_stateid;
2350 /* Serialization for the sequence id */
2351 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2352 if (calldata->arg.seqid == NULL)
2353 goto out_free_calldata;
2354 calldata->arg.fmode = 0;
2355 calldata->arg.bitmask = server->cache_consistency_bitmask;
2356 calldata->res.fattr = &calldata->fattr;
2357 calldata->res.seqid = calldata->arg.seqid;
2358 calldata->res.server = server;
2359 calldata->roc = pnfs_roc(state->inode);
2360 nfs_sb_active(calldata->inode->i_sb);
2362 msg.rpc_argp = &calldata->arg;
2363 msg.rpc_resp = &calldata->res;
2364 task_setup_data.callback_data = calldata;
2365 task = rpc_run_task(&task_setup_data);
2367 return PTR_ERR(task);
2370 status = rpc_wait_for_completion_task(task);
2376 nfs4_put_open_state(state);
2377 nfs4_put_state_owner(sp);
2381 static struct inode *
2382 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2384 struct nfs4_state *state;
2386 /* Protect against concurrent sillydeletes */
2387 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2388 ctx->cred, &ctx->mdsthreshold);
2390 return ERR_CAST(state);
2392 return igrab(state->inode);
2395 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2397 if (ctx->state == NULL)
2400 nfs4_close_sync(ctx->state, ctx->mode);
2402 nfs4_close_state(ctx->state, ctx->mode);
2405 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2407 struct nfs4_server_caps_arg args = {
2410 struct nfs4_server_caps_res res = {};
2411 struct rpc_message msg = {
2412 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2418 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2420 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2421 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2422 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2423 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2424 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2425 NFS_CAP_CTIME|NFS_CAP_MTIME);
2426 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2427 server->caps |= NFS_CAP_ACLS;
2428 if (res.has_links != 0)
2429 server->caps |= NFS_CAP_HARDLINKS;
2430 if (res.has_symlinks != 0)
2431 server->caps |= NFS_CAP_SYMLINKS;
2432 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2433 server->caps |= NFS_CAP_FILEID;
2434 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2435 server->caps |= NFS_CAP_MODE;
2436 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2437 server->caps |= NFS_CAP_NLINK;
2438 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2439 server->caps |= NFS_CAP_OWNER;
2440 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2441 server->caps |= NFS_CAP_OWNER_GROUP;
2442 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2443 server->caps |= NFS_CAP_ATIME;
2444 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2445 server->caps |= NFS_CAP_CTIME;
2446 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2447 server->caps |= NFS_CAP_MTIME;
2449 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2450 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2451 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2452 server->acl_bitmask = res.acl_bitmask;
2453 server->fh_expire_type = res.fh_expire_type;
2459 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2461 struct nfs4_exception exception = { };
2464 err = nfs4_handle_exception(server,
2465 _nfs4_server_capabilities(server, fhandle),
2467 } while (exception.retry);
2471 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2472 struct nfs_fsinfo *info)
2474 struct nfs4_lookup_root_arg args = {
2475 .bitmask = nfs4_fattr_bitmap,
2477 struct nfs4_lookup_res res = {
2479 .fattr = info->fattr,
2482 struct rpc_message msg = {
2483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2488 nfs_fattr_init(info->fattr);
2489 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2492 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2493 struct nfs_fsinfo *info)
2495 struct nfs4_exception exception = { };
2498 err = _nfs4_lookup_root(server, fhandle, info);
2501 case -NFS4ERR_WRONGSEC:
2504 err = nfs4_handle_exception(server, err, &exception);
2506 } while (exception.retry);
2511 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2512 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2514 struct rpc_auth *auth;
2517 auth = rpcauth_create(flavor, server->client);
2522 ret = nfs4_lookup_root(server, fhandle, info);
2527 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2528 struct nfs_fsinfo *info)
2530 int i, len, status = 0;
2531 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2533 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2536 for (i = 0; i < len; i++) {
2537 /* AUTH_UNIX is the default flavor if none was specified,
2538 * thus has already been tried. */
2539 if (flav_array[i] == RPC_AUTH_UNIX)
2542 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2543 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2548 * -EACCESS could mean that the user doesn't have correct permissions
2549 * to access the mount. It could also mean that we tried to mount
2550 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2551 * existing mount programs don't handle -EACCES very well so it should
2552 * be mapped to -EPERM instead.
2554 if (status == -EACCES)
2560 * get the file handle for the "/" directory on the server
2562 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2563 struct nfs_fsinfo *info)
2565 int minor_version = server->nfs_client->cl_minorversion;
2566 int status = nfs4_lookup_root(server, fhandle, info);
2567 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2569 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2570 * by nfs4_map_errors() as this function exits.
2572 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2574 status = nfs4_server_capabilities(server, fhandle);
2576 status = nfs4_do_fsinfo(server, fhandle, info);
2577 return nfs4_map_errors(status);
2580 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2581 struct nfs_fsinfo *info)
2584 struct nfs_fattr *fattr = info->fattr;
2586 error = nfs4_server_capabilities(server, mntfh);
2588 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2592 error = nfs4_proc_getattr(server, mntfh, fattr);
2594 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2598 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2599 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2600 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2606 * Get locations and (maybe) other attributes of a referral.
2607 * Note that we'll actually follow the referral later when
2608 * we detect fsid mismatch in inode revalidation
2610 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2611 const struct qstr *name, struct nfs_fattr *fattr,
2612 struct nfs_fh *fhandle)
2614 int status = -ENOMEM;
2615 struct page *page = NULL;
2616 struct nfs4_fs_locations *locations = NULL;
2618 page = alloc_page(GFP_KERNEL);
2621 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2622 if (locations == NULL)
2625 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2628 /* Make sure server returned a different fsid for the referral */
2629 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2630 dprintk("%s: server did not return a different fsid for"
2631 " a referral at %s\n", __func__, name->name);
2635 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2636 nfs_fixup_referral_attributes(&locations->fattr);
2638 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2639 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2640 memset(fhandle, 0, sizeof(struct nfs_fh));
2648 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2650 struct nfs4_getattr_arg args = {
2652 .bitmask = server->attr_bitmask,
2654 struct nfs4_getattr_res res = {
2658 struct rpc_message msg = {
2659 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2664 nfs_fattr_init(fattr);
2665 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2668 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2670 struct nfs4_exception exception = { };
2673 err = nfs4_handle_exception(server,
2674 _nfs4_proc_getattr(server, fhandle, fattr),
2676 } while (exception.retry);
2681 * The file is not closed if it is opened due to the a request to change
2682 * the size of the file. The open call will not be needed once the
2683 * VFS layer lookup-intents are implemented.
2685 * Close is called when the inode is destroyed.
2686 * If we haven't opened the file for O_WRONLY, we
2687 * need to in the size_change case to obtain a stateid.
2690 * Because OPEN is always done by name in nfsv4, it is
2691 * possible that we opened a different file by the same
2692 * name. We can recognize this race condition, but we
2693 * can't do anything about it besides returning an error.
2695 * This will be fixed with VFS changes (lookup-intent).
2698 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2699 struct iattr *sattr)
2701 struct inode *inode = dentry->d_inode;
2702 struct rpc_cred *cred = NULL;
2703 struct nfs4_state *state = NULL;
2706 if (pnfs_ld_layoutret_on_setattr(inode))
2707 pnfs_return_layout(inode);
2709 nfs_fattr_init(fattr);
2711 /* Deal with open(O_TRUNC) */
2712 if (sattr->ia_valid & ATTR_OPEN)
2713 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2715 /* Optimization: if the end result is no change, don't RPC */
2716 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2719 /* Search for an existing open(O_WRITE) file */
2720 if (sattr->ia_valid & ATTR_FILE) {
2721 struct nfs_open_context *ctx;
2723 ctx = nfs_file_open_context(sattr->ia_file);
2730 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2732 nfs_setattr_update_inode(inode, sattr);
2736 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2737 const struct qstr *name, struct nfs_fh *fhandle,
2738 struct nfs_fattr *fattr)
2740 struct nfs_server *server = NFS_SERVER(dir);
2742 struct nfs4_lookup_arg args = {
2743 .bitmask = server->attr_bitmask,
2744 .dir_fh = NFS_FH(dir),
2747 struct nfs4_lookup_res res = {
2752 struct rpc_message msg = {
2753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2758 nfs_fattr_init(fattr);
2760 dprintk("NFS call lookup %s\n", name->name);
2761 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2762 dprintk("NFS reply lookup: %d\n", status);
2766 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2768 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2769 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2770 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2774 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2775 struct qstr *name, struct nfs_fh *fhandle,
2776 struct nfs_fattr *fattr)
2778 struct nfs4_exception exception = { };
2779 struct rpc_clnt *client = *clnt;
2782 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2784 case -NFS4ERR_BADNAME:
2787 case -NFS4ERR_MOVED:
2788 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
2790 case -NFS4ERR_WRONGSEC:
2792 if (client != *clnt)
2795 client = nfs4_create_sec_client(client, dir, name);
2797 return PTR_ERR(client);
2799 exception.retry = 1;
2802 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2804 } while (exception.retry);
2809 else if (client != *clnt)
2810 rpc_shutdown_client(client);
2815 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
2816 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2819 struct rpc_clnt *client = NFS_CLIENT(dir);
2821 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2822 if (client != NFS_CLIENT(dir)) {
2823 rpc_shutdown_client(client);
2824 nfs_fixup_secinfo_attributes(fattr);
2830 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2831 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2834 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2836 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2838 rpc_shutdown_client(client);
2839 return ERR_PTR(status);
2844 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2846 struct nfs_server *server = NFS_SERVER(inode);
2847 struct nfs4_accessargs args = {
2848 .fh = NFS_FH(inode),
2849 .bitmask = server->cache_consistency_bitmask,
2851 struct nfs4_accessres res = {
2854 struct rpc_message msg = {
2855 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2858 .rpc_cred = entry->cred,
2860 int mode = entry->mask;
2864 * Determine which access bits we want to ask for...
2866 if (mode & MAY_READ)
2867 args.access |= NFS4_ACCESS_READ;
2868 if (S_ISDIR(inode->i_mode)) {
2869 if (mode & MAY_WRITE)
2870 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2871 if (mode & MAY_EXEC)
2872 args.access |= NFS4_ACCESS_LOOKUP;
2874 if (mode & MAY_WRITE)
2875 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2876 if (mode & MAY_EXEC)
2877 args.access |= NFS4_ACCESS_EXECUTE;
2880 res.fattr = nfs_alloc_fattr();
2881 if (res.fattr == NULL)
2884 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2886 nfs_access_set_mask(entry, res.access);
2887 nfs_refresh_inode(inode, res.fattr);
2889 nfs_free_fattr(res.fattr);
2893 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2895 struct nfs4_exception exception = { };
2898 err = nfs4_handle_exception(NFS_SERVER(inode),
2899 _nfs4_proc_access(inode, entry),
2901 } while (exception.retry);
2906 * TODO: For the time being, we don't try to get any attributes
2907 * along with any of the zero-copy operations READ, READDIR,
2910 * In the case of the first three, we want to put the GETATTR
2911 * after the read-type operation -- this is because it is hard
2912 * to predict the length of a GETATTR response in v4, and thus
2913 * align the READ data correctly. This means that the GETATTR
2914 * may end up partially falling into the page cache, and we should
2915 * shift it into the 'tail' of the xdr_buf before processing.
2916 * To do this efficiently, we need to know the total length
2917 * of data received, which doesn't seem to be available outside
2920 * In the case of WRITE, we also want to put the GETATTR after
2921 * the operation -- in this case because we want to make sure
2922 * we get the post-operation mtime and size.
2924 * Both of these changes to the XDR layer would in fact be quite
2925 * minor, but I decided to leave them for a subsequent patch.
2927 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2928 unsigned int pgbase, unsigned int pglen)
2930 struct nfs4_readlink args = {
2931 .fh = NFS_FH(inode),
2936 struct nfs4_readlink_res res;
2937 struct rpc_message msg = {
2938 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2943 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2946 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2947 unsigned int pgbase, unsigned int pglen)
2949 struct nfs4_exception exception = { };
2952 err = nfs4_handle_exception(NFS_SERVER(inode),
2953 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2955 } while (exception.retry);
2960 * This is just for mknod. open(O_CREAT) will always do ->open_context().
2963 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2966 struct nfs_open_context *ctx;
2967 struct nfs4_state *state;
2970 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2972 return PTR_ERR(ctx);
2974 sattr->ia_mode &= ~current_umask();
2975 state = nfs4_do_open(dir, dentry, ctx->mode,
2976 flags, sattr, ctx->cred,
2977 &ctx->mdsthreshold);
2979 if (IS_ERR(state)) {
2980 status = PTR_ERR(state);
2983 d_add(dentry, igrab(state->inode));
2984 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2987 put_nfs_open_context(ctx);
2991 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2993 struct nfs_server *server = NFS_SERVER(dir);
2994 struct nfs_removeargs args = {
2998 struct nfs_removeres res = {
3001 struct rpc_message msg = {
3002 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3008 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3010 update_changeattr(dir, &res.cinfo);
3014 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3016 struct nfs4_exception exception = { };
3019 err = nfs4_handle_exception(NFS_SERVER(dir),
3020 _nfs4_proc_remove(dir, name),
3022 } while (exception.retry);
3026 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3028 struct nfs_server *server = NFS_SERVER(dir);
3029 struct nfs_removeargs *args = msg->rpc_argp;
3030 struct nfs_removeres *res = msg->rpc_resp;
3032 res->server = server;
3033 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3034 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
3037 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3039 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
3040 &data->args.seq_args,
3044 rpc_call_start(task);
3047 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3049 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3051 if (!nfs4_sequence_done(task, &res->seq_res))
3053 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3055 update_changeattr(dir, &res->cinfo);
3059 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3061 struct nfs_server *server = NFS_SERVER(dir);
3062 struct nfs_renameargs *arg = msg->rpc_argp;
3063 struct nfs_renameres *res = msg->rpc_resp;
3065 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3066 res->server = server;
3067 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
3070 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3072 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3073 &data->args.seq_args,
3077 rpc_call_start(task);
3080 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3081 struct inode *new_dir)
3083 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3085 if (!nfs4_sequence_done(task, &res->seq_res))
3087 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3090 update_changeattr(old_dir, &res->old_cinfo);
3091 update_changeattr(new_dir, &res->new_cinfo);
3095 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3096 struct inode *new_dir, struct qstr *new_name)
3098 struct nfs_server *server = NFS_SERVER(old_dir);
3099 struct nfs_renameargs arg = {
3100 .old_dir = NFS_FH(old_dir),
3101 .new_dir = NFS_FH(new_dir),
3102 .old_name = old_name,
3103 .new_name = new_name,
3105 struct nfs_renameres res = {
3108 struct rpc_message msg = {
3109 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3113 int status = -ENOMEM;
3115 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3117 update_changeattr(old_dir, &res.old_cinfo);
3118 update_changeattr(new_dir, &res.new_cinfo);
3123 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3124 struct inode *new_dir, struct qstr *new_name)
3126 struct nfs4_exception exception = { };
3129 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3130 _nfs4_proc_rename(old_dir, old_name,
3133 } while (exception.retry);
3137 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3139 struct nfs_server *server = NFS_SERVER(inode);
3140 struct nfs4_link_arg arg = {
3141 .fh = NFS_FH(inode),
3142 .dir_fh = NFS_FH(dir),
3144 .bitmask = server->attr_bitmask,
3146 struct nfs4_link_res res = {
3149 struct rpc_message msg = {
3150 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3154 int status = -ENOMEM;
3156 res.fattr = nfs_alloc_fattr();
3157 if (res.fattr == NULL)
3160 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3162 update_changeattr(dir, &res.cinfo);
3163 nfs_post_op_update_inode(inode, res.fattr);
3166 nfs_free_fattr(res.fattr);
3170 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3172 struct nfs4_exception exception = { };
3175 err = nfs4_handle_exception(NFS_SERVER(inode),
3176 _nfs4_proc_link(inode, dir, name),
3178 } while (exception.retry);
3182 struct nfs4_createdata {
3183 struct rpc_message msg;
3184 struct nfs4_create_arg arg;
3185 struct nfs4_create_res res;
3187 struct nfs_fattr fattr;
3190 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3191 struct qstr *name, struct iattr *sattr, u32 ftype)
3193 struct nfs4_createdata *data;
3195 data = kzalloc(sizeof(*data), GFP_KERNEL);
3197 struct nfs_server *server = NFS_SERVER(dir);
3199 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3200 data->msg.rpc_argp = &data->arg;
3201 data->msg.rpc_resp = &data->res;
3202 data->arg.dir_fh = NFS_FH(dir);
3203 data->arg.server = server;
3204 data->arg.name = name;
3205 data->arg.attrs = sattr;
3206 data->arg.ftype = ftype;
3207 data->arg.bitmask = server->attr_bitmask;
3208 data->res.server = server;
3209 data->res.fh = &data->fh;
3210 data->res.fattr = &data->fattr;
3211 nfs_fattr_init(data->res.fattr);
3216 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3218 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3219 &data->arg.seq_args, &data->res.seq_res, 1);
3221 update_changeattr(dir, &data->res.dir_cinfo);
3222 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3227 static void nfs4_free_createdata(struct nfs4_createdata *data)
3232 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3233 struct page *page, unsigned int len, struct iattr *sattr)
3235 struct nfs4_createdata *data;
3236 int status = -ENAMETOOLONG;
3238 if (len > NFS4_MAXPATHLEN)
3242 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3246 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3247 data->arg.u.symlink.pages = &page;
3248 data->arg.u.symlink.len = len;
3250 status = nfs4_do_create(dir, dentry, data);
3252 nfs4_free_createdata(data);
3257 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3258 struct page *page, unsigned int len, struct iattr *sattr)
3260 struct nfs4_exception exception = { };
3263 err = nfs4_handle_exception(NFS_SERVER(dir),
3264 _nfs4_proc_symlink(dir, dentry, page,
3267 } while (exception.retry);
3271 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3272 struct iattr *sattr)
3274 struct nfs4_createdata *data;
3275 int status = -ENOMEM;
3277 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3281 status = nfs4_do_create(dir, dentry, data);
3283 nfs4_free_createdata(data);
3288 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3289 struct iattr *sattr)
3291 struct nfs4_exception exception = { };
3294 sattr->ia_mode &= ~current_umask();
3296 err = nfs4_handle_exception(NFS_SERVER(dir),
3297 _nfs4_proc_mkdir(dir, dentry, sattr),
3299 } while (exception.retry);
3303 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3304 u64 cookie, struct page **pages, unsigned int count, int plus)
3306 struct inode *dir = dentry->d_inode;
3307 struct nfs4_readdir_arg args = {
3312 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3315 struct nfs4_readdir_res res;
3316 struct rpc_message msg = {
3317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3324 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3325 dentry->d_parent->d_name.name,
3326 dentry->d_name.name,
3327 (unsigned long long)cookie);
3328 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3329 res.pgbase = args.pgbase;
3330 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3332 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3333 status += args.pgbase;
3336 nfs_invalidate_atime(dir);
3338 dprintk("%s: returns %d\n", __func__, status);
3342 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3343 u64 cookie, struct page **pages, unsigned int count, int plus)
3345 struct nfs4_exception exception = { };
3348 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3349 _nfs4_proc_readdir(dentry, cred, cookie,
3350 pages, count, plus),
3352 } while (exception.retry);
3356 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3357 struct iattr *sattr, dev_t rdev)
3359 struct nfs4_createdata *data;
3360 int mode = sattr->ia_mode;
3361 int status = -ENOMEM;
3363 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3364 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3366 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3371 data->arg.ftype = NF4FIFO;
3372 else if (S_ISBLK(mode)) {
3373 data->arg.ftype = NF4BLK;
3374 data->arg.u.device.specdata1 = MAJOR(rdev);
3375 data->arg.u.device.specdata2 = MINOR(rdev);
3377 else if (S_ISCHR(mode)) {
3378 data->arg.ftype = NF4CHR;
3379 data->arg.u.device.specdata1 = MAJOR(rdev);
3380 data->arg.u.device.specdata2 = MINOR(rdev);
3383 status = nfs4_do_create(dir, dentry, data);
3385 nfs4_free_createdata(data);
3390 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3391 struct iattr *sattr, dev_t rdev)
3393 struct nfs4_exception exception = { };
3396 sattr->ia_mode &= ~current_umask();
3398 err = nfs4_handle_exception(NFS_SERVER(dir),
3399 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3401 } while (exception.retry);
3405 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3406 struct nfs_fsstat *fsstat)
3408 struct nfs4_statfs_arg args = {
3410 .bitmask = server->attr_bitmask,
3412 struct nfs4_statfs_res res = {
3415 struct rpc_message msg = {
3416 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3421 nfs_fattr_init(fsstat->fattr);
3422 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3425 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3427 struct nfs4_exception exception = { };
3430 err = nfs4_handle_exception(server,
3431 _nfs4_proc_statfs(server, fhandle, fsstat),
3433 } while (exception.retry);
3437 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3438 struct nfs_fsinfo *fsinfo)
3440 struct nfs4_fsinfo_arg args = {
3442 .bitmask = server->attr_bitmask,
3444 struct nfs4_fsinfo_res res = {
3447 struct rpc_message msg = {
3448 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3453 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3456 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3458 struct nfs4_exception exception = { };
3462 err = nfs4_handle_exception(server,
3463 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3465 } while (exception.retry);
3469 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3473 nfs_fattr_init(fsinfo->fattr);
3474 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3476 /* block layout checks this! */
3477 server->pnfs_blksize = fsinfo->blksize;
3478 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3484 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3485 struct nfs_pathconf *pathconf)
3487 struct nfs4_pathconf_arg args = {
3489 .bitmask = server->attr_bitmask,
3491 struct nfs4_pathconf_res res = {
3492 .pathconf = pathconf,
3494 struct rpc_message msg = {
3495 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3500 /* None of the pathconf attributes are mandatory to implement */
3501 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3502 memset(pathconf, 0, sizeof(*pathconf));
3506 nfs_fattr_init(pathconf->fattr);
3507 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3510 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3511 struct nfs_pathconf *pathconf)
3513 struct nfs4_exception exception = { };
3517 err = nfs4_handle_exception(server,
3518 _nfs4_proc_pathconf(server, fhandle, pathconf),
3520 } while (exception.retry);
3524 void __nfs4_read_done_cb(struct nfs_read_data *data)
3526 nfs_invalidate_atime(data->header->inode);
3529 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3531 struct nfs_server *server = NFS_SERVER(data->header->inode);
3533 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3534 rpc_restart_call_prepare(task);
3538 __nfs4_read_done_cb(data);
3539 if (task->tk_status > 0)
3540 renew_lease(server, data->timestamp);
3544 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3547 dprintk("--> %s\n", __func__);
3549 if (!nfs4_sequence_done(task, &data->res.seq_res))
3552 return data->read_done_cb ? data->read_done_cb(task, data) :
3553 nfs4_read_done_cb(task, data);
3556 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3558 data->timestamp = jiffies;
3559 data->read_done_cb = nfs4_read_done_cb;
3560 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3561 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3564 static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3566 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3567 &data->args.seq_args,
3571 rpc_call_start(task);
3574 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3576 struct inode *inode = data->header->inode;
3578 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3579 rpc_restart_call_prepare(task);
3582 if (task->tk_status >= 0) {
3583 renew_lease(NFS_SERVER(inode), data->timestamp);
3584 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
3589 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3591 if (!nfs4_sequence_done(task, &data->res.seq_res))
3593 return data->write_done_cb ? data->write_done_cb(task, data) :
3594 nfs4_write_done_cb(task, data);
3598 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3600 const struct nfs_pgio_header *hdr = data->header;
3602 /* Don't request attributes for pNFS or O_DIRECT writes */
3603 if (data->ds_clp != NULL || hdr->dreq != NULL)
3605 /* Otherwise, request attributes if and only if we don't hold
3608 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
3611 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3613 struct nfs_server *server = NFS_SERVER(data->header->inode);
3615 if (!nfs4_write_need_cache_consistency_data(data)) {
3616 data->args.bitmask = NULL;
3617 data->res.fattr = NULL;
3619 data->args.bitmask = server->cache_consistency_bitmask;
3621 if (!data->write_done_cb)
3622 data->write_done_cb = nfs4_write_done_cb;
3623 data->res.server = server;
3624 data->timestamp = jiffies;
3626 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3627 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3630 static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3632 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3633 &data->args.seq_args,
3637 rpc_call_start(task);
3640 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3642 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3643 &data->args.seq_args,
3647 rpc_call_start(task);
3650 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
3652 struct inode *inode = data->inode;
3654 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3655 rpc_restart_call_prepare(task);
3661 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
3663 if (!nfs4_sequence_done(task, &data->res.seq_res))
3665 return data->commit_done_cb(task, data);
3668 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
3670 struct nfs_server *server = NFS_SERVER(data->inode);
3672 if (data->commit_done_cb == NULL)
3673 data->commit_done_cb = nfs4_commit_done_cb;
3674 data->res.server = server;
3675 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3676 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3679 struct nfs4_renewdata {
3680 struct nfs_client *client;
3681 unsigned long timestamp;
3685 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3686 * standalone procedure for queueing an asynchronous RENEW.
3688 static void nfs4_renew_release(void *calldata)
3690 struct nfs4_renewdata *data = calldata;
3691 struct nfs_client *clp = data->client;
3693 if (atomic_read(&clp->cl_count) > 1)
3694 nfs4_schedule_state_renewal(clp);
3695 nfs_put_client(clp);
3699 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3701 struct nfs4_renewdata *data = calldata;
3702 struct nfs_client *clp = data->client;
3703 unsigned long timestamp = data->timestamp;
3705 if (task->tk_status < 0) {
3706 /* Unless we're shutting down, schedule state recovery! */
3707 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3709 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3710 nfs4_schedule_lease_recovery(clp);
3713 nfs4_schedule_path_down_recovery(clp);
3715 do_renew_lease(clp, timestamp);
3718 static const struct rpc_call_ops nfs4_renew_ops = {
3719 .rpc_call_done = nfs4_renew_done,
3720 .rpc_release = nfs4_renew_release,
3723 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3725 struct rpc_message msg = {
3726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3730 struct nfs4_renewdata *data;
3732 if (renew_flags == 0)
3734 if (!atomic_inc_not_zero(&clp->cl_count))
3736 data = kmalloc(sizeof(*data), GFP_NOFS);
3740 data->timestamp = jiffies;
3741 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3742 &nfs4_renew_ops, data);
3745 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3747 struct rpc_message msg = {
3748 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3752 unsigned long now = jiffies;
3755 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3758 do_renew_lease(clp, now);
3762 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3764 return (server->caps & NFS_CAP_ACLS)
3765 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3766 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3769 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3770 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
3773 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
3775 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3776 struct page **pages, unsigned int *pgbase)
3778 struct page *newpage, **spages;
3784 len = min_t(size_t, PAGE_SIZE, buflen);
3785 newpage = alloc_page(GFP_KERNEL);
3787 if (newpage == NULL)
3789 memcpy(page_address(newpage), buf, len);
3794 } while (buflen != 0);
3800 __free_page(spages[rc-1]);
3804 struct nfs4_cached_acl {
3810 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3812 struct nfs_inode *nfsi = NFS_I(inode);
3814 spin_lock(&inode->i_lock);
3815 kfree(nfsi->nfs4_acl);
3816 nfsi->nfs4_acl = acl;
3817 spin_unlock(&inode->i_lock);
3820 static void nfs4_zap_acl_attr(struct inode *inode)
3822 nfs4_set_cached_acl(inode, NULL);
3825 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3827 struct nfs_inode *nfsi = NFS_I(inode);
3828 struct nfs4_cached_acl *acl;
3831 spin_lock(&inode->i_lock);
3832 acl = nfsi->nfs4_acl;
3835 if (buf == NULL) /* user is just asking for length */
3837 if (acl->cached == 0)
3839 ret = -ERANGE; /* see getxattr(2) man page */
3840 if (acl->len > buflen)
3842 memcpy(buf, acl->data, acl->len);
3846 spin_unlock(&inode->i_lock);
3850 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3852 struct nfs4_cached_acl *acl;
3853 size_t buflen = sizeof(*acl) + acl_len;
3855 if (buflen <= PAGE_SIZE) {
3856 acl = kmalloc(buflen, GFP_KERNEL);
3860 _copy_from_pages(acl->data, pages, pgbase, acl_len);
3862 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3869 nfs4_set_cached_acl(inode, acl);
3873 * The getxattr API returns the required buffer length when called with a
3874 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3875 * the required buf. On a NULL buf, we send a page of data to the server
3876 * guessing that the ACL request can be serviced by a page. If so, we cache
3877 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3878 * the cache. If not so, we throw away the page, and cache the required
3879 * length. The next getxattr call will then produce another round trip to
3880 * the server, this time with the input buf of the required size.
3882 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3884 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3885 struct nfs_getaclargs args = {
3886 .fh = NFS_FH(inode),
3890 struct nfs_getaclres res = {
3893 struct rpc_message msg = {
3894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3898 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3899 int ret = -ENOMEM, i;
3901 /* As long as we're doing a round trip to the server anyway,
3902 * let's be prepared for a page of acl data. */
3905 if (npages > ARRAY_SIZE(pages))
3908 for (i = 0; i < npages; i++) {
3909 pages[i] = alloc_page(GFP_KERNEL);
3914 /* for decoding across pages */
3915 res.acl_scratch = alloc_page(GFP_KERNEL);
3916 if (!res.acl_scratch)
3919 args.acl_len = npages * PAGE_SIZE;
3920 args.acl_pgbase = 0;
3922 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3923 __func__, buf, buflen, npages, args.acl_len);
3924 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3925 &msg, &args.seq_args, &res.seq_res, 0);
3929 /* Handle the case where the passed-in buffer is too short */
3930 if (res.acl_flags & NFS4_ACL_TRUNC) {
3931 /* Did the user only issue a request for the acl length? */
3937 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3939 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3943 for (i = 0; i < npages; i++)
3945 __free_page(pages[i]);
3946 if (res.acl_scratch)
3947 __free_page(res.acl_scratch);
3951 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3953 struct nfs4_exception exception = { };
3956 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3959 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3960 } while (exception.retry);
3964 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3966 struct nfs_server *server = NFS_SERVER(inode);
3969 if (!nfs4_server_supports_acls(server))
3971 ret = nfs_revalidate_inode(server, inode);
3974 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3975 nfs_zap_acl_cache(inode);
3976 ret = nfs4_read_cached_acl(inode, buf, buflen);
3978 /* -ENOENT is returned if there is no ACL or if there is an ACL
3979 * but no cached acl data, just the acl length */
3981 return nfs4_get_acl_uncached(inode, buf, buflen);
3984 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3986 struct nfs_server *server = NFS_SERVER(inode);
3987 struct page *pages[NFS4ACL_MAXPAGES];
3988 struct nfs_setaclargs arg = {
3989 .fh = NFS_FH(inode),
3993 struct nfs_setaclres res;
3994 struct rpc_message msg = {
3995 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3999 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4002 if (!nfs4_server_supports_acls(server))
4004 if (npages > ARRAY_SIZE(pages))
4006 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4009 nfs4_inode_return_delegation(inode);
4010 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4013 * Free each page after tx, so the only ref left is
4014 * held by the network stack
4017 put_page(pages[i-1]);
4020 * Acl update can result in inode attribute update.
4021 * so mark the attribute cache invalid.
4023 spin_lock(&inode->i_lock);
4024 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4025 spin_unlock(&inode->i_lock);
4026 nfs_access_zap_cache(inode);
4027 nfs_zap_acl_cache(inode);
4031 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4033 struct nfs4_exception exception = { };
4036 err = nfs4_handle_exception(NFS_SERVER(inode),
4037 __nfs4_proc_set_acl(inode, buf, buflen),
4039 } while (exception.retry);
4044 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
4046 struct nfs_client *clp = server->nfs_client;
4048 if (task->tk_status >= 0)
4050 switch(task->tk_status) {
4051 case -NFS4ERR_DELEG_REVOKED:
4052 case -NFS4ERR_ADMIN_REVOKED:
4053 case -NFS4ERR_BAD_STATEID:
4056 nfs_remove_bad_delegation(state->inode);
4057 case -NFS4ERR_OPENMODE:
4060 nfs4_schedule_stateid_recovery(server, state);
4061 goto wait_on_recovery;
4062 case -NFS4ERR_EXPIRED:
4064 nfs4_schedule_stateid_recovery(server, state);
4065 case -NFS4ERR_STALE_STATEID:
4066 case -NFS4ERR_STALE_CLIENTID:
4067 nfs4_schedule_lease_recovery(clp);
4068 goto wait_on_recovery;
4069 #if defined(CONFIG_NFS_V4_1)
4070 case -NFS4ERR_BADSESSION:
4071 case -NFS4ERR_BADSLOT:
4072 case -NFS4ERR_BAD_HIGH_SLOT:
4073 case -NFS4ERR_DEADSESSION:
4074 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4075 case -NFS4ERR_SEQ_FALSE_RETRY:
4076 case -NFS4ERR_SEQ_MISORDERED:
4077 dprintk("%s ERROR %d, Reset session\n", __func__,
4079 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4080 task->tk_status = 0;
4082 #endif /* CONFIG_NFS_V4_1 */
4083 case -NFS4ERR_DELAY:
4084 nfs_inc_server_stats(server, NFSIOS_DELAY);
4085 case -NFS4ERR_GRACE:
4087 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4088 task->tk_status = 0;
4090 case -NFS4ERR_RETRY_UNCACHED_REP:
4091 case -NFS4ERR_OLD_STATEID:
4092 task->tk_status = 0;
4095 task->tk_status = nfs4_map_errors(task->tk_status);
4098 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4099 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4100 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4101 task->tk_status = 0;
4105 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4106 nfs4_verifier *bootverf)
4110 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4111 /* An impossible timestamp guarantees this value
4112 * will never match a generated boot time. */
4114 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4116 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4117 verf[0] = (__be32)nn->boot_time.tv_sec;
4118 verf[1] = (__be32)nn->boot_time.tv_nsec;
4120 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4124 nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4125 char *buf, size_t len)
4127 unsigned int result;
4130 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4132 rpc_peeraddr2str(clp->cl_rpcclient,
4134 rpc_peeraddr2str(clp->cl_rpcclient,
4135 RPC_DISPLAY_PROTO));
4141 nfs4_init_uniform_client_string(const struct nfs_client *clp,
4142 char *buf, size_t len)
4144 char *nodename = clp->cl_rpcclient->cl_nodename;
4146 if (nfs4_client_id_uniquifier[0] != '\0')
4147 nodename = nfs4_client_id_uniquifier;
4148 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4149 clp->rpc_ops->version, clp->cl_minorversion,
4154 * nfs4_proc_setclientid - Negotiate client ID
4155 * @clp: state data structure
4156 * @program: RPC program for NFSv4 callback service
4157 * @port: IP port number for NFS4 callback service
4158 * @cred: RPC credential to use for this call
4159 * @res: where to place the result
4161 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4163 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4164 unsigned short port, struct rpc_cred *cred,
4165 struct nfs4_setclientid_res *res)
4167 nfs4_verifier sc_verifier;
4168 struct nfs4_setclientid setclientid = {
4169 .sc_verifier = &sc_verifier,
4171 .sc_cb_ident = clp->cl_cb_ident,
4173 struct rpc_message msg = {
4174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4175 .rpc_argp = &setclientid,
4181 /* nfs_client_id4 */
4182 nfs4_init_boot_verifier(clp, &sc_verifier);
4183 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4184 setclientid.sc_name_len =
4185 nfs4_init_uniform_client_string(clp,
4186 setclientid.sc_name,
4187 sizeof(setclientid.sc_name));
4189 setclientid.sc_name_len =
4190 nfs4_init_nonuniform_client_string(clp,
4191 setclientid.sc_name,
4192 sizeof(setclientid.sc_name));
4195 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4196 sizeof(setclientid.sc_netid),
4197 rpc_peeraddr2str(clp->cl_rpcclient,
4198 RPC_DISPLAY_NETID));
4200 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4201 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4202 clp->cl_ipaddr, port >> 8, port & 255);
4204 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4205 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4206 setclientid.sc_name_len, setclientid.sc_name);
4207 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4208 dprintk("NFS reply setclientid: %d\n", status);
4213 * nfs4_proc_setclientid_confirm - Confirm client ID
4214 * @clp: state data structure
4215 * @res: result of a previous SETCLIENTID
4216 * @cred: RPC credential to use for this call
4218 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4220 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4221 struct nfs4_setclientid_res *arg,
4222 struct rpc_cred *cred)
4224 struct nfs_fsinfo fsinfo;
4225 struct rpc_message msg = {
4226 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4228 .rpc_resp = &fsinfo,
4234 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4235 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4238 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4240 spin_lock(&clp->cl_lock);
4241 clp->cl_lease_time = fsinfo.lease_time * HZ;
4242 clp->cl_last_renewal = now;
4243 spin_unlock(&clp->cl_lock);
4245 dprintk("NFS reply setclientid_confirm: %d\n", status);
4249 struct nfs4_delegreturndata {
4250 struct nfs4_delegreturnargs args;
4251 struct nfs4_delegreturnres res;
4253 nfs4_stateid stateid;
4254 unsigned long timestamp;
4255 struct nfs_fattr fattr;
4259 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4261 struct nfs4_delegreturndata *data = calldata;
4263 if (!nfs4_sequence_done(task, &data->res.seq_res))
4266 switch (task->tk_status) {
4267 case -NFS4ERR_STALE_STATEID:
4268 case -NFS4ERR_EXPIRED:
4270 renew_lease(data->res.server, data->timestamp);
4273 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4275 rpc_restart_call_prepare(task);
4279 data->rpc_status = task->tk_status;
4282 static void nfs4_delegreturn_release(void *calldata)
4287 #if defined(CONFIG_NFS_V4_1)
4288 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4290 struct nfs4_delegreturndata *d_data;
4292 d_data = (struct nfs4_delegreturndata *)data;
4294 if (nfs4_setup_sequence(d_data->res.server,
4295 &d_data->args.seq_args,
4296 &d_data->res.seq_res, task))
4298 rpc_call_start(task);
4300 #endif /* CONFIG_NFS_V4_1 */
4302 static const struct rpc_call_ops nfs4_delegreturn_ops = {
4303 #if defined(CONFIG_NFS_V4_1)
4304 .rpc_call_prepare = nfs4_delegreturn_prepare,
4305 #endif /* CONFIG_NFS_V4_1 */
4306 .rpc_call_done = nfs4_delegreturn_done,
4307 .rpc_release = nfs4_delegreturn_release,
4310 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4312 struct nfs4_delegreturndata *data;
4313 struct nfs_server *server = NFS_SERVER(inode);
4314 struct rpc_task *task;
4315 struct rpc_message msg = {
4316 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4319 struct rpc_task_setup task_setup_data = {
4320 .rpc_client = server->client,
4321 .rpc_message = &msg,
4322 .callback_ops = &nfs4_delegreturn_ops,
4323 .flags = RPC_TASK_ASYNC,
4327 data = kzalloc(sizeof(*data), GFP_NOFS);
4330 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4331 data->args.fhandle = &data->fh;
4332 data->args.stateid = &data->stateid;
4333 data->args.bitmask = server->cache_consistency_bitmask;
4334 nfs_copy_fh(&data->fh, NFS_FH(inode));
4335 nfs4_stateid_copy(&data->stateid, stateid);
4336 data->res.fattr = &data->fattr;
4337 data->res.server = server;
4338 nfs_fattr_init(data->res.fattr);
4339 data->timestamp = jiffies;
4340 data->rpc_status = 0;
4342 task_setup_data.callback_data = data;
4343 msg.rpc_argp = &data->args;
4344 msg.rpc_resp = &data->res;
4345 task = rpc_run_task(&task_setup_data);
4347 return PTR_ERR(task);
4350 status = nfs4_wait_for_completion_rpc_task(task);
4353 status = data->rpc_status;
4355 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4357 nfs_refresh_inode(inode, &data->fattr);
4363 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4365 struct nfs_server *server = NFS_SERVER(inode);
4366 struct nfs4_exception exception = { };
4369 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4371 case -NFS4ERR_STALE_STATEID:
4372 case -NFS4ERR_EXPIRED:
4376 err = nfs4_handle_exception(server, err, &exception);
4377 } while (exception.retry);
4381 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4382 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4385 * sleep, with exponential backoff, and retry the LOCK operation.
4387 static unsigned long
4388 nfs4_set_lock_task_retry(unsigned long timeout)
4390 freezable_schedule_timeout_killable(timeout);
4392 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4393 return NFS4_LOCK_MAXTIMEOUT;
4397 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4399 struct inode *inode = state->inode;
4400 struct nfs_server *server = NFS_SERVER(inode);
4401 struct nfs_client *clp = server->nfs_client;
4402 struct nfs_lockt_args arg = {
4403 .fh = NFS_FH(inode),
4406 struct nfs_lockt_res res = {
4409 struct rpc_message msg = {
4410 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4413 .rpc_cred = state->owner->so_cred,
4415 struct nfs4_lock_state *lsp;
4418 arg.lock_owner.clientid = clp->cl_clientid;
4419 status = nfs4_set_lock_state(state, request);
4422 lsp = request->fl_u.nfs4_fl.owner;
4423 arg.lock_owner.id = lsp->ls_seqid.owner_id;
4424 arg.lock_owner.s_dev = server->s_dev;
4425 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4428 request->fl_type = F_UNLCK;
4430 case -NFS4ERR_DENIED:
4433 request->fl_ops->fl_release_private(request);
4438 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4440 struct nfs4_exception exception = { };
4444 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4445 _nfs4_proc_getlk(state, cmd, request),
4447 } while (exception.retry);
4451 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4454 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4456 res = posix_lock_file_wait(file, fl);
4459 res = flock_lock_file_wait(file, fl);
4467 struct nfs4_unlockdata {
4468 struct nfs_locku_args arg;
4469 struct nfs_locku_res res;
4470 struct nfs4_lock_state *lsp;
4471 struct nfs_open_context *ctx;
4472 struct file_lock fl;
4473 const struct nfs_server *server;
4474 unsigned long timestamp;
4477 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4478 struct nfs_open_context *ctx,
4479 struct nfs4_lock_state *lsp,
4480 struct nfs_seqid *seqid)
4482 struct nfs4_unlockdata *p;
4483 struct inode *inode = lsp->ls_state->inode;
4485 p = kzalloc(sizeof(*p), GFP_NOFS);
4488 p->arg.fh = NFS_FH(inode);
4490 p->arg.seqid = seqid;
4491 p->res.seqid = seqid;
4492 p->arg.stateid = &lsp->ls_stateid;
4494 atomic_inc(&lsp->ls_count);
4495 /* Ensure we don't close file until we're done freeing locks! */
4496 p->ctx = get_nfs_open_context(ctx);
4497 memcpy(&p->fl, fl, sizeof(p->fl));
4498 p->server = NFS_SERVER(inode);
4502 static void nfs4_locku_release_calldata(void *data)
4504 struct nfs4_unlockdata *calldata = data;
4505 nfs_free_seqid(calldata->arg.seqid);
4506 nfs4_put_lock_state(calldata->lsp);
4507 put_nfs_open_context(calldata->ctx);
4511 static void nfs4_locku_done(struct rpc_task *task, void *data)
4513 struct nfs4_unlockdata *calldata = data;
4515 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4517 switch (task->tk_status) {
4519 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4520 &calldata->res.stateid);
4521 renew_lease(calldata->server, calldata->timestamp);
4523 case -NFS4ERR_BAD_STATEID:
4524 case -NFS4ERR_OLD_STATEID:
4525 case -NFS4ERR_STALE_STATEID:
4526 case -NFS4ERR_EXPIRED:
4529 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4530 rpc_restart_call_prepare(task);
4534 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4536 struct nfs4_unlockdata *calldata = data;
4538 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4540 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
4541 /* Note: exit _without_ running nfs4_locku_done */
4542 task->tk_action = NULL;
4545 calldata->timestamp = jiffies;
4546 if (nfs4_setup_sequence(calldata->server,
4547 &calldata->arg.seq_args,
4548 &calldata->res.seq_res, task))
4550 rpc_call_start(task);
4553 static const struct rpc_call_ops nfs4_locku_ops = {
4554 .rpc_call_prepare = nfs4_locku_prepare,
4555 .rpc_call_done = nfs4_locku_done,
4556 .rpc_release = nfs4_locku_release_calldata,
4559 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4560 struct nfs_open_context *ctx,
4561 struct nfs4_lock_state *lsp,
4562 struct nfs_seqid *seqid)
4564 struct nfs4_unlockdata *data;
4565 struct rpc_message msg = {
4566 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4567 .rpc_cred = ctx->cred,
4569 struct rpc_task_setup task_setup_data = {
4570 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4571 .rpc_message = &msg,
4572 .callback_ops = &nfs4_locku_ops,
4573 .workqueue = nfsiod_workqueue,
4574 .flags = RPC_TASK_ASYNC,
4577 /* Ensure this is an unlock - when canceling a lock, the
4578 * canceled lock is passed in, and it won't be an unlock.
4580 fl->fl_type = F_UNLCK;
4582 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4584 nfs_free_seqid(seqid);
4585 return ERR_PTR(-ENOMEM);
4588 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4589 msg.rpc_argp = &data->arg;
4590 msg.rpc_resp = &data->res;
4591 task_setup_data.callback_data = data;
4592 return rpc_run_task(&task_setup_data);
4595 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4597 struct nfs_inode *nfsi = NFS_I(state->inode);
4598 struct nfs_seqid *seqid;
4599 struct nfs4_lock_state *lsp;
4600 struct rpc_task *task;
4602 unsigned char fl_flags = request->fl_flags;
4604 status = nfs4_set_lock_state(state, request);
4605 /* Unlock _before_ we do the RPC call */
4606 request->fl_flags |= FL_EXISTS;
4607 down_read(&nfsi->rwsem);
4608 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4609 up_read(&nfsi->rwsem);
4612 up_read(&nfsi->rwsem);
4615 /* Is this a delegated lock? */
4616 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4618 lsp = request->fl_u.nfs4_fl.owner;
4619 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4623 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4624 status = PTR_ERR(task);
4627 status = nfs4_wait_for_completion_rpc_task(task);
4630 request->fl_flags = fl_flags;
4634 struct nfs4_lockdata {
4635 struct nfs_lock_args arg;
4636 struct nfs_lock_res res;
4637 struct nfs4_lock_state *lsp;
4638 struct nfs_open_context *ctx;
4639 struct file_lock fl;
4640 unsigned long timestamp;
4643 struct nfs_server *server;
4646 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4647 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4650 struct nfs4_lockdata *p;
4651 struct inode *inode = lsp->ls_state->inode;
4652 struct nfs_server *server = NFS_SERVER(inode);
4654 p = kzalloc(sizeof(*p), gfp_mask);
4658 p->arg.fh = NFS_FH(inode);
4660 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4661 if (p->arg.open_seqid == NULL)
4663 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4664 if (p->arg.lock_seqid == NULL)
4665 goto out_free_seqid;
4666 p->arg.lock_stateid = &lsp->ls_stateid;
4667 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4668 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4669 p->arg.lock_owner.s_dev = server->s_dev;
4670 p->res.lock_seqid = p->arg.lock_seqid;
4673 atomic_inc(&lsp->ls_count);
4674 p->ctx = get_nfs_open_context(ctx);
4675 memcpy(&p->fl, fl, sizeof(p->fl));
4678 nfs_free_seqid(p->arg.open_seqid);
4684 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4686 struct nfs4_lockdata *data = calldata;
4687 struct nfs4_state *state = data->lsp->ls_state;
4689 dprintk("%s: begin!\n", __func__);
4690 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4692 /* Do we need to do an open_to_lock_owner? */
4693 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4694 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4696 data->arg.open_stateid = &state->stateid;
4697 data->arg.new_lock_owner = 1;
4698 data->res.open_seqid = data->arg.open_seqid;
4700 data->arg.new_lock_owner = 0;
4701 data->timestamp = jiffies;
4702 if (nfs4_setup_sequence(data->server,
4703 &data->arg.seq_args,
4704 &data->res.seq_res, task))
4706 rpc_call_start(task);
4707 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4710 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4712 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4713 nfs4_lock_prepare(task, calldata);
4716 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4718 struct nfs4_lockdata *data = calldata;
4720 dprintk("%s: begin!\n", __func__);
4722 if (!nfs4_sequence_done(task, &data->res.seq_res))
4725 data->rpc_status = task->tk_status;
4726 if (data->arg.new_lock_owner != 0) {
4727 if (data->rpc_status == 0)
4728 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4732 if (data->rpc_status == 0) {
4733 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4734 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
4735 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4738 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4741 static void nfs4_lock_release(void *calldata)
4743 struct nfs4_lockdata *data = calldata;
4745 dprintk("%s: begin!\n", __func__);
4746 nfs_free_seqid(data->arg.open_seqid);
4747 if (data->cancelled != 0) {
4748 struct rpc_task *task;
4749 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4750 data->arg.lock_seqid);
4752 rpc_put_task_async(task);
4753 dprintk("%s: cancelling lock!\n", __func__);
4755 nfs_free_seqid(data->arg.lock_seqid);
4756 nfs4_put_lock_state(data->lsp);
4757 put_nfs_open_context(data->ctx);
4759 dprintk("%s: done!\n", __func__);
4762 static const struct rpc_call_ops nfs4_lock_ops = {
4763 .rpc_call_prepare = nfs4_lock_prepare,
4764 .rpc_call_done = nfs4_lock_done,
4765 .rpc_release = nfs4_lock_release,
4768 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4769 .rpc_call_prepare = nfs4_recover_lock_prepare,
4770 .rpc_call_done = nfs4_lock_done,
4771 .rpc_release = nfs4_lock_release,
4774 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4777 case -NFS4ERR_ADMIN_REVOKED:
4778 case -NFS4ERR_BAD_STATEID:
4779 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4780 if (new_lock_owner != 0 ||
4781 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
4782 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4784 case -NFS4ERR_STALE_STATEID:
4785 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4786 case -NFS4ERR_EXPIRED:
4787 nfs4_schedule_lease_recovery(server->nfs_client);
4791 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4793 struct nfs4_lockdata *data;
4794 struct rpc_task *task;
4795 struct rpc_message msg = {
4796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4797 .rpc_cred = state->owner->so_cred,
4799 struct rpc_task_setup task_setup_data = {
4800 .rpc_client = NFS_CLIENT(state->inode),
4801 .rpc_message = &msg,
4802 .callback_ops = &nfs4_lock_ops,
4803 .workqueue = nfsiod_workqueue,
4804 .flags = RPC_TASK_ASYNC,
4808 dprintk("%s: begin!\n", __func__);
4809 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4810 fl->fl_u.nfs4_fl.owner,
4811 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4815 data->arg.block = 1;
4816 if (recovery_type > NFS_LOCK_NEW) {
4817 if (recovery_type == NFS_LOCK_RECLAIM)
4818 data->arg.reclaim = NFS_LOCK_RECLAIM;
4819 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4821 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4822 msg.rpc_argp = &data->arg;
4823 msg.rpc_resp = &data->res;
4824 task_setup_data.callback_data = data;
4825 task = rpc_run_task(&task_setup_data);
4827 return PTR_ERR(task);
4828 ret = nfs4_wait_for_completion_rpc_task(task);
4830 ret = data->rpc_status;
4832 nfs4_handle_setlk_error(data->server, data->lsp,
4833 data->arg.new_lock_owner, ret);
4835 data->cancelled = 1;
4837 dprintk("%s: done, ret = %d!\n", __func__, ret);
4841 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4843 struct nfs_server *server = NFS_SERVER(state->inode);
4844 struct nfs4_exception exception = {
4845 .inode = state->inode,
4850 /* Cache the lock if possible... */
4851 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4853 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4854 if (err != -NFS4ERR_DELAY)
4856 nfs4_handle_exception(server, err, &exception);
4857 } while (exception.retry);
4861 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4863 struct nfs_server *server = NFS_SERVER(state->inode);
4864 struct nfs4_exception exception = {
4865 .inode = state->inode,
4869 err = nfs4_set_lock_state(state, request);
4873 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4875 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4879 case -NFS4ERR_GRACE:
4880 case -NFS4ERR_DELAY:
4881 nfs4_handle_exception(server, err, &exception);
4884 } while (exception.retry);
4889 #if defined(CONFIG_NFS_V4_1)
4891 * nfs41_check_expired_locks - possibly free a lock stateid
4893 * @state: NFSv4 state for an inode
4895 * Returns NFS_OK if recovery for this stateid is now finished.
4896 * Otherwise a negative NFS4ERR value is returned.
4898 static int nfs41_check_expired_locks(struct nfs4_state *state)
4900 int status, ret = -NFS4ERR_BAD_STATEID;
4901 struct nfs4_lock_state *lsp;
4902 struct nfs_server *server = NFS_SERVER(state->inode);
4904 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4905 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
4906 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4907 if (status != NFS_OK) {
4908 /* Free the stateid unless the server
4909 * informs us the stateid is unrecognized. */
4910 if (status != -NFS4ERR_BAD_STATEID)
4911 nfs41_free_stateid(server,
4913 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
4922 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4924 int status = NFS_OK;
4926 if (test_bit(LK_STATE_IN_USE, &state->flags))
4927 status = nfs41_check_expired_locks(state);
4928 if (status != NFS_OK)
4929 status = nfs4_lock_expired(state, request);
4934 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4936 struct nfs_inode *nfsi = NFS_I(state->inode);
4937 unsigned char fl_flags = request->fl_flags;
4938 int status = -ENOLCK;
4940 if ((fl_flags & FL_POSIX) &&
4941 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4943 /* Is this a delegated open? */
4944 status = nfs4_set_lock_state(state, request);
4947 request->fl_flags |= FL_ACCESS;
4948 status = do_vfs_lock(request->fl_file, request);
4951 down_read(&nfsi->rwsem);
4952 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4953 /* Yes: cache locks! */
4954 /* ...but avoid races with delegation recall... */
4955 request->fl_flags = fl_flags & ~FL_SLEEP;
4956 status = do_vfs_lock(request->fl_file, request);
4959 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4962 /* Note: we always want to sleep here! */
4963 request->fl_flags = fl_flags | FL_SLEEP;
4964 if (do_vfs_lock(request->fl_file, request) < 0)
4965 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4966 "manager!\n", __func__);
4968 up_read(&nfsi->rwsem);
4970 request->fl_flags = fl_flags;
4974 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4976 struct nfs4_exception exception = {
4978 .inode = state->inode,
4983 err = _nfs4_proc_setlk(state, cmd, request);
4984 if (err == -NFS4ERR_DENIED)
4986 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4988 } while (exception.retry);
4993 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4995 struct nfs_open_context *ctx;
4996 struct nfs4_state *state;
4997 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5000 /* verify open state */
5001 ctx = nfs_file_open_context(filp);
5004 if (request->fl_start < 0 || request->fl_end < 0)
5007 if (IS_GETLK(cmd)) {
5009 return nfs4_proc_getlk(state, F_GETLK, request);
5013 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5016 if (request->fl_type == F_UNLCK) {
5018 return nfs4_proc_unlck(state, cmd, request);
5025 * Don't rely on the VFS having checked the file open mode,
5026 * since it won't do this for flock() locks.
5028 switch (request->fl_type) {
5030 if (!(filp->f_mode & FMODE_READ))
5034 if (!(filp->f_mode & FMODE_WRITE))
5039 status = nfs4_proc_setlk(state, cmd, request);
5040 if ((status != -EAGAIN) || IS_SETLK(cmd))
5042 timeout = nfs4_set_lock_task_retry(timeout);
5043 status = -ERESTARTSYS;
5046 } while(status < 0);
5050 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5052 struct nfs_server *server = NFS_SERVER(state->inode);
5053 struct nfs4_exception exception = { };
5056 err = nfs4_set_lock_state(state, fl);
5060 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
5063 printk(KERN_ERR "NFS: %s: unhandled error "
5064 "%d.\n", __func__, err);
5068 case -NFS4ERR_EXPIRED:
5069 nfs4_schedule_stateid_recovery(server, state);
5070 case -NFS4ERR_STALE_CLIENTID:
5071 case -NFS4ERR_STALE_STATEID:
5072 nfs4_schedule_lease_recovery(server->nfs_client);
5074 case -NFS4ERR_BADSESSION:
5075 case -NFS4ERR_BADSLOT:
5076 case -NFS4ERR_BAD_HIGH_SLOT:
5077 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5078 case -NFS4ERR_DEADSESSION:
5079 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
5083 * The show must go on: exit, but mark the
5084 * stateid as needing recovery.
5086 case -NFS4ERR_DELEG_REVOKED:
5087 case -NFS4ERR_ADMIN_REVOKED:
5088 case -NFS4ERR_BAD_STATEID:
5089 case -NFS4ERR_OPENMODE:
5090 nfs4_schedule_stateid_recovery(server, state);
5095 * User RPCSEC_GSS context has expired.
5096 * We cannot recover this stateid now, so
5097 * skip it and allow recovery thread to
5103 case -NFS4ERR_DENIED:
5104 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5107 case -NFS4ERR_DELAY:
5110 err = nfs4_handle_exception(server, err, &exception);
5111 } while (exception.retry);
5116 struct nfs_release_lockowner_data {
5117 struct nfs4_lock_state *lsp;
5118 struct nfs_server *server;
5119 struct nfs_release_lockowner_args args;
5122 static void nfs4_release_lockowner_release(void *calldata)
5124 struct nfs_release_lockowner_data *data = calldata;
5125 nfs4_free_lock_state(data->server, data->lsp);
5129 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5130 .rpc_release = nfs4_release_lockowner_release,
5133 int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
5135 struct nfs_server *server = lsp->ls_state->owner->so_server;
5136 struct nfs_release_lockowner_data *data;
5137 struct rpc_message msg = {
5138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5141 if (server->nfs_client->cl_mvops->minor_version != 0)
5143 data = kmalloc(sizeof(*data), GFP_NOFS);
5147 data->server = server;
5148 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5149 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5150 data->args.lock_owner.s_dev = server->s_dev;
5151 msg.rpc_argp = &data->args;
5152 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5156 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5158 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5159 const void *buf, size_t buflen,
5160 int flags, int type)
5162 if (strcmp(key, "") != 0)
5165 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5168 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5169 void *buf, size_t buflen, int type)
5171 if (strcmp(key, "") != 0)
5174 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5177 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5178 size_t list_len, const char *name,
5179 size_t name_len, int type)
5181 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5183 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5186 if (list && len <= list_len)
5187 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5192 * nfs_fhget will use either the mounted_on_fileid or the fileid
5194 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5196 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5197 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5198 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
5199 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
5202 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
5203 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
5204 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5208 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5209 const struct qstr *name,
5210 struct nfs4_fs_locations *fs_locations,
5213 struct nfs_server *server = NFS_SERVER(dir);
5215 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
5217 struct nfs4_fs_locations_arg args = {
5218 .dir_fh = NFS_FH(dir),
5223 struct nfs4_fs_locations_res res = {
5224 .fs_locations = fs_locations,
5226 struct rpc_message msg = {
5227 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5233 dprintk("%s: start\n", __func__);
5235 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5236 * is not supported */
5237 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5238 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5240 bitmask[0] |= FATTR4_WORD0_FILEID;
5242 nfs_fattr_init(&fs_locations->fattr);
5243 fs_locations->server = server;
5244 fs_locations->nlocations = 0;
5245 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
5246 dprintk("%s: returned status = %d\n", __func__, status);
5250 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5251 const struct qstr *name,
5252 struct nfs4_fs_locations *fs_locations,
5255 struct nfs4_exception exception = { };
5258 err = nfs4_handle_exception(NFS_SERVER(dir),
5259 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
5261 } while (exception.retry);
5265 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5268 struct nfs4_secinfo_arg args = {
5269 .dir_fh = NFS_FH(dir),
5272 struct nfs4_secinfo_res res = {
5275 struct rpc_message msg = {
5276 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5281 dprintk("NFS call secinfo %s\n", name->name);
5282 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5283 dprintk("NFS reply secinfo: %d\n", status);
5287 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5288 struct nfs4_secinfo_flavors *flavors)
5290 struct nfs4_exception exception = { };
5293 err = nfs4_handle_exception(NFS_SERVER(dir),
5294 _nfs4_proc_secinfo(dir, name, flavors),
5296 } while (exception.retry);
5300 #ifdef CONFIG_NFS_V4_1
5302 * Check the exchange flags returned by the server for invalid flags, having
5303 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5306 static int nfs4_check_cl_exchange_flags(u32 flags)
5308 if (flags & ~EXCHGID4_FLAG_MASK_R)
5310 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5311 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5313 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5317 return -NFS4ERR_INVAL;
5321 nfs41_same_server_scope(struct nfs41_server_scope *a,
5322 struct nfs41_server_scope *b)
5324 if (a->server_scope_sz == b->server_scope_sz &&
5325 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5332 * nfs4_proc_bind_conn_to_session()
5334 * The 4.1 client currently uses the same TCP connection for the
5335 * fore and backchannel.
5337 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
5340 struct nfs41_bind_conn_to_session_res res;
5341 struct rpc_message msg = {
5343 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5349 dprintk("--> %s\n", __func__);
5350 BUG_ON(clp == NULL);
5352 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5353 if (unlikely(res.session == NULL)) {
5358 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5360 if (memcmp(res.session->sess_id.data,
5361 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5362 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5366 if (res.dir != NFS4_CDFS4_BOTH) {
5367 dprintk("NFS: %s: Unexpected direction from server\n",
5372 if (res.use_conn_in_rdma_mode) {
5373 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5382 dprintk("<-- %s status= %d\n", __func__, status);
5387 * nfs4_proc_exchange_id()
5389 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5391 * Since the clientid has expired, all compounds using sessions
5392 * associated with the stale clientid will be returning
5393 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5394 * be in some phase of session reset.
5396 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
5398 nfs4_verifier verifier;
5399 struct nfs41_exchange_id_args args = {
5400 .verifier = &verifier,
5402 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
5404 struct nfs41_exchange_id_res res = {
5408 struct rpc_message msg = {
5409 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5415 nfs4_init_boot_verifier(clp, &verifier);
5416 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5418 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5419 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5420 args.id_len, args.id);
5422 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
5424 if (unlikely(res.server_owner == NULL)) {
5429 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
5431 if (unlikely(res.server_scope == NULL)) {
5433 goto out_server_owner;
5436 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
5437 if (unlikely(res.impl_id == NULL)) {
5439 goto out_server_scope;
5442 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5444 status = nfs4_check_cl_exchange_flags(res.flags);
5447 clp->cl_clientid = res.clientid;
5448 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5449 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5450 clp->cl_seqid = res.seqid;
5452 kfree(clp->cl_serverowner);
5453 clp->cl_serverowner = res.server_owner;
5454 res.server_owner = NULL;
5456 /* use the most recent implementation id */
5457 kfree(clp->cl_implid);
5458 clp->cl_implid = res.impl_id;
5460 if (clp->cl_serverscope != NULL &&
5461 !nfs41_same_server_scope(clp->cl_serverscope,
5462 res.server_scope)) {
5463 dprintk("%s: server_scope mismatch detected\n",
5465 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5466 kfree(clp->cl_serverscope);
5467 clp->cl_serverscope = NULL;
5470 if (clp->cl_serverscope == NULL) {
5471 clp->cl_serverscope = res.server_scope;
5478 kfree(res.server_owner);
5480 kfree(res.server_scope);
5482 if (clp->cl_implid != NULL)
5483 dprintk("NFS reply exchange_id: Server Implementation ID: "
5484 "domain: %s, name: %s, date: %llu,%u\n",
5485 clp->cl_implid->domain, clp->cl_implid->name,
5486 clp->cl_implid->date.seconds,
5487 clp->cl_implid->date.nseconds);
5488 dprintk("NFS reply exchange_id: %d\n", status);
5492 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5493 struct rpc_cred *cred)
5495 struct rpc_message msg = {
5496 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5502 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5504 dprintk("NFS: Got error %d from the server %s on "
5505 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5509 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5510 struct rpc_cred *cred)
5515 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5516 ret = _nfs4_proc_destroy_clientid(clp, cred);
5518 case -NFS4ERR_DELAY:
5519 case -NFS4ERR_CLIENTID_BUSY:
5529 int nfs4_destroy_clientid(struct nfs_client *clp)
5531 struct rpc_cred *cred;
5534 if (clp->cl_mvops->minor_version < 1)
5536 if (clp->cl_exchange_flags == 0)
5538 if (clp->cl_preserve_clid)
5540 cred = nfs4_get_exchange_id_cred(clp);
5541 ret = nfs4_proc_destroy_clientid(clp, cred);
5546 case -NFS4ERR_STALE_CLIENTID:
5547 clp->cl_exchange_flags = 0;
5553 struct nfs4_get_lease_time_data {
5554 struct nfs4_get_lease_time_args *args;
5555 struct nfs4_get_lease_time_res *res;
5556 struct nfs_client *clp;
5559 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5563 struct nfs4_get_lease_time_data *data =
5564 (struct nfs4_get_lease_time_data *)calldata;
5566 dprintk("--> %s\n", __func__);
5567 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5568 /* just setup sequence, do not trigger session recovery
5569 since we're invoked within one */
5570 ret = nfs41_setup_sequence(data->clp->cl_session,
5571 &data->args->la_seq_args,
5572 &data->res->lr_seq_res, task);
5574 BUG_ON(ret == -EAGAIN);
5575 rpc_call_start(task);
5576 dprintk("<-- %s\n", __func__);
5580 * Called from nfs4_state_manager thread for session setup, so don't recover
5581 * from sequence operation or clientid errors.
5583 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5585 struct nfs4_get_lease_time_data *data =
5586 (struct nfs4_get_lease_time_data *)calldata;
5588 dprintk("--> %s\n", __func__);
5589 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5591 switch (task->tk_status) {
5592 case -NFS4ERR_DELAY:
5593 case -NFS4ERR_GRACE:
5594 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5595 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5596 task->tk_status = 0;
5598 case -NFS4ERR_RETRY_UNCACHED_REP:
5599 rpc_restart_call_prepare(task);
5602 dprintk("<-- %s\n", __func__);
5605 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5606 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5607 .rpc_call_done = nfs4_get_lease_time_done,
5610 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5612 struct rpc_task *task;
5613 struct nfs4_get_lease_time_args args;
5614 struct nfs4_get_lease_time_res res = {
5615 .lr_fsinfo = fsinfo,
5617 struct nfs4_get_lease_time_data data = {
5622 struct rpc_message msg = {
5623 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5627 struct rpc_task_setup task_setup = {
5628 .rpc_client = clp->cl_rpcclient,
5629 .rpc_message = &msg,
5630 .callback_ops = &nfs4_get_lease_time_ops,
5631 .callback_data = &data,
5632 .flags = RPC_TASK_TIMEOUT,
5636 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5637 dprintk("--> %s\n", __func__);
5638 task = rpc_run_task(&task_setup);
5641 status = PTR_ERR(task);
5643 status = task->tk_status;
5646 dprintk("<-- %s return %d\n", __func__, status);
5651 static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5653 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5656 static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5657 struct nfs4_slot *new,
5661 struct nfs4_slot *old = NULL;
5664 spin_lock(&tbl->slot_tbl_lock);
5668 tbl->max_slots = max_slots;
5670 tbl->highest_used_slotid = -1; /* no slot is currently used */
5671 for (i = 0; i < tbl->max_slots; i++)
5672 tbl->slots[i].seq_nr = ivalue;
5673 spin_unlock(&tbl->slot_tbl_lock);
5678 * (re)Initialise a slot table
5680 static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5683 struct nfs4_slot *new = NULL;
5686 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5687 max_reqs, tbl->max_slots);
5689 /* Does the newly negotiated max_reqs match the existing slot table? */
5690 if (max_reqs != tbl->max_slots) {
5691 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
5697 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
5698 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5699 tbl, tbl->slots, tbl->max_slots);
5701 dprintk("<-- %s: return %d\n", __func__, ret);
5705 /* Destroy the slot table */
5706 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5708 if (session->fc_slot_table.slots != NULL) {
5709 kfree(session->fc_slot_table.slots);
5710 session->fc_slot_table.slots = NULL;
5712 if (session->bc_slot_table.slots != NULL) {
5713 kfree(session->bc_slot_table.slots);
5714 session->bc_slot_table.slots = NULL;
5720 * Initialize or reset the forechannel and backchannel tables
5722 static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
5724 struct nfs4_slot_table *tbl;
5727 dprintk("--> %s\n", __func__);
5729 tbl = &ses->fc_slot_table;
5730 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5731 if (status) /* -ENOMEM */
5734 tbl = &ses->bc_slot_table;
5735 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5736 if (status && tbl->slots == NULL)
5737 /* Fore and back channel share a connection so get
5738 * both slot tables or neither */
5739 nfs4_destroy_slot_tables(ses);
5743 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5745 struct nfs4_session *session;
5746 struct nfs4_slot_table *tbl;
5748 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5752 tbl = &session->fc_slot_table;
5753 tbl->highest_used_slotid = NFS4_NO_SLOT;
5754 spin_lock_init(&tbl->slot_tbl_lock);
5755 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5756 init_completion(&tbl->complete);
5758 tbl = &session->bc_slot_table;
5759 tbl->highest_used_slotid = NFS4_NO_SLOT;
5760 spin_lock_init(&tbl->slot_tbl_lock);
5761 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5762 init_completion(&tbl->complete);
5764 session->session_state = 1<<NFS4_SESSION_INITING;
5770 void nfs4_destroy_session(struct nfs4_session *session)
5772 struct rpc_xprt *xprt;
5773 struct rpc_cred *cred;
5775 cred = nfs4_get_exchange_id_cred(session->clp);
5776 nfs4_proc_destroy_session(session, cred);
5781 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5783 dprintk("%s Destroy backchannel for xprt %p\n",
5785 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
5786 nfs4_destroy_slot_tables(session);
5791 * Initialize the values to be used by the client in CREATE_SESSION
5792 * If nfs4_init_session set the fore channel request and response sizes,
5795 * Set the back channel max_resp_sz_cached to zero to force the client to
5796 * always set csa_cachethis to FALSE because the current implementation
5797 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5799 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5801 struct nfs4_session *session = args->client->cl_session;
5802 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5803 mxresp_sz = session->fc_attrs.max_resp_sz;
5806 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5808 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5809 /* Fore channel attributes */
5810 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5811 args->fc_attrs.max_resp_sz = mxresp_sz;
5812 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5813 args->fc_attrs.max_reqs = max_session_slots;
5815 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5816 "max_ops=%u max_reqs=%u\n",
5818 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5819 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5821 /* Back channel attributes */
5822 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5823 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5824 args->bc_attrs.max_resp_sz_cached = 0;
5825 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5826 args->bc_attrs.max_reqs = 1;
5828 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5829 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5831 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5832 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5833 args->bc_attrs.max_reqs);
5836 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5838 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5839 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5841 if (rcvd->max_resp_sz > sent->max_resp_sz)
5844 * Our requested max_ops is the minimum we need; we're not
5845 * prepared to break up compounds into smaller pieces than that.
5846 * So, no point even trying to continue if the server won't
5849 if (rcvd->max_ops < sent->max_ops)
5851 if (rcvd->max_reqs == 0)
5853 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5854 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5858 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5860 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5861 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5863 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5865 if (rcvd->max_resp_sz < sent->max_resp_sz)
5867 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5869 /* These would render the backchannel useless: */
5870 if (rcvd->max_ops != sent->max_ops)
5872 if (rcvd->max_reqs != sent->max_reqs)
5877 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5878 struct nfs4_session *session)
5882 ret = nfs4_verify_fore_channel_attrs(args, session);
5885 return nfs4_verify_back_channel_attrs(args, session);
5888 static int _nfs4_proc_create_session(struct nfs_client *clp,
5889 struct rpc_cred *cred)
5891 struct nfs4_session *session = clp->cl_session;
5892 struct nfs41_create_session_args args = {
5894 .cb_program = NFS4_CALLBACK,
5896 struct nfs41_create_session_res res = {
5899 struct rpc_message msg = {
5900 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5907 nfs4_init_channel_attrs(&args);
5908 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5910 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5913 /* Verify the session's negotiated channel_attrs values */
5914 status = nfs4_verify_channel_attrs(&args, session);
5916 /* Increment the clientid slot sequence id */
5924 * Issues a CREATE_SESSION operation to the server.
5925 * It is the responsibility of the caller to verify the session is
5926 * expired before calling this routine.
5928 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
5932 struct nfs4_session *session = clp->cl_session;
5934 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5936 status = _nfs4_proc_create_session(clp, cred);
5940 /* Init or reset the session slot tables */
5941 status = nfs4_setup_session_slot_tables(session);
5942 dprintk("slot table setup returned %d\n", status);
5946 ptr = (unsigned *)&session->sess_id.data[0];
5947 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5948 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5950 dprintk("<-- %s\n", __func__);
5955 * Issue the over-the-wire RPC DESTROY_SESSION.
5956 * The caller must serialize access to this routine.
5958 int nfs4_proc_destroy_session(struct nfs4_session *session,
5959 struct rpc_cred *cred)
5961 struct rpc_message msg = {
5962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5963 .rpc_argp = session,
5968 dprintk("--> nfs4_proc_destroy_session\n");
5970 /* session is still being setup */
5971 if (session->clp->cl_cons_state != NFS_CS_READY)
5974 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5977 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
5978 "Session has been destroyed regardless...\n", status);
5980 dprintk("<-- nfs4_proc_destroy_session\n");
5985 * With sessions, the client is not marked ready until after a
5986 * successful EXCHANGE_ID and CREATE_SESSION.
5988 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5989 * other versions of NFS can be tried.
5991 static int nfs41_check_session_ready(struct nfs_client *clp)
5995 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5996 ret = nfs4_client_recover_expired_lease(clp);
6000 if (clp->cl_cons_state < NFS_CS_READY)
6001 return -EPROTONOSUPPORT;
6006 int nfs4_init_session(struct nfs_server *server)
6008 struct nfs_client *clp = server->nfs_client;
6009 struct nfs4_session *session;
6010 unsigned int rsize, wsize;
6012 if (!nfs4_has_session(clp))
6015 session = clp->cl_session;
6016 spin_lock(&clp->cl_lock);
6017 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6019 rsize = server->rsize;
6021 rsize = NFS_MAX_FILE_IO_SIZE;
6022 wsize = server->wsize;
6024 wsize = NFS_MAX_FILE_IO_SIZE;
6026 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
6027 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
6029 spin_unlock(&clp->cl_lock);
6031 return nfs41_check_session_ready(clp);
6034 int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
6036 struct nfs4_session *session = clp->cl_session;
6039 spin_lock(&clp->cl_lock);
6040 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6042 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
6043 * DS lease to be equal to the MDS lease.
6045 clp->cl_lease_time = lease_time;
6046 clp->cl_last_renewal = jiffies;
6048 spin_unlock(&clp->cl_lock);
6050 ret = nfs41_check_session_ready(clp);
6053 /* Test for the DS role */
6054 if (!is_ds_client(clp))
6058 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
6062 * Renew the cl_session lease.
6064 struct nfs4_sequence_data {
6065 struct nfs_client *clp;
6066 struct nfs4_sequence_args args;
6067 struct nfs4_sequence_res res;
6070 static void nfs41_sequence_release(void *data)
6072 struct nfs4_sequence_data *calldata = data;
6073 struct nfs_client *clp = calldata->clp;
6075 if (atomic_read(&clp->cl_count) > 1)
6076 nfs4_schedule_state_renewal(clp);
6077 nfs_put_client(clp);
6081 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6083 switch(task->tk_status) {
6084 case -NFS4ERR_DELAY:
6085 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6088 nfs4_schedule_lease_recovery(clp);
6093 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
6095 struct nfs4_sequence_data *calldata = data;
6096 struct nfs_client *clp = calldata->clp;
6098 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6101 if (task->tk_status < 0) {
6102 dprintk("%s ERROR %d\n", __func__, task->tk_status);
6103 if (atomic_read(&clp->cl_count) == 1)
6106 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6107 rpc_restart_call_prepare(task);
6111 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
6113 dprintk("<-- %s\n", __func__);
6116 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6118 struct nfs4_sequence_data *calldata = data;
6119 struct nfs_client *clp = calldata->clp;
6120 struct nfs4_sequence_args *args;
6121 struct nfs4_sequence_res *res;
6123 args = task->tk_msg.rpc_argp;
6124 res = task->tk_msg.rpc_resp;
6126 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
6128 rpc_call_start(task);
6131 static const struct rpc_call_ops nfs41_sequence_ops = {
6132 .rpc_call_done = nfs41_sequence_call_done,
6133 .rpc_call_prepare = nfs41_sequence_prepare,
6134 .rpc_release = nfs41_sequence_release,
6137 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6139 struct nfs4_sequence_data *calldata;
6140 struct rpc_message msg = {
6141 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6144 struct rpc_task_setup task_setup_data = {
6145 .rpc_client = clp->cl_rpcclient,
6146 .rpc_message = &msg,
6147 .callback_ops = &nfs41_sequence_ops,
6148 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6151 if (!atomic_inc_not_zero(&clp->cl_count))
6152 return ERR_PTR(-EIO);
6153 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6154 if (calldata == NULL) {
6155 nfs_put_client(clp);
6156 return ERR_PTR(-ENOMEM);
6158 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
6159 msg.rpc_argp = &calldata->args;
6160 msg.rpc_resp = &calldata->res;
6161 calldata->clp = clp;
6162 task_setup_data.callback_data = calldata;
6164 return rpc_run_task(&task_setup_data);
6167 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
6169 struct rpc_task *task;
6172 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6174 task = _nfs41_proc_sequence(clp, cred);
6176 ret = PTR_ERR(task);
6178 rpc_put_task_async(task);
6179 dprintk("<-- %s status=%d\n", __func__, ret);
6183 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6185 struct rpc_task *task;
6188 task = _nfs41_proc_sequence(clp, cred);
6190 ret = PTR_ERR(task);
6193 ret = rpc_wait_for_completion_task(task);
6195 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6197 if (task->tk_status == 0)
6198 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
6199 ret = task->tk_status;
6203 dprintk("<-- %s status=%d\n", __func__, ret);
6207 struct nfs4_reclaim_complete_data {
6208 struct nfs_client *clp;
6209 struct nfs41_reclaim_complete_args arg;
6210 struct nfs41_reclaim_complete_res res;
6213 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6215 struct nfs4_reclaim_complete_data *calldata = data;
6217 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6218 if (nfs41_setup_sequence(calldata->clp->cl_session,
6219 &calldata->arg.seq_args,
6220 &calldata->res.seq_res, task))
6223 rpc_call_start(task);
6226 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6228 switch(task->tk_status) {
6230 case -NFS4ERR_COMPLETE_ALREADY:
6231 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6233 case -NFS4ERR_DELAY:
6234 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6236 case -NFS4ERR_RETRY_UNCACHED_REP:
6239 nfs4_schedule_lease_recovery(clp);
6244 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6246 struct nfs4_reclaim_complete_data *calldata = data;
6247 struct nfs_client *clp = calldata->clp;
6248 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6250 dprintk("--> %s\n", __func__);
6251 if (!nfs41_sequence_done(task, res))
6254 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6255 rpc_restart_call_prepare(task);
6258 dprintk("<-- %s\n", __func__);
6261 static void nfs4_free_reclaim_complete_data(void *data)
6263 struct nfs4_reclaim_complete_data *calldata = data;
6268 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6269 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6270 .rpc_call_done = nfs4_reclaim_complete_done,
6271 .rpc_release = nfs4_free_reclaim_complete_data,
6275 * Issue a global reclaim complete.
6277 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6279 struct nfs4_reclaim_complete_data *calldata;
6280 struct rpc_task *task;
6281 struct rpc_message msg = {
6282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6284 struct rpc_task_setup task_setup_data = {
6285 .rpc_client = clp->cl_rpcclient,
6286 .rpc_message = &msg,
6287 .callback_ops = &nfs4_reclaim_complete_call_ops,
6288 .flags = RPC_TASK_ASYNC,
6290 int status = -ENOMEM;
6292 dprintk("--> %s\n", __func__);
6293 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6294 if (calldata == NULL)
6296 calldata->clp = clp;
6297 calldata->arg.one_fs = 0;
6299 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
6300 msg.rpc_argp = &calldata->arg;
6301 msg.rpc_resp = &calldata->res;
6302 task_setup_data.callback_data = calldata;
6303 task = rpc_run_task(&task_setup_data);
6305 status = PTR_ERR(task);
6308 status = nfs4_wait_for_completion_rpc_task(task);
6310 status = task->tk_status;
6314 dprintk("<-- %s status=%d\n", __func__, status);
6319 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6321 struct nfs4_layoutget *lgp = calldata;
6322 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6324 dprintk("--> %s\n", __func__);
6325 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6326 * right now covering the LAYOUTGET we are about to send.
6327 * However, that is not so catastrophic, and there seems
6328 * to be no way to prevent it completely.
6330 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
6331 &lgp->res.seq_res, task))
6333 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6334 NFS_I(lgp->args.inode)->layout,
6335 lgp->args.ctx->state)) {
6336 rpc_exit(task, NFS4_OK);
6339 rpc_call_start(task);
6342 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6344 struct nfs4_layoutget *lgp = calldata;
6345 struct inode *inode = lgp->args.inode;
6346 struct nfs_server *server = NFS_SERVER(inode);
6347 struct pnfs_layout_hdr *lo;
6348 struct nfs4_state *state = NULL;
6350 dprintk("--> %s\n", __func__);
6352 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6355 switch (task->tk_status) {
6358 case -NFS4ERR_LAYOUTTRYLATER:
6359 case -NFS4ERR_RECALLCONFLICT:
6360 task->tk_status = -NFS4ERR_DELAY;
6362 case -NFS4ERR_EXPIRED:
6363 case -NFS4ERR_BAD_STATEID:
6364 spin_lock(&inode->i_lock);
6365 lo = NFS_I(inode)->layout;
6366 if (!lo || list_empty(&lo->plh_segs)) {
6367 spin_unlock(&inode->i_lock);
6368 /* If the open stateid was bad, then recover it. */
6369 state = lgp->args.ctx->state;
6373 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6374 spin_unlock(&inode->i_lock);
6375 /* Mark the bad layout state as invalid, then
6376 * retry using the open stateid. */
6377 pnfs_free_lseg_list(&head);
6380 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6381 rpc_restart_call_prepare(task);
6383 dprintk("<-- %s\n", __func__);
6386 static size_t max_response_pages(struct nfs_server *server)
6388 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6389 return nfs_page_array_len(0, max_resp_sz);
6392 static void nfs4_free_pages(struct page **pages, size_t size)
6399 for (i = 0; i < size; i++) {
6402 __free_page(pages[i]);
6407 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6409 struct page **pages;
6412 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6414 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6418 for (i = 0; i < size; i++) {
6419 pages[i] = alloc_page(gfp_flags);
6421 dprintk("%s: failed to allocate page\n", __func__);
6422 nfs4_free_pages(pages, size);
6430 static void nfs4_layoutget_release(void *calldata)
6432 struct nfs4_layoutget *lgp = calldata;
6433 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6434 size_t max_pages = max_response_pages(server);
6436 dprintk("--> %s\n", __func__);
6437 nfs4_free_pages(lgp->args.layout.pages, max_pages);
6438 put_nfs_open_context(lgp->args.ctx);
6440 dprintk("<-- %s\n", __func__);
6443 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6444 .rpc_call_prepare = nfs4_layoutget_prepare,
6445 .rpc_call_done = nfs4_layoutget_done,
6446 .rpc_release = nfs4_layoutget_release,
6449 struct pnfs_layout_segment *
6450 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
6452 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6453 size_t max_pages = max_response_pages(server);
6454 struct rpc_task *task;
6455 struct rpc_message msg = {
6456 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6457 .rpc_argp = &lgp->args,
6458 .rpc_resp = &lgp->res,
6460 struct rpc_task_setup task_setup_data = {
6461 .rpc_client = server->client,
6462 .rpc_message = &msg,
6463 .callback_ops = &nfs4_layoutget_call_ops,
6464 .callback_data = lgp,
6465 .flags = RPC_TASK_ASYNC,
6467 struct pnfs_layout_segment *lseg = NULL;
6470 dprintk("--> %s\n", __func__);
6472 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6473 if (!lgp->args.layout.pages) {
6474 nfs4_layoutget_release(lgp);
6475 return ERR_PTR(-ENOMEM);
6477 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6479 lgp->res.layoutp = &lgp->args.layout;
6480 lgp->res.seq_res.sr_slot = NULL;
6481 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
6482 task = rpc_run_task(&task_setup_data);
6484 return ERR_CAST(task);
6485 status = nfs4_wait_for_completion_rpc_task(task);
6487 status = task->tk_status;
6489 lseg = pnfs_layout_process(lgp);
6491 dprintk("<-- %s status=%d\n", __func__, status);
6493 return ERR_PTR(status);
6498 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6500 struct nfs4_layoutreturn *lrp = calldata;
6502 dprintk("--> %s\n", __func__);
6503 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
6504 &lrp->res.seq_res, task))
6506 rpc_call_start(task);
6509 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6511 struct nfs4_layoutreturn *lrp = calldata;
6512 struct nfs_server *server;
6514 dprintk("--> %s\n", __func__);
6516 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6519 server = NFS_SERVER(lrp->args.inode);
6520 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6521 rpc_restart_call_prepare(task);
6524 dprintk("<-- %s\n", __func__);
6527 static void nfs4_layoutreturn_release(void *calldata)
6529 struct nfs4_layoutreturn *lrp = calldata;
6530 struct pnfs_layout_hdr *lo = lrp->args.layout;
6532 dprintk("--> %s\n", __func__);
6533 spin_lock(&lo->plh_inode->i_lock);
6534 if (lrp->res.lrs_present)
6535 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6536 lo->plh_block_lgets--;
6537 spin_unlock(&lo->plh_inode->i_lock);
6538 pnfs_put_layout_hdr(lrp->args.layout);
6540 dprintk("<-- %s\n", __func__);
6543 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6544 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6545 .rpc_call_done = nfs4_layoutreturn_done,
6546 .rpc_release = nfs4_layoutreturn_release,
6549 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6551 struct rpc_task *task;
6552 struct rpc_message msg = {
6553 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6554 .rpc_argp = &lrp->args,
6555 .rpc_resp = &lrp->res,
6557 struct rpc_task_setup task_setup_data = {
6558 .rpc_client = lrp->clp->cl_rpcclient,
6559 .rpc_message = &msg,
6560 .callback_ops = &nfs4_layoutreturn_call_ops,
6561 .callback_data = lrp,
6565 dprintk("--> %s\n", __func__);
6566 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
6567 task = rpc_run_task(&task_setup_data);
6569 return PTR_ERR(task);
6570 status = task->tk_status;
6571 dprintk("<-- %s status=%d\n", __func__, status);
6577 * Retrieve the list of Data Server devices from the MDS.
6579 static int _nfs4_getdevicelist(struct nfs_server *server,
6580 const struct nfs_fh *fh,
6581 struct pnfs_devicelist *devlist)
6583 struct nfs4_getdevicelist_args args = {
6585 .layoutclass = server->pnfs_curr_ld->id,
6587 struct nfs4_getdevicelist_res res = {
6590 struct rpc_message msg = {
6591 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6597 dprintk("--> %s\n", __func__);
6598 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6600 dprintk("<-- %s status=%d\n", __func__, status);
6604 int nfs4_proc_getdevicelist(struct nfs_server *server,
6605 const struct nfs_fh *fh,
6606 struct pnfs_devicelist *devlist)
6608 struct nfs4_exception exception = { };
6612 err = nfs4_handle_exception(server,
6613 _nfs4_getdevicelist(server, fh, devlist),
6615 } while (exception.retry);
6617 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6618 err, devlist->num_devs);
6622 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6625 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6627 struct nfs4_getdeviceinfo_args args = {
6630 struct nfs4_getdeviceinfo_res res = {
6633 struct rpc_message msg = {
6634 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6640 dprintk("--> %s\n", __func__);
6641 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6642 dprintk("<-- %s status=%d\n", __func__, status);
6647 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6649 struct nfs4_exception exception = { };
6653 err = nfs4_handle_exception(server,
6654 _nfs4_proc_getdeviceinfo(server, pdev),
6656 } while (exception.retry);
6659 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6661 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6663 struct nfs4_layoutcommit_data *data = calldata;
6664 struct nfs_server *server = NFS_SERVER(data->args.inode);
6666 if (nfs4_setup_sequence(server, &data->args.seq_args,
6667 &data->res.seq_res, task))
6669 rpc_call_start(task);
6673 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6675 struct nfs4_layoutcommit_data *data = calldata;
6676 struct nfs_server *server = NFS_SERVER(data->args.inode);
6678 if (!nfs4_sequence_done(task, &data->res.seq_res))
6681 switch (task->tk_status) { /* Just ignore these failures */
6682 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6683 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6684 case -NFS4ERR_BADLAYOUT: /* no layout */
6685 case -NFS4ERR_GRACE: /* loca_recalim always false */
6686 task->tk_status = 0;
6689 nfs_post_op_update_inode_force_wcc(data->args.inode,
6693 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6694 rpc_restart_call_prepare(task);
6700 static void nfs4_layoutcommit_release(void *calldata)
6702 struct nfs4_layoutcommit_data *data = calldata;
6703 struct pnfs_layout_segment *lseg, *tmp;
6704 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
6706 pnfs_cleanup_layoutcommit(data);
6707 /* Matched by references in pnfs_set_layoutcommit */
6708 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6709 list_del_init(&lseg->pls_lc_list);
6710 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6712 pnfs_put_lseg(lseg);
6715 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6716 smp_mb__after_clear_bit();
6717 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6719 put_rpccred(data->cred);
6723 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6724 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6725 .rpc_call_done = nfs4_layoutcommit_done,
6726 .rpc_release = nfs4_layoutcommit_release,
6730 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6732 struct rpc_message msg = {
6733 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6734 .rpc_argp = &data->args,
6735 .rpc_resp = &data->res,
6736 .rpc_cred = data->cred,
6738 struct rpc_task_setup task_setup_data = {
6739 .task = &data->task,
6740 .rpc_client = NFS_CLIENT(data->args.inode),
6741 .rpc_message = &msg,
6742 .callback_ops = &nfs4_layoutcommit_ops,
6743 .callback_data = data,
6744 .flags = RPC_TASK_ASYNC,
6746 struct rpc_task *task;
6749 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6750 "lbw: %llu inode %lu\n",
6751 data->task.tk_pid, sync,
6752 data->args.lastbytewritten,
6753 data->args.inode->i_ino);
6755 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6756 task = rpc_run_task(&task_setup_data);
6758 return PTR_ERR(task);
6761 status = nfs4_wait_for_completion_rpc_task(task);
6764 status = task->tk_status;
6766 dprintk("%s: status %d\n", __func__, status);
6772 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6773 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6775 struct nfs41_secinfo_no_name_args args = {
6776 .style = SECINFO_STYLE_CURRENT_FH,
6778 struct nfs4_secinfo_res res = {
6781 struct rpc_message msg = {
6782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6786 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6790 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6791 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6793 struct nfs4_exception exception = { };
6796 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6799 case -NFS4ERR_WRONGSEC:
6800 case -NFS4ERR_NOTSUPP:
6803 err = nfs4_handle_exception(server, err, &exception);
6805 } while (exception.retry);
6811 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6812 struct nfs_fsinfo *info)
6816 rpc_authflavor_t flavor;
6817 struct nfs4_secinfo_flavors *flavors;
6819 page = alloc_page(GFP_KERNEL);
6825 flavors = page_address(page);
6826 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6829 * Fall back on "guess and check" method if
6830 * the server doesn't support SECINFO_NO_NAME
6832 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6833 err = nfs4_find_root_sec(server, fhandle, info);
6839 flavor = nfs_find_best_sec(flavors);
6841 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6851 static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6854 struct nfs41_test_stateid_args args = {
6857 struct nfs41_test_stateid_res res;
6858 struct rpc_message msg = {
6859 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6864 dprintk("NFS call test_stateid %p\n", stateid);
6865 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6866 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6867 if (status != NFS_OK) {
6868 dprintk("NFS reply test_stateid: failed, %d\n", status);
6871 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
6876 * nfs41_test_stateid - perform a TEST_STATEID operation
6878 * @server: server / transport on which to perform the operation
6879 * @stateid: state ID to test
6881 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6882 * Otherwise a negative NFS4ERR value is returned if the operation
6883 * failed or the state ID is not currently valid.
6885 static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6887 struct nfs4_exception exception = { };
6890 err = _nfs41_test_stateid(server, stateid);
6891 if (err != -NFS4ERR_DELAY)
6893 nfs4_handle_exception(server, err, &exception);
6894 } while (exception.retry);
6898 static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6900 struct nfs41_free_stateid_args args = {
6903 struct nfs41_free_stateid_res res;
6904 struct rpc_message msg = {
6905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6911 dprintk("NFS call free_stateid %p\n", stateid);
6912 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6913 status = nfs4_call_sync_sequence(server->client, server, &msg,
6914 &args.seq_args, &res.seq_res, 1);
6915 dprintk("NFS reply free_stateid: %d\n", status);
6920 * nfs41_free_stateid - perform a FREE_STATEID operation
6922 * @server: server / transport on which to perform the operation
6923 * @stateid: state ID to release
6925 * Returns NFS_OK if the server freed "stateid". Otherwise a
6926 * negative NFS4ERR value is returned.
6928 static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6930 struct nfs4_exception exception = { };
6933 err = _nfs4_free_stateid(server, stateid);
6934 if (err != -NFS4ERR_DELAY)
6936 nfs4_handle_exception(server, err, &exception);
6937 } while (exception.retry);
6941 static bool nfs41_match_stateid(const nfs4_stateid *s1,
6942 const nfs4_stateid *s2)
6944 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
6947 if (s1->seqid == s2->seqid)
6949 if (s1->seqid == 0 || s2->seqid == 0)
6955 #endif /* CONFIG_NFS_V4_1 */
6957 static bool nfs4_match_stateid(const nfs4_stateid *s1,
6958 const nfs4_stateid *s2)
6960 return nfs4_stateid_match(s1, s2);
6964 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6965 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6966 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6967 .recover_open = nfs4_open_reclaim,
6968 .recover_lock = nfs4_lock_reclaim,
6969 .establish_clid = nfs4_init_clientid,
6970 .get_clid_cred = nfs4_get_setclientid_cred,
6971 .detect_trunking = nfs40_discover_server_trunking,
6974 #if defined(CONFIG_NFS_V4_1)
6975 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6976 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6977 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6978 .recover_open = nfs4_open_reclaim,
6979 .recover_lock = nfs4_lock_reclaim,
6980 .establish_clid = nfs41_init_clientid,
6981 .get_clid_cred = nfs4_get_exchange_id_cred,
6982 .reclaim_complete = nfs41_proc_reclaim_complete,
6983 .detect_trunking = nfs41_discover_server_trunking,
6985 #endif /* CONFIG_NFS_V4_1 */
6987 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6988 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6989 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6990 .recover_open = nfs4_open_expired,
6991 .recover_lock = nfs4_lock_expired,
6992 .establish_clid = nfs4_init_clientid,
6993 .get_clid_cred = nfs4_get_setclientid_cred,
6996 #if defined(CONFIG_NFS_V4_1)
6997 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6998 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6999 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
7000 .recover_open = nfs41_open_expired,
7001 .recover_lock = nfs41_lock_expired,
7002 .establish_clid = nfs41_init_clientid,
7003 .get_clid_cred = nfs4_get_exchange_id_cred,
7005 #endif /* CONFIG_NFS_V4_1 */
7007 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
7008 .sched_state_renewal = nfs4_proc_async_renew,
7009 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
7010 .renew_lease = nfs4_proc_renew,
7013 #if defined(CONFIG_NFS_V4_1)
7014 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
7015 .sched_state_renewal = nfs41_proc_async_sequence,
7016 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
7017 .renew_lease = nfs4_proc_sequence,
7021 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7023 .call_sync = _nfs4_call_sync,
7024 .match_stateid = nfs4_match_stateid,
7025 .find_root_sec = nfs4_find_root_sec,
7026 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7027 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7028 .state_renewal_ops = &nfs40_state_renewal_ops,
7031 #if defined(CONFIG_NFS_V4_1)
7032 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7034 .call_sync = _nfs4_call_sync_session,
7035 .match_stateid = nfs41_match_stateid,
7036 .find_root_sec = nfs41_find_root_sec,
7037 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7038 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7039 .state_renewal_ops = &nfs41_state_renewal_ops,
7043 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7044 [0] = &nfs_v4_0_minor_ops,
7045 #if defined(CONFIG_NFS_V4_1)
7046 [1] = &nfs_v4_1_minor_ops,
7050 const struct inode_operations nfs4_dir_inode_operations = {
7051 .create = nfs_create,
7052 .lookup = nfs_lookup,
7053 .atomic_open = nfs_atomic_open,
7055 .unlink = nfs_unlink,
7056 .symlink = nfs_symlink,
7060 .rename = nfs_rename,
7061 .permission = nfs_permission,
7062 .getattr = nfs_getattr,
7063 .setattr = nfs_setattr,
7064 .getxattr = generic_getxattr,
7065 .setxattr = generic_setxattr,
7066 .listxattr = generic_listxattr,
7067 .removexattr = generic_removexattr,
7070 static const struct inode_operations nfs4_file_inode_operations = {
7071 .permission = nfs_permission,
7072 .getattr = nfs_getattr,
7073 .setattr = nfs_setattr,
7074 .getxattr = generic_getxattr,
7075 .setxattr = generic_setxattr,
7076 .listxattr = generic_listxattr,
7077 .removexattr = generic_removexattr,
7080 const struct nfs_rpc_ops nfs_v4_clientops = {
7081 .version = 4, /* protocol version */
7082 .dentry_ops = &nfs4_dentry_operations,
7083 .dir_inode_ops = &nfs4_dir_inode_operations,
7084 .file_inode_ops = &nfs4_file_inode_operations,
7085 .file_ops = &nfs4_file_operations,
7086 .getroot = nfs4_proc_get_root,
7087 .submount = nfs4_submount,
7088 .try_mount = nfs4_try_mount,
7089 .getattr = nfs4_proc_getattr,
7090 .setattr = nfs4_proc_setattr,
7091 .lookup = nfs4_proc_lookup,
7092 .access = nfs4_proc_access,
7093 .readlink = nfs4_proc_readlink,
7094 .create = nfs4_proc_create,
7095 .remove = nfs4_proc_remove,
7096 .unlink_setup = nfs4_proc_unlink_setup,
7097 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
7098 .unlink_done = nfs4_proc_unlink_done,
7099 .rename = nfs4_proc_rename,
7100 .rename_setup = nfs4_proc_rename_setup,
7101 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
7102 .rename_done = nfs4_proc_rename_done,
7103 .link = nfs4_proc_link,
7104 .symlink = nfs4_proc_symlink,
7105 .mkdir = nfs4_proc_mkdir,
7106 .rmdir = nfs4_proc_remove,
7107 .readdir = nfs4_proc_readdir,
7108 .mknod = nfs4_proc_mknod,
7109 .statfs = nfs4_proc_statfs,
7110 .fsinfo = nfs4_proc_fsinfo,
7111 .pathconf = nfs4_proc_pathconf,
7112 .set_capabilities = nfs4_server_capabilities,
7113 .decode_dirent = nfs4_decode_dirent,
7114 .read_setup = nfs4_proc_read_setup,
7115 .read_pageio_init = pnfs_pageio_init_read,
7116 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
7117 .read_done = nfs4_read_done,
7118 .write_setup = nfs4_proc_write_setup,
7119 .write_pageio_init = pnfs_pageio_init_write,
7120 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
7121 .write_done = nfs4_write_done,
7122 .commit_setup = nfs4_proc_commit_setup,
7123 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
7124 .commit_done = nfs4_commit_done,
7125 .lock = nfs4_proc_lock,
7126 .clear_acl_cache = nfs4_zap_acl_attr,
7127 .close_context = nfs4_close_context,
7128 .open_context = nfs4_atomic_open,
7129 .have_delegation = nfs4_have_delegation,
7130 .return_delegation = nfs4_inode_return_delegation,
7131 .alloc_client = nfs4_alloc_client,
7132 .init_client = nfs4_init_client,
7133 .free_client = nfs4_free_client,
7134 .create_server = nfs4_create_server,
7135 .clone_server = nfs_clone_server,
7138 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7139 .prefix = XATTR_NAME_NFSV4_ACL,
7140 .list = nfs4_xattr_list_nfs4_acl,
7141 .get = nfs4_xattr_get_nfs4_acl,
7142 .set = nfs4_xattr_set_nfs4_acl,
7145 const struct xattr_handler *nfs4_xattr_handlers[] = {
7146 &nfs4_xattr_nfs4_acl_handler,