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 && nfs_have_delegation(inode, FMODE_READ)) {
298 nfs_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 nfs_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 nfs41_free_stateid(server, stateid);
1768 state->flags &= ~flags;
1774 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1776 int deleg_status, open_status;
1777 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1778 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1780 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1781 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1783 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1785 return nfs4_open_expired(sp, state);
1790 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1791 * fields corresponding to attributes that were used to store the verifier.
1792 * Make sure we clobber those fields in the later setattr call
1794 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1796 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1797 !(sattr->ia_valid & ATTR_ATIME_SET))
1798 sattr->ia_valid |= ATTR_ATIME;
1800 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1801 !(sattr->ia_valid & ATTR_MTIME_SET))
1802 sattr->ia_valid |= ATTR_MTIME;
1806 * Returns a referenced nfs4_state
1808 static int _nfs4_do_open(struct inode *dir,
1809 struct dentry *dentry,
1812 struct iattr *sattr,
1813 struct rpc_cred *cred,
1814 struct nfs4_state **res,
1815 struct nfs4_threshold **ctx_th)
1817 struct nfs4_state_owner *sp;
1818 struct nfs4_state *state = NULL;
1819 struct nfs_server *server = NFS_SERVER(dir);
1820 struct nfs4_opendata *opendata;
1823 /* Protect against reboot recovery conflicts */
1825 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1827 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1830 status = nfs4_recover_expired_lease(server);
1832 goto err_put_state_owner;
1833 if (dentry->d_inode != NULL)
1834 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1836 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1837 if (opendata == NULL)
1838 goto err_put_state_owner;
1840 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1841 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1842 if (!opendata->f_attr.mdsthreshold)
1843 goto err_opendata_put;
1844 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
1846 if (dentry->d_inode != NULL)
1847 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1849 status = _nfs4_proc_open(opendata);
1851 goto err_opendata_put;
1853 state = nfs4_opendata_to_nfs4_state(opendata);
1854 status = PTR_ERR(state);
1856 goto err_opendata_put;
1857 if (server->caps & NFS_CAP_POSIX_LOCK)
1858 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1860 if (opendata->o_arg.open_flags & O_EXCL) {
1861 nfs4_exclusive_attrset(opendata, sattr);
1863 nfs_fattr_init(opendata->o_res.f_attr);
1864 status = nfs4_do_setattr(state->inode, cred,
1865 opendata->o_res.f_attr, sattr,
1868 nfs_setattr_update_inode(state->inode, sattr);
1869 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1872 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1873 *ctx_th = opendata->f_attr.mdsthreshold;
1875 kfree(opendata->f_attr.mdsthreshold);
1876 opendata->f_attr.mdsthreshold = NULL;
1878 nfs4_opendata_put(opendata);
1879 nfs4_put_state_owner(sp);
1883 kfree(opendata->f_attr.mdsthreshold);
1884 nfs4_opendata_put(opendata);
1885 err_put_state_owner:
1886 nfs4_put_state_owner(sp);
1893 static struct nfs4_state *nfs4_do_open(struct inode *dir,
1894 struct dentry *dentry,
1897 struct iattr *sattr,
1898 struct rpc_cred *cred,
1899 struct nfs4_threshold **ctx_th)
1901 struct nfs4_exception exception = { };
1902 struct nfs4_state *res;
1905 fmode &= FMODE_READ|FMODE_WRITE;
1907 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1911 /* NOTE: BAD_SEQID means the server and client disagree about the
1912 * book-keeping w.r.t. state-changing operations
1913 * (OPEN/CLOSE/LOCK/LOCKU...)
1914 * It is actually a sign of a bug on the client or on the server.
1916 * If we receive a BAD_SEQID error in the particular case of
1917 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1918 * have unhashed the old state_owner for us, and that we can
1919 * therefore safely retry using a new one. We should still warn
1920 * the user though...
1922 if (status == -NFS4ERR_BAD_SEQID) {
1923 pr_warn_ratelimited("NFS: v4 server %s "
1924 " returned a bad sequence-id error!\n",
1925 NFS_SERVER(dir)->nfs_client->cl_hostname);
1926 exception.retry = 1;
1930 * BAD_STATEID on OPEN means that the server cancelled our
1931 * state before it received the OPEN_CONFIRM.
1932 * Recover by retrying the request as per the discussion
1933 * on Page 181 of RFC3530.
1935 if (status == -NFS4ERR_BAD_STATEID) {
1936 exception.retry = 1;
1939 if (status == -EAGAIN) {
1940 /* We must have found a delegation */
1941 exception.retry = 1;
1944 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1945 status, &exception));
1946 } while (exception.retry);
1950 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1951 struct nfs_fattr *fattr, struct iattr *sattr,
1952 struct nfs4_state *state)
1954 struct nfs_server *server = NFS_SERVER(inode);
1955 struct nfs_setattrargs arg = {
1956 .fh = NFS_FH(inode),
1959 .bitmask = server->attr_bitmask,
1961 struct nfs_setattrres res = {
1965 struct rpc_message msg = {
1966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1971 unsigned long timestamp = jiffies;
1974 nfs_fattr_init(fattr);
1976 if (state != NULL) {
1977 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1978 current->files, current->tgid);
1979 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1981 /* Use that stateid */
1983 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
1985 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1986 if (status == 0 && state != NULL)
1987 renew_lease(server, timestamp);
1991 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1992 struct nfs_fattr *fattr, struct iattr *sattr,
1993 struct nfs4_state *state)
1995 struct nfs_server *server = NFS_SERVER(inode);
1996 struct nfs4_exception exception = {
2002 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2004 case -NFS4ERR_OPENMODE:
2005 if (state && !(state->state & FMODE_WRITE)) {
2007 if (sattr->ia_valid & ATTR_OPEN)
2012 err = nfs4_handle_exception(server, err, &exception);
2013 } while (exception.retry);
2018 struct nfs4_closedata {
2019 struct inode *inode;
2020 struct nfs4_state *state;
2021 struct nfs_closeargs arg;
2022 struct nfs_closeres res;
2023 struct nfs_fattr fattr;
2024 unsigned long timestamp;
2029 static void nfs4_free_closedata(void *data)
2031 struct nfs4_closedata *calldata = data;
2032 struct nfs4_state_owner *sp = calldata->state->owner;
2033 struct super_block *sb = calldata->state->inode->i_sb;
2036 pnfs_roc_release(calldata->state->inode);
2037 nfs4_put_open_state(calldata->state);
2038 nfs_free_seqid(calldata->arg.seqid);
2039 nfs4_put_state_owner(sp);
2040 nfs_sb_deactive(sb);
2044 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2047 spin_lock(&state->owner->so_lock);
2048 if (!(fmode & FMODE_READ))
2049 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2050 if (!(fmode & FMODE_WRITE))
2051 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2052 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2053 spin_unlock(&state->owner->so_lock);
2056 static void nfs4_close_done(struct rpc_task *task, void *data)
2058 struct nfs4_closedata *calldata = data;
2059 struct nfs4_state *state = calldata->state;
2060 struct nfs_server *server = NFS_SERVER(calldata->inode);
2062 dprintk("%s: begin!\n", __func__);
2063 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2065 /* hmm. we are done with the inode, and in the process of freeing
2066 * the state_owner. we keep this around to process errors
2068 switch (task->tk_status) {
2071 pnfs_roc_set_barrier(state->inode,
2072 calldata->roc_barrier);
2073 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2074 renew_lease(server, calldata->timestamp);
2075 nfs4_close_clear_stateid_flags(state,
2076 calldata->arg.fmode);
2078 case -NFS4ERR_STALE_STATEID:
2079 case -NFS4ERR_OLD_STATEID:
2080 case -NFS4ERR_BAD_STATEID:
2081 case -NFS4ERR_EXPIRED:
2082 if (calldata->arg.fmode == 0)
2085 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2086 rpc_restart_call_prepare(task);
2088 nfs_release_seqid(calldata->arg.seqid);
2089 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2090 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2093 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2095 struct nfs4_closedata *calldata = data;
2096 struct nfs4_state *state = calldata->state;
2099 dprintk("%s: begin!\n", __func__);
2100 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2103 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2104 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2105 spin_lock(&state->owner->so_lock);
2106 /* Calculate the change in open mode */
2107 if (state->n_rdwr == 0) {
2108 if (state->n_rdonly == 0) {
2109 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2110 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2111 calldata->arg.fmode &= ~FMODE_READ;
2113 if (state->n_wronly == 0) {
2114 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2115 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2116 calldata->arg.fmode &= ~FMODE_WRITE;
2119 spin_unlock(&state->owner->so_lock);
2122 /* Note: exit _without_ calling nfs4_close_done */
2123 task->tk_action = NULL;
2127 if (calldata->arg.fmode == 0) {
2128 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2129 if (calldata->roc &&
2130 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2131 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2137 nfs_fattr_init(calldata->res.fattr);
2138 calldata->timestamp = jiffies;
2139 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
2140 &calldata->arg.seq_args,
2141 &calldata->res.seq_res,
2144 rpc_call_start(task);
2146 dprintk("%s: done!\n", __func__);
2149 static const struct rpc_call_ops nfs4_close_ops = {
2150 .rpc_call_prepare = nfs4_close_prepare,
2151 .rpc_call_done = nfs4_close_done,
2152 .rpc_release = nfs4_free_closedata,
2156 * It is possible for data to be read/written from a mem-mapped file
2157 * after the sys_close call (which hits the vfs layer as a flush).
2158 * This means that we can't safely call nfsv4 close on a file until
2159 * the inode is cleared. This in turn means that we are not good
2160 * NFSv4 citizens - we do not indicate to the server to update the file's
2161 * share state even when we are done with one of the three share
2162 * stateid's in the inode.
2164 * NOTE: Caller must be holding the sp->so_owner semaphore!
2166 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
2168 struct nfs_server *server = NFS_SERVER(state->inode);
2169 struct nfs4_closedata *calldata;
2170 struct nfs4_state_owner *sp = state->owner;
2171 struct rpc_task *task;
2172 struct rpc_message msg = {
2173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2174 .rpc_cred = state->owner->so_cred,
2176 struct rpc_task_setup task_setup_data = {
2177 .rpc_client = server->client,
2178 .rpc_message = &msg,
2179 .callback_ops = &nfs4_close_ops,
2180 .workqueue = nfsiod_workqueue,
2181 .flags = RPC_TASK_ASYNC,
2183 int status = -ENOMEM;
2185 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2186 if (calldata == NULL)
2188 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2189 calldata->inode = state->inode;
2190 calldata->state = state;
2191 calldata->arg.fh = NFS_FH(state->inode);
2192 calldata->arg.stateid = &state->open_stateid;
2193 /* Serialization for the sequence id */
2194 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2195 if (calldata->arg.seqid == NULL)
2196 goto out_free_calldata;
2197 calldata->arg.fmode = 0;
2198 calldata->arg.bitmask = server->cache_consistency_bitmask;
2199 calldata->res.fattr = &calldata->fattr;
2200 calldata->res.seqid = calldata->arg.seqid;
2201 calldata->res.server = server;
2202 calldata->roc = roc;
2203 nfs_sb_active(calldata->inode->i_sb);
2205 msg.rpc_argp = &calldata->arg;
2206 msg.rpc_resp = &calldata->res;
2207 task_setup_data.callback_data = calldata;
2208 task = rpc_run_task(&task_setup_data);
2210 return PTR_ERR(task);
2213 status = rpc_wait_for_completion_task(task);
2220 pnfs_roc_release(state->inode);
2221 nfs4_put_open_state(state);
2222 nfs4_put_state_owner(sp);
2226 static struct inode *
2227 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2229 struct nfs4_state *state;
2231 /* Protect against concurrent sillydeletes */
2232 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2233 ctx->cred, &ctx->mdsthreshold);
2235 return ERR_CAST(state);
2237 return igrab(state->inode);
2240 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2242 if (ctx->state == NULL)
2245 nfs4_close_sync(ctx->state, ctx->mode);
2247 nfs4_close_state(ctx->state, ctx->mode);
2250 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2252 struct nfs4_server_caps_arg args = {
2255 struct nfs4_server_caps_res res = {};
2256 struct rpc_message msg = {
2257 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2263 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2265 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2266 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2267 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2268 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2269 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2270 NFS_CAP_CTIME|NFS_CAP_MTIME);
2271 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2272 server->caps |= NFS_CAP_ACLS;
2273 if (res.has_links != 0)
2274 server->caps |= NFS_CAP_HARDLINKS;
2275 if (res.has_symlinks != 0)
2276 server->caps |= NFS_CAP_SYMLINKS;
2277 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2278 server->caps |= NFS_CAP_FILEID;
2279 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2280 server->caps |= NFS_CAP_MODE;
2281 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2282 server->caps |= NFS_CAP_NLINK;
2283 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2284 server->caps |= NFS_CAP_OWNER;
2285 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2286 server->caps |= NFS_CAP_OWNER_GROUP;
2287 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2288 server->caps |= NFS_CAP_ATIME;
2289 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2290 server->caps |= NFS_CAP_CTIME;
2291 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2292 server->caps |= NFS_CAP_MTIME;
2294 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2295 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2296 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2297 server->acl_bitmask = res.acl_bitmask;
2298 server->fh_expire_type = res.fh_expire_type;
2304 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2306 struct nfs4_exception exception = { };
2309 err = nfs4_handle_exception(server,
2310 _nfs4_server_capabilities(server, fhandle),
2312 } while (exception.retry);
2316 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2317 struct nfs_fsinfo *info)
2319 struct nfs4_lookup_root_arg args = {
2320 .bitmask = nfs4_fattr_bitmap,
2322 struct nfs4_lookup_res res = {
2324 .fattr = info->fattr,
2327 struct rpc_message msg = {
2328 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2333 nfs_fattr_init(info->fattr);
2334 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2337 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2338 struct nfs_fsinfo *info)
2340 struct nfs4_exception exception = { };
2343 err = _nfs4_lookup_root(server, fhandle, info);
2346 case -NFS4ERR_WRONGSEC:
2349 err = nfs4_handle_exception(server, err, &exception);
2351 } while (exception.retry);
2356 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2357 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2359 struct rpc_auth *auth;
2362 auth = rpcauth_create(flavor, server->client);
2367 ret = nfs4_lookup_root(server, fhandle, info);
2372 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2373 struct nfs_fsinfo *info)
2375 int i, len, status = 0;
2376 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2378 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2379 flav_array[len] = RPC_AUTH_NULL;
2382 for (i = 0; i < len; i++) {
2383 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2384 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2389 * -EACCESS could mean that the user doesn't have correct permissions
2390 * to access the mount. It could also mean that we tried to mount
2391 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2392 * existing mount programs don't handle -EACCES very well so it should
2393 * be mapped to -EPERM instead.
2395 if (status == -EACCES)
2401 * get the file handle for the "/" directory on the server
2403 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2404 struct nfs_fsinfo *info)
2406 int minor_version = server->nfs_client->cl_minorversion;
2407 int status = nfs4_lookup_root(server, fhandle, info);
2408 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2410 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2411 * by nfs4_map_errors() as this function exits.
2413 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2415 status = nfs4_server_capabilities(server, fhandle);
2417 status = nfs4_do_fsinfo(server, fhandle, info);
2418 return nfs4_map_errors(status);
2421 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2422 struct nfs_fsinfo *info)
2425 struct nfs_fattr *fattr = info->fattr;
2427 error = nfs4_server_capabilities(server, mntfh);
2429 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2433 error = nfs4_proc_getattr(server, mntfh, fattr);
2435 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2439 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2440 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2441 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2447 * Get locations and (maybe) other attributes of a referral.
2448 * Note that we'll actually follow the referral later when
2449 * we detect fsid mismatch in inode revalidation
2451 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2452 const struct qstr *name, struct nfs_fattr *fattr,
2453 struct nfs_fh *fhandle)
2455 int status = -ENOMEM;
2456 struct page *page = NULL;
2457 struct nfs4_fs_locations *locations = NULL;
2459 page = alloc_page(GFP_KERNEL);
2462 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2463 if (locations == NULL)
2466 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2469 /* Make sure server returned a different fsid for the referral */
2470 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2471 dprintk("%s: server did not return a different fsid for"
2472 " a referral at %s\n", __func__, name->name);
2476 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2477 nfs_fixup_referral_attributes(&locations->fattr);
2479 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2480 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2481 memset(fhandle, 0, sizeof(struct nfs_fh));
2489 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2491 struct nfs4_getattr_arg args = {
2493 .bitmask = server->attr_bitmask,
2495 struct nfs4_getattr_res res = {
2499 struct rpc_message msg = {
2500 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2505 nfs_fattr_init(fattr);
2506 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2509 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2511 struct nfs4_exception exception = { };
2514 err = nfs4_handle_exception(server,
2515 _nfs4_proc_getattr(server, fhandle, fattr),
2517 } while (exception.retry);
2522 * The file is not closed if it is opened due to the a request to change
2523 * the size of the file. The open call will not be needed once the
2524 * VFS layer lookup-intents are implemented.
2526 * Close is called when the inode is destroyed.
2527 * If we haven't opened the file for O_WRONLY, we
2528 * need to in the size_change case to obtain a stateid.
2531 * Because OPEN is always done by name in nfsv4, it is
2532 * possible that we opened a different file by the same
2533 * name. We can recognize this race condition, but we
2534 * can't do anything about it besides returning an error.
2536 * This will be fixed with VFS changes (lookup-intent).
2539 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2540 struct iattr *sattr)
2542 struct inode *inode = dentry->d_inode;
2543 struct rpc_cred *cred = NULL;
2544 struct nfs4_state *state = NULL;
2547 if (pnfs_ld_layoutret_on_setattr(inode))
2548 pnfs_return_layout(inode);
2550 nfs_fattr_init(fattr);
2552 /* Deal with open(O_TRUNC) */
2553 if (sattr->ia_valid & ATTR_OPEN)
2554 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2556 /* Optimization: if the end result is no change, don't RPC */
2557 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2560 /* Search for an existing open(O_WRITE) file */
2561 if (sattr->ia_valid & ATTR_FILE) {
2562 struct nfs_open_context *ctx;
2564 ctx = nfs_file_open_context(sattr->ia_file);
2571 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2573 nfs_setattr_update_inode(inode, sattr);
2577 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2578 const struct qstr *name, struct nfs_fh *fhandle,
2579 struct nfs_fattr *fattr)
2581 struct nfs_server *server = NFS_SERVER(dir);
2583 struct nfs4_lookup_arg args = {
2584 .bitmask = server->attr_bitmask,
2585 .dir_fh = NFS_FH(dir),
2588 struct nfs4_lookup_res res = {
2593 struct rpc_message msg = {
2594 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2599 nfs_fattr_init(fattr);
2601 dprintk("NFS call lookup %s\n", name->name);
2602 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2603 dprintk("NFS reply lookup: %d\n", status);
2607 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2609 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2610 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2611 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2615 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2616 struct qstr *name, struct nfs_fh *fhandle,
2617 struct nfs_fattr *fattr)
2619 struct nfs4_exception exception = { };
2620 struct rpc_clnt *client = *clnt;
2623 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2625 case -NFS4ERR_BADNAME:
2628 case -NFS4ERR_MOVED:
2629 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
2631 case -NFS4ERR_WRONGSEC:
2633 if (client != *clnt)
2636 client = nfs4_create_sec_client(client, dir, name);
2638 return PTR_ERR(client);
2640 exception.retry = 1;
2643 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2645 } while (exception.retry);
2650 else if (client != *clnt)
2651 rpc_shutdown_client(client);
2656 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
2657 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2660 struct rpc_clnt *client = NFS_CLIENT(dir);
2662 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2663 if (client != NFS_CLIENT(dir)) {
2664 rpc_shutdown_client(client);
2665 nfs_fixup_secinfo_attributes(fattr);
2671 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2672 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2675 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2677 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2679 rpc_shutdown_client(client);
2680 return ERR_PTR(status);
2685 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2687 struct nfs_server *server = NFS_SERVER(inode);
2688 struct nfs4_accessargs args = {
2689 .fh = NFS_FH(inode),
2690 .bitmask = server->cache_consistency_bitmask,
2692 struct nfs4_accessres res = {
2695 struct rpc_message msg = {
2696 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2699 .rpc_cred = entry->cred,
2701 int mode = entry->mask;
2705 * Determine which access bits we want to ask for...
2707 if (mode & MAY_READ)
2708 args.access |= NFS4_ACCESS_READ;
2709 if (S_ISDIR(inode->i_mode)) {
2710 if (mode & MAY_WRITE)
2711 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2712 if (mode & MAY_EXEC)
2713 args.access |= NFS4_ACCESS_LOOKUP;
2715 if (mode & MAY_WRITE)
2716 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2717 if (mode & MAY_EXEC)
2718 args.access |= NFS4_ACCESS_EXECUTE;
2721 res.fattr = nfs_alloc_fattr();
2722 if (res.fattr == NULL)
2725 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2728 if (res.access & NFS4_ACCESS_READ)
2729 entry->mask |= MAY_READ;
2730 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2731 entry->mask |= MAY_WRITE;
2732 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2733 entry->mask |= MAY_EXEC;
2734 nfs_refresh_inode(inode, res.fattr);
2736 nfs_free_fattr(res.fattr);
2740 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2742 struct nfs4_exception exception = { };
2745 err = nfs4_handle_exception(NFS_SERVER(inode),
2746 _nfs4_proc_access(inode, entry),
2748 } while (exception.retry);
2753 * TODO: For the time being, we don't try to get any attributes
2754 * along with any of the zero-copy operations READ, READDIR,
2757 * In the case of the first three, we want to put the GETATTR
2758 * after the read-type operation -- this is because it is hard
2759 * to predict the length of a GETATTR response in v4, and thus
2760 * align the READ data correctly. This means that the GETATTR
2761 * may end up partially falling into the page cache, and we should
2762 * shift it into the 'tail' of the xdr_buf before processing.
2763 * To do this efficiently, we need to know the total length
2764 * of data received, which doesn't seem to be available outside
2767 * In the case of WRITE, we also want to put the GETATTR after
2768 * the operation -- in this case because we want to make sure
2769 * we get the post-operation mtime and size. This means that
2770 * we can't use xdr_encode_pages() as written: we need a variant
2771 * of it which would leave room in the 'tail' iovec.
2773 * Both of these changes to the XDR layer would in fact be quite
2774 * minor, but I decided to leave them for a subsequent patch.
2776 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2777 unsigned int pgbase, unsigned int pglen)
2779 struct nfs4_readlink args = {
2780 .fh = NFS_FH(inode),
2785 struct nfs4_readlink_res res;
2786 struct rpc_message msg = {
2787 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2792 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2795 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2796 unsigned int pgbase, unsigned int pglen)
2798 struct nfs4_exception exception = { };
2801 err = nfs4_handle_exception(NFS_SERVER(inode),
2802 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2804 } while (exception.retry);
2809 * This is just for mknod. open(O_CREAT) will always do ->open_context().
2812 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2815 struct nfs_open_context *ctx;
2816 struct nfs4_state *state;
2819 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2821 return PTR_ERR(ctx);
2823 sattr->ia_mode &= ~current_umask();
2824 state = nfs4_do_open(dir, dentry, ctx->mode, flags, sattr, ctx->cred, NULL);
2826 if (IS_ERR(state)) {
2827 status = PTR_ERR(state);
2830 d_add(dentry, igrab(state->inode));
2831 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2834 put_nfs_open_context(ctx);
2838 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2840 struct nfs_server *server = NFS_SERVER(dir);
2841 struct nfs_removeargs args = {
2845 struct nfs_removeres res = {
2848 struct rpc_message msg = {
2849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2855 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2857 update_changeattr(dir, &res.cinfo);
2861 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2863 struct nfs4_exception exception = { };
2866 err = nfs4_handle_exception(NFS_SERVER(dir),
2867 _nfs4_proc_remove(dir, name),
2869 } while (exception.retry);
2873 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2875 struct nfs_server *server = NFS_SERVER(dir);
2876 struct nfs_removeargs *args = msg->rpc_argp;
2877 struct nfs_removeres *res = msg->rpc_resp;
2879 res->server = server;
2880 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2881 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
2884 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2886 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2887 &data->args.seq_args,
2891 rpc_call_start(task);
2894 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2896 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2898 if (!nfs4_sequence_done(task, &res->seq_res))
2900 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2902 update_changeattr(dir, &res->cinfo);
2906 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2908 struct nfs_server *server = NFS_SERVER(dir);
2909 struct nfs_renameargs *arg = msg->rpc_argp;
2910 struct nfs_renameres *res = msg->rpc_resp;
2912 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2913 res->server = server;
2914 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
2917 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2919 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2920 &data->args.seq_args,
2924 rpc_call_start(task);
2927 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2928 struct inode *new_dir)
2930 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2932 if (!nfs4_sequence_done(task, &res->seq_res))
2934 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2937 update_changeattr(old_dir, &res->old_cinfo);
2938 update_changeattr(new_dir, &res->new_cinfo);
2942 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2943 struct inode *new_dir, struct qstr *new_name)
2945 struct nfs_server *server = NFS_SERVER(old_dir);
2946 struct nfs_renameargs arg = {
2947 .old_dir = NFS_FH(old_dir),
2948 .new_dir = NFS_FH(new_dir),
2949 .old_name = old_name,
2950 .new_name = new_name,
2952 struct nfs_renameres res = {
2955 struct rpc_message msg = {
2956 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2960 int status = -ENOMEM;
2962 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2964 update_changeattr(old_dir, &res.old_cinfo);
2965 update_changeattr(new_dir, &res.new_cinfo);
2970 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2971 struct inode *new_dir, struct qstr *new_name)
2973 struct nfs4_exception exception = { };
2976 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2977 _nfs4_proc_rename(old_dir, old_name,
2980 } while (exception.retry);
2984 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2986 struct nfs_server *server = NFS_SERVER(inode);
2987 struct nfs4_link_arg arg = {
2988 .fh = NFS_FH(inode),
2989 .dir_fh = NFS_FH(dir),
2991 .bitmask = server->attr_bitmask,
2993 struct nfs4_link_res res = {
2996 struct rpc_message msg = {
2997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3001 int status = -ENOMEM;
3003 res.fattr = nfs_alloc_fattr();
3004 if (res.fattr == NULL)
3007 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3009 update_changeattr(dir, &res.cinfo);
3010 nfs_post_op_update_inode(inode, res.fattr);
3013 nfs_free_fattr(res.fattr);
3017 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3019 struct nfs4_exception exception = { };
3022 err = nfs4_handle_exception(NFS_SERVER(inode),
3023 _nfs4_proc_link(inode, dir, name),
3025 } while (exception.retry);
3029 struct nfs4_createdata {
3030 struct rpc_message msg;
3031 struct nfs4_create_arg arg;
3032 struct nfs4_create_res res;
3034 struct nfs_fattr fattr;
3037 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3038 struct qstr *name, struct iattr *sattr, u32 ftype)
3040 struct nfs4_createdata *data;
3042 data = kzalloc(sizeof(*data), GFP_KERNEL);
3044 struct nfs_server *server = NFS_SERVER(dir);
3046 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3047 data->msg.rpc_argp = &data->arg;
3048 data->msg.rpc_resp = &data->res;
3049 data->arg.dir_fh = NFS_FH(dir);
3050 data->arg.server = server;
3051 data->arg.name = name;
3052 data->arg.attrs = sattr;
3053 data->arg.ftype = ftype;
3054 data->arg.bitmask = server->attr_bitmask;
3055 data->res.server = server;
3056 data->res.fh = &data->fh;
3057 data->res.fattr = &data->fattr;
3058 nfs_fattr_init(data->res.fattr);
3063 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3065 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3066 &data->arg.seq_args, &data->res.seq_res, 1);
3068 update_changeattr(dir, &data->res.dir_cinfo);
3069 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3074 static void nfs4_free_createdata(struct nfs4_createdata *data)
3079 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3080 struct page *page, unsigned int len, struct iattr *sattr)
3082 struct nfs4_createdata *data;
3083 int status = -ENAMETOOLONG;
3085 if (len > NFS4_MAXPATHLEN)
3089 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3093 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3094 data->arg.u.symlink.pages = &page;
3095 data->arg.u.symlink.len = len;
3097 status = nfs4_do_create(dir, dentry, data);
3099 nfs4_free_createdata(data);
3104 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3105 struct page *page, unsigned int len, struct iattr *sattr)
3107 struct nfs4_exception exception = { };
3110 err = nfs4_handle_exception(NFS_SERVER(dir),
3111 _nfs4_proc_symlink(dir, dentry, page,
3114 } while (exception.retry);
3118 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3119 struct iattr *sattr)
3121 struct nfs4_createdata *data;
3122 int status = -ENOMEM;
3124 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3128 status = nfs4_do_create(dir, dentry, data);
3130 nfs4_free_createdata(data);
3135 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3136 struct iattr *sattr)
3138 struct nfs4_exception exception = { };
3141 sattr->ia_mode &= ~current_umask();
3143 err = nfs4_handle_exception(NFS_SERVER(dir),
3144 _nfs4_proc_mkdir(dir, dentry, sattr),
3146 } while (exception.retry);
3150 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3151 u64 cookie, struct page **pages, unsigned int count, int plus)
3153 struct inode *dir = dentry->d_inode;
3154 struct nfs4_readdir_arg args = {
3159 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3162 struct nfs4_readdir_res res;
3163 struct rpc_message msg = {
3164 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3171 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3172 dentry->d_parent->d_name.name,
3173 dentry->d_name.name,
3174 (unsigned long long)cookie);
3175 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3176 res.pgbase = args.pgbase;
3177 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3179 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
3180 status += args.pgbase;
3183 nfs_invalidate_atime(dir);
3185 dprintk("%s: returns %d\n", __func__, status);
3189 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3190 u64 cookie, struct page **pages, unsigned int count, int plus)
3192 struct nfs4_exception exception = { };
3195 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3196 _nfs4_proc_readdir(dentry, cred, cookie,
3197 pages, count, plus),
3199 } while (exception.retry);
3203 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3204 struct iattr *sattr, dev_t rdev)
3206 struct nfs4_createdata *data;
3207 int mode = sattr->ia_mode;
3208 int status = -ENOMEM;
3210 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3211 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3213 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3218 data->arg.ftype = NF4FIFO;
3219 else if (S_ISBLK(mode)) {
3220 data->arg.ftype = NF4BLK;
3221 data->arg.u.device.specdata1 = MAJOR(rdev);
3222 data->arg.u.device.specdata2 = MINOR(rdev);
3224 else if (S_ISCHR(mode)) {
3225 data->arg.ftype = NF4CHR;
3226 data->arg.u.device.specdata1 = MAJOR(rdev);
3227 data->arg.u.device.specdata2 = MINOR(rdev);
3230 status = nfs4_do_create(dir, dentry, data);
3232 nfs4_free_createdata(data);
3237 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3238 struct iattr *sattr, dev_t rdev)
3240 struct nfs4_exception exception = { };
3243 sattr->ia_mode &= ~current_umask();
3245 err = nfs4_handle_exception(NFS_SERVER(dir),
3246 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3248 } while (exception.retry);
3252 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3253 struct nfs_fsstat *fsstat)
3255 struct nfs4_statfs_arg args = {
3257 .bitmask = server->attr_bitmask,
3259 struct nfs4_statfs_res res = {
3262 struct rpc_message msg = {
3263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3268 nfs_fattr_init(fsstat->fattr);
3269 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3272 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3274 struct nfs4_exception exception = { };
3277 err = nfs4_handle_exception(server,
3278 _nfs4_proc_statfs(server, fhandle, fsstat),
3280 } while (exception.retry);
3284 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3285 struct nfs_fsinfo *fsinfo)
3287 struct nfs4_fsinfo_arg args = {
3289 .bitmask = server->attr_bitmask,
3291 struct nfs4_fsinfo_res res = {
3294 struct rpc_message msg = {
3295 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3300 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3303 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3305 struct nfs4_exception exception = { };
3309 err = nfs4_handle_exception(server,
3310 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3312 } while (exception.retry);
3316 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3318 nfs_fattr_init(fsinfo->fattr);
3319 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3322 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3323 struct nfs_pathconf *pathconf)
3325 struct nfs4_pathconf_arg args = {
3327 .bitmask = server->attr_bitmask,
3329 struct nfs4_pathconf_res res = {
3330 .pathconf = pathconf,
3332 struct rpc_message msg = {
3333 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3338 /* None of the pathconf attributes are mandatory to implement */
3339 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3340 memset(pathconf, 0, sizeof(*pathconf));
3344 nfs_fattr_init(pathconf->fattr);
3345 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3348 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3349 struct nfs_pathconf *pathconf)
3351 struct nfs4_exception exception = { };
3355 err = nfs4_handle_exception(server,
3356 _nfs4_proc_pathconf(server, fhandle, pathconf),
3358 } while (exception.retry);
3362 void __nfs4_read_done_cb(struct nfs_read_data *data)
3364 nfs_invalidate_atime(data->header->inode);
3367 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3369 struct nfs_server *server = NFS_SERVER(data->header->inode);
3371 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3372 rpc_restart_call_prepare(task);
3376 __nfs4_read_done_cb(data);
3377 if (task->tk_status > 0)
3378 renew_lease(server, data->timestamp);
3382 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3385 dprintk("--> %s\n", __func__);
3387 if (!nfs4_sequence_done(task, &data->res.seq_res))
3390 return data->read_done_cb ? data->read_done_cb(task, data) :
3391 nfs4_read_done_cb(task, data);
3394 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3396 data->timestamp = jiffies;
3397 data->read_done_cb = nfs4_read_done_cb;
3398 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3399 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3402 static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3404 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3405 &data->args.seq_args,
3409 rpc_call_start(task);
3412 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3414 struct inode *inode = data->header->inode;
3416 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3417 rpc_restart_call_prepare(task);
3420 if (task->tk_status >= 0) {
3421 renew_lease(NFS_SERVER(inode), data->timestamp);
3422 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
3427 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3429 if (!nfs4_sequence_done(task, &data->res.seq_res))
3431 return data->write_done_cb ? data->write_done_cb(task, data) :
3432 nfs4_write_done_cb(task, data);
3436 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3438 const struct nfs_pgio_header *hdr = data->header;
3440 /* Don't request attributes for pNFS or O_DIRECT writes */
3441 if (data->ds_clp != NULL || hdr->dreq != NULL)
3443 /* Otherwise, request attributes if and only if we don't hold
3446 return nfs_have_delegation(hdr->inode, FMODE_READ) == 0;
3449 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3451 struct nfs_server *server = NFS_SERVER(data->header->inode);
3453 if (!nfs4_write_need_cache_consistency_data(data)) {
3454 data->args.bitmask = NULL;
3455 data->res.fattr = NULL;
3457 data->args.bitmask = server->cache_consistency_bitmask;
3459 if (!data->write_done_cb)
3460 data->write_done_cb = nfs4_write_done_cb;
3461 data->res.server = server;
3462 data->timestamp = jiffies;
3464 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3465 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3468 static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3470 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3471 &data->args.seq_args,
3475 rpc_call_start(task);
3478 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3480 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3481 &data->args.seq_args,
3485 rpc_call_start(task);
3488 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
3490 struct inode *inode = data->inode;
3492 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3493 rpc_restart_call_prepare(task);
3499 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
3501 if (!nfs4_sequence_done(task, &data->res.seq_res))
3503 return data->commit_done_cb(task, data);
3506 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
3508 struct nfs_server *server = NFS_SERVER(data->inode);
3510 if (data->commit_done_cb == NULL)
3511 data->commit_done_cb = nfs4_commit_done_cb;
3512 data->res.server = server;
3513 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3514 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3517 struct nfs4_renewdata {
3518 struct nfs_client *client;
3519 unsigned long timestamp;
3523 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3524 * standalone procedure for queueing an asynchronous RENEW.
3526 static void nfs4_renew_release(void *calldata)
3528 struct nfs4_renewdata *data = calldata;
3529 struct nfs_client *clp = data->client;
3531 if (atomic_read(&clp->cl_count) > 1)
3532 nfs4_schedule_state_renewal(clp);
3533 nfs_put_client(clp);
3537 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3539 struct nfs4_renewdata *data = calldata;
3540 struct nfs_client *clp = data->client;
3541 unsigned long timestamp = data->timestamp;
3543 if (task->tk_status < 0) {
3544 /* Unless we're shutting down, schedule state recovery! */
3545 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3547 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3548 nfs4_schedule_lease_recovery(clp);
3551 nfs4_schedule_path_down_recovery(clp);
3553 do_renew_lease(clp, timestamp);
3556 static const struct rpc_call_ops nfs4_renew_ops = {
3557 .rpc_call_done = nfs4_renew_done,
3558 .rpc_release = nfs4_renew_release,
3561 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3563 struct rpc_message msg = {
3564 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3568 struct nfs4_renewdata *data;
3570 if (renew_flags == 0)
3572 if (!atomic_inc_not_zero(&clp->cl_count))
3574 data = kmalloc(sizeof(*data), GFP_NOFS);
3578 data->timestamp = jiffies;
3579 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3580 &nfs4_renew_ops, data);
3583 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3585 struct rpc_message msg = {
3586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3590 unsigned long now = jiffies;
3593 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3596 do_renew_lease(clp, now);
3600 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3602 return (server->caps & NFS_CAP_ACLS)
3603 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3604 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3607 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3608 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3611 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3613 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3614 struct page **pages, unsigned int *pgbase)
3616 struct page *newpage, **spages;
3622 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3623 newpage = alloc_page(GFP_KERNEL);
3625 if (newpage == NULL)
3627 memcpy(page_address(newpage), buf, len);
3632 } while (buflen != 0);
3638 __free_page(spages[rc-1]);
3642 struct nfs4_cached_acl {
3648 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3650 struct nfs_inode *nfsi = NFS_I(inode);
3652 spin_lock(&inode->i_lock);
3653 kfree(nfsi->nfs4_acl);
3654 nfsi->nfs4_acl = acl;
3655 spin_unlock(&inode->i_lock);
3658 static void nfs4_zap_acl_attr(struct inode *inode)
3660 nfs4_set_cached_acl(inode, NULL);
3663 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3665 struct nfs_inode *nfsi = NFS_I(inode);
3666 struct nfs4_cached_acl *acl;
3669 spin_lock(&inode->i_lock);
3670 acl = nfsi->nfs4_acl;
3673 if (buf == NULL) /* user is just asking for length */
3675 if (acl->cached == 0)
3677 ret = -ERANGE; /* see getxattr(2) man page */
3678 if (acl->len > buflen)
3680 memcpy(buf, acl->data, acl->len);
3684 spin_unlock(&inode->i_lock);
3688 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3690 struct nfs4_cached_acl *acl;
3692 if (pages && acl_len <= PAGE_SIZE) {
3693 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3697 _copy_from_pages(acl->data, pages, pgbase, acl_len);
3699 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3706 nfs4_set_cached_acl(inode, acl);
3710 * The getxattr API returns the required buffer length when called with a
3711 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3712 * the required buf. On a NULL buf, we send a page of data to the server
3713 * guessing that the ACL request can be serviced by a page. If so, we cache
3714 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3715 * the cache. If not so, we throw away the page, and cache the required
3716 * length. The next getxattr call will then produce another round trip to
3717 * the server, this time with the input buf of the required size.
3719 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3721 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3722 struct nfs_getaclargs args = {
3723 .fh = NFS_FH(inode),
3727 struct nfs_getaclres res = {
3730 struct rpc_message msg = {
3731 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3735 int ret = -ENOMEM, npages, i, acl_len = 0;
3737 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3738 /* As long as we're doing a round trip to the server anyway,
3739 * let's be prepared for a page of acl data. */
3743 /* Add an extra page to handle the bitmap returned */
3746 for (i = 0; i < npages; i++) {
3747 pages[i] = alloc_page(GFP_KERNEL);
3752 /* for decoding across pages */
3753 res.acl_scratch = alloc_page(GFP_KERNEL);
3754 if (!res.acl_scratch)
3757 args.acl_len = npages * PAGE_SIZE;
3758 args.acl_pgbase = 0;
3760 /* Let decode_getfacl know not to fail if the ACL data is larger than
3761 * the page we send as a guess */
3763 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3765 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3766 __func__, buf, buflen, npages, args.acl_len);
3767 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3768 &msg, &args.seq_args, &res.seq_res, 0);
3772 acl_len = res.acl_len - res.acl_data_offset;
3773 if (acl_len > args.acl_len)
3774 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
3776 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
3780 if (acl_len > buflen)
3782 _copy_from_pages(buf, pages, res.acl_data_offset,
3787 for (i = 0; i < npages; i++)
3789 __free_page(pages[i]);
3790 if (res.acl_scratch)
3791 __free_page(res.acl_scratch);
3795 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3797 struct nfs4_exception exception = { };
3800 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3803 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3804 } while (exception.retry);
3808 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3810 struct nfs_server *server = NFS_SERVER(inode);
3813 if (!nfs4_server_supports_acls(server))
3815 ret = nfs_revalidate_inode(server, inode);
3818 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3819 nfs_zap_acl_cache(inode);
3820 ret = nfs4_read_cached_acl(inode, buf, buflen);
3822 /* -ENOENT is returned if there is no ACL or if there is an ACL
3823 * but no cached acl data, just the acl length */
3825 return nfs4_get_acl_uncached(inode, buf, buflen);
3828 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3830 struct nfs_server *server = NFS_SERVER(inode);
3831 struct page *pages[NFS4ACL_MAXPAGES];
3832 struct nfs_setaclargs arg = {
3833 .fh = NFS_FH(inode),
3837 struct nfs_setaclres res;
3838 struct rpc_message msg = {
3839 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3845 if (!nfs4_server_supports_acls(server))
3847 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3850 nfs_inode_return_delegation(inode);
3851 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3854 * Free each page after tx, so the only ref left is
3855 * held by the network stack
3858 put_page(pages[i-1]);
3861 * Acl update can result in inode attribute update.
3862 * so mark the attribute cache invalid.
3864 spin_lock(&inode->i_lock);
3865 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3866 spin_unlock(&inode->i_lock);
3867 nfs_access_zap_cache(inode);
3868 nfs_zap_acl_cache(inode);
3872 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3874 struct nfs4_exception exception = { };
3877 err = nfs4_handle_exception(NFS_SERVER(inode),
3878 __nfs4_proc_set_acl(inode, buf, buflen),
3880 } while (exception.retry);
3885 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3887 struct nfs_client *clp = server->nfs_client;
3889 if (task->tk_status >= 0)
3891 switch(task->tk_status) {
3892 case -NFS4ERR_DELEG_REVOKED:
3893 case -NFS4ERR_ADMIN_REVOKED:
3894 case -NFS4ERR_BAD_STATEID:
3897 nfs_remove_bad_delegation(state->inode);
3898 case -NFS4ERR_OPENMODE:
3901 nfs4_schedule_stateid_recovery(server, state);
3902 goto wait_on_recovery;
3903 case -NFS4ERR_EXPIRED:
3905 nfs4_schedule_stateid_recovery(server, state);
3906 case -NFS4ERR_STALE_STATEID:
3907 case -NFS4ERR_STALE_CLIENTID:
3908 nfs4_schedule_lease_recovery(clp);
3909 goto wait_on_recovery;
3910 #if defined(CONFIG_NFS_V4_1)
3911 case -NFS4ERR_BADSESSION:
3912 case -NFS4ERR_BADSLOT:
3913 case -NFS4ERR_BAD_HIGH_SLOT:
3914 case -NFS4ERR_DEADSESSION:
3915 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3916 case -NFS4ERR_SEQ_FALSE_RETRY:
3917 case -NFS4ERR_SEQ_MISORDERED:
3918 dprintk("%s ERROR %d, Reset session\n", __func__,
3920 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
3921 task->tk_status = 0;
3923 #endif /* CONFIG_NFS_V4_1 */
3924 case -NFS4ERR_DELAY:
3925 nfs_inc_server_stats(server, NFSIOS_DELAY);
3926 case -NFS4ERR_GRACE:
3928 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3929 task->tk_status = 0;
3931 case -NFS4ERR_RETRY_UNCACHED_REP:
3932 case -NFS4ERR_OLD_STATEID:
3933 task->tk_status = 0;
3936 task->tk_status = nfs4_map_errors(task->tk_status);
3939 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3940 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3941 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3942 task->tk_status = 0;
3946 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3947 nfs4_verifier *bootverf)
3951 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3952 /* An impossible timestamp guarantees this value
3953 * will never match a generated boot time. */
3955 verf[1] = (__be32)(NSEC_PER_SEC + 1);
3957 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
3958 verf[0] = (__be32)nn->boot_time.tv_sec;
3959 verf[1] = (__be32)nn->boot_time.tv_nsec;
3961 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3964 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3965 unsigned short port, struct rpc_cred *cred,
3966 struct nfs4_setclientid_res *res)
3968 nfs4_verifier sc_verifier;
3969 struct nfs4_setclientid setclientid = {
3970 .sc_verifier = &sc_verifier,
3972 .sc_cb_ident = clp->cl_cb_ident,
3974 struct rpc_message msg = {
3975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3976 .rpc_argp = &setclientid,
3983 nfs4_init_boot_verifier(clp, &sc_verifier);
3987 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3988 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3990 rpc_peeraddr2str(clp->cl_rpcclient,
3992 rpc_peeraddr2str(clp->cl_rpcclient,
3994 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3995 clp->cl_id_uniquifier);
3996 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3997 sizeof(setclientid.sc_netid),
3998 rpc_peeraddr2str(clp->cl_rpcclient,
3999 RPC_DISPLAY_NETID));
4000 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4001 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4002 clp->cl_ipaddr, port >> 8, port & 255);
4005 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4006 if (status != -NFS4ERR_CLID_INUSE)
4009 ++clp->cl_id_uniquifier;
4013 ssleep(clp->cl_lease_time / HZ + 1);
4018 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4019 struct nfs4_setclientid_res *arg,
4020 struct rpc_cred *cred)
4022 struct nfs_fsinfo fsinfo;
4023 struct rpc_message msg = {
4024 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4026 .rpc_resp = &fsinfo,
4033 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4035 spin_lock(&clp->cl_lock);
4036 clp->cl_lease_time = fsinfo.lease_time * HZ;
4037 clp->cl_last_renewal = now;
4038 spin_unlock(&clp->cl_lock);
4043 struct nfs4_delegreturndata {
4044 struct nfs4_delegreturnargs args;
4045 struct nfs4_delegreturnres res;
4047 nfs4_stateid stateid;
4048 unsigned long timestamp;
4049 struct nfs_fattr fattr;
4053 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4055 struct nfs4_delegreturndata *data = calldata;
4057 if (!nfs4_sequence_done(task, &data->res.seq_res))
4060 switch (task->tk_status) {
4061 case -NFS4ERR_STALE_STATEID:
4062 case -NFS4ERR_EXPIRED:
4064 renew_lease(data->res.server, data->timestamp);
4067 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4069 rpc_restart_call_prepare(task);
4073 data->rpc_status = task->tk_status;
4076 static void nfs4_delegreturn_release(void *calldata)
4081 #if defined(CONFIG_NFS_V4_1)
4082 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4084 struct nfs4_delegreturndata *d_data;
4086 d_data = (struct nfs4_delegreturndata *)data;
4088 if (nfs4_setup_sequence(d_data->res.server,
4089 &d_data->args.seq_args,
4090 &d_data->res.seq_res, task))
4092 rpc_call_start(task);
4094 #endif /* CONFIG_NFS_V4_1 */
4096 static const struct rpc_call_ops nfs4_delegreturn_ops = {
4097 #if defined(CONFIG_NFS_V4_1)
4098 .rpc_call_prepare = nfs4_delegreturn_prepare,
4099 #endif /* CONFIG_NFS_V4_1 */
4100 .rpc_call_done = nfs4_delegreturn_done,
4101 .rpc_release = nfs4_delegreturn_release,
4104 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4106 struct nfs4_delegreturndata *data;
4107 struct nfs_server *server = NFS_SERVER(inode);
4108 struct rpc_task *task;
4109 struct rpc_message msg = {
4110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4113 struct rpc_task_setup task_setup_data = {
4114 .rpc_client = server->client,
4115 .rpc_message = &msg,
4116 .callback_ops = &nfs4_delegreturn_ops,
4117 .flags = RPC_TASK_ASYNC,
4121 data = kzalloc(sizeof(*data), GFP_NOFS);
4124 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4125 data->args.fhandle = &data->fh;
4126 data->args.stateid = &data->stateid;
4127 data->args.bitmask = server->cache_consistency_bitmask;
4128 nfs_copy_fh(&data->fh, NFS_FH(inode));
4129 nfs4_stateid_copy(&data->stateid, stateid);
4130 data->res.fattr = &data->fattr;
4131 data->res.server = server;
4132 nfs_fattr_init(data->res.fattr);
4133 data->timestamp = jiffies;
4134 data->rpc_status = 0;
4136 task_setup_data.callback_data = data;
4137 msg.rpc_argp = &data->args;
4138 msg.rpc_resp = &data->res;
4139 task = rpc_run_task(&task_setup_data);
4141 return PTR_ERR(task);
4144 status = nfs4_wait_for_completion_rpc_task(task);
4147 status = data->rpc_status;
4149 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4151 nfs_refresh_inode(inode, &data->fattr);
4157 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4159 struct nfs_server *server = NFS_SERVER(inode);
4160 struct nfs4_exception exception = { };
4163 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4165 case -NFS4ERR_STALE_STATEID:
4166 case -NFS4ERR_EXPIRED:
4170 err = nfs4_handle_exception(server, err, &exception);
4171 } while (exception.retry);
4175 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4176 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4179 * sleep, with exponential backoff, and retry the LOCK operation.
4181 static unsigned long
4182 nfs4_set_lock_task_retry(unsigned long timeout)
4184 freezable_schedule_timeout_killable(timeout);
4186 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4187 return NFS4_LOCK_MAXTIMEOUT;
4191 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4193 struct inode *inode = state->inode;
4194 struct nfs_server *server = NFS_SERVER(inode);
4195 struct nfs_client *clp = server->nfs_client;
4196 struct nfs_lockt_args arg = {
4197 .fh = NFS_FH(inode),
4200 struct nfs_lockt_res res = {
4203 struct rpc_message msg = {
4204 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4207 .rpc_cred = state->owner->so_cred,
4209 struct nfs4_lock_state *lsp;
4212 arg.lock_owner.clientid = clp->cl_clientid;
4213 status = nfs4_set_lock_state(state, request);
4216 lsp = request->fl_u.nfs4_fl.owner;
4217 arg.lock_owner.id = lsp->ls_seqid.owner_id;
4218 arg.lock_owner.s_dev = server->s_dev;
4219 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4222 request->fl_type = F_UNLCK;
4224 case -NFS4ERR_DENIED:
4227 request->fl_ops->fl_release_private(request);
4232 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4234 struct nfs4_exception exception = { };
4238 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4239 _nfs4_proc_getlk(state, cmd, request),
4241 } while (exception.retry);
4245 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4248 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4250 res = posix_lock_file_wait(file, fl);
4253 res = flock_lock_file_wait(file, fl);
4261 struct nfs4_unlockdata {
4262 struct nfs_locku_args arg;
4263 struct nfs_locku_res res;
4264 struct nfs4_lock_state *lsp;
4265 struct nfs_open_context *ctx;
4266 struct file_lock fl;
4267 const struct nfs_server *server;
4268 unsigned long timestamp;
4271 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4272 struct nfs_open_context *ctx,
4273 struct nfs4_lock_state *lsp,
4274 struct nfs_seqid *seqid)
4276 struct nfs4_unlockdata *p;
4277 struct inode *inode = lsp->ls_state->inode;
4279 p = kzalloc(sizeof(*p), GFP_NOFS);
4282 p->arg.fh = NFS_FH(inode);
4284 p->arg.seqid = seqid;
4285 p->res.seqid = seqid;
4286 p->arg.stateid = &lsp->ls_stateid;
4288 atomic_inc(&lsp->ls_count);
4289 /* Ensure we don't close file until we're done freeing locks! */
4290 p->ctx = get_nfs_open_context(ctx);
4291 memcpy(&p->fl, fl, sizeof(p->fl));
4292 p->server = NFS_SERVER(inode);
4296 static void nfs4_locku_release_calldata(void *data)
4298 struct nfs4_unlockdata *calldata = data;
4299 nfs_free_seqid(calldata->arg.seqid);
4300 nfs4_put_lock_state(calldata->lsp);
4301 put_nfs_open_context(calldata->ctx);
4305 static void nfs4_locku_done(struct rpc_task *task, void *data)
4307 struct nfs4_unlockdata *calldata = data;
4309 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4311 switch (task->tk_status) {
4313 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4314 &calldata->res.stateid);
4315 renew_lease(calldata->server, calldata->timestamp);
4317 case -NFS4ERR_BAD_STATEID:
4318 case -NFS4ERR_OLD_STATEID:
4319 case -NFS4ERR_STALE_STATEID:
4320 case -NFS4ERR_EXPIRED:
4323 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4324 rpc_restart_call_prepare(task);
4328 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4330 struct nfs4_unlockdata *calldata = data;
4332 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4334 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
4335 /* Note: exit _without_ running nfs4_locku_done */
4336 task->tk_action = NULL;
4339 calldata->timestamp = jiffies;
4340 if (nfs4_setup_sequence(calldata->server,
4341 &calldata->arg.seq_args,
4342 &calldata->res.seq_res, task))
4344 rpc_call_start(task);
4347 static const struct rpc_call_ops nfs4_locku_ops = {
4348 .rpc_call_prepare = nfs4_locku_prepare,
4349 .rpc_call_done = nfs4_locku_done,
4350 .rpc_release = nfs4_locku_release_calldata,
4353 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4354 struct nfs_open_context *ctx,
4355 struct nfs4_lock_state *lsp,
4356 struct nfs_seqid *seqid)
4358 struct nfs4_unlockdata *data;
4359 struct rpc_message msg = {
4360 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4361 .rpc_cred = ctx->cred,
4363 struct rpc_task_setup task_setup_data = {
4364 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4365 .rpc_message = &msg,
4366 .callback_ops = &nfs4_locku_ops,
4367 .workqueue = nfsiod_workqueue,
4368 .flags = RPC_TASK_ASYNC,
4371 /* Ensure this is an unlock - when canceling a lock, the
4372 * canceled lock is passed in, and it won't be an unlock.
4374 fl->fl_type = F_UNLCK;
4376 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4378 nfs_free_seqid(seqid);
4379 return ERR_PTR(-ENOMEM);
4382 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4383 msg.rpc_argp = &data->arg;
4384 msg.rpc_resp = &data->res;
4385 task_setup_data.callback_data = data;
4386 return rpc_run_task(&task_setup_data);
4389 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4391 struct nfs_inode *nfsi = NFS_I(state->inode);
4392 struct nfs_seqid *seqid;
4393 struct nfs4_lock_state *lsp;
4394 struct rpc_task *task;
4396 unsigned char fl_flags = request->fl_flags;
4398 status = nfs4_set_lock_state(state, request);
4399 /* Unlock _before_ we do the RPC call */
4400 request->fl_flags |= FL_EXISTS;
4401 down_read(&nfsi->rwsem);
4402 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4403 up_read(&nfsi->rwsem);
4406 up_read(&nfsi->rwsem);
4409 /* Is this a delegated lock? */
4410 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4412 lsp = request->fl_u.nfs4_fl.owner;
4413 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4417 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4418 status = PTR_ERR(task);
4421 status = nfs4_wait_for_completion_rpc_task(task);
4424 request->fl_flags = fl_flags;
4428 struct nfs4_lockdata {
4429 struct nfs_lock_args arg;
4430 struct nfs_lock_res res;
4431 struct nfs4_lock_state *lsp;
4432 struct nfs_open_context *ctx;
4433 struct file_lock fl;
4434 unsigned long timestamp;
4437 struct nfs_server *server;
4440 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4441 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4444 struct nfs4_lockdata *p;
4445 struct inode *inode = lsp->ls_state->inode;
4446 struct nfs_server *server = NFS_SERVER(inode);
4448 p = kzalloc(sizeof(*p), gfp_mask);
4452 p->arg.fh = NFS_FH(inode);
4454 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4455 if (p->arg.open_seqid == NULL)
4457 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4458 if (p->arg.lock_seqid == NULL)
4459 goto out_free_seqid;
4460 p->arg.lock_stateid = &lsp->ls_stateid;
4461 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4462 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4463 p->arg.lock_owner.s_dev = server->s_dev;
4464 p->res.lock_seqid = p->arg.lock_seqid;
4467 atomic_inc(&lsp->ls_count);
4468 p->ctx = get_nfs_open_context(ctx);
4469 memcpy(&p->fl, fl, sizeof(p->fl));
4472 nfs_free_seqid(p->arg.open_seqid);
4478 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4480 struct nfs4_lockdata *data = calldata;
4481 struct nfs4_state *state = data->lsp->ls_state;
4483 dprintk("%s: begin!\n", __func__);
4484 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4486 /* Do we need to do an open_to_lock_owner? */
4487 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4488 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4490 data->arg.open_stateid = &state->stateid;
4491 data->arg.new_lock_owner = 1;
4492 data->res.open_seqid = data->arg.open_seqid;
4494 data->arg.new_lock_owner = 0;
4495 data->timestamp = jiffies;
4496 if (nfs4_setup_sequence(data->server,
4497 &data->arg.seq_args,
4498 &data->res.seq_res, task))
4500 rpc_call_start(task);
4501 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4504 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4506 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4507 nfs4_lock_prepare(task, calldata);
4510 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4512 struct nfs4_lockdata *data = calldata;
4514 dprintk("%s: begin!\n", __func__);
4516 if (!nfs4_sequence_done(task, &data->res.seq_res))
4519 data->rpc_status = task->tk_status;
4520 if (data->arg.new_lock_owner != 0) {
4521 if (data->rpc_status == 0)
4522 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4526 if (data->rpc_status == 0) {
4527 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4528 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4529 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4532 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4535 static void nfs4_lock_release(void *calldata)
4537 struct nfs4_lockdata *data = calldata;
4539 dprintk("%s: begin!\n", __func__);
4540 nfs_free_seqid(data->arg.open_seqid);
4541 if (data->cancelled != 0) {
4542 struct rpc_task *task;
4543 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4544 data->arg.lock_seqid);
4546 rpc_put_task_async(task);
4547 dprintk("%s: cancelling lock!\n", __func__);
4549 nfs_free_seqid(data->arg.lock_seqid);
4550 nfs4_put_lock_state(data->lsp);
4551 put_nfs_open_context(data->ctx);
4553 dprintk("%s: done!\n", __func__);
4556 static const struct rpc_call_ops nfs4_lock_ops = {
4557 .rpc_call_prepare = nfs4_lock_prepare,
4558 .rpc_call_done = nfs4_lock_done,
4559 .rpc_release = nfs4_lock_release,
4562 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4563 .rpc_call_prepare = nfs4_recover_lock_prepare,
4564 .rpc_call_done = nfs4_lock_done,
4565 .rpc_release = nfs4_lock_release,
4568 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4571 case -NFS4ERR_ADMIN_REVOKED:
4572 case -NFS4ERR_BAD_STATEID:
4573 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4574 if (new_lock_owner != 0 ||
4575 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4576 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4578 case -NFS4ERR_STALE_STATEID:
4579 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4580 case -NFS4ERR_EXPIRED:
4581 nfs4_schedule_lease_recovery(server->nfs_client);
4585 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4587 struct nfs4_lockdata *data;
4588 struct rpc_task *task;
4589 struct rpc_message msg = {
4590 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4591 .rpc_cred = state->owner->so_cred,
4593 struct rpc_task_setup task_setup_data = {
4594 .rpc_client = NFS_CLIENT(state->inode),
4595 .rpc_message = &msg,
4596 .callback_ops = &nfs4_lock_ops,
4597 .workqueue = nfsiod_workqueue,
4598 .flags = RPC_TASK_ASYNC,
4602 dprintk("%s: begin!\n", __func__);
4603 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4604 fl->fl_u.nfs4_fl.owner,
4605 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4609 data->arg.block = 1;
4610 if (recovery_type > NFS_LOCK_NEW) {
4611 if (recovery_type == NFS_LOCK_RECLAIM)
4612 data->arg.reclaim = NFS_LOCK_RECLAIM;
4613 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4615 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4616 msg.rpc_argp = &data->arg;
4617 msg.rpc_resp = &data->res;
4618 task_setup_data.callback_data = data;
4619 task = rpc_run_task(&task_setup_data);
4621 return PTR_ERR(task);
4622 ret = nfs4_wait_for_completion_rpc_task(task);
4624 ret = data->rpc_status;
4626 nfs4_handle_setlk_error(data->server, data->lsp,
4627 data->arg.new_lock_owner, ret);
4629 data->cancelled = 1;
4631 dprintk("%s: done, ret = %d!\n", __func__, ret);
4635 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4637 struct nfs_server *server = NFS_SERVER(state->inode);
4638 struct nfs4_exception exception = {
4639 .inode = state->inode,
4644 /* Cache the lock if possible... */
4645 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4647 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4648 if (err != -NFS4ERR_DELAY)
4650 nfs4_handle_exception(server, err, &exception);
4651 } while (exception.retry);
4655 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4657 struct nfs_server *server = NFS_SERVER(state->inode);
4658 struct nfs4_exception exception = {
4659 .inode = state->inode,
4663 err = nfs4_set_lock_state(state, request);
4667 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4669 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4673 case -NFS4ERR_GRACE:
4674 case -NFS4ERR_DELAY:
4675 nfs4_handle_exception(server, err, &exception);
4678 } while (exception.retry);
4683 #if defined(CONFIG_NFS_V4_1)
4684 static int nfs41_check_expired_locks(struct nfs4_state *state)
4686 int status, ret = NFS_OK;
4687 struct nfs4_lock_state *lsp;
4688 struct nfs_server *server = NFS_SERVER(state->inode);
4690 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4691 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4692 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4693 if (status != NFS_OK) {
4694 nfs41_free_stateid(server, &lsp->ls_stateid);
4695 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4704 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4706 int status = NFS_OK;
4708 if (test_bit(LK_STATE_IN_USE, &state->flags))
4709 status = nfs41_check_expired_locks(state);
4710 if (status == NFS_OK)
4712 return nfs4_lock_expired(state, request);
4716 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4718 struct nfs_inode *nfsi = NFS_I(state->inode);
4719 unsigned char fl_flags = request->fl_flags;
4720 int status = -ENOLCK;
4722 if ((fl_flags & FL_POSIX) &&
4723 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4725 /* Is this a delegated open? */
4726 status = nfs4_set_lock_state(state, request);
4729 request->fl_flags |= FL_ACCESS;
4730 status = do_vfs_lock(request->fl_file, request);
4733 down_read(&nfsi->rwsem);
4734 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4735 /* Yes: cache locks! */
4736 /* ...but avoid races with delegation recall... */
4737 request->fl_flags = fl_flags & ~FL_SLEEP;
4738 status = do_vfs_lock(request->fl_file, request);
4741 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4744 /* Note: we always want to sleep here! */
4745 request->fl_flags = fl_flags | FL_SLEEP;
4746 if (do_vfs_lock(request->fl_file, request) < 0)
4747 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4748 "manager!\n", __func__);
4750 up_read(&nfsi->rwsem);
4752 request->fl_flags = fl_flags;
4756 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4758 struct nfs4_exception exception = {
4760 .inode = state->inode,
4765 err = _nfs4_proc_setlk(state, cmd, request);
4766 if (err == -NFS4ERR_DENIED)
4768 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4770 } while (exception.retry);
4775 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4777 struct nfs_open_context *ctx;
4778 struct nfs4_state *state;
4779 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4782 /* verify open state */
4783 ctx = nfs_file_open_context(filp);
4786 if (request->fl_start < 0 || request->fl_end < 0)
4789 if (IS_GETLK(cmd)) {
4791 return nfs4_proc_getlk(state, F_GETLK, request);
4795 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4798 if (request->fl_type == F_UNLCK) {
4800 return nfs4_proc_unlck(state, cmd, request);
4807 * Don't rely on the VFS having checked the file open mode,
4808 * since it won't do this for flock() locks.
4810 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4812 if (!(filp->f_mode & FMODE_READ))
4816 if (!(filp->f_mode & FMODE_WRITE))
4821 status = nfs4_proc_setlk(state, cmd, request);
4822 if ((status != -EAGAIN) || IS_SETLK(cmd))
4824 timeout = nfs4_set_lock_task_retry(timeout);
4825 status = -ERESTARTSYS;
4828 } while(status < 0);
4832 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4834 struct nfs_server *server = NFS_SERVER(state->inode);
4835 struct nfs4_exception exception = { };
4838 err = nfs4_set_lock_state(state, fl);
4842 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4845 printk(KERN_ERR "NFS: %s: unhandled error "
4846 "%d.\n", __func__, err);
4850 case -NFS4ERR_EXPIRED:
4851 nfs4_schedule_stateid_recovery(server, state);
4852 case -NFS4ERR_STALE_CLIENTID:
4853 case -NFS4ERR_STALE_STATEID:
4854 nfs4_schedule_lease_recovery(server->nfs_client);
4856 case -NFS4ERR_BADSESSION:
4857 case -NFS4ERR_BADSLOT:
4858 case -NFS4ERR_BAD_HIGH_SLOT:
4859 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4860 case -NFS4ERR_DEADSESSION:
4861 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
4865 * The show must go on: exit, but mark the
4866 * stateid as needing recovery.
4868 case -NFS4ERR_DELEG_REVOKED:
4869 case -NFS4ERR_ADMIN_REVOKED:
4870 case -NFS4ERR_BAD_STATEID:
4871 case -NFS4ERR_OPENMODE:
4872 nfs4_schedule_stateid_recovery(server, state);
4877 * User RPCSEC_GSS context has expired.
4878 * We cannot recover this stateid now, so
4879 * skip it and allow recovery thread to
4885 case -NFS4ERR_DENIED:
4886 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4889 case -NFS4ERR_DELAY:
4892 err = nfs4_handle_exception(server, err, &exception);
4893 } while (exception.retry);
4898 struct nfs_release_lockowner_data {
4899 struct nfs4_lock_state *lsp;
4900 struct nfs_server *server;
4901 struct nfs_release_lockowner_args args;
4904 static void nfs4_release_lockowner_release(void *calldata)
4906 struct nfs_release_lockowner_data *data = calldata;
4907 nfs4_free_lock_state(data->server, data->lsp);
4911 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
4912 .rpc_release = nfs4_release_lockowner_release,
4915 int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
4917 struct nfs_server *server = lsp->ls_state->owner->so_server;
4918 struct nfs_release_lockowner_data *data;
4919 struct rpc_message msg = {
4920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4923 if (server->nfs_client->cl_mvops->minor_version != 0)
4925 data = kmalloc(sizeof(*data), GFP_NOFS);
4929 data->server = server;
4930 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4931 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4932 data->args.lock_owner.s_dev = server->s_dev;
4933 msg.rpc_argp = &data->args;
4934 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4938 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4940 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4941 const void *buf, size_t buflen,
4942 int flags, int type)
4944 if (strcmp(key, "") != 0)
4947 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
4950 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4951 void *buf, size_t buflen, int type)
4953 if (strcmp(key, "") != 0)
4956 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
4959 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4960 size_t list_len, const char *name,
4961 size_t name_len, int type)
4963 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
4965 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4968 if (list && len <= list_len)
4969 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
4974 * nfs_fhget will use either the mounted_on_fileid or the fileid
4976 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4978 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4979 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4980 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4981 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
4984 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4985 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
4986 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4990 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
4991 const struct qstr *name,
4992 struct nfs4_fs_locations *fs_locations,
4995 struct nfs_server *server = NFS_SERVER(dir);
4997 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4999 struct nfs4_fs_locations_arg args = {
5000 .dir_fh = NFS_FH(dir),
5005 struct nfs4_fs_locations_res res = {
5006 .fs_locations = fs_locations,
5008 struct rpc_message msg = {
5009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5015 dprintk("%s: start\n", __func__);
5017 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5018 * is not supported */
5019 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5020 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5022 bitmask[0] |= FATTR4_WORD0_FILEID;
5024 nfs_fattr_init(&fs_locations->fattr);
5025 fs_locations->server = server;
5026 fs_locations->nlocations = 0;
5027 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
5028 dprintk("%s: returned status = %d\n", __func__, status);
5032 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5033 const struct qstr *name,
5034 struct nfs4_fs_locations *fs_locations,
5037 struct nfs4_exception exception = { };
5040 err = nfs4_handle_exception(NFS_SERVER(dir),
5041 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
5043 } while (exception.retry);
5047 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5050 struct nfs4_secinfo_arg args = {
5051 .dir_fh = NFS_FH(dir),
5054 struct nfs4_secinfo_res res = {
5057 struct rpc_message msg = {
5058 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5063 dprintk("NFS call secinfo %s\n", name->name);
5064 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5065 dprintk("NFS reply secinfo: %d\n", status);
5069 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5070 struct nfs4_secinfo_flavors *flavors)
5072 struct nfs4_exception exception = { };
5075 err = nfs4_handle_exception(NFS_SERVER(dir),
5076 _nfs4_proc_secinfo(dir, name, flavors),
5078 } while (exception.retry);
5082 #ifdef CONFIG_NFS_V4_1
5084 * Check the exchange flags returned by the server for invalid flags, having
5085 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5088 static int nfs4_check_cl_exchange_flags(u32 flags)
5090 if (flags & ~EXCHGID4_FLAG_MASK_R)
5092 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5093 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5095 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5099 return -NFS4ERR_INVAL;
5103 nfs41_same_server_scope(struct nfs41_server_scope *a,
5104 struct nfs41_server_scope *b)
5106 if (a->server_scope_sz == b->server_scope_sz &&
5107 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5114 * nfs4_proc_bind_conn_to_session()
5116 * The 4.1 client currently uses the same TCP connection for the
5117 * fore and backchannel.
5119 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
5122 struct nfs41_bind_conn_to_session_res res;
5123 struct rpc_message msg = {
5125 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5131 dprintk("--> %s\n", __func__);
5132 BUG_ON(clp == NULL);
5134 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5135 if (unlikely(res.session == NULL)) {
5140 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5142 if (memcmp(res.session->sess_id.data,
5143 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5144 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5148 if (res.dir != NFS4_CDFS4_BOTH) {
5149 dprintk("NFS: %s: Unexpected direction from server\n",
5154 if (res.use_conn_in_rdma_mode) {
5155 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5164 dprintk("<-- %s status= %d\n", __func__, status);
5169 * nfs4_proc_exchange_id()
5171 * Since the clientid has expired, all compounds using sessions
5172 * associated with the stale clientid will be returning
5173 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5174 * be in some phase of session reset.
5176 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
5178 nfs4_verifier verifier;
5179 struct nfs41_exchange_id_args args = {
5180 .verifier = &verifier,
5182 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
5184 struct nfs41_exchange_id_res res = {
5188 struct rpc_message msg = {
5189 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5195 dprintk("--> %s\n", __func__);
5196 BUG_ON(clp == NULL);
5198 nfs4_init_boot_verifier(clp, &verifier);
5200 args.id_len = scnprintf(args.id, sizeof(args.id),
5203 clp->cl_rpcclient->cl_nodename,
5204 clp->cl_rpcclient->cl_auth->au_flavor);
5206 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
5208 if (unlikely(res.server_owner == NULL)) {
5213 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
5215 if (unlikely(res.server_scope == NULL)) {
5217 goto out_server_owner;
5220 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
5221 if (unlikely(res.impl_id == NULL)) {
5223 goto out_server_scope;
5226 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5228 status = nfs4_check_cl_exchange_flags(res.flags);
5231 clp->cl_clientid = res.clientid;
5232 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5233 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5234 clp->cl_seqid = res.seqid;
5236 kfree(clp->cl_serverowner);
5237 clp->cl_serverowner = res.server_owner;
5238 res.server_owner = NULL;
5240 /* use the most recent implementation id */
5241 kfree(clp->cl_implid);
5242 clp->cl_implid = res.impl_id;
5244 if (clp->cl_serverscope != NULL &&
5245 !nfs41_same_server_scope(clp->cl_serverscope,
5246 res.server_scope)) {
5247 dprintk("%s: server_scope mismatch detected\n",
5249 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5250 kfree(clp->cl_serverscope);
5251 clp->cl_serverscope = NULL;
5254 if (clp->cl_serverscope == NULL) {
5255 clp->cl_serverscope = res.server_scope;
5262 kfree(res.server_owner);
5264 kfree(res.server_scope);
5266 if (clp->cl_implid != NULL)
5267 dprintk("%s: Server Implementation ID: "
5268 "domain: %s, name: %s, date: %llu,%u\n",
5269 __func__, clp->cl_implid->domain, clp->cl_implid->name,
5270 clp->cl_implid->date.seconds,
5271 clp->cl_implid->date.nseconds);
5272 dprintk("<-- %s status= %d\n", __func__, status);
5276 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5277 struct rpc_cred *cred)
5279 struct rpc_message msg = {
5280 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5286 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5288 dprintk("NFS: Got error %d from the server %s on "
5289 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5293 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5294 struct rpc_cred *cred)
5299 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5300 ret = _nfs4_proc_destroy_clientid(clp, cred);
5302 case -NFS4ERR_DELAY:
5303 case -NFS4ERR_CLIENTID_BUSY:
5313 int nfs4_destroy_clientid(struct nfs_client *clp)
5315 struct rpc_cred *cred;
5318 if (clp->cl_mvops->minor_version < 1)
5320 if (clp->cl_exchange_flags == 0)
5322 cred = nfs4_get_exchange_id_cred(clp);
5323 ret = nfs4_proc_destroy_clientid(clp, cred);
5328 case -NFS4ERR_STALE_CLIENTID:
5329 clp->cl_exchange_flags = 0;
5335 struct nfs4_get_lease_time_data {
5336 struct nfs4_get_lease_time_args *args;
5337 struct nfs4_get_lease_time_res *res;
5338 struct nfs_client *clp;
5341 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5345 struct nfs4_get_lease_time_data *data =
5346 (struct nfs4_get_lease_time_data *)calldata;
5348 dprintk("--> %s\n", __func__);
5349 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5350 /* just setup sequence, do not trigger session recovery
5351 since we're invoked within one */
5352 ret = nfs41_setup_sequence(data->clp->cl_session,
5353 &data->args->la_seq_args,
5354 &data->res->lr_seq_res, task);
5356 BUG_ON(ret == -EAGAIN);
5357 rpc_call_start(task);
5358 dprintk("<-- %s\n", __func__);
5362 * Called from nfs4_state_manager thread for session setup, so don't recover
5363 * from sequence operation or clientid errors.
5365 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5367 struct nfs4_get_lease_time_data *data =
5368 (struct nfs4_get_lease_time_data *)calldata;
5370 dprintk("--> %s\n", __func__);
5371 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5373 switch (task->tk_status) {
5374 case -NFS4ERR_DELAY:
5375 case -NFS4ERR_GRACE:
5376 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5377 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5378 task->tk_status = 0;
5380 case -NFS4ERR_RETRY_UNCACHED_REP:
5381 rpc_restart_call_prepare(task);
5384 dprintk("<-- %s\n", __func__);
5387 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5388 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5389 .rpc_call_done = nfs4_get_lease_time_done,
5392 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5394 struct rpc_task *task;
5395 struct nfs4_get_lease_time_args args;
5396 struct nfs4_get_lease_time_res res = {
5397 .lr_fsinfo = fsinfo,
5399 struct nfs4_get_lease_time_data data = {
5404 struct rpc_message msg = {
5405 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5409 struct rpc_task_setup task_setup = {
5410 .rpc_client = clp->cl_rpcclient,
5411 .rpc_message = &msg,
5412 .callback_ops = &nfs4_get_lease_time_ops,
5413 .callback_data = &data,
5414 .flags = RPC_TASK_TIMEOUT,
5418 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5419 dprintk("--> %s\n", __func__);
5420 task = rpc_run_task(&task_setup);
5423 status = PTR_ERR(task);
5425 status = task->tk_status;
5428 dprintk("<-- %s return %d\n", __func__, status);
5433 static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5435 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5438 static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5439 struct nfs4_slot *new,
5443 struct nfs4_slot *old = NULL;
5446 spin_lock(&tbl->slot_tbl_lock);
5450 tbl->max_slots = max_slots;
5452 tbl->highest_used_slotid = -1; /* no slot is currently used */
5453 for (i = 0; i < tbl->max_slots; i++)
5454 tbl->slots[i].seq_nr = ivalue;
5455 spin_unlock(&tbl->slot_tbl_lock);
5460 * (re)Initialise a slot table
5462 static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5465 struct nfs4_slot *new = NULL;
5468 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5469 max_reqs, tbl->max_slots);
5471 /* Does the newly negotiated max_reqs match the existing slot table? */
5472 if (max_reqs != tbl->max_slots) {
5473 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
5479 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
5480 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5481 tbl, tbl->slots, tbl->max_slots);
5483 dprintk("<-- %s: return %d\n", __func__, ret);
5487 /* Destroy the slot table */
5488 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5490 if (session->fc_slot_table.slots != NULL) {
5491 kfree(session->fc_slot_table.slots);
5492 session->fc_slot_table.slots = NULL;
5494 if (session->bc_slot_table.slots != NULL) {
5495 kfree(session->bc_slot_table.slots);
5496 session->bc_slot_table.slots = NULL;
5502 * Initialize or reset the forechannel and backchannel tables
5504 static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
5506 struct nfs4_slot_table *tbl;
5509 dprintk("--> %s\n", __func__);
5511 tbl = &ses->fc_slot_table;
5512 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5513 if (status) /* -ENOMEM */
5516 tbl = &ses->bc_slot_table;
5517 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5518 if (status && tbl->slots == NULL)
5519 /* Fore and back channel share a connection so get
5520 * both slot tables or neither */
5521 nfs4_destroy_slot_tables(ses);
5525 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5527 struct nfs4_session *session;
5528 struct nfs4_slot_table *tbl;
5530 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5534 tbl = &session->fc_slot_table;
5535 tbl->highest_used_slotid = NFS4_NO_SLOT;
5536 spin_lock_init(&tbl->slot_tbl_lock);
5537 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5538 init_completion(&tbl->complete);
5540 tbl = &session->bc_slot_table;
5541 tbl->highest_used_slotid = NFS4_NO_SLOT;
5542 spin_lock_init(&tbl->slot_tbl_lock);
5543 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5544 init_completion(&tbl->complete);
5546 session->session_state = 1<<NFS4_SESSION_INITING;
5552 void nfs4_destroy_session(struct nfs4_session *session)
5554 struct rpc_xprt *xprt;
5555 struct rpc_cred *cred;
5557 cred = nfs4_get_exchange_id_cred(session->clp);
5558 nfs4_proc_destroy_session(session, cred);
5563 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5565 dprintk("%s Destroy backchannel for xprt %p\n",
5567 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
5568 nfs4_destroy_slot_tables(session);
5573 * Initialize the values to be used by the client in CREATE_SESSION
5574 * If nfs4_init_session set the fore channel request and response sizes,
5577 * Set the back channel max_resp_sz_cached to zero to force the client to
5578 * always set csa_cachethis to FALSE because the current implementation
5579 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5581 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5583 struct nfs4_session *session = args->client->cl_session;
5584 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5585 mxresp_sz = session->fc_attrs.max_resp_sz;
5588 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5590 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5591 /* Fore channel attributes */
5592 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5593 args->fc_attrs.max_resp_sz = mxresp_sz;
5594 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5595 args->fc_attrs.max_reqs = max_session_slots;
5597 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5598 "max_ops=%u max_reqs=%u\n",
5600 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5601 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5603 /* Back channel attributes */
5604 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5605 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5606 args->bc_attrs.max_resp_sz_cached = 0;
5607 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5608 args->bc_attrs.max_reqs = 1;
5610 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5611 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5613 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5614 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5615 args->bc_attrs.max_reqs);
5618 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5620 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5621 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5623 if (rcvd->max_resp_sz > sent->max_resp_sz)
5626 * Our requested max_ops is the minimum we need; we're not
5627 * prepared to break up compounds into smaller pieces than that.
5628 * So, no point even trying to continue if the server won't
5631 if (rcvd->max_ops < sent->max_ops)
5633 if (rcvd->max_reqs == 0)
5635 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5636 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5640 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5642 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5643 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5645 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5647 if (rcvd->max_resp_sz < sent->max_resp_sz)
5649 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5651 /* These would render the backchannel useless: */
5652 if (rcvd->max_ops != sent->max_ops)
5654 if (rcvd->max_reqs != sent->max_reqs)
5659 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5660 struct nfs4_session *session)
5664 ret = nfs4_verify_fore_channel_attrs(args, session);
5667 return nfs4_verify_back_channel_attrs(args, session);
5670 static int _nfs4_proc_create_session(struct nfs_client *clp,
5671 struct rpc_cred *cred)
5673 struct nfs4_session *session = clp->cl_session;
5674 struct nfs41_create_session_args args = {
5676 .cb_program = NFS4_CALLBACK,
5678 struct nfs41_create_session_res res = {
5681 struct rpc_message msg = {
5682 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5689 nfs4_init_channel_attrs(&args);
5690 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5692 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5695 /* Verify the session's negotiated channel_attrs values */
5696 status = nfs4_verify_channel_attrs(&args, session);
5698 /* Increment the clientid slot sequence id */
5706 * Issues a CREATE_SESSION operation to the server.
5707 * It is the responsibility of the caller to verify the session is
5708 * expired before calling this routine.
5710 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
5714 struct nfs4_session *session = clp->cl_session;
5716 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5718 status = _nfs4_proc_create_session(clp, cred);
5722 /* Init or reset the session slot tables */
5723 status = nfs4_setup_session_slot_tables(session);
5724 dprintk("slot table setup returned %d\n", status);
5728 ptr = (unsigned *)&session->sess_id.data[0];
5729 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5730 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5732 dprintk("<-- %s\n", __func__);
5737 * Issue the over-the-wire RPC DESTROY_SESSION.
5738 * The caller must serialize access to this routine.
5740 int nfs4_proc_destroy_session(struct nfs4_session *session,
5741 struct rpc_cred *cred)
5743 struct rpc_message msg = {
5744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5745 .rpc_argp = session,
5750 dprintk("--> nfs4_proc_destroy_session\n");
5752 /* session is still being setup */
5753 if (session->clp->cl_cons_state != NFS_CS_READY)
5756 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5759 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
5760 "Session has been destroyed regardless...\n", status);
5762 dprintk("<-- nfs4_proc_destroy_session\n");
5767 * With sessions, the client is not marked ready until after a
5768 * successful EXCHANGE_ID and CREATE_SESSION.
5770 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5771 * other versions of NFS can be tried.
5773 static int nfs41_check_session_ready(struct nfs_client *clp)
5777 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5778 ret = nfs4_client_recover_expired_lease(clp);
5782 if (clp->cl_cons_state < NFS_CS_READY)
5783 return -EPROTONOSUPPORT;
5788 int nfs4_init_session(struct nfs_server *server)
5790 struct nfs_client *clp = server->nfs_client;
5791 struct nfs4_session *session;
5792 unsigned int rsize, wsize;
5794 if (!nfs4_has_session(clp))
5797 session = clp->cl_session;
5798 spin_lock(&clp->cl_lock);
5799 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5801 rsize = server->rsize;
5803 rsize = NFS_MAX_FILE_IO_SIZE;
5804 wsize = server->wsize;
5806 wsize = NFS_MAX_FILE_IO_SIZE;
5808 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5809 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5811 spin_unlock(&clp->cl_lock);
5813 return nfs41_check_session_ready(clp);
5816 int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
5818 struct nfs4_session *session = clp->cl_session;
5821 spin_lock(&clp->cl_lock);
5822 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5824 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
5825 * DS lease to be equal to the MDS lease.
5827 clp->cl_lease_time = lease_time;
5828 clp->cl_last_renewal = jiffies;
5830 spin_unlock(&clp->cl_lock);
5832 ret = nfs41_check_session_ready(clp);
5835 /* Test for the DS role */
5836 if (!is_ds_client(clp))
5840 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5844 * Renew the cl_session lease.
5846 struct nfs4_sequence_data {
5847 struct nfs_client *clp;
5848 struct nfs4_sequence_args args;
5849 struct nfs4_sequence_res res;
5852 static void nfs41_sequence_release(void *data)
5854 struct nfs4_sequence_data *calldata = data;
5855 struct nfs_client *clp = calldata->clp;
5857 if (atomic_read(&clp->cl_count) > 1)
5858 nfs4_schedule_state_renewal(clp);
5859 nfs_put_client(clp);
5863 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5865 switch(task->tk_status) {
5866 case -NFS4ERR_DELAY:
5867 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5870 nfs4_schedule_lease_recovery(clp);
5875 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5877 struct nfs4_sequence_data *calldata = data;
5878 struct nfs_client *clp = calldata->clp;
5880 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5883 if (task->tk_status < 0) {
5884 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5885 if (atomic_read(&clp->cl_count) == 1)
5888 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5889 rpc_restart_call_prepare(task);
5893 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5895 dprintk("<-- %s\n", __func__);
5898 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5900 struct nfs4_sequence_data *calldata = data;
5901 struct nfs_client *clp = calldata->clp;
5902 struct nfs4_sequence_args *args;
5903 struct nfs4_sequence_res *res;
5905 args = task->tk_msg.rpc_argp;
5906 res = task->tk_msg.rpc_resp;
5908 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
5910 rpc_call_start(task);
5913 static const struct rpc_call_ops nfs41_sequence_ops = {
5914 .rpc_call_done = nfs41_sequence_call_done,
5915 .rpc_call_prepare = nfs41_sequence_prepare,
5916 .rpc_release = nfs41_sequence_release,
5919 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5921 struct nfs4_sequence_data *calldata;
5922 struct rpc_message msg = {
5923 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5926 struct rpc_task_setup task_setup_data = {
5927 .rpc_client = clp->cl_rpcclient,
5928 .rpc_message = &msg,
5929 .callback_ops = &nfs41_sequence_ops,
5930 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5933 if (!atomic_inc_not_zero(&clp->cl_count))
5934 return ERR_PTR(-EIO);
5935 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5936 if (calldata == NULL) {
5937 nfs_put_client(clp);
5938 return ERR_PTR(-ENOMEM);
5940 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
5941 msg.rpc_argp = &calldata->args;
5942 msg.rpc_resp = &calldata->res;
5943 calldata->clp = clp;
5944 task_setup_data.callback_data = calldata;
5946 return rpc_run_task(&task_setup_data);
5949 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5951 struct rpc_task *task;
5954 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5956 task = _nfs41_proc_sequence(clp, cred);
5958 ret = PTR_ERR(task);
5960 rpc_put_task_async(task);
5961 dprintk("<-- %s status=%d\n", __func__, ret);
5965 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5967 struct rpc_task *task;
5970 task = _nfs41_proc_sequence(clp, cred);
5972 ret = PTR_ERR(task);
5975 ret = rpc_wait_for_completion_task(task);
5977 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5979 if (task->tk_status == 0)
5980 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5981 ret = task->tk_status;
5985 dprintk("<-- %s status=%d\n", __func__, ret);
5989 struct nfs4_reclaim_complete_data {
5990 struct nfs_client *clp;
5991 struct nfs41_reclaim_complete_args arg;
5992 struct nfs41_reclaim_complete_res res;
5995 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5997 struct nfs4_reclaim_complete_data *calldata = data;
5999 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6000 if (nfs41_setup_sequence(calldata->clp->cl_session,
6001 &calldata->arg.seq_args,
6002 &calldata->res.seq_res, task))
6005 rpc_call_start(task);
6008 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6010 switch(task->tk_status) {
6012 case -NFS4ERR_COMPLETE_ALREADY:
6013 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6015 case -NFS4ERR_DELAY:
6016 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6018 case -NFS4ERR_RETRY_UNCACHED_REP:
6021 nfs4_schedule_lease_recovery(clp);
6026 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6028 struct nfs4_reclaim_complete_data *calldata = data;
6029 struct nfs_client *clp = calldata->clp;
6030 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6032 dprintk("--> %s\n", __func__);
6033 if (!nfs41_sequence_done(task, res))
6036 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6037 rpc_restart_call_prepare(task);
6040 dprintk("<-- %s\n", __func__);
6043 static void nfs4_free_reclaim_complete_data(void *data)
6045 struct nfs4_reclaim_complete_data *calldata = data;
6050 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6051 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6052 .rpc_call_done = nfs4_reclaim_complete_done,
6053 .rpc_release = nfs4_free_reclaim_complete_data,
6057 * Issue a global reclaim complete.
6059 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6061 struct nfs4_reclaim_complete_data *calldata;
6062 struct rpc_task *task;
6063 struct rpc_message msg = {
6064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6066 struct rpc_task_setup task_setup_data = {
6067 .rpc_client = clp->cl_rpcclient,
6068 .rpc_message = &msg,
6069 .callback_ops = &nfs4_reclaim_complete_call_ops,
6070 .flags = RPC_TASK_ASYNC,
6072 int status = -ENOMEM;
6074 dprintk("--> %s\n", __func__);
6075 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6076 if (calldata == NULL)
6078 calldata->clp = clp;
6079 calldata->arg.one_fs = 0;
6081 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
6082 msg.rpc_argp = &calldata->arg;
6083 msg.rpc_resp = &calldata->res;
6084 task_setup_data.callback_data = calldata;
6085 task = rpc_run_task(&task_setup_data);
6087 status = PTR_ERR(task);
6090 status = nfs4_wait_for_completion_rpc_task(task);
6092 status = task->tk_status;
6096 dprintk("<-- %s status=%d\n", __func__, status);
6101 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6103 struct nfs4_layoutget *lgp = calldata;
6104 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6106 dprintk("--> %s\n", __func__);
6107 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6108 * right now covering the LAYOUTGET we are about to send.
6109 * However, that is not so catastrophic, and there seems
6110 * to be no way to prevent it completely.
6112 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
6113 &lgp->res.seq_res, task))
6115 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6116 NFS_I(lgp->args.inode)->layout,
6117 lgp->args.ctx->state)) {
6118 rpc_exit(task, NFS4_OK);
6121 rpc_call_start(task);
6124 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6126 struct nfs4_layoutget *lgp = calldata;
6127 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6129 dprintk("--> %s\n", __func__);
6131 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6134 switch (task->tk_status) {
6137 case -NFS4ERR_LAYOUTTRYLATER:
6138 case -NFS4ERR_RECALLCONFLICT:
6139 task->tk_status = -NFS4ERR_DELAY;
6142 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6143 rpc_restart_call_prepare(task);
6147 dprintk("<-- %s\n", __func__);
6150 static void nfs4_layoutget_release(void *calldata)
6152 struct nfs4_layoutget *lgp = calldata;
6154 dprintk("--> %s\n", __func__);
6155 put_nfs_open_context(lgp->args.ctx);
6157 dprintk("<-- %s\n", __func__);
6160 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6161 .rpc_call_prepare = nfs4_layoutget_prepare,
6162 .rpc_call_done = nfs4_layoutget_done,
6163 .rpc_release = nfs4_layoutget_release,
6166 int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
6168 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6169 struct rpc_task *task;
6170 struct rpc_message msg = {
6171 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6172 .rpc_argp = &lgp->args,
6173 .rpc_resp = &lgp->res,
6175 struct rpc_task_setup task_setup_data = {
6176 .rpc_client = server->client,
6177 .rpc_message = &msg,
6178 .callback_ops = &nfs4_layoutget_call_ops,
6179 .callback_data = lgp,
6180 .flags = RPC_TASK_ASYNC,
6184 dprintk("--> %s\n", __func__);
6186 lgp->res.layoutp = &lgp->args.layout;
6187 lgp->res.seq_res.sr_slot = NULL;
6188 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
6189 task = rpc_run_task(&task_setup_data);
6191 return PTR_ERR(task);
6192 status = nfs4_wait_for_completion_rpc_task(task);
6194 status = task->tk_status;
6196 status = pnfs_layout_process(lgp);
6198 dprintk("<-- %s status=%d\n", __func__, status);
6203 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6205 struct nfs4_layoutreturn *lrp = calldata;
6207 dprintk("--> %s\n", __func__);
6208 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
6209 &lrp->res.seq_res, task))
6211 rpc_call_start(task);
6214 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6216 struct nfs4_layoutreturn *lrp = calldata;
6217 struct nfs_server *server;
6218 struct pnfs_layout_hdr *lo = lrp->args.layout;
6220 dprintk("--> %s\n", __func__);
6222 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6225 server = NFS_SERVER(lrp->args.inode);
6226 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6227 rpc_restart_call_prepare(task);
6230 spin_lock(&lo->plh_inode->i_lock);
6231 if (task->tk_status == 0) {
6232 if (lrp->res.lrs_present) {
6233 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6235 BUG_ON(!list_empty(&lo->plh_segs));
6237 lo->plh_block_lgets--;
6238 spin_unlock(&lo->plh_inode->i_lock);
6239 dprintk("<-- %s\n", __func__);
6242 static void nfs4_layoutreturn_release(void *calldata)
6244 struct nfs4_layoutreturn *lrp = calldata;
6246 dprintk("--> %s\n", __func__);
6247 put_layout_hdr(lrp->args.layout);
6249 dprintk("<-- %s\n", __func__);
6252 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6253 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6254 .rpc_call_done = nfs4_layoutreturn_done,
6255 .rpc_release = nfs4_layoutreturn_release,
6258 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6260 struct rpc_task *task;
6261 struct rpc_message msg = {
6262 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6263 .rpc_argp = &lrp->args,
6264 .rpc_resp = &lrp->res,
6266 struct rpc_task_setup task_setup_data = {
6267 .rpc_client = lrp->clp->cl_rpcclient,
6268 .rpc_message = &msg,
6269 .callback_ops = &nfs4_layoutreturn_call_ops,
6270 .callback_data = lrp,
6274 dprintk("--> %s\n", __func__);
6275 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
6276 task = rpc_run_task(&task_setup_data);
6278 return PTR_ERR(task);
6279 status = task->tk_status;
6280 dprintk("<-- %s status=%d\n", __func__, status);
6286 * Retrieve the list of Data Server devices from the MDS.
6288 static int _nfs4_getdevicelist(struct nfs_server *server,
6289 const struct nfs_fh *fh,
6290 struct pnfs_devicelist *devlist)
6292 struct nfs4_getdevicelist_args args = {
6294 .layoutclass = server->pnfs_curr_ld->id,
6296 struct nfs4_getdevicelist_res res = {
6299 struct rpc_message msg = {
6300 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6306 dprintk("--> %s\n", __func__);
6307 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6309 dprintk("<-- %s status=%d\n", __func__, status);
6313 int nfs4_proc_getdevicelist(struct nfs_server *server,
6314 const struct nfs_fh *fh,
6315 struct pnfs_devicelist *devlist)
6317 struct nfs4_exception exception = { };
6321 err = nfs4_handle_exception(server,
6322 _nfs4_getdevicelist(server, fh, devlist),
6324 } while (exception.retry);
6326 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6327 err, devlist->num_devs);
6331 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6334 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6336 struct nfs4_getdeviceinfo_args args = {
6339 struct nfs4_getdeviceinfo_res res = {
6342 struct rpc_message msg = {
6343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6349 dprintk("--> %s\n", __func__);
6350 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6351 dprintk("<-- %s status=%d\n", __func__, status);
6356 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6358 struct nfs4_exception exception = { };
6362 err = nfs4_handle_exception(server,
6363 _nfs4_proc_getdeviceinfo(server, pdev),
6365 } while (exception.retry);
6368 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6370 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6372 struct nfs4_layoutcommit_data *data = calldata;
6373 struct nfs_server *server = NFS_SERVER(data->args.inode);
6375 if (nfs4_setup_sequence(server, &data->args.seq_args,
6376 &data->res.seq_res, task))
6378 rpc_call_start(task);
6382 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6384 struct nfs4_layoutcommit_data *data = calldata;
6385 struct nfs_server *server = NFS_SERVER(data->args.inode);
6387 if (!nfs4_sequence_done(task, &data->res.seq_res))
6390 switch (task->tk_status) { /* Just ignore these failures */
6391 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6392 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6393 case -NFS4ERR_BADLAYOUT: /* no layout */
6394 case -NFS4ERR_GRACE: /* loca_recalim always false */
6395 task->tk_status = 0;
6398 nfs_post_op_update_inode_force_wcc(data->args.inode,
6402 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6403 rpc_restart_call_prepare(task);
6409 static void nfs4_layoutcommit_release(void *calldata)
6411 struct nfs4_layoutcommit_data *data = calldata;
6412 struct pnfs_layout_segment *lseg, *tmp;
6413 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
6415 pnfs_cleanup_layoutcommit(data);
6416 /* Matched by references in pnfs_set_layoutcommit */
6417 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6418 list_del_init(&lseg->pls_lc_list);
6419 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6424 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6425 smp_mb__after_clear_bit();
6426 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6428 put_rpccred(data->cred);
6432 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6433 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6434 .rpc_call_done = nfs4_layoutcommit_done,
6435 .rpc_release = nfs4_layoutcommit_release,
6439 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6441 struct rpc_message msg = {
6442 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6443 .rpc_argp = &data->args,
6444 .rpc_resp = &data->res,
6445 .rpc_cred = data->cred,
6447 struct rpc_task_setup task_setup_data = {
6448 .task = &data->task,
6449 .rpc_client = NFS_CLIENT(data->args.inode),
6450 .rpc_message = &msg,
6451 .callback_ops = &nfs4_layoutcommit_ops,
6452 .callback_data = data,
6453 .flags = RPC_TASK_ASYNC,
6455 struct rpc_task *task;
6458 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6459 "lbw: %llu inode %lu\n",
6460 data->task.tk_pid, sync,
6461 data->args.lastbytewritten,
6462 data->args.inode->i_ino);
6464 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6465 task = rpc_run_task(&task_setup_data);
6467 return PTR_ERR(task);
6470 status = nfs4_wait_for_completion_rpc_task(task);
6473 status = task->tk_status;
6475 dprintk("%s: status %d\n", __func__, status);
6481 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6482 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6484 struct nfs41_secinfo_no_name_args args = {
6485 .style = SECINFO_STYLE_CURRENT_FH,
6487 struct nfs4_secinfo_res res = {
6490 struct rpc_message msg = {
6491 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6495 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6499 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6500 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6502 struct nfs4_exception exception = { };
6505 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6508 case -NFS4ERR_WRONGSEC:
6509 case -NFS4ERR_NOTSUPP:
6512 err = nfs4_handle_exception(server, err, &exception);
6514 } while (exception.retry);
6520 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6521 struct nfs_fsinfo *info)
6525 rpc_authflavor_t flavor;
6526 struct nfs4_secinfo_flavors *flavors;
6528 page = alloc_page(GFP_KERNEL);
6534 flavors = page_address(page);
6535 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6538 * Fall back on "guess and check" method if
6539 * the server doesn't support SECINFO_NO_NAME
6541 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6542 err = nfs4_find_root_sec(server, fhandle, info);
6548 flavor = nfs_find_best_sec(flavors);
6550 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6560 static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6563 struct nfs41_test_stateid_args args = {
6566 struct nfs41_test_stateid_res res;
6567 struct rpc_message msg = {
6568 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6573 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6574 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6576 if (status == NFS_OK)
6581 static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6583 struct nfs4_exception exception = { };
6586 err = nfs4_handle_exception(server,
6587 _nfs41_test_stateid(server, stateid),
6589 } while (exception.retry);
6593 static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6595 struct nfs41_free_stateid_args args = {
6598 struct nfs41_free_stateid_res res;
6599 struct rpc_message msg = {
6600 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6605 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6606 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6609 static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6611 struct nfs4_exception exception = { };
6614 err = nfs4_handle_exception(server,
6615 _nfs4_free_stateid(server, stateid),
6617 } while (exception.retry);
6621 static bool nfs41_match_stateid(const nfs4_stateid *s1,
6622 const nfs4_stateid *s2)
6624 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
6627 if (s1->seqid == s2->seqid)
6629 if (s1->seqid == 0 || s2->seqid == 0)
6635 #endif /* CONFIG_NFS_V4_1 */
6637 static bool nfs4_match_stateid(const nfs4_stateid *s1,
6638 const nfs4_stateid *s2)
6640 return nfs4_stateid_match(s1, s2);
6644 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6645 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6646 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6647 .recover_open = nfs4_open_reclaim,
6648 .recover_lock = nfs4_lock_reclaim,
6649 .establish_clid = nfs4_init_clientid,
6650 .get_clid_cred = nfs4_get_setclientid_cred,
6653 #if defined(CONFIG_NFS_V4_1)
6654 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6655 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6656 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6657 .recover_open = nfs4_open_reclaim,
6658 .recover_lock = nfs4_lock_reclaim,
6659 .establish_clid = nfs41_init_clientid,
6660 .get_clid_cred = nfs4_get_exchange_id_cred,
6661 .reclaim_complete = nfs41_proc_reclaim_complete,
6663 #endif /* CONFIG_NFS_V4_1 */
6665 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6666 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6667 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6668 .recover_open = nfs4_open_expired,
6669 .recover_lock = nfs4_lock_expired,
6670 .establish_clid = nfs4_init_clientid,
6671 .get_clid_cred = nfs4_get_setclientid_cred,
6674 #if defined(CONFIG_NFS_V4_1)
6675 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6676 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6677 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6678 .recover_open = nfs41_open_expired,
6679 .recover_lock = nfs41_lock_expired,
6680 .establish_clid = nfs41_init_clientid,
6681 .get_clid_cred = nfs4_get_exchange_id_cred,
6683 #endif /* CONFIG_NFS_V4_1 */
6685 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6686 .sched_state_renewal = nfs4_proc_async_renew,
6687 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6688 .renew_lease = nfs4_proc_renew,
6691 #if defined(CONFIG_NFS_V4_1)
6692 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6693 .sched_state_renewal = nfs41_proc_async_sequence,
6694 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
6695 .renew_lease = nfs4_proc_sequence,
6699 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6701 .call_sync = _nfs4_call_sync,
6702 .match_stateid = nfs4_match_stateid,
6703 .find_root_sec = nfs4_find_root_sec,
6704 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6705 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6706 .state_renewal_ops = &nfs40_state_renewal_ops,
6709 #if defined(CONFIG_NFS_V4_1)
6710 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6712 .call_sync = _nfs4_call_sync_session,
6713 .match_stateid = nfs41_match_stateid,
6714 .find_root_sec = nfs41_find_root_sec,
6715 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6716 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6717 .state_renewal_ops = &nfs41_state_renewal_ops,
6721 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6722 [0] = &nfs_v4_0_minor_ops,
6723 #if defined(CONFIG_NFS_V4_1)
6724 [1] = &nfs_v4_1_minor_ops,
6728 static const struct inode_operations nfs4_file_inode_operations = {
6729 .permission = nfs_permission,
6730 .getattr = nfs_getattr,
6731 .setattr = nfs_setattr,
6732 .getxattr = generic_getxattr,
6733 .setxattr = generic_setxattr,
6734 .listxattr = generic_listxattr,
6735 .removexattr = generic_removexattr,
6738 const struct nfs_rpc_ops nfs_v4_clientops = {
6739 .version = 4, /* protocol version */
6740 .dentry_ops = &nfs4_dentry_operations,
6741 .dir_inode_ops = &nfs4_dir_inode_operations,
6742 .file_inode_ops = &nfs4_file_inode_operations,
6743 .file_ops = &nfs4_file_operations,
6744 .getroot = nfs4_proc_get_root,
6745 .submount = nfs4_submount,
6746 .getattr = nfs4_proc_getattr,
6747 .setattr = nfs4_proc_setattr,
6748 .lookup = nfs4_proc_lookup,
6749 .access = nfs4_proc_access,
6750 .readlink = nfs4_proc_readlink,
6751 .create = nfs4_proc_create,
6752 .remove = nfs4_proc_remove,
6753 .unlink_setup = nfs4_proc_unlink_setup,
6754 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
6755 .unlink_done = nfs4_proc_unlink_done,
6756 .rename = nfs4_proc_rename,
6757 .rename_setup = nfs4_proc_rename_setup,
6758 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
6759 .rename_done = nfs4_proc_rename_done,
6760 .link = nfs4_proc_link,
6761 .symlink = nfs4_proc_symlink,
6762 .mkdir = nfs4_proc_mkdir,
6763 .rmdir = nfs4_proc_remove,
6764 .readdir = nfs4_proc_readdir,
6765 .mknod = nfs4_proc_mknod,
6766 .statfs = nfs4_proc_statfs,
6767 .fsinfo = nfs4_proc_fsinfo,
6768 .pathconf = nfs4_proc_pathconf,
6769 .set_capabilities = nfs4_server_capabilities,
6770 .decode_dirent = nfs4_decode_dirent,
6771 .read_setup = nfs4_proc_read_setup,
6772 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
6773 .read_done = nfs4_read_done,
6774 .write_setup = nfs4_proc_write_setup,
6775 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
6776 .write_done = nfs4_write_done,
6777 .commit_setup = nfs4_proc_commit_setup,
6778 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
6779 .commit_done = nfs4_commit_done,
6780 .lock = nfs4_proc_lock,
6781 .clear_acl_cache = nfs4_zap_acl_attr,
6782 .close_context = nfs4_close_context,
6783 .open_context = nfs4_atomic_open,
6784 .init_client = nfs4_init_client,
6787 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6788 .prefix = XATTR_NAME_NFSV4_ACL,
6789 .list = nfs4_xattr_list_nfs4_acl,
6790 .get = nfs4_xattr_get_nfs4_acl,
6791 .set = nfs4_xattr_set_nfs4_acl,
6794 const struct xattr_handler *nfs4_xattr_handlers[] = {
6795 &nfs4_xattr_nfs4_acl_handler,
6799 module_param(max_session_slots, ushort, 0644);
6800 MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6801 "requests the client will negotiate");