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/sunrpc/gss_api.h>
47 #include <linux/nfs.h>
48 #include <linux/nfs4.h>
49 #include <linux/nfs_fs.h>
50 #include <linux/nfs_page.h>
51 #include <linux/nfs_mount.h>
52 #include <linux/namei.h>
53 #include <linux/mount.h>
54 #include <linux/module.h>
55 #include <linux/nfs_idmap.h>
56 #include <linux/sunrpc/bc_xprt.h>
57 #include <linux/xattr.h>
58 #include <linux/utsname.h>
59 #include <linux/freezer.h>
62 #include "delegation.h"
69 #define NFSDBG_FACILITY NFSDBG_PROC
71 #define NFS4_POLL_RETRY_MIN (HZ/10)
72 #define NFS4_POLL_RETRY_MAX (15*HZ)
74 #define NFS4_MAX_LOOP_ON_RECOVER (10)
76 static unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
79 static int _nfs4_proc_open(struct nfs4_opendata *data);
80 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
81 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
82 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
83 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
84 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
85 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
86 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
87 struct nfs_fattr *fattr, struct iattr *sattr,
88 struct nfs4_state *state);
89 #ifdef CONFIG_NFS_V4_1
90 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
91 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
93 /* Prevent leaks of NFSv4 errors into userland */
94 static int nfs4_map_errors(int err)
99 case -NFS4ERR_RESOURCE:
101 case -NFS4ERR_WRONGSEC:
103 case -NFS4ERR_BADOWNER:
104 case -NFS4ERR_BADNAME:
106 case -NFS4ERR_SHARE_DENIED:
108 case -NFS4ERR_MINOR_VERS_MISMATCH:
109 return -EPROTONOSUPPORT;
111 dprintk("%s could not handle NFSv4 error %d\n",
119 * This is our standard bitmap for GETATTR requests.
121 const u32 nfs4_fattr_bitmap[3] = {
123 | FATTR4_WORD0_CHANGE
126 | FATTR4_WORD0_FILEID,
128 | FATTR4_WORD1_NUMLINKS
130 | FATTR4_WORD1_OWNER_GROUP
131 | FATTR4_WORD1_RAWDEV
132 | FATTR4_WORD1_SPACE_USED
133 | FATTR4_WORD1_TIME_ACCESS
134 | FATTR4_WORD1_TIME_METADATA
135 | FATTR4_WORD1_TIME_MODIFY
138 static const u32 nfs4_pnfs_open_bitmap[3] = {
140 | FATTR4_WORD0_CHANGE
143 | FATTR4_WORD0_FILEID,
145 | FATTR4_WORD1_NUMLINKS
147 | FATTR4_WORD1_OWNER_GROUP
148 | FATTR4_WORD1_RAWDEV
149 | FATTR4_WORD1_SPACE_USED
150 | FATTR4_WORD1_TIME_ACCESS
151 | FATTR4_WORD1_TIME_METADATA
152 | FATTR4_WORD1_TIME_MODIFY,
153 FATTR4_WORD2_MDSTHRESHOLD
156 const u32 nfs4_statfs_bitmap[2] = {
157 FATTR4_WORD0_FILES_AVAIL
158 | FATTR4_WORD0_FILES_FREE
159 | FATTR4_WORD0_FILES_TOTAL,
160 FATTR4_WORD1_SPACE_AVAIL
161 | FATTR4_WORD1_SPACE_FREE
162 | FATTR4_WORD1_SPACE_TOTAL
165 const u32 nfs4_pathconf_bitmap[2] = {
167 | FATTR4_WORD0_MAXNAME,
171 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
172 | FATTR4_WORD0_MAXREAD
173 | FATTR4_WORD0_MAXWRITE
174 | FATTR4_WORD0_LEASE_TIME,
175 FATTR4_WORD1_TIME_DELTA
176 | FATTR4_WORD1_FS_LAYOUT_TYPES,
177 FATTR4_WORD2_LAYOUT_BLKSIZE
180 const u32 nfs4_fs_locations_bitmap[2] = {
182 | FATTR4_WORD0_CHANGE
185 | FATTR4_WORD0_FILEID
186 | FATTR4_WORD0_FS_LOCATIONS,
188 | FATTR4_WORD1_NUMLINKS
190 | FATTR4_WORD1_OWNER_GROUP
191 | FATTR4_WORD1_RAWDEV
192 | FATTR4_WORD1_SPACE_USED
193 | FATTR4_WORD1_TIME_ACCESS
194 | FATTR4_WORD1_TIME_METADATA
195 | FATTR4_WORD1_TIME_MODIFY
196 | FATTR4_WORD1_MOUNTED_ON_FILEID
199 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
200 struct nfs4_readdir_arg *readdir)
204 BUG_ON(readdir->count < 80);
206 readdir->cookie = cookie;
207 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
212 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
217 * NFSv4 servers do not return entries for '.' and '..'
218 * Therefore, we fake these entries here. We let '.'
219 * have cookie 0 and '..' have cookie 1. Note that
220 * when talking to the server, we always send cookie 0
223 start = p = kmap_atomic(*readdir->pages);
226 *p++ = xdr_one; /* next */
227 *p++ = xdr_zero; /* cookie, first word */
228 *p++ = xdr_one; /* cookie, second word */
229 *p++ = xdr_one; /* entry len */
230 memcpy(p, ".\0\0\0", 4); /* entry */
232 *p++ = xdr_one; /* bitmap length */
233 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
234 *p++ = htonl(8); /* attribute buffer length */
235 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
238 *p++ = xdr_one; /* next */
239 *p++ = xdr_zero; /* cookie, first word */
240 *p++ = xdr_two; /* cookie, second word */
241 *p++ = xdr_two; /* entry len */
242 memcpy(p, "..\0\0", 4); /* entry */
244 *p++ = xdr_one; /* bitmap length */
245 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
246 *p++ = htonl(8); /* attribute buffer length */
247 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
249 readdir->pgbase = (char *)p - (char *)start;
250 readdir->count -= readdir->pgbase;
251 kunmap_atomic(start);
254 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
260 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
261 nfs_wait_bit_killable, TASK_KILLABLE);
265 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
272 *timeout = NFS4_POLL_RETRY_MIN;
273 if (*timeout > NFS4_POLL_RETRY_MAX)
274 *timeout = NFS4_POLL_RETRY_MAX;
275 freezable_schedule_timeout_killable(*timeout);
276 if (fatal_signal_pending(current))
282 /* This is the error handling routine for processes that are allowed
285 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
287 struct nfs_client *clp = server->nfs_client;
288 struct nfs4_state *state = exception->state;
289 struct inode *inode = exception->inode;
292 exception->retry = 0;
296 case -NFS4ERR_OPENMODE:
297 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
298 nfs4_inode_return_delegation(inode);
299 exception->retry = 1;
304 nfs4_schedule_stateid_recovery(server, state);
305 goto wait_on_recovery;
306 case -NFS4ERR_DELEG_REVOKED:
307 case -NFS4ERR_ADMIN_REVOKED:
308 case -NFS4ERR_BAD_STATEID:
311 nfs_remove_bad_delegation(state->inode);
312 nfs4_schedule_stateid_recovery(server, state);
313 goto wait_on_recovery;
314 case -NFS4ERR_EXPIRED:
316 nfs4_schedule_stateid_recovery(server, state);
317 case -NFS4ERR_STALE_STATEID:
318 case -NFS4ERR_STALE_CLIENTID:
319 nfs4_schedule_lease_recovery(clp);
320 goto wait_on_recovery;
321 #if defined(CONFIG_NFS_V4_1)
322 case -NFS4ERR_BADSESSION:
323 case -NFS4ERR_BADSLOT:
324 case -NFS4ERR_BAD_HIGH_SLOT:
325 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
326 case -NFS4ERR_DEADSESSION:
327 case -NFS4ERR_SEQ_FALSE_RETRY:
328 case -NFS4ERR_SEQ_MISORDERED:
329 dprintk("%s ERROR: %d Reset session\n", __func__,
331 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
332 exception->retry = 1;
334 #endif /* defined(CONFIG_NFS_V4_1) */
335 case -NFS4ERR_FILE_OPEN:
336 if (exception->timeout > HZ) {
337 /* We have retried a decent amount, time to
346 ret = nfs4_delay(server->client, &exception->timeout);
349 case -NFS4ERR_RETRY_UNCACHED_REP:
350 case -NFS4ERR_OLD_STATEID:
351 exception->retry = 1;
353 case -NFS4ERR_BADOWNER:
354 /* The following works around a Linux server bug! */
355 case -NFS4ERR_BADNAME:
356 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
357 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
358 exception->retry = 1;
359 printk(KERN_WARNING "NFS: v4 server %s "
360 "does not accept raw "
362 "Reenabling the idmapper.\n",
363 server->nfs_client->cl_hostname);
366 /* We failed to handle the error */
367 return nfs4_map_errors(ret);
369 ret = nfs4_wait_clnt_recover(clp);
371 exception->retry = 1;
376 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
378 spin_lock(&clp->cl_lock);
379 if (time_before(clp->cl_last_renewal,timestamp))
380 clp->cl_last_renewal = timestamp;
381 spin_unlock(&clp->cl_lock);
384 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
386 do_renew_lease(server->nfs_client, timestamp);
389 #if defined(CONFIG_NFS_V4_1)
392 * nfs4_free_slot - free a slot and efficiently update slot table.
394 * freeing a slot is trivially done by clearing its respective bit
396 * If the freed slotid equals highest_used_slotid we want to update it
397 * so that the server would be able to size down the slot table if needed,
398 * otherwise we know that the highest_used_slotid is still in use.
399 * When updating highest_used_slotid there may be "holes" in the bitmap
400 * so we need to scan down from highest_used_slotid to 0 looking for the now
401 * highest slotid in use.
402 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
404 * Must be called while holding tbl->slot_tbl_lock
407 nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
409 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
410 /* clear used bit in bitmap */
411 __clear_bit(slotid, tbl->used_slots);
413 /* update highest_used_slotid when it is freed */
414 if (slotid == tbl->highest_used_slotid) {
415 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
416 if (slotid < tbl->max_slots)
417 tbl->highest_used_slotid = slotid;
419 tbl->highest_used_slotid = NFS4_NO_SLOT;
421 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
422 slotid, tbl->highest_used_slotid);
425 bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
427 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
432 * Signal state manager thread if session fore channel is drained
434 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
436 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
437 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
438 nfs4_set_task_privileged, NULL);
442 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
445 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
446 complete(&ses->fc_slot_table.complete);
450 * Signal state manager thread if session back channel is drained
452 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
454 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
455 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
457 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
458 complete(&ses->bc_slot_table.complete);
461 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
463 struct nfs4_slot_table *tbl;
465 tbl = &res->sr_session->fc_slot_table;
467 /* just wake up the next guy waiting since
468 * we may have not consumed a slot after all */
469 dprintk("%s: No slot\n", __func__);
473 spin_lock(&tbl->slot_tbl_lock);
474 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
475 nfs4_check_drain_fc_complete(res->sr_session);
476 spin_unlock(&tbl->slot_tbl_lock);
480 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
482 unsigned long timestamp;
483 struct nfs_client *clp;
486 * sr_status remains 1 if an RPC level error occurred. The server
487 * may or may not have processed the sequence operation..
488 * Proceed as if the server received and processed the sequence
491 if (res->sr_status == 1)
492 res->sr_status = NFS_OK;
494 /* don't increment the sequence number if the task wasn't sent */
495 if (!RPC_WAS_SENT(task))
498 /* Check the SEQUENCE operation status */
499 switch (res->sr_status) {
501 /* Update the slot's sequence and clientid lease timer */
502 ++res->sr_slot->seq_nr;
503 timestamp = res->sr_renewal_time;
504 clp = res->sr_session->clp;
505 do_renew_lease(clp, timestamp);
506 /* Check sequence flags */
507 if (res->sr_status_flags != 0)
508 nfs4_schedule_lease_recovery(clp);
511 /* The server detected a resend of the RPC call and
512 * returned NFS4ERR_DELAY as per Section 2.10.6.2
515 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
517 res->sr_slot - res->sr_session->fc_slot_table.slots,
518 res->sr_slot->seq_nr);
521 /* Just update the slot sequence no. */
522 ++res->sr_slot->seq_nr;
525 /* The session may be reset by one of the error handlers. */
526 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
527 nfs41_sequence_free_slot(res);
530 if (!rpc_restart_call(task))
532 rpc_delay(task, NFS4_POLL_RETRY_MAX);
536 static int nfs4_sequence_done(struct rpc_task *task,
537 struct nfs4_sequence_res *res)
539 if (res->sr_session == NULL)
541 return nfs41_sequence_done(task, res);
545 * nfs4_find_slot - efficiently look for a free slot
547 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
548 * If found, we mark the slot as used, update the highest_used_slotid,
549 * and respectively set up the sequence operation args.
550 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
552 * Note: must be called with under the slot_tbl_lock.
555 nfs4_find_slot(struct nfs4_slot_table *tbl)
558 u32 ret_id = NFS4_NO_SLOT;
560 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
561 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
563 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
564 if (slotid >= tbl->max_slots)
566 __set_bit(slotid, tbl->used_slots);
567 if (slotid > tbl->highest_used_slotid ||
568 tbl->highest_used_slotid == NFS4_NO_SLOT)
569 tbl->highest_used_slotid = slotid;
572 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
573 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
577 static void nfs41_init_sequence(struct nfs4_sequence_args *args,
578 struct nfs4_sequence_res *res, int cache_reply)
580 args->sa_session = NULL;
581 args->sa_cache_this = 0;
583 args->sa_cache_this = 1;
584 res->sr_session = NULL;
588 int nfs41_setup_sequence(struct nfs4_session *session,
589 struct nfs4_sequence_args *args,
590 struct nfs4_sequence_res *res,
591 struct rpc_task *task)
593 struct nfs4_slot *slot;
594 struct nfs4_slot_table *tbl;
597 dprintk("--> %s\n", __func__);
598 /* slot already allocated? */
599 if (res->sr_slot != NULL)
602 tbl = &session->fc_slot_table;
604 spin_lock(&tbl->slot_tbl_lock);
605 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
606 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
607 /* The state manager will wait until the slot table is empty */
608 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
609 spin_unlock(&tbl->slot_tbl_lock);
610 dprintk("%s session is draining\n", __func__);
614 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
615 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
616 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
617 spin_unlock(&tbl->slot_tbl_lock);
618 dprintk("%s enforce FIFO order\n", __func__);
622 slotid = nfs4_find_slot(tbl);
623 if (slotid == NFS4_NO_SLOT) {
624 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
625 spin_unlock(&tbl->slot_tbl_lock);
626 dprintk("<-- %s: no free slots\n", __func__);
629 spin_unlock(&tbl->slot_tbl_lock);
631 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
632 slot = tbl->slots + slotid;
633 args->sa_session = session;
634 args->sa_slotid = slotid;
636 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
638 res->sr_session = session;
640 res->sr_renewal_time = jiffies;
641 res->sr_status_flags = 0;
643 * sr_status is only set in decode_sequence, and so will remain
644 * set to 1 if an rpc level failure occurs.
649 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
651 int nfs4_setup_sequence(const struct nfs_server *server,
652 struct nfs4_sequence_args *args,
653 struct nfs4_sequence_res *res,
654 struct rpc_task *task)
656 struct nfs4_session *session = nfs4_get_session(server);
662 dprintk("--> %s clp %p session %p sr_slot %td\n",
663 __func__, session->clp, session, res->sr_slot ?
664 res->sr_slot - session->fc_slot_table.slots : -1);
666 ret = nfs41_setup_sequence(session, args, res, task);
668 dprintk("<-- %s status=%d\n", __func__, ret);
672 struct nfs41_call_sync_data {
673 const struct nfs_server *seq_server;
674 struct nfs4_sequence_args *seq_args;
675 struct nfs4_sequence_res *seq_res;
678 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
680 struct nfs41_call_sync_data *data = calldata;
682 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
684 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
685 data->seq_res, task))
687 rpc_call_start(task);
690 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
692 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
693 nfs41_call_sync_prepare(task, calldata);
696 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
698 struct nfs41_call_sync_data *data = calldata;
700 nfs41_sequence_done(task, data->seq_res);
703 static const struct rpc_call_ops nfs41_call_sync_ops = {
704 .rpc_call_prepare = nfs41_call_sync_prepare,
705 .rpc_call_done = nfs41_call_sync_done,
708 static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
709 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
710 .rpc_call_done = nfs41_call_sync_done,
713 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
714 struct nfs_server *server,
715 struct rpc_message *msg,
716 struct nfs4_sequence_args *args,
717 struct nfs4_sequence_res *res,
721 struct rpc_task *task;
722 struct nfs41_call_sync_data data = {
723 .seq_server = server,
727 struct rpc_task_setup task_setup = {
730 .callback_ops = &nfs41_call_sync_ops,
731 .callback_data = &data
735 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
736 task = rpc_run_task(&task_setup);
740 ret = task->tk_status;
746 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
747 struct nfs_server *server,
748 struct rpc_message *msg,
749 struct nfs4_sequence_args *args,
750 struct nfs4_sequence_res *res,
753 nfs41_init_sequence(args, res, cache_reply);
754 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
759 void nfs41_init_sequence(struct nfs4_sequence_args *args,
760 struct nfs4_sequence_res *res, int cache_reply)
764 static int nfs4_sequence_done(struct rpc_task *task,
765 struct nfs4_sequence_res *res)
769 #endif /* CONFIG_NFS_V4_1 */
771 int _nfs4_call_sync(struct rpc_clnt *clnt,
772 struct nfs_server *server,
773 struct rpc_message *msg,
774 struct nfs4_sequence_args *args,
775 struct nfs4_sequence_res *res,
778 nfs41_init_sequence(args, res, cache_reply);
779 return rpc_call_sync(clnt, msg, 0);
783 int nfs4_call_sync(struct rpc_clnt *clnt,
784 struct nfs_server *server,
785 struct rpc_message *msg,
786 struct nfs4_sequence_args *args,
787 struct nfs4_sequence_res *res,
790 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
791 args, res, cache_reply);
794 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
796 struct nfs_inode *nfsi = NFS_I(dir);
798 spin_lock(&dir->i_lock);
799 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
800 if (!cinfo->atomic || cinfo->before != dir->i_version)
801 nfs_force_lookup_revalidate(dir);
802 dir->i_version = cinfo->after;
803 spin_unlock(&dir->i_lock);
806 struct nfs4_opendata {
808 struct nfs_openargs o_arg;
809 struct nfs_openres o_res;
810 struct nfs_open_confirmargs c_arg;
811 struct nfs_open_confirmres c_res;
812 struct nfs4_string owner_name;
813 struct nfs4_string group_name;
814 struct nfs_fattr f_attr;
816 struct dentry *dentry;
817 struct nfs4_state_owner *owner;
818 struct nfs4_state *state;
820 unsigned long timestamp;
821 unsigned int rpc_done : 1;
827 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
829 p->o_res.f_attr = &p->f_attr;
830 p->o_res.seqid = p->o_arg.seqid;
831 p->c_res.seqid = p->c_arg.seqid;
832 p->o_res.server = p->o_arg.server;
833 nfs_fattr_init(&p->f_attr);
834 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
837 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
838 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
839 const struct iattr *attrs,
842 struct dentry *parent = dget_parent(dentry);
843 struct inode *dir = parent->d_inode;
844 struct nfs_server *server = NFS_SERVER(dir);
845 struct nfs4_opendata *p;
847 p = kzalloc(sizeof(*p), gfp_mask);
850 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
851 if (p->o_arg.seqid == NULL)
853 nfs_sb_active(dentry->d_sb);
854 p->dentry = dget(dentry);
857 atomic_inc(&sp->so_count);
858 p->o_arg.fh = NFS_FH(dir);
859 p->o_arg.open_flags = flags;
860 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
861 p->o_arg.clientid = server->nfs_client->cl_clientid;
862 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
863 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
864 p->o_arg.name = &dentry->d_name;
865 p->o_arg.server = server;
866 p->o_arg.bitmask = server->attr_bitmask;
867 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
868 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
869 if (attrs != NULL && attrs->ia_valid != 0) {
872 p->o_arg.u.attrs = &p->attrs;
873 memcpy(&p->attrs, attrs, sizeof(p->attrs));
876 verf[1] = current->pid;
877 memcpy(p->o_arg.u.verifier.data, verf,
878 sizeof(p->o_arg.u.verifier.data));
880 p->c_arg.fh = &p->o_res.fh;
881 p->c_arg.stateid = &p->o_res.stateid;
882 p->c_arg.seqid = p->o_arg.seqid;
883 nfs4_init_opendata_res(p);
893 static void nfs4_opendata_free(struct kref *kref)
895 struct nfs4_opendata *p = container_of(kref,
896 struct nfs4_opendata, kref);
897 struct super_block *sb = p->dentry->d_sb;
899 nfs_free_seqid(p->o_arg.seqid);
900 if (p->state != NULL)
901 nfs4_put_open_state(p->state);
902 nfs4_put_state_owner(p->owner);
906 nfs_fattr_free_names(&p->f_attr);
910 static void nfs4_opendata_put(struct nfs4_opendata *p)
913 kref_put(&p->kref, nfs4_opendata_free);
916 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
920 ret = rpc_wait_for_completion_task(task);
924 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
928 if (open_mode & (O_EXCL|O_TRUNC))
930 switch (mode & (FMODE_READ|FMODE_WRITE)) {
932 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
933 && state->n_rdonly != 0;
936 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
937 && state->n_wronly != 0;
939 case FMODE_READ|FMODE_WRITE:
940 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
941 && state->n_rdwr != 0;
947 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
949 if (delegation == NULL)
951 if ((delegation->type & fmode) != fmode)
953 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
955 nfs_mark_delegation_referenced(delegation);
959 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
968 case FMODE_READ|FMODE_WRITE:
971 nfs4_state_set_mode_locked(state, state->state | fmode);
974 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
976 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
977 nfs4_stateid_copy(&state->stateid, stateid);
978 nfs4_stateid_copy(&state->open_stateid, stateid);
981 set_bit(NFS_O_RDONLY_STATE, &state->flags);
984 set_bit(NFS_O_WRONLY_STATE, &state->flags);
986 case FMODE_READ|FMODE_WRITE:
987 set_bit(NFS_O_RDWR_STATE, &state->flags);
991 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
993 write_seqlock(&state->seqlock);
994 nfs_set_open_stateid_locked(state, stateid, fmode);
995 write_sequnlock(&state->seqlock);
998 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1001 * Protect the call to nfs4_state_set_mode_locked and
1002 * serialise the stateid update
1004 write_seqlock(&state->seqlock);
1005 if (deleg_stateid != NULL) {
1006 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1007 set_bit(NFS_DELEGATED_STATE, &state->flags);
1009 if (open_stateid != NULL)
1010 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1011 write_sequnlock(&state->seqlock);
1012 spin_lock(&state->owner->so_lock);
1013 update_open_stateflags(state, fmode);
1014 spin_unlock(&state->owner->so_lock);
1017 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1019 struct nfs_inode *nfsi = NFS_I(state->inode);
1020 struct nfs_delegation *deleg_cur;
1023 fmode &= (FMODE_READ|FMODE_WRITE);
1026 deleg_cur = rcu_dereference(nfsi->delegation);
1027 if (deleg_cur == NULL)
1030 spin_lock(&deleg_cur->lock);
1031 if (nfsi->delegation != deleg_cur ||
1032 (deleg_cur->type & fmode) != fmode)
1033 goto no_delegation_unlock;
1035 if (delegation == NULL)
1036 delegation = &deleg_cur->stateid;
1037 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1038 goto no_delegation_unlock;
1040 nfs_mark_delegation_referenced(deleg_cur);
1041 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1043 no_delegation_unlock:
1044 spin_unlock(&deleg_cur->lock);
1048 if (!ret && open_stateid != NULL) {
1049 __update_open_stateid(state, open_stateid, NULL, fmode);
1057 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1059 struct nfs_delegation *delegation;
1062 delegation = rcu_dereference(NFS_I(inode)->delegation);
1063 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1068 nfs4_inode_return_delegation(inode);
1071 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1073 struct nfs4_state *state = opendata->state;
1074 struct nfs_inode *nfsi = NFS_I(state->inode);
1075 struct nfs_delegation *delegation;
1076 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
1077 fmode_t fmode = opendata->o_arg.fmode;
1078 nfs4_stateid stateid;
1082 if (can_open_cached(state, fmode, open_mode)) {
1083 spin_lock(&state->owner->so_lock);
1084 if (can_open_cached(state, fmode, open_mode)) {
1085 update_open_stateflags(state, fmode);
1086 spin_unlock(&state->owner->so_lock);
1087 goto out_return_state;
1089 spin_unlock(&state->owner->so_lock);
1092 delegation = rcu_dereference(nfsi->delegation);
1093 if (!can_open_delegated(delegation, fmode)) {
1097 /* Save the delegation */
1098 nfs4_stateid_copy(&stateid, &delegation->stateid);
1100 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1105 /* Try to update the stateid using the delegation */
1106 if (update_open_stateid(state, NULL, &stateid, fmode))
1107 goto out_return_state;
1110 return ERR_PTR(ret);
1112 atomic_inc(&state->count);
1116 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1118 struct inode *inode;
1119 struct nfs4_state *state = NULL;
1120 struct nfs_delegation *delegation;
1123 if (!data->rpc_done) {
1124 state = nfs4_try_open_cached(data);
1129 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1131 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1132 ret = PTR_ERR(inode);
1136 state = nfs4_get_open_state(inode, data->owner);
1139 if (data->o_res.delegation_type != 0) {
1140 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1141 int delegation_flags = 0;
1144 delegation = rcu_dereference(NFS_I(inode)->delegation);
1146 delegation_flags = delegation->flags;
1148 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1149 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1150 "returning a delegation for "
1151 "OPEN(CLAIM_DELEGATE_CUR)\n",
1153 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1154 nfs_inode_set_delegation(state->inode,
1155 data->owner->so_cred,
1158 nfs_inode_reclaim_delegation(state->inode,
1159 data->owner->so_cred,
1163 update_open_stateid(state, &data->o_res.stateid, NULL,
1171 return ERR_PTR(ret);
1174 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1176 struct nfs_inode *nfsi = NFS_I(state->inode);
1177 struct nfs_open_context *ctx;
1179 spin_lock(&state->inode->i_lock);
1180 list_for_each_entry(ctx, &nfsi->open_files, list) {
1181 if (ctx->state != state)
1183 get_nfs_open_context(ctx);
1184 spin_unlock(&state->inode->i_lock);
1187 spin_unlock(&state->inode->i_lock);
1188 return ERR_PTR(-ENOENT);
1191 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1193 struct nfs4_opendata *opendata;
1195 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1196 if (opendata == NULL)
1197 return ERR_PTR(-ENOMEM);
1198 opendata->state = state;
1199 atomic_inc(&state->count);
1203 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1205 struct nfs4_state *newstate;
1208 opendata->o_arg.open_flags = 0;
1209 opendata->o_arg.fmode = fmode;
1210 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1211 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1212 nfs4_init_opendata_res(opendata);
1213 ret = _nfs4_recover_proc_open(opendata);
1216 newstate = nfs4_opendata_to_nfs4_state(opendata);
1217 if (IS_ERR(newstate))
1218 return PTR_ERR(newstate);
1219 nfs4_close_state(newstate, fmode);
1224 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1226 struct nfs4_state *newstate;
1229 /* memory barrier prior to reading state->n_* */
1230 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1232 if (state->n_rdwr != 0) {
1233 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1234 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1237 if (newstate != state)
1240 if (state->n_wronly != 0) {
1241 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1242 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1245 if (newstate != state)
1248 if (state->n_rdonly != 0) {
1249 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1250 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1253 if (newstate != state)
1257 * We may have performed cached opens for all three recoveries.
1258 * Check if we need to update the current stateid.
1260 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1261 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1262 write_seqlock(&state->seqlock);
1263 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1264 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1265 write_sequnlock(&state->seqlock);
1272 * reclaim state on the server after a reboot.
1274 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1276 struct nfs_delegation *delegation;
1277 struct nfs4_opendata *opendata;
1278 fmode_t delegation_type = 0;
1281 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1282 if (IS_ERR(opendata))
1283 return PTR_ERR(opendata);
1284 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1285 opendata->o_arg.fh = NFS_FH(state->inode);
1287 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1288 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1289 delegation_type = delegation->type;
1291 opendata->o_arg.u.delegation_type = delegation_type;
1292 status = nfs4_open_recover(opendata, state);
1293 nfs4_opendata_put(opendata);
1297 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1299 struct nfs_server *server = NFS_SERVER(state->inode);
1300 struct nfs4_exception exception = { };
1303 err = _nfs4_do_open_reclaim(ctx, state);
1304 if (err != -NFS4ERR_DELAY)
1306 nfs4_handle_exception(server, err, &exception);
1307 } while (exception.retry);
1311 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1313 struct nfs_open_context *ctx;
1316 ctx = nfs4_state_find_open_context(state);
1318 return PTR_ERR(ctx);
1319 ret = nfs4_do_open_reclaim(ctx, state);
1320 put_nfs_open_context(ctx);
1324 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1326 struct nfs4_opendata *opendata;
1329 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1330 if (IS_ERR(opendata))
1331 return PTR_ERR(opendata);
1332 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1333 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1334 ret = nfs4_open_recover(opendata, state);
1335 nfs4_opendata_put(opendata);
1339 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1341 struct nfs4_exception exception = { };
1342 struct nfs_server *server = NFS_SERVER(state->inode);
1345 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1351 case -NFS4ERR_BADSESSION:
1352 case -NFS4ERR_BADSLOT:
1353 case -NFS4ERR_BAD_HIGH_SLOT:
1354 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1355 case -NFS4ERR_DEADSESSION:
1356 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1358 case -NFS4ERR_STALE_CLIENTID:
1359 case -NFS4ERR_STALE_STATEID:
1360 case -NFS4ERR_EXPIRED:
1361 /* Don't recall a delegation if it was lost */
1362 nfs4_schedule_lease_recovery(server->nfs_client);
1366 * The show must go on: exit, but mark the
1367 * stateid as needing recovery.
1369 case -NFS4ERR_DELEG_REVOKED:
1370 case -NFS4ERR_ADMIN_REVOKED:
1371 case -NFS4ERR_BAD_STATEID:
1372 nfs_inode_find_state_and_recover(state->inode,
1374 nfs4_schedule_stateid_recovery(server, state);
1377 * User RPCSEC_GSS context has expired.
1378 * We cannot recover this stateid now, so
1379 * skip it and allow recovery thread to
1386 err = nfs4_handle_exception(server, err, &exception);
1387 } while (exception.retry);
1392 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1394 struct nfs4_opendata *data = calldata;
1396 data->rpc_status = task->tk_status;
1397 if (data->rpc_status == 0) {
1398 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1399 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1400 renew_lease(data->o_res.server, data->timestamp);
1405 static void nfs4_open_confirm_release(void *calldata)
1407 struct nfs4_opendata *data = calldata;
1408 struct nfs4_state *state = NULL;
1410 /* If this request hasn't been cancelled, do nothing */
1411 if (data->cancelled == 0)
1413 /* In case of error, no cleanup! */
1414 if (!data->rpc_done)
1416 state = nfs4_opendata_to_nfs4_state(data);
1418 nfs4_close_state(state, data->o_arg.fmode);
1420 nfs4_opendata_put(data);
1423 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1424 .rpc_call_done = nfs4_open_confirm_done,
1425 .rpc_release = nfs4_open_confirm_release,
1429 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1431 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1433 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1434 struct rpc_task *task;
1435 struct rpc_message msg = {
1436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1437 .rpc_argp = &data->c_arg,
1438 .rpc_resp = &data->c_res,
1439 .rpc_cred = data->owner->so_cred,
1441 struct rpc_task_setup task_setup_data = {
1442 .rpc_client = server->client,
1443 .rpc_message = &msg,
1444 .callback_ops = &nfs4_open_confirm_ops,
1445 .callback_data = data,
1446 .workqueue = nfsiod_workqueue,
1447 .flags = RPC_TASK_ASYNC,
1451 kref_get(&data->kref);
1453 data->rpc_status = 0;
1454 data->timestamp = jiffies;
1455 task = rpc_run_task(&task_setup_data);
1457 return PTR_ERR(task);
1458 status = nfs4_wait_for_completion_rpc_task(task);
1460 data->cancelled = 1;
1463 status = data->rpc_status;
1468 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1470 struct nfs4_opendata *data = calldata;
1471 struct nfs4_state_owner *sp = data->owner;
1473 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1476 * Check if we still need to send an OPEN call, or if we can use
1477 * a delegation instead.
1479 if (data->state != NULL) {
1480 struct nfs_delegation *delegation;
1482 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1485 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1486 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1487 can_open_delegated(delegation, data->o_arg.fmode))
1488 goto unlock_no_action;
1491 /* Update client id. */
1492 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1493 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1494 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1495 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1497 data->timestamp = jiffies;
1498 if (nfs4_setup_sequence(data->o_arg.server,
1499 &data->o_arg.seq_args,
1500 &data->o_res.seq_res, task))
1502 rpc_call_start(task);
1507 task->tk_action = NULL;
1511 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1513 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1514 nfs4_open_prepare(task, calldata);
1517 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1519 struct nfs4_opendata *data = calldata;
1521 data->rpc_status = task->tk_status;
1523 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1526 if (task->tk_status == 0) {
1527 switch (data->o_res.f_attr->mode & S_IFMT) {
1531 data->rpc_status = -ELOOP;
1534 data->rpc_status = -EISDIR;
1537 data->rpc_status = -ENOTDIR;
1539 renew_lease(data->o_res.server, data->timestamp);
1540 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1541 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1546 static void nfs4_open_release(void *calldata)
1548 struct nfs4_opendata *data = calldata;
1549 struct nfs4_state *state = NULL;
1551 /* If this request hasn't been cancelled, do nothing */
1552 if (data->cancelled == 0)
1554 /* In case of error, no cleanup! */
1555 if (data->rpc_status != 0 || !data->rpc_done)
1557 /* In case we need an open_confirm, no cleanup! */
1558 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1560 state = nfs4_opendata_to_nfs4_state(data);
1562 nfs4_close_state(state, data->o_arg.fmode);
1564 nfs4_opendata_put(data);
1567 static const struct rpc_call_ops nfs4_open_ops = {
1568 .rpc_call_prepare = nfs4_open_prepare,
1569 .rpc_call_done = nfs4_open_done,
1570 .rpc_release = nfs4_open_release,
1573 static const struct rpc_call_ops nfs4_recover_open_ops = {
1574 .rpc_call_prepare = nfs4_recover_open_prepare,
1575 .rpc_call_done = nfs4_open_done,
1576 .rpc_release = nfs4_open_release,
1579 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1581 struct inode *dir = data->dir->d_inode;
1582 struct nfs_server *server = NFS_SERVER(dir);
1583 struct nfs_openargs *o_arg = &data->o_arg;
1584 struct nfs_openres *o_res = &data->o_res;
1585 struct rpc_task *task;
1586 struct rpc_message msg = {
1587 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1590 .rpc_cred = data->owner->so_cred,
1592 struct rpc_task_setup task_setup_data = {
1593 .rpc_client = server->client,
1594 .rpc_message = &msg,
1595 .callback_ops = &nfs4_open_ops,
1596 .callback_data = data,
1597 .workqueue = nfsiod_workqueue,
1598 .flags = RPC_TASK_ASYNC,
1602 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1603 kref_get(&data->kref);
1605 data->rpc_status = 0;
1606 data->cancelled = 0;
1608 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1609 task = rpc_run_task(&task_setup_data);
1611 return PTR_ERR(task);
1612 status = nfs4_wait_for_completion_rpc_task(task);
1614 data->cancelled = 1;
1617 status = data->rpc_status;
1623 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1625 struct inode *dir = data->dir->d_inode;
1626 struct nfs_openres *o_res = &data->o_res;
1629 status = nfs4_run_open_task(data, 1);
1630 if (status != 0 || !data->rpc_done)
1633 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1635 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1636 status = _nfs4_proc_open_confirm(data);
1645 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1647 static int _nfs4_proc_open(struct nfs4_opendata *data)
1649 struct inode *dir = data->dir->d_inode;
1650 struct nfs_server *server = NFS_SERVER(dir);
1651 struct nfs_openargs *o_arg = &data->o_arg;
1652 struct nfs_openres *o_res = &data->o_res;
1655 status = nfs4_run_open_task(data, 0);
1656 if (!data->rpc_done)
1659 if (status == -NFS4ERR_BADNAME &&
1660 !(o_arg->open_flags & O_CREAT))
1665 nfs_fattr_map_and_free_names(server, &data->f_attr);
1667 if (o_arg->open_flags & O_CREAT)
1668 update_changeattr(dir, &o_res->cinfo);
1669 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1670 server->caps &= ~NFS_CAP_POSIX_LOCK;
1671 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1672 status = _nfs4_proc_open_confirm(data);
1676 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1677 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1681 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1686 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1687 ret = nfs4_wait_clnt_recover(clp);
1690 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1691 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1693 nfs4_schedule_state_manager(clp);
1699 static int nfs4_recover_expired_lease(struct nfs_server *server)
1701 return nfs4_client_recover_expired_lease(server->nfs_client);
1706 * reclaim state on the server after a network partition.
1707 * Assumes caller holds the appropriate lock
1709 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1711 struct nfs4_opendata *opendata;
1714 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1715 if (IS_ERR(opendata))
1716 return PTR_ERR(opendata);
1717 ret = nfs4_open_recover(opendata, state);
1719 d_drop(ctx->dentry);
1720 nfs4_opendata_put(opendata);
1724 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1726 struct nfs_server *server = NFS_SERVER(state->inode);
1727 struct nfs4_exception exception = { };
1731 err = _nfs4_open_expired(ctx, state);
1735 case -NFS4ERR_GRACE:
1736 case -NFS4ERR_DELAY:
1737 nfs4_handle_exception(server, err, &exception);
1740 } while (exception.retry);
1745 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1747 struct nfs_open_context *ctx;
1750 ctx = nfs4_state_find_open_context(state);
1752 return PTR_ERR(ctx);
1753 ret = nfs4_do_open_expired(ctx, state);
1754 put_nfs_open_context(ctx);
1758 #if defined(CONFIG_NFS_V4_1)
1759 static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
1761 int status = NFS_OK;
1762 struct nfs_server *server = NFS_SERVER(state->inode);
1764 if (state->flags & flags) {
1765 status = nfs41_test_stateid(server, stateid);
1766 if (status != NFS_OK) {
1767 if (status != -NFS4ERR_BAD_STATEID)
1768 nfs41_free_stateid(server, stateid);
1769 state->flags &= ~flags;
1775 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1777 int deleg_status, open_status;
1778 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1779 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1781 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1782 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1784 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1786 return nfs4_open_expired(sp, state);
1791 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1792 * fields corresponding to attributes that were used to store the verifier.
1793 * Make sure we clobber those fields in the later setattr call
1795 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1797 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1798 !(sattr->ia_valid & ATTR_ATIME_SET))
1799 sattr->ia_valid |= ATTR_ATIME;
1801 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1802 !(sattr->ia_valid & ATTR_MTIME_SET))
1803 sattr->ia_valid |= ATTR_MTIME;
1807 * Returns a referenced nfs4_state
1809 static int _nfs4_do_open(struct inode *dir,
1810 struct dentry *dentry,
1813 struct iattr *sattr,
1814 struct rpc_cred *cred,
1815 struct nfs4_state **res,
1816 struct nfs4_threshold **ctx_th)
1818 struct nfs4_state_owner *sp;
1819 struct nfs4_state *state = NULL;
1820 struct nfs_server *server = NFS_SERVER(dir);
1821 struct nfs4_opendata *opendata;
1824 /* Protect against reboot recovery conflicts */
1826 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1828 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1831 status = nfs4_recover_expired_lease(server);
1833 goto err_put_state_owner;
1834 if (dentry->d_inode != NULL)
1835 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1837 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1838 if (opendata == NULL)
1839 goto err_put_state_owner;
1841 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1842 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1843 if (!opendata->f_attr.mdsthreshold)
1844 goto err_opendata_put;
1845 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
1847 if (dentry->d_inode != NULL)
1848 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1850 status = _nfs4_proc_open(opendata);
1852 goto err_opendata_put;
1854 state = nfs4_opendata_to_nfs4_state(opendata);
1855 status = PTR_ERR(state);
1857 goto err_opendata_put;
1858 if (server->caps & NFS_CAP_POSIX_LOCK)
1859 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1861 if (opendata->o_arg.open_flags & O_EXCL) {
1862 nfs4_exclusive_attrset(opendata, sattr);
1864 nfs_fattr_init(opendata->o_res.f_attr);
1865 status = nfs4_do_setattr(state->inode, cred,
1866 opendata->o_res.f_attr, sattr,
1869 nfs_setattr_update_inode(state->inode, sattr);
1870 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1873 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1874 *ctx_th = opendata->f_attr.mdsthreshold;
1876 kfree(opendata->f_attr.mdsthreshold);
1877 opendata->f_attr.mdsthreshold = NULL;
1879 nfs4_opendata_put(opendata);
1880 nfs4_put_state_owner(sp);
1884 kfree(opendata->f_attr.mdsthreshold);
1885 nfs4_opendata_put(opendata);
1886 err_put_state_owner:
1887 nfs4_put_state_owner(sp);
1894 static struct nfs4_state *nfs4_do_open(struct inode *dir,
1895 struct dentry *dentry,
1898 struct iattr *sattr,
1899 struct rpc_cred *cred,
1900 struct nfs4_threshold **ctx_th)
1902 struct nfs4_exception exception = { };
1903 struct nfs4_state *res;
1906 fmode &= FMODE_READ|FMODE_WRITE;
1908 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1912 /* NOTE: BAD_SEQID means the server and client disagree about the
1913 * book-keeping w.r.t. state-changing operations
1914 * (OPEN/CLOSE/LOCK/LOCKU...)
1915 * It is actually a sign of a bug on the client or on the server.
1917 * If we receive a BAD_SEQID error in the particular case of
1918 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1919 * have unhashed the old state_owner for us, and that we can
1920 * therefore safely retry using a new one. We should still warn
1921 * the user though...
1923 if (status == -NFS4ERR_BAD_SEQID) {
1924 pr_warn_ratelimited("NFS: v4 server %s "
1925 " returned a bad sequence-id error!\n",
1926 NFS_SERVER(dir)->nfs_client->cl_hostname);
1927 exception.retry = 1;
1931 * BAD_STATEID on OPEN means that the server cancelled our
1932 * state before it received the OPEN_CONFIRM.
1933 * Recover by retrying the request as per the discussion
1934 * on Page 181 of RFC3530.
1936 if (status == -NFS4ERR_BAD_STATEID) {
1937 exception.retry = 1;
1940 if (status == -EAGAIN) {
1941 /* We must have found a delegation */
1942 exception.retry = 1;
1945 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1946 status, &exception));
1947 } while (exception.retry);
1951 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1952 struct nfs_fattr *fattr, struct iattr *sattr,
1953 struct nfs4_state *state)
1955 struct nfs_server *server = NFS_SERVER(inode);
1956 struct nfs_setattrargs arg = {
1957 .fh = NFS_FH(inode),
1960 .bitmask = server->attr_bitmask,
1962 struct nfs_setattrres res = {
1966 struct rpc_message msg = {
1967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1972 unsigned long timestamp = jiffies;
1975 nfs_fattr_init(fattr);
1977 if (state != NULL) {
1978 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1979 current->files, current->tgid);
1980 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1982 /* Use that stateid */
1984 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
1986 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1987 if (status == 0 && state != NULL)
1988 renew_lease(server, timestamp);
1992 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1993 struct nfs_fattr *fattr, struct iattr *sattr,
1994 struct nfs4_state *state)
1996 struct nfs_server *server = NFS_SERVER(inode);
1997 struct nfs4_exception exception = {
2003 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2005 case -NFS4ERR_OPENMODE:
2006 if (state && !(state->state & FMODE_WRITE)) {
2008 if (sattr->ia_valid & ATTR_OPEN)
2013 err = nfs4_handle_exception(server, err, &exception);
2014 } while (exception.retry);
2019 struct nfs4_closedata {
2020 struct inode *inode;
2021 struct nfs4_state *state;
2022 struct nfs_closeargs arg;
2023 struct nfs_closeres res;
2024 struct nfs_fattr fattr;
2025 unsigned long timestamp;
2030 static void nfs4_free_closedata(void *data)
2032 struct nfs4_closedata *calldata = data;
2033 struct nfs4_state_owner *sp = calldata->state->owner;
2034 struct super_block *sb = calldata->state->inode->i_sb;
2037 pnfs_roc_release(calldata->state->inode);
2038 nfs4_put_open_state(calldata->state);
2039 nfs_free_seqid(calldata->arg.seqid);
2040 nfs4_put_state_owner(sp);
2041 nfs_sb_deactive(sb);
2045 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2048 spin_lock(&state->owner->so_lock);
2049 if (!(fmode & FMODE_READ))
2050 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2051 if (!(fmode & FMODE_WRITE))
2052 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2053 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2054 spin_unlock(&state->owner->so_lock);
2057 static void nfs4_close_done(struct rpc_task *task, void *data)
2059 struct nfs4_closedata *calldata = data;
2060 struct nfs4_state *state = calldata->state;
2061 struct nfs_server *server = NFS_SERVER(calldata->inode);
2063 dprintk("%s: begin!\n", __func__);
2064 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2066 /* hmm. we are done with the inode, and in the process of freeing
2067 * the state_owner. we keep this around to process errors
2069 switch (task->tk_status) {
2072 pnfs_roc_set_barrier(state->inode,
2073 calldata->roc_barrier);
2074 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2075 renew_lease(server, calldata->timestamp);
2076 nfs4_close_clear_stateid_flags(state,
2077 calldata->arg.fmode);
2079 case -NFS4ERR_STALE_STATEID:
2080 case -NFS4ERR_OLD_STATEID:
2081 case -NFS4ERR_BAD_STATEID:
2082 case -NFS4ERR_EXPIRED:
2083 if (calldata->arg.fmode == 0)
2086 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2087 rpc_restart_call_prepare(task);
2089 nfs_release_seqid(calldata->arg.seqid);
2090 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2091 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2094 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2096 struct nfs4_closedata *calldata = data;
2097 struct nfs4_state *state = calldata->state;
2100 dprintk("%s: begin!\n", __func__);
2101 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2104 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2105 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2106 spin_lock(&state->owner->so_lock);
2107 /* Calculate the change in open mode */
2108 if (state->n_rdwr == 0) {
2109 if (state->n_rdonly == 0) {
2110 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2111 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2112 calldata->arg.fmode &= ~FMODE_READ;
2114 if (state->n_wronly == 0) {
2115 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2116 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2117 calldata->arg.fmode &= ~FMODE_WRITE;
2120 spin_unlock(&state->owner->so_lock);
2123 /* Note: exit _without_ calling nfs4_close_done */
2124 task->tk_action = NULL;
2128 if (calldata->arg.fmode == 0) {
2129 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2130 if (calldata->roc &&
2131 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2132 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2138 nfs_fattr_init(calldata->res.fattr);
2139 calldata->timestamp = jiffies;
2140 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
2141 &calldata->arg.seq_args,
2142 &calldata->res.seq_res,
2145 rpc_call_start(task);
2147 dprintk("%s: done!\n", __func__);
2150 static const struct rpc_call_ops nfs4_close_ops = {
2151 .rpc_call_prepare = nfs4_close_prepare,
2152 .rpc_call_done = nfs4_close_done,
2153 .rpc_release = nfs4_free_closedata,
2157 * It is possible for data to be read/written from a mem-mapped file
2158 * after the sys_close call (which hits the vfs layer as a flush).
2159 * This means that we can't safely call nfsv4 close on a file until
2160 * the inode is cleared. This in turn means that we are not good
2161 * NFSv4 citizens - we do not indicate to the server to update the file's
2162 * share state even when we are done with one of the three share
2163 * stateid's in the inode.
2165 * NOTE: Caller must be holding the sp->so_owner semaphore!
2167 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
2169 struct nfs_server *server = NFS_SERVER(state->inode);
2170 struct nfs4_closedata *calldata;
2171 struct nfs4_state_owner *sp = state->owner;
2172 struct rpc_task *task;
2173 struct rpc_message msg = {
2174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2175 .rpc_cred = state->owner->so_cred,
2177 struct rpc_task_setup task_setup_data = {
2178 .rpc_client = server->client,
2179 .rpc_message = &msg,
2180 .callback_ops = &nfs4_close_ops,
2181 .workqueue = nfsiod_workqueue,
2182 .flags = RPC_TASK_ASYNC,
2184 int status = -ENOMEM;
2186 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2187 if (calldata == NULL)
2189 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2190 calldata->inode = state->inode;
2191 calldata->state = state;
2192 calldata->arg.fh = NFS_FH(state->inode);
2193 calldata->arg.stateid = &state->open_stateid;
2194 /* Serialization for the sequence id */
2195 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2196 if (calldata->arg.seqid == NULL)
2197 goto out_free_calldata;
2198 calldata->arg.fmode = 0;
2199 calldata->arg.bitmask = server->cache_consistency_bitmask;
2200 calldata->res.fattr = &calldata->fattr;
2201 calldata->res.seqid = calldata->arg.seqid;
2202 calldata->res.server = server;
2203 calldata->roc = roc;
2204 nfs_sb_active(calldata->inode->i_sb);
2206 msg.rpc_argp = &calldata->arg;
2207 msg.rpc_resp = &calldata->res;
2208 task_setup_data.callback_data = calldata;
2209 task = rpc_run_task(&task_setup_data);
2211 return PTR_ERR(task);
2214 status = rpc_wait_for_completion_task(task);
2221 pnfs_roc_release(state->inode);
2222 nfs4_put_open_state(state);
2223 nfs4_put_state_owner(sp);
2227 static struct inode *
2228 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2230 struct nfs4_state *state;
2232 /* Protect against concurrent sillydeletes */
2233 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2234 ctx->cred, &ctx->mdsthreshold);
2236 return ERR_CAST(state);
2238 return igrab(state->inode);
2241 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2243 if (ctx->state == NULL)
2246 nfs4_close_sync(ctx->state, ctx->mode);
2248 nfs4_close_state(ctx->state, ctx->mode);
2251 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2253 struct nfs4_server_caps_arg args = {
2256 struct nfs4_server_caps_res res = {};
2257 struct rpc_message msg = {
2258 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2264 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2266 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2267 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2268 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2269 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2270 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2271 NFS_CAP_CTIME|NFS_CAP_MTIME);
2272 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2273 server->caps |= NFS_CAP_ACLS;
2274 if (res.has_links != 0)
2275 server->caps |= NFS_CAP_HARDLINKS;
2276 if (res.has_symlinks != 0)
2277 server->caps |= NFS_CAP_SYMLINKS;
2278 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2279 server->caps |= NFS_CAP_FILEID;
2280 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2281 server->caps |= NFS_CAP_MODE;
2282 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2283 server->caps |= NFS_CAP_NLINK;
2284 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2285 server->caps |= NFS_CAP_OWNER;
2286 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2287 server->caps |= NFS_CAP_OWNER_GROUP;
2288 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2289 server->caps |= NFS_CAP_ATIME;
2290 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2291 server->caps |= NFS_CAP_CTIME;
2292 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2293 server->caps |= NFS_CAP_MTIME;
2295 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2296 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2297 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2298 server->acl_bitmask = res.acl_bitmask;
2299 server->fh_expire_type = res.fh_expire_type;
2305 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2307 struct nfs4_exception exception = { };
2310 err = nfs4_handle_exception(server,
2311 _nfs4_server_capabilities(server, fhandle),
2313 } while (exception.retry);
2317 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2318 struct nfs_fsinfo *info)
2320 struct nfs4_lookup_root_arg args = {
2321 .bitmask = nfs4_fattr_bitmap,
2323 struct nfs4_lookup_res res = {
2325 .fattr = info->fattr,
2328 struct rpc_message msg = {
2329 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2334 nfs_fattr_init(info->fattr);
2335 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2338 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2339 struct nfs_fsinfo *info)
2341 struct nfs4_exception exception = { };
2344 err = _nfs4_lookup_root(server, fhandle, info);
2347 case -NFS4ERR_WRONGSEC:
2350 err = nfs4_handle_exception(server, err, &exception);
2352 } while (exception.retry);
2357 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2358 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2360 struct rpc_auth *auth;
2363 auth = rpcauth_create(flavor, server->client);
2368 ret = nfs4_lookup_root(server, fhandle, info);
2373 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2374 struct nfs_fsinfo *info)
2376 int i, len, status = 0;
2377 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2379 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2380 flav_array[len] = RPC_AUTH_NULL;
2383 for (i = 0; i < len; i++) {
2384 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2385 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2390 * -EACCESS could mean that the user doesn't have correct permissions
2391 * to access the mount. It could also mean that we tried to mount
2392 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2393 * existing mount programs don't handle -EACCES very well so it should
2394 * be mapped to -EPERM instead.
2396 if (status == -EACCES)
2402 * get the file handle for the "/" directory on the server
2404 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2405 struct nfs_fsinfo *info)
2407 int minor_version = server->nfs_client->cl_minorversion;
2408 int status = nfs4_lookup_root(server, fhandle, info);
2409 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2411 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2412 * by nfs4_map_errors() as this function exits.
2414 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2416 status = nfs4_server_capabilities(server, fhandle);
2418 status = nfs4_do_fsinfo(server, fhandle, info);
2419 return nfs4_map_errors(status);
2422 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2423 struct nfs_fsinfo *info)
2426 struct nfs_fattr *fattr = info->fattr;
2428 error = nfs4_server_capabilities(server, mntfh);
2430 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2434 error = nfs4_proc_getattr(server, mntfh, fattr);
2436 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2440 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2441 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2442 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2448 * Get locations and (maybe) other attributes of a referral.
2449 * Note that we'll actually follow the referral later when
2450 * we detect fsid mismatch in inode revalidation
2452 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2453 const struct qstr *name, struct nfs_fattr *fattr,
2454 struct nfs_fh *fhandle)
2456 int status = -ENOMEM;
2457 struct page *page = NULL;
2458 struct nfs4_fs_locations *locations = NULL;
2460 page = alloc_page(GFP_KERNEL);
2463 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2464 if (locations == NULL)
2467 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2470 /* Make sure server returned a different fsid for the referral */
2471 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2472 dprintk("%s: server did not return a different fsid for"
2473 " a referral at %s\n", __func__, name->name);
2477 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2478 nfs_fixup_referral_attributes(&locations->fattr);
2480 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2481 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2482 memset(fhandle, 0, sizeof(struct nfs_fh));
2490 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2492 struct nfs4_getattr_arg args = {
2494 .bitmask = server->attr_bitmask,
2496 struct nfs4_getattr_res res = {
2500 struct rpc_message msg = {
2501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2506 nfs_fattr_init(fattr);
2507 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2510 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2512 struct nfs4_exception exception = { };
2515 err = nfs4_handle_exception(server,
2516 _nfs4_proc_getattr(server, fhandle, fattr),
2518 } while (exception.retry);
2523 * The file is not closed if it is opened due to the a request to change
2524 * the size of the file. The open call will not be needed once the
2525 * VFS layer lookup-intents are implemented.
2527 * Close is called when the inode is destroyed.
2528 * If we haven't opened the file for O_WRONLY, we
2529 * need to in the size_change case to obtain a stateid.
2532 * Because OPEN is always done by name in nfsv4, it is
2533 * possible that we opened a different file by the same
2534 * name. We can recognize this race condition, but we
2535 * can't do anything about it besides returning an error.
2537 * This will be fixed with VFS changes (lookup-intent).
2540 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2541 struct iattr *sattr)
2543 struct inode *inode = dentry->d_inode;
2544 struct rpc_cred *cred = NULL;
2545 struct nfs4_state *state = NULL;
2548 if (pnfs_ld_layoutret_on_setattr(inode))
2549 pnfs_return_layout(inode);
2551 nfs_fattr_init(fattr);
2553 /* Deal with open(O_TRUNC) */
2554 if (sattr->ia_valid & ATTR_OPEN)
2555 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2557 /* Optimization: if the end result is no change, don't RPC */
2558 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2561 /* Search for an existing open(O_WRITE) file */
2562 if (sattr->ia_valid & ATTR_FILE) {
2563 struct nfs_open_context *ctx;
2565 ctx = nfs_file_open_context(sattr->ia_file);
2572 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2574 nfs_setattr_update_inode(inode, sattr);
2578 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2579 const struct qstr *name, struct nfs_fh *fhandle,
2580 struct nfs_fattr *fattr)
2582 struct nfs_server *server = NFS_SERVER(dir);
2584 struct nfs4_lookup_arg args = {
2585 .bitmask = server->attr_bitmask,
2586 .dir_fh = NFS_FH(dir),
2589 struct nfs4_lookup_res res = {
2594 struct rpc_message msg = {
2595 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2600 nfs_fattr_init(fattr);
2602 dprintk("NFS call lookup %s\n", name->name);
2603 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2604 dprintk("NFS reply lookup: %d\n", status);
2608 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2610 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2611 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2612 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2616 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2617 struct qstr *name, struct nfs_fh *fhandle,
2618 struct nfs_fattr *fattr)
2620 struct nfs4_exception exception = { };
2621 struct rpc_clnt *client = *clnt;
2624 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2626 case -NFS4ERR_BADNAME:
2629 case -NFS4ERR_MOVED:
2630 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
2632 case -NFS4ERR_WRONGSEC:
2634 if (client != *clnt)
2637 client = nfs4_create_sec_client(client, dir, name);
2639 return PTR_ERR(client);
2641 exception.retry = 1;
2644 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2646 } while (exception.retry);
2651 else if (client != *clnt)
2652 rpc_shutdown_client(client);
2657 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
2658 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2661 struct rpc_clnt *client = NFS_CLIENT(dir);
2663 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2664 if (client != NFS_CLIENT(dir)) {
2665 rpc_shutdown_client(client);
2666 nfs_fixup_secinfo_attributes(fattr);
2672 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2673 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2676 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2678 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2680 rpc_shutdown_client(client);
2681 return ERR_PTR(status);
2686 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2688 struct nfs_server *server = NFS_SERVER(inode);
2689 struct nfs4_accessargs args = {
2690 .fh = NFS_FH(inode),
2691 .bitmask = server->cache_consistency_bitmask,
2693 struct nfs4_accessres res = {
2696 struct rpc_message msg = {
2697 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2700 .rpc_cred = entry->cred,
2702 int mode = entry->mask;
2706 * Determine which access bits we want to ask for...
2708 if (mode & MAY_READ)
2709 args.access |= NFS4_ACCESS_READ;
2710 if (S_ISDIR(inode->i_mode)) {
2711 if (mode & MAY_WRITE)
2712 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2713 if (mode & MAY_EXEC)
2714 args.access |= NFS4_ACCESS_LOOKUP;
2716 if (mode & MAY_WRITE)
2717 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2718 if (mode & MAY_EXEC)
2719 args.access |= NFS4_ACCESS_EXECUTE;
2722 res.fattr = nfs_alloc_fattr();
2723 if (res.fattr == NULL)
2726 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2729 if (res.access & NFS4_ACCESS_READ)
2730 entry->mask |= MAY_READ;
2731 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2732 entry->mask |= MAY_WRITE;
2733 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2734 entry->mask |= MAY_EXEC;
2735 nfs_refresh_inode(inode, res.fattr);
2737 nfs_free_fattr(res.fattr);
2741 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2743 struct nfs4_exception exception = { };
2746 err = nfs4_handle_exception(NFS_SERVER(inode),
2747 _nfs4_proc_access(inode, entry),
2749 } while (exception.retry);
2754 * TODO: For the time being, we don't try to get any attributes
2755 * along with any of the zero-copy operations READ, READDIR,
2758 * In the case of the first three, we want to put the GETATTR
2759 * after the read-type operation -- this is because it is hard
2760 * to predict the length of a GETATTR response in v4, and thus
2761 * align the READ data correctly. This means that the GETATTR
2762 * may end up partially falling into the page cache, and we should
2763 * shift it into the 'tail' of the xdr_buf before processing.
2764 * To do this efficiently, we need to know the total length
2765 * of data received, which doesn't seem to be available outside
2768 * In the case of WRITE, we also want to put the GETATTR after
2769 * the operation -- in this case because we want to make sure
2770 * we get the post-operation mtime and size.
2772 * Both of these changes to the XDR layer would in fact be quite
2773 * minor, but I decided to leave them for a subsequent patch.
2775 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2776 unsigned int pgbase, unsigned int pglen)
2778 struct nfs4_readlink args = {
2779 .fh = NFS_FH(inode),
2784 struct nfs4_readlink_res res;
2785 struct rpc_message msg = {
2786 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2791 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2794 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2795 unsigned int pgbase, unsigned int pglen)
2797 struct nfs4_exception exception = { };
2800 err = nfs4_handle_exception(NFS_SERVER(inode),
2801 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2803 } while (exception.retry);
2808 * This is just for mknod. open(O_CREAT) will always do ->open_context().
2811 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2814 struct nfs_open_context *ctx;
2815 struct nfs4_state *state;
2818 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2820 return PTR_ERR(ctx);
2822 sattr->ia_mode &= ~current_umask();
2823 state = nfs4_do_open(dir, dentry, ctx->mode,
2824 flags, sattr, ctx->cred,
2825 &ctx->mdsthreshold);
2827 if (IS_ERR(state)) {
2828 status = PTR_ERR(state);
2831 d_add(dentry, igrab(state->inode));
2832 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2835 put_nfs_open_context(ctx);
2839 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2841 struct nfs_server *server = NFS_SERVER(dir);
2842 struct nfs_removeargs args = {
2846 struct nfs_removeres res = {
2849 struct rpc_message msg = {
2850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2856 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2858 update_changeattr(dir, &res.cinfo);
2862 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2864 struct nfs4_exception exception = { };
2867 err = nfs4_handle_exception(NFS_SERVER(dir),
2868 _nfs4_proc_remove(dir, name),
2870 } while (exception.retry);
2874 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2876 struct nfs_server *server = NFS_SERVER(dir);
2877 struct nfs_removeargs *args = msg->rpc_argp;
2878 struct nfs_removeres *res = msg->rpc_resp;
2880 res->server = server;
2881 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2882 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
2885 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2887 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2888 &data->args.seq_args,
2892 rpc_call_start(task);
2895 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2897 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2899 if (!nfs4_sequence_done(task, &res->seq_res))
2901 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2903 update_changeattr(dir, &res->cinfo);
2907 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2909 struct nfs_server *server = NFS_SERVER(dir);
2910 struct nfs_renameargs *arg = msg->rpc_argp;
2911 struct nfs_renameres *res = msg->rpc_resp;
2913 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2914 res->server = server;
2915 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
2918 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2920 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2921 &data->args.seq_args,
2925 rpc_call_start(task);
2928 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2929 struct inode *new_dir)
2931 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2933 if (!nfs4_sequence_done(task, &res->seq_res))
2935 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2938 update_changeattr(old_dir, &res->old_cinfo);
2939 update_changeattr(new_dir, &res->new_cinfo);
2943 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2944 struct inode *new_dir, struct qstr *new_name)
2946 struct nfs_server *server = NFS_SERVER(old_dir);
2947 struct nfs_renameargs arg = {
2948 .old_dir = NFS_FH(old_dir),
2949 .new_dir = NFS_FH(new_dir),
2950 .old_name = old_name,
2951 .new_name = new_name,
2953 struct nfs_renameres res = {
2956 struct rpc_message msg = {
2957 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2961 int status = -ENOMEM;
2963 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2965 update_changeattr(old_dir, &res.old_cinfo);
2966 update_changeattr(new_dir, &res.new_cinfo);
2971 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2972 struct inode *new_dir, struct qstr *new_name)
2974 struct nfs4_exception exception = { };
2977 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2978 _nfs4_proc_rename(old_dir, old_name,
2981 } while (exception.retry);
2985 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2987 struct nfs_server *server = NFS_SERVER(inode);
2988 struct nfs4_link_arg arg = {
2989 .fh = NFS_FH(inode),
2990 .dir_fh = NFS_FH(dir),
2992 .bitmask = server->attr_bitmask,
2994 struct nfs4_link_res res = {
2997 struct rpc_message msg = {
2998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3002 int status = -ENOMEM;
3004 res.fattr = nfs_alloc_fattr();
3005 if (res.fattr == NULL)
3008 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3010 update_changeattr(dir, &res.cinfo);
3011 nfs_post_op_update_inode(inode, res.fattr);
3014 nfs_free_fattr(res.fattr);
3018 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3020 struct nfs4_exception exception = { };
3023 err = nfs4_handle_exception(NFS_SERVER(inode),
3024 _nfs4_proc_link(inode, dir, name),
3026 } while (exception.retry);
3030 struct nfs4_createdata {
3031 struct rpc_message msg;
3032 struct nfs4_create_arg arg;
3033 struct nfs4_create_res res;
3035 struct nfs_fattr fattr;
3038 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3039 struct qstr *name, struct iattr *sattr, u32 ftype)
3041 struct nfs4_createdata *data;
3043 data = kzalloc(sizeof(*data), GFP_KERNEL);
3045 struct nfs_server *server = NFS_SERVER(dir);
3047 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3048 data->msg.rpc_argp = &data->arg;
3049 data->msg.rpc_resp = &data->res;
3050 data->arg.dir_fh = NFS_FH(dir);
3051 data->arg.server = server;
3052 data->arg.name = name;
3053 data->arg.attrs = sattr;
3054 data->arg.ftype = ftype;
3055 data->arg.bitmask = server->attr_bitmask;
3056 data->res.server = server;
3057 data->res.fh = &data->fh;
3058 data->res.fattr = &data->fattr;
3059 nfs_fattr_init(data->res.fattr);
3064 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3066 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3067 &data->arg.seq_args, &data->res.seq_res, 1);
3069 update_changeattr(dir, &data->res.dir_cinfo);
3070 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3075 static void nfs4_free_createdata(struct nfs4_createdata *data)
3080 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3081 struct page *page, unsigned int len, struct iattr *sattr)
3083 struct nfs4_createdata *data;
3084 int status = -ENAMETOOLONG;
3086 if (len > NFS4_MAXPATHLEN)
3090 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3094 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3095 data->arg.u.symlink.pages = &page;
3096 data->arg.u.symlink.len = len;
3098 status = nfs4_do_create(dir, dentry, data);
3100 nfs4_free_createdata(data);
3105 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3106 struct page *page, unsigned int len, struct iattr *sattr)
3108 struct nfs4_exception exception = { };
3111 err = nfs4_handle_exception(NFS_SERVER(dir),
3112 _nfs4_proc_symlink(dir, dentry, page,
3115 } while (exception.retry);
3119 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3120 struct iattr *sattr)
3122 struct nfs4_createdata *data;
3123 int status = -ENOMEM;
3125 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3129 status = nfs4_do_create(dir, dentry, data);
3131 nfs4_free_createdata(data);
3136 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3137 struct iattr *sattr)
3139 struct nfs4_exception exception = { };
3142 sattr->ia_mode &= ~current_umask();
3144 err = nfs4_handle_exception(NFS_SERVER(dir),
3145 _nfs4_proc_mkdir(dir, dentry, sattr),
3147 } while (exception.retry);
3151 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3152 u64 cookie, struct page **pages, unsigned int count, int plus)
3154 struct inode *dir = dentry->d_inode;
3155 struct nfs4_readdir_arg args = {
3160 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3163 struct nfs4_readdir_res res;
3164 struct rpc_message msg = {
3165 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3172 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3173 dentry->d_parent->d_name.name,
3174 dentry->d_name.name,
3175 (unsigned long long)cookie);
3176 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3177 res.pgbase = args.pgbase;
3178 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3180 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
3181 status += args.pgbase;
3184 nfs_invalidate_atime(dir);
3186 dprintk("%s: returns %d\n", __func__, status);
3190 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3191 u64 cookie, struct page **pages, unsigned int count, int plus)
3193 struct nfs4_exception exception = { };
3196 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3197 _nfs4_proc_readdir(dentry, cred, cookie,
3198 pages, count, plus),
3200 } while (exception.retry);
3204 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3205 struct iattr *sattr, dev_t rdev)
3207 struct nfs4_createdata *data;
3208 int mode = sattr->ia_mode;
3209 int status = -ENOMEM;
3211 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3212 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3214 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3219 data->arg.ftype = NF4FIFO;
3220 else if (S_ISBLK(mode)) {
3221 data->arg.ftype = NF4BLK;
3222 data->arg.u.device.specdata1 = MAJOR(rdev);
3223 data->arg.u.device.specdata2 = MINOR(rdev);
3225 else if (S_ISCHR(mode)) {
3226 data->arg.ftype = NF4CHR;
3227 data->arg.u.device.specdata1 = MAJOR(rdev);
3228 data->arg.u.device.specdata2 = MINOR(rdev);
3231 status = nfs4_do_create(dir, dentry, data);
3233 nfs4_free_createdata(data);
3238 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3239 struct iattr *sattr, dev_t rdev)
3241 struct nfs4_exception exception = { };
3244 sattr->ia_mode &= ~current_umask();
3246 err = nfs4_handle_exception(NFS_SERVER(dir),
3247 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3249 } while (exception.retry);
3253 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3254 struct nfs_fsstat *fsstat)
3256 struct nfs4_statfs_arg args = {
3258 .bitmask = server->attr_bitmask,
3260 struct nfs4_statfs_res res = {
3263 struct rpc_message msg = {
3264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3269 nfs_fattr_init(fsstat->fattr);
3270 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3273 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3275 struct nfs4_exception exception = { };
3278 err = nfs4_handle_exception(server,
3279 _nfs4_proc_statfs(server, fhandle, fsstat),
3281 } while (exception.retry);
3285 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3286 struct nfs_fsinfo *fsinfo)
3288 struct nfs4_fsinfo_arg args = {
3290 .bitmask = server->attr_bitmask,
3292 struct nfs4_fsinfo_res res = {
3295 struct rpc_message msg = {
3296 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3301 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3304 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3306 struct nfs4_exception exception = { };
3310 err = nfs4_handle_exception(server,
3311 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3313 } while (exception.retry);
3317 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3321 nfs_fattr_init(fsinfo->fattr);
3322 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3324 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3329 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3330 struct nfs_pathconf *pathconf)
3332 struct nfs4_pathconf_arg args = {
3334 .bitmask = server->attr_bitmask,
3336 struct nfs4_pathconf_res res = {
3337 .pathconf = pathconf,
3339 struct rpc_message msg = {
3340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3345 /* None of the pathconf attributes are mandatory to implement */
3346 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3347 memset(pathconf, 0, sizeof(*pathconf));
3351 nfs_fattr_init(pathconf->fattr);
3352 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3355 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3356 struct nfs_pathconf *pathconf)
3358 struct nfs4_exception exception = { };
3362 err = nfs4_handle_exception(server,
3363 _nfs4_proc_pathconf(server, fhandle, pathconf),
3365 } while (exception.retry);
3369 void __nfs4_read_done_cb(struct nfs_read_data *data)
3371 nfs_invalidate_atime(data->header->inode);
3374 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3376 struct nfs_server *server = NFS_SERVER(data->header->inode);
3378 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3379 rpc_restart_call_prepare(task);
3383 __nfs4_read_done_cb(data);
3384 if (task->tk_status > 0)
3385 renew_lease(server, data->timestamp);
3389 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3392 dprintk("--> %s\n", __func__);
3394 if (!nfs4_sequence_done(task, &data->res.seq_res))
3397 return data->read_done_cb ? data->read_done_cb(task, data) :
3398 nfs4_read_done_cb(task, data);
3401 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3403 data->timestamp = jiffies;
3404 data->read_done_cb = nfs4_read_done_cb;
3405 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3406 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3409 static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3411 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3412 &data->args.seq_args,
3416 rpc_call_start(task);
3419 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3421 struct inode *inode = data->header->inode;
3423 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3424 rpc_restart_call_prepare(task);
3427 if (task->tk_status >= 0) {
3428 renew_lease(NFS_SERVER(inode), data->timestamp);
3429 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
3434 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3436 if (!nfs4_sequence_done(task, &data->res.seq_res))
3438 return data->write_done_cb ? data->write_done_cb(task, data) :
3439 nfs4_write_done_cb(task, data);
3443 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3445 const struct nfs_pgio_header *hdr = data->header;
3447 /* Don't request attributes for pNFS or O_DIRECT writes */
3448 if (data->ds_clp != NULL || hdr->dreq != NULL)
3450 /* Otherwise, request attributes if and only if we don't hold
3453 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
3456 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3458 struct nfs_server *server = NFS_SERVER(data->header->inode);
3460 if (!nfs4_write_need_cache_consistency_data(data)) {
3461 data->args.bitmask = NULL;
3462 data->res.fattr = NULL;
3464 data->args.bitmask = server->cache_consistency_bitmask;
3466 if (!data->write_done_cb)
3467 data->write_done_cb = nfs4_write_done_cb;
3468 data->res.server = server;
3469 data->timestamp = jiffies;
3471 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3472 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3475 static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3477 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3478 &data->args.seq_args,
3482 rpc_call_start(task);
3485 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3487 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3488 &data->args.seq_args,
3492 rpc_call_start(task);
3495 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
3497 struct inode *inode = data->inode;
3499 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3500 rpc_restart_call_prepare(task);
3506 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
3508 if (!nfs4_sequence_done(task, &data->res.seq_res))
3510 return data->commit_done_cb(task, data);
3513 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
3515 struct nfs_server *server = NFS_SERVER(data->inode);
3517 if (data->commit_done_cb == NULL)
3518 data->commit_done_cb = nfs4_commit_done_cb;
3519 data->res.server = server;
3520 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3521 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3524 struct nfs4_renewdata {
3525 struct nfs_client *client;
3526 unsigned long timestamp;
3530 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3531 * standalone procedure for queueing an asynchronous RENEW.
3533 static void nfs4_renew_release(void *calldata)
3535 struct nfs4_renewdata *data = calldata;
3536 struct nfs_client *clp = data->client;
3538 if (atomic_read(&clp->cl_count) > 1)
3539 nfs4_schedule_state_renewal(clp);
3540 nfs_put_client(clp);
3544 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3546 struct nfs4_renewdata *data = calldata;
3547 struct nfs_client *clp = data->client;
3548 unsigned long timestamp = data->timestamp;
3550 if (task->tk_status < 0) {
3551 /* Unless we're shutting down, schedule state recovery! */
3552 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3554 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3555 nfs4_schedule_lease_recovery(clp);
3558 nfs4_schedule_path_down_recovery(clp);
3560 do_renew_lease(clp, timestamp);
3563 static const struct rpc_call_ops nfs4_renew_ops = {
3564 .rpc_call_done = nfs4_renew_done,
3565 .rpc_release = nfs4_renew_release,
3568 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3570 struct rpc_message msg = {
3571 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3575 struct nfs4_renewdata *data;
3577 if (renew_flags == 0)
3579 if (!atomic_inc_not_zero(&clp->cl_count))
3581 data = kmalloc(sizeof(*data), GFP_NOFS);
3585 data->timestamp = jiffies;
3586 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3587 &nfs4_renew_ops, data);
3590 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3592 struct rpc_message msg = {
3593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3597 unsigned long now = jiffies;
3600 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3603 do_renew_lease(clp, now);
3607 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3609 return (server->caps & NFS_CAP_ACLS)
3610 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3611 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3614 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3615 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3618 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3620 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3621 struct page **pages, unsigned int *pgbase)
3623 struct page *newpage, **spages;
3629 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3630 newpage = alloc_page(GFP_KERNEL);
3632 if (newpage == NULL)
3634 memcpy(page_address(newpage), buf, len);
3639 } while (buflen != 0);
3645 __free_page(spages[rc-1]);
3649 struct nfs4_cached_acl {
3655 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3657 struct nfs_inode *nfsi = NFS_I(inode);
3659 spin_lock(&inode->i_lock);
3660 kfree(nfsi->nfs4_acl);
3661 nfsi->nfs4_acl = acl;
3662 spin_unlock(&inode->i_lock);
3665 static void nfs4_zap_acl_attr(struct inode *inode)
3667 nfs4_set_cached_acl(inode, NULL);
3670 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3672 struct nfs_inode *nfsi = NFS_I(inode);
3673 struct nfs4_cached_acl *acl;
3676 spin_lock(&inode->i_lock);
3677 acl = nfsi->nfs4_acl;
3680 if (buf == NULL) /* user is just asking for length */
3682 if (acl->cached == 0)
3684 ret = -ERANGE; /* see getxattr(2) man page */
3685 if (acl->len > buflen)
3687 memcpy(buf, acl->data, acl->len);
3691 spin_unlock(&inode->i_lock);
3695 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3697 struct nfs4_cached_acl *acl;
3699 if (pages && acl_len <= PAGE_SIZE) {
3700 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3704 _copy_from_pages(acl->data, pages, pgbase, acl_len);
3706 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3713 nfs4_set_cached_acl(inode, acl);
3717 * The getxattr API returns the required buffer length when called with a
3718 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3719 * the required buf. On a NULL buf, we send a page of data to the server
3720 * guessing that the ACL request can be serviced by a page. If so, we cache
3721 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3722 * the cache. If not so, we throw away the page, and cache the required
3723 * length. The next getxattr call will then produce another round trip to
3724 * the server, this time with the input buf of the required size.
3726 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3728 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3729 struct nfs_getaclargs args = {
3730 .fh = NFS_FH(inode),
3734 struct nfs_getaclres res = {
3737 struct rpc_message msg = {
3738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3742 int ret = -ENOMEM, npages, i, acl_len = 0;
3744 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3745 /* As long as we're doing a round trip to the server anyway,
3746 * let's be prepared for a page of acl data. */
3750 /* Add an extra page to handle the bitmap returned */
3753 for (i = 0; i < npages; i++) {
3754 pages[i] = alloc_page(GFP_KERNEL);
3759 /* for decoding across pages */
3760 res.acl_scratch = alloc_page(GFP_KERNEL);
3761 if (!res.acl_scratch)
3764 args.acl_len = npages * PAGE_SIZE;
3765 args.acl_pgbase = 0;
3767 /* Let decode_getfacl know not to fail if the ACL data is larger than
3768 * the page we send as a guess */
3770 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3772 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3773 __func__, buf, buflen, npages, args.acl_len);
3774 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3775 &msg, &args.seq_args, &res.seq_res, 0);
3779 acl_len = res.acl_len - res.acl_data_offset;
3780 if (acl_len > args.acl_len)
3781 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
3783 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
3787 if (acl_len > buflen)
3789 _copy_from_pages(buf, pages, res.acl_data_offset,
3794 for (i = 0; i < npages; i++)
3796 __free_page(pages[i]);
3797 if (res.acl_scratch)
3798 __free_page(res.acl_scratch);
3802 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3804 struct nfs4_exception exception = { };
3807 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3810 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3811 } while (exception.retry);
3815 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3817 struct nfs_server *server = NFS_SERVER(inode);
3820 if (!nfs4_server_supports_acls(server))
3822 ret = nfs_revalidate_inode(server, inode);
3825 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3826 nfs_zap_acl_cache(inode);
3827 ret = nfs4_read_cached_acl(inode, buf, buflen);
3829 /* -ENOENT is returned if there is no ACL or if there is an ACL
3830 * but no cached acl data, just the acl length */
3832 return nfs4_get_acl_uncached(inode, buf, buflen);
3835 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3837 struct nfs_server *server = NFS_SERVER(inode);
3838 struct page *pages[NFS4ACL_MAXPAGES];
3839 struct nfs_setaclargs arg = {
3840 .fh = NFS_FH(inode),
3844 struct nfs_setaclres res;
3845 struct rpc_message msg = {
3846 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3852 if (!nfs4_server_supports_acls(server))
3854 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3857 nfs4_inode_return_delegation(inode);
3858 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3861 * Free each page after tx, so the only ref left is
3862 * held by the network stack
3865 put_page(pages[i-1]);
3868 * Acl update can result in inode attribute update.
3869 * so mark the attribute cache invalid.
3871 spin_lock(&inode->i_lock);
3872 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3873 spin_unlock(&inode->i_lock);
3874 nfs_access_zap_cache(inode);
3875 nfs_zap_acl_cache(inode);
3879 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3881 struct nfs4_exception exception = { };
3884 err = nfs4_handle_exception(NFS_SERVER(inode),
3885 __nfs4_proc_set_acl(inode, buf, buflen),
3887 } while (exception.retry);
3892 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3894 struct nfs_client *clp = server->nfs_client;
3896 if (task->tk_status >= 0)
3898 switch(task->tk_status) {
3899 case -NFS4ERR_DELEG_REVOKED:
3900 case -NFS4ERR_ADMIN_REVOKED:
3901 case -NFS4ERR_BAD_STATEID:
3904 nfs_remove_bad_delegation(state->inode);
3905 case -NFS4ERR_OPENMODE:
3908 nfs4_schedule_stateid_recovery(server, state);
3909 goto wait_on_recovery;
3910 case -NFS4ERR_EXPIRED:
3912 nfs4_schedule_stateid_recovery(server, state);
3913 case -NFS4ERR_STALE_STATEID:
3914 case -NFS4ERR_STALE_CLIENTID:
3915 nfs4_schedule_lease_recovery(clp);
3916 goto wait_on_recovery;
3917 #if defined(CONFIG_NFS_V4_1)
3918 case -NFS4ERR_BADSESSION:
3919 case -NFS4ERR_BADSLOT:
3920 case -NFS4ERR_BAD_HIGH_SLOT:
3921 case -NFS4ERR_DEADSESSION:
3922 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3923 case -NFS4ERR_SEQ_FALSE_RETRY:
3924 case -NFS4ERR_SEQ_MISORDERED:
3925 dprintk("%s ERROR %d, Reset session\n", __func__,
3927 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
3928 task->tk_status = 0;
3930 #endif /* CONFIG_NFS_V4_1 */
3931 case -NFS4ERR_DELAY:
3932 nfs_inc_server_stats(server, NFSIOS_DELAY);
3933 case -NFS4ERR_GRACE:
3935 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3936 task->tk_status = 0;
3938 case -NFS4ERR_RETRY_UNCACHED_REP:
3939 case -NFS4ERR_OLD_STATEID:
3940 task->tk_status = 0;
3943 task->tk_status = nfs4_map_errors(task->tk_status);
3946 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3947 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3948 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3949 task->tk_status = 0;
3953 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3954 nfs4_verifier *bootverf)
3958 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3959 /* An impossible timestamp guarantees this value
3960 * will never match a generated boot time. */
3962 verf[1] = (__be32)(NSEC_PER_SEC + 1);
3964 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
3965 verf[0] = (__be32)nn->boot_time.tv_sec;
3966 verf[1] = (__be32)nn->boot_time.tv_nsec;
3968 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3971 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3972 unsigned short port, struct rpc_cred *cred,
3973 struct nfs4_setclientid_res *res)
3975 nfs4_verifier sc_verifier;
3976 struct nfs4_setclientid setclientid = {
3977 .sc_verifier = &sc_verifier,
3979 .sc_cb_ident = clp->cl_cb_ident,
3981 struct rpc_message msg = {
3982 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3983 .rpc_argp = &setclientid,
3990 nfs4_init_boot_verifier(clp, &sc_verifier);
3994 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3995 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3997 rpc_peeraddr2str(clp->cl_rpcclient,
3999 rpc_peeraddr2str(clp->cl_rpcclient,
4001 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4002 clp->cl_id_uniquifier);
4003 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4004 sizeof(setclientid.sc_netid),
4005 rpc_peeraddr2str(clp->cl_rpcclient,
4006 RPC_DISPLAY_NETID));
4007 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4008 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4009 clp->cl_ipaddr, port >> 8, port & 255);
4012 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4013 if (status != -NFS4ERR_CLID_INUSE)
4016 ++clp->cl_id_uniquifier;
4020 ssleep(clp->cl_lease_time / HZ + 1);
4025 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4026 struct nfs4_setclientid_res *arg,
4027 struct rpc_cred *cred)
4029 struct nfs_fsinfo fsinfo;
4030 struct rpc_message msg = {
4031 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4033 .rpc_resp = &fsinfo,
4040 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4042 spin_lock(&clp->cl_lock);
4043 clp->cl_lease_time = fsinfo.lease_time * HZ;
4044 clp->cl_last_renewal = now;
4045 spin_unlock(&clp->cl_lock);
4050 struct nfs4_delegreturndata {
4051 struct nfs4_delegreturnargs args;
4052 struct nfs4_delegreturnres res;
4054 nfs4_stateid stateid;
4055 unsigned long timestamp;
4056 struct nfs_fattr fattr;
4060 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4062 struct nfs4_delegreturndata *data = calldata;
4064 if (!nfs4_sequence_done(task, &data->res.seq_res))
4067 switch (task->tk_status) {
4068 case -NFS4ERR_STALE_STATEID:
4069 case -NFS4ERR_EXPIRED:
4071 renew_lease(data->res.server, data->timestamp);
4074 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4076 rpc_restart_call_prepare(task);
4080 data->rpc_status = task->tk_status;
4083 static void nfs4_delegreturn_release(void *calldata)
4088 #if defined(CONFIG_NFS_V4_1)
4089 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4091 struct nfs4_delegreturndata *d_data;
4093 d_data = (struct nfs4_delegreturndata *)data;
4095 if (nfs4_setup_sequence(d_data->res.server,
4096 &d_data->args.seq_args,
4097 &d_data->res.seq_res, task))
4099 rpc_call_start(task);
4101 #endif /* CONFIG_NFS_V4_1 */
4103 static const struct rpc_call_ops nfs4_delegreturn_ops = {
4104 #if defined(CONFIG_NFS_V4_1)
4105 .rpc_call_prepare = nfs4_delegreturn_prepare,
4106 #endif /* CONFIG_NFS_V4_1 */
4107 .rpc_call_done = nfs4_delegreturn_done,
4108 .rpc_release = nfs4_delegreturn_release,
4111 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4113 struct nfs4_delegreturndata *data;
4114 struct nfs_server *server = NFS_SERVER(inode);
4115 struct rpc_task *task;
4116 struct rpc_message msg = {
4117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4120 struct rpc_task_setup task_setup_data = {
4121 .rpc_client = server->client,
4122 .rpc_message = &msg,
4123 .callback_ops = &nfs4_delegreturn_ops,
4124 .flags = RPC_TASK_ASYNC,
4128 data = kzalloc(sizeof(*data), GFP_NOFS);
4131 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4132 data->args.fhandle = &data->fh;
4133 data->args.stateid = &data->stateid;
4134 data->args.bitmask = server->cache_consistency_bitmask;
4135 nfs_copy_fh(&data->fh, NFS_FH(inode));
4136 nfs4_stateid_copy(&data->stateid, stateid);
4137 data->res.fattr = &data->fattr;
4138 data->res.server = server;
4139 nfs_fattr_init(data->res.fattr);
4140 data->timestamp = jiffies;
4141 data->rpc_status = 0;
4143 task_setup_data.callback_data = data;
4144 msg.rpc_argp = &data->args;
4145 msg.rpc_resp = &data->res;
4146 task = rpc_run_task(&task_setup_data);
4148 return PTR_ERR(task);
4151 status = nfs4_wait_for_completion_rpc_task(task);
4154 status = data->rpc_status;
4156 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4158 nfs_refresh_inode(inode, &data->fattr);
4164 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4166 struct nfs_server *server = NFS_SERVER(inode);
4167 struct nfs4_exception exception = { };
4170 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4172 case -NFS4ERR_STALE_STATEID:
4173 case -NFS4ERR_EXPIRED:
4177 err = nfs4_handle_exception(server, err, &exception);
4178 } while (exception.retry);
4182 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4183 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4186 * sleep, with exponential backoff, and retry the LOCK operation.
4188 static unsigned long
4189 nfs4_set_lock_task_retry(unsigned long timeout)
4191 freezable_schedule_timeout_killable(timeout);
4193 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4194 return NFS4_LOCK_MAXTIMEOUT;
4198 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4200 struct inode *inode = state->inode;
4201 struct nfs_server *server = NFS_SERVER(inode);
4202 struct nfs_client *clp = server->nfs_client;
4203 struct nfs_lockt_args arg = {
4204 .fh = NFS_FH(inode),
4207 struct nfs_lockt_res res = {
4210 struct rpc_message msg = {
4211 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4214 .rpc_cred = state->owner->so_cred,
4216 struct nfs4_lock_state *lsp;
4219 arg.lock_owner.clientid = clp->cl_clientid;
4220 status = nfs4_set_lock_state(state, request);
4223 lsp = request->fl_u.nfs4_fl.owner;
4224 arg.lock_owner.id = lsp->ls_seqid.owner_id;
4225 arg.lock_owner.s_dev = server->s_dev;
4226 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4229 request->fl_type = F_UNLCK;
4231 case -NFS4ERR_DENIED:
4234 request->fl_ops->fl_release_private(request);
4239 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4241 struct nfs4_exception exception = { };
4245 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4246 _nfs4_proc_getlk(state, cmd, request),
4248 } while (exception.retry);
4252 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4255 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4257 res = posix_lock_file_wait(file, fl);
4260 res = flock_lock_file_wait(file, fl);
4268 struct nfs4_unlockdata {
4269 struct nfs_locku_args arg;
4270 struct nfs_locku_res res;
4271 struct nfs4_lock_state *lsp;
4272 struct nfs_open_context *ctx;
4273 struct file_lock fl;
4274 const struct nfs_server *server;
4275 unsigned long timestamp;
4278 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4279 struct nfs_open_context *ctx,
4280 struct nfs4_lock_state *lsp,
4281 struct nfs_seqid *seqid)
4283 struct nfs4_unlockdata *p;
4284 struct inode *inode = lsp->ls_state->inode;
4286 p = kzalloc(sizeof(*p), GFP_NOFS);
4289 p->arg.fh = NFS_FH(inode);
4291 p->arg.seqid = seqid;
4292 p->res.seqid = seqid;
4293 p->arg.stateid = &lsp->ls_stateid;
4295 atomic_inc(&lsp->ls_count);
4296 /* Ensure we don't close file until we're done freeing locks! */
4297 p->ctx = get_nfs_open_context(ctx);
4298 memcpy(&p->fl, fl, sizeof(p->fl));
4299 p->server = NFS_SERVER(inode);
4303 static void nfs4_locku_release_calldata(void *data)
4305 struct nfs4_unlockdata *calldata = data;
4306 nfs_free_seqid(calldata->arg.seqid);
4307 nfs4_put_lock_state(calldata->lsp);
4308 put_nfs_open_context(calldata->ctx);
4312 static void nfs4_locku_done(struct rpc_task *task, void *data)
4314 struct nfs4_unlockdata *calldata = data;
4316 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4318 switch (task->tk_status) {
4320 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4321 &calldata->res.stateid);
4322 renew_lease(calldata->server, calldata->timestamp);
4324 case -NFS4ERR_BAD_STATEID:
4325 case -NFS4ERR_OLD_STATEID:
4326 case -NFS4ERR_STALE_STATEID:
4327 case -NFS4ERR_EXPIRED:
4330 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4331 rpc_restart_call_prepare(task);
4335 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4337 struct nfs4_unlockdata *calldata = data;
4339 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4341 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
4342 /* Note: exit _without_ running nfs4_locku_done */
4343 task->tk_action = NULL;
4346 calldata->timestamp = jiffies;
4347 if (nfs4_setup_sequence(calldata->server,
4348 &calldata->arg.seq_args,
4349 &calldata->res.seq_res, task))
4351 rpc_call_start(task);
4354 static const struct rpc_call_ops nfs4_locku_ops = {
4355 .rpc_call_prepare = nfs4_locku_prepare,
4356 .rpc_call_done = nfs4_locku_done,
4357 .rpc_release = nfs4_locku_release_calldata,
4360 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4361 struct nfs_open_context *ctx,
4362 struct nfs4_lock_state *lsp,
4363 struct nfs_seqid *seqid)
4365 struct nfs4_unlockdata *data;
4366 struct rpc_message msg = {
4367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4368 .rpc_cred = ctx->cred,
4370 struct rpc_task_setup task_setup_data = {
4371 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4372 .rpc_message = &msg,
4373 .callback_ops = &nfs4_locku_ops,
4374 .workqueue = nfsiod_workqueue,
4375 .flags = RPC_TASK_ASYNC,
4378 /* Ensure this is an unlock - when canceling a lock, the
4379 * canceled lock is passed in, and it won't be an unlock.
4381 fl->fl_type = F_UNLCK;
4383 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4385 nfs_free_seqid(seqid);
4386 return ERR_PTR(-ENOMEM);
4389 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4390 msg.rpc_argp = &data->arg;
4391 msg.rpc_resp = &data->res;
4392 task_setup_data.callback_data = data;
4393 return rpc_run_task(&task_setup_data);
4396 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4398 struct nfs_inode *nfsi = NFS_I(state->inode);
4399 struct nfs_seqid *seqid;
4400 struct nfs4_lock_state *lsp;
4401 struct rpc_task *task;
4403 unsigned char fl_flags = request->fl_flags;
4405 status = nfs4_set_lock_state(state, request);
4406 /* Unlock _before_ we do the RPC call */
4407 request->fl_flags |= FL_EXISTS;
4408 down_read(&nfsi->rwsem);
4409 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4410 up_read(&nfsi->rwsem);
4413 up_read(&nfsi->rwsem);
4416 /* Is this a delegated lock? */
4417 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4419 lsp = request->fl_u.nfs4_fl.owner;
4420 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4424 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4425 status = PTR_ERR(task);
4428 status = nfs4_wait_for_completion_rpc_task(task);
4431 request->fl_flags = fl_flags;
4435 struct nfs4_lockdata {
4436 struct nfs_lock_args arg;
4437 struct nfs_lock_res res;
4438 struct nfs4_lock_state *lsp;
4439 struct nfs_open_context *ctx;
4440 struct file_lock fl;
4441 unsigned long timestamp;
4444 struct nfs_server *server;
4447 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4448 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4451 struct nfs4_lockdata *p;
4452 struct inode *inode = lsp->ls_state->inode;
4453 struct nfs_server *server = NFS_SERVER(inode);
4455 p = kzalloc(sizeof(*p), gfp_mask);
4459 p->arg.fh = NFS_FH(inode);
4461 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4462 if (p->arg.open_seqid == NULL)
4464 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4465 if (p->arg.lock_seqid == NULL)
4466 goto out_free_seqid;
4467 p->arg.lock_stateid = &lsp->ls_stateid;
4468 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4469 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4470 p->arg.lock_owner.s_dev = server->s_dev;
4471 p->res.lock_seqid = p->arg.lock_seqid;
4474 atomic_inc(&lsp->ls_count);
4475 p->ctx = get_nfs_open_context(ctx);
4476 memcpy(&p->fl, fl, sizeof(p->fl));
4479 nfs_free_seqid(p->arg.open_seqid);
4485 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4487 struct nfs4_lockdata *data = calldata;
4488 struct nfs4_state *state = data->lsp->ls_state;
4490 dprintk("%s: begin!\n", __func__);
4491 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4493 /* Do we need to do an open_to_lock_owner? */
4494 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4495 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4497 data->arg.open_stateid = &state->stateid;
4498 data->arg.new_lock_owner = 1;
4499 data->res.open_seqid = data->arg.open_seqid;
4501 data->arg.new_lock_owner = 0;
4502 data->timestamp = jiffies;
4503 if (nfs4_setup_sequence(data->server,
4504 &data->arg.seq_args,
4505 &data->res.seq_res, task))
4507 rpc_call_start(task);
4508 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4511 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4513 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4514 nfs4_lock_prepare(task, calldata);
4517 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4519 struct nfs4_lockdata *data = calldata;
4521 dprintk("%s: begin!\n", __func__);
4523 if (!nfs4_sequence_done(task, &data->res.seq_res))
4526 data->rpc_status = task->tk_status;
4527 if (data->arg.new_lock_owner != 0) {
4528 if (data->rpc_status == 0)
4529 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4533 if (data->rpc_status == 0) {
4534 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4535 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4536 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4539 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4542 static void nfs4_lock_release(void *calldata)
4544 struct nfs4_lockdata *data = calldata;
4546 dprintk("%s: begin!\n", __func__);
4547 nfs_free_seqid(data->arg.open_seqid);
4548 if (data->cancelled != 0) {
4549 struct rpc_task *task;
4550 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4551 data->arg.lock_seqid);
4553 rpc_put_task_async(task);
4554 dprintk("%s: cancelling lock!\n", __func__);
4556 nfs_free_seqid(data->arg.lock_seqid);
4557 nfs4_put_lock_state(data->lsp);
4558 put_nfs_open_context(data->ctx);
4560 dprintk("%s: done!\n", __func__);
4563 static const struct rpc_call_ops nfs4_lock_ops = {
4564 .rpc_call_prepare = nfs4_lock_prepare,
4565 .rpc_call_done = nfs4_lock_done,
4566 .rpc_release = nfs4_lock_release,
4569 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4570 .rpc_call_prepare = nfs4_recover_lock_prepare,
4571 .rpc_call_done = nfs4_lock_done,
4572 .rpc_release = nfs4_lock_release,
4575 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4578 case -NFS4ERR_ADMIN_REVOKED:
4579 case -NFS4ERR_BAD_STATEID:
4580 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4581 if (new_lock_owner != 0 ||
4582 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4583 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4585 case -NFS4ERR_STALE_STATEID:
4586 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4587 case -NFS4ERR_EXPIRED:
4588 nfs4_schedule_lease_recovery(server->nfs_client);
4592 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4594 struct nfs4_lockdata *data;
4595 struct rpc_task *task;
4596 struct rpc_message msg = {
4597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4598 .rpc_cred = state->owner->so_cred,
4600 struct rpc_task_setup task_setup_data = {
4601 .rpc_client = NFS_CLIENT(state->inode),
4602 .rpc_message = &msg,
4603 .callback_ops = &nfs4_lock_ops,
4604 .workqueue = nfsiod_workqueue,
4605 .flags = RPC_TASK_ASYNC,
4609 dprintk("%s: begin!\n", __func__);
4610 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4611 fl->fl_u.nfs4_fl.owner,
4612 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4616 data->arg.block = 1;
4617 if (recovery_type > NFS_LOCK_NEW) {
4618 if (recovery_type == NFS_LOCK_RECLAIM)
4619 data->arg.reclaim = NFS_LOCK_RECLAIM;
4620 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4622 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4623 msg.rpc_argp = &data->arg;
4624 msg.rpc_resp = &data->res;
4625 task_setup_data.callback_data = data;
4626 task = rpc_run_task(&task_setup_data);
4628 return PTR_ERR(task);
4629 ret = nfs4_wait_for_completion_rpc_task(task);
4631 ret = data->rpc_status;
4633 nfs4_handle_setlk_error(data->server, data->lsp,
4634 data->arg.new_lock_owner, ret);
4636 data->cancelled = 1;
4638 dprintk("%s: done, ret = %d!\n", __func__, ret);
4642 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4644 struct nfs_server *server = NFS_SERVER(state->inode);
4645 struct nfs4_exception exception = {
4646 .inode = state->inode,
4651 /* Cache the lock if possible... */
4652 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4654 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4655 if (err != -NFS4ERR_DELAY)
4657 nfs4_handle_exception(server, err, &exception);
4658 } while (exception.retry);
4662 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4664 struct nfs_server *server = NFS_SERVER(state->inode);
4665 struct nfs4_exception exception = {
4666 .inode = state->inode,
4670 err = nfs4_set_lock_state(state, request);
4674 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4676 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4680 case -NFS4ERR_GRACE:
4681 case -NFS4ERR_DELAY:
4682 nfs4_handle_exception(server, err, &exception);
4685 } while (exception.retry);
4690 #if defined(CONFIG_NFS_V4_1)
4691 static int nfs41_check_expired_locks(struct nfs4_state *state)
4693 int status, ret = NFS_OK;
4694 struct nfs4_lock_state *lsp;
4695 struct nfs_server *server = NFS_SERVER(state->inode);
4697 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4698 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4699 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4700 if (status != NFS_OK) {
4701 if (status != -NFS4ERR_BAD_STATEID)
4702 nfs41_free_stateid(server,
4704 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4713 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4715 int status = NFS_OK;
4717 if (test_bit(LK_STATE_IN_USE, &state->flags))
4718 status = nfs41_check_expired_locks(state);
4719 if (status == NFS_OK)
4721 return nfs4_lock_expired(state, request);
4725 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4727 struct nfs_inode *nfsi = NFS_I(state->inode);
4728 unsigned char fl_flags = request->fl_flags;
4729 int status = -ENOLCK;
4731 if ((fl_flags & FL_POSIX) &&
4732 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4734 /* Is this a delegated open? */
4735 status = nfs4_set_lock_state(state, request);
4738 request->fl_flags |= FL_ACCESS;
4739 status = do_vfs_lock(request->fl_file, request);
4742 down_read(&nfsi->rwsem);
4743 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4744 /* Yes: cache locks! */
4745 /* ...but avoid races with delegation recall... */
4746 request->fl_flags = fl_flags & ~FL_SLEEP;
4747 status = do_vfs_lock(request->fl_file, request);
4750 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4753 /* Note: we always want to sleep here! */
4754 request->fl_flags = fl_flags | FL_SLEEP;
4755 if (do_vfs_lock(request->fl_file, request) < 0)
4756 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4757 "manager!\n", __func__);
4759 up_read(&nfsi->rwsem);
4761 request->fl_flags = fl_flags;
4765 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4767 struct nfs4_exception exception = {
4769 .inode = state->inode,
4774 err = _nfs4_proc_setlk(state, cmd, request);
4775 if (err == -NFS4ERR_DENIED)
4777 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4779 } while (exception.retry);
4784 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4786 struct nfs_open_context *ctx;
4787 struct nfs4_state *state;
4788 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4791 /* verify open state */
4792 ctx = nfs_file_open_context(filp);
4795 if (request->fl_start < 0 || request->fl_end < 0)
4798 if (IS_GETLK(cmd)) {
4800 return nfs4_proc_getlk(state, F_GETLK, request);
4804 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4807 if (request->fl_type == F_UNLCK) {
4809 return nfs4_proc_unlck(state, cmd, request);
4816 * Don't rely on the VFS having checked the file open mode,
4817 * since it won't do this for flock() locks.
4819 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4821 if (!(filp->f_mode & FMODE_READ))
4825 if (!(filp->f_mode & FMODE_WRITE))
4830 status = nfs4_proc_setlk(state, cmd, request);
4831 if ((status != -EAGAIN) || IS_SETLK(cmd))
4833 timeout = nfs4_set_lock_task_retry(timeout);
4834 status = -ERESTARTSYS;
4837 } while(status < 0);
4841 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4843 struct nfs_server *server = NFS_SERVER(state->inode);
4844 struct nfs4_exception exception = { };
4847 err = nfs4_set_lock_state(state, fl);
4851 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4854 printk(KERN_ERR "NFS: %s: unhandled error "
4855 "%d.\n", __func__, err);
4859 case -NFS4ERR_EXPIRED:
4860 nfs4_schedule_stateid_recovery(server, state);
4861 case -NFS4ERR_STALE_CLIENTID:
4862 case -NFS4ERR_STALE_STATEID:
4863 nfs4_schedule_lease_recovery(server->nfs_client);
4865 case -NFS4ERR_BADSESSION:
4866 case -NFS4ERR_BADSLOT:
4867 case -NFS4ERR_BAD_HIGH_SLOT:
4868 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4869 case -NFS4ERR_DEADSESSION:
4870 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
4874 * The show must go on: exit, but mark the
4875 * stateid as needing recovery.
4877 case -NFS4ERR_DELEG_REVOKED:
4878 case -NFS4ERR_ADMIN_REVOKED:
4879 case -NFS4ERR_BAD_STATEID:
4880 case -NFS4ERR_OPENMODE:
4881 nfs4_schedule_stateid_recovery(server, state);
4886 * User RPCSEC_GSS context has expired.
4887 * We cannot recover this stateid now, so
4888 * skip it and allow recovery thread to
4894 case -NFS4ERR_DENIED:
4895 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4898 case -NFS4ERR_DELAY:
4901 err = nfs4_handle_exception(server, err, &exception);
4902 } while (exception.retry);
4907 struct nfs_release_lockowner_data {
4908 struct nfs4_lock_state *lsp;
4909 struct nfs_server *server;
4910 struct nfs_release_lockowner_args args;
4913 static void nfs4_release_lockowner_release(void *calldata)
4915 struct nfs_release_lockowner_data *data = calldata;
4916 nfs4_free_lock_state(data->server, data->lsp);
4920 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
4921 .rpc_release = nfs4_release_lockowner_release,
4924 int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
4926 struct nfs_server *server = lsp->ls_state->owner->so_server;
4927 struct nfs_release_lockowner_data *data;
4928 struct rpc_message msg = {
4929 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4932 if (server->nfs_client->cl_mvops->minor_version != 0)
4934 data = kmalloc(sizeof(*data), GFP_NOFS);
4938 data->server = server;
4939 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4940 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4941 data->args.lock_owner.s_dev = server->s_dev;
4942 msg.rpc_argp = &data->args;
4943 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4947 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4949 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4950 const void *buf, size_t buflen,
4951 int flags, int type)
4953 if (strcmp(key, "") != 0)
4956 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
4959 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4960 void *buf, size_t buflen, int type)
4962 if (strcmp(key, "") != 0)
4965 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
4968 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4969 size_t list_len, const char *name,
4970 size_t name_len, int type)
4972 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
4974 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4977 if (list && len <= list_len)
4978 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
4983 * nfs_fhget will use either the mounted_on_fileid or the fileid
4985 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4987 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4988 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4989 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4990 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
4993 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4994 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
4995 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4999 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5000 const struct qstr *name,
5001 struct nfs4_fs_locations *fs_locations,
5004 struct nfs_server *server = NFS_SERVER(dir);
5006 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
5008 struct nfs4_fs_locations_arg args = {
5009 .dir_fh = NFS_FH(dir),
5014 struct nfs4_fs_locations_res res = {
5015 .fs_locations = fs_locations,
5017 struct rpc_message msg = {
5018 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5024 dprintk("%s: start\n", __func__);
5026 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5027 * is not supported */
5028 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5029 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5031 bitmask[0] |= FATTR4_WORD0_FILEID;
5033 nfs_fattr_init(&fs_locations->fattr);
5034 fs_locations->server = server;
5035 fs_locations->nlocations = 0;
5036 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
5037 dprintk("%s: returned status = %d\n", __func__, status);
5041 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5042 const struct qstr *name,
5043 struct nfs4_fs_locations *fs_locations,
5046 struct nfs4_exception exception = { };
5049 err = nfs4_handle_exception(NFS_SERVER(dir),
5050 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
5052 } while (exception.retry);
5056 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5059 struct nfs4_secinfo_arg args = {
5060 .dir_fh = NFS_FH(dir),
5063 struct nfs4_secinfo_res res = {
5066 struct rpc_message msg = {
5067 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5072 dprintk("NFS call secinfo %s\n", name->name);
5073 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5074 dprintk("NFS reply secinfo: %d\n", status);
5078 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5079 struct nfs4_secinfo_flavors *flavors)
5081 struct nfs4_exception exception = { };
5084 err = nfs4_handle_exception(NFS_SERVER(dir),
5085 _nfs4_proc_secinfo(dir, name, flavors),
5087 } while (exception.retry);
5091 #ifdef CONFIG_NFS_V4_1
5093 * Check the exchange flags returned by the server for invalid flags, having
5094 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5097 static int nfs4_check_cl_exchange_flags(u32 flags)
5099 if (flags & ~EXCHGID4_FLAG_MASK_R)
5101 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5102 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5104 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5108 return -NFS4ERR_INVAL;
5112 nfs41_same_server_scope(struct nfs41_server_scope *a,
5113 struct nfs41_server_scope *b)
5115 if (a->server_scope_sz == b->server_scope_sz &&
5116 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5123 * nfs4_proc_bind_conn_to_session()
5125 * The 4.1 client currently uses the same TCP connection for the
5126 * fore and backchannel.
5128 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
5131 struct nfs41_bind_conn_to_session_res res;
5132 struct rpc_message msg = {
5134 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5140 dprintk("--> %s\n", __func__);
5141 BUG_ON(clp == NULL);
5143 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5144 if (unlikely(res.session == NULL)) {
5149 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5151 if (memcmp(res.session->sess_id.data,
5152 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5153 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5157 if (res.dir != NFS4_CDFS4_BOTH) {
5158 dprintk("NFS: %s: Unexpected direction from server\n",
5163 if (res.use_conn_in_rdma_mode) {
5164 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5173 dprintk("<-- %s status= %d\n", __func__, status);
5178 * nfs4_proc_exchange_id()
5180 * Since the clientid has expired, all compounds using sessions
5181 * associated with the stale clientid will be returning
5182 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5183 * be in some phase of session reset.
5185 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
5187 nfs4_verifier verifier;
5188 struct nfs41_exchange_id_args args = {
5189 .verifier = &verifier,
5191 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
5193 struct nfs41_exchange_id_res res = {
5197 struct rpc_message msg = {
5198 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5204 dprintk("--> %s\n", __func__);
5205 BUG_ON(clp == NULL);
5207 nfs4_init_boot_verifier(clp, &verifier);
5209 args.id_len = scnprintf(args.id, sizeof(args.id),
5212 clp->cl_rpcclient->cl_nodename,
5213 clp->cl_rpcclient->cl_auth->au_flavor);
5215 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
5217 if (unlikely(res.server_owner == NULL)) {
5222 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
5224 if (unlikely(res.server_scope == NULL)) {
5226 goto out_server_owner;
5229 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
5230 if (unlikely(res.impl_id == NULL)) {
5232 goto out_server_scope;
5235 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5237 status = nfs4_check_cl_exchange_flags(res.flags);
5240 clp->cl_clientid = res.clientid;
5241 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5242 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5243 clp->cl_seqid = res.seqid;
5245 kfree(clp->cl_serverowner);
5246 clp->cl_serverowner = res.server_owner;
5247 res.server_owner = NULL;
5249 /* use the most recent implementation id */
5250 kfree(clp->cl_implid);
5251 clp->cl_implid = res.impl_id;
5253 if (clp->cl_serverscope != NULL &&
5254 !nfs41_same_server_scope(clp->cl_serverscope,
5255 res.server_scope)) {
5256 dprintk("%s: server_scope mismatch detected\n",
5258 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5259 kfree(clp->cl_serverscope);
5260 clp->cl_serverscope = NULL;
5263 if (clp->cl_serverscope == NULL) {
5264 clp->cl_serverscope = res.server_scope;
5271 kfree(res.server_owner);
5273 kfree(res.server_scope);
5275 if (clp->cl_implid != NULL)
5276 dprintk("%s: Server Implementation ID: "
5277 "domain: %s, name: %s, date: %llu,%u\n",
5278 __func__, clp->cl_implid->domain, clp->cl_implid->name,
5279 clp->cl_implid->date.seconds,
5280 clp->cl_implid->date.nseconds);
5281 dprintk("<-- %s status= %d\n", __func__, status);
5285 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5286 struct rpc_cred *cred)
5288 struct rpc_message msg = {
5289 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5295 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5297 dprintk("NFS: Got error %d from the server %s on "
5298 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5302 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5303 struct rpc_cred *cred)
5308 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5309 ret = _nfs4_proc_destroy_clientid(clp, cred);
5311 case -NFS4ERR_DELAY:
5312 case -NFS4ERR_CLIENTID_BUSY:
5322 int nfs4_destroy_clientid(struct nfs_client *clp)
5324 struct rpc_cred *cred;
5327 if (clp->cl_mvops->minor_version < 1)
5329 if (clp->cl_exchange_flags == 0)
5331 cred = nfs4_get_exchange_id_cred(clp);
5332 ret = nfs4_proc_destroy_clientid(clp, cred);
5337 case -NFS4ERR_STALE_CLIENTID:
5338 clp->cl_exchange_flags = 0;
5344 struct nfs4_get_lease_time_data {
5345 struct nfs4_get_lease_time_args *args;
5346 struct nfs4_get_lease_time_res *res;
5347 struct nfs_client *clp;
5350 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5354 struct nfs4_get_lease_time_data *data =
5355 (struct nfs4_get_lease_time_data *)calldata;
5357 dprintk("--> %s\n", __func__);
5358 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5359 /* just setup sequence, do not trigger session recovery
5360 since we're invoked within one */
5361 ret = nfs41_setup_sequence(data->clp->cl_session,
5362 &data->args->la_seq_args,
5363 &data->res->lr_seq_res, task);
5365 BUG_ON(ret == -EAGAIN);
5366 rpc_call_start(task);
5367 dprintk("<-- %s\n", __func__);
5371 * Called from nfs4_state_manager thread for session setup, so don't recover
5372 * from sequence operation or clientid errors.
5374 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5376 struct nfs4_get_lease_time_data *data =
5377 (struct nfs4_get_lease_time_data *)calldata;
5379 dprintk("--> %s\n", __func__);
5380 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5382 switch (task->tk_status) {
5383 case -NFS4ERR_DELAY:
5384 case -NFS4ERR_GRACE:
5385 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5386 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5387 task->tk_status = 0;
5389 case -NFS4ERR_RETRY_UNCACHED_REP:
5390 rpc_restart_call_prepare(task);
5393 dprintk("<-- %s\n", __func__);
5396 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5397 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5398 .rpc_call_done = nfs4_get_lease_time_done,
5401 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5403 struct rpc_task *task;
5404 struct nfs4_get_lease_time_args args;
5405 struct nfs4_get_lease_time_res res = {
5406 .lr_fsinfo = fsinfo,
5408 struct nfs4_get_lease_time_data data = {
5413 struct rpc_message msg = {
5414 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5418 struct rpc_task_setup task_setup = {
5419 .rpc_client = clp->cl_rpcclient,
5420 .rpc_message = &msg,
5421 .callback_ops = &nfs4_get_lease_time_ops,
5422 .callback_data = &data,
5423 .flags = RPC_TASK_TIMEOUT,
5427 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5428 dprintk("--> %s\n", __func__);
5429 task = rpc_run_task(&task_setup);
5432 status = PTR_ERR(task);
5434 status = task->tk_status;
5437 dprintk("<-- %s return %d\n", __func__, status);
5442 static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5444 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5447 static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5448 struct nfs4_slot *new,
5452 struct nfs4_slot *old = NULL;
5455 spin_lock(&tbl->slot_tbl_lock);
5459 tbl->max_slots = max_slots;
5461 tbl->highest_used_slotid = -1; /* no slot is currently used */
5462 for (i = 0; i < tbl->max_slots; i++)
5463 tbl->slots[i].seq_nr = ivalue;
5464 spin_unlock(&tbl->slot_tbl_lock);
5469 * (re)Initialise a slot table
5471 static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5474 struct nfs4_slot *new = NULL;
5477 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5478 max_reqs, tbl->max_slots);
5480 /* Does the newly negotiated max_reqs match the existing slot table? */
5481 if (max_reqs != tbl->max_slots) {
5482 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
5488 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
5489 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5490 tbl, tbl->slots, tbl->max_slots);
5492 dprintk("<-- %s: return %d\n", __func__, ret);
5496 /* Destroy the slot table */
5497 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5499 if (session->fc_slot_table.slots != NULL) {
5500 kfree(session->fc_slot_table.slots);
5501 session->fc_slot_table.slots = NULL;
5503 if (session->bc_slot_table.slots != NULL) {
5504 kfree(session->bc_slot_table.slots);
5505 session->bc_slot_table.slots = NULL;
5511 * Initialize or reset the forechannel and backchannel tables
5513 static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
5515 struct nfs4_slot_table *tbl;
5518 dprintk("--> %s\n", __func__);
5520 tbl = &ses->fc_slot_table;
5521 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5522 if (status) /* -ENOMEM */
5525 tbl = &ses->bc_slot_table;
5526 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5527 if (status && tbl->slots == NULL)
5528 /* Fore and back channel share a connection so get
5529 * both slot tables or neither */
5530 nfs4_destroy_slot_tables(ses);
5534 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5536 struct nfs4_session *session;
5537 struct nfs4_slot_table *tbl;
5539 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5543 tbl = &session->fc_slot_table;
5544 tbl->highest_used_slotid = NFS4_NO_SLOT;
5545 spin_lock_init(&tbl->slot_tbl_lock);
5546 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5547 init_completion(&tbl->complete);
5549 tbl = &session->bc_slot_table;
5550 tbl->highest_used_slotid = NFS4_NO_SLOT;
5551 spin_lock_init(&tbl->slot_tbl_lock);
5552 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5553 init_completion(&tbl->complete);
5555 session->session_state = 1<<NFS4_SESSION_INITING;
5561 void nfs4_destroy_session(struct nfs4_session *session)
5563 struct rpc_xprt *xprt;
5564 struct rpc_cred *cred;
5566 cred = nfs4_get_exchange_id_cred(session->clp);
5567 nfs4_proc_destroy_session(session, cred);
5572 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5574 dprintk("%s Destroy backchannel for xprt %p\n",
5576 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
5577 nfs4_destroy_slot_tables(session);
5582 * Initialize the values to be used by the client in CREATE_SESSION
5583 * If nfs4_init_session set the fore channel request and response sizes,
5586 * Set the back channel max_resp_sz_cached to zero to force the client to
5587 * always set csa_cachethis to FALSE because the current implementation
5588 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5590 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5592 struct nfs4_session *session = args->client->cl_session;
5593 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5594 mxresp_sz = session->fc_attrs.max_resp_sz;
5597 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5599 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5600 /* Fore channel attributes */
5601 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5602 args->fc_attrs.max_resp_sz = mxresp_sz;
5603 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5604 args->fc_attrs.max_reqs = max_session_slots;
5606 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5607 "max_ops=%u max_reqs=%u\n",
5609 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5610 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5612 /* Back channel attributes */
5613 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5614 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5615 args->bc_attrs.max_resp_sz_cached = 0;
5616 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5617 args->bc_attrs.max_reqs = 1;
5619 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5620 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5622 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5623 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5624 args->bc_attrs.max_reqs);
5627 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5629 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5630 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5632 if (rcvd->max_resp_sz > sent->max_resp_sz)
5635 * Our requested max_ops is the minimum we need; we're not
5636 * prepared to break up compounds into smaller pieces than that.
5637 * So, no point even trying to continue if the server won't
5640 if (rcvd->max_ops < sent->max_ops)
5642 if (rcvd->max_reqs == 0)
5644 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5645 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5649 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5651 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5652 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5654 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5656 if (rcvd->max_resp_sz < sent->max_resp_sz)
5658 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5660 /* These would render the backchannel useless: */
5661 if (rcvd->max_ops != sent->max_ops)
5663 if (rcvd->max_reqs != sent->max_reqs)
5668 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5669 struct nfs4_session *session)
5673 ret = nfs4_verify_fore_channel_attrs(args, session);
5676 return nfs4_verify_back_channel_attrs(args, session);
5679 static int _nfs4_proc_create_session(struct nfs_client *clp,
5680 struct rpc_cred *cred)
5682 struct nfs4_session *session = clp->cl_session;
5683 struct nfs41_create_session_args args = {
5685 .cb_program = NFS4_CALLBACK,
5687 struct nfs41_create_session_res res = {
5690 struct rpc_message msg = {
5691 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5698 nfs4_init_channel_attrs(&args);
5699 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5701 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5704 /* Verify the session's negotiated channel_attrs values */
5705 status = nfs4_verify_channel_attrs(&args, session);
5707 /* Increment the clientid slot sequence id */
5715 * Issues a CREATE_SESSION operation to the server.
5716 * It is the responsibility of the caller to verify the session is
5717 * expired before calling this routine.
5719 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
5723 struct nfs4_session *session = clp->cl_session;
5725 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5727 status = _nfs4_proc_create_session(clp, cred);
5731 /* Init or reset the session slot tables */
5732 status = nfs4_setup_session_slot_tables(session);
5733 dprintk("slot table setup returned %d\n", status);
5737 ptr = (unsigned *)&session->sess_id.data[0];
5738 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5739 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5741 dprintk("<-- %s\n", __func__);
5746 * Issue the over-the-wire RPC DESTROY_SESSION.
5747 * The caller must serialize access to this routine.
5749 int nfs4_proc_destroy_session(struct nfs4_session *session,
5750 struct rpc_cred *cred)
5752 struct rpc_message msg = {
5753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5754 .rpc_argp = session,
5759 dprintk("--> nfs4_proc_destroy_session\n");
5761 /* session is still being setup */
5762 if (session->clp->cl_cons_state != NFS_CS_READY)
5765 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5768 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
5769 "Session has been destroyed regardless...\n", status);
5771 dprintk("<-- nfs4_proc_destroy_session\n");
5776 * With sessions, the client is not marked ready until after a
5777 * successful EXCHANGE_ID and CREATE_SESSION.
5779 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5780 * other versions of NFS can be tried.
5782 static int nfs41_check_session_ready(struct nfs_client *clp)
5786 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5787 ret = nfs4_client_recover_expired_lease(clp);
5791 if (clp->cl_cons_state < NFS_CS_READY)
5792 return -EPROTONOSUPPORT;
5797 int nfs4_init_session(struct nfs_server *server)
5799 struct nfs_client *clp = server->nfs_client;
5800 struct nfs4_session *session;
5801 unsigned int rsize, wsize;
5803 if (!nfs4_has_session(clp))
5806 session = clp->cl_session;
5807 spin_lock(&clp->cl_lock);
5808 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5810 rsize = server->rsize;
5812 rsize = NFS_MAX_FILE_IO_SIZE;
5813 wsize = server->wsize;
5815 wsize = NFS_MAX_FILE_IO_SIZE;
5817 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5818 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5820 spin_unlock(&clp->cl_lock);
5822 return nfs41_check_session_ready(clp);
5825 int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
5827 struct nfs4_session *session = clp->cl_session;
5830 spin_lock(&clp->cl_lock);
5831 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5833 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
5834 * DS lease to be equal to the MDS lease.
5836 clp->cl_lease_time = lease_time;
5837 clp->cl_last_renewal = jiffies;
5839 spin_unlock(&clp->cl_lock);
5841 ret = nfs41_check_session_ready(clp);
5844 /* Test for the DS role */
5845 if (!is_ds_client(clp))
5849 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5853 * Renew the cl_session lease.
5855 struct nfs4_sequence_data {
5856 struct nfs_client *clp;
5857 struct nfs4_sequence_args args;
5858 struct nfs4_sequence_res res;
5861 static void nfs41_sequence_release(void *data)
5863 struct nfs4_sequence_data *calldata = data;
5864 struct nfs_client *clp = calldata->clp;
5866 if (atomic_read(&clp->cl_count) > 1)
5867 nfs4_schedule_state_renewal(clp);
5868 nfs_put_client(clp);
5872 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5874 switch(task->tk_status) {
5875 case -NFS4ERR_DELAY:
5876 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5879 nfs4_schedule_lease_recovery(clp);
5884 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5886 struct nfs4_sequence_data *calldata = data;
5887 struct nfs_client *clp = calldata->clp;
5889 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5892 if (task->tk_status < 0) {
5893 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5894 if (atomic_read(&clp->cl_count) == 1)
5897 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5898 rpc_restart_call_prepare(task);
5902 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5904 dprintk("<-- %s\n", __func__);
5907 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5909 struct nfs4_sequence_data *calldata = data;
5910 struct nfs_client *clp = calldata->clp;
5911 struct nfs4_sequence_args *args;
5912 struct nfs4_sequence_res *res;
5914 args = task->tk_msg.rpc_argp;
5915 res = task->tk_msg.rpc_resp;
5917 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
5919 rpc_call_start(task);
5922 static const struct rpc_call_ops nfs41_sequence_ops = {
5923 .rpc_call_done = nfs41_sequence_call_done,
5924 .rpc_call_prepare = nfs41_sequence_prepare,
5925 .rpc_release = nfs41_sequence_release,
5928 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5930 struct nfs4_sequence_data *calldata;
5931 struct rpc_message msg = {
5932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5935 struct rpc_task_setup task_setup_data = {
5936 .rpc_client = clp->cl_rpcclient,
5937 .rpc_message = &msg,
5938 .callback_ops = &nfs41_sequence_ops,
5939 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5942 if (!atomic_inc_not_zero(&clp->cl_count))
5943 return ERR_PTR(-EIO);
5944 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5945 if (calldata == NULL) {
5946 nfs_put_client(clp);
5947 return ERR_PTR(-ENOMEM);
5949 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
5950 msg.rpc_argp = &calldata->args;
5951 msg.rpc_resp = &calldata->res;
5952 calldata->clp = clp;
5953 task_setup_data.callback_data = calldata;
5955 return rpc_run_task(&task_setup_data);
5958 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5960 struct rpc_task *task;
5963 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5965 task = _nfs41_proc_sequence(clp, cred);
5967 ret = PTR_ERR(task);
5969 rpc_put_task_async(task);
5970 dprintk("<-- %s status=%d\n", __func__, ret);
5974 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5976 struct rpc_task *task;
5979 task = _nfs41_proc_sequence(clp, cred);
5981 ret = PTR_ERR(task);
5984 ret = rpc_wait_for_completion_task(task);
5986 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5988 if (task->tk_status == 0)
5989 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5990 ret = task->tk_status;
5994 dprintk("<-- %s status=%d\n", __func__, ret);
5998 struct nfs4_reclaim_complete_data {
5999 struct nfs_client *clp;
6000 struct nfs41_reclaim_complete_args arg;
6001 struct nfs41_reclaim_complete_res res;
6004 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6006 struct nfs4_reclaim_complete_data *calldata = data;
6008 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6009 if (nfs41_setup_sequence(calldata->clp->cl_session,
6010 &calldata->arg.seq_args,
6011 &calldata->res.seq_res, task))
6014 rpc_call_start(task);
6017 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6019 switch(task->tk_status) {
6021 case -NFS4ERR_COMPLETE_ALREADY:
6022 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6024 case -NFS4ERR_DELAY:
6025 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6027 case -NFS4ERR_RETRY_UNCACHED_REP:
6030 nfs4_schedule_lease_recovery(clp);
6035 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6037 struct nfs4_reclaim_complete_data *calldata = data;
6038 struct nfs_client *clp = calldata->clp;
6039 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6041 dprintk("--> %s\n", __func__);
6042 if (!nfs41_sequence_done(task, res))
6045 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6046 rpc_restart_call_prepare(task);
6049 dprintk("<-- %s\n", __func__);
6052 static void nfs4_free_reclaim_complete_data(void *data)
6054 struct nfs4_reclaim_complete_data *calldata = data;
6059 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6060 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6061 .rpc_call_done = nfs4_reclaim_complete_done,
6062 .rpc_release = nfs4_free_reclaim_complete_data,
6066 * Issue a global reclaim complete.
6068 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6070 struct nfs4_reclaim_complete_data *calldata;
6071 struct rpc_task *task;
6072 struct rpc_message msg = {
6073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6075 struct rpc_task_setup task_setup_data = {
6076 .rpc_client = clp->cl_rpcclient,
6077 .rpc_message = &msg,
6078 .callback_ops = &nfs4_reclaim_complete_call_ops,
6079 .flags = RPC_TASK_ASYNC,
6081 int status = -ENOMEM;
6083 dprintk("--> %s\n", __func__);
6084 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6085 if (calldata == NULL)
6087 calldata->clp = clp;
6088 calldata->arg.one_fs = 0;
6090 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
6091 msg.rpc_argp = &calldata->arg;
6092 msg.rpc_resp = &calldata->res;
6093 task_setup_data.callback_data = calldata;
6094 task = rpc_run_task(&task_setup_data);
6096 status = PTR_ERR(task);
6099 status = nfs4_wait_for_completion_rpc_task(task);
6101 status = task->tk_status;
6105 dprintk("<-- %s status=%d\n", __func__, status);
6110 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6112 struct nfs4_layoutget *lgp = calldata;
6113 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6115 dprintk("--> %s\n", __func__);
6116 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6117 * right now covering the LAYOUTGET we are about to send.
6118 * However, that is not so catastrophic, and there seems
6119 * to be no way to prevent it completely.
6121 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
6122 &lgp->res.seq_res, task))
6124 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6125 NFS_I(lgp->args.inode)->layout,
6126 lgp->args.ctx->state)) {
6127 rpc_exit(task, NFS4_OK);
6130 rpc_call_start(task);
6133 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6135 struct nfs4_layoutget *lgp = calldata;
6136 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6138 dprintk("--> %s\n", __func__);
6140 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6143 switch (task->tk_status) {
6146 case -NFS4ERR_LAYOUTTRYLATER:
6147 case -NFS4ERR_RECALLCONFLICT:
6148 task->tk_status = -NFS4ERR_DELAY;
6151 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6152 rpc_restart_call_prepare(task);
6156 dprintk("<-- %s\n", __func__);
6159 static void nfs4_layoutget_release(void *calldata)
6161 struct nfs4_layoutget *lgp = calldata;
6163 dprintk("--> %s\n", __func__);
6164 put_nfs_open_context(lgp->args.ctx);
6166 dprintk("<-- %s\n", __func__);
6169 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6170 .rpc_call_prepare = nfs4_layoutget_prepare,
6171 .rpc_call_done = nfs4_layoutget_done,
6172 .rpc_release = nfs4_layoutget_release,
6175 int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
6177 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6178 struct rpc_task *task;
6179 struct rpc_message msg = {
6180 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6181 .rpc_argp = &lgp->args,
6182 .rpc_resp = &lgp->res,
6184 struct rpc_task_setup task_setup_data = {
6185 .rpc_client = server->client,
6186 .rpc_message = &msg,
6187 .callback_ops = &nfs4_layoutget_call_ops,
6188 .callback_data = lgp,
6189 .flags = RPC_TASK_ASYNC,
6193 dprintk("--> %s\n", __func__);
6195 lgp->res.layoutp = &lgp->args.layout;
6196 lgp->res.seq_res.sr_slot = NULL;
6197 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
6198 task = rpc_run_task(&task_setup_data);
6200 return PTR_ERR(task);
6201 status = nfs4_wait_for_completion_rpc_task(task);
6203 status = task->tk_status;
6205 status = pnfs_layout_process(lgp);
6207 dprintk("<-- %s status=%d\n", __func__, status);
6212 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6214 struct nfs4_layoutreturn *lrp = calldata;
6216 dprintk("--> %s\n", __func__);
6217 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
6218 &lrp->res.seq_res, task))
6220 rpc_call_start(task);
6223 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6225 struct nfs4_layoutreturn *lrp = calldata;
6226 struct nfs_server *server;
6227 struct pnfs_layout_hdr *lo = lrp->args.layout;
6229 dprintk("--> %s\n", __func__);
6231 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6234 server = NFS_SERVER(lrp->args.inode);
6235 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6236 rpc_restart_call_prepare(task);
6239 spin_lock(&lo->plh_inode->i_lock);
6240 if (task->tk_status == 0) {
6241 if (lrp->res.lrs_present) {
6242 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6244 BUG_ON(!list_empty(&lo->plh_segs));
6246 lo->plh_block_lgets--;
6247 spin_unlock(&lo->plh_inode->i_lock);
6248 dprintk("<-- %s\n", __func__);
6251 static void nfs4_layoutreturn_release(void *calldata)
6253 struct nfs4_layoutreturn *lrp = calldata;
6255 dprintk("--> %s\n", __func__);
6256 put_layout_hdr(lrp->args.layout);
6258 dprintk("<-- %s\n", __func__);
6261 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6262 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6263 .rpc_call_done = nfs4_layoutreturn_done,
6264 .rpc_release = nfs4_layoutreturn_release,
6267 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6269 struct rpc_task *task;
6270 struct rpc_message msg = {
6271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6272 .rpc_argp = &lrp->args,
6273 .rpc_resp = &lrp->res,
6275 struct rpc_task_setup task_setup_data = {
6276 .rpc_client = lrp->clp->cl_rpcclient,
6277 .rpc_message = &msg,
6278 .callback_ops = &nfs4_layoutreturn_call_ops,
6279 .callback_data = lrp,
6283 dprintk("--> %s\n", __func__);
6284 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
6285 task = rpc_run_task(&task_setup_data);
6287 return PTR_ERR(task);
6288 status = task->tk_status;
6289 dprintk("<-- %s status=%d\n", __func__, status);
6295 * Retrieve the list of Data Server devices from the MDS.
6297 static int _nfs4_getdevicelist(struct nfs_server *server,
6298 const struct nfs_fh *fh,
6299 struct pnfs_devicelist *devlist)
6301 struct nfs4_getdevicelist_args args = {
6303 .layoutclass = server->pnfs_curr_ld->id,
6305 struct nfs4_getdevicelist_res res = {
6308 struct rpc_message msg = {
6309 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6315 dprintk("--> %s\n", __func__);
6316 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6318 dprintk("<-- %s status=%d\n", __func__, status);
6322 int nfs4_proc_getdevicelist(struct nfs_server *server,
6323 const struct nfs_fh *fh,
6324 struct pnfs_devicelist *devlist)
6326 struct nfs4_exception exception = { };
6330 err = nfs4_handle_exception(server,
6331 _nfs4_getdevicelist(server, fh, devlist),
6333 } while (exception.retry);
6335 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6336 err, devlist->num_devs);
6340 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6343 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6345 struct nfs4_getdeviceinfo_args args = {
6348 struct nfs4_getdeviceinfo_res res = {
6351 struct rpc_message msg = {
6352 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6358 dprintk("--> %s\n", __func__);
6359 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6360 dprintk("<-- %s status=%d\n", __func__, status);
6365 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6367 struct nfs4_exception exception = { };
6371 err = nfs4_handle_exception(server,
6372 _nfs4_proc_getdeviceinfo(server, pdev),
6374 } while (exception.retry);
6377 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6379 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6381 struct nfs4_layoutcommit_data *data = calldata;
6382 struct nfs_server *server = NFS_SERVER(data->args.inode);
6384 if (nfs4_setup_sequence(server, &data->args.seq_args,
6385 &data->res.seq_res, task))
6387 rpc_call_start(task);
6391 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6393 struct nfs4_layoutcommit_data *data = calldata;
6394 struct nfs_server *server = NFS_SERVER(data->args.inode);
6396 if (!nfs4_sequence_done(task, &data->res.seq_res))
6399 switch (task->tk_status) { /* Just ignore these failures */
6400 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6401 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6402 case -NFS4ERR_BADLAYOUT: /* no layout */
6403 case -NFS4ERR_GRACE: /* loca_recalim always false */
6404 task->tk_status = 0;
6407 nfs_post_op_update_inode_force_wcc(data->args.inode,
6411 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6412 rpc_restart_call_prepare(task);
6418 static void nfs4_layoutcommit_release(void *calldata)
6420 struct nfs4_layoutcommit_data *data = calldata;
6421 struct pnfs_layout_segment *lseg, *tmp;
6422 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
6424 pnfs_cleanup_layoutcommit(data);
6425 /* Matched by references in pnfs_set_layoutcommit */
6426 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6427 list_del_init(&lseg->pls_lc_list);
6428 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6433 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6434 smp_mb__after_clear_bit();
6435 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6437 put_rpccred(data->cred);
6441 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6442 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6443 .rpc_call_done = nfs4_layoutcommit_done,
6444 .rpc_release = nfs4_layoutcommit_release,
6448 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6450 struct rpc_message msg = {
6451 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6452 .rpc_argp = &data->args,
6453 .rpc_resp = &data->res,
6454 .rpc_cred = data->cred,
6456 struct rpc_task_setup task_setup_data = {
6457 .task = &data->task,
6458 .rpc_client = NFS_CLIENT(data->args.inode),
6459 .rpc_message = &msg,
6460 .callback_ops = &nfs4_layoutcommit_ops,
6461 .callback_data = data,
6462 .flags = RPC_TASK_ASYNC,
6464 struct rpc_task *task;
6467 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6468 "lbw: %llu inode %lu\n",
6469 data->task.tk_pid, sync,
6470 data->args.lastbytewritten,
6471 data->args.inode->i_ino);
6473 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6474 task = rpc_run_task(&task_setup_data);
6476 return PTR_ERR(task);
6479 status = nfs4_wait_for_completion_rpc_task(task);
6482 status = task->tk_status;
6484 dprintk("%s: status %d\n", __func__, status);
6490 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6491 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6493 struct nfs41_secinfo_no_name_args args = {
6494 .style = SECINFO_STYLE_CURRENT_FH,
6496 struct nfs4_secinfo_res res = {
6499 struct rpc_message msg = {
6500 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6504 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6508 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6509 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6511 struct nfs4_exception exception = { };
6514 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6517 case -NFS4ERR_WRONGSEC:
6518 case -NFS4ERR_NOTSUPP:
6521 err = nfs4_handle_exception(server, err, &exception);
6523 } while (exception.retry);
6529 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6530 struct nfs_fsinfo *info)
6534 rpc_authflavor_t flavor;
6535 struct nfs4_secinfo_flavors *flavors;
6537 page = alloc_page(GFP_KERNEL);
6543 flavors = page_address(page);
6544 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6547 * Fall back on "guess and check" method if
6548 * the server doesn't support SECINFO_NO_NAME
6550 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6551 err = nfs4_find_root_sec(server, fhandle, info);
6557 flavor = nfs_find_best_sec(flavors);
6559 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6569 static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6572 struct nfs41_test_stateid_args args = {
6575 struct nfs41_test_stateid_res res;
6576 struct rpc_message msg = {
6577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6582 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6583 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6584 if (status != NFS_OK)
6589 static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6591 struct nfs4_exception exception = { };
6594 err = _nfs41_test_stateid(server, stateid);
6595 if (err != -NFS4ERR_DELAY)
6597 nfs4_handle_exception(server, err, &exception);
6598 } while (exception.retry);
6602 static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6604 struct nfs41_free_stateid_args args = {
6607 struct nfs41_free_stateid_res res;
6608 struct rpc_message msg = {
6609 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6614 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6615 return nfs4_call_sync_sequence(server->client, server, &msg,
6616 &args.seq_args, &res.seq_res, 1);
6619 static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6621 struct nfs4_exception exception = { };
6624 err = _nfs4_free_stateid(server, stateid);
6625 if (err != -NFS4ERR_DELAY)
6627 nfs4_handle_exception(server, err, &exception);
6628 } while (exception.retry);
6632 static bool nfs41_match_stateid(const nfs4_stateid *s1,
6633 const nfs4_stateid *s2)
6635 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
6638 if (s1->seqid == s2->seqid)
6640 if (s1->seqid == 0 || s2->seqid == 0)
6646 #endif /* CONFIG_NFS_V4_1 */
6648 static bool nfs4_match_stateid(const nfs4_stateid *s1,
6649 const nfs4_stateid *s2)
6651 return nfs4_stateid_match(s1, s2);
6655 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6656 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6657 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6658 .recover_open = nfs4_open_reclaim,
6659 .recover_lock = nfs4_lock_reclaim,
6660 .establish_clid = nfs4_init_clientid,
6661 .get_clid_cred = nfs4_get_setclientid_cred,
6664 #if defined(CONFIG_NFS_V4_1)
6665 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6666 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6667 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6668 .recover_open = nfs4_open_reclaim,
6669 .recover_lock = nfs4_lock_reclaim,
6670 .establish_clid = nfs41_init_clientid,
6671 .get_clid_cred = nfs4_get_exchange_id_cred,
6672 .reclaim_complete = nfs41_proc_reclaim_complete,
6674 #endif /* CONFIG_NFS_V4_1 */
6676 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6677 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6678 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6679 .recover_open = nfs4_open_expired,
6680 .recover_lock = nfs4_lock_expired,
6681 .establish_clid = nfs4_init_clientid,
6682 .get_clid_cred = nfs4_get_setclientid_cred,
6685 #if defined(CONFIG_NFS_V4_1)
6686 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6687 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6688 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6689 .recover_open = nfs41_open_expired,
6690 .recover_lock = nfs41_lock_expired,
6691 .establish_clid = nfs41_init_clientid,
6692 .get_clid_cred = nfs4_get_exchange_id_cred,
6694 #endif /* CONFIG_NFS_V4_1 */
6696 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6697 .sched_state_renewal = nfs4_proc_async_renew,
6698 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6699 .renew_lease = nfs4_proc_renew,
6702 #if defined(CONFIG_NFS_V4_1)
6703 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6704 .sched_state_renewal = nfs41_proc_async_sequence,
6705 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
6706 .renew_lease = nfs4_proc_sequence,
6710 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6712 .call_sync = _nfs4_call_sync,
6713 .match_stateid = nfs4_match_stateid,
6714 .find_root_sec = nfs4_find_root_sec,
6715 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6716 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6717 .state_renewal_ops = &nfs40_state_renewal_ops,
6720 #if defined(CONFIG_NFS_V4_1)
6721 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6723 .call_sync = _nfs4_call_sync_session,
6724 .match_stateid = nfs41_match_stateid,
6725 .find_root_sec = nfs41_find_root_sec,
6726 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6727 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6728 .state_renewal_ops = &nfs41_state_renewal_ops,
6732 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6733 [0] = &nfs_v4_0_minor_ops,
6734 #if defined(CONFIG_NFS_V4_1)
6735 [1] = &nfs_v4_1_minor_ops,
6739 static const struct inode_operations nfs4_file_inode_operations = {
6740 .permission = nfs_permission,
6741 .getattr = nfs_getattr,
6742 .setattr = nfs_setattr,
6743 .getxattr = generic_getxattr,
6744 .setxattr = generic_setxattr,
6745 .listxattr = generic_listxattr,
6746 .removexattr = generic_removexattr,
6749 const struct nfs_rpc_ops nfs_v4_clientops = {
6750 .version = 4, /* protocol version */
6751 .dentry_ops = &nfs4_dentry_operations,
6752 .dir_inode_ops = &nfs4_dir_inode_operations,
6753 .file_inode_ops = &nfs4_file_inode_operations,
6754 .file_ops = &nfs4_file_operations,
6755 .getroot = nfs4_proc_get_root,
6756 .submount = nfs4_submount,
6757 .getattr = nfs4_proc_getattr,
6758 .setattr = nfs4_proc_setattr,
6759 .lookup = nfs4_proc_lookup,
6760 .access = nfs4_proc_access,
6761 .readlink = nfs4_proc_readlink,
6762 .create = nfs4_proc_create,
6763 .remove = nfs4_proc_remove,
6764 .unlink_setup = nfs4_proc_unlink_setup,
6765 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
6766 .unlink_done = nfs4_proc_unlink_done,
6767 .rename = nfs4_proc_rename,
6768 .rename_setup = nfs4_proc_rename_setup,
6769 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
6770 .rename_done = nfs4_proc_rename_done,
6771 .link = nfs4_proc_link,
6772 .symlink = nfs4_proc_symlink,
6773 .mkdir = nfs4_proc_mkdir,
6774 .rmdir = nfs4_proc_remove,
6775 .readdir = nfs4_proc_readdir,
6776 .mknod = nfs4_proc_mknod,
6777 .statfs = nfs4_proc_statfs,
6778 .fsinfo = nfs4_proc_fsinfo,
6779 .pathconf = nfs4_proc_pathconf,
6780 .set_capabilities = nfs4_server_capabilities,
6781 .decode_dirent = nfs4_decode_dirent,
6782 .read_setup = nfs4_proc_read_setup,
6783 .read_pageio_init = pnfs_pageio_init_read,
6784 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
6785 .read_done = nfs4_read_done,
6786 .write_setup = nfs4_proc_write_setup,
6787 .write_pageio_init = pnfs_pageio_init_write,
6788 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
6789 .write_done = nfs4_write_done,
6790 .commit_setup = nfs4_proc_commit_setup,
6791 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
6792 .commit_done = nfs4_commit_done,
6793 .lock = nfs4_proc_lock,
6794 .clear_acl_cache = nfs4_zap_acl_attr,
6795 .close_context = nfs4_close_context,
6796 .open_context = nfs4_atomic_open,
6797 .have_delegation = nfs4_have_delegation,
6798 .return_delegation = nfs4_inode_return_delegation,
6799 .alloc_client = nfs4_alloc_client,
6800 .init_client = nfs4_init_client,
6801 .free_client = nfs4_free_client,
6804 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6805 .prefix = XATTR_NAME_NFSV4_ACL,
6806 .list = nfs4_xattr_list_nfs4_acl,
6807 .get = nfs4_xattr_get_nfs4_acl,
6808 .set = nfs4_xattr_set_nfs4_acl,
6811 const struct xattr_handler *nfs4_xattr_handlers[] = {
6812 &nfs4_xattr_nfs4_acl_handler,
6816 module_param(max_session_slots, ushort, 0644);
6817 MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6818 "requests the client will negotiate");