4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/nfs_idmap.h>
55 #include <linux/sunrpc/bc_xprt.h>
56 #include <linux/xattr.h>
57 #include <linux/utsname.h>
58 #include <linux/freezer.h>
61 #include "delegation.h"
68 #define NFSDBG_FACILITY NFSDBG_PROC
70 #define NFS4_POLL_RETRY_MIN (HZ/10)
71 #define NFS4_POLL_RETRY_MAX (15*HZ)
73 #define NFS4_MAX_LOOP_ON_RECOVER (10)
76 static int _nfs4_proc_open(struct nfs4_opendata *data);
77 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
78 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
79 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
80 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
81 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
82 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
83 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
84 struct nfs_fattr *fattr, struct iattr *sattr,
85 struct nfs4_state *state);
86 #ifdef CONFIG_NFS_V4_1
87 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
88 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
90 /* Prevent leaks of NFSv4 errors into userland */
91 static int nfs4_map_errors(int err)
96 case -NFS4ERR_RESOURCE:
98 case -NFS4ERR_WRONGSEC:
100 case -NFS4ERR_BADOWNER:
101 case -NFS4ERR_BADNAME:
103 case -NFS4ERR_SHARE_DENIED:
105 case -NFS4ERR_MINOR_VERS_MISMATCH:
106 return -EPROTONOSUPPORT;
107 case -NFS4ERR_ACCESS:
110 dprintk("%s could not handle NFSv4 error %d\n",
118 * This is our standard bitmap for GETATTR requests.
120 const u32 nfs4_fattr_bitmap[3] = {
122 | FATTR4_WORD0_CHANGE
125 | FATTR4_WORD0_FILEID,
127 | FATTR4_WORD1_NUMLINKS
129 | FATTR4_WORD1_OWNER_GROUP
130 | FATTR4_WORD1_RAWDEV
131 | FATTR4_WORD1_SPACE_USED
132 | FATTR4_WORD1_TIME_ACCESS
133 | FATTR4_WORD1_TIME_METADATA
134 | FATTR4_WORD1_TIME_MODIFY
137 static const u32 nfs4_pnfs_open_bitmap[3] = {
139 | FATTR4_WORD0_CHANGE
142 | FATTR4_WORD0_FILEID,
144 | FATTR4_WORD1_NUMLINKS
146 | FATTR4_WORD1_OWNER_GROUP
147 | FATTR4_WORD1_RAWDEV
148 | FATTR4_WORD1_SPACE_USED
149 | FATTR4_WORD1_TIME_ACCESS
150 | FATTR4_WORD1_TIME_METADATA
151 | FATTR4_WORD1_TIME_MODIFY,
152 FATTR4_WORD2_MDSTHRESHOLD
155 const u32 nfs4_statfs_bitmap[2] = {
156 FATTR4_WORD0_FILES_AVAIL
157 | FATTR4_WORD0_FILES_FREE
158 | FATTR4_WORD0_FILES_TOTAL,
159 FATTR4_WORD1_SPACE_AVAIL
160 | FATTR4_WORD1_SPACE_FREE
161 | FATTR4_WORD1_SPACE_TOTAL
164 const u32 nfs4_pathconf_bitmap[2] = {
166 | FATTR4_WORD0_MAXNAME,
170 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
171 | FATTR4_WORD0_MAXREAD
172 | FATTR4_WORD0_MAXWRITE
173 | FATTR4_WORD0_LEASE_TIME,
174 FATTR4_WORD1_TIME_DELTA
175 | FATTR4_WORD1_FS_LAYOUT_TYPES,
176 FATTR4_WORD2_LAYOUT_BLKSIZE
179 const u32 nfs4_fs_locations_bitmap[2] = {
181 | FATTR4_WORD0_CHANGE
184 | FATTR4_WORD0_FILEID
185 | FATTR4_WORD0_FS_LOCATIONS,
187 | FATTR4_WORD1_NUMLINKS
189 | FATTR4_WORD1_OWNER_GROUP
190 | FATTR4_WORD1_RAWDEV
191 | FATTR4_WORD1_SPACE_USED
192 | FATTR4_WORD1_TIME_ACCESS
193 | FATTR4_WORD1_TIME_METADATA
194 | FATTR4_WORD1_TIME_MODIFY
195 | FATTR4_WORD1_MOUNTED_ON_FILEID
198 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
199 struct nfs4_readdir_arg *readdir)
203 BUG_ON(readdir->count < 80);
205 readdir->cookie = cookie;
206 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
211 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
216 * NFSv4 servers do not return entries for '.' and '..'
217 * Therefore, we fake these entries here. We let '.'
218 * have cookie 0 and '..' have cookie 1. Note that
219 * when talking to the server, we always send cookie 0
222 start = p = kmap_atomic(*readdir->pages);
225 *p++ = xdr_one; /* next */
226 *p++ = xdr_zero; /* cookie, first word */
227 *p++ = xdr_one; /* cookie, second word */
228 *p++ = xdr_one; /* entry len */
229 memcpy(p, ".\0\0\0", 4); /* entry */
231 *p++ = xdr_one; /* bitmap length */
232 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
233 *p++ = htonl(8); /* attribute buffer length */
234 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
237 *p++ = xdr_one; /* next */
238 *p++ = xdr_zero; /* cookie, first word */
239 *p++ = xdr_two; /* cookie, second word */
240 *p++ = xdr_two; /* entry len */
241 memcpy(p, "..\0\0", 4); /* entry */
243 *p++ = xdr_one; /* bitmap length */
244 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
245 *p++ = htonl(8); /* attribute buffer length */
246 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
248 readdir->pgbase = (char *)p - (char *)start;
249 readdir->count -= readdir->pgbase;
250 kunmap_atomic(start);
253 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
259 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
260 nfs_wait_bit_killable, TASK_KILLABLE);
264 if (clp->cl_cons_state < 0)
265 return clp->cl_cons_state;
269 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
276 *timeout = NFS4_POLL_RETRY_MIN;
277 if (*timeout > NFS4_POLL_RETRY_MAX)
278 *timeout = NFS4_POLL_RETRY_MAX;
279 freezable_schedule_timeout_killable(*timeout);
280 if (fatal_signal_pending(current))
286 /* This is the error handling routine for processes that are allowed
289 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
291 struct nfs_client *clp = server->nfs_client;
292 struct nfs4_state *state = exception->state;
293 struct inode *inode = exception->inode;
296 exception->retry = 0;
300 case -NFS4ERR_OPENMODE:
301 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
302 nfs4_inode_return_delegation(inode);
303 exception->retry = 1;
308 nfs4_schedule_stateid_recovery(server, state);
309 goto wait_on_recovery;
310 case -NFS4ERR_DELEG_REVOKED:
311 case -NFS4ERR_ADMIN_REVOKED:
312 case -NFS4ERR_BAD_STATEID:
315 nfs_remove_bad_delegation(state->inode);
316 nfs4_schedule_stateid_recovery(server, state);
317 goto wait_on_recovery;
318 case -NFS4ERR_EXPIRED:
320 nfs4_schedule_stateid_recovery(server, state);
321 case -NFS4ERR_STALE_STATEID:
322 case -NFS4ERR_STALE_CLIENTID:
323 nfs4_schedule_lease_recovery(clp);
324 goto wait_on_recovery;
325 #if defined(CONFIG_NFS_V4_1)
326 case -NFS4ERR_BADSESSION:
327 case -NFS4ERR_BADSLOT:
328 case -NFS4ERR_BAD_HIGH_SLOT:
329 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
330 case -NFS4ERR_DEADSESSION:
331 case -NFS4ERR_SEQ_FALSE_RETRY:
332 case -NFS4ERR_SEQ_MISORDERED:
333 dprintk("%s ERROR: %d Reset session\n", __func__,
335 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
336 exception->retry = 1;
338 #endif /* defined(CONFIG_NFS_V4_1) */
339 case -NFS4ERR_FILE_OPEN:
340 if (exception->timeout > HZ) {
341 /* We have retried a decent amount, time to
350 ret = nfs4_delay(server->client, &exception->timeout);
353 case -NFS4ERR_RETRY_UNCACHED_REP:
354 case -NFS4ERR_OLD_STATEID:
355 exception->retry = 1;
357 case -NFS4ERR_BADOWNER:
358 /* The following works around a Linux server bug! */
359 case -NFS4ERR_BADNAME:
360 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
361 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
362 exception->retry = 1;
363 printk(KERN_WARNING "NFS: v4 server %s "
364 "does not accept raw "
366 "Reenabling the idmapper.\n",
367 server->nfs_client->cl_hostname);
370 /* We failed to handle the error */
371 return nfs4_map_errors(ret);
373 ret = nfs4_wait_clnt_recover(clp);
375 exception->retry = 1;
380 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
382 spin_lock(&clp->cl_lock);
383 if (time_before(clp->cl_last_renewal,timestamp))
384 clp->cl_last_renewal = timestamp;
385 spin_unlock(&clp->cl_lock);
388 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
390 do_renew_lease(server->nfs_client, timestamp);
393 #if defined(CONFIG_NFS_V4_1)
396 * nfs4_free_slot - free a slot and efficiently update slot table.
398 * freeing a slot is trivially done by clearing its respective bit
400 * If the freed slotid equals highest_used_slotid we want to update it
401 * so that the server would be able to size down the slot table if needed,
402 * otherwise we know that the highest_used_slotid is still in use.
403 * When updating highest_used_slotid there may be "holes" in the bitmap
404 * so we need to scan down from highest_used_slotid to 0 looking for the now
405 * highest slotid in use.
406 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
408 * Must be called while holding tbl->slot_tbl_lock
411 nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
413 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
414 /* clear used bit in bitmap */
415 __clear_bit(slotid, tbl->used_slots);
417 /* update highest_used_slotid when it is freed */
418 if (slotid == tbl->highest_used_slotid) {
419 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
420 if (slotid < tbl->max_slots)
421 tbl->highest_used_slotid = slotid;
423 tbl->highest_used_slotid = NFS4_NO_SLOT;
425 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
426 slotid, tbl->highest_used_slotid);
429 bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
431 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
436 * Signal state manager thread if session fore channel is drained
438 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
440 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
441 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
442 nfs4_set_task_privileged, NULL);
446 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
449 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
450 complete(&ses->fc_slot_table.complete);
454 * Signal state manager thread if session back channel is drained
456 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
458 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
459 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
461 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
462 complete(&ses->bc_slot_table.complete);
465 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
467 struct nfs4_slot_table *tbl;
469 tbl = &res->sr_session->fc_slot_table;
471 /* just wake up the next guy waiting since
472 * we may have not consumed a slot after all */
473 dprintk("%s: No slot\n", __func__);
477 spin_lock(&tbl->slot_tbl_lock);
478 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
479 nfs4_check_drain_fc_complete(res->sr_session);
480 spin_unlock(&tbl->slot_tbl_lock);
484 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
486 unsigned long timestamp;
487 struct nfs_client *clp;
490 * sr_status remains 1 if an RPC level error occurred. The server
491 * may or may not have processed the sequence operation..
492 * Proceed as if the server received and processed the sequence
495 if (res->sr_status == 1)
496 res->sr_status = NFS_OK;
498 /* don't increment the sequence number if the task wasn't sent */
499 if (!RPC_WAS_SENT(task))
502 /* Check the SEQUENCE operation status */
503 switch (res->sr_status) {
505 /* Update the slot's sequence and clientid lease timer */
506 ++res->sr_slot->seq_nr;
507 timestamp = res->sr_renewal_time;
508 clp = res->sr_session->clp;
509 do_renew_lease(clp, timestamp);
510 /* Check sequence flags */
511 if (res->sr_status_flags != 0)
512 nfs4_schedule_lease_recovery(clp);
515 /* The server detected a resend of the RPC call and
516 * returned NFS4ERR_DELAY as per Section 2.10.6.2
519 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
521 res->sr_slot - res->sr_session->fc_slot_table.slots,
522 res->sr_slot->seq_nr);
525 /* Just update the slot sequence no. */
526 ++res->sr_slot->seq_nr;
529 /* The session may be reset by one of the error handlers. */
530 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
531 nfs41_sequence_free_slot(res);
534 if (!rpc_restart_call(task))
536 rpc_delay(task, NFS4_POLL_RETRY_MAX);
540 static int nfs4_sequence_done(struct rpc_task *task,
541 struct nfs4_sequence_res *res)
543 if (res->sr_session == NULL)
545 return nfs41_sequence_done(task, res);
549 * nfs4_find_slot - efficiently look for a free slot
551 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
552 * If found, we mark the slot as used, update the highest_used_slotid,
553 * and respectively set up the sequence operation args.
554 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
556 * Note: must be called with under the slot_tbl_lock.
559 nfs4_find_slot(struct nfs4_slot_table *tbl)
562 u32 ret_id = NFS4_NO_SLOT;
564 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
565 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
567 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
568 if (slotid >= tbl->max_slots)
570 __set_bit(slotid, tbl->used_slots);
571 if (slotid > tbl->highest_used_slotid ||
572 tbl->highest_used_slotid == NFS4_NO_SLOT)
573 tbl->highest_used_slotid = slotid;
576 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
577 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
581 static void nfs41_init_sequence(struct nfs4_sequence_args *args,
582 struct nfs4_sequence_res *res, int cache_reply)
584 args->sa_session = NULL;
585 args->sa_cache_this = 0;
587 args->sa_cache_this = 1;
588 res->sr_session = NULL;
592 int nfs41_setup_sequence(struct nfs4_session *session,
593 struct nfs4_sequence_args *args,
594 struct nfs4_sequence_res *res,
595 struct rpc_task *task)
597 struct nfs4_slot *slot;
598 struct nfs4_slot_table *tbl;
601 dprintk("--> %s\n", __func__);
602 /* slot already allocated? */
603 if (res->sr_slot != NULL)
606 tbl = &session->fc_slot_table;
608 spin_lock(&tbl->slot_tbl_lock);
609 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
610 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
611 /* The state manager will wait until the slot table is empty */
612 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
613 spin_unlock(&tbl->slot_tbl_lock);
614 dprintk("%s session is draining\n", __func__);
618 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
619 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
620 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
621 spin_unlock(&tbl->slot_tbl_lock);
622 dprintk("%s enforce FIFO order\n", __func__);
626 slotid = nfs4_find_slot(tbl);
627 if (slotid == NFS4_NO_SLOT) {
628 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
629 spin_unlock(&tbl->slot_tbl_lock);
630 dprintk("<-- %s: no free slots\n", __func__);
633 spin_unlock(&tbl->slot_tbl_lock);
635 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
636 slot = tbl->slots + slotid;
637 args->sa_session = session;
638 args->sa_slotid = slotid;
640 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
642 res->sr_session = session;
644 res->sr_renewal_time = jiffies;
645 res->sr_status_flags = 0;
647 * sr_status is only set in decode_sequence, and so will remain
648 * set to 1 if an rpc level failure occurs.
653 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
655 int nfs4_setup_sequence(const struct nfs_server *server,
656 struct nfs4_sequence_args *args,
657 struct nfs4_sequence_res *res,
658 struct rpc_task *task)
660 struct nfs4_session *session = nfs4_get_session(server);
666 dprintk("--> %s clp %p session %p sr_slot %td\n",
667 __func__, session->clp, session, res->sr_slot ?
668 res->sr_slot - session->fc_slot_table.slots : -1);
670 ret = nfs41_setup_sequence(session, args, res, task);
672 dprintk("<-- %s status=%d\n", __func__, ret);
676 struct nfs41_call_sync_data {
677 const struct nfs_server *seq_server;
678 struct nfs4_sequence_args *seq_args;
679 struct nfs4_sequence_res *seq_res;
682 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
684 struct nfs41_call_sync_data *data = calldata;
686 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
688 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
689 data->seq_res, task))
691 rpc_call_start(task);
694 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
696 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
697 nfs41_call_sync_prepare(task, calldata);
700 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
702 struct nfs41_call_sync_data *data = calldata;
704 nfs41_sequence_done(task, data->seq_res);
707 static const struct rpc_call_ops nfs41_call_sync_ops = {
708 .rpc_call_prepare = nfs41_call_sync_prepare,
709 .rpc_call_done = nfs41_call_sync_done,
712 static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
713 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
714 .rpc_call_done = nfs41_call_sync_done,
717 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
718 struct nfs_server *server,
719 struct rpc_message *msg,
720 struct nfs4_sequence_args *args,
721 struct nfs4_sequence_res *res,
725 struct rpc_task *task;
726 struct nfs41_call_sync_data data = {
727 .seq_server = server,
731 struct rpc_task_setup task_setup = {
734 .callback_ops = &nfs41_call_sync_ops,
735 .callback_data = &data
739 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
740 task = rpc_run_task(&task_setup);
744 ret = task->tk_status;
750 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
751 struct nfs_server *server,
752 struct rpc_message *msg,
753 struct nfs4_sequence_args *args,
754 struct nfs4_sequence_res *res,
757 nfs41_init_sequence(args, res, cache_reply);
758 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
763 void nfs41_init_sequence(struct nfs4_sequence_args *args,
764 struct nfs4_sequence_res *res, int cache_reply)
768 static int nfs4_sequence_done(struct rpc_task *task,
769 struct nfs4_sequence_res *res)
773 #endif /* CONFIG_NFS_V4_1 */
775 int _nfs4_call_sync(struct rpc_clnt *clnt,
776 struct nfs_server *server,
777 struct rpc_message *msg,
778 struct nfs4_sequence_args *args,
779 struct nfs4_sequence_res *res,
782 nfs41_init_sequence(args, res, cache_reply);
783 return rpc_call_sync(clnt, msg, 0);
787 int nfs4_call_sync(struct rpc_clnt *clnt,
788 struct nfs_server *server,
789 struct rpc_message *msg,
790 struct nfs4_sequence_args *args,
791 struct nfs4_sequence_res *res,
794 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
795 args, res, cache_reply);
798 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
800 struct nfs_inode *nfsi = NFS_I(dir);
802 spin_lock(&dir->i_lock);
803 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
804 if (!cinfo->atomic || cinfo->before != dir->i_version)
805 nfs_force_lookup_revalidate(dir);
806 dir->i_version = cinfo->after;
807 spin_unlock(&dir->i_lock);
810 struct nfs4_opendata {
812 struct nfs_openargs o_arg;
813 struct nfs_openres o_res;
814 struct nfs_open_confirmargs c_arg;
815 struct nfs_open_confirmres c_res;
816 struct nfs4_string owner_name;
817 struct nfs4_string group_name;
818 struct nfs_fattr f_attr;
820 struct dentry *dentry;
821 struct nfs4_state_owner *owner;
822 struct nfs4_state *state;
824 unsigned long timestamp;
825 unsigned int rpc_done : 1;
831 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
833 p->o_res.f_attr = &p->f_attr;
834 p->o_res.seqid = p->o_arg.seqid;
835 p->c_res.seqid = p->c_arg.seqid;
836 p->o_res.server = p->o_arg.server;
837 nfs_fattr_init(&p->f_attr);
838 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
841 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
842 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
843 const struct iattr *attrs,
846 struct dentry *parent = dget_parent(dentry);
847 struct inode *dir = parent->d_inode;
848 struct nfs_server *server = NFS_SERVER(dir);
849 struct nfs4_opendata *p;
851 p = kzalloc(sizeof(*p), gfp_mask);
854 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
855 if (p->o_arg.seqid == NULL)
857 nfs_sb_active(dentry->d_sb);
858 p->dentry = dget(dentry);
861 atomic_inc(&sp->so_count);
862 p->o_arg.fh = NFS_FH(dir);
863 p->o_arg.open_flags = flags;
864 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
865 /* ask server to check for all possible rights as results are cached */
866 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
867 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
868 p->o_arg.clientid = server->nfs_client->cl_clientid;
869 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
870 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
871 p->o_arg.name = &dentry->d_name;
872 p->o_arg.server = server;
873 p->o_arg.bitmask = server->attr_bitmask;
874 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
875 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
876 if (attrs != NULL && attrs->ia_valid != 0) {
879 p->o_arg.u.attrs = &p->attrs;
880 memcpy(&p->attrs, attrs, sizeof(p->attrs));
883 verf[1] = current->pid;
884 memcpy(p->o_arg.u.verifier.data, verf,
885 sizeof(p->o_arg.u.verifier.data));
887 p->c_arg.fh = &p->o_res.fh;
888 p->c_arg.stateid = &p->o_res.stateid;
889 p->c_arg.seqid = p->o_arg.seqid;
890 nfs4_init_opendata_res(p);
900 static void nfs4_opendata_free(struct kref *kref)
902 struct nfs4_opendata *p = container_of(kref,
903 struct nfs4_opendata, kref);
904 struct super_block *sb = p->dentry->d_sb;
906 nfs_free_seqid(p->o_arg.seqid);
907 if (p->state != NULL)
908 nfs4_put_open_state(p->state);
909 nfs4_put_state_owner(p->owner);
913 nfs_fattr_free_names(&p->f_attr);
917 static void nfs4_opendata_put(struct nfs4_opendata *p)
920 kref_put(&p->kref, nfs4_opendata_free);
923 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
927 ret = rpc_wait_for_completion_task(task);
931 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
935 if (open_mode & (O_EXCL|O_TRUNC))
937 switch (mode & (FMODE_READ|FMODE_WRITE)) {
939 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
940 && state->n_rdonly != 0;
943 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
944 && state->n_wronly != 0;
946 case FMODE_READ|FMODE_WRITE:
947 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
948 && state->n_rdwr != 0;
954 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
956 if (delegation == NULL)
958 if ((delegation->type & fmode) != fmode)
960 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
962 nfs_mark_delegation_referenced(delegation);
966 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
975 case FMODE_READ|FMODE_WRITE:
978 nfs4_state_set_mode_locked(state, state->state | fmode);
981 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
983 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
984 nfs4_stateid_copy(&state->stateid, stateid);
985 nfs4_stateid_copy(&state->open_stateid, stateid);
988 set_bit(NFS_O_RDONLY_STATE, &state->flags);
991 set_bit(NFS_O_WRONLY_STATE, &state->flags);
993 case FMODE_READ|FMODE_WRITE:
994 set_bit(NFS_O_RDWR_STATE, &state->flags);
998 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1000 write_seqlock(&state->seqlock);
1001 nfs_set_open_stateid_locked(state, stateid, fmode);
1002 write_sequnlock(&state->seqlock);
1005 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1008 * Protect the call to nfs4_state_set_mode_locked and
1009 * serialise the stateid update
1011 write_seqlock(&state->seqlock);
1012 if (deleg_stateid != NULL) {
1013 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1014 set_bit(NFS_DELEGATED_STATE, &state->flags);
1016 if (open_stateid != NULL)
1017 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1018 write_sequnlock(&state->seqlock);
1019 spin_lock(&state->owner->so_lock);
1020 update_open_stateflags(state, fmode);
1021 spin_unlock(&state->owner->so_lock);
1024 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1026 struct nfs_inode *nfsi = NFS_I(state->inode);
1027 struct nfs_delegation *deleg_cur;
1030 fmode &= (FMODE_READ|FMODE_WRITE);
1033 deleg_cur = rcu_dereference(nfsi->delegation);
1034 if (deleg_cur == NULL)
1037 spin_lock(&deleg_cur->lock);
1038 if (nfsi->delegation != deleg_cur ||
1039 (deleg_cur->type & fmode) != fmode)
1040 goto no_delegation_unlock;
1042 if (delegation == NULL)
1043 delegation = &deleg_cur->stateid;
1044 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1045 goto no_delegation_unlock;
1047 nfs_mark_delegation_referenced(deleg_cur);
1048 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1050 no_delegation_unlock:
1051 spin_unlock(&deleg_cur->lock);
1055 if (!ret && open_stateid != NULL) {
1056 __update_open_stateid(state, open_stateid, NULL, fmode);
1064 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1066 struct nfs_delegation *delegation;
1069 delegation = rcu_dereference(NFS_I(inode)->delegation);
1070 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1075 nfs4_inode_return_delegation(inode);
1078 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1080 struct nfs4_state *state = opendata->state;
1081 struct nfs_inode *nfsi = NFS_I(state->inode);
1082 struct nfs_delegation *delegation;
1083 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
1084 fmode_t fmode = opendata->o_arg.fmode;
1085 nfs4_stateid stateid;
1089 if (can_open_cached(state, fmode, open_mode)) {
1090 spin_lock(&state->owner->so_lock);
1091 if (can_open_cached(state, fmode, open_mode)) {
1092 update_open_stateflags(state, fmode);
1093 spin_unlock(&state->owner->so_lock);
1094 goto out_return_state;
1096 spin_unlock(&state->owner->so_lock);
1099 delegation = rcu_dereference(nfsi->delegation);
1100 if (!can_open_delegated(delegation, fmode)) {
1104 /* Save the delegation */
1105 nfs4_stateid_copy(&stateid, &delegation->stateid);
1107 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1112 /* Try to update the stateid using the delegation */
1113 if (update_open_stateid(state, NULL, &stateid, fmode))
1114 goto out_return_state;
1117 return ERR_PTR(ret);
1119 atomic_inc(&state->count);
1123 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1125 struct inode *inode;
1126 struct nfs4_state *state = NULL;
1127 struct nfs_delegation *delegation;
1130 if (!data->rpc_done) {
1131 state = nfs4_try_open_cached(data);
1136 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1138 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1139 ret = PTR_ERR(inode);
1143 state = nfs4_get_open_state(inode, data->owner);
1146 if (data->o_res.delegation_type != 0) {
1147 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1148 int delegation_flags = 0;
1151 delegation = rcu_dereference(NFS_I(inode)->delegation);
1153 delegation_flags = delegation->flags;
1155 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1156 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1157 "returning a delegation for "
1158 "OPEN(CLAIM_DELEGATE_CUR)\n",
1160 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1161 nfs_inode_set_delegation(state->inode,
1162 data->owner->so_cred,
1165 nfs_inode_reclaim_delegation(state->inode,
1166 data->owner->so_cred,
1170 update_open_stateid(state, &data->o_res.stateid, NULL,
1178 return ERR_PTR(ret);
1181 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1183 struct nfs_inode *nfsi = NFS_I(state->inode);
1184 struct nfs_open_context *ctx;
1186 spin_lock(&state->inode->i_lock);
1187 list_for_each_entry(ctx, &nfsi->open_files, list) {
1188 if (ctx->state != state)
1190 get_nfs_open_context(ctx);
1191 spin_unlock(&state->inode->i_lock);
1194 spin_unlock(&state->inode->i_lock);
1195 return ERR_PTR(-ENOENT);
1198 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1200 struct nfs4_opendata *opendata;
1202 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1203 if (opendata == NULL)
1204 return ERR_PTR(-ENOMEM);
1205 opendata->state = state;
1206 atomic_inc(&state->count);
1210 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1212 struct nfs4_state *newstate;
1215 opendata->o_arg.open_flags = 0;
1216 opendata->o_arg.fmode = fmode;
1217 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1218 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1219 nfs4_init_opendata_res(opendata);
1220 ret = _nfs4_recover_proc_open(opendata);
1223 newstate = nfs4_opendata_to_nfs4_state(opendata);
1224 if (IS_ERR(newstate))
1225 return PTR_ERR(newstate);
1226 nfs4_close_state(newstate, fmode);
1231 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1233 struct nfs4_state *newstate;
1236 /* memory barrier prior to reading state->n_* */
1237 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1239 if (state->n_rdwr != 0) {
1240 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1241 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1244 if (newstate != state)
1247 if (state->n_wronly != 0) {
1248 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1249 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1252 if (newstate != state)
1255 if (state->n_rdonly != 0) {
1256 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1257 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1260 if (newstate != state)
1264 * We may have performed cached opens for all three recoveries.
1265 * Check if we need to update the current stateid.
1267 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1268 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1269 write_seqlock(&state->seqlock);
1270 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1271 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1272 write_sequnlock(&state->seqlock);
1279 * reclaim state on the server after a reboot.
1281 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1283 struct nfs_delegation *delegation;
1284 struct nfs4_opendata *opendata;
1285 fmode_t delegation_type = 0;
1288 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1289 if (IS_ERR(opendata))
1290 return PTR_ERR(opendata);
1291 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1292 opendata->o_arg.fh = NFS_FH(state->inode);
1294 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1295 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1296 delegation_type = delegation->type;
1298 opendata->o_arg.u.delegation_type = delegation_type;
1299 status = nfs4_open_recover(opendata, state);
1300 nfs4_opendata_put(opendata);
1304 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1306 struct nfs_server *server = NFS_SERVER(state->inode);
1307 struct nfs4_exception exception = { };
1310 err = _nfs4_do_open_reclaim(ctx, state);
1311 if (err != -NFS4ERR_DELAY)
1313 nfs4_handle_exception(server, err, &exception);
1314 } while (exception.retry);
1318 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1320 struct nfs_open_context *ctx;
1323 ctx = nfs4_state_find_open_context(state);
1325 return PTR_ERR(ctx);
1326 ret = nfs4_do_open_reclaim(ctx, state);
1327 put_nfs_open_context(ctx);
1331 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1333 struct nfs4_opendata *opendata;
1336 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1337 if (IS_ERR(opendata))
1338 return PTR_ERR(opendata);
1339 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1340 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1341 ret = nfs4_open_recover(opendata, state);
1342 nfs4_opendata_put(opendata);
1346 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1348 struct nfs4_exception exception = { };
1349 struct nfs_server *server = NFS_SERVER(state->inode);
1352 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1358 case -NFS4ERR_BADSESSION:
1359 case -NFS4ERR_BADSLOT:
1360 case -NFS4ERR_BAD_HIGH_SLOT:
1361 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1362 case -NFS4ERR_DEADSESSION:
1363 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1365 case -NFS4ERR_STALE_CLIENTID:
1366 case -NFS4ERR_STALE_STATEID:
1367 case -NFS4ERR_EXPIRED:
1368 /* Don't recall a delegation if it was lost */
1369 nfs4_schedule_lease_recovery(server->nfs_client);
1373 * The show must go on: exit, but mark the
1374 * stateid as needing recovery.
1376 case -NFS4ERR_DELEG_REVOKED:
1377 case -NFS4ERR_ADMIN_REVOKED:
1378 case -NFS4ERR_BAD_STATEID:
1379 nfs_inode_find_state_and_recover(state->inode,
1381 nfs4_schedule_stateid_recovery(server, state);
1384 * User RPCSEC_GSS context has expired.
1385 * We cannot recover this stateid now, so
1386 * skip it and allow recovery thread to
1393 err = nfs4_handle_exception(server, err, &exception);
1394 } while (exception.retry);
1399 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1401 struct nfs4_opendata *data = calldata;
1403 data->rpc_status = task->tk_status;
1404 if (data->rpc_status == 0) {
1405 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1406 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1407 renew_lease(data->o_res.server, data->timestamp);
1412 static void nfs4_open_confirm_release(void *calldata)
1414 struct nfs4_opendata *data = calldata;
1415 struct nfs4_state *state = NULL;
1417 /* If this request hasn't been cancelled, do nothing */
1418 if (data->cancelled == 0)
1420 /* In case of error, no cleanup! */
1421 if (!data->rpc_done)
1423 state = nfs4_opendata_to_nfs4_state(data);
1425 nfs4_close_state(state, data->o_arg.fmode);
1427 nfs4_opendata_put(data);
1430 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1431 .rpc_call_done = nfs4_open_confirm_done,
1432 .rpc_release = nfs4_open_confirm_release,
1436 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1438 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1440 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1441 struct rpc_task *task;
1442 struct rpc_message msg = {
1443 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1444 .rpc_argp = &data->c_arg,
1445 .rpc_resp = &data->c_res,
1446 .rpc_cred = data->owner->so_cred,
1448 struct rpc_task_setup task_setup_data = {
1449 .rpc_client = server->client,
1450 .rpc_message = &msg,
1451 .callback_ops = &nfs4_open_confirm_ops,
1452 .callback_data = data,
1453 .workqueue = nfsiod_workqueue,
1454 .flags = RPC_TASK_ASYNC,
1458 kref_get(&data->kref);
1460 data->rpc_status = 0;
1461 data->timestamp = jiffies;
1462 task = rpc_run_task(&task_setup_data);
1464 return PTR_ERR(task);
1465 status = nfs4_wait_for_completion_rpc_task(task);
1467 data->cancelled = 1;
1470 status = data->rpc_status;
1475 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1477 struct nfs4_opendata *data = calldata;
1478 struct nfs4_state_owner *sp = data->owner;
1480 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1483 * Check if we still need to send an OPEN call, or if we can use
1484 * a delegation instead.
1486 if (data->state != NULL) {
1487 struct nfs_delegation *delegation;
1489 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1492 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1493 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1494 can_open_delegated(delegation, data->o_arg.fmode))
1495 goto unlock_no_action;
1498 /* Update client id. */
1499 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1500 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1501 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1502 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1504 data->timestamp = jiffies;
1505 if (nfs4_setup_sequence(data->o_arg.server,
1506 &data->o_arg.seq_args,
1507 &data->o_res.seq_res, task))
1509 rpc_call_start(task);
1514 task->tk_action = NULL;
1518 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1520 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1521 nfs4_open_prepare(task, calldata);
1524 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1526 struct nfs4_opendata *data = calldata;
1528 data->rpc_status = task->tk_status;
1530 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1533 if (task->tk_status == 0) {
1534 switch (data->o_res.f_attr->mode & S_IFMT) {
1538 data->rpc_status = -ELOOP;
1541 data->rpc_status = -EISDIR;
1544 data->rpc_status = -ENOTDIR;
1546 renew_lease(data->o_res.server, data->timestamp);
1547 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1548 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1553 static void nfs4_open_release(void *calldata)
1555 struct nfs4_opendata *data = calldata;
1556 struct nfs4_state *state = NULL;
1558 /* If this request hasn't been cancelled, do nothing */
1559 if (data->cancelled == 0)
1561 /* In case of error, no cleanup! */
1562 if (data->rpc_status != 0 || !data->rpc_done)
1564 /* In case we need an open_confirm, no cleanup! */
1565 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1567 state = nfs4_opendata_to_nfs4_state(data);
1569 nfs4_close_state(state, data->o_arg.fmode);
1571 nfs4_opendata_put(data);
1574 static const struct rpc_call_ops nfs4_open_ops = {
1575 .rpc_call_prepare = nfs4_open_prepare,
1576 .rpc_call_done = nfs4_open_done,
1577 .rpc_release = nfs4_open_release,
1580 static const struct rpc_call_ops nfs4_recover_open_ops = {
1581 .rpc_call_prepare = nfs4_recover_open_prepare,
1582 .rpc_call_done = nfs4_open_done,
1583 .rpc_release = nfs4_open_release,
1586 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1588 struct inode *dir = data->dir->d_inode;
1589 struct nfs_server *server = NFS_SERVER(dir);
1590 struct nfs_openargs *o_arg = &data->o_arg;
1591 struct nfs_openres *o_res = &data->o_res;
1592 struct rpc_task *task;
1593 struct rpc_message msg = {
1594 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1597 .rpc_cred = data->owner->so_cred,
1599 struct rpc_task_setup task_setup_data = {
1600 .rpc_client = server->client,
1601 .rpc_message = &msg,
1602 .callback_ops = &nfs4_open_ops,
1603 .callback_data = data,
1604 .workqueue = nfsiod_workqueue,
1605 .flags = RPC_TASK_ASYNC,
1609 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1610 kref_get(&data->kref);
1612 data->rpc_status = 0;
1613 data->cancelled = 0;
1615 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1616 task = rpc_run_task(&task_setup_data);
1618 return PTR_ERR(task);
1619 status = nfs4_wait_for_completion_rpc_task(task);
1621 data->cancelled = 1;
1624 status = data->rpc_status;
1630 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1632 struct inode *dir = data->dir->d_inode;
1633 struct nfs_openres *o_res = &data->o_res;
1636 status = nfs4_run_open_task(data, 1);
1637 if (status != 0 || !data->rpc_done)
1640 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1642 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1643 status = _nfs4_proc_open_confirm(data);
1651 static int nfs4_opendata_access(struct rpc_cred *cred,
1652 struct nfs4_opendata *opendata,
1653 struct nfs4_state *state, fmode_t fmode)
1655 struct nfs_access_entry cache;
1658 /* access call failed or for some reason the server doesn't
1659 * support any access modes -- defer access call until later */
1660 if (opendata->o_res.access_supported == 0)
1664 if (fmode & FMODE_READ)
1666 if (fmode & FMODE_WRITE)
1668 if (fmode & FMODE_EXEC)
1672 cache.jiffies = jiffies;
1673 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1674 nfs_access_add_cache(state->inode, &cache);
1676 if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
1679 /* even though OPEN succeeded, access is denied. Close the file */
1680 nfs4_close_state(state, fmode);
1681 return -NFS4ERR_ACCESS;
1685 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1687 static int _nfs4_proc_open(struct nfs4_opendata *data)
1689 struct inode *dir = data->dir->d_inode;
1690 struct nfs_server *server = NFS_SERVER(dir);
1691 struct nfs_openargs *o_arg = &data->o_arg;
1692 struct nfs_openres *o_res = &data->o_res;
1695 status = nfs4_run_open_task(data, 0);
1696 if (!data->rpc_done)
1699 if (status == -NFS4ERR_BADNAME &&
1700 !(o_arg->open_flags & O_CREAT))
1705 nfs_fattr_map_and_free_names(server, &data->f_attr);
1707 if (o_arg->open_flags & O_CREAT)
1708 update_changeattr(dir, &o_res->cinfo);
1709 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1710 server->caps &= ~NFS_CAP_POSIX_LOCK;
1711 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1712 status = _nfs4_proc_open_confirm(data);
1716 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1717 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1721 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1726 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1727 ret = nfs4_wait_clnt_recover(clp);
1730 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1731 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1733 nfs4_schedule_state_manager(clp);
1739 static int nfs4_recover_expired_lease(struct nfs_server *server)
1741 return nfs4_client_recover_expired_lease(server->nfs_client);
1746 * reclaim state on the server after a network partition.
1747 * Assumes caller holds the appropriate lock
1749 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1751 struct nfs4_opendata *opendata;
1754 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1755 if (IS_ERR(opendata))
1756 return PTR_ERR(opendata);
1757 ret = nfs4_open_recover(opendata, state);
1759 d_drop(ctx->dentry);
1760 nfs4_opendata_put(opendata);
1764 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1766 struct nfs_server *server = NFS_SERVER(state->inode);
1767 struct nfs4_exception exception = { };
1771 err = _nfs4_open_expired(ctx, state);
1775 case -NFS4ERR_GRACE:
1776 case -NFS4ERR_DELAY:
1777 nfs4_handle_exception(server, err, &exception);
1780 } while (exception.retry);
1785 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1787 struct nfs_open_context *ctx;
1790 ctx = nfs4_state_find_open_context(state);
1792 return PTR_ERR(ctx);
1793 ret = nfs4_do_open_expired(ctx, state);
1794 put_nfs_open_context(ctx);
1798 #if defined(CONFIG_NFS_V4_1)
1799 static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
1801 struct nfs_server *server = NFS_SERVER(state->inode);
1802 nfs4_stateid *stateid = &state->stateid;
1805 /* If a state reset has been done, test_stateid is unneeded */
1806 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1809 status = nfs41_test_stateid(server, stateid);
1810 if (status != NFS_OK) {
1811 /* Free the stateid unless the server explicitly
1812 * informs us the stateid is unrecognized. */
1813 if (status != -NFS4ERR_BAD_STATEID)
1814 nfs41_free_stateid(server, stateid);
1815 nfs_remove_bad_delegation(state->inode);
1817 write_seqlock(&state->seqlock);
1818 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1819 write_sequnlock(&state->seqlock);
1820 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1825 * nfs41_check_open_stateid - possibly free an open stateid
1827 * @state: NFSv4 state for an inode
1829 * Returns NFS_OK if recovery for this stateid is now finished.
1830 * Otherwise a negative NFS4ERR value is returned.
1832 static int nfs41_check_open_stateid(struct nfs4_state *state)
1834 struct nfs_server *server = NFS_SERVER(state->inode);
1835 nfs4_stateid *stateid = &state->open_stateid;
1838 /* If a state reset has been done, test_stateid is unneeded */
1839 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1840 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1841 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1842 return -NFS4ERR_BAD_STATEID;
1844 status = nfs41_test_stateid(server, stateid);
1845 if (status != NFS_OK) {
1846 /* Free the stateid unless the server explicitly
1847 * informs us the stateid is unrecognized. */
1848 if (status != -NFS4ERR_BAD_STATEID)
1849 nfs41_free_stateid(server, stateid);
1851 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1852 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1853 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1858 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1862 nfs41_clear_delegation_stateid(state);
1863 status = nfs41_check_open_stateid(state);
1864 if (status != NFS_OK)
1865 status = nfs4_open_expired(sp, state);
1871 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1872 * fields corresponding to attributes that were used to store the verifier.
1873 * Make sure we clobber those fields in the later setattr call
1875 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1877 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1878 !(sattr->ia_valid & ATTR_ATIME_SET))
1879 sattr->ia_valid |= ATTR_ATIME;
1881 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1882 !(sattr->ia_valid & ATTR_MTIME_SET))
1883 sattr->ia_valid |= ATTR_MTIME;
1887 * Returns a referenced nfs4_state
1889 static int _nfs4_do_open(struct inode *dir,
1890 struct dentry *dentry,
1893 struct iattr *sattr,
1894 struct rpc_cred *cred,
1895 struct nfs4_state **res,
1896 struct nfs4_threshold **ctx_th)
1898 struct nfs4_state_owner *sp;
1899 struct nfs4_state *state = NULL;
1900 struct nfs_server *server = NFS_SERVER(dir);
1901 struct nfs4_opendata *opendata;
1904 /* Protect against reboot recovery conflicts */
1906 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1908 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1911 status = nfs4_recover_expired_lease(server);
1913 goto err_put_state_owner;
1914 if (dentry->d_inode != NULL)
1915 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1917 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1918 if (opendata == NULL)
1919 goto err_put_state_owner;
1921 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1922 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1923 if (!opendata->f_attr.mdsthreshold)
1924 goto err_opendata_put;
1925 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
1927 if (dentry->d_inode != NULL)
1928 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1930 status = _nfs4_proc_open(opendata);
1932 goto err_opendata_put;
1934 state = nfs4_opendata_to_nfs4_state(opendata);
1935 status = PTR_ERR(state);
1937 goto err_opendata_put;
1938 if (server->caps & NFS_CAP_POSIX_LOCK)
1939 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1941 status = nfs4_opendata_access(cred, opendata, state, fmode);
1943 goto err_opendata_put;
1945 if (opendata->o_arg.open_flags & O_EXCL) {
1946 nfs4_exclusive_attrset(opendata, sattr);
1948 nfs_fattr_init(opendata->o_res.f_attr);
1949 status = nfs4_do_setattr(state->inode, cred,
1950 opendata->o_res.f_attr, sattr,
1953 nfs_setattr_update_inode(state->inode, sattr);
1954 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1957 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1958 *ctx_th = opendata->f_attr.mdsthreshold;
1960 kfree(opendata->f_attr.mdsthreshold);
1961 opendata->f_attr.mdsthreshold = NULL;
1963 nfs4_opendata_put(opendata);
1964 nfs4_put_state_owner(sp);
1968 kfree(opendata->f_attr.mdsthreshold);
1969 nfs4_opendata_put(opendata);
1970 err_put_state_owner:
1971 nfs4_put_state_owner(sp);
1978 static struct nfs4_state *nfs4_do_open(struct inode *dir,
1979 struct dentry *dentry,
1982 struct iattr *sattr,
1983 struct rpc_cred *cred,
1984 struct nfs4_threshold **ctx_th)
1986 struct nfs4_exception exception = { };
1987 struct nfs4_state *res;
1990 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
1992 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1996 /* NOTE: BAD_SEQID means the server and client disagree about the
1997 * book-keeping w.r.t. state-changing operations
1998 * (OPEN/CLOSE/LOCK/LOCKU...)
1999 * It is actually a sign of a bug on the client or on the server.
2001 * If we receive a BAD_SEQID error in the particular case of
2002 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2003 * have unhashed the old state_owner for us, and that we can
2004 * therefore safely retry using a new one. We should still warn
2005 * the user though...
2007 if (status == -NFS4ERR_BAD_SEQID) {
2008 pr_warn_ratelimited("NFS: v4 server %s "
2009 " returned a bad sequence-id error!\n",
2010 NFS_SERVER(dir)->nfs_client->cl_hostname);
2011 exception.retry = 1;
2015 * BAD_STATEID on OPEN means that the server cancelled our
2016 * state before it received the OPEN_CONFIRM.
2017 * Recover by retrying the request as per the discussion
2018 * on Page 181 of RFC3530.
2020 if (status == -NFS4ERR_BAD_STATEID) {
2021 exception.retry = 1;
2024 if (status == -EAGAIN) {
2025 /* We must have found a delegation */
2026 exception.retry = 1;
2029 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2030 status, &exception));
2031 } while (exception.retry);
2035 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2036 struct nfs_fattr *fattr, struct iattr *sattr,
2037 struct nfs4_state *state)
2039 struct nfs_server *server = NFS_SERVER(inode);
2040 struct nfs_setattrargs arg = {
2041 .fh = NFS_FH(inode),
2044 .bitmask = server->attr_bitmask,
2046 struct nfs_setattrres res = {
2050 struct rpc_message msg = {
2051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2056 unsigned long timestamp = jiffies;
2059 nfs_fattr_init(fattr);
2061 if (state != NULL) {
2062 struct nfs_lockowner lockowner = {
2063 .l_owner = current->files,
2064 .l_pid = current->tgid,
2066 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2068 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2070 /* Use that stateid */
2072 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2074 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2075 if (status == 0 && state != NULL)
2076 renew_lease(server, timestamp);
2080 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2081 struct nfs_fattr *fattr, struct iattr *sattr,
2082 struct nfs4_state *state)
2084 struct nfs_server *server = NFS_SERVER(inode);
2085 struct nfs4_exception exception = {
2091 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2093 case -NFS4ERR_OPENMODE:
2094 if (state && !(state->state & FMODE_WRITE)) {
2096 if (sattr->ia_valid & ATTR_OPEN)
2101 err = nfs4_handle_exception(server, err, &exception);
2102 } while (exception.retry);
2107 struct nfs4_closedata {
2108 struct inode *inode;
2109 struct nfs4_state *state;
2110 struct nfs_closeargs arg;
2111 struct nfs_closeres res;
2112 struct nfs_fattr fattr;
2113 unsigned long timestamp;
2118 static void nfs4_free_closedata(void *data)
2120 struct nfs4_closedata *calldata = data;
2121 struct nfs4_state_owner *sp = calldata->state->owner;
2122 struct super_block *sb = calldata->state->inode->i_sb;
2125 pnfs_roc_release(calldata->state->inode);
2126 nfs4_put_open_state(calldata->state);
2127 nfs_free_seqid(calldata->arg.seqid);
2128 nfs4_put_state_owner(sp);
2129 nfs_sb_deactive(sb);
2133 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2136 spin_lock(&state->owner->so_lock);
2137 if (!(fmode & FMODE_READ))
2138 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2139 if (!(fmode & FMODE_WRITE))
2140 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2141 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2142 spin_unlock(&state->owner->so_lock);
2145 static void nfs4_close_done(struct rpc_task *task, void *data)
2147 struct nfs4_closedata *calldata = data;
2148 struct nfs4_state *state = calldata->state;
2149 struct nfs_server *server = NFS_SERVER(calldata->inode);
2151 dprintk("%s: begin!\n", __func__);
2152 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2154 /* hmm. we are done with the inode, and in the process of freeing
2155 * the state_owner. we keep this around to process errors
2157 switch (task->tk_status) {
2160 pnfs_roc_set_barrier(state->inode,
2161 calldata->roc_barrier);
2162 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2163 renew_lease(server, calldata->timestamp);
2164 nfs4_close_clear_stateid_flags(state,
2165 calldata->arg.fmode);
2167 case -NFS4ERR_STALE_STATEID:
2168 case -NFS4ERR_OLD_STATEID:
2169 case -NFS4ERR_BAD_STATEID:
2170 case -NFS4ERR_EXPIRED:
2171 if (calldata->arg.fmode == 0)
2174 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2175 rpc_restart_call_prepare(task);
2177 nfs_release_seqid(calldata->arg.seqid);
2178 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2179 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2182 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2184 struct nfs4_closedata *calldata = data;
2185 struct nfs4_state *state = calldata->state;
2186 struct inode *inode = calldata->inode;
2189 dprintk("%s: begin!\n", __func__);
2190 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2193 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2194 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2195 spin_lock(&state->owner->so_lock);
2196 /* Calculate the change in open mode */
2197 if (state->n_rdwr == 0) {
2198 if (state->n_rdonly == 0) {
2199 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2200 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2201 calldata->arg.fmode &= ~FMODE_READ;
2203 if (state->n_wronly == 0) {
2204 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2205 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2206 calldata->arg.fmode &= ~FMODE_WRITE;
2209 spin_unlock(&state->owner->so_lock);
2212 /* Note: exit _without_ calling nfs4_close_done */
2213 task->tk_action = NULL;
2217 if (calldata->arg.fmode == 0) {
2218 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2219 if (calldata->roc &&
2220 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
2224 nfs_fattr_init(calldata->res.fattr);
2225 calldata->timestamp = jiffies;
2226 if (nfs4_setup_sequence(NFS_SERVER(inode),
2227 &calldata->arg.seq_args,
2228 &calldata->res.seq_res,
2231 rpc_call_start(task);
2233 dprintk("%s: done!\n", __func__);
2236 static const struct rpc_call_ops nfs4_close_ops = {
2237 .rpc_call_prepare = nfs4_close_prepare,
2238 .rpc_call_done = nfs4_close_done,
2239 .rpc_release = nfs4_free_closedata,
2243 * It is possible for data to be read/written from a mem-mapped file
2244 * after the sys_close call (which hits the vfs layer as a flush).
2245 * This means that we can't safely call nfsv4 close on a file until
2246 * the inode is cleared. This in turn means that we are not good
2247 * NFSv4 citizens - we do not indicate to the server to update the file's
2248 * share state even when we are done with one of the three share
2249 * stateid's in the inode.
2251 * NOTE: Caller must be holding the sp->so_owner semaphore!
2253 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2255 struct nfs_server *server = NFS_SERVER(state->inode);
2256 struct nfs4_closedata *calldata;
2257 struct nfs4_state_owner *sp = state->owner;
2258 struct rpc_task *task;
2259 struct rpc_message msg = {
2260 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2261 .rpc_cred = state->owner->so_cred,
2263 struct rpc_task_setup task_setup_data = {
2264 .rpc_client = server->client,
2265 .rpc_message = &msg,
2266 .callback_ops = &nfs4_close_ops,
2267 .workqueue = nfsiod_workqueue,
2268 .flags = RPC_TASK_ASYNC,
2270 int status = -ENOMEM;
2272 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2273 if (calldata == NULL)
2275 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2276 calldata->inode = state->inode;
2277 calldata->state = state;
2278 calldata->arg.fh = NFS_FH(state->inode);
2279 calldata->arg.stateid = &state->open_stateid;
2280 /* Serialization for the sequence id */
2281 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2282 if (calldata->arg.seqid == NULL)
2283 goto out_free_calldata;
2284 calldata->arg.fmode = 0;
2285 calldata->arg.bitmask = server->cache_consistency_bitmask;
2286 calldata->res.fattr = &calldata->fattr;
2287 calldata->res.seqid = calldata->arg.seqid;
2288 calldata->res.server = server;
2289 calldata->roc = pnfs_roc(state->inode);
2290 nfs_sb_active(calldata->inode->i_sb);
2292 msg.rpc_argp = &calldata->arg;
2293 msg.rpc_resp = &calldata->res;
2294 task_setup_data.callback_data = calldata;
2295 task = rpc_run_task(&task_setup_data);
2297 return PTR_ERR(task);
2300 status = rpc_wait_for_completion_task(task);
2306 nfs4_put_open_state(state);
2307 nfs4_put_state_owner(sp);
2311 static struct inode *
2312 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2314 struct nfs4_state *state;
2316 /* Protect against concurrent sillydeletes */
2317 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2318 ctx->cred, &ctx->mdsthreshold);
2320 return ERR_CAST(state);
2322 return igrab(state->inode);
2325 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2327 if (ctx->state == NULL)
2330 nfs4_close_sync(ctx->state, ctx->mode);
2332 nfs4_close_state(ctx->state, ctx->mode);
2335 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2337 struct nfs4_server_caps_arg args = {
2340 struct nfs4_server_caps_res res = {};
2341 struct rpc_message msg = {
2342 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2348 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2350 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2351 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2352 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2353 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2354 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2355 NFS_CAP_CTIME|NFS_CAP_MTIME);
2356 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2357 server->caps |= NFS_CAP_ACLS;
2358 if (res.has_links != 0)
2359 server->caps |= NFS_CAP_HARDLINKS;
2360 if (res.has_symlinks != 0)
2361 server->caps |= NFS_CAP_SYMLINKS;
2362 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2363 server->caps |= NFS_CAP_FILEID;
2364 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2365 server->caps |= NFS_CAP_MODE;
2366 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2367 server->caps |= NFS_CAP_NLINK;
2368 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2369 server->caps |= NFS_CAP_OWNER;
2370 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2371 server->caps |= NFS_CAP_OWNER_GROUP;
2372 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2373 server->caps |= NFS_CAP_ATIME;
2374 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2375 server->caps |= NFS_CAP_CTIME;
2376 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2377 server->caps |= NFS_CAP_MTIME;
2379 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2380 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2381 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2382 server->acl_bitmask = res.acl_bitmask;
2383 server->fh_expire_type = res.fh_expire_type;
2389 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2391 struct nfs4_exception exception = { };
2394 err = nfs4_handle_exception(server,
2395 _nfs4_server_capabilities(server, fhandle),
2397 } while (exception.retry);
2401 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2402 struct nfs_fsinfo *info)
2404 struct nfs4_lookup_root_arg args = {
2405 .bitmask = nfs4_fattr_bitmap,
2407 struct nfs4_lookup_res res = {
2409 .fattr = info->fattr,
2412 struct rpc_message msg = {
2413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2418 nfs_fattr_init(info->fattr);
2419 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2422 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2423 struct nfs_fsinfo *info)
2425 struct nfs4_exception exception = { };
2428 err = _nfs4_lookup_root(server, fhandle, info);
2431 case -NFS4ERR_WRONGSEC:
2434 err = nfs4_handle_exception(server, err, &exception);
2436 } while (exception.retry);
2441 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2442 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2444 struct rpc_auth *auth;
2447 auth = rpcauth_create(flavor, server->client);
2452 ret = nfs4_lookup_root(server, fhandle, info);
2457 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2458 struct nfs_fsinfo *info)
2460 int i, len, status = 0;
2461 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2463 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2466 for (i = 0; i < len; i++) {
2467 /* AUTH_UNIX is the default flavor if none was specified,
2468 * thus has already been tried. */
2469 if (flav_array[i] == RPC_AUTH_UNIX)
2472 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2473 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2478 * -EACCESS could mean that the user doesn't have correct permissions
2479 * to access the mount. It could also mean that we tried to mount
2480 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2481 * existing mount programs don't handle -EACCES very well so it should
2482 * be mapped to -EPERM instead.
2484 if (status == -EACCES)
2490 * get the file handle for the "/" directory on the server
2492 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2493 struct nfs_fsinfo *info)
2495 int minor_version = server->nfs_client->cl_minorversion;
2496 int status = nfs4_lookup_root(server, fhandle, info);
2497 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2499 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2500 * by nfs4_map_errors() as this function exits.
2502 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2504 status = nfs4_server_capabilities(server, fhandle);
2506 status = nfs4_do_fsinfo(server, fhandle, info);
2507 return nfs4_map_errors(status);
2510 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2511 struct nfs_fsinfo *info)
2514 struct nfs_fattr *fattr = info->fattr;
2516 error = nfs4_server_capabilities(server, mntfh);
2518 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2522 error = nfs4_proc_getattr(server, mntfh, fattr);
2524 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2528 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2529 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2530 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2536 * Get locations and (maybe) other attributes of a referral.
2537 * Note that we'll actually follow the referral later when
2538 * we detect fsid mismatch in inode revalidation
2540 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2541 const struct qstr *name, struct nfs_fattr *fattr,
2542 struct nfs_fh *fhandle)
2544 int status = -ENOMEM;
2545 struct page *page = NULL;
2546 struct nfs4_fs_locations *locations = NULL;
2548 page = alloc_page(GFP_KERNEL);
2551 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2552 if (locations == NULL)
2555 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2558 /* Make sure server returned a different fsid for the referral */
2559 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2560 dprintk("%s: server did not return a different fsid for"
2561 " a referral at %s\n", __func__, name->name);
2565 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2566 nfs_fixup_referral_attributes(&locations->fattr);
2568 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2569 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2570 memset(fhandle, 0, sizeof(struct nfs_fh));
2578 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2580 struct nfs4_getattr_arg args = {
2582 .bitmask = server->attr_bitmask,
2584 struct nfs4_getattr_res res = {
2588 struct rpc_message msg = {
2589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2594 nfs_fattr_init(fattr);
2595 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2598 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2600 struct nfs4_exception exception = { };
2603 err = nfs4_handle_exception(server,
2604 _nfs4_proc_getattr(server, fhandle, fattr),
2606 } while (exception.retry);
2611 * The file is not closed if it is opened due to the a request to change
2612 * the size of the file. The open call will not be needed once the
2613 * VFS layer lookup-intents are implemented.
2615 * Close is called when the inode is destroyed.
2616 * If we haven't opened the file for O_WRONLY, we
2617 * need to in the size_change case to obtain a stateid.
2620 * Because OPEN is always done by name in nfsv4, it is
2621 * possible that we opened a different file by the same
2622 * name. We can recognize this race condition, but we
2623 * can't do anything about it besides returning an error.
2625 * This will be fixed with VFS changes (lookup-intent).
2628 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2629 struct iattr *sattr)
2631 struct inode *inode = dentry->d_inode;
2632 struct rpc_cred *cred = NULL;
2633 struct nfs4_state *state = NULL;
2636 if (pnfs_ld_layoutret_on_setattr(inode))
2637 pnfs_return_layout(inode);
2639 nfs_fattr_init(fattr);
2641 /* Deal with open(O_TRUNC) */
2642 if (sattr->ia_valid & ATTR_OPEN)
2643 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2645 /* Optimization: if the end result is no change, don't RPC */
2646 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2649 /* Search for an existing open(O_WRITE) file */
2650 if (sattr->ia_valid & ATTR_FILE) {
2651 struct nfs_open_context *ctx;
2653 ctx = nfs_file_open_context(sattr->ia_file);
2660 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2662 nfs_setattr_update_inode(inode, sattr);
2666 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2667 const struct qstr *name, struct nfs_fh *fhandle,
2668 struct nfs_fattr *fattr)
2670 struct nfs_server *server = NFS_SERVER(dir);
2672 struct nfs4_lookup_arg args = {
2673 .bitmask = server->attr_bitmask,
2674 .dir_fh = NFS_FH(dir),
2677 struct nfs4_lookup_res res = {
2682 struct rpc_message msg = {
2683 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2688 nfs_fattr_init(fattr);
2690 dprintk("NFS call lookup %s\n", name->name);
2691 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2692 dprintk("NFS reply lookup: %d\n", status);
2696 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2698 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2699 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2700 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2704 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2705 struct qstr *name, struct nfs_fh *fhandle,
2706 struct nfs_fattr *fattr)
2708 struct nfs4_exception exception = { };
2709 struct rpc_clnt *client = *clnt;
2712 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2714 case -NFS4ERR_BADNAME:
2717 case -NFS4ERR_MOVED:
2718 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
2720 case -NFS4ERR_WRONGSEC:
2722 if (client != *clnt)
2725 client = nfs4_create_sec_client(client, dir, name);
2727 return PTR_ERR(client);
2729 exception.retry = 1;
2732 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2734 } while (exception.retry);
2739 else if (client != *clnt)
2740 rpc_shutdown_client(client);
2745 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
2746 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2749 struct rpc_clnt *client = NFS_CLIENT(dir);
2751 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2752 if (client != NFS_CLIENT(dir)) {
2753 rpc_shutdown_client(client);
2754 nfs_fixup_secinfo_attributes(fattr);
2760 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2761 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2764 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2766 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2768 rpc_shutdown_client(client);
2769 return ERR_PTR(status);
2774 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2776 struct nfs_server *server = NFS_SERVER(inode);
2777 struct nfs4_accessargs args = {
2778 .fh = NFS_FH(inode),
2779 .bitmask = server->cache_consistency_bitmask,
2781 struct nfs4_accessres res = {
2784 struct rpc_message msg = {
2785 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2788 .rpc_cred = entry->cred,
2790 int mode = entry->mask;
2794 * Determine which access bits we want to ask for...
2796 if (mode & MAY_READ)
2797 args.access |= NFS4_ACCESS_READ;
2798 if (S_ISDIR(inode->i_mode)) {
2799 if (mode & MAY_WRITE)
2800 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2801 if (mode & MAY_EXEC)
2802 args.access |= NFS4_ACCESS_LOOKUP;
2804 if (mode & MAY_WRITE)
2805 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2806 if (mode & MAY_EXEC)
2807 args.access |= NFS4_ACCESS_EXECUTE;
2810 res.fattr = nfs_alloc_fattr();
2811 if (res.fattr == NULL)
2814 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2816 nfs_access_set_mask(entry, res.access);
2817 nfs_refresh_inode(inode, res.fattr);
2819 nfs_free_fattr(res.fattr);
2823 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2825 struct nfs4_exception exception = { };
2828 err = nfs4_handle_exception(NFS_SERVER(inode),
2829 _nfs4_proc_access(inode, entry),
2831 } while (exception.retry);
2836 * TODO: For the time being, we don't try to get any attributes
2837 * along with any of the zero-copy operations READ, READDIR,
2840 * In the case of the first three, we want to put the GETATTR
2841 * after the read-type operation -- this is because it is hard
2842 * to predict the length of a GETATTR response in v4, and thus
2843 * align the READ data correctly. This means that the GETATTR
2844 * may end up partially falling into the page cache, and we should
2845 * shift it into the 'tail' of the xdr_buf before processing.
2846 * To do this efficiently, we need to know the total length
2847 * of data received, which doesn't seem to be available outside
2850 * In the case of WRITE, we also want to put the GETATTR after
2851 * the operation -- in this case because we want to make sure
2852 * we get the post-operation mtime and size.
2854 * Both of these changes to the XDR layer would in fact be quite
2855 * minor, but I decided to leave them for a subsequent patch.
2857 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2858 unsigned int pgbase, unsigned int pglen)
2860 struct nfs4_readlink args = {
2861 .fh = NFS_FH(inode),
2866 struct nfs4_readlink_res res;
2867 struct rpc_message msg = {
2868 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2873 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2876 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2877 unsigned int pgbase, unsigned int pglen)
2879 struct nfs4_exception exception = { };
2882 err = nfs4_handle_exception(NFS_SERVER(inode),
2883 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2885 } while (exception.retry);
2890 * This is just for mknod. open(O_CREAT) will always do ->open_context().
2893 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2896 struct nfs_open_context *ctx;
2897 struct nfs4_state *state;
2900 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2902 return PTR_ERR(ctx);
2904 sattr->ia_mode &= ~current_umask();
2905 state = nfs4_do_open(dir, dentry, ctx->mode,
2906 flags, sattr, ctx->cred,
2907 &ctx->mdsthreshold);
2909 if (IS_ERR(state)) {
2910 status = PTR_ERR(state);
2913 d_add(dentry, igrab(state->inode));
2914 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2917 put_nfs_open_context(ctx);
2921 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2923 struct nfs_server *server = NFS_SERVER(dir);
2924 struct nfs_removeargs args = {
2928 struct nfs_removeres res = {
2931 struct rpc_message msg = {
2932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2938 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2940 update_changeattr(dir, &res.cinfo);
2944 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2946 struct nfs4_exception exception = { };
2949 err = nfs4_handle_exception(NFS_SERVER(dir),
2950 _nfs4_proc_remove(dir, name),
2952 } while (exception.retry);
2956 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2958 struct nfs_server *server = NFS_SERVER(dir);
2959 struct nfs_removeargs *args = msg->rpc_argp;
2960 struct nfs_removeres *res = msg->rpc_resp;
2962 res->server = server;
2963 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2964 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
2967 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2969 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2970 &data->args.seq_args,
2974 rpc_call_start(task);
2977 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2979 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2981 if (!nfs4_sequence_done(task, &res->seq_res))
2983 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2985 update_changeattr(dir, &res->cinfo);
2989 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2991 struct nfs_server *server = NFS_SERVER(dir);
2992 struct nfs_renameargs *arg = msg->rpc_argp;
2993 struct nfs_renameres *res = msg->rpc_resp;
2995 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2996 res->server = server;
2997 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
3000 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3002 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3003 &data->args.seq_args,
3007 rpc_call_start(task);
3010 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3011 struct inode *new_dir)
3013 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3015 if (!nfs4_sequence_done(task, &res->seq_res))
3017 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3020 update_changeattr(old_dir, &res->old_cinfo);
3021 update_changeattr(new_dir, &res->new_cinfo);
3025 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3026 struct inode *new_dir, struct qstr *new_name)
3028 struct nfs_server *server = NFS_SERVER(old_dir);
3029 struct nfs_renameargs arg = {
3030 .old_dir = NFS_FH(old_dir),
3031 .new_dir = NFS_FH(new_dir),
3032 .old_name = old_name,
3033 .new_name = new_name,
3035 struct nfs_renameres res = {
3038 struct rpc_message msg = {
3039 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3043 int status = -ENOMEM;
3045 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3047 update_changeattr(old_dir, &res.old_cinfo);
3048 update_changeattr(new_dir, &res.new_cinfo);
3053 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3054 struct inode *new_dir, struct qstr *new_name)
3056 struct nfs4_exception exception = { };
3059 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3060 _nfs4_proc_rename(old_dir, old_name,
3063 } while (exception.retry);
3067 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3069 struct nfs_server *server = NFS_SERVER(inode);
3070 struct nfs4_link_arg arg = {
3071 .fh = NFS_FH(inode),
3072 .dir_fh = NFS_FH(dir),
3074 .bitmask = server->attr_bitmask,
3076 struct nfs4_link_res res = {
3079 struct rpc_message msg = {
3080 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3084 int status = -ENOMEM;
3086 res.fattr = nfs_alloc_fattr();
3087 if (res.fattr == NULL)
3090 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3092 update_changeattr(dir, &res.cinfo);
3093 nfs_post_op_update_inode(inode, res.fattr);
3096 nfs_free_fattr(res.fattr);
3100 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3102 struct nfs4_exception exception = { };
3105 err = nfs4_handle_exception(NFS_SERVER(inode),
3106 _nfs4_proc_link(inode, dir, name),
3108 } while (exception.retry);
3112 struct nfs4_createdata {
3113 struct rpc_message msg;
3114 struct nfs4_create_arg arg;
3115 struct nfs4_create_res res;
3117 struct nfs_fattr fattr;
3120 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3121 struct qstr *name, struct iattr *sattr, u32 ftype)
3123 struct nfs4_createdata *data;
3125 data = kzalloc(sizeof(*data), GFP_KERNEL);
3127 struct nfs_server *server = NFS_SERVER(dir);
3129 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3130 data->msg.rpc_argp = &data->arg;
3131 data->msg.rpc_resp = &data->res;
3132 data->arg.dir_fh = NFS_FH(dir);
3133 data->arg.server = server;
3134 data->arg.name = name;
3135 data->arg.attrs = sattr;
3136 data->arg.ftype = ftype;
3137 data->arg.bitmask = server->attr_bitmask;
3138 data->res.server = server;
3139 data->res.fh = &data->fh;
3140 data->res.fattr = &data->fattr;
3141 nfs_fattr_init(data->res.fattr);
3146 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3148 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3149 &data->arg.seq_args, &data->res.seq_res, 1);
3151 update_changeattr(dir, &data->res.dir_cinfo);
3152 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3157 static void nfs4_free_createdata(struct nfs4_createdata *data)
3162 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3163 struct page *page, unsigned int len, struct iattr *sattr)
3165 struct nfs4_createdata *data;
3166 int status = -ENAMETOOLONG;
3168 if (len > NFS4_MAXPATHLEN)
3172 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3176 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3177 data->arg.u.symlink.pages = &page;
3178 data->arg.u.symlink.len = len;
3180 status = nfs4_do_create(dir, dentry, data);
3182 nfs4_free_createdata(data);
3187 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3188 struct page *page, unsigned int len, struct iattr *sattr)
3190 struct nfs4_exception exception = { };
3193 err = nfs4_handle_exception(NFS_SERVER(dir),
3194 _nfs4_proc_symlink(dir, dentry, page,
3197 } while (exception.retry);
3201 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3202 struct iattr *sattr)
3204 struct nfs4_createdata *data;
3205 int status = -ENOMEM;
3207 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3211 status = nfs4_do_create(dir, dentry, data);
3213 nfs4_free_createdata(data);
3218 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3219 struct iattr *sattr)
3221 struct nfs4_exception exception = { };
3224 sattr->ia_mode &= ~current_umask();
3226 err = nfs4_handle_exception(NFS_SERVER(dir),
3227 _nfs4_proc_mkdir(dir, dentry, sattr),
3229 } while (exception.retry);
3233 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3234 u64 cookie, struct page **pages, unsigned int count, int plus)
3236 struct inode *dir = dentry->d_inode;
3237 struct nfs4_readdir_arg args = {
3242 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3245 struct nfs4_readdir_res res;
3246 struct rpc_message msg = {
3247 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3254 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3255 dentry->d_parent->d_name.name,
3256 dentry->d_name.name,
3257 (unsigned long long)cookie);
3258 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3259 res.pgbase = args.pgbase;
3260 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3262 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3263 status += args.pgbase;
3266 nfs_invalidate_atime(dir);
3268 dprintk("%s: returns %d\n", __func__, status);
3272 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3273 u64 cookie, struct page **pages, unsigned int count, int plus)
3275 struct nfs4_exception exception = { };
3278 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3279 _nfs4_proc_readdir(dentry, cred, cookie,
3280 pages, count, plus),
3282 } while (exception.retry);
3286 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3287 struct iattr *sattr, dev_t rdev)
3289 struct nfs4_createdata *data;
3290 int mode = sattr->ia_mode;
3291 int status = -ENOMEM;
3293 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3294 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3296 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3301 data->arg.ftype = NF4FIFO;
3302 else if (S_ISBLK(mode)) {
3303 data->arg.ftype = NF4BLK;
3304 data->arg.u.device.specdata1 = MAJOR(rdev);
3305 data->arg.u.device.specdata2 = MINOR(rdev);
3307 else if (S_ISCHR(mode)) {
3308 data->arg.ftype = NF4CHR;
3309 data->arg.u.device.specdata1 = MAJOR(rdev);
3310 data->arg.u.device.specdata2 = MINOR(rdev);
3313 status = nfs4_do_create(dir, dentry, data);
3315 nfs4_free_createdata(data);
3320 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3321 struct iattr *sattr, dev_t rdev)
3323 struct nfs4_exception exception = { };
3326 sattr->ia_mode &= ~current_umask();
3328 err = nfs4_handle_exception(NFS_SERVER(dir),
3329 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3331 } while (exception.retry);
3335 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3336 struct nfs_fsstat *fsstat)
3338 struct nfs4_statfs_arg args = {
3340 .bitmask = server->attr_bitmask,
3342 struct nfs4_statfs_res res = {
3345 struct rpc_message msg = {
3346 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3351 nfs_fattr_init(fsstat->fattr);
3352 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3355 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3357 struct nfs4_exception exception = { };
3360 err = nfs4_handle_exception(server,
3361 _nfs4_proc_statfs(server, fhandle, fsstat),
3363 } while (exception.retry);
3367 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3368 struct nfs_fsinfo *fsinfo)
3370 struct nfs4_fsinfo_arg args = {
3372 .bitmask = server->attr_bitmask,
3374 struct nfs4_fsinfo_res res = {
3377 struct rpc_message msg = {
3378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3383 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3386 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3388 struct nfs4_exception exception = { };
3392 err = nfs4_handle_exception(server,
3393 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3395 } while (exception.retry);
3399 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3403 nfs_fattr_init(fsinfo->fattr);
3404 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3406 /* block layout checks this! */
3407 server->pnfs_blksize = fsinfo->blksize;
3408 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3414 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3415 struct nfs_pathconf *pathconf)
3417 struct nfs4_pathconf_arg args = {
3419 .bitmask = server->attr_bitmask,
3421 struct nfs4_pathconf_res res = {
3422 .pathconf = pathconf,
3424 struct rpc_message msg = {
3425 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3430 /* None of the pathconf attributes are mandatory to implement */
3431 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3432 memset(pathconf, 0, sizeof(*pathconf));
3436 nfs_fattr_init(pathconf->fattr);
3437 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3440 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3441 struct nfs_pathconf *pathconf)
3443 struct nfs4_exception exception = { };
3447 err = nfs4_handle_exception(server,
3448 _nfs4_proc_pathconf(server, fhandle, pathconf),
3450 } while (exception.retry);
3454 void __nfs4_read_done_cb(struct nfs_read_data *data)
3456 nfs_invalidate_atime(data->header->inode);
3459 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3461 struct nfs_server *server = NFS_SERVER(data->header->inode);
3463 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3464 rpc_restart_call_prepare(task);
3468 __nfs4_read_done_cb(data);
3469 if (task->tk_status > 0)
3470 renew_lease(server, data->timestamp);
3474 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3477 dprintk("--> %s\n", __func__);
3479 if (!nfs4_sequence_done(task, &data->res.seq_res))
3482 return data->read_done_cb ? data->read_done_cb(task, data) :
3483 nfs4_read_done_cb(task, data);
3486 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3488 data->timestamp = jiffies;
3489 data->read_done_cb = nfs4_read_done_cb;
3490 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3491 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3494 static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3496 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3497 &data->args.seq_args,
3501 rpc_call_start(task);
3504 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3506 struct inode *inode = data->header->inode;
3508 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3509 rpc_restart_call_prepare(task);
3512 if (task->tk_status >= 0) {
3513 renew_lease(NFS_SERVER(inode), data->timestamp);
3514 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
3519 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3521 if (!nfs4_sequence_done(task, &data->res.seq_res))
3523 return data->write_done_cb ? data->write_done_cb(task, data) :
3524 nfs4_write_done_cb(task, data);
3528 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3530 const struct nfs_pgio_header *hdr = data->header;
3532 /* Don't request attributes for pNFS or O_DIRECT writes */
3533 if (data->ds_clp != NULL || hdr->dreq != NULL)
3535 /* Otherwise, request attributes if and only if we don't hold
3538 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
3541 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3543 struct nfs_server *server = NFS_SERVER(data->header->inode);
3545 if (!nfs4_write_need_cache_consistency_data(data)) {
3546 data->args.bitmask = NULL;
3547 data->res.fattr = NULL;
3549 data->args.bitmask = server->cache_consistency_bitmask;
3551 if (!data->write_done_cb)
3552 data->write_done_cb = nfs4_write_done_cb;
3553 data->res.server = server;
3554 data->timestamp = jiffies;
3556 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3557 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3560 static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3562 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3563 &data->args.seq_args,
3567 rpc_call_start(task);
3570 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3572 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3573 &data->args.seq_args,
3577 rpc_call_start(task);
3580 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
3582 struct inode *inode = data->inode;
3584 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3585 rpc_restart_call_prepare(task);
3591 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
3593 if (!nfs4_sequence_done(task, &data->res.seq_res))
3595 return data->commit_done_cb(task, data);
3598 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
3600 struct nfs_server *server = NFS_SERVER(data->inode);
3602 if (data->commit_done_cb == NULL)
3603 data->commit_done_cb = nfs4_commit_done_cb;
3604 data->res.server = server;
3605 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3606 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3609 struct nfs4_renewdata {
3610 struct nfs_client *client;
3611 unsigned long timestamp;
3615 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3616 * standalone procedure for queueing an asynchronous RENEW.
3618 static void nfs4_renew_release(void *calldata)
3620 struct nfs4_renewdata *data = calldata;
3621 struct nfs_client *clp = data->client;
3623 if (atomic_read(&clp->cl_count) > 1)
3624 nfs4_schedule_state_renewal(clp);
3625 nfs_put_client(clp);
3629 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3631 struct nfs4_renewdata *data = calldata;
3632 struct nfs_client *clp = data->client;
3633 unsigned long timestamp = data->timestamp;
3635 if (task->tk_status < 0) {
3636 /* Unless we're shutting down, schedule state recovery! */
3637 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3639 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3640 nfs4_schedule_lease_recovery(clp);
3643 nfs4_schedule_path_down_recovery(clp);
3645 do_renew_lease(clp, timestamp);
3648 static const struct rpc_call_ops nfs4_renew_ops = {
3649 .rpc_call_done = nfs4_renew_done,
3650 .rpc_release = nfs4_renew_release,
3653 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3655 struct rpc_message msg = {
3656 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3660 struct nfs4_renewdata *data;
3662 if (renew_flags == 0)
3664 if (!atomic_inc_not_zero(&clp->cl_count))
3666 data = kmalloc(sizeof(*data), GFP_NOFS);
3670 data->timestamp = jiffies;
3671 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3672 &nfs4_renew_ops, data);
3675 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3677 struct rpc_message msg = {
3678 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3682 unsigned long now = jiffies;
3685 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3688 do_renew_lease(clp, now);
3692 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3694 return (server->caps & NFS_CAP_ACLS)
3695 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3696 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3699 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3700 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
3703 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
3705 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3706 struct page **pages, unsigned int *pgbase)
3708 struct page *newpage, **spages;
3714 len = min_t(size_t, PAGE_SIZE, buflen);
3715 newpage = alloc_page(GFP_KERNEL);
3717 if (newpage == NULL)
3719 memcpy(page_address(newpage), buf, len);
3724 } while (buflen != 0);
3730 __free_page(spages[rc-1]);
3734 struct nfs4_cached_acl {
3740 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3742 struct nfs_inode *nfsi = NFS_I(inode);
3744 spin_lock(&inode->i_lock);
3745 kfree(nfsi->nfs4_acl);
3746 nfsi->nfs4_acl = acl;
3747 spin_unlock(&inode->i_lock);
3750 static void nfs4_zap_acl_attr(struct inode *inode)
3752 nfs4_set_cached_acl(inode, NULL);
3755 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3757 struct nfs_inode *nfsi = NFS_I(inode);
3758 struct nfs4_cached_acl *acl;
3761 spin_lock(&inode->i_lock);
3762 acl = nfsi->nfs4_acl;
3765 if (buf == NULL) /* user is just asking for length */
3767 if (acl->cached == 0)
3769 ret = -ERANGE; /* see getxattr(2) man page */
3770 if (acl->len > buflen)
3772 memcpy(buf, acl->data, acl->len);
3776 spin_unlock(&inode->i_lock);
3780 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3782 struct nfs4_cached_acl *acl;
3783 size_t buflen = sizeof(*acl) + acl_len;
3785 if (buflen <= PAGE_SIZE) {
3786 acl = kmalloc(buflen, GFP_KERNEL);
3790 _copy_from_pages(acl->data, pages, pgbase, acl_len);
3792 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3799 nfs4_set_cached_acl(inode, acl);
3803 * The getxattr API returns the required buffer length when called with a
3804 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3805 * the required buf. On a NULL buf, we send a page of data to the server
3806 * guessing that the ACL request can be serviced by a page. If so, we cache
3807 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3808 * the cache. If not so, we throw away the page, and cache the required
3809 * length. The next getxattr call will then produce another round trip to
3810 * the server, this time with the input buf of the required size.
3812 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3814 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3815 struct nfs_getaclargs args = {
3816 .fh = NFS_FH(inode),
3820 struct nfs_getaclres res = {
3823 struct rpc_message msg = {
3824 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3828 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3829 int ret = -ENOMEM, i;
3831 /* As long as we're doing a round trip to the server anyway,
3832 * let's be prepared for a page of acl data. */
3835 if (npages > ARRAY_SIZE(pages))
3838 for (i = 0; i < npages; i++) {
3839 pages[i] = alloc_page(GFP_KERNEL);
3844 /* for decoding across pages */
3845 res.acl_scratch = alloc_page(GFP_KERNEL);
3846 if (!res.acl_scratch)
3849 args.acl_len = npages * PAGE_SIZE;
3850 args.acl_pgbase = 0;
3852 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3853 __func__, buf, buflen, npages, args.acl_len);
3854 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3855 &msg, &args.seq_args, &res.seq_res, 0);
3859 /* Handle the case where the passed-in buffer is too short */
3860 if (res.acl_flags & NFS4_ACL_TRUNC) {
3861 /* Did the user only issue a request for the acl length? */
3867 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3869 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3873 for (i = 0; i < npages; i++)
3875 __free_page(pages[i]);
3876 if (res.acl_scratch)
3877 __free_page(res.acl_scratch);
3881 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3883 struct nfs4_exception exception = { };
3886 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3889 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3890 } while (exception.retry);
3894 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3896 struct nfs_server *server = NFS_SERVER(inode);
3899 if (!nfs4_server_supports_acls(server))
3901 ret = nfs_revalidate_inode(server, inode);
3904 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3905 nfs_zap_acl_cache(inode);
3906 ret = nfs4_read_cached_acl(inode, buf, buflen);
3908 /* -ENOENT is returned if there is no ACL or if there is an ACL
3909 * but no cached acl data, just the acl length */
3911 return nfs4_get_acl_uncached(inode, buf, buflen);
3914 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3916 struct nfs_server *server = NFS_SERVER(inode);
3917 struct page *pages[NFS4ACL_MAXPAGES];
3918 struct nfs_setaclargs arg = {
3919 .fh = NFS_FH(inode),
3923 struct nfs_setaclres res;
3924 struct rpc_message msg = {
3925 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3929 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3932 if (!nfs4_server_supports_acls(server))
3934 if (npages > ARRAY_SIZE(pages))
3936 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3939 nfs4_inode_return_delegation(inode);
3940 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3943 * Free each page after tx, so the only ref left is
3944 * held by the network stack
3947 put_page(pages[i-1]);
3950 * Acl update can result in inode attribute update.
3951 * so mark the attribute cache invalid.
3953 spin_lock(&inode->i_lock);
3954 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3955 spin_unlock(&inode->i_lock);
3956 nfs_access_zap_cache(inode);
3957 nfs_zap_acl_cache(inode);
3961 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3963 struct nfs4_exception exception = { };
3966 err = nfs4_handle_exception(NFS_SERVER(inode),
3967 __nfs4_proc_set_acl(inode, buf, buflen),
3969 } while (exception.retry);
3974 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3976 struct nfs_client *clp = server->nfs_client;
3978 if (task->tk_status >= 0)
3980 switch(task->tk_status) {
3981 case -NFS4ERR_DELEG_REVOKED:
3982 case -NFS4ERR_ADMIN_REVOKED:
3983 case -NFS4ERR_BAD_STATEID:
3986 nfs_remove_bad_delegation(state->inode);
3987 case -NFS4ERR_OPENMODE:
3990 nfs4_schedule_stateid_recovery(server, state);
3991 goto wait_on_recovery;
3992 case -NFS4ERR_EXPIRED:
3994 nfs4_schedule_stateid_recovery(server, state);
3995 case -NFS4ERR_STALE_STATEID:
3996 case -NFS4ERR_STALE_CLIENTID:
3997 nfs4_schedule_lease_recovery(clp);
3998 goto wait_on_recovery;
3999 #if defined(CONFIG_NFS_V4_1)
4000 case -NFS4ERR_BADSESSION:
4001 case -NFS4ERR_BADSLOT:
4002 case -NFS4ERR_BAD_HIGH_SLOT:
4003 case -NFS4ERR_DEADSESSION:
4004 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4005 case -NFS4ERR_SEQ_FALSE_RETRY:
4006 case -NFS4ERR_SEQ_MISORDERED:
4007 dprintk("%s ERROR %d, Reset session\n", __func__,
4009 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4010 task->tk_status = 0;
4012 #endif /* CONFIG_NFS_V4_1 */
4013 case -NFS4ERR_DELAY:
4014 nfs_inc_server_stats(server, NFSIOS_DELAY);
4015 case -NFS4ERR_GRACE:
4017 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4018 task->tk_status = 0;
4020 case -NFS4ERR_RETRY_UNCACHED_REP:
4021 case -NFS4ERR_OLD_STATEID:
4022 task->tk_status = 0;
4025 task->tk_status = nfs4_map_errors(task->tk_status);
4028 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4029 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4030 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4031 task->tk_status = 0;
4035 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4036 nfs4_verifier *bootverf)
4040 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4041 /* An impossible timestamp guarantees this value
4042 * will never match a generated boot time. */
4044 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4046 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4047 verf[0] = (__be32)nn->boot_time.tv_sec;
4048 verf[1] = (__be32)nn->boot_time.tv_nsec;
4050 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4054 nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4055 char *buf, size_t len)
4057 unsigned int result;
4060 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4062 rpc_peeraddr2str(clp->cl_rpcclient,
4064 rpc_peeraddr2str(clp->cl_rpcclient,
4065 RPC_DISPLAY_PROTO));
4071 nfs4_init_uniform_client_string(const struct nfs_client *clp,
4072 char *buf, size_t len)
4074 char *nodename = clp->cl_rpcclient->cl_nodename;
4076 if (nfs4_client_id_uniquifier[0] != '\0')
4077 nodename = nfs4_client_id_uniquifier;
4078 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4079 clp->rpc_ops->version, clp->cl_minorversion,
4084 * nfs4_proc_setclientid - Negotiate client ID
4085 * @clp: state data structure
4086 * @program: RPC program for NFSv4 callback service
4087 * @port: IP port number for NFS4 callback service
4088 * @cred: RPC credential to use for this call
4089 * @res: where to place the result
4091 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4093 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4094 unsigned short port, struct rpc_cred *cred,
4095 struct nfs4_setclientid_res *res)
4097 nfs4_verifier sc_verifier;
4098 struct nfs4_setclientid setclientid = {
4099 .sc_verifier = &sc_verifier,
4101 .sc_cb_ident = clp->cl_cb_ident,
4103 struct rpc_message msg = {
4104 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4105 .rpc_argp = &setclientid,
4111 /* nfs_client_id4 */
4112 nfs4_init_boot_verifier(clp, &sc_verifier);
4113 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4114 setclientid.sc_name_len =
4115 nfs4_init_uniform_client_string(clp,
4116 setclientid.sc_name,
4117 sizeof(setclientid.sc_name));
4119 setclientid.sc_name_len =
4120 nfs4_init_nonuniform_client_string(clp,
4121 setclientid.sc_name,
4122 sizeof(setclientid.sc_name));
4125 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4126 sizeof(setclientid.sc_netid),
4127 rpc_peeraddr2str(clp->cl_rpcclient,
4128 RPC_DISPLAY_NETID));
4130 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4131 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4132 clp->cl_ipaddr, port >> 8, port & 255);
4134 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4135 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4136 setclientid.sc_name_len, setclientid.sc_name);
4137 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4138 dprintk("NFS reply setclientid: %d\n", status);
4143 * nfs4_proc_setclientid_confirm - Confirm client ID
4144 * @clp: state data structure
4145 * @res: result of a previous SETCLIENTID
4146 * @cred: RPC credential to use for this call
4148 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4150 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4151 struct nfs4_setclientid_res *arg,
4152 struct rpc_cred *cred)
4154 struct nfs_fsinfo fsinfo;
4155 struct rpc_message msg = {
4156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4158 .rpc_resp = &fsinfo,
4164 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4165 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4168 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4170 spin_lock(&clp->cl_lock);
4171 clp->cl_lease_time = fsinfo.lease_time * HZ;
4172 clp->cl_last_renewal = now;
4173 spin_unlock(&clp->cl_lock);
4175 dprintk("NFS reply setclientid_confirm: %d\n", status);
4179 struct nfs4_delegreturndata {
4180 struct nfs4_delegreturnargs args;
4181 struct nfs4_delegreturnres res;
4183 nfs4_stateid stateid;
4184 unsigned long timestamp;
4185 struct nfs_fattr fattr;
4189 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4191 struct nfs4_delegreturndata *data = calldata;
4193 if (!nfs4_sequence_done(task, &data->res.seq_res))
4196 switch (task->tk_status) {
4197 case -NFS4ERR_STALE_STATEID:
4198 case -NFS4ERR_EXPIRED:
4200 renew_lease(data->res.server, data->timestamp);
4203 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4205 rpc_restart_call_prepare(task);
4209 data->rpc_status = task->tk_status;
4212 static void nfs4_delegreturn_release(void *calldata)
4217 #if defined(CONFIG_NFS_V4_1)
4218 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4220 struct nfs4_delegreturndata *d_data;
4222 d_data = (struct nfs4_delegreturndata *)data;
4224 if (nfs4_setup_sequence(d_data->res.server,
4225 &d_data->args.seq_args,
4226 &d_data->res.seq_res, task))
4228 rpc_call_start(task);
4230 #endif /* CONFIG_NFS_V4_1 */
4232 static const struct rpc_call_ops nfs4_delegreturn_ops = {
4233 #if defined(CONFIG_NFS_V4_1)
4234 .rpc_call_prepare = nfs4_delegreturn_prepare,
4235 #endif /* CONFIG_NFS_V4_1 */
4236 .rpc_call_done = nfs4_delegreturn_done,
4237 .rpc_release = nfs4_delegreturn_release,
4240 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4242 struct nfs4_delegreturndata *data;
4243 struct nfs_server *server = NFS_SERVER(inode);
4244 struct rpc_task *task;
4245 struct rpc_message msg = {
4246 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4249 struct rpc_task_setup task_setup_data = {
4250 .rpc_client = server->client,
4251 .rpc_message = &msg,
4252 .callback_ops = &nfs4_delegreturn_ops,
4253 .flags = RPC_TASK_ASYNC,
4257 data = kzalloc(sizeof(*data), GFP_NOFS);
4260 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4261 data->args.fhandle = &data->fh;
4262 data->args.stateid = &data->stateid;
4263 data->args.bitmask = server->cache_consistency_bitmask;
4264 nfs_copy_fh(&data->fh, NFS_FH(inode));
4265 nfs4_stateid_copy(&data->stateid, stateid);
4266 data->res.fattr = &data->fattr;
4267 data->res.server = server;
4268 nfs_fattr_init(data->res.fattr);
4269 data->timestamp = jiffies;
4270 data->rpc_status = 0;
4272 task_setup_data.callback_data = data;
4273 msg.rpc_argp = &data->args;
4274 msg.rpc_resp = &data->res;
4275 task = rpc_run_task(&task_setup_data);
4277 return PTR_ERR(task);
4280 status = nfs4_wait_for_completion_rpc_task(task);
4283 status = data->rpc_status;
4285 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4287 nfs_refresh_inode(inode, &data->fattr);
4293 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4295 struct nfs_server *server = NFS_SERVER(inode);
4296 struct nfs4_exception exception = { };
4299 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4301 case -NFS4ERR_STALE_STATEID:
4302 case -NFS4ERR_EXPIRED:
4306 err = nfs4_handle_exception(server, err, &exception);
4307 } while (exception.retry);
4311 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4312 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4315 * sleep, with exponential backoff, and retry the LOCK operation.
4317 static unsigned long
4318 nfs4_set_lock_task_retry(unsigned long timeout)
4320 freezable_schedule_timeout_killable(timeout);
4322 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4323 return NFS4_LOCK_MAXTIMEOUT;
4327 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4329 struct inode *inode = state->inode;
4330 struct nfs_server *server = NFS_SERVER(inode);
4331 struct nfs_client *clp = server->nfs_client;
4332 struct nfs_lockt_args arg = {
4333 .fh = NFS_FH(inode),
4336 struct nfs_lockt_res res = {
4339 struct rpc_message msg = {
4340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4343 .rpc_cred = state->owner->so_cred,
4345 struct nfs4_lock_state *lsp;
4348 arg.lock_owner.clientid = clp->cl_clientid;
4349 status = nfs4_set_lock_state(state, request);
4352 lsp = request->fl_u.nfs4_fl.owner;
4353 arg.lock_owner.id = lsp->ls_seqid.owner_id;
4354 arg.lock_owner.s_dev = server->s_dev;
4355 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4358 request->fl_type = F_UNLCK;
4360 case -NFS4ERR_DENIED:
4363 request->fl_ops->fl_release_private(request);
4368 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4370 struct nfs4_exception exception = { };
4374 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4375 _nfs4_proc_getlk(state, cmd, request),
4377 } while (exception.retry);
4381 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4384 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4386 res = posix_lock_file_wait(file, fl);
4389 res = flock_lock_file_wait(file, fl);
4397 struct nfs4_unlockdata {
4398 struct nfs_locku_args arg;
4399 struct nfs_locku_res res;
4400 struct nfs4_lock_state *lsp;
4401 struct nfs_open_context *ctx;
4402 struct file_lock fl;
4403 const struct nfs_server *server;
4404 unsigned long timestamp;
4407 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4408 struct nfs_open_context *ctx,
4409 struct nfs4_lock_state *lsp,
4410 struct nfs_seqid *seqid)
4412 struct nfs4_unlockdata *p;
4413 struct inode *inode = lsp->ls_state->inode;
4415 p = kzalloc(sizeof(*p), GFP_NOFS);
4418 p->arg.fh = NFS_FH(inode);
4420 p->arg.seqid = seqid;
4421 p->res.seqid = seqid;
4422 p->arg.stateid = &lsp->ls_stateid;
4424 atomic_inc(&lsp->ls_count);
4425 /* Ensure we don't close file until we're done freeing locks! */
4426 p->ctx = get_nfs_open_context(ctx);
4427 memcpy(&p->fl, fl, sizeof(p->fl));
4428 p->server = NFS_SERVER(inode);
4432 static void nfs4_locku_release_calldata(void *data)
4434 struct nfs4_unlockdata *calldata = data;
4435 nfs_free_seqid(calldata->arg.seqid);
4436 nfs4_put_lock_state(calldata->lsp);
4437 put_nfs_open_context(calldata->ctx);
4441 static void nfs4_locku_done(struct rpc_task *task, void *data)
4443 struct nfs4_unlockdata *calldata = data;
4445 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4447 switch (task->tk_status) {
4449 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4450 &calldata->res.stateid);
4451 renew_lease(calldata->server, calldata->timestamp);
4453 case -NFS4ERR_BAD_STATEID:
4454 case -NFS4ERR_OLD_STATEID:
4455 case -NFS4ERR_STALE_STATEID:
4456 case -NFS4ERR_EXPIRED:
4459 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4460 rpc_restart_call_prepare(task);
4464 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4466 struct nfs4_unlockdata *calldata = data;
4468 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4470 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
4471 /* Note: exit _without_ running nfs4_locku_done */
4472 task->tk_action = NULL;
4475 calldata->timestamp = jiffies;
4476 if (nfs4_setup_sequence(calldata->server,
4477 &calldata->arg.seq_args,
4478 &calldata->res.seq_res, task))
4480 rpc_call_start(task);
4483 static const struct rpc_call_ops nfs4_locku_ops = {
4484 .rpc_call_prepare = nfs4_locku_prepare,
4485 .rpc_call_done = nfs4_locku_done,
4486 .rpc_release = nfs4_locku_release_calldata,
4489 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4490 struct nfs_open_context *ctx,
4491 struct nfs4_lock_state *lsp,
4492 struct nfs_seqid *seqid)
4494 struct nfs4_unlockdata *data;
4495 struct rpc_message msg = {
4496 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4497 .rpc_cred = ctx->cred,
4499 struct rpc_task_setup task_setup_data = {
4500 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4501 .rpc_message = &msg,
4502 .callback_ops = &nfs4_locku_ops,
4503 .workqueue = nfsiod_workqueue,
4504 .flags = RPC_TASK_ASYNC,
4507 /* Ensure this is an unlock - when canceling a lock, the
4508 * canceled lock is passed in, and it won't be an unlock.
4510 fl->fl_type = F_UNLCK;
4512 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4514 nfs_free_seqid(seqid);
4515 return ERR_PTR(-ENOMEM);
4518 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4519 msg.rpc_argp = &data->arg;
4520 msg.rpc_resp = &data->res;
4521 task_setup_data.callback_data = data;
4522 return rpc_run_task(&task_setup_data);
4525 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4527 struct nfs_inode *nfsi = NFS_I(state->inode);
4528 struct nfs_seqid *seqid;
4529 struct nfs4_lock_state *lsp;
4530 struct rpc_task *task;
4532 unsigned char fl_flags = request->fl_flags;
4534 status = nfs4_set_lock_state(state, request);
4535 /* Unlock _before_ we do the RPC call */
4536 request->fl_flags |= FL_EXISTS;
4537 down_read(&nfsi->rwsem);
4538 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4539 up_read(&nfsi->rwsem);
4542 up_read(&nfsi->rwsem);
4545 /* Is this a delegated lock? */
4546 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4548 lsp = request->fl_u.nfs4_fl.owner;
4549 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4553 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4554 status = PTR_ERR(task);
4557 status = nfs4_wait_for_completion_rpc_task(task);
4560 request->fl_flags = fl_flags;
4564 struct nfs4_lockdata {
4565 struct nfs_lock_args arg;
4566 struct nfs_lock_res res;
4567 struct nfs4_lock_state *lsp;
4568 struct nfs_open_context *ctx;
4569 struct file_lock fl;
4570 unsigned long timestamp;
4573 struct nfs_server *server;
4576 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4577 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4580 struct nfs4_lockdata *p;
4581 struct inode *inode = lsp->ls_state->inode;
4582 struct nfs_server *server = NFS_SERVER(inode);
4584 p = kzalloc(sizeof(*p), gfp_mask);
4588 p->arg.fh = NFS_FH(inode);
4590 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4591 if (p->arg.open_seqid == NULL)
4593 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4594 if (p->arg.lock_seqid == NULL)
4595 goto out_free_seqid;
4596 p->arg.lock_stateid = &lsp->ls_stateid;
4597 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4598 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4599 p->arg.lock_owner.s_dev = server->s_dev;
4600 p->res.lock_seqid = p->arg.lock_seqid;
4603 atomic_inc(&lsp->ls_count);
4604 p->ctx = get_nfs_open_context(ctx);
4605 memcpy(&p->fl, fl, sizeof(p->fl));
4608 nfs_free_seqid(p->arg.open_seqid);
4614 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4616 struct nfs4_lockdata *data = calldata;
4617 struct nfs4_state *state = data->lsp->ls_state;
4619 dprintk("%s: begin!\n", __func__);
4620 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4622 /* Do we need to do an open_to_lock_owner? */
4623 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4624 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4626 data->arg.open_stateid = &state->stateid;
4627 data->arg.new_lock_owner = 1;
4628 data->res.open_seqid = data->arg.open_seqid;
4630 data->arg.new_lock_owner = 0;
4631 data->timestamp = jiffies;
4632 if (nfs4_setup_sequence(data->server,
4633 &data->arg.seq_args,
4634 &data->res.seq_res, task))
4636 rpc_call_start(task);
4637 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4640 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4642 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4643 nfs4_lock_prepare(task, calldata);
4646 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4648 struct nfs4_lockdata *data = calldata;
4650 dprintk("%s: begin!\n", __func__);
4652 if (!nfs4_sequence_done(task, &data->res.seq_res))
4655 data->rpc_status = task->tk_status;
4656 if (data->arg.new_lock_owner != 0) {
4657 if (data->rpc_status == 0)
4658 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4662 if (data->rpc_status == 0) {
4663 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4664 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
4665 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4668 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4671 static void nfs4_lock_release(void *calldata)
4673 struct nfs4_lockdata *data = calldata;
4675 dprintk("%s: begin!\n", __func__);
4676 nfs_free_seqid(data->arg.open_seqid);
4677 if (data->cancelled != 0) {
4678 struct rpc_task *task;
4679 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4680 data->arg.lock_seqid);
4682 rpc_put_task_async(task);
4683 dprintk("%s: cancelling lock!\n", __func__);
4685 nfs_free_seqid(data->arg.lock_seqid);
4686 nfs4_put_lock_state(data->lsp);
4687 put_nfs_open_context(data->ctx);
4689 dprintk("%s: done!\n", __func__);
4692 static const struct rpc_call_ops nfs4_lock_ops = {
4693 .rpc_call_prepare = nfs4_lock_prepare,
4694 .rpc_call_done = nfs4_lock_done,
4695 .rpc_release = nfs4_lock_release,
4698 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4699 .rpc_call_prepare = nfs4_recover_lock_prepare,
4700 .rpc_call_done = nfs4_lock_done,
4701 .rpc_release = nfs4_lock_release,
4704 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4707 case -NFS4ERR_ADMIN_REVOKED:
4708 case -NFS4ERR_BAD_STATEID:
4709 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4710 if (new_lock_owner != 0 ||
4711 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
4712 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4714 case -NFS4ERR_STALE_STATEID:
4715 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4716 case -NFS4ERR_EXPIRED:
4717 nfs4_schedule_lease_recovery(server->nfs_client);
4721 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4723 struct nfs4_lockdata *data;
4724 struct rpc_task *task;
4725 struct rpc_message msg = {
4726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4727 .rpc_cred = state->owner->so_cred,
4729 struct rpc_task_setup task_setup_data = {
4730 .rpc_client = NFS_CLIENT(state->inode),
4731 .rpc_message = &msg,
4732 .callback_ops = &nfs4_lock_ops,
4733 .workqueue = nfsiod_workqueue,
4734 .flags = RPC_TASK_ASYNC,
4738 dprintk("%s: begin!\n", __func__);
4739 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4740 fl->fl_u.nfs4_fl.owner,
4741 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4745 data->arg.block = 1;
4746 if (recovery_type > NFS_LOCK_NEW) {
4747 if (recovery_type == NFS_LOCK_RECLAIM)
4748 data->arg.reclaim = NFS_LOCK_RECLAIM;
4749 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4751 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4752 msg.rpc_argp = &data->arg;
4753 msg.rpc_resp = &data->res;
4754 task_setup_data.callback_data = data;
4755 task = rpc_run_task(&task_setup_data);
4757 return PTR_ERR(task);
4758 ret = nfs4_wait_for_completion_rpc_task(task);
4760 ret = data->rpc_status;
4762 nfs4_handle_setlk_error(data->server, data->lsp,
4763 data->arg.new_lock_owner, ret);
4765 data->cancelled = 1;
4767 dprintk("%s: done, ret = %d!\n", __func__, ret);
4771 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4773 struct nfs_server *server = NFS_SERVER(state->inode);
4774 struct nfs4_exception exception = {
4775 .inode = state->inode,
4780 /* Cache the lock if possible... */
4781 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4783 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4784 if (err != -NFS4ERR_DELAY)
4786 nfs4_handle_exception(server, err, &exception);
4787 } while (exception.retry);
4791 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4793 struct nfs_server *server = NFS_SERVER(state->inode);
4794 struct nfs4_exception exception = {
4795 .inode = state->inode,
4799 err = nfs4_set_lock_state(state, request);
4803 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4805 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4809 case -NFS4ERR_GRACE:
4810 case -NFS4ERR_DELAY:
4811 nfs4_handle_exception(server, err, &exception);
4814 } while (exception.retry);
4819 #if defined(CONFIG_NFS_V4_1)
4821 * nfs41_check_expired_locks - possibly free a lock stateid
4823 * @state: NFSv4 state for an inode
4825 * Returns NFS_OK if recovery for this stateid is now finished.
4826 * Otherwise a negative NFS4ERR value is returned.
4828 static int nfs41_check_expired_locks(struct nfs4_state *state)
4830 int status, ret = -NFS4ERR_BAD_STATEID;
4831 struct nfs4_lock_state *lsp;
4832 struct nfs_server *server = NFS_SERVER(state->inode);
4834 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4835 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
4836 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4837 if (status != NFS_OK) {
4838 /* Free the stateid unless the server
4839 * informs us the stateid is unrecognized. */
4840 if (status != -NFS4ERR_BAD_STATEID)
4841 nfs41_free_stateid(server,
4843 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
4852 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4854 int status = NFS_OK;
4856 if (test_bit(LK_STATE_IN_USE, &state->flags))
4857 status = nfs41_check_expired_locks(state);
4858 if (status != NFS_OK)
4859 status = nfs4_lock_expired(state, request);
4864 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4866 struct nfs_inode *nfsi = NFS_I(state->inode);
4867 unsigned char fl_flags = request->fl_flags;
4868 int status = -ENOLCK;
4870 if ((fl_flags & FL_POSIX) &&
4871 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4873 /* Is this a delegated open? */
4874 status = nfs4_set_lock_state(state, request);
4877 request->fl_flags |= FL_ACCESS;
4878 status = do_vfs_lock(request->fl_file, request);
4881 down_read(&nfsi->rwsem);
4882 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4883 /* Yes: cache locks! */
4884 /* ...but avoid races with delegation recall... */
4885 request->fl_flags = fl_flags & ~FL_SLEEP;
4886 status = do_vfs_lock(request->fl_file, request);
4889 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4892 /* Note: we always want to sleep here! */
4893 request->fl_flags = fl_flags | FL_SLEEP;
4894 if (do_vfs_lock(request->fl_file, request) < 0)
4895 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4896 "manager!\n", __func__);
4898 up_read(&nfsi->rwsem);
4900 request->fl_flags = fl_flags;
4904 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4906 struct nfs4_exception exception = {
4908 .inode = state->inode,
4913 err = _nfs4_proc_setlk(state, cmd, request);
4914 if (err == -NFS4ERR_DENIED)
4916 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4918 } while (exception.retry);
4923 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4925 struct nfs_open_context *ctx;
4926 struct nfs4_state *state;
4927 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4930 /* verify open state */
4931 ctx = nfs_file_open_context(filp);
4934 if (request->fl_start < 0 || request->fl_end < 0)
4937 if (IS_GETLK(cmd)) {
4939 return nfs4_proc_getlk(state, F_GETLK, request);
4943 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4946 if (request->fl_type == F_UNLCK) {
4948 return nfs4_proc_unlck(state, cmd, request);
4955 * Don't rely on the VFS having checked the file open mode,
4956 * since it won't do this for flock() locks.
4958 switch (request->fl_type) {
4960 if (!(filp->f_mode & FMODE_READ))
4964 if (!(filp->f_mode & FMODE_WRITE))
4969 status = nfs4_proc_setlk(state, cmd, request);
4970 if ((status != -EAGAIN) || IS_SETLK(cmd))
4972 timeout = nfs4_set_lock_task_retry(timeout);
4973 status = -ERESTARTSYS;
4976 } while(status < 0);
4980 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4982 struct nfs_server *server = NFS_SERVER(state->inode);
4983 struct nfs4_exception exception = { };
4986 err = nfs4_set_lock_state(state, fl);
4990 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4993 printk(KERN_ERR "NFS: %s: unhandled error "
4994 "%d.\n", __func__, err);
4998 case -NFS4ERR_EXPIRED:
4999 nfs4_schedule_stateid_recovery(server, state);
5000 case -NFS4ERR_STALE_CLIENTID:
5001 case -NFS4ERR_STALE_STATEID:
5002 nfs4_schedule_lease_recovery(server->nfs_client);
5004 case -NFS4ERR_BADSESSION:
5005 case -NFS4ERR_BADSLOT:
5006 case -NFS4ERR_BAD_HIGH_SLOT:
5007 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5008 case -NFS4ERR_DEADSESSION:
5009 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
5013 * The show must go on: exit, but mark the
5014 * stateid as needing recovery.
5016 case -NFS4ERR_DELEG_REVOKED:
5017 case -NFS4ERR_ADMIN_REVOKED:
5018 case -NFS4ERR_BAD_STATEID:
5019 case -NFS4ERR_OPENMODE:
5020 nfs4_schedule_stateid_recovery(server, state);
5025 * User RPCSEC_GSS context has expired.
5026 * We cannot recover this stateid now, so
5027 * skip it and allow recovery thread to
5033 case -NFS4ERR_DENIED:
5034 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5037 case -NFS4ERR_DELAY:
5040 err = nfs4_handle_exception(server, err, &exception);
5041 } while (exception.retry);
5046 struct nfs_release_lockowner_data {
5047 struct nfs4_lock_state *lsp;
5048 struct nfs_server *server;
5049 struct nfs_release_lockowner_args args;
5052 static void nfs4_release_lockowner_release(void *calldata)
5054 struct nfs_release_lockowner_data *data = calldata;
5055 nfs4_free_lock_state(data->server, data->lsp);
5059 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5060 .rpc_release = nfs4_release_lockowner_release,
5063 int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
5065 struct nfs_server *server = lsp->ls_state->owner->so_server;
5066 struct nfs_release_lockowner_data *data;
5067 struct rpc_message msg = {
5068 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5071 if (server->nfs_client->cl_mvops->minor_version != 0)
5073 data = kmalloc(sizeof(*data), GFP_NOFS);
5077 data->server = server;
5078 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5079 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5080 data->args.lock_owner.s_dev = server->s_dev;
5081 msg.rpc_argp = &data->args;
5082 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5086 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5088 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5089 const void *buf, size_t buflen,
5090 int flags, int type)
5092 if (strcmp(key, "") != 0)
5095 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5098 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5099 void *buf, size_t buflen, int type)
5101 if (strcmp(key, "") != 0)
5104 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5107 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5108 size_t list_len, const char *name,
5109 size_t name_len, int type)
5111 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5113 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5116 if (list && len <= list_len)
5117 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5122 * nfs_fhget will use either the mounted_on_fileid or the fileid
5124 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5126 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5127 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5128 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
5129 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
5132 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
5133 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
5134 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5138 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5139 const struct qstr *name,
5140 struct nfs4_fs_locations *fs_locations,
5143 struct nfs_server *server = NFS_SERVER(dir);
5145 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
5147 struct nfs4_fs_locations_arg args = {
5148 .dir_fh = NFS_FH(dir),
5153 struct nfs4_fs_locations_res res = {
5154 .fs_locations = fs_locations,
5156 struct rpc_message msg = {
5157 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5163 dprintk("%s: start\n", __func__);
5165 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5166 * is not supported */
5167 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5168 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5170 bitmask[0] |= FATTR4_WORD0_FILEID;
5172 nfs_fattr_init(&fs_locations->fattr);
5173 fs_locations->server = server;
5174 fs_locations->nlocations = 0;
5175 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
5176 dprintk("%s: returned status = %d\n", __func__, status);
5180 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5181 const struct qstr *name,
5182 struct nfs4_fs_locations *fs_locations,
5185 struct nfs4_exception exception = { };
5188 err = nfs4_handle_exception(NFS_SERVER(dir),
5189 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
5191 } while (exception.retry);
5195 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5198 struct nfs4_secinfo_arg args = {
5199 .dir_fh = NFS_FH(dir),
5202 struct nfs4_secinfo_res res = {
5205 struct rpc_message msg = {
5206 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5211 dprintk("NFS call secinfo %s\n", name->name);
5212 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5213 dprintk("NFS reply secinfo: %d\n", status);
5217 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5218 struct nfs4_secinfo_flavors *flavors)
5220 struct nfs4_exception exception = { };
5223 err = nfs4_handle_exception(NFS_SERVER(dir),
5224 _nfs4_proc_secinfo(dir, name, flavors),
5226 } while (exception.retry);
5230 #ifdef CONFIG_NFS_V4_1
5232 * Check the exchange flags returned by the server for invalid flags, having
5233 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5236 static int nfs4_check_cl_exchange_flags(u32 flags)
5238 if (flags & ~EXCHGID4_FLAG_MASK_R)
5240 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5241 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5243 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5247 return -NFS4ERR_INVAL;
5251 nfs41_same_server_scope(struct nfs41_server_scope *a,
5252 struct nfs41_server_scope *b)
5254 if (a->server_scope_sz == b->server_scope_sz &&
5255 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5262 * nfs4_proc_bind_conn_to_session()
5264 * The 4.1 client currently uses the same TCP connection for the
5265 * fore and backchannel.
5267 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
5270 struct nfs41_bind_conn_to_session_res res;
5271 struct rpc_message msg = {
5273 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5279 dprintk("--> %s\n", __func__);
5280 BUG_ON(clp == NULL);
5282 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5283 if (unlikely(res.session == NULL)) {
5288 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5290 if (memcmp(res.session->sess_id.data,
5291 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5292 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5296 if (res.dir != NFS4_CDFS4_BOTH) {
5297 dprintk("NFS: %s: Unexpected direction from server\n",
5302 if (res.use_conn_in_rdma_mode) {
5303 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5312 dprintk("<-- %s status= %d\n", __func__, status);
5317 * nfs4_proc_exchange_id()
5319 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5321 * Since the clientid has expired, all compounds using sessions
5322 * associated with the stale clientid will be returning
5323 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5324 * be in some phase of session reset.
5326 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
5328 nfs4_verifier verifier;
5329 struct nfs41_exchange_id_args args = {
5330 .verifier = &verifier,
5332 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
5334 struct nfs41_exchange_id_res res = {
5338 struct rpc_message msg = {
5339 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5345 nfs4_init_boot_verifier(clp, &verifier);
5346 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5348 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5349 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5350 args.id_len, args.id);
5352 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
5354 if (unlikely(res.server_owner == NULL)) {
5359 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
5361 if (unlikely(res.server_scope == NULL)) {
5363 goto out_server_owner;
5366 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
5367 if (unlikely(res.impl_id == NULL)) {
5369 goto out_server_scope;
5372 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5374 status = nfs4_check_cl_exchange_flags(res.flags);
5377 clp->cl_clientid = res.clientid;
5378 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5379 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5380 clp->cl_seqid = res.seqid;
5382 kfree(clp->cl_serverowner);
5383 clp->cl_serverowner = res.server_owner;
5384 res.server_owner = NULL;
5386 /* use the most recent implementation id */
5387 kfree(clp->cl_implid);
5388 clp->cl_implid = res.impl_id;
5390 if (clp->cl_serverscope != NULL &&
5391 !nfs41_same_server_scope(clp->cl_serverscope,
5392 res.server_scope)) {
5393 dprintk("%s: server_scope mismatch detected\n",
5395 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5396 kfree(clp->cl_serverscope);
5397 clp->cl_serverscope = NULL;
5400 if (clp->cl_serverscope == NULL) {
5401 clp->cl_serverscope = res.server_scope;
5408 kfree(res.server_owner);
5410 kfree(res.server_scope);
5412 if (clp->cl_implid != NULL)
5413 dprintk("NFS reply exchange_id: Server Implementation ID: "
5414 "domain: %s, name: %s, date: %llu,%u\n",
5415 clp->cl_implid->domain, clp->cl_implid->name,
5416 clp->cl_implid->date.seconds,
5417 clp->cl_implid->date.nseconds);
5418 dprintk("NFS reply exchange_id: %d\n", status);
5422 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5423 struct rpc_cred *cred)
5425 struct rpc_message msg = {
5426 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5432 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5434 dprintk("NFS: Got error %d from the server %s on "
5435 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5439 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5440 struct rpc_cred *cred)
5445 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5446 ret = _nfs4_proc_destroy_clientid(clp, cred);
5448 case -NFS4ERR_DELAY:
5449 case -NFS4ERR_CLIENTID_BUSY:
5459 int nfs4_destroy_clientid(struct nfs_client *clp)
5461 struct rpc_cred *cred;
5464 if (clp->cl_mvops->minor_version < 1)
5466 if (clp->cl_exchange_flags == 0)
5468 if (clp->cl_preserve_clid)
5470 cred = nfs4_get_exchange_id_cred(clp);
5471 ret = nfs4_proc_destroy_clientid(clp, cred);
5476 case -NFS4ERR_STALE_CLIENTID:
5477 clp->cl_exchange_flags = 0;
5483 struct nfs4_get_lease_time_data {
5484 struct nfs4_get_lease_time_args *args;
5485 struct nfs4_get_lease_time_res *res;
5486 struct nfs_client *clp;
5489 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5493 struct nfs4_get_lease_time_data *data =
5494 (struct nfs4_get_lease_time_data *)calldata;
5496 dprintk("--> %s\n", __func__);
5497 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5498 /* just setup sequence, do not trigger session recovery
5499 since we're invoked within one */
5500 ret = nfs41_setup_sequence(data->clp->cl_session,
5501 &data->args->la_seq_args,
5502 &data->res->lr_seq_res, task);
5504 BUG_ON(ret == -EAGAIN);
5505 rpc_call_start(task);
5506 dprintk("<-- %s\n", __func__);
5510 * Called from nfs4_state_manager thread for session setup, so don't recover
5511 * from sequence operation or clientid errors.
5513 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5515 struct nfs4_get_lease_time_data *data =
5516 (struct nfs4_get_lease_time_data *)calldata;
5518 dprintk("--> %s\n", __func__);
5519 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5521 switch (task->tk_status) {
5522 case -NFS4ERR_DELAY:
5523 case -NFS4ERR_GRACE:
5524 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5525 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5526 task->tk_status = 0;
5528 case -NFS4ERR_RETRY_UNCACHED_REP:
5529 rpc_restart_call_prepare(task);
5532 dprintk("<-- %s\n", __func__);
5535 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5536 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5537 .rpc_call_done = nfs4_get_lease_time_done,
5540 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5542 struct rpc_task *task;
5543 struct nfs4_get_lease_time_args args;
5544 struct nfs4_get_lease_time_res res = {
5545 .lr_fsinfo = fsinfo,
5547 struct nfs4_get_lease_time_data data = {
5552 struct rpc_message msg = {
5553 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5557 struct rpc_task_setup task_setup = {
5558 .rpc_client = clp->cl_rpcclient,
5559 .rpc_message = &msg,
5560 .callback_ops = &nfs4_get_lease_time_ops,
5561 .callback_data = &data,
5562 .flags = RPC_TASK_TIMEOUT,
5566 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5567 dprintk("--> %s\n", __func__);
5568 task = rpc_run_task(&task_setup);
5571 status = PTR_ERR(task);
5573 status = task->tk_status;
5576 dprintk("<-- %s return %d\n", __func__, status);
5581 static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5583 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5586 static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5587 struct nfs4_slot *new,
5591 struct nfs4_slot *old = NULL;
5594 spin_lock(&tbl->slot_tbl_lock);
5598 tbl->max_slots = max_slots;
5600 tbl->highest_used_slotid = -1; /* no slot is currently used */
5601 for (i = 0; i < tbl->max_slots; i++)
5602 tbl->slots[i].seq_nr = ivalue;
5603 spin_unlock(&tbl->slot_tbl_lock);
5608 * (re)Initialise a slot table
5610 static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5613 struct nfs4_slot *new = NULL;
5616 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5617 max_reqs, tbl->max_slots);
5619 /* Does the newly negotiated max_reqs match the existing slot table? */
5620 if (max_reqs != tbl->max_slots) {
5621 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
5627 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
5628 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5629 tbl, tbl->slots, tbl->max_slots);
5631 dprintk("<-- %s: return %d\n", __func__, ret);
5635 /* Destroy the slot table */
5636 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5638 if (session->fc_slot_table.slots != NULL) {
5639 kfree(session->fc_slot_table.slots);
5640 session->fc_slot_table.slots = NULL;
5642 if (session->bc_slot_table.slots != NULL) {
5643 kfree(session->bc_slot_table.slots);
5644 session->bc_slot_table.slots = NULL;
5650 * Initialize or reset the forechannel and backchannel tables
5652 static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
5654 struct nfs4_slot_table *tbl;
5657 dprintk("--> %s\n", __func__);
5659 tbl = &ses->fc_slot_table;
5660 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5661 if (status) /* -ENOMEM */
5664 tbl = &ses->bc_slot_table;
5665 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5666 if (status && tbl->slots == NULL)
5667 /* Fore and back channel share a connection so get
5668 * both slot tables or neither */
5669 nfs4_destroy_slot_tables(ses);
5673 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5675 struct nfs4_session *session;
5676 struct nfs4_slot_table *tbl;
5678 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5682 tbl = &session->fc_slot_table;
5683 tbl->highest_used_slotid = NFS4_NO_SLOT;
5684 spin_lock_init(&tbl->slot_tbl_lock);
5685 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5686 init_completion(&tbl->complete);
5688 tbl = &session->bc_slot_table;
5689 tbl->highest_used_slotid = NFS4_NO_SLOT;
5690 spin_lock_init(&tbl->slot_tbl_lock);
5691 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5692 init_completion(&tbl->complete);
5694 session->session_state = 1<<NFS4_SESSION_INITING;
5700 void nfs4_destroy_session(struct nfs4_session *session)
5702 struct rpc_xprt *xprt;
5703 struct rpc_cred *cred;
5705 cred = nfs4_get_exchange_id_cred(session->clp);
5706 nfs4_proc_destroy_session(session, cred);
5711 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5713 dprintk("%s Destroy backchannel for xprt %p\n",
5715 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
5716 nfs4_destroy_slot_tables(session);
5721 * Initialize the values to be used by the client in CREATE_SESSION
5722 * If nfs4_init_session set the fore channel request and response sizes,
5725 * Set the back channel max_resp_sz_cached to zero to force the client to
5726 * always set csa_cachethis to FALSE because the current implementation
5727 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5729 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5731 struct nfs4_session *session = args->client->cl_session;
5732 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5733 mxresp_sz = session->fc_attrs.max_resp_sz;
5736 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5738 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5739 /* Fore channel attributes */
5740 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5741 args->fc_attrs.max_resp_sz = mxresp_sz;
5742 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5743 args->fc_attrs.max_reqs = max_session_slots;
5745 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5746 "max_ops=%u max_reqs=%u\n",
5748 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5749 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5751 /* Back channel attributes */
5752 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5753 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5754 args->bc_attrs.max_resp_sz_cached = 0;
5755 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5756 args->bc_attrs.max_reqs = 1;
5758 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5759 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5761 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5762 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5763 args->bc_attrs.max_reqs);
5766 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5768 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5769 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5771 if (rcvd->max_resp_sz > sent->max_resp_sz)
5774 * Our requested max_ops is the minimum we need; we're not
5775 * prepared to break up compounds into smaller pieces than that.
5776 * So, no point even trying to continue if the server won't
5779 if (rcvd->max_ops < sent->max_ops)
5781 if (rcvd->max_reqs == 0)
5783 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5784 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5788 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5790 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5791 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5793 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5795 if (rcvd->max_resp_sz < sent->max_resp_sz)
5797 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5799 /* These would render the backchannel useless: */
5800 if (rcvd->max_ops != sent->max_ops)
5802 if (rcvd->max_reqs != sent->max_reqs)
5807 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5808 struct nfs4_session *session)
5812 ret = nfs4_verify_fore_channel_attrs(args, session);
5815 return nfs4_verify_back_channel_attrs(args, session);
5818 static int _nfs4_proc_create_session(struct nfs_client *clp,
5819 struct rpc_cred *cred)
5821 struct nfs4_session *session = clp->cl_session;
5822 struct nfs41_create_session_args args = {
5824 .cb_program = NFS4_CALLBACK,
5826 struct nfs41_create_session_res res = {
5829 struct rpc_message msg = {
5830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5837 nfs4_init_channel_attrs(&args);
5838 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5840 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5843 /* Verify the session's negotiated channel_attrs values */
5844 status = nfs4_verify_channel_attrs(&args, session);
5846 /* Increment the clientid slot sequence id */
5854 * Issues a CREATE_SESSION operation to the server.
5855 * It is the responsibility of the caller to verify the session is
5856 * expired before calling this routine.
5858 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
5862 struct nfs4_session *session = clp->cl_session;
5864 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5866 status = _nfs4_proc_create_session(clp, cred);
5870 /* Init or reset the session slot tables */
5871 status = nfs4_setup_session_slot_tables(session);
5872 dprintk("slot table setup returned %d\n", status);
5876 ptr = (unsigned *)&session->sess_id.data[0];
5877 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5878 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5880 dprintk("<-- %s\n", __func__);
5885 * Issue the over-the-wire RPC DESTROY_SESSION.
5886 * The caller must serialize access to this routine.
5888 int nfs4_proc_destroy_session(struct nfs4_session *session,
5889 struct rpc_cred *cred)
5891 struct rpc_message msg = {
5892 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5893 .rpc_argp = session,
5898 dprintk("--> nfs4_proc_destroy_session\n");
5900 /* session is still being setup */
5901 if (session->clp->cl_cons_state != NFS_CS_READY)
5904 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5907 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
5908 "Session has been destroyed regardless...\n", status);
5910 dprintk("<-- nfs4_proc_destroy_session\n");
5915 * With sessions, the client is not marked ready until after a
5916 * successful EXCHANGE_ID and CREATE_SESSION.
5918 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5919 * other versions of NFS can be tried.
5921 static int nfs41_check_session_ready(struct nfs_client *clp)
5925 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5926 ret = nfs4_client_recover_expired_lease(clp);
5930 if (clp->cl_cons_state < NFS_CS_READY)
5931 return -EPROTONOSUPPORT;
5936 int nfs4_init_session(struct nfs_server *server)
5938 struct nfs_client *clp = server->nfs_client;
5939 struct nfs4_session *session;
5940 unsigned int rsize, wsize;
5942 if (!nfs4_has_session(clp))
5945 session = clp->cl_session;
5946 spin_lock(&clp->cl_lock);
5947 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5949 rsize = server->rsize;
5951 rsize = NFS_MAX_FILE_IO_SIZE;
5952 wsize = server->wsize;
5954 wsize = NFS_MAX_FILE_IO_SIZE;
5956 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5957 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5959 spin_unlock(&clp->cl_lock);
5961 return nfs41_check_session_ready(clp);
5964 int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
5966 struct nfs4_session *session = clp->cl_session;
5969 spin_lock(&clp->cl_lock);
5970 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5972 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
5973 * DS lease to be equal to the MDS lease.
5975 clp->cl_lease_time = lease_time;
5976 clp->cl_last_renewal = jiffies;
5978 spin_unlock(&clp->cl_lock);
5980 ret = nfs41_check_session_ready(clp);
5983 /* Test for the DS role */
5984 if (!is_ds_client(clp))
5988 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5992 * Renew the cl_session lease.
5994 struct nfs4_sequence_data {
5995 struct nfs_client *clp;
5996 struct nfs4_sequence_args args;
5997 struct nfs4_sequence_res res;
6000 static void nfs41_sequence_release(void *data)
6002 struct nfs4_sequence_data *calldata = data;
6003 struct nfs_client *clp = calldata->clp;
6005 if (atomic_read(&clp->cl_count) > 1)
6006 nfs4_schedule_state_renewal(clp);
6007 nfs_put_client(clp);
6011 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6013 switch(task->tk_status) {
6014 case -NFS4ERR_DELAY:
6015 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6018 nfs4_schedule_lease_recovery(clp);
6023 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
6025 struct nfs4_sequence_data *calldata = data;
6026 struct nfs_client *clp = calldata->clp;
6028 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6031 if (task->tk_status < 0) {
6032 dprintk("%s ERROR %d\n", __func__, task->tk_status);
6033 if (atomic_read(&clp->cl_count) == 1)
6036 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6037 rpc_restart_call_prepare(task);
6041 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
6043 dprintk("<-- %s\n", __func__);
6046 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6048 struct nfs4_sequence_data *calldata = data;
6049 struct nfs_client *clp = calldata->clp;
6050 struct nfs4_sequence_args *args;
6051 struct nfs4_sequence_res *res;
6053 args = task->tk_msg.rpc_argp;
6054 res = task->tk_msg.rpc_resp;
6056 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
6058 rpc_call_start(task);
6061 static const struct rpc_call_ops nfs41_sequence_ops = {
6062 .rpc_call_done = nfs41_sequence_call_done,
6063 .rpc_call_prepare = nfs41_sequence_prepare,
6064 .rpc_release = nfs41_sequence_release,
6067 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6069 struct nfs4_sequence_data *calldata;
6070 struct rpc_message msg = {
6071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6074 struct rpc_task_setup task_setup_data = {
6075 .rpc_client = clp->cl_rpcclient,
6076 .rpc_message = &msg,
6077 .callback_ops = &nfs41_sequence_ops,
6078 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6081 if (!atomic_inc_not_zero(&clp->cl_count))
6082 return ERR_PTR(-EIO);
6083 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6084 if (calldata == NULL) {
6085 nfs_put_client(clp);
6086 return ERR_PTR(-ENOMEM);
6088 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
6089 msg.rpc_argp = &calldata->args;
6090 msg.rpc_resp = &calldata->res;
6091 calldata->clp = clp;
6092 task_setup_data.callback_data = calldata;
6094 return rpc_run_task(&task_setup_data);
6097 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
6099 struct rpc_task *task;
6102 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6104 task = _nfs41_proc_sequence(clp, cred);
6106 ret = PTR_ERR(task);
6108 rpc_put_task_async(task);
6109 dprintk("<-- %s status=%d\n", __func__, ret);
6113 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6115 struct rpc_task *task;
6118 task = _nfs41_proc_sequence(clp, cred);
6120 ret = PTR_ERR(task);
6123 ret = rpc_wait_for_completion_task(task);
6125 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6127 if (task->tk_status == 0)
6128 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
6129 ret = task->tk_status;
6133 dprintk("<-- %s status=%d\n", __func__, ret);
6137 struct nfs4_reclaim_complete_data {
6138 struct nfs_client *clp;
6139 struct nfs41_reclaim_complete_args arg;
6140 struct nfs41_reclaim_complete_res res;
6143 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6145 struct nfs4_reclaim_complete_data *calldata = data;
6147 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6148 if (nfs41_setup_sequence(calldata->clp->cl_session,
6149 &calldata->arg.seq_args,
6150 &calldata->res.seq_res, task))
6153 rpc_call_start(task);
6156 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6158 switch(task->tk_status) {
6160 case -NFS4ERR_COMPLETE_ALREADY:
6161 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6163 case -NFS4ERR_DELAY:
6164 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6166 case -NFS4ERR_RETRY_UNCACHED_REP:
6169 nfs4_schedule_lease_recovery(clp);
6174 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6176 struct nfs4_reclaim_complete_data *calldata = data;
6177 struct nfs_client *clp = calldata->clp;
6178 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6180 dprintk("--> %s\n", __func__);
6181 if (!nfs41_sequence_done(task, res))
6184 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6185 rpc_restart_call_prepare(task);
6188 dprintk("<-- %s\n", __func__);
6191 static void nfs4_free_reclaim_complete_data(void *data)
6193 struct nfs4_reclaim_complete_data *calldata = data;
6198 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6199 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6200 .rpc_call_done = nfs4_reclaim_complete_done,
6201 .rpc_release = nfs4_free_reclaim_complete_data,
6205 * Issue a global reclaim complete.
6207 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6209 struct nfs4_reclaim_complete_data *calldata;
6210 struct rpc_task *task;
6211 struct rpc_message msg = {
6212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6214 struct rpc_task_setup task_setup_data = {
6215 .rpc_client = clp->cl_rpcclient,
6216 .rpc_message = &msg,
6217 .callback_ops = &nfs4_reclaim_complete_call_ops,
6218 .flags = RPC_TASK_ASYNC,
6220 int status = -ENOMEM;
6222 dprintk("--> %s\n", __func__);
6223 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6224 if (calldata == NULL)
6226 calldata->clp = clp;
6227 calldata->arg.one_fs = 0;
6229 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
6230 msg.rpc_argp = &calldata->arg;
6231 msg.rpc_resp = &calldata->res;
6232 task_setup_data.callback_data = calldata;
6233 task = rpc_run_task(&task_setup_data);
6235 status = PTR_ERR(task);
6238 status = nfs4_wait_for_completion_rpc_task(task);
6240 status = task->tk_status;
6244 dprintk("<-- %s status=%d\n", __func__, status);
6249 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6251 struct nfs4_layoutget *lgp = calldata;
6252 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6254 dprintk("--> %s\n", __func__);
6255 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6256 * right now covering the LAYOUTGET we are about to send.
6257 * However, that is not so catastrophic, and there seems
6258 * to be no way to prevent it completely.
6260 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
6261 &lgp->res.seq_res, task))
6263 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6264 NFS_I(lgp->args.inode)->layout,
6265 lgp->args.ctx->state)) {
6266 rpc_exit(task, NFS4_OK);
6269 rpc_call_start(task);
6272 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6274 struct nfs4_layoutget *lgp = calldata;
6275 struct inode *inode = lgp->args.inode;
6276 struct nfs_server *server = NFS_SERVER(inode);
6277 struct pnfs_layout_hdr *lo;
6278 struct nfs4_state *state = NULL;
6280 dprintk("--> %s\n", __func__);
6282 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6285 switch (task->tk_status) {
6288 case -NFS4ERR_LAYOUTTRYLATER:
6289 case -NFS4ERR_RECALLCONFLICT:
6290 task->tk_status = -NFS4ERR_DELAY;
6292 case -NFS4ERR_EXPIRED:
6293 case -NFS4ERR_BAD_STATEID:
6294 spin_lock(&inode->i_lock);
6295 lo = NFS_I(inode)->layout;
6296 if (!lo || list_empty(&lo->plh_segs)) {
6297 spin_unlock(&inode->i_lock);
6298 /* If the open stateid was bad, then recover it. */
6299 state = lgp->args.ctx->state;
6303 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6304 spin_unlock(&inode->i_lock);
6305 /* Mark the bad layout state as invalid, then
6306 * retry using the open stateid. */
6307 pnfs_free_lseg_list(&head);
6310 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6311 rpc_restart_call_prepare(task);
6313 dprintk("<-- %s\n", __func__);
6316 static size_t max_response_pages(struct nfs_server *server)
6318 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6319 return nfs_page_array_len(0, max_resp_sz);
6322 static void nfs4_free_pages(struct page **pages, size_t size)
6329 for (i = 0; i < size; i++) {
6332 __free_page(pages[i]);
6337 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6339 struct page **pages;
6342 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6344 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6348 for (i = 0; i < size; i++) {
6349 pages[i] = alloc_page(gfp_flags);
6351 dprintk("%s: failed to allocate page\n", __func__);
6352 nfs4_free_pages(pages, size);
6360 static void nfs4_layoutget_release(void *calldata)
6362 struct nfs4_layoutget *lgp = calldata;
6363 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6364 size_t max_pages = max_response_pages(server);
6366 dprintk("--> %s\n", __func__);
6367 nfs4_free_pages(lgp->args.layout.pages, max_pages);
6368 put_nfs_open_context(lgp->args.ctx);
6370 dprintk("<-- %s\n", __func__);
6373 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6374 .rpc_call_prepare = nfs4_layoutget_prepare,
6375 .rpc_call_done = nfs4_layoutget_done,
6376 .rpc_release = nfs4_layoutget_release,
6379 struct pnfs_layout_segment *
6380 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
6382 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6383 size_t max_pages = max_response_pages(server);
6384 struct rpc_task *task;
6385 struct rpc_message msg = {
6386 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6387 .rpc_argp = &lgp->args,
6388 .rpc_resp = &lgp->res,
6390 struct rpc_task_setup task_setup_data = {
6391 .rpc_client = server->client,
6392 .rpc_message = &msg,
6393 .callback_ops = &nfs4_layoutget_call_ops,
6394 .callback_data = lgp,
6395 .flags = RPC_TASK_ASYNC,
6397 struct pnfs_layout_segment *lseg = NULL;
6400 dprintk("--> %s\n", __func__);
6402 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6403 if (!lgp->args.layout.pages) {
6404 nfs4_layoutget_release(lgp);
6405 return ERR_PTR(-ENOMEM);
6407 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6409 lgp->res.layoutp = &lgp->args.layout;
6410 lgp->res.seq_res.sr_slot = NULL;
6411 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
6412 task = rpc_run_task(&task_setup_data);
6414 return ERR_CAST(task);
6415 status = nfs4_wait_for_completion_rpc_task(task);
6417 status = task->tk_status;
6419 lseg = pnfs_layout_process(lgp);
6421 dprintk("<-- %s status=%d\n", __func__, status);
6423 return ERR_PTR(status);
6428 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6430 struct nfs4_layoutreturn *lrp = calldata;
6432 dprintk("--> %s\n", __func__);
6433 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
6434 &lrp->res.seq_res, task))
6436 rpc_call_start(task);
6439 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6441 struct nfs4_layoutreturn *lrp = calldata;
6442 struct nfs_server *server;
6444 dprintk("--> %s\n", __func__);
6446 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6449 server = NFS_SERVER(lrp->args.inode);
6450 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6451 rpc_restart_call_prepare(task);
6454 dprintk("<-- %s\n", __func__);
6457 static void nfs4_layoutreturn_release(void *calldata)
6459 struct nfs4_layoutreturn *lrp = calldata;
6460 struct pnfs_layout_hdr *lo = lrp->args.layout;
6462 dprintk("--> %s\n", __func__);
6463 spin_lock(&lo->plh_inode->i_lock);
6464 if (lrp->res.lrs_present)
6465 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6466 lo->plh_block_lgets--;
6467 spin_unlock(&lo->plh_inode->i_lock);
6468 pnfs_put_layout_hdr(lrp->args.layout);
6470 dprintk("<-- %s\n", __func__);
6473 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6474 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6475 .rpc_call_done = nfs4_layoutreturn_done,
6476 .rpc_release = nfs4_layoutreturn_release,
6479 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6481 struct rpc_task *task;
6482 struct rpc_message msg = {
6483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6484 .rpc_argp = &lrp->args,
6485 .rpc_resp = &lrp->res,
6487 struct rpc_task_setup task_setup_data = {
6488 .rpc_client = lrp->clp->cl_rpcclient,
6489 .rpc_message = &msg,
6490 .callback_ops = &nfs4_layoutreturn_call_ops,
6491 .callback_data = lrp,
6495 dprintk("--> %s\n", __func__);
6496 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
6497 task = rpc_run_task(&task_setup_data);
6499 return PTR_ERR(task);
6500 status = task->tk_status;
6501 dprintk("<-- %s status=%d\n", __func__, status);
6507 * Retrieve the list of Data Server devices from the MDS.
6509 static int _nfs4_getdevicelist(struct nfs_server *server,
6510 const struct nfs_fh *fh,
6511 struct pnfs_devicelist *devlist)
6513 struct nfs4_getdevicelist_args args = {
6515 .layoutclass = server->pnfs_curr_ld->id,
6517 struct nfs4_getdevicelist_res res = {
6520 struct rpc_message msg = {
6521 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6527 dprintk("--> %s\n", __func__);
6528 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6530 dprintk("<-- %s status=%d\n", __func__, status);
6534 int nfs4_proc_getdevicelist(struct nfs_server *server,
6535 const struct nfs_fh *fh,
6536 struct pnfs_devicelist *devlist)
6538 struct nfs4_exception exception = { };
6542 err = nfs4_handle_exception(server,
6543 _nfs4_getdevicelist(server, fh, devlist),
6545 } while (exception.retry);
6547 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6548 err, devlist->num_devs);
6552 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6555 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6557 struct nfs4_getdeviceinfo_args args = {
6560 struct nfs4_getdeviceinfo_res res = {
6563 struct rpc_message msg = {
6564 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6570 dprintk("--> %s\n", __func__);
6571 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6572 dprintk("<-- %s status=%d\n", __func__, status);
6577 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6579 struct nfs4_exception exception = { };
6583 err = nfs4_handle_exception(server,
6584 _nfs4_proc_getdeviceinfo(server, pdev),
6586 } while (exception.retry);
6589 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6591 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6593 struct nfs4_layoutcommit_data *data = calldata;
6594 struct nfs_server *server = NFS_SERVER(data->args.inode);
6596 if (nfs4_setup_sequence(server, &data->args.seq_args,
6597 &data->res.seq_res, task))
6599 rpc_call_start(task);
6603 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6605 struct nfs4_layoutcommit_data *data = calldata;
6606 struct nfs_server *server = NFS_SERVER(data->args.inode);
6608 if (!nfs4_sequence_done(task, &data->res.seq_res))
6611 switch (task->tk_status) { /* Just ignore these failures */
6612 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6613 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6614 case -NFS4ERR_BADLAYOUT: /* no layout */
6615 case -NFS4ERR_GRACE: /* loca_recalim always false */
6616 task->tk_status = 0;
6619 nfs_post_op_update_inode_force_wcc(data->args.inode,
6623 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6624 rpc_restart_call_prepare(task);
6630 static void nfs4_layoutcommit_release(void *calldata)
6632 struct nfs4_layoutcommit_data *data = calldata;
6633 struct pnfs_layout_segment *lseg, *tmp;
6634 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
6636 pnfs_cleanup_layoutcommit(data);
6637 /* Matched by references in pnfs_set_layoutcommit */
6638 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6639 list_del_init(&lseg->pls_lc_list);
6640 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6642 pnfs_put_lseg(lseg);
6645 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6646 smp_mb__after_clear_bit();
6647 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6649 put_rpccred(data->cred);
6653 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6654 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6655 .rpc_call_done = nfs4_layoutcommit_done,
6656 .rpc_release = nfs4_layoutcommit_release,
6660 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6662 struct rpc_message msg = {
6663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6664 .rpc_argp = &data->args,
6665 .rpc_resp = &data->res,
6666 .rpc_cred = data->cred,
6668 struct rpc_task_setup task_setup_data = {
6669 .task = &data->task,
6670 .rpc_client = NFS_CLIENT(data->args.inode),
6671 .rpc_message = &msg,
6672 .callback_ops = &nfs4_layoutcommit_ops,
6673 .callback_data = data,
6674 .flags = RPC_TASK_ASYNC,
6676 struct rpc_task *task;
6679 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6680 "lbw: %llu inode %lu\n",
6681 data->task.tk_pid, sync,
6682 data->args.lastbytewritten,
6683 data->args.inode->i_ino);
6685 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6686 task = rpc_run_task(&task_setup_data);
6688 return PTR_ERR(task);
6691 status = nfs4_wait_for_completion_rpc_task(task);
6694 status = task->tk_status;
6696 dprintk("%s: status %d\n", __func__, status);
6702 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6703 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6705 struct nfs41_secinfo_no_name_args args = {
6706 .style = SECINFO_STYLE_CURRENT_FH,
6708 struct nfs4_secinfo_res res = {
6711 struct rpc_message msg = {
6712 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6716 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6720 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6721 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6723 struct nfs4_exception exception = { };
6726 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6729 case -NFS4ERR_WRONGSEC:
6730 case -NFS4ERR_NOTSUPP:
6733 err = nfs4_handle_exception(server, err, &exception);
6735 } while (exception.retry);
6741 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6742 struct nfs_fsinfo *info)
6746 rpc_authflavor_t flavor;
6747 struct nfs4_secinfo_flavors *flavors;
6749 page = alloc_page(GFP_KERNEL);
6755 flavors = page_address(page);
6756 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6759 * Fall back on "guess and check" method if
6760 * the server doesn't support SECINFO_NO_NAME
6762 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6763 err = nfs4_find_root_sec(server, fhandle, info);
6769 flavor = nfs_find_best_sec(flavors);
6771 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6781 static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6784 struct nfs41_test_stateid_args args = {
6787 struct nfs41_test_stateid_res res;
6788 struct rpc_message msg = {
6789 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6794 dprintk("NFS call test_stateid %p\n", stateid);
6795 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6796 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6797 if (status != NFS_OK) {
6798 dprintk("NFS reply test_stateid: failed, %d\n", status);
6801 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
6806 * nfs41_test_stateid - perform a TEST_STATEID operation
6808 * @server: server / transport on which to perform the operation
6809 * @stateid: state ID to test
6811 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6812 * Otherwise a negative NFS4ERR value is returned if the operation
6813 * failed or the state ID is not currently valid.
6815 static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6817 struct nfs4_exception exception = { };
6820 err = _nfs41_test_stateid(server, stateid);
6821 if (err != -NFS4ERR_DELAY)
6823 nfs4_handle_exception(server, err, &exception);
6824 } while (exception.retry);
6828 static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6830 struct nfs41_free_stateid_args args = {
6833 struct nfs41_free_stateid_res res;
6834 struct rpc_message msg = {
6835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6841 dprintk("NFS call free_stateid %p\n", stateid);
6842 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6843 status = nfs4_call_sync_sequence(server->client, server, &msg,
6844 &args.seq_args, &res.seq_res, 1);
6845 dprintk("NFS reply free_stateid: %d\n", status);
6850 * nfs41_free_stateid - perform a FREE_STATEID operation
6852 * @server: server / transport on which to perform the operation
6853 * @stateid: state ID to release
6855 * Returns NFS_OK if the server freed "stateid". Otherwise a
6856 * negative NFS4ERR value is returned.
6858 static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6860 struct nfs4_exception exception = { };
6863 err = _nfs4_free_stateid(server, stateid);
6864 if (err != -NFS4ERR_DELAY)
6866 nfs4_handle_exception(server, err, &exception);
6867 } while (exception.retry);
6871 static bool nfs41_match_stateid(const nfs4_stateid *s1,
6872 const nfs4_stateid *s2)
6874 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
6877 if (s1->seqid == s2->seqid)
6879 if (s1->seqid == 0 || s2->seqid == 0)
6885 #endif /* CONFIG_NFS_V4_1 */
6887 static bool nfs4_match_stateid(const nfs4_stateid *s1,
6888 const nfs4_stateid *s2)
6890 return nfs4_stateid_match(s1, s2);
6894 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6895 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6896 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6897 .recover_open = nfs4_open_reclaim,
6898 .recover_lock = nfs4_lock_reclaim,
6899 .establish_clid = nfs4_init_clientid,
6900 .get_clid_cred = nfs4_get_setclientid_cred,
6901 .detect_trunking = nfs40_discover_server_trunking,
6904 #if defined(CONFIG_NFS_V4_1)
6905 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6906 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6907 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6908 .recover_open = nfs4_open_reclaim,
6909 .recover_lock = nfs4_lock_reclaim,
6910 .establish_clid = nfs41_init_clientid,
6911 .get_clid_cred = nfs4_get_exchange_id_cred,
6912 .reclaim_complete = nfs41_proc_reclaim_complete,
6913 .detect_trunking = nfs41_discover_server_trunking,
6915 #endif /* CONFIG_NFS_V4_1 */
6917 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6918 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6919 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6920 .recover_open = nfs4_open_expired,
6921 .recover_lock = nfs4_lock_expired,
6922 .establish_clid = nfs4_init_clientid,
6923 .get_clid_cred = nfs4_get_setclientid_cred,
6926 #if defined(CONFIG_NFS_V4_1)
6927 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6928 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6929 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6930 .recover_open = nfs41_open_expired,
6931 .recover_lock = nfs41_lock_expired,
6932 .establish_clid = nfs41_init_clientid,
6933 .get_clid_cred = nfs4_get_exchange_id_cred,
6935 #endif /* CONFIG_NFS_V4_1 */
6937 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6938 .sched_state_renewal = nfs4_proc_async_renew,
6939 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6940 .renew_lease = nfs4_proc_renew,
6943 #if defined(CONFIG_NFS_V4_1)
6944 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6945 .sched_state_renewal = nfs41_proc_async_sequence,
6946 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
6947 .renew_lease = nfs4_proc_sequence,
6951 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6953 .call_sync = _nfs4_call_sync,
6954 .match_stateid = nfs4_match_stateid,
6955 .find_root_sec = nfs4_find_root_sec,
6956 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6957 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6958 .state_renewal_ops = &nfs40_state_renewal_ops,
6961 #if defined(CONFIG_NFS_V4_1)
6962 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6964 .call_sync = _nfs4_call_sync_session,
6965 .match_stateid = nfs41_match_stateid,
6966 .find_root_sec = nfs41_find_root_sec,
6967 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6968 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6969 .state_renewal_ops = &nfs41_state_renewal_ops,
6973 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6974 [0] = &nfs_v4_0_minor_ops,
6975 #if defined(CONFIG_NFS_V4_1)
6976 [1] = &nfs_v4_1_minor_ops,
6980 const struct inode_operations nfs4_dir_inode_operations = {
6981 .create = nfs_create,
6982 .lookup = nfs_lookup,
6983 .atomic_open = nfs_atomic_open,
6985 .unlink = nfs_unlink,
6986 .symlink = nfs_symlink,
6990 .rename = nfs_rename,
6991 .permission = nfs_permission,
6992 .getattr = nfs_getattr,
6993 .setattr = nfs_setattr,
6994 .getxattr = generic_getxattr,
6995 .setxattr = generic_setxattr,
6996 .listxattr = generic_listxattr,
6997 .removexattr = generic_removexattr,
7000 static const struct inode_operations nfs4_file_inode_operations = {
7001 .permission = nfs_permission,
7002 .getattr = nfs_getattr,
7003 .setattr = nfs_setattr,
7004 .getxattr = generic_getxattr,
7005 .setxattr = generic_setxattr,
7006 .listxattr = generic_listxattr,
7007 .removexattr = generic_removexattr,
7010 const struct nfs_rpc_ops nfs_v4_clientops = {
7011 .version = 4, /* protocol version */
7012 .dentry_ops = &nfs4_dentry_operations,
7013 .dir_inode_ops = &nfs4_dir_inode_operations,
7014 .file_inode_ops = &nfs4_file_inode_operations,
7015 .file_ops = &nfs4_file_operations,
7016 .getroot = nfs4_proc_get_root,
7017 .submount = nfs4_submount,
7018 .try_mount = nfs4_try_mount,
7019 .getattr = nfs4_proc_getattr,
7020 .setattr = nfs4_proc_setattr,
7021 .lookup = nfs4_proc_lookup,
7022 .access = nfs4_proc_access,
7023 .readlink = nfs4_proc_readlink,
7024 .create = nfs4_proc_create,
7025 .remove = nfs4_proc_remove,
7026 .unlink_setup = nfs4_proc_unlink_setup,
7027 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
7028 .unlink_done = nfs4_proc_unlink_done,
7029 .rename = nfs4_proc_rename,
7030 .rename_setup = nfs4_proc_rename_setup,
7031 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
7032 .rename_done = nfs4_proc_rename_done,
7033 .link = nfs4_proc_link,
7034 .symlink = nfs4_proc_symlink,
7035 .mkdir = nfs4_proc_mkdir,
7036 .rmdir = nfs4_proc_remove,
7037 .readdir = nfs4_proc_readdir,
7038 .mknod = nfs4_proc_mknod,
7039 .statfs = nfs4_proc_statfs,
7040 .fsinfo = nfs4_proc_fsinfo,
7041 .pathconf = nfs4_proc_pathconf,
7042 .set_capabilities = nfs4_server_capabilities,
7043 .decode_dirent = nfs4_decode_dirent,
7044 .read_setup = nfs4_proc_read_setup,
7045 .read_pageio_init = pnfs_pageio_init_read,
7046 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
7047 .read_done = nfs4_read_done,
7048 .write_setup = nfs4_proc_write_setup,
7049 .write_pageio_init = pnfs_pageio_init_write,
7050 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
7051 .write_done = nfs4_write_done,
7052 .commit_setup = nfs4_proc_commit_setup,
7053 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
7054 .commit_done = nfs4_commit_done,
7055 .lock = nfs4_proc_lock,
7056 .clear_acl_cache = nfs4_zap_acl_attr,
7057 .close_context = nfs4_close_context,
7058 .open_context = nfs4_atomic_open,
7059 .have_delegation = nfs4_have_delegation,
7060 .return_delegation = nfs4_inode_return_delegation,
7061 .alloc_client = nfs4_alloc_client,
7062 .init_client = nfs4_init_client,
7063 .free_client = nfs4_free_client,
7064 .create_server = nfs4_create_server,
7065 .clone_server = nfs_clone_server,
7068 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7069 .prefix = XATTR_NAME_NFSV4_ACL,
7070 .list = nfs4_xattr_list_nfs4_acl,
7071 .get = nfs4_xattr_get_nfs4_acl,
7072 .set = nfs4_xattr_set_nfs4_acl,
7075 const struct xattr_handler *nfs4_xattr_handlers[] = {
7076 &nfs4_xattr_nfs4_acl_handler,