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 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
866 * will return permission denied for all bits until close */
867 if (!(flags & O_EXCL)) {
868 /* ask server to check for all possible rights as results
870 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
871 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
872 p->o_res.access_request = p->o_arg.access;
874 p->o_arg.clientid = server->nfs_client->cl_clientid;
875 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
876 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
877 p->o_arg.name = &dentry->d_name;
878 p->o_arg.server = server;
879 p->o_arg.bitmask = server->attr_bitmask;
880 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
881 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
882 if (attrs != NULL && attrs->ia_valid != 0) {
885 p->o_arg.u.attrs = &p->attrs;
886 memcpy(&p->attrs, attrs, sizeof(p->attrs));
889 verf[1] = current->pid;
890 memcpy(p->o_arg.u.verifier.data, verf,
891 sizeof(p->o_arg.u.verifier.data));
893 p->c_arg.fh = &p->o_res.fh;
894 p->c_arg.stateid = &p->o_res.stateid;
895 p->c_arg.seqid = p->o_arg.seqid;
896 nfs4_init_opendata_res(p);
906 static void nfs4_opendata_free(struct kref *kref)
908 struct nfs4_opendata *p = container_of(kref,
909 struct nfs4_opendata, kref);
910 struct super_block *sb = p->dentry->d_sb;
912 nfs_free_seqid(p->o_arg.seqid);
913 if (p->state != NULL)
914 nfs4_put_open_state(p->state);
915 nfs4_put_state_owner(p->owner);
919 nfs_fattr_free_names(&p->f_attr);
923 static void nfs4_opendata_put(struct nfs4_opendata *p)
926 kref_put(&p->kref, nfs4_opendata_free);
929 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
933 ret = rpc_wait_for_completion_task(task);
937 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
941 if (open_mode & (O_EXCL|O_TRUNC))
943 switch (mode & (FMODE_READ|FMODE_WRITE)) {
945 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
946 && state->n_rdonly != 0;
949 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
950 && state->n_wronly != 0;
952 case FMODE_READ|FMODE_WRITE:
953 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
954 && state->n_rdwr != 0;
960 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
962 if (delegation == NULL)
964 if ((delegation->type & fmode) != fmode)
966 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
968 nfs_mark_delegation_referenced(delegation);
972 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
981 case FMODE_READ|FMODE_WRITE:
984 nfs4_state_set_mode_locked(state, state->state | fmode);
987 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
989 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
990 nfs4_stateid_copy(&state->stateid, stateid);
991 nfs4_stateid_copy(&state->open_stateid, stateid);
994 set_bit(NFS_O_RDONLY_STATE, &state->flags);
997 set_bit(NFS_O_WRONLY_STATE, &state->flags);
999 case FMODE_READ|FMODE_WRITE:
1000 set_bit(NFS_O_RDWR_STATE, &state->flags);
1004 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1006 write_seqlock(&state->seqlock);
1007 nfs_set_open_stateid_locked(state, stateid, fmode);
1008 write_sequnlock(&state->seqlock);
1011 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1014 * Protect the call to nfs4_state_set_mode_locked and
1015 * serialise the stateid update
1017 write_seqlock(&state->seqlock);
1018 if (deleg_stateid != NULL) {
1019 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1020 set_bit(NFS_DELEGATED_STATE, &state->flags);
1022 if (open_stateid != NULL)
1023 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1024 write_sequnlock(&state->seqlock);
1025 spin_lock(&state->owner->so_lock);
1026 update_open_stateflags(state, fmode);
1027 spin_unlock(&state->owner->so_lock);
1030 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1032 struct nfs_inode *nfsi = NFS_I(state->inode);
1033 struct nfs_delegation *deleg_cur;
1036 fmode &= (FMODE_READ|FMODE_WRITE);
1039 deleg_cur = rcu_dereference(nfsi->delegation);
1040 if (deleg_cur == NULL)
1043 spin_lock(&deleg_cur->lock);
1044 if (nfsi->delegation != deleg_cur ||
1045 (deleg_cur->type & fmode) != fmode)
1046 goto no_delegation_unlock;
1048 if (delegation == NULL)
1049 delegation = &deleg_cur->stateid;
1050 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1051 goto no_delegation_unlock;
1053 nfs_mark_delegation_referenced(deleg_cur);
1054 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1056 no_delegation_unlock:
1057 spin_unlock(&deleg_cur->lock);
1061 if (!ret && open_stateid != NULL) {
1062 __update_open_stateid(state, open_stateid, NULL, fmode);
1070 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1072 struct nfs_delegation *delegation;
1075 delegation = rcu_dereference(NFS_I(inode)->delegation);
1076 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1081 nfs4_inode_return_delegation(inode);
1084 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1086 struct nfs4_state *state = opendata->state;
1087 struct nfs_inode *nfsi = NFS_I(state->inode);
1088 struct nfs_delegation *delegation;
1089 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
1090 fmode_t fmode = opendata->o_arg.fmode;
1091 nfs4_stateid stateid;
1095 if (can_open_cached(state, fmode, open_mode)) {
1096 spin_lock(&state->owner->so_lock);
1097 if (can_open_cached(state, fmode, open_mode)) {
1098 update_open_stateflags(state, fmode);
1099 spin_unlock(&state->owner->so_lock);
1100 goto out_return_state;
1102 spin_unlock(&state->owner->so_lock);
1105 delegation = rcu_dereference(nfsi->delegation);
1106 if (!can_open_delegated(delegation, fmode)) {
1110 /* Save the delegation */
1111 nfs4_stateid_copy(&stateid, &delegation->stateid);
1113 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1118 /* Try to update the stateid using the delegation */
1119 if (update_open_stateid(state, NULL, &stateid, fmode))
1120 goto out_return_state;
1123 return ERR_PTR(ret);
1125 atomic_inc(&state->count);
1129 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1131 struct inode *inode;
1132 struct nfs4_state *state = NULL;
1133 struct nfs_delegation *delegation;
1136 if (!data->rpc_done) {
1137 state = nfs4_try_open_cached(data);
1142 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1144 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1145 ret = PTR_ERR(inode);
1149 state = nfs4_get_open_state(inode, data->owner);
1152 if (data->o_res.delegation_type != 0) {
1153 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1154 int delegation_flags = 0;
1157 delegation = rcu_dereference(NFS_I(inode)->delegation);
1159 delegation_flags = delegation->flags;
1161 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1162 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1163 "returning a delegation for "
1164 "OPEN(CLAIM_DELEGATE_CUR)\n",
1166 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1167 nfs_inode_set_delegation(state->inode,
1168 data->owner->so_cred,
1171 nfs_inode_reclaim_delegation(state->inode,
1172 data->owner->so_cred,
1176 update_open_stateid(state, &data->o_res.stateid, NULL,
1184 return ERR_PTR(ret);
1187 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1189 struct nfs_inode *nfsi = NFS_I(state->inode);
1190 struct nfs_open_context *ctx;
1192 spin_lock(&state->inode->i_lock);
1193 list_for_each_entry(ctx, &nfsi->open_files, list) {
1194 if (ctx->state != state)
1196 get_nfs_open_context(ctx);
1197 spin_unlock(&state->inode->i_lock);
1200 spin_unlock(&state->inode->i_lock);
1201 return ERR_PTR(-ENOENT);
1204 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1206 struct nfs4_opendata *opendata;
1208 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1209 if (opendata == NULL)
1210 return ERR_PTR(-ENOMEM);
1211 opendata->state = state;
1212 atomic_inc(&state->count);
1216 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1218 struct nfs4_state *newstate;
1221 opendata->o_arg.open_flags = 0;
1222 opendata->o_arg.fmode = fmode;
1223 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1224 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1225 nfs4_init_opendata_res(opendata);
1226 ret = _nfs4_recover_proc_open(opendata);
1229 newstate = nfs4_opendata_to_nfs4_state(opendata);
1230 if (IS_ERR(newstate))
1231 return PTR_ERR(newstate);
1232 nfs4_close_state(newstate, fmode);
1237 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1239 struct nfs4_state *newstate;
1242 /* memory barrier prior to reading state->n_* */
1243 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1245 if (state->n_rdwr != 0) {
1246 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1247 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1250 if (newstate != state)
1253 if (state->n_wronly != 0) {
1254 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1255 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1258 if (newstate != state)
1261 if (state->n_rdonly != 0) {
1262 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1263 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1266 if (newstate != state)
1270 * We may have performed cached opens for all three recoveries.
1271 * Check if we need to update the current stateid.
1273 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1274 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1275 write_seqlock(&state->seqlock);
1276 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1277 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1278 write_sequnlock(&state->seqlock);
1285 * reclaim state on the server after a reboot.
1287 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1289 struct nfs_delegation *delegation;
1290 struct nfs4_opendata *opendata;
1291 fmode_t delegation_type = 0;
1294 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1295 if (IS_ERR(opendata))
1296 return PTR_ERR(opendata);
1297 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1298 opendata->o_arg.fh = NFS_FH(state->inode);
1300 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1301 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1302 delegation_type = delegation->type;
1304 opendata->o_arg.u.delegation_type = delegation_type;
1305 status = nfs4_open_recover(opendata, state);
1306 nfs4_opendata_put(opendata);
1310 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1312 struct nfs_server *server = NFS_SERVER(state->inode);
1313 struct nfs4_exception exception = { };
1316 err = _nfs4_do_open_reclaim(ctx, state);
1317 if (err != -NFS4ERR_DELAY)
1319 nfs4_handle_exception(server, err, &exception);
1320 } while (exception.retry);
1324 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1326 struct nfs_open_context *ctx;
1329 ctx = nfs4_state_find_open_context(state);
1331 return PTR_ERR(ctx);
1332 ret = nfs4_do_open_reclaim(ctx, state);
1333 put_nfs_open_context(ctx);
1337 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1339 struct nfs4_opendata *opendata;
1342 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1343 if (IS_ERR(opendata))
1344 return PTR_ERR(opendata);
1345 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1346 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1347 ret = nfs4_open_recover(opendata, state);
1348 nfs4_opendata_put(opendata);
1352 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1354 struct nfs4_exception exception = { };
1355 struct nfs_server *server = NFS_SERVER(state->inode);
1358 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1364 case -NFS4ERR_BADSESSION:
1365 case -NFS4ERR_BADSLOT:
1366 case -NFS4ERR_BAD_HIGH_SLOT:
1367 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1368 case -NFS4ERR_DEADSESSION:
1369 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1371 case -NFS4ERR_STALE_CLIENTID:
1372 case -NFS4ERR_STALE_STATEID:
1373 case -NFS4ERR_EXPIRED:
1374 /* Don't recall a delegation if it was lost */
1375 nfs4_schedule_lease_recovery(server->nfs_client);
1379 * The show must go on: exit, but mark the
1380 * stateid as needing recovery.
1382 case -NFS4ERR_DELEG_REVOKED:
1383 case -NFS4ERR_ADMIN_REVOKED:
1384 case -NFS4ERR_BAD_STATEID:
1385 nfs_inode_find_state_and_recover(state->inode,
1387 nfs4_schedule_stateid_recovery(server, state);
1390 * User RPCSEC_GSS context has expired.
1391 * We cannot recover this stateid now, so
1392 * skip it and allow recovery thread to
1399 err = nfs4_handle_exception(server, err, &exception);
1400 } while (exception.retry);
1405 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1407 struct nfs4_opendata *data = calldata;
1409 data->rpc_status = task->tk_status;
1410 if (data->rpc_status == 0) {
1411 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1412 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1413 renew_lease(data->o_res.server, data->timestamp);
1418 static void nfs4_open_confirm_release(void *calldata)
1420 struct nfs4_opendata *data = calldata;
1421 struct nfs4_state *state = NULL;
1423 /* If this request hasn't been cancelled, do nothing */
1424 if (data->cancelled == 0)
1426 /* In case of error, no cleanup! */
1427 if (!data->rpc_done)
1429 state = nfs4_opendata_to_nfs4_state(data);
1431 nfs4_close_state(state, data->o_arg.fmode);
1433 nfs4_opendata_put(data);
1436 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1437 .rpc_call_done = nfs4_open_confirm_done,
1438 .rpc_release = nfs4_open_confirm_release,
1442 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1444 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1446 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1447 struct rpc_task *task;
1448 struct rpc_message msg = {
1449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1450 .rpc_argp = &data->c_arg,
1451 .rpc_resp = &data->c_res,
1452 .rpc_cred = data->owner->so_cred,
1454 struct rpc_task_setup task_setup_data = {
1455 .rpc_client = server->client,
1456 .rpc_message = &msg,
1457 .callback_ops = &nfs4_open_confirm_ops,
1458 .callback_data = data,
1459 .workqueue = nfsiod_workqueue,
1460 .flags = RPC_TASK_ASYNC,
1464 kref_get(&data->kref);
1466 data->rpc_status = 0;
1467 data->timestamp = jiffies;
1468 task = rpc_run_task(&task_setup_data);
1470 return PTR_ERR(task);
1471 status = nfs4_wait_for_completion_rpc_task(task);
1473 data->cancelled = 1;
1476 status = data->rpc_status;
1481 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1483 struct nfs4_opendata *data = calldata;
1484 struct nfs4_state_owner *sp = data->owner;
1486 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1489 * Check if we still need to send an OPEN call, or if we can use
1490 * a delegation instead.
1492 if (data->state != NULL) {
1493 struct nfs_delegation *delegation;
1495 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1498 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1499 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1500 can_open_delegated(delegation, data->o_arg.fmode))
1501 goto unlock_no_action;
1504 /* Update client id. */
1505 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1506 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1507 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1508 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1510 data->timestamp = jiffies;
1511 if (nfs4_setup_sequence(data->o_arg.server,
1512 &data->o_arg.seq_args,
1513 &data->o_res.seq_res, task))
1515 rpc_call_start(task);
1520 task->tk_action = NULL;
1524 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1526 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1527 nfs4_open_prepare(task, calldata);
1530 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1532 struct nfs4_opendata *data = calldata;
1534 data->rpc_status = task->tk_status;
1536 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1539 if (task->tk_status == 0) {
1540 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1541 switch (data->o_res.f_attr->mode & S_IFMT) {
1545 data->rpc_status = -ELOOP;
1548 data->rpc_status = -EISDIR;
1551 data->rpc_status = -ENOTDIR;
1554 renew_lease(data->o_res.server, data->timestamp);
1555 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1556 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1561 static void nfs4_open_release(void *calldata)
1563 struct nfs4_opendata *data = calldata;
1564 struct nfs4_state *state = NULL;
1566 /* If this request hasn't been cancelled, do nothing */
1567 if (data->cancelled == 0)
1569 /* In case of error, no cleanup! */
1570 if (data->rpc_status != 0 || !data->rpc_done)
1572 /* In case we need an open_confirm, no cleanup! */
1573 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1575 state = nfs4_opendata_to_nfs4_state(data);
1577 nfs4_close_state(state, data->o_arg.fmode);
1579 nfs4_opendata_put(data);
1582 static const struct rpc_call_ops nfs4_open_ops = {
1583 .rpc_call_prepare = nfs4_open_prepare,
1584 .rpc_call_done = nfs4_open_done,
1585 .rpc_release = nfs4_open_release,
1588 static const struct rpc_call_ops nfs4_recover_open_ops = {
1589 .rpc_call_prepare = nfs4_recover_open_prepare,
1590 .rpc_call_done = nfs4_open_done,
1591 .rpc_release = nfs4_open_release,
1594 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1596 struct inode *dir = data->dir->d_inode;
1597 struct nfs_server *server = NFS_SERVER(dir);
1598 struct nfs_openargs *o_arg = &data->o_arg;
1599 struct nfs_openres *o_res = &data->o_res;
1600 struct rpc_task *task;
1601 struct rpc_message msg = {
1602 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1605 .rpc_cred = data->owner->so_cred,
1607 struct rpc_task_setup task_setup_data = {
1608 .rpc_client = server->client,
1609 .rpc_message = &msg,
1610 .callback_ops = &nfs4_open_ops,
1611 .callback_data = data,
1612 .workqueue = nfsiod_workqueue,
1613 .flags = RPC_TASK_ASYNC,
1617 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1618 kref_get(&data->kref);
1620 data->rpc_status = 0;
1621 data->cancelled = 0;
1623 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1624 task = rpc_run_task(&task_setup_data);
1626 return PTR_ERR(task);
1627 status = nfs4_wait_for_completion_rpc_task(task);
1629 data->cancelled = 1;
1632 status = data->rpc_status;
1638 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1640 struct inode *dir = data->dir->d_inode;
1641 struct nfs_openres *o_res = &data->o_res;
1644 status = nfs4_run_open_task(data, 1);
1645 if (status != 0 || !data->rpc_done)
1648 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1650 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1651 status = _nfs4_proc_open_confirm(data);
1659 static int nfs4_opendata_access(struct rpc_cred *cred,
1660 struct nfs4_opendata *opendata,
1661 struct nfs4_state *state, fmode_t fmode)
1663 struct nfs_access_entry cache;
1666 /* access call failed or for some reason the server doesn't
1667 * support any access modes -- defer access call until later */
1668 if (opendata->o_res.access_supported == 0)
1672 /* don't check MAY_WRITE - a newly created file may not have
1673 * write mode bits, but POSIX allows the creating process to write */
1674 if (fmode & FMODE_READ)
1676 if (fmode & FMODE_EXEC)
1680 cache.jiffies = jiffies;
1681 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1682 nfs_access_add_cache(state->inode, &cache);
1684 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1687 /* even though OPEN succeeded, access is denied. Close the file */
1688 nfs4_close_state(state, fmode);
1689 return -NFS4ERR_ACCESS;
1693 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1695 static int _nfs4_proc_open(struct nfs4_opendata *data)
1697 struct inode *dir = data->dir->d_inode;
1698 struct nfs_server *server = NFS_SERVER(dir);
1699 struct nfs_openargs *o_arg = &data->o_arg;
1700 struct nfs_openres *o_res = &data->o_res;
1703 status = nfs4_run_open_task(data, 0);
1704 if (!data->rpc_done)
1707 if (status == -NFS4ERR_BADNAME &&
1708 !(o_arg->open_flags & O_CREAT))
1713 nfs_fattr_map_and_free_names(server, &data->f_attr);
1715 if (o_arg->open_flags & O_CREAT)
1716 update_changeattr(dir, &o_res->cinfo);
1717 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1718 server->caps &= ~NFS_CAP_POSIX_LOCK;
1719 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1720 status = _nfs4_proc_open_confirm(data);
1724 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1725 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1729 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1734 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1735 ret = nfs4_wait_clnt_recover(clp);
1738 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1739 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1741 nfs4_schedule_state_manager(clp);
1747 static int nfs4_recover_expired_lease(struct nfs_server *server)
1749 return nfs4_client_recover_expired_lease(server->nfs_client);
1754 * reclaim state on the server after a network partition.
1755 * Assumes caller holds the appropriate lock
1757 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1759 struct nfs4_opendata *opendata;
1762 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1763 if (IS_ERR(opendata))
1764 return PTR_ERR(opendata);
1765 ret = nfs4_open_recover(opendata, state);
1767 d_drop(ctx->dentry);
1768 nfs4_opendata_put(opendata);
1772 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1774 struct nfs_server *server = NFS_SERVER(state->inode);
1775 struct nfs4_exception exception = { };
1779 err = _nfs4_open_expired(ctx, state);
1783 case -NFS4ERR_GRACE:
1784 case -NFS4ERR_DELAY:
1785 nfs4_handle_exception(server, err, &exception);
1788 } while (exception.retry);
1793 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1795 struct nfs_open_context *ctx;
1798 ctx = nfs4_state_find_open_context(state);
1800 return PTR_ERR(ctx);
1801 ret = nfs4_do_open_expired(ctx, state);
1802 put_nfs_open_context(ctx);
1806 #if defined(CONFIG_NFS_V4_1)
1807 static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
1809 struct nfs_server *server = NFS_SERVER(state->inode);
1810 nfs4_stateid *stateid = &state->stateid;
1813 /* If a state reset has been done, test_stateid is unneeded */
1814 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1817 status = nfs41_test_stateid(server, stateid);
1818 if (status != NFS_OK) {
1819 /* Free the stateid unless the server explicitly
1820 * informs us the stateid is unrecognized. */
1821 if (status != -NFS4ERR_BAD_STATEID)
1822 nfs41_free_stateid(server, stateid);
1823 nfs_remove_bad_delegation(state->inode);
1825 write_seqlock(&state->seqlock);
1826 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1827 write_sequnlock(&state->seqlock);
1828 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1833 * nfs41_check_open_stateid - possibly free an open stateid
1835 * @state: NFSv4 state for an inode
1837 * Returns NFS_OK if recovery for this stateid is now finished.
1838 * Otherwise a negative NFS4ERR value is returned.
1840 static int nfs41_check_open_stateid(struct nfs4_state *state)
1842 struct nfs_server *server = NFS_SERVER(state->inode);
1843 nfs4_stateid *stateid = &state->open_stateid;
1846 /* If a state reset has been done, test_stateid is unneeded */
1847 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1848 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1849 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1850 return -NFS4ERR_BAD_STATEID;
1852 status = nfs41_test_stateid(server, stateid);
1853 if (status != NFS_OK) {
1854 /* Free the stateid unless the server explicitly
1855 * informs us the stateid is unrecognized. */
1856 if (status != -NFS4ERR_BAD_STATEID)
1857 nfs41_free_stateid(server, stateid);
1859 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1860 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1861 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1866 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1870 nfs41_clear_delegation_stateid(state);
1871 status = nfs41_check_open_stateid(state);
1872 if (status != NFS_OK)
1873 status = nfs4_open_expired(sp, state);
1879 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1880 * fields corresponding to attributes that were used to store the verifier.
1881 * Make sure we clobber those fields in the later setattr call
1883 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1885 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1886 !(sattr->ia_valid & ATTR_ATIME_SET))
1887 sattr->ia_valid |= ATTR_ATIME;
1889 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1890 !(sattr->ia_valid & ATTR_MTIME_SET))
1891 sattr->ia_valid |= ATTR_MTIME;
1895 * Returns a referenced nfs4_state
1897 static int _nfs4_do_open(struct inode *dir,
1898 struct dentry *dentry,
1901 struct iattr *sattr,
1902 struct rpc_cred *cred,
1903 struct nfs4_state **res,
1904 struct nfs4_threshold **ctx_th)
1906 struct nfs4_state_owner *sp;
1907 struct nfs4_state *state = NULL;
1908 struct nfs_server *server = NFS_SERVER(dir);
1909 struct nfs4_opendata *opendata;
1912 /* Protect against reboot recovery conflicts */
1914 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1916 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1919 status = nfs4_recover_expired_lease(server);
1921 goto err_put_state_owner;
1922 if (dentry->d_inode != NULL)
1923 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1925 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1926 if (opendata == NULL)
1927 goto err_put_state_owner;
1929 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1930 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1931 if (!opendata->f_attr.mdsthreshold)
1932 goto err_opendata_put;
1933 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
1935 if (dentry->d_inode != NULL)
1936 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1938 status = _nfs4_proc_open(opendata);
1940 goto err_opendata_put;
1942 state = nfs4_opendata_to_nfs4_state(opendata);
1943 status = PTR_ERR(state);
1945 goto err_opendata_put;
1946 if (server->caps & NFS_CAP_POSIX_LOCK)
1947 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1949 status = nfs4_opendata_access(cred, opendata, state, fmode);
1951 goto err_opendata_put;
1953 if (opendata->o_arg.open_flags & O_EXCL) {
1954 nfs4_exclusive_attrset(opendata, sattr);
1956 nfs_fattr_init(opendata->o_res.f_attr);
1957 status = nfs4_do_setattr(state->inode, cred,
1958 opendata->o_res.f_attr, sattr,
1961 nfs_setattr_update_inode(state->inode, sattr);
1962 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1965 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1966 *ctx_th = opendata->f_attr.mdsthreshold;
1968 kfree(opendata->f_attr.mdsthreshold);
1969 opendata->f_attr.mdsthreshold = NULL;
1971 nfs4_opendata_put(opendata);
1972 nfs4_put_state_owner(sp);
1976 kfree(opendata->f_attr.mdsthreshold);
1977 nfs4_opendata_put(opendata);
1978 err_put_state_owner:
1979 nfs4_put_state_owner(sp);
1986 static struct nfs4_state *nfs4_do_open(struct inode *dir,
1987 struct dentry *dentry,
1990 struct iattr *sattr,
1991 struct rpc_cred *cred,
1992 struct nfs4_threshold **ctx_th)
1994 struct nfs4_exception exception = { };
1995 struct nfs4_state *res;
1998 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
2000 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2004 /* NOTE: BAD_SEQID means the server and client disagree about the
2005 * book-keeping w.r.t. state-changing operations
2006 * (OPEN/CLOSE/LOCK/LOCKU...)
2007 * It is actually a sign of a bug on the client or on the server.
2009 * If we receive a BAD_SEQID error in the particular case of
2010 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2011 * have unhashed the old state_owner for us, and that we can
2012 * therefore safely retry using a new one. We should still warn
2013 * the user though...
2015 if (status == -NFS4ERR_BAD_SEQID) {
2016 pr_warn_ratelimited("NFS: v4 server %s "
2017 " returned a bad sequence-id error!\n",
2018 NFS_SERVER(dir)->nfs_client->cl_hostname);
2019 exception.retry = 1;
2023 * BAD_STATEID on OPEN means that the server cancelled our
2024 * state before it received the OPEN_CONFIRM.
2025 * Recover by retrying the request as per the discussion
2026 * on Page 181 of RFC3530.
2028 if (status == -NFS4ERR_BAD_STATEID) {
2029 exception.retry = 1;
2032 if (status == -EAGAIN) {
2033 /* We must have found a delegation */
2034 exception.retry = 1;
2037 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2038 status, &exception));
2039 } while (exception.retry);
2043 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2044 struct nfs_fattr *fattr, struct iattr *sattr,
2045 struct nfs4_state *state)
2047 struct nfs_server *server = NFS_SERVER(inode);
2048 struct nfs_setattrargs arg = {
2049 .fh = NFS_FH(inode),
2052 .bitmask = server->attr_bitmask,
2054 struct nfs_setattrres res = {
2058 struct rpc_message msg = {
2059 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2064 unsigned long timestamp = jiffies;
2067 nfs_fattr_init(fattr);
2069 if (state != NULL) {
2070 struct nfs_lockowner lockowner = {
2071 .l_owner = current->files,
2072 .l_pid = current->tgid,
2074 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2076 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2078 /* Use that stateid */
2080 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2082 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2083 if (status == 0 && state != NULL)
2084 renew_lease(server, timestamp);
2088 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2089 struct nfs_fattr *fattr, struct iattr *sattr,
2090 struct nfs4_state *state)
2092 struct nfs_server *server = NFS_SERVER(inode);
2093 struct nfs4_exception exception = {
2099 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2101 case -NFS4ERR_OPENMODE:
2102 if (state && !(state->state & FMODE_WRITE)) {
2104 if (sattr->ia_valid & ATTR_OPEN)
2109 err = nfs4_handle_exception(server, err, &exception);
2110 } while (exception.retry);
2115 struct nfs4_closedata {
2116 struct inode *inode;
2117 struct nfs4_state *state;
2118 struct nfs_closeargs arg;
2119 struct nfs_closeres res;
2120 struct nfs_fattr fattr;
2121 unsigned long timestamp;
2126 static void nfs4_free_closedata(void *data)
2128 struct nfs4_closedata *calldata = data;
2129 struct nfs4_state_owner *sp = calldata->state->owner;
2130 struct super_block *sb = calldata->state->inode->i_sb;
2133 pnfs_roc_release(calldata->state->inode);
2134 nfs4_put_open_state(calldata->state);
2135 nfs_free_seqid(calldata->arg.seqid);
2136 nfs4_put_state_owner(sp);
2137 nfs_sb_deactive(sb);
2141 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2144 spin_lock(&state->owner->so_lock);
2145 if (!(fmode & FMODE_READ))
2146 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2147 if (!(fmode & FMODE_WRITE))
2148 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2149 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2150 spin_unlock(&state->owner->so_lock);
2153 static void nfs4_close_done(struct rpc_task *task, void *data)
2155 struct nfs4_closedata *calldata = data;
2156 struct nfs4_state *state = calldata->state;
2157 struct nfs_server *server = NFS_SERVER(calldata->inode);
2159 dprintk("%s: begin!\n", __func__);
2160 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2162 /* hmm. we are done with the inode, and in the process of freeing
2163 * the state_owner. we keep this around to process errors
2165 switch (task->tk_status) {
2168 pnfs_roc_set_barrier(state->inode,
2169 calldata->roc_barrier);
2170 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2171 renew_lease(server, calldata->timestamp);
2172 nfs4_close_clear_stateid_flags(state,
2173 calldata->arg.fmode);
2175 case -NFS4ERR_STALE_STATEID:
2176 case -NFS4ERR_OLD_STATEID:
2177 case -NFS4ERR_BAD_STATEID:
2178 case -NFS4ERR_EXPIRED:
2179 if (calldata->arg.fmode == 0)
2182 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2183 rpc_restart_call_prepare(task);
2185 nfs_release_seqid(calldata->arg.seqid);
2186 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2187 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2190 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2192 struct nfs4_closedata *calldata = data;
2193 struct nfs4_state *state = calldata->state;
2194 struct inode *inode = calldata->inode;
2197 dprintk("%s: begin!\n", __func__);
2198 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2201 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2202 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2203 spin_lock(&state->owner->so_lock);
2204 /* Calculate the change in open mode */
2205 if (state->n_rdwr == 0) {
2206 if (state->n_rdonly == 0) {
2207 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2208 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2209 calldata->arg.fmode &= ~FMODE_READ;
2211 if (state->n_wronly == 0) {
2212 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2213 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2214 calldata->arg.fmode &= ~FMODE_WRITE;
2217 spin_unlock(&state->owner->so_lock);
2220 /* Note: exit _without_ calling nfs4_close_done */
2221 task->tk_action = NULL;
2225 if (calldata->arg.fmode == 0) {
2226 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2227 if (calldata->roc &&
2228 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
2232 nfs_fattr_init(calldata->res.fattr);
2233 calldata->timestamp = jiffies;
2234 if (nfs4_setup_sequence(NFS_SERVER(inode),
2235 &calldata->arg.seq_args,
2236 &calldata->res.seq_res,
2239 rpc_call_start(task);
2241 dprintk("%s: done!\n", __func__);
2244 static const struct rpc_call_ops nfs4_close_ops = {
2245 .rpc_call_prepare = nfs4_close_prepare,
2246 .rpc_call_done = nfs4_close_done,
2247 .rpc_release = nfs4_free_closedata,
2251 * It is possible for data to be read/written from a mem-mapped file
2252 * after the sys_close call (which hits the vfs layer as a flush).
2253 * This means that we can't safely call nfsv4 close on a file until
2254 * the inode is cleared. This in turn means that we are not good
2255 * NFSv4 citizens - we do not indicate to the server to update the file's
2256 * share state even when we are done with one of the three share
2257 * stateid's in the inode.
2259 * NOTE: Caller must be holding the sp->so_owner semaphore!
2261 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2263 struct nfs_server *server = NFS_SERVER(state->inode);
2264 struct nfs4_closedata *calldata;
2265 struct nfs4_state_owner *sp = state->owner;
2266 struct rpc_task *task;
2267 struct rpc_message msg = {
2268 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2269 .rpc_cred = state->owner->so_cred,
2271 struct rpc_task_setup task_setup_data = {
2272 .rpc_client = server->client,
2273 .rpc_message = &msg,
2274 .callback_ops = &nfs4_close_ops,
2275 .workqueue = nfsiod_workqueue,
2276 .flags = RPC_TASK_ASYNC,
2278 int status = -ENOMEM;
2280 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2281 if (calldata == NULL)
2283 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2284 calldata->inode = state->inode;
2285 calldata->state = state;
2286 calldata->arg.fh = NFS_FH(state->inode);
2287 calldata->arg.stateid = &state->open_stateid;
2288 /* Serialization for the sequence id */
2289 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2290 if (calldata->arg.seqid == NULL)
2291 goto out_free_calldata;
2292 calldata->arg.fmode = 0;
2293 calldata->arg.bitmask = server->cache_consistency_bitmask;
2294 calldata->res.fattr = &calldata->fattr;
2295 calldata->res.seqid = calldata->arg.seqid;
2296 calldata->res.server = server;
2297 calldata->roc = pnfs_roc(state->inode);
2298 nfs_sb_active(calldata->inode->i_sb);
2300 msg.rpc_argp = &calldata->arg;
2301 msg.rpc_resp = &calldata->res;
2302 task_setup_data.callback_data = calldata;
2303 task = rpc_run_task(&task_setup_data);
2305 return PTR_ERR(task);
2308 status = rpc_wait_for_completion_task(task);
2314 nfs4_put_open_state(state);
2315 nfs4_put_state_owner(sp);
2319 static struct inode *
2320 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2322 struct nfs4_state *state;
2324 /* Protect against concurrent sillydeletes */
2325 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2326 ctx->cred, &ctx->mdsthreshold);
2328 return ERR_CAST(state);
2330 return igrab(state->inode);
2333 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2335 if (ctx->state == NULL)
2338 nfs4_close_sync(ctx->state, ctx->mode);
2340 nfs4_close_state(ctx->state, ctx->mode);
2343 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2345 struct nfs4_server_caps_arg args = {
2348 struct nfs4_server_caps_res res = {};
2349 struct rpc_message msg = {
2350 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2356 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2358 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2359 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2360 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2361 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2362 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2363 NFS_CAP_CTIME|NFS_CAP_MTIME);
2364 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2365 server->caps |= NFS_CAP_ACLS;
2366 if (res.has_links != 0)
2367 server->caps |= NFS_CAP_HARDLINKS;
2368 if (res.has_symlinks != 0)
2369 server->caps |= NFS_CAP_SYMLINKS;
2370 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2371 server->caps |= NFS_CAP_FILEID;
2372 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2373 server->caps |= NFS_CAP_MODE;
2374 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2375 server->caps |= NFS_CAP_NLINK;
2376 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2377 server->caps |= NFS_CAP_OWNER;
2378 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2379 server->caps |= NFS_CAP_OWNER_GROUP;
2380 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2381 server->caps |= NFS_CAP_ATIME;
2382 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2383 server->caps |= NFS_CAP_CTIME;
2384 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2385 server->caps |= NFS_CAP_MTIME;
2387 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2388 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2389 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2390 server->acl_bitmask = res.acl_bitmask;
2391 server->fh_expire_type = res.fh_expire_type;
2397 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2399 struct nfs4_exception exception = { };
2402 err = nfs4_handle_exception(server,
2403 _nfs4_server_capabilities(server, fhandle),
2405 } while (exception.retry);
2409 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2410 struct nfs_fsinfo *info)
2412 struct nfs4_lookup_root_arg args = {
2413 .bitmask = nfs4_fattr_bitmap,
2415 struct nfs4_lookup_res res = {
2417 .fattr = info->fattr,
2420 struct rpc_message msg = {
2421 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2426 nfs_fattr_init(info->fattr);
2427 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2430 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2431 struct nfs_fsinfo *info)
2433 struct nfs4_exception exception = { };
2436 err = _nfs4_lookup_root(server, fhandle, info);
2439 case -NFS4ERR_WRONGSEC:
2442 err = nfs4_handle_exception(server, err, &exception);
2444 } while (exception.retry);
2449 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2450 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2452 struct rpc_auth *auth;
2455 auth = rpcauth_create(flavor, server->client);
2460 ret = nfs4_lookup_root(server, fhandle, info);
2465 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2466 struct nfs_fsinfo *info)
2468 int i, len, status = 0;
2469 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2471 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2474 for (i = 0; i < len; i++) {
2475 /* AUTH_UNIX is the default flavor if none was specified,
2476 * thus has already been tried. */
2477 if (flav_array[i] == RPC_AUTH_UNIX)
2480 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2481 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2486 * -EACCESS could mean that the user doesn't have correct permissions
2487 * to access the mount. It could also mean that we tried to mount
2488 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2489 * existing mount programs don't handle -EACCES very well so it should
2490 * be mapped to -EPERM instead.
2492 if (status == -EACCES)
2498 * get the file handle for the "/" directory on the server
2500 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2501 struct nfs_fsinfo *info)
2503 int minor_version = server->nfs_client->cl_minorversion;
2504 int status = nfs4_lookup_root(server, fhandle, info);
2505 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2507 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2508 * by nfs4_map_errors() as this function exits.
2510 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2512 status = nfs4_server_capabilities(server, fhandle);
2514 status = nfs4_do_fsinfo(server, fhandle, info);
2515 return nfs4_map_errors(status);
2518 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2519 struct nfs_fsinfo *info)
2522 struct nfs_fattr *fattr = info->fattr;
2524 error = nfs4_server_capabilities(server, mntfh);
2526 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2530 error = nfs4_proc_getattr(server, mntfh, fattr);
2532 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2536 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2537 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2538 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2544 * Get locations and (maybe) other attributes of a referral.
2545 * Note that we'll actually follow the referral later when
2546 * we detect fsid mismatch in inode revalidation
2548 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2549 const struct qstr *name, struct nfs_fattr *fattr,
2550 struct nfs_fh *fhandle)
2552 int status = -ENOMEM;
2553 struct page *page = NULL;
2554 struct nfs4_fs_locations *locations = NULL;
2556 page = alloc_page(GFP_KERNEL);
2559 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2560 if (locations == NULL)
2563 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2566 /* Make sure server returned a different fsid for the referral */
2567 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2568 dprintk("%s: server did not return a different fsid for"
2569 " a referral at %s\n", __func__, name->name);
2573 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2574 nfs_fixup_referral_attributes(&locations->fattr);
2576 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2577 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2578 memset(fhandle, 0, sizeof(struct nfs_fh));
2586 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2588 struct nfs4_getattr_arg args = {
2590 .bitmask = server->attr_bitmask,
2592 struct nfs4_getattr_res res = {
2596 struct rpc_message msg = {
2597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2602 nfs_fattr_init(fattr);
2603 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2606 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2608 struct nfs4_exception exception = { };
2611 err = nfs4_handle_exception(server,
2612 _nfs4_proc_getattr(server, fhandle, fattr),
2614 } while (exception.retry);
2619 * The file is not closed if it is opened due to the a request to change
2620 * the size of the file. The open call will not be needed once the
2621 * VFS layer lookup-intents are implemented.
2623 * Close is called when the inode is destroyed.
2624 * If we haven't opened the file for O_WRONLY, we
2625 * need to in the size_change case to obtain a stateid.
2628 * Because OPEN is always done by name in nfsv4, it is
2629 * possible that we opened a different file by the same
2630 * name. We can recognize this race condition, but we
2631 * can't do anything about it besides returning an error.
2633 * This will be fixed with VFS changes (lookup-intent).
2636 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2637 struct iattr *sattr)
2639 struct inode *inode = dentry->d_inode;
2640 struct rpc_cred *cred = NULL;
2641 struct nfs4_state *state = NULL;
2644 if (pnfs_ld_layoutret_on_setattr(inode))
2645 pnfs_return_layout(inode);
2647 nfs_fattr_init(fattr);
2649 /* Deal with open(O_TRUNC) */
2650 if (sattr->ia_valid & ATTR_OPEN)
2651 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2653 /* Optimization: if the end result is no change, don't RPC */
2654 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2657 /* Search for an existing open(O_WRITE) file */
2658 if (sattr->ia_valid & ATTR_FILE) {
2659 struct nfs_open_context *ctx;
2661 ctx = nfs_file_open_context(sattr->ia_file);
2668 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2670 nfs_setattr_update_inode(inode, sattr);
2674 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2675 const struct qstr *name, struct nfs_fh *fhandle,
2676 struct nfs_fattr *fattr)
2678 struct nfs_server *server = NFS_SERVER(dir);
2680 struct nfs4_lookup_arg args = {
2681 .bitmask = server->attr_bitmask,
2682 .dir_fh = NFS_FH(dir),
2685 struct nfs4_lookup_res res = {
2690 struct rpc_message msg = {
2691 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2696 nfs_fattr_init(fattr);
2698 dprintk("NFS call lookup %s\n", name->name);
2699 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2700 dprintk("NFS reply lookup: %d\n", status);
2704 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2706 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2707 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2708 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2712 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2713 struct qstr *name, struct nfs_fh *fhandle,
2714 struct nfs_fattr *fattr)
2716 struct nfs4_exception exception = { };
2717 struct rpc_clnt *client = *clnt;
2720 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2722 case -NFS4ERR_BADNAME:
2725 case -NFS4ERR_MOVED:
2726 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
2728 case -NFS4ERR_WRONGSEC:
2730 if (client != *clnt)
2733 client = nfs4_create_sec_client(client, dir, name);
2735 return PTR_ERR(client);
2737 exception.retry = 1;
2740 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2742 } while (exception.retry);
2747 else if (client != *clnt)
2748 rpc_shutdown_client(client);
2753 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
2754 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2757 struct rpc_clnt *client = NFS_CLIENT(dir);
2759 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2760 if (client != NFS_CLIENT(dir)) {
2761 rpc_shutdown_client(client);
2762 nfs_fixup_secinfo_attributes(fattr);
2768 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2769 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2772 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2774 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2776 rpc_shutdown_client(client);
2777 return ERR_PTR(status);
2782 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2784 struct nfs_server *server = NFS_SERVER(inode);
2785 struct nfs4_accessargs args = {
2786 .fh = NFS_FH(inode),
2787 .bitmask = server->cache_consistency_bitmask,
2789 struct nfs4_accessres res = {
2792 struct rpc_message msg = {
2793 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2796 .rpc_cred = entry->cred,
2798 int mode = entry->mask;
2802 * Determine which access bits we want to ask for...
2804 if (mode & MAY_READ)
2805 args.access |= NFS4_ACCESS_READ;
2806 if (S_ISDIR(inode->i_mode)) {
2807 if (mode & MAY_WRITE)
2808 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2809 if (mode & MAY_EXEC)
2810 args.access |= NFS4_ACCESS_LOOKUP;
2812 if (mode & MAY_WRITE)
2813 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2814 if (mode & MAY_EXEC)
2815 args.access |= NFS4_ACCESS_EXECUTE;
2818 res.fattr = nfs_alloc_fattr();
2819 if (res.fattr == NULL)
2822 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2824 nfs_access_set_mask(entry, res.access);
2825 nfs_refresh_inode(inode, res.fattr);
2827 nfs_free_fattr(res.fattr);
2831 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2833 struct nfs4_exception exception = { };
2836 err = nfs4_handle_exception(NFS_SERVER(inode),
2837 _nfs4_proc_access(inode, entry),
2839 } while (exception.retry);
2844 * TODO: For the time being, we don't try to get any attributes
2845 * along with any of the zero-copy operations READ, READDIR,
2848 * In the case of the first three, we want to put the GETATTR
2849 * after the read-type operation -- this is because it is hard
2850 * to predict the length of a GETATTR response in v4, and thus
2851 * align the READ data correctly. This means that the GETATTR
2852 * may end up partially falling into the page cache, and we should
2853 * shift it into the 'tail' of the xdr_buf before processing.
2854 * To do this efficiently, we need to know the total length
2855 * of data received, which doesn't seem to be available outside
2858 * In the case of WRITE, we also want to put the GETATTR after
2859 * the operation -- in this case because we want to make sure
2860 * we get the post-operation mtime and size.
2862 * Both of these changes to the XDR layer would in fact be quite
2863 * minor, but I decided to leave them for a subsequent patch.
2865 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2866 unsigned int pgbase, unsigned int pglen)
2868 struct nfs4_readlink args = {
2869 .fh = NFS_FH(inode),
2874 struct nfs4_readlink_res res;
2875 struct rpc_message msg = {
2876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2881 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2884 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2885 unsigned int pgbase, unsigned int pglen)
2887 struct nfs4_exception exception = { };
2890 err = nfs4_handle_exception(NFS_SERVER(inode),
2891 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2893 } while (exception.retry);
2898 * This is just for mknod. open(O_CREAT) will always do ->open_context().
2901 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2904 struct nfs_open_context *ctx;
2905 struct nfs4_state *state;
2908 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2910 return PTR_ERR(ctx);
2912 sattr->ia_mode &= ~current_umask();
2913 state = nfs4_do_open(dir, dentry, ctx->mode,
2914 flags, sattr, ctx->cred,
2915 &ctx->mdsthreshold);
2917 if (IS_ERR(state)) {
2918 status = PTR_ERR(state);
2921 d_add(dentry, igrab(state->inode));
2922 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2925 put_nfs_open_context(ctx);
2929 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2931 struct nfs_server *server = NFS_SERVER(dir);
2932 struct nfs_removeargs args = {
2936 struct nfs_removeres res = {
2939 struct rpc_message msg = {
2940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2946 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2948 update_changeattr(dir, &res.cinfo);
2952 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2954 struct nfs4_exception exception = { };
2957 err = nfs4_handle_exception(NFS_SERVER(dir),
2958 _nfs4_proc_remove(dir, name),
2960 } while (exception.retry);
2964 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2966 struct nfs_server *server = NFS_SERVER(dir);
2967 struct nfs_removeargs *args = msg->rpc_argp;
2968 struct nfs_removeres *res = msg->rpc_resp;
2970 res->server = server;
2971 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2972 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
2975 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2977 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2978 &data->args.seq_args,
2982 rpc_call_start(task);
2985 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2987 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2989 if (!nfs4_sequence_done(task, &res->seq_res))
2991 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2993 update_changeattr(dir, &res->cinfo);
2997 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2999 struct nfs_server *server = NFS_SERVER(dir);
3000 struct nfs_renameargs *arg = msg->rpc_argp;
3001 struct nfs_renameres *res = msg->rpc_resp;
3003 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3004 res->server = server;
3005 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
3008 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3010 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3011 &data->args.seq_args,
3015 rpc_call_start(task);
3018 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3019 struct inode *new_dir)
3021 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3023 if (!nfs4_sequence_done(task, &res->seq_res))
3025 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3028 update_changeattr(old_dir, &res->old_cinfo);
3029 update_changeattr(new_dir, &res->new_cinfo);
3033 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3034 struct inode *new_dir, struct qstr *new_name)
3036 struct nfs_server *server = NFS_SERVER(old_dir);
3037 struct nfs_renameargs arg = {
3038 .old_dir = NFS_FH(old_dir),
3039 .new_dir = NFS_FH(new_dir),
3040 .old_name = old_name,
3041 .new_name = new_name,
3043 struct nfs_renameres res = {
3046 struct rpc_message msg = {
3047 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3051 int status = -ENOMEM;
3053 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3055 update_changeattr(old_dir, &res.old_cinfo);
3056 update_changeattr(new_dir, &res.new_cinfo);
3061 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3062 struct inode *new_dir, struct qstr *new_name)
3064 struct nfs4_exception exception = { };
3067 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3068 _nfs4_proc_rename(old_dir, old_name,
3071 } while (exception.retry);
3075 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3077 struct nfs_server *server = NFS_SERVER(inode);
3078 struct nfs4_link_arg arg = {
3079 .fh = NFS_FH(inode),
3080 .dir_fh = NFS_FH(dir),
3082 .bitmask = server->attr_bitmask,
3084 struct nfs4_link_res res = {
3087 struct rpc_message msg = {
3088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3092 int status = -ENOMEM;
3094 res.fattr = nfs_alloc_fattr();
3095 if (res.fattr == NULL)
3098 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3100 update_changeattr(dir, &res.cinfo);
3101 nfs_post_op_update_inode(inode, res.fattr);
3104 nfs_free_fattr(res.fattr);
3108 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3110 struct nfs4_exception exception = { };
3113 err = nfs4_handle_exception(NFS_SERVER(inode),
3114 _nfs4_proc_link(inode, dir, name),
3116 } while (exception.retry);
3120 struct nfs4_createdata {
3121 struct rpc_message msg;
3122 struct nfs4_create_arg arg;
3123 struct nfs4_create_res res;
3125 struct nfs_fattr fattr;
3128 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3129 struct qstr *name, struct iattr *sattr, u32 ftype)
3131 struct nfs4_createdata *data;
3133 data = kzalloc(sizeof(*data), GFP_KERNEL);
3135 struct nfs_server *server = NFS_SERVER(dir);
3137 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3138 data->msg.rpc_argp = &data->arg;
3139 data->msg.rpc_resp = &data->res;
3140 data->arg.dir_fh = NFS_FH(dir);
3141 data->arg.server = server;
3142 data->arg.name = name;
3143 data->arg.attrs = sattr;
3144 data->arg.ftype = ftype;
3145 data->arg.bitmask = server->attr_bitmask;
3146 data->res.server = server;
3147 data->res.fh = &data->fh;
3148 data->res.fattr = &data->fattr;
3149 nfs_fattr_init(data->res.fattr);
3154 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3156 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3157 &data->arg.seq_args, &data->res.seq_res, 1);
3159 update_changeattr(dir, &data->res.dir_cinfo);
3160 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3165 static void nfs4_free_createdata(struct nfs4_createdata *data)
3170 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3171 struct page *page, unsigned int len, struct iattr *sattr)
3173 struct nfs4_createdata *data;
3174 int status = -ENAMETOOLONG;
3176 if (len > NFS4_MAXPATHLEN)
3180 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3184 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3185 data->arg.u.symlink.pages = &page;
3186 data->arg.u.symlink.len = len;
3188 status = nfs4_do_create(dir, dentry, data);
3190 nfs4_free_createdata(data);
3195 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3196 struct page *page, unsigned int len, struct iattr *sattr)
3198 struct nfs4_exception exception = { };
3201 err = nfs4_handle_exception(NFS_SERVER(dir),
3202 _nfs4_proc_symlink(dir, dentry, page,
3205 } while (exception.retry);
3209 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3210 struct iattr *sattr)
3212 struct nfs4_createdata *data;
3213 int status = -ENOMEM;
3215 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3219 status = nfs4_do_create(dir, dentry, data);
3221 nfs4_free_createdata(data);
3226 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3227 struct iattr *sattr)
3229 struct nfs4_exception exception = { };
3232 sattr->ia_mode &= ~current_umask();
3234 err = nfs4_handle_exception(NFS_SERVER(dir),
3235 _nfs4_proc_mkdir(dir, dentry, sattr),
3237 } while (exception.retry);
3241 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3242 u64 cookie, struct page **pages, unsigned int count, int plus)
3244 struct inode *dir = dentry->d_inode;
3245 struct nfs4_readdir_arg args = {
3250 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3253 struct nfs4_readdir_res res;
3254 struct rpc_message msg = {
3255 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3262 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3263 dentry->d_parent->d_name.name,
3264 dentry->d_name.name,
3265 (unsigned long long)cookie);
3266 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3267 res.pgbase = args.pgbase;
3268 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3270 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3271 status += args.pgbase;
3274 nfs_invalidate_atime(dir);
3276 dprintk("%s: returns %d\n", __func__, status);
3280 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3281 u64 cookie, struct page **pages, unsigned int count, int plus)
3283 struct nfs4_exception exception = { };
3286 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3287 _nfs4_proc_readdir(dentry, cred, cookie,
3288 pages, count, plus),
3290 } while (exception.retry);
3294 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3295 struct iattr *sattr, dev_t rdev)
3297 struct nfs4_createdata *data;
3298 int mode = sattr->ia_mode;
3299 int status = -ENOMEM;
3301 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3302 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3304 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3309 data->arg.ftype = NF4FIFO;
3310 else if (S_ISBLK(mode)) {
3311 data->arg.ftype = NF4BLK;
3312 data->arg.u.device.specdata1 = MAJOR(rdev);
3313 data->arg.u.device.specdata2 = MINOR(rdev);
3315 else if (S_ISCHR(mode)) {
3316 data->arg.ftype = NF4CHR;
3317 data->arg.u.device.specdata1 = MAJOR(rdev);
3318 data->arg.u.device.specdata2 = MINOR(rdev);
3321 status = nfs4_do_create(dir, dentry, data);
3323 nfs4_free_createdata(data);
3328 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3329 struct iattr *sattr, dev_t rdev)
3331 struct nfs4_exception exception = { };
3334 sattr->ia_mode &= ~current_umask();
3336 err = nfs4_handle_exception(NFS_SERVER(dir),
3337 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3339 } while (exception.retry);
3343 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3344 struct nfs_fsstat *fsstat)
3346 struct nfs4_statfs_arg args = {
3348 .bitmask = server->attr_bitmask,
3350 struct nfs4_statfs_res res = {
3353 struct rpc_message msg = {
3354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3359 nfs_fattr_init(fsstat->fattr);
3360 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3363 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3365 struct nfs4_exception exception = { };
3368 err = nfs4_handle_exception(server,
3369 _nfs4_proc_statfs(server, fhandle, fsstat),
3371 } while (exception.retry);
3375 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3376 struct nfs_fsinfo *fsinfo)
3378 struct nfs4_fsinfo_arg args = {
3380 .bitmask = server->attr_bitmask,
3382 struct nfs4_fsinfo_res res = {
3385 struct rpc_message msg = {
3386 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3391 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3394 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3396 struct nfs4_exception exception = { };
3400 err = nfs4_handle_exception(server,
3401 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3403 } while (exception.retry);
3407 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3411 nfs_fattr_init(fsinfo->fattr);
3412 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3414 /* block layout checks this! */
3415 server->pnfs_blksize = fsinfo->blksize;
3416 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3422 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3423 struct nfs_pathconf *pathconf)
3425 struct nfs4_pathconf_arg args = {
3427 .bitmask = server->attr_bitmask,
3429 struct nfs4_pathconf_res res = {
3430 .pathconf = pathconf,
3432 struct rpc_message msg = {
3433 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3438 /* None of the pathconf attributes are mandatory to implement */
3439 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3440 memset(pathconf, 0, sizeof(*pathconf));
3444 nfs_fattr_init(pathconf->fattr);
3445 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3448 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3449 struct nfs_pathconf *pathconf)
3451 struct nfs4_exception exception = { };
3455 err = nfs4_handle_exception(server,
3456 _nfs4_proc_pathconf(server, fhandle, pathconf),
3458 } while (exception.retry);
3462 void __nfs4_read_done_cb(struct nfs_read_data *data)
3464 nfs_invalidate_atime(data->header->inode);
3467 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3469 struct nfs_server *server = NFS_SERVER(data->header->inode);
3471 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3472 rpc_restart_call_prepare(task);
3476 __nfs4_read_done_cb(data);
3477 if (task->tk_status > 0)
3478 renew_lease(server, data->timestamp);
3482 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3485 dprintk("--> %s\n", __func__);
3487 if (!nfs4_sequence_done(task, &data->res.seq_res))
3490 return data->read_done_cb ? data->read_done_cb(task, data) :
3491 nfs4_read_done_cb(task, data);
3494 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3496 data->timestamp = jiffies;
3497 data->read_done_cb = nfs4_read_done_cb;
3498 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3499 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3502 static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3504 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3505 &data->args.seq_args,
3509 rpc_call_start(task);
3512 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3514 struct inode *inode = data->header->inode;
3516 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3517 rpc_restart_call_prepare(task);
3520 if (task->tk_status >= 0) {
3521 renew_lease(NFS_SERVER(inode), data->timestamp);
3522 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
3527 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3529 if (!nfs4_sequence_done(task, &data->res.seq_res))
3531 return data->write_done_cb ? data->write_done_cb(task, data) :
3532 nfs4_write_done_cb(task, data);
3536 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3538 const struct nfs_pgio_header *hdr = data->header;
3540 /* Don't request attributes for pNFS or O_DIRECT writes */
3541 if (data->ds_clp != NULL || hdr->dreq != NULL)
3543 /* Otherwise, request attributes if and only if we don't hold
3546 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
3549 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3551 struct nfs_server *server = NFS_SERVER(data->header->inode);
3553 if (!nfs4_write_need_cache_consistency_data(data)) {
3554 data->args.bitmask = NULL;
3555 data->res.fattr = NULL;
3557 data->args.bitmask = server->cache_consistency_bitmask;
3559 if (!data->write_done_cb)
3560 data->write_done_cb = nfs4_write_done_cb;
3561 data->res.server = server;
3562 data->timestamp = jiffies;
3564 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3565 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3568 static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3570 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3571 &data->args.seq_args,
3575 rpc_call_start(task);
3578 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3580 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3581 &data->args.seq_args,
3585 rpc_call_start(task);
3588 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
3590 struct inode *inode = data->inode;
3592 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3593 rpc_restart_call_prepare(task);
3599 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
3601 if (!nfs4_sequence_done(task, &data->res.seq_res))
3603 return data->commit_done_cb(task, data);
3606 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
3608 struct nfs_server *server = NFS_SERVER(data->inode);
3610 if (data->commit_done_cb == NULL)
3611 data->commit_done_cb = nfs4_commit_done_cb;
3612 data->res.server = server;
3613 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3614 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3617 struct nfs4_renewdata {
3618 struct nfs_client *client;
3619 unsigned long timestamp;
3623 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3624 * standalone procedure for queueing an asynchronous RENEW.
3626 static void nfs4_renew_release(void *calldata)
3628 struct nfs4_renewdata *data = calldata;
3629 struct nfs_client *clp = data->client;
3631 if (atomic_read(&clp->cl_count) > 1)
3632 nfs4_schedule_state_renewal(clp);
3633 nfs_put_client(clp);
3637 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3639 struct nfs4_renewdata *data = calldata;
3640 struct nfs_client *clp = data->client;
3641 unsigned long timestamp = data->timestamp;
3643 if (task->tk_status < 0) {
3644 /* Unless we're shutting down, schedule state recovery! */
3645 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3647 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3648 nfs4_schedule_lease_recovery(clp);
3651 nfs4_schedule_path_down_recovery(clp);
3653 do_renew_lease(clp, timestamp);
3656 static const struct rpc_call_ops nfs4_renew_ops = {
3657 .rpc_call_done = nfs4_renew_done,
3658 .rpc_release = nfs4_renew_release,
3661 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3663 struct rpc_message msg = {
3664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3668 struct nfs4_renewdata *data;
3670 if (renew_flags == 0)
3672 if (!atomic_inc_not_zero(&clp->cl_count))
3674 data = kmalloc(sizeof(*data), GFP_NOFS);
3678 data->timestamp = jiffies;
3679 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3680 &nfs4_renew_ops, data);
3683 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3685 struct rpc_message msg = {
3686 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3690 unsigned long now = jiffies;
3693 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3696 do_renew_lease(clp, now);
3700 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3702 return (server->caps & NFS_CAP_ACLS)
3703 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3704 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3707 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3708 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
3711 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
3713 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3714 struct page **pages, unsigned int *pgbase)
3716 struct page *newpage, **spages;
3722 len = min_t(size_t, PAGE_SIZE, buflen);
3723 newpage = alloc_page(GFP_KERNEL);
3725 if (newpage == NULL)
3727 memcpy(page_address(newpage), buf, len);
3732 } while (buflen != 0);
3738 __free_page(spages[rc-1]);
3742 struct nfs4_cached_acl {
3748 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3750 struct nfs_inode *nfsi = NFS_I(inode);
3752 spin_lock(&inode->i_lock);
3753 kfree(nfsi->nfs4_acl);
3754 nfsi->nfs4_acl = acl;
3755 spin_unlock(&inode->i_lock);
3758 static void nfs4_zap_acl_attr(struct inode *inode)
3760 nfs4_set_cached_acl(inode, NULL);
3763 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3765 struct nfs_inode *nfsi = NFS_I(inode);
3766 struct nfs4_cached_acl *acl;
3769 spin_lock(&inode->i_lock);
3770 acl = nfsi->nfs4_acl;
3773 if (buf == NULL) /* user is just asking for length */
3775 if (acl->cached == 0)
3777 ret = -ERANGE; /* see getxattr(2) man page */
3778 if (acl->len > buflen)
3780 memcpy(buf, acl->data, acl->len);
3784 spin_unlock(&inode->i_lock);
3788 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3790 struct nfs4_cached_acl *acl;
3791 size_t buflen = sizeof(*acl) + acl_len;
3793 if (buflen <= PAGE_SIZE) {
3794 acl = kmalloc(buflen, GFP_KERNEL);
3798 _copy_from_pages(acl->data, pages, pgbase, acl_len);
3800 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3807 nfs4_set_cached_acl(inode, acl);
3811 * The getxattr API returns the required buffer length when called with a
3812 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3813 * the required buf. On a NULL buf, we send a page of data to the server
3814 * guessing that the ACL request can be serviced by a page. If so, we cache
3815 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3816 * the cache. If not so, we throw away the page, and cache the required
3817 * length. The next getxattr call will then produce another round trip to
3818 * the server, this time with the input buf of the required size.
3820 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3822 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3823 struct nfs_getaclargs args = {
3824 .fh = NFS_FH(inode),
3828 struct nfs_getaclres res = {
3831 struct rpc_message msg = {
3832 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3836 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3837 int ret = -ENOMEM, i;
3839 /* As long as we're doing a round trip to the server anyway,
3840 * let's be prepared for a page of acl data. */
3843 if (npages > ARRAY_SIZE(pages))
3846 for (i = 0; i < npages; i++) {
3847 pages[i] = alloc_page(GFP_KERNEL);
3852 /* for decoding across pages */
3853 res.acl_scratch = alloc_page(GFP_KERNEL);
3854 if (!res.acl_scratch)
3857 args.acl_len = npages * PAGE_SIZE;
3858 args.acl_pgbase = 0;
3860 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3861 __func__, buf, buflen, npages, args.acl_len);
3862 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3863 &msg, &args.seq_args, &res.seq_res, 0);
3867 /* Handle the case where the passed-in buffer is too short */
3868 if (res.acl_flags & NFS4_ACL_TRUNC) {
3869 /* Did the user only issue a request for the acl length? */
3875 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3877 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3881 for (i = 0; i < npages; i++)
3883 __free_page(pages[i]);
3884 if (res.acl_scratch)
3885 __free_page(res.acl_scratch);
3889 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3891 struct nfs4_exception exception = { };
3894 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3897 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3898 } while (exception.retry);
3902 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3904 struct nfs_server *server = NFS_SERVER(inode);
3907 if (!nfs4_server_supports_acls(server))
3909 ret = nfs_revalidate_inode(server, inode);
3912 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3913 nfs_zap_acl_cache(inode);
3914 ret = nfs4_read_cached_acl(inode, buf, buflen);
3916 /* -ENOENT is returned if there is no ACL or if there is an ACL
3917 * but no cached acl data, just the acl length */
3919 return nfs4_get_acl_uncached(inode, buf, buflen);
3922 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3924 struct nfs_server *server = NFS_SERVER(inode);
3925 struct page *pages[NFS4ACL_MAXPAGES];
3926 struct nfs_setaclargs arg = {
3927 .fh = NFS_FH(inode),
3931 struct nfs_setaclres res;
3932 struct rpc_message msg = {
3933 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3937 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3940 if (!nfs4_server_supports_acls(server))
3942 if (npages > ARRAY_SIZE(pages))
3944 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3947 nfs4_inode_return_delegation(inode);
3948 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3951 * Free each page after tx, so the only ref left is
3952 * held by the network stack
3955 put_page(pages[i-1]);
3958 * Acl update can result in inode attribute update.
3959 * so mark the attribute cache invalid.
3961 spin_lock(&inode->i_lock);
3962 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3963 spin_unlock(&inode->i_lock);
3964 nfs_access_zap_cache(inode);
3965 nfs_zap_acl_cache(inode);
3969 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3971 struct nfs4_exception exception = { };
3974 err = nfs4_handle_exception(NFS_SERVER(inode),
3975 __nfs4_proc_set_acl(inode, buf, buflen),
3977 } while (exception.retry);
3982 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3984 struct nfs_client *clp = server->nfs_client;
3986 if (task->tk_status >= 0)
3988 switch(task->tk_status) {
3989 case -NFS4ERR_DELEG_REVOKED:
3990 case -NFS4ERR_ADMIN_REVOKED:
3991 case -NFS4ERR_BAD_STATEID:
3994 nfs_remove_bad_delegation(state->inode);
3995 case -NFS4ERR_OPENMODE:
3998 nfs4_schedule_stateid_recovery(server, state);
3999 goto wait_on_recovery;
4000 case -NFS4ERR_EXPIRED:
4002 nfs4_schedule_stateid_recovery(server, state);
4003 case -NFS4ERR_STALE_STATEID:
4004 case -NFS4ERR_STALE_CLIENTID:
4005 nfs4_schedule_lease_recovery(clp);
4006 goto wait_on_recovery;
4007 #if defined(CONFIG_NFS_V4_1)
4008 case -NFS4ERR_BADSESSION:
4009 case -NFS4ERR_BADSLOT:
4010 case -NFS4ERR_BAD_HIGH_SLOT:
4011 case -NFS4ERR_DEADSESSION:
4012 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4013 case -NFS4ERR_SEQ_FALSE_RETRY:
4014 case -NFS4ERR_SEQ_MISORDERED:
4015 dprintk("%s ERROR %d, Reset session\n", __func__,
4017 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4018 task->tk_status = 0;
4020 #endif /* CONFIG_NFS_V4_1 */
4021 case -NFS4ERR_DELAY:
4022 nfs_inc_server_stats(server, NFSIOS_DELAY);
4023 case -NFS4ERR_GRACE:
4025 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4026 task->tk_status = 0;
4028 case -NFS4ERR_RETRY_UNCACHED_REP:
4029 case -NFS4ERR_OLD_STATEID:
4030 task->tk_status = 0;
4033 task->tk_status = nfs4_map_errors(task->tk_status);
4036 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4037 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4038 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4039 task->tk_status = 0;
4043 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4044 nfs4_verifier *bootverf)
4048 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4049 /* An impossible timestamp guarantees this value
4050 * will never match a generated boot time. */
4052 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4054 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4055 verf[0] = (__be32)nn->boot_time.tv_sec;
4056 verf[1] = (__be32)nn->boot_time.tv_nsec;
4058 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4062 nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4063 char *buf, size_t len)
4065 unsigned int result;
4068 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4070 rpc_peeraddr2str(clp->cl_rpcclient,
4072 rpc_peeraddr2str(clp->cl_rpcclient,
4073 RPC_DISPLAY_PROTO));
4079 nfs4_init_uniform_client_string(const struct nfs_client *clp,
4080 char *buf, size_t len)
4082 char *nodename = clp->cl_rpcclient->cl_nodename;
4084 if (nfs4_client_id_uniquifier[0] != '\0')
4085 nodename = nfs4_client_id_uniquifier;
4086 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4087 clp->rpc_ops->version, clp->cl_minorversion,
4092 * nfs4_proc_setclientid - Negotiate client ID
4093 * @clp: state data structure
4094 * @program: RPC program for NFSv4 callback service
4095 * @port: IP port number for NFS4 callback service
4096 * @cred: RPC credential to use for this call
4097 * @res: where to place the result
4099 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4101 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4102 unsigned short port, struct rpc_cred *cred,
4103 struct nfs4_setclientid_res *res)
4105 nfs4_verifier sc_verifier;
4106 struct nfs4_setclientid setclientid = {
4107 .sc_verifier = &sc_verifier,
4109 .sc_cb_ident = clp->cl_cb_ident,
4111 struct rpc_message msg = {
4112 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4113 .rpc_argp = &setclientid,
4119 /* nfs_client_id4 */
4120 nfs4_init_boot_verifier(clp, &sc_verifier);
4121 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4122 setclientid.sc_name_len =
4123 nfs4_init_uniform_client_string(clp,
4124 setclientid.sc_name,
4125 sizeof(setclientid.sc_name));
4127 setclientid.sc_name_len =
4128 nfs4_init_nonuniform_client_string(clp,
4129 setclientid.sc_name,
4130 sizeof(setclientid.sc_name));
4133 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4134 sizeof(setclientid.sc_netid),
4135 rpc_peeraddr2str(clp->cl_rpcclient,
4136 RPC_DISPLAY_NETID));
4138 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4139 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4140 clp->cl_ipaddr, port >> 8, port & 255);
4142 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4143 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4144 setclientid.sc_name_len, setclientid.sc_name);
4145 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4146 dprintk("NFS reply setclientid: %d\n", status);
4151 * nfs4_proc_setclientid_confirm - Confirm client ID
4152 * @clp: state data structure
4153 * @res: result of a previous SETCLIENTID
4154 * @cred: RPC credential to use for this call
4156 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4158 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4159 struct nfs4_setclientid_res *arg,
4160 struct rpc_cred *cred)
4162 struct nfs_fsinfo fsinfo;
4163 struct rpc_message msg = {
4164 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4166 .rpc_resp = &fsinfo,
4172 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4173 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4176 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4178 spin_lock(&clp->cl_lock);
4179 clp->cl_lease_time = fsinfo.lease_time * HZ;
4180 clp->cl_last_renewal = now;
4181 spin_unlock(&clp->cl_lock);
4183 dprintk("NFS reply setclientid_confirm: %d\n", status);
4187 struct nfs4_delegreturndata {
4188 struct nfs4_delegreturnargs args;
4189 struct nfs4_delegreturnres res;
4191 nfs4_stateid stateid;
4192 unsigned long timestamp;
4193 struct nfs_fattr fattr;
4197 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4199 struct nfs4_delegreturndata *data = calldata;
4201 if (!nfs4_sequence_done(task, &data->res.seq_res))
4204 switch (task->tk_status) {
4205 case -NFS4ERR_STALE_STATEID:
4206 case -NFS4ERR_EXPIRED:
4208 renew_lease(data->res.server, data->timestamp);
4211 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4213 rpc_restart_call_prepare(task);
4217 data->rpc_status = task->tk_status;
4220 static void nfs4_delegreturn_release(void *calldata)
4225 #if defined(CONFIG_NFS_V4_1)
4226 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4228 struct nfs4_delegreturndata *d_data;
4230 d_data = (struct nfs4_delegreturndata *)data;
4232 if (nfs4_setup_sequence(d_data->res.server,
4233 &d_data->args.seq_args,
4234 &d_data->res.seq_res, task))
4236 rpc_call_start(task);
4238 #endif /* CONFIG_NFS_V4_1 */
4240 static const struct rpc_call_ops nfs4_delegreturn_ops = {
4241 #if defined(CONFIG_NFS_V4_1)
4242 .rpc_call_prepare = nfs4_delegreturn_prepare,
4243 #endif /* CONFIG_NFS_V4_1 */
4244 .rpc_call_done = nfs4_delegreturn_done,
4245 .rpc_release = nfs4_delegreturn_release,
4248 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4250 struct nfs4_delegreturndata *data;
4251 struct nfs_server *server = NFS_SERVER(inode);
4252 struct rpc_task *task;
4253 struct rpc_message msg = {
4254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4257 struct rpc_task_setup task_setup_data = {
4258 .rpc_client = server->client,
4259 .rpc_message = &msg,
4260 .callback_ops = &nfs4_delegreturn_ops,
4261 .flags = RPC_TASK_ASYNC,
4265 data = kzalloc(sizeof(*data), GFP_NOFS);
4268 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4269 data->args.fhandle = &data->fh;
4270 data->args.stateid = &data->stateid;
4271 data->args.bitmask = server->cache_consistency_bitmask;
4272 nfs_copy_fh(&data->fh, NFS_FH(inode));
4273 nfs4_stateid_copy(&data->stateid, stateid);
4274 data->res.fattr = &data->fattr;
4275 data->res.server = server;
4276 nfs_fattr_init(data->res.fattr);
4277 data->timestamp = jiffies;
4278 data->rpc_status = 0;
4280 task_setup_data.callback_data = data;
4281 msg.rpc_argp = &data->args;
4282 msg.rpc_resp = &data->res;
4283 task = rpc_run_task(&task_setup_data);
4285 return PTR_ERR(task);
4288 status = nfs4_wait_for_completion_rpc_task(task);
4291 status = data->rpc_status;
4293 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4295 nfs_refresh_inode(inode, &data->fattr);
4301 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4303 struct nfs_server *server = NFS_SERVER(inode);
4304 struct nfs4_exception exception = { };
4307 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4309 case -NFS4ERR_STALE_STATEID:
4310 case -NFS4ERR_EXPIRED:
4314 err = nfs4_handle_exception(server, err, &exception);
4315 } while (exception.retry);
4319 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4320 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4323 * sleep, with exponential backoff, and retry the LOCK operation.
4325 static unsigned long
4326 nfs4_set_lock_task_retry(unsigned long timeout)
4328 freezable_schedule_timeout_killable(timeout);
4330 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4331 return NFS4_LOCK_MAXTIMEOUT;
4335 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4337 struct inode *inode = state->inode;
4338 struct nfs_server *server = NFS_SERVER(inode);
4339 struct nfs_client *clp = server->nfs_client;
4340 struct nfs_lockt_args arg = {
4341 .fh = NFS_FH(inode),
4344 struct nfs_lockt_res res = {
4347 struct rpc_message msg = {
4348 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4351 .rpc_cred = state->owner->so_cred,
4353 struct nfs4_lock_state *lsp;
4356 arg.lock_owner.clientid = clp->cl_clientid;
4357 status = nfs4_set_lock_state(state, request);
4360 lsp = request->fl_u.nfs4_fl.owner;
4361 arg.lock_owner.id = lsp->ls_seqid.owner_id;
4362 arg.lock_owner.s_dev = server->s_dev;
4363 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4366 request->fl_type = F_UNLCK;
4368 case -NFS4ERR_DENIED:
4371 request->fl_ops->fl_release_private(request);
4376 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4378 struct nfs4_exception exception = { };
4382 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4383 _nfs4_proc_getlk(state, cmd, request),
4385 } while (exception.retry);
4389 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4392 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4394 res = posix_lock_file_wait(file, fl);
4397 res = flock_lock_file_wait(file, fl);
4405 struct nfs4_unlockdata {
4406 struct nfs_locku_args arg;
4407 struct nfs_locku_res res;
4408 struct nfs4_lock_state *lsp;
4409 struct nfs_open_context *ctx;
4410 struct file_lock fl;
4411 const struct nfs_server *server;
4412 unsigned long timestamp;
4415 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4416 struct nfs_open_context *ctx,
4417 struct nfs4_lock_state *lsp,
4418 struct nfs_seqid *seqid)
4420 struct nfs4_unlockdata *p;
4421 struct inode *inode = lsp->ls_state->inode;
4423 p = kzalloc(sizeof(*p), GFP_NOFS);
4426 p->arg.fh = NFS_FH(inode);
4428 p->arg.seqid = seqid;
4429 p->res.seqid = seqid;
4430 p->arg.stateid = &lsp->ls_stateid;
4432 atomic_inc(&lsp->ls_count);
4433 /* Ensure we don't close file until we're done freeing locks! */
4434 p->ctx = get_nfs_open_context(ctx);
4435 memcpy(&p->fl, fl, sizeof(p->fl));
4436 p->server = NFS_SERVER(inode);
4440 static void nfs4_locku_release_calldata(void *data)
4442 struct nfs4_unlockdata *calldata = data;
4443 nfs_free_seqid(calldata->arg.seqid);
4444 nfs4_put_lock_state(calldata->lsp);
4445 put_nfs_open_context(calldata->ctx);
4449 static void nfs4_locku_done(struct rpc_task *task, void *data)
4451 struct nfs4_unlockdata *calldata = data;
4453 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4455 switch (task->tk_status) {
4457 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4458 &calldata->res.stateid);
4459 renew_lease(calldata->server, calldata->timestamp);
4461 case -NFS4ERR_BAD_STATEID:
4462 case -NFS4ERR_OLD_STATEID:
4463 case -NFS4ERR_STALE_STATEID:
4464 case -NFS4ERR_EXPIRED:
4467 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4468 rpc_restart_call_prepare(task);
4472 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4474 struct nfs4_unlockdata *calldata = data;
4476 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4478 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
4479 /* Note: exit _without_ running nfs4_locku_done */
4480 task->tk_action = NULL;
4483 calldata->timestamp = jiffies;
4484 if (nfs4_setup_sequence(calldata->server,
4485 &calldata->arg.seq_args,
4486 &calldata->res.seq_res, task))
4488 rpc_call_start(task);
4491 static const struct rpc_call_ops nfs4_locku_ops = {
4492 .rpc_call_prepare = nfs4_locku_prepare,
4493 .rpc_call_done = nfs4_locku_done,
4494 .rpc_release = nfs4_locku_release_calldata,
4497 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4498 struct nfs_open_context *ctx,
4499 struct nfs4_lock_state *lsp,
4500 struct nfs_seqid *seqid)
4502 struct nfs4_unlockdata *data;
4503 struct rpc_message msg = {
4504 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4505 .rpc_cred = ctx->cred,
4507 struct rpc_task_setup task_setup_data = {
4508 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4509 .rpc_message = &msg,
4510 .callback_ops = &nfs4_locku_ops,
4511 .workqueue = nfsiod_workqueue,
4512 .flags = RPC_TASK_ASYNC,
4515 /* Ensure this is an unlock - when canceling a lock, the
4516 * canceled lock is passed in, and it won't be an unlock.
4518 fl->fl_type = F_UNLCK;
4520 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4522 nfs_free_seqid(seqid);
4523 return ERR_PTR(-ENOMEM);
4526 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4527 msg.rpc_argp = &data->arg;
4528 msg.rpc_resp = &data->res;
4529 task_setup_data.callback_data = data;
4530 return rpc_run_task(&task_setup_data);
4533 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4535 struct nfs_inode *nfsi = NFS_I(state->inode);
4536 struct nfs_seqid *seqid;
4537 struct nfs4_lock_state *lsp;
4538 struct rpc_task *task;
4540 unsigned char fl_flags = request->fl_flags;
4542 status = nfs4_set_lock_state(state, request);
4543 /* Unlock _before_ we do the RPC call */
4544 request->fl_flags |= FL_EXISTS;
4545 down_read(&nfsi->rwsem);
4546 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4547 up_read(&nfsi->rwsem);
4550 up_read(&nfsi->rwsem);
4553 /* Is this a delegated lock? */
4554 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4556 lsp = request->fl_u.nfs4_fl.owner;
4557 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4561 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4562 status = PTR_ERR(task);
4565 status = nfs4_wait_for_completion_rpc_task(task);
4568 request->fl_flags = fl_flags;
4572 struct nfs4_lockdata {
4573 struct nfs_lock_args arg;
4574 struct nfs_lock_res res;
4575 struct nfs4_lock_state *lsp;
4576 struct nfs_open_context *ctx;
4577 struct file_lock fl;
4578 unsigned long timestamp;
4581 struct nfs_server *server;
4584 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4585 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4588 struct nfs4_lockdata *p;
4589 struct inode *inode = lsp->ls_state->inode;
4590 struct nfs_server *server = NFS_SERVER(inode);
4592 p = kzalloc(sizeof(*p), gfp_mask);
4596 p->arg.fh = NFS_FH(inode);
4598 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4599 if (p->arg.open_seqid == NULL)
4601 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4602 if (p->arg.lock_seqid == NULL)
4603 goto out_free_seqid;
4604 p->arg.lock_stateid = &lsp->ls_stateid;
4605 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4606 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4607 p->arg.lock_owner.s_dev = server->s_dev;
4608 p->res.lock_seqid = p->arg.lock_seqid;
4611 atomic_inc(&lsp->ls_count);
4612 p->ctx = get_nfs_open_context(ctx);
4613 memcpy(&p->fl, fl, sizeof(p->fl));
4616 nfs_free_seqid(p->arg.open_seqid);
4622 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4624 struct nfs4_lockdata *data = calldata;
4625 struct nfs4_state *state = data->lsp->ls_state;
4627 dprintk("%s: begin!\n", __func__);
4628 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4630 /* Do we need to do an open_to_lock_owner? */
4631 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4632 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4634 data->arg.open_stateid = &state->stateid;
4635 data->arg.new_lock_owner = 1;
4636 data->res.open_seqid = data->arg.open_seqid;
4638 data->arg.new_lock_owner = 0;
4639 data->timestamp = jiffies;
4640 if (nfs4_setup_sequence(data->server,
4641 &data->arg.seq_args,
4642 &data->res.seq_res, task))
4644 rpc_call_start(task);
4645 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4648 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4650 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4651 nfs4_lock_prepare(task, calldata);
4654 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4656 struct nfs4_lockdata *data = calldata;
4658 dprintk("%s: begin!\n", __func__);
4660 if (!nfs4_sequence_done(task, &data->res.seq_res))
4663 data->rpc_status = task->tk_status;
4664 if (data->arg.new_lock_owner != 0) {
4665 if (data->rpc_status == 0)
4666 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4670 if (data->rpc_status == 0) {
4671 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4672 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
4673 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4676 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4679 static void nfs4_lock_release(void *calldata)
4681 struct nfs4_lockdata *data = calldata;
4683 dprintk("%s: begin!\n", __func__);
4684 nfs_free_seqid(data->arg.open_seqid);
4685 if (data->cancelled != 0) {
4686 struct rpc_task *task;
4687 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4688 data->arg.lock_seqid);
4690 rpc_put_task_async(task);
4691 dprintk("%s: cancelling lock!\n", __func__);
4693 nfs_free_seqid(data->arg.lock_seqid);
4694 nfs4_put_lock_state(data->lsp);
4695 put_nfs_open_context(data->ctx);
4697 dprintk("%s: done!\n", __func__);
4700 static const struct rpc_call_ops nfs4_lock_ops = {
4701 .rpc_call_prepare = nfs4_lock_prepare,
4702 .rpc_call_done = nfs4_lock_done,
4703 .rpc_release = nfs4_lock_release,
4706 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4707 .rpc_call_prepare = nfs4_recover_lock_prepare,
4708 .rpc_call_done = nfs4_lock_done,
4709 .rpc_release = nfs4_lock_release,
4712 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4715 case -NFS4ERR_ADMIN_REVOKED:
4716 case -NFS4ERR_BAD_STATEID:
4717 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4718 if (new_lock_owner != 0 ||
4719 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
4720 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4722 case -NFS4ERR_STALE_STATEID:
4723 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4724 case -NFS4ERR_EXPIRED:
4725 nfs4_schedule_lease_recovery(server->nfs_client);
4729 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4731 struct nfs4_lockdata *data;
4732 struct rpc_task *task;
4733 struct rpc_message msg = {
4734 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4735 .rpc_cred = state->owner->so_cred,
4737 struct rpc_task_setup task_setup_data = {
4738 .rpc_client = NFS_CLIENT(state->inode),
4739 .rpc_message = &msg,
4740 .callback_ops = &nfs4_lock_ops,
4741 .workqueue = nfsiod_workqueue,
4742 .flags = RPC_TASK_ASYNC,
4746 dprintk("%s: begin!\n", __func__);
4747 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4748 fl->fl_u.nfs4_fl.owner,
4749 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4753 data->arg.block = 1;
4754 if (recovery_type > NFS_LOCK_NEW) {
4755 if (recovery_type == NFS_LOCK_RECLAIM)
4756 data->arg.reclaim = NFS_LOCK_RECLAIM;
4757 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4759 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4760 msg.rpc_argp = &data->arg;
4761 msg.rpc_resp = &data->res;
4762 task_setup_data.callback_data = data;
4763 task = rpc_run_task(&task_setup_data);
4765 return PTR_ERR(task);
4766 ret = nfs4_wait_for_completion_rpc_task(task);
4768 ret = data->rpc_status;
4770 nfs4_handle_setlk_error(data->server, data->lsp,
4771 data->arg.new_lock_owner, ret);
4773 data->cancelled = 1;
4775 dprintk("%s: done, ret = %d!\n", __func__, ret);
4779 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4781 struct nfs_server *server = NFS_SERVER(state->inode);
4782 struct nfs4_exception exception = {
4783 .inode = state->inode,
4788 /* Cache the lock if possible... */
4789 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4791 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4792 if (err != -NFS4ERR_DELAY)
4794 nfs4_handle_exception(server, err, &exception);
4795 } while (exception.retry);
4799 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4801 struct nfs_server *server = NFS_SERVER(state->inode);
4802 struct nfs4_exception exception = {
4803 .inode = state->inode,
4807 err = nfs4_set_lock_state(state, request);
4811 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4813 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4817 case -NFS4ERR_GRACE:
4818 case -NFS4ERR_DELAY:
4819 nfs4_handle_exception(server, err, &exception);
4822 } while (exception.retry);
4827 #if defined(CONFIG_NFS_V4_1)
4829 * nfs41_check_expired_locks - possibly free a lock stateid
4831 * @state: NFSv4 state for an inode
4833 * Returns NFS_OK if recovery for this stateid is now finished.
4834 * Otherwise a negative NFS4ERR value is returned.
4836 static int nfs41_check_expired_locks(struct nfs4_state *state)
4838 int status, ret = -NFS4ERR_BAD_STATEID;
4839 struct nfs4_lock_state *lsp;
4840 struct nfs_server *server = NFS_SERVER(state->inode);
4842 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4843 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
4844 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4845 if (status != NFS_OK) {
4846 /* Free the stateid unless the server
4847 * informs us the stateid is unrecognized. */
4848 if (status != -NFS4ERR_BAD_STATEID)
4849 nfs41_free_stateid(server,
4851 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
4860 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4862 int status = NFS_OK;
4864 if (test_bit(LK_STATE_IN_USE, &state->flags))
4865 status = nfs41_check_expired_locks(state);
4866 if (status != NFS_OK)
4867 status = nfs4_lock_expired(state, request);
4872 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4874 struct nfs_inode *nfsi = NFS_I(state->inode);
4875 unsigned char fl_flags = request->fl_flags;
4876 int status = -ENOLCK;
4878 if ((fl_flags & FL_POSIX) &&
4879 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4881 /* Is this a delegated open? */
4882 status = nfs4_set_lock_state(state, request);
4885 request->fl_flags |= FL_ACCESS;
4886 status = do_vfs_lock(request->fl_file, request);
4889 down_read(&nfsi->rwsem);
4890 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4891 /* Yes: cache locks! */
4892 /* ...but avoid races with delegation recall... */
4893 request->fl_flags = fl_flags & ~FL_SLEEP;
4894 status = do_vfs_lock(request->fl_file, request);
4897 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4900 /* Note: we always want to sleep here! */
4901 request->fl_flags = fl_flags | FL_SLEEP;
4902 if (do_vfs_lock(request->fl_file, request) < 0)
4903 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4904 "manager!\n", __func__);
4906 up_read(&nfsi->rwsem);
4908 request->fl_flags = fl_flags;
4912 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4914 struct nfs4_exception exception = {
4916 .inode = state->inode,
4921 err = _nfs4_proc_setlk(state, cmd, request);
4922 if (err == -NFS4ERR_DENIED)
4924 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4926 } while (exception.retry);
4931 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4933 struct nfs_open_context *ctx;
4934 struct nfs4_state *state;
4935 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4938 /* verify open state */
4939 ctx = nfs_file_open_context(filp);
4942 if (request->fl_start < 0 || request->fl_end < 0)
4945 if (IS_GETLK(cmd)) {
4947 return nfs4_proc_getlk(state, F_GETLK, request);
4951 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4954 if (request->fl_type == F_UNLCK) {
4956 return nfs4_proc_unlck(state, cmd, request);
4963 * Don't rely on the VFS having checked the file open mode,
4964 * since it won't do this for flock() locks.
4966 switch (request->fl_type) {
4968 if (!(filp->f_mode & FMODE_READ))
4972 if (!(filp->f_mode & FMODE_WRITE))
4977 status = nfs4_proc_setlk(state, cmd, request);
4978 if ((status != -EAGAIN) || IS_SETLK(cmd))
4980 timeout = nfs4_set_lock_task_retry(timeout);
4981 status = -ERESTARTSYS;
4984 } while(status < 0);
4988 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4990 struct nfs_server *server = NFS_SERVER(state->inode);
4991 struct nfs4_exception exception = { };
4994 err = nfs4_set_lock_state(state, fl);
4998 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
5001 printk(KERN_ERR "NFS: %s: unhandled error "
5002 "%d.\n", __func__, err);
5006 case -NFS4ERR_EXPIRED:
5007 nfs4_schedule_stateid_recovery(server, state);
5008 case -NFS4ERR_STALE_CLIENTID:
5009 case -NFS4ERR_STALE_STATEID:
5010 nfs4_schedule_lease_recovery(server->nfs_client);
5012 case -NFS4ERR_BADSESSION:
5013 case -NFS4ERR_BADSLOT:
5014 case -NFS4ERR_BAD_HIGH_SLOT:
5015 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5016 case -NFS4ERR_DEADSESSION:
5017 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
5021 * The show must go on: exit, but mark the
5022 * stateid as needing recovery.
5024 case -NFS4ERR_DELEG_REVOKED:
5025 case -NFS4ERR_ADMIN_REVOKED:
5026 case -NFS4ERR_BAD_STATEID:
5027 case -NFS4ERR_OPENMODE:
5028 nfs4_schedule_stateid_recovery(server, state);
5033 * User RPCSEC_GSS context has expired.
5034 * We cannot recover this stateid now, so
5035 * skip it and allow recovery thread to
5041 case -NFS4ERR_DENIED:
5042 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5045 case -NFS4ERR_DELAY:
5048 err = nfs4_handle_exception(server, err, &exception);
5049 } while (exception.retry);
5054 struct nfs_release_lockowner_data {
5055 struct nfs4_lock_state *lsp;
5056 struct nfs_server *server;
5057 struct nfs_release_lockowner_args args;
5060 static void nfs4_release_lockowner_release(void *calldata)
5062 struct nfs_release_lockowner_data *data = calldata;
5063 nfs4_free_lock_state(data->server, data->lsp);
5067 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5068 .rpc_release = nfs4_release_lockowner_release,
5071 int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
5073 struct nfs_server *server = lsp->ls_state->owner->so_server;
5074 struct nfs_release_lockowner_data *data;
5075 struct rpc_message msg = {
5076 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5079 if (server->nfs_client->cl_mvops->minor_version != 0)
5081 data = kmalloc(sizeof(*data), GFP_NOFS);
5085 data->server = server;
5086 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5087 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5088 data->args.lock_owner.s_dev = server->s_dev;
5089 msg.rpc_argp = &data->args;
5090 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5094 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5096 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5097 const void *buf, size_t buflen,
5098 int flags, int type)
5100 if (strcmp(key, "") != 0)
5103 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5106 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5107 void *buf, size_t buflen, int type)
5109 if (strcmp(key, "") != 0)
5112 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5115 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5116 size_t list_len, const char *name,
5117 size_t name_len, int type)
5119 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5121 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5124 if (list && len <= list_len)
5125 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5130 * nfs_fhget will use either the mounted_on_fileid or the fileid
5132 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5134 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5135 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5136 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
5137 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
5140 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
5141 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
5142 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5146 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5147 const struct qstr *name,
5148 struct nfs4_fs_locations *fs_locations,
5151 struct nfs_server *server = NFS_SERVER(dir);
5153 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
5155 struct nfs4_fs_locations_arg args = {
5156 .dir_fh = NFS_FH(dir),
5161 struct nfs4_fs_locations_res res = {
5162 .fs_locations = fs_locations,
5164 struct rpc_message msg = {
5165 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5171 dprintk("%s: start\n", __func__);
5173 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5174 * is not supported */
5175 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5176 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5178 bitmask[0] |= FATTR4_WORD0_FILEID;
5180 nfs_fattr_init(&fs_locations->fattr);
5181 fs_locations->server = server;
5182 fs_locations->nlocations = 0;
5183 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
5184 dprintk("%s: returned status = %d\n", __func__, status);
5188 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5189 const struct qstr *name,
5190 struct nfs4_fs_locations *fs_locations,
5193 struct nfs4_exception exception = { };
5196 err = nfs4_handle_exception(NFS_SERVER(dir),
5197 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
5199 } while (exception.retry);
5203 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5206 struct nfs4_secinfo_arg args = {
5207 .dir_fh = NFS_FH(dir),
5210 struct nfs4_secinfo_res res = {
5213 struct rpc_message msg = {
5214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5219 dprintk("NFS call secinfo %s\n", name->name);
5220 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5221 dprintk("NFS reply secinfo: %d\n", status);
5225 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5226 struct nfs4_secinfo_flavors *flavors)
5228 struct nfs4_exception exception = { };
5231 err = nfs4_handle_exception(NFS_SERVER(dir),
5232 _nfs4_proc_secinfo(dir, name, flavors),
5234 } while (exception.retry);
5238 #ifdef CONFIG_NFS_V4_1
5240 * Check the exchange flags returned by the server for invalid flags, having
5241 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5244 static int nfs4_check_cl_exchange_flags(u32 flags)
5246 if (flags & ~EXCHGID4_FLAG_MASK_R)
5248 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5249 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5251 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5255 return -NFS4ERR_INVAL;
5259 nfs41_same_server_scope(struct nfs41_server_scope *a,
5260 struct nfs41_server_scope *b)
5262 if (a->server_scope_sz == b->server_scope_sz &&
5263 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5270 * nfs4_proc_bind_conn_to_session()
5272 * The 4.1 client currently uses the same TCP connection for the
5273 * fore and backchannel.
5275 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
5278 struct nfs41_bind_conn_to_session_res res;
5279 struct rpc_message msg = {
5281 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5287 dprintk("--> %s\n", __func__);
5288 BUG_ON(clp == NULL);
5290 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5291 if (unlikely(res.session == NULL)) {
5296 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5298 if (memcmp(res.session->sess_id.data,
5299 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5300 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5304 if (res.dir != NFS4_CDFS4_BOTH) {
5305 dprintk("NFS: %s: Unexpected direction from server\n",
5310 if (res.use_conn_in_rdma_mode) {
5311 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5320 dprintk("<-- %s status= %d\n", __func__, status);
5325 * nfs4_proc_exchange_id()
5327 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5329 * Since the clientid has expired, all compounds using sessions
5330 * associated with the stale clientid will be returning
5331 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5332 * be in some phase of session reset.
5334 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
5336 nfs4_verifier verifier;
5337 struct nfs41_exchange_id_args args = {
5338 .verifier = &verifier,
5340 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
5342 struct nfs41_exchange_id_res res = {
5346 struct rpc_message msg = {
5347 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5353 nfs4_init_boot_verifier(clp, &verifier);
5354 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5356 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5357 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5358 args.id_len, args.id);
5360 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
5362 if (unlikely(res.server_owner == NULL)) {
5367 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
5369 if (unlikely(res.server_scope == NULL)) {
5371 goto out_server_owner;
5374 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
5375 if (unlikely(res.impl_id == NULL)) {
5377 goto out_server_scope;
5380 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5382 status = nfs4_check_cl_exchange_flags(res.flags);
5385 clp->cl_clientid = res.clientid;
5386 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5387 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5388 clp->cl_seqid = res.seqid;
5390 kfree(clp->cl_serverowner);
5391 clp->cl_serverowner = res.server_owner;
5392 res.server_owner = NULL;
5394 /* use the most recent implementation id */
5395 kfree(clp->cl_implid);
5396 clp->cl_implid = res.impl_id;
5398 if (clp->cl_serverscope != NULL &&
5399 !nfs41_same_server_scope(clp->cl_serverscope,
5400 res.server_scope)) {
5401 dprintk("%s: server_scope mismatch detected\n",
5403 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5404 kfree(clp->cl_serverscope);
5405 clp->cl_serverscope = NULL;
5408 if (clp->cl_serverscope == NULL) {
5409 clp->cl_serverscope = res.server_scope;
5416 kfree(res.server_owner);
5418 kfree(res.server_scope);
5420 if (clp->cl_implid != NULL)
5421 dprintk("NFS reply exchange_id: Server Implementation ID: "
5422 "domain: %s, name: %s, date: %llu,%u\n",
5423 clp->cl_implid->domain, clp->cl_implid->name,
5424 clp->cl_implid->date.seconds,
5425 clp->cl_implid->date.nseconds);
5426 dprintk("NFS reply exchange_id: %d\n", status);
5430 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5431 struct rpc_cred *cred)
5433 struct rpc_message msg = {
5434 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5440 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5442 dprintk("NFS: Got error %d from the server %s on "
5443 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5447 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5448 struct rpc_cred *cred)
5453 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5454 ret = _nfs4_proc_destroy_clientid(clp, cred);
5456 case -NFS4ERR_DELAY:
5457 case -NFS4ERR_CLIENTID_BUSY:
5467 int nfs4_destroy_clientid(struct nfs_client *clp)
5469 struct rpc_cred *cred;
5472 if (clp->cl_mvops->minor_version < 1)
5474 if (clp->cl_exchange_flags == 0)
5476 if (clp->cl_preserve_clid)
5478 cred = nfs4_get_exchange_id_cred(clp);
5479 ret = nfs4_proc_destroy_clientid(clp, cred);
5484 case -NFS4ERR_STALE_CLIENTID:
5485 clp->cl_exchange_flags = 0;
5491 struct nfs4_get_lease_time_data {
5492 struct nfs4_get_lease_time_args *args;
5493 struct nfs4_get_lease_time_res *res;
5494 struct nfs_client *clp;
5497 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5501 struct nfs4_get_lease_time_data *data =
5502 (struct nfs4_get_lease_time_data *)calldata;
5504 dprintk("--> %s\n", __func__);
5505 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5506 /* just setup sequence, do not trigger session recovery
5507 since we're invoked within one */
5508 ret = nfs41_setup_sequence(data->clp->cl_session,
5509 &data->args->la_seq_args,
5510 &data->res->lr_seq_res, task);
5512 BUG_ON(ret == -EAGAIN);
5513 rpc_call_start(task);
5514 dprintk("<-- %s\n", __func__);
5518 * Called from nfs4_state_manager thread for session setup, so don't recover
5519 * from sequence operation or clientid errors.
5521 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5523 struct nfs4_get_lease_time_data *data =
5524 (struct nfs4_get_lease_time_data *)calldata;
5526 dprintk("--> %s\n", __func__);
5527 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5529 switch (task->tk_status) {
5530 case -NFS4ERR_DELAY:
5531 case -NFS4ERR_GRACE:
5532 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5533 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5534 task->tk_status = 0;
5536 case -NFS4ERR_RETRY_UNCACHED_REP:
5537 rpc_restart_call_prepare(task);
5540 dprintk("<-- %s\n", __func__);
5543 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5544 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5545 .rpc_call_done = nfs4_get_lease_time_done,
5548 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5550 struct rpc_task *task;
5551 struct nfs4_get_lease_time_args args;
5552 struct nfs4_get_lease_time_res res = {
5553 .lr_fsinfo = fsinfo,
5555 struct nfs4_get_lease_time_data data = {
5560 struct rpc_message msg = {
5561 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5565 struct rpc_task_setup task_setup = {
5566 .rpc_client = clp->cl_rpcclient,
5567 .rpc_message = &msg,
5568 .callback_ops = &nfs4_get_lease_time_ops,
5569 .callback_data = &data,
5570 .flags = RPC_TASK_TIMEOUT,
5574 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5575 dprintk("--> %s\n", __func__);
5576 task = rpc_run_task(&task_setup);
5579 status = PTR_ERR(task);
5581 status = task->tk_status;
5584 dprintk("<-- %s return %d\n", __func__, status);
5589 static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5591 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5594 static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5595 struct nfs4_slot *new,
5599 struct nfs4_slot *old = NULL;
5602 spin_lock(&tbl->slot_tbl_lock);
5606 tbl->max_slots = max_slots;
5608 tbl->highest_used_slotid = -1; /* no slot is currently used */
5609 for (i = 0; i < tbl->max_slots; i++)
5610 tbl->slots[i].seq_nr = ivalue;
5611 spin_unlock(&tbl->slot_tbl_lock);
5616 * (re)Initialise a slot table
5618 static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5621 struct nfs4_slot *new = NULL;
5624 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5625 max_reqs, tbl->max_slots);
5627 /* Does the newly negotiated max_reqs match the existing slot table? */
5628 if (max_reqs != tbl->max_slots) {
5629 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
5635 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
5636 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5637 tbl, tbl->slots, tbl->max_slots);
5639 dprintk("<-- %s: return %d\n", __func__, ret);
5643 /* Destroy the slot table */
5644 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5646 if (session->fc_slot_table.slots != NULL) {
5647 kfree(session->fc_slot_table.slots);
5648 session->fc_slot_table.slots = NULL;
5650 if (session->bc_slot_table.slots != NULL) {
5651 kfree(session->bc_slot_table.slots);
5652 session->bc_slot_table.slots = NULL;
5658 * Initialize or reset the forechannel and backchannel tables
5660 static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
5662 struct nfs4_slot_table *tbl;
5665 dprintk("--> %s\n", __func__);
5667 tbl = &ses->fc_slot_table;
5668 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5669 if (status) /* -ENOMEM */
5672 tbl = &ses->bc_slot_table;
5673 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5674 if (status && tbl->slots == NULL)
5675 /* Fore and back channel share a connection so get
5676 * both slot tables or neither */
5677 nfs4_destroy_slot_tables(ses);
5681 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5683 struct nfs4_session *session;
5684 struct nfs4_slot_table *tbl;
5686 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5690 tbl = &session->fc_slot_table;
5691 tbl->highest_used_slotid = NFS4_NO_SLOT;
5692 spin_lock_init(&tbl->slot_tbl_lock);
5693 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5694 init_completion(&tbl->complete);
5696 tbl = &session->bc_slot_table;
5697 tbl->highest_used_slotid = NFS4_NO_SLOT;
5698 spin_lock_init(&tbl->slot_tbl_lock);
5699 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5700 init_completion(&tbl->complete);
5702 session->session_state = 1<<NFS4_SESSION_INITING;
5708 void nfs4_destroy_session(struct nfs4_session *session)
5710 struct rpc_xprt *xprt;
5711 struct rpc_cred *cred;
5713 cred = nfs4_get_exchange_id_cred(session->clp);
5714 nfs4_proc_destroy_session(session, cred);
5719 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5721 dprintk("%s Destroy backchannel for xprt %p\n",
5723 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
5724 nfs4_destroy_slot_tables(session);
5729 * Initialize the values to be used by the client in CREATE_SESSION
5730 * If nfs4_init_session set the fore channel request and response sizes,
5733 * Set the back channel max_resp_sz_cached to zero to force the client to
5734 * always set csa_cachethis to FALSE because the current implementation
5735 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5737 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5739 struct nfs4_session *session = args->client->cl_session;
5740 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5741 mxresp_sz = session->fc_attrs.max_resp_sz;
5744 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5746 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5747 /* Fore channel attributes */
5748 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5749 args->fc_attrs.max_resp_sz = mxresp_sz;
5750 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5751 args->fc_attrs.max_reqs = max_session_slots;
5753 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5754 "max_ops=%u max_reqs=%u\n",
5756 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5757 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5759 /* Back channel attributes */
5760 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5761 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5762 args->bc_attrs.max_resp_sz_cached = 0;
5763 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5764 args->bc_attrs.max_reqs = 1;
5766 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5767 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5769 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5770 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5771 args->bc_attrs.max_reqs);
5774 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5776 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5777 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5779 if (rcvd->max_resp_sz > sent->max_resp_sz)
5782 * Our requested max_ops is the minimum we need; we're not
5783 * prepared to break up compounds into smaller pieces than that.
5784 * So, no point even trying to continue if the server won't
5787 if (rcvd->max_ops < sent->max_ops)
5789 if (rcvd->max_reqs == 0)
5791 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5792 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5796 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5798 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5799 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5801 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5803 if (rcvd->max_resp_sz < sent->max_resp_sz)
5805 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5807 /* These would render the backchannel useless: */
5808 if (rcvd->max_ops != sent->max_ops)
5810 if (rcvd->max_reqs != sent->max_reqs)
5815 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5816 struct nfs4_session *session)
5820 ret = nfs4_verify_fore_channel_attrs(args, session);
5823 return nfs4_verify_back_channel_attrs(args, session);
5826 static int _nfs4_proc_create_session(struct nfs_client *clp,
5827 struct rpc_cred *cred)
5829 struct nfs4_session *session = clp->cl_session;
5830 struct nfs41_create_session_args args = {
5832 .cb_program = NFS4_CALLBACK,
5834 struct nfs41_create_session_res res = {
5837 struct rpc_message msg = {
5838 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5845 nfs4_init_channel_attrs(&args);
5846 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5848 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5851 /* Verify the session's negotiated channel_attrs values */
5852 status = nfs4_verify_channel_attrs(&args, session);
5854 /* Increment the clientid slot sequence id */
5862 * Issues a CREATE_SESSION operation to the server.
5863 * It is the responsibility of the caller to verify the session is
5864 * expired before calling this routine.
5866 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
5870 struct nfs4_session *session = clp->cl_session;
5872 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5874 status = _nfs4_proc_create_session(clp, cred);
5878 /* Init or reset the session slot tables */
5879 status = nfs4_setup_session_slot_tables(session);
5880 dprintk("slot table setup returned %d\n", status);
5884 ptr = (unsigned *)&session->sess_id.data[0];
5885 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5886 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5888 dprintk("<-- %s\n", __func__);
5893 * Issue the over-the-wire RPC DESTROY_SESSION.
5894 * The caller must serialize access to this routine.
5896 int nfs4_proc_destroy_session(struct nfs4_session *session,
5897 struct rpc_cred *cred)
5899 struct rpc_message msg = {
5900 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5901 .rpc_argp = session,
5906 dprintk("--> nfs4_proc_destroy_session\n");
5908 /* session is still being setup */
5909 if (session->clp->cl_cons_state != NFS_CS_READY)
5912 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5915 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
5916 "Session has been destroyed regardless...\n", status);
5918 dprintk("<-- nfs4_proc_destroy_session\n");
5923 * With sessions, the client is not marked ready until after a
5924 * successful EXCHANGE_ID and CREATE_SESSION.
5926 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5927 * other versions of NFS can be tried.
5929 static int nfs41_check_session_ready(struct nfs_client *clp)
5933 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5934 ret = nfs4_client_recover_expired_lease(clp);
5938 if (clp->cl_cons_state < NFS_CS_READY)
5939 return -EPROTONOSUPPORT;
5944 int nfs4_init_session(struct nfs_server *server)
5946 struct nfs_client *clp = server->nfs_client;
5947 struct nfs4_session *session;
5948 unsigned int rsize, wsize;
5950 if (!nfs4_has_session(clp))
5953 session = clp->cl_session;
5954 spin_lock(&clp->cl_lock);
5955 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5957 rsize = server->rsize;
5959 rsize = NFS_MAX_FILE_IO_SIZE;
5960 wsize = server->wsize;
5962 wsize = NFS_MAX_FILE_IO_SIZE;
5964 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5965 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5967 spin_unlock(&clp->cl_lock);
5969 return nfs41_check_session_ready(clp);
5972 int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
5974 struct nfs4_session *session = clp->cl_session;
5977 spin_lock(&clp->cl_lock);
5978 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5980 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
5981 * DS lease to be equal to the MDS lease.
5983 clp->cl_lease_time = lease_time;
5984 clp->cl_last_renewal = jiffies;
5986 spin_unlock(&clp->cl_lock);
5988 ret = nfs41_check_session_ready(clp);
5991 /* Test for the DS role */
5992 if (!is_ds_client(clp))
5996 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
6000 * Renew the cl_session lease.
6002 struct nfs4_sequence_data {
6003 struct nfs_client *clp;
6004 struct nfs4_sequence_args args;
6005 struct nfs4_sequence_res res;
6008 static void nfs41_sequence_release(void *data)
6010 struct nfs4_sequence_data *calldata = data;
6011 struct nfs_client *clp = calldata->clp;
6013 if (atomic_read(&clp->cl_count) > 1)
6014 nfs4_schedule_state_renewal(clp);
6015 nfs_put_client(clp);
6019 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6021 switch(task->tk_status) {
6022 case -NFS4ERR_DELAY:
6023 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6026 nfs4_schedule_lease_recovery(clp);
6031 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
6033 struct nfs4_sequence_data *calldata = data;
6034 struct nfs_client *clp = calldata->clp;
6036 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6039 if (task->tk_status < 0) {
6040 dprintk("%s ERROR %d\n", __func__, task->tk_status);
6041 if (atomic_read(&clp->cl_count) == 1)
6044 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6045 rpc_restart_call_prepare(task);
6049 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
6051 dprintk("<-- %s\n", __func__);
6054 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6056 struct nfs4_sequence_data *calldata = data;
6057 struct nfs_client *clp = calldata->clp;
6058 struct nfs4_sequence_args *args;
6059 struct nfs4_sequence_res *res;
6061 args = task->tk_msg.rpc_argp;
6062 res = task->tk_msg.rpc_resp;
6064 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
6066 rpc_call_start(task);
6069 static const struct rpc_call_ops nfs41_sequence_ops = {
6070 .rpc_call_done = nfs41_sequence_call_done,
6071 .rpc_call_prepare = nfs41_sequence_prepare,
6072 .rpc_release = nfs41_sequence_release,
6075 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6077 struct nfs4_sequence_data *calldata;
6078 struct rpc_message msg = {
6079 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6082 struct rpc_task_setup task_setup_data = {
6083 .rpc_client = clp->cl_rpcclient,
6084 .rpc_message = &msg,
6085 .callback_ops = &nfs41_sequence_ops,
6086 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6089 if (!atomic_inc_not_zero(&clp->cl_count))
6090 return ERR_PTR(-EIO);
6091 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6092 if (calldata == NULL) {
6093 nfs_put_client(clp);
6094 return ERR_PTR(-ENOMEM);
6096 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
6097 msg.rpc_argp = &calldata->args;
6098 msg.rpc_resp = &calldata->res;
6099 calldata->clp = clp;
6100 task_setup_data.callback_data = calldata;
6102 return rpc_run_task(&task_setup_data);
6105 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
6107 struct rpc_task *task;
6110 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6112 task = _nfs41_proc_sequence(clp, cred);
6114 ret = PTR_ERR(task);
6116 rpc_put_task_async(task);
6117 dprintk("<-- %s status=%d\n", __func__, ret);
6121 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6123 struct rpc_task *task;
6126 task = _nfs41_proc_sequence(clp, cred);
6128 ret = PTR_ERR(task);
6131 ret = rpc_wait_for_completion_task(task);
6133 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6135 if (task->tk_status == 0)
6136 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
6137 ret = task->tk_status;
6141 dprintk("<-- %s status=%d\n", __func__, ret);
6145 struct nfs4_reclaim_complete_data {
6146 struct nfs_client *clp;
6147 struct nfs41_reclaim_complete_args arg;
6148 struct nfs41_reclaim_complete_res res;
6151 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6153 struct nfs4_reclaim_complete_data *calldata = data;
6155 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6156 if (nfs41_setup_sequence(calldata->clp->cl_session,
6157 &calldata->arg.seq_args,
6158 &calldata->res.seq_res, task))
6161 rpc_call_start(task);
6164 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6166 switch(task->tk_status) {
6168 case -NFS4ERR_COMPLETE_ALREADY:
6169 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6171 case -NFS4ERR_DELAY:
6172 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6174 case -NFS4ERR_RETRY_UNCACHED_REP:
6177 nfs4_schedule_lease_recovery(clp);
6182 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6184 struct nfs4_reclaim_complete_data *calldata = data;
6185 struct nfs_client *clp = calldata->clp;
6186 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6188 dprintk("--> %s\n", __func__);
6189 if (!nfs41_sequence_done(task, res))
6192 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6193 rpc_restart_call_prepare(task);
6196 dprintk("<-- %s\n", __func__);
6199 static void nfs4_free_reclaim_complete_data(void *data)
6201 struct nfs4_reclaim_complete_data *calldata = data;
6206 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6207 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6208 .rpc_call_done = nfs4_reclaim_complete_done,
6209 .rpc_release = nfs4_free_reclaim_complete_data,
6213 * Issue a global reclaim complete.
6215 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6217 struct nfs4_reclaim_complete_data *calldata;
6218 struct rpc_task *task;
6219 struct rpc_message msg = {
6220 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6222 struct rpc_task_setup task_setup_data = {
6223 .rpc_client = clp->cl_rpcclient,
6224 .rpc_message = &msg,
6225 .callback_ops = &nfs4_reclaim_complete_call_ops,
6226 .flags = RPC_TASK_ASYNC,
6228 int status = -ENOMEM;
6230 dprintk("--> %s\n", __func__);
6231 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6232 if (calldata == NULL)
6234 calldata->clp = clp;
6235 calldata->arg.one_fs = 0;
6237 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
6238 msg.rpc_argp = &calldata->arg;
6239 msg.rpc_resp = &calldata->res;
6240 task_setup_data.callback_data = calldata;
6241 task = rpc_run_task(&task_setup_data);
6243 status = PTR_ERR(task);
6246 status = nfs4_wait_for_completion_rpc_task(task);
6248 status = task->tk_status;
6252 dprintk("<-- %s status=%d\n", __func__, status);
6257 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6259 struct nfs4_layoutget *lgp = calldata;
6260 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6262 dprintk("--> %s\n", __func__);
6263 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6264 * right now covering the LAYOUTGET we are about to send.
6265 * However, that is not so catastrophic, and there seems
6266 * to be no way to prevent it completely.
6268 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
6269 &lgp->res.seq_res, task))
6271 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6272 NFS_I(lgp->args.inode)->layout,
6273 lgp->args.ctx->state)) {
6274 rpc_exit(task, NFS4_OK);
6277 rpc_call_start(task);
6280 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6282 struct nfs4_layoutget *lgp = calldata;
6283 struct inode *inode = lgp->args.inode;
6284 struct nfs_server *server = NFS_SERVER(inode);
6285 struct pnfs_layout_hdr *lo;
6286 struct nfs4_state *state = NULL;
6288 dprintk("--> %s\n", __func__);
6290 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6293 switch (task->tk_status) {
6296 case -NFS4ERR_LAYOUTTRYLATER:
6297 case -NFS4ERR_RECALLCONFLICT:
6298 task->tk_status = -NFS4ERR_DELAY;
6300 case -NFS4ERR_EXPIRED:
6301 case -NFS4ERR_BAD_STATEID:
6302 spin_lock(&inode->i_lock);
6303 lo = NFS_I(inode)->layout;
6304 if (!lo || list_empty(&lo->plh_segs)) {
6305 spin_unlock(&inode->i_lock);
6306 /* If the open stateid was bad, then recover it. */
6307 state = lgp->args.ctx->state;
6311 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6312 spin_unlock(&inode->i_lock);
6313 /* Mark the bad layout state as invalid, then
6314 * retry using the open stateid. */
6315 pnfs_free_lseg_list(&head);
6318 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6319 rpc_restart_call_prepare(task);
6321 dprintk("<-- %s\n", __func__);
6324 static size_t max_response_pages(struct nfs_server *server)
6326 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6327 return nfs_page_array_len(0, max_resp_sz);
6330 static void nfs4_free_pages(struct page **pages, size_t size)
6337 for (i = 0; i < size; i++) {
6340 __free_page(pages[i]);
6345 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6347 struct page **pages;
6350 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6352 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6356 for (i = 0; i < size; i++) {
6357 pages[i] = alloc_page(gfp_flags);
6359 dprintk("%s: failed to allocate page\n", __func__);
6360 nfs4_free_pages(pages, size);
6368 static void nfs4_layoutget_release(void *calldata)
6370 struct nfs4_layoutget *lgp = calldata;
6371 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6372 size_t max_pages = max_response_pages(server);
6374 dprintk("--> %s\n", __func__);
6375 nfs4_free_pages(lgp->args.layout.pages, max_pages);
6376 put_nfs_open_context(lgp->args.ctx);
6378 dprintk("<-- %s\n", __func__);
6381 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6382 .rpc_call_prepare = nfs4_layoutget_prepare,
6383 .rpc_call_done = nfs4_layoutget_done,
6384 .rpc_release = nfs4_layoutget_release,
6387 struct pnfs_layout_segment *
6388 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
6390 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6391 size_t max_pages = max_response_pages(server);
6392 struct rpc_task *task;
6393 struct rpc_message msg = {
6394 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6395 .rpc_argp = &lgp->args,
6396 .rpc_resp = &lgp->res,
6398 struct rpc_task_setup task_setup_data = {
6399 .rpc_client = server->client,
6400 .rpc_message = &msg,
6401 .callback_ops = &nfs4_layoutget_call_ops,
6402 .callback_data = lgp,
6403 .flags = RPC_TASK_ASYNC,
6405 struct pnfs_layout_segment *lseg = NULL;
6408 dprintk("--> %s\n", __func__);
6410 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6411 if (!lgp->args.layout.pages) {
6412 nfs4_layoutget_release(lgp);
6413 return ERR_PTR(-ENOMEM);
6415 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6417 lgp->res.layoutp = &lgp->args.layout;
6418 lgp->res.seq_res.sr_slot = NULL;
6419 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
6420 task = rpc_run_task(&task_setup_data);
6422 return ERR_CAST(task);
6423 status = nfs4_wait_for_completion_rpc_task(task);
6425 status = task->tk_status;
6427 lseg = pnfs_layout_process(lgp);
6429 dprintk("<-- %s status=%d\n", __func__, status);
6431 return ERR_PTR(status);
6436 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6438 struct nfs4_layoutreturn *lrp = calldata;
6440 dprintk("--> %s\n", __func__);
6441 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
6442 &lrp->res.seq_res, task))
6444 rpc_call_start(task);
6447 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6449 struct nfs4_layoutreturn *lrp = calldata;
6450 struct nfs_server *server;
6452 dprintk("--> %s\n", __func__);
6454 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6457 server = NFS_SERVER(lrp->args.inode);
6458 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6459 rpc_restart_call_prepare(task);
6462 dprintk("<-- %s\n", __func__);
6465 static void nfs4_layoutreturn_release(void *calldata)
6467 struct nfs4_layoutreturn *lrp = calldata;
6468 struct pnfs_layout_hdr *lo = lrp->args.layout;
6470 dprintk("--> %s\n", __func__);
6471 spin_lock(&lo->plh_inode->i_lock);
6472 if (lrp->res.lrs_present)
6473 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6474 lo->plh_block_lgets--;
6475 spin_unlock(&lo->plh_inode->i_lock);
6476 pnfs_put_layout_hdr(lrp->args.layout);
6478 dprintk("<-- %s\n", __func__);
6481 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6482 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6483 .rpc_call_done = nfs4_layoutreturn_done,
6484 .rpc_release = nfs4_layoutreturn_release,
6487 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6489 struct rpc_task *task;
6490 struct rpc_message msg = {
6491 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6492 .rpc_argp = &lrp->args,
6493 .rpc_resp = &lrp->res,
6495 struct rpc_task_setup task_setup_data = {
6496 .rpc_client = lrp->clp->cl_rpcclient,
6497 .rpc_message = &msg,
6498 .callback_ops = &nfs4_layoutreturn_call_ops,
6499 .callback_data = lrp,
6503 dprintk("--> %s\n", __func__);
6504 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
6505 task = rpc_run_task(&task_setup_data);
6507 return PTR_ERR(task);
6508 status = task->tk_status;
6509 dprintk("<-- %s status=%d\n", __func__, status);
6515 * Retrieve the list of Data Server devices from the MDS.
6517 static int _nfs4_getdevicelist(struct nfs_server *server,
6518 const struct nfs_fh *fh,
6519 struct pnfs_devicelist *devlist)
6521 struct nfs4_getdevicelist_args args = {
6523 .layoutclass = server->pnfs_curr_ld->id,
6525 struct nfs4_getdevicelist_res res = {
6528 struct rpc_message msg = {
6529 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6535 dprintk("--> %s\n", __func__);
6536 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6538 dprintk("<-- %s status=%d\n", __func__, status);
6542 int nfs4_proc_getdevicelist(struct nfs_server *server,
6543 const struct nfs_fh *fh,
6544 struct pnfs_devicelist *devlist)
6546 struct nfs4_exception exception = { };
6550 err = nfs4_handle_exception(server,
6551 _nfs4_getdevicelist(server, fh, devlist),
6553 } while (exception.retry);
6555 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6556 err, devlist->num_devs);
6560 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6563 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6565 struct nfs4_getdeviceinfo_args args = {
6568 struct nfs4_getdeviceinfo_res res = {
6571 struct rpc_message msg = {
6572 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6578 dprintk("--> %s\n", __func__);
6579 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6580 dprintk("<-- %s status=%d\n", __func__, status);
6585 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6587 struct nfs4_exception exception = { };
6591 err = nfs4_handle_exception(server,
6592 _nfs4_proc_getdeviceinfo(server, pdev),
6594 } while (exception.retry);
6597 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6599 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6601 struct nfs4_layoutcommit_data *data = calldata;
6602 struct nfs_server *server = NFS_SERVER(data->args.inode);
6604 if (nfs4_setup_sequence(server, &data->args.seq_args,
6605 &data->res.seq_res, task))
6607 rpc_call_start(task);
6611 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6613 struct nfs4_layoutcommit_data *data = calldata;
6614 struct nfs_server *server = NFS_SERVER(data->args.inode);
6616 if (!nfs4_sequence_done(task, &data->res.seq_res))
6619 switch (task->tk_status) { /* Just ignore these failures */
6620 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6621 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6622 case -NFS4ERR_BADLAYOUT: /* no layout */
6623 case -NFS4ERR_GRACE: /* loca_recalim always false */
6624 task->tk_status = 0;
6627 nfs_post_op_update_inode_force_wcc(data->args.inode,
6631 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6632 rpc_restart_call_prepare(task);
6638 static void nfs4_layoutcommit_release(void *calldata)
6640 struct nfs4_layoutcommit_data *data = calldata;
6641 struct pnfs_layout_segment *lseg, *tmp;
6642 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
6644 pnfs_cleanup_layoutcommit(data);
6645 /* Matched by references in pnfs_set_layoutcommit */
6646 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6647 list_del_init(&lseg->pls_lc_list);
6648 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6650 pnfs_put_lseg(lseg);
6653 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6654 smp_mb__after_clear_bit();
6655 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6657 put_rpccred(data->cred);
6661 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6662 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6663 .rpc_call_done = nfs4_layoutcommit_done,
6664 .rpc_release = nfs4_layoutcommit_release,
6668 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6670 struct rpc_message msg = {
6671 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6672 .rpc_argp = &data->args,
6673 .rpc_resp = &data->res,
6674 .rpc_cred = data->cred,
6676 struct rpc_task_setup task_setup_data = {
6677 .task = &data->task,
6678 .rpc_client = NFS_CLIENT(data->args.inode),
6679 .rpc_message = &msg,
6680 .callback_ops = &nfs4_layoutcommit_ops,
6681 .callback_data = data,
6682 .flags = RPC_TASK_ASYNC,
6684 struct rpc_task *task;
6687 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6688 "lbw: %llu inode %lu\n",
6689 data->task.tk_pid, sync,
6690 data->args.lastbytewritten,
6691 data->args.inode->i_ino);
6693 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6694 task = rpc_run_task(&task_setup_data);
6696 return PTR_ERR(task);
6699 status = nfs4_wait_for_completion_rpc_task(task);
6702 status = task->tk_status;
6704 dprintk("%s: status %d\n", __func__, status);
6710 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6711 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6713 struct nfs41_secinfo_no_name_args args = {
6714 .style = SECINFO_STYLE_CURRENT_FH,
6716 struct nfs4_secinfo_res res = {
6719 struct rpc_message msg = {
6720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6724 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6728 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6729 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6731 struct nfs4_exception exception = { };
6734 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6737 case -NFS4ERR_WRONGSEC:
6738 case -NFS4ERR_NOTSUPP:
6741 err = nfs4_handle_exception(server, err, &exception);
6743 } while (exception.retry);
6749 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6750 struct nfs_fsinfo *info)
6754 rpc_authflavor_t flavor;
6755 struct nfs4_secinfo_flavors *flavors;
6757 page = alloc_page(GFP_KERNEL);
6763 flavors = page_address(page);
6764 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6767 * Fall back on "guess and check" method if
6768 * the server doesn't support SECINFO_NO_NAME
6770 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6771 err = nfs4_find_root_sec(server, fhandle, info);
6777 flavor = nfs_find_best_sec(flavors);
6779 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6789 static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6792 struct nfs41_test_stateid_args args = {
6795 struct nfs41_test_stateid_res res;
6796 struct rpc_message msg = {
6797 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6802 dprintk("NFS call test_stateid %p\n", stateid);
6803 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6804 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6805 if (status != NFS_OK) {
6806 dprintk("NFS reply test_stateid: failed, %d\n", status);
6809 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
6814 * nfs41_test_stateid - perform a TEST_STATEID operation
6816 * @server: server / transport on which to perform the operation
6817 * @stateid: state ID to test
6819 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6820 * Otherwise a negative NFS4ERR value is returned if the operation
6821 * failed or the state ID is not currently valid.
6823 static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6825 struct nfs4_exception exception = { };
6828 err = _nfs41_test_stateid(server, stateid);
6829 if (err != -NFS4ERR_DELAY)
6831 nfs4_handle_exception(server, err, &exception);
6832 } while (exception.retry);
6836 static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6838 struct nfs41_free_stateid_args args = {
6841 struct nfs41_free_stateid_res res;
6842 struct rpc_message msg = {
6843 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6849 dprintk("NFS call free_stateid %p\n", stateid);
6850 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6851 status = nfs4_call_sync_sequence(server->client, server, &msg,
6852 &args.seq_args, &res.seq_res, 1);
6853 dprintk("NFS reply free_stateid: %d\n", status);
6858 * nfs41_free_stateid - perform a FREE_STATEID operation
6860 * @server: server / transport on which to perform the operation
6861 * @stateid: state ID to release
6863 * Returns NFS_OK if the server freed "stateid". Otherwise a
6864 * negative NFS4ERR value is returned.
6866 static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6868 struct nfs4_exception exception = { };
6871 err = _nfs4_free_stateid(server, stateid);
6872 if (err != -NFS4ERR_DELAY)
6874 nfs4_handle_exception(server, err, &exception);
6875 } while (exception.retry);
6879 static bool nfs41_match_stateid(const nfs4_stateid *s1,
6880 const nfs4_stateid *s2)
6882 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
6885 if (s1->seqid == s2->seqid)
6887 if (s1->seqid == 0 || s2->seqid == 0)
6893 #endif /* CONFIG_NFS_V4_1 */
6895 static bool nfs4_match_stateid(const nfs4_stateid *s1,
6896 const nfs4_stateid *s2)
6898 return nfs4_stateid_match(s1, s2);
6902 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6903 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6904 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6905 .recover_open = nfs4_open_reclaim,
6906 .recover_lock = nfs4_lock_reclaim,
6907 .establish_clid = nfs4_init_clientid,
6908 .get_clid_cred = nfs4_get_setclientid_cred,
6909 .detect_trunking = nfs40_discover_server_trunking,
6912 #if defined(CONFIG_NFS_V4_1)
6913 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6914 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6915 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6916 .recover_open = nfs4_open_reclaim,
6917 .recover_lock = nfs4_lock_reclaim,
6918 .establish_clid = nfs41_init_clientid,
6919 .get_clid_cred = nfs4_get_exchange_id_cred,
6920 .reclaim_complete = nfs41_proc_reclaim_complete,
6921 .detect_trunking = nfs41_discover_server_trunking,
6923 #endif /* CONFIG_NFS_V4_1 */
6925 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6926 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6927 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6928 .recover_open = nfs4_open_expired,
6929 .recover_lock = nfs4_lock_expired,
6930 .establish_clid = nfs4_init_clientid,
6931 .get_clid_cred = nfs4_get_setclientid_cred,
6934 #if defined(CONFIG_NFS_V4_1)
6935 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6936 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6937 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6938 .recover_open = nfs41_open_expired,
6939 .recover_lock = nfs41_lock_expired,
6940 .establish_clid = nfs41_init_clientid,
6941 .get_clid_cred = nfs4_get_exchange_id_cred,
6943 #endif /* CONFIG_NFS_V4_1 */
6945 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6946 .sched_state_renewal = nfs4_proc_async_renew,
6947 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6948 .renew_lease = nfs4_proc_renew,
6951 #if defined(CONFIG_NFS_V4_1)
6952 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6953 .sched_state_renewal = nfs41_proc_async_sequence,
6954 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
6955 .renew_lease = nfs4_proc_sequence,
6959 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6961 .call_sync = _nfs4_call_sync,
6962 .match_stateid = nfs4_match_stateid,
6963 .find_root_sec = nfs4_find_root_sec,
6964 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6965 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6966 .state_renewal_ops = &nfs40_state_renewal_ops,
6969 #if defined(CONFIG_NFS_V4_1)
6970 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6972 .call_sync = _nfs4_call_sync_session,
6973 .match_stateid = nfs41_match_stateid,
6974 .find_root_sec = nfs41_find_root_sec,
6975 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6976 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6977 .state_renewal_ops = &nfs41_state_renewal_ops,
6981 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6982 [0] = &nfs_v4_0_minor_ops,
6983 #if defined(CONFIG_NFS_V4_1)
6984 [1] = &nfs_v4_1_minor_ops,
6988 const struct inode_operations nfs4_dir_inode_operations = {
6989 .create = nfs_create,
6990 .lookup = nfs_lookup,
6991 .atomic_open = nfs_atomic_open,
6993 .unlink = nfs_unlink,
6994 .symlink = nfs_symlink,
6998 .rename = nfs_rename,
6999 .permission = nfs_permission,
7000 .getattr = nfs_getattr,
7001 .setattr = nfs_setattr,
7002 .getxattr = generic_getxattr,
7003 .setxattr = generic_setxattr,
7004 .listxattr = generic_listxattr,
7005 .removexattr = generic_removexattr,
7008 static const struct inode_operations nfs4_file_inode_operations = {
7009 .permission = nfs_permission,
7010 .getattr = nfs_getattr,
7011 .setattr = nfs_setattr,
7012 .getxattr = generic_getxattr,
7013 .setxattr = generic_setxattr,
7014 .listxattr = generic_listxattr,
7015 .removexattr = generic_removexattr,
7018 const struct nfs_rpc_ops nfs_v4_clientops = {
7019 .version = 4, /* protocol version */
7020 .dentry_ops = &nfs4_dentry_operations,
7021 .dir_inode_ops = &nfs4_dir_inode_operations,
7022 .file_inode_ops = &nfs4_file_inode_operations,
7023 .file_ops = &nfs4_file_operations,
7024 .getroot = nfs4_proc_get_root,
7025 .submount = nfs4_submount,
7026 .try_mount = nfs4_try_mount,
7027 .getattr = nfs4_proc_getattr,
7028 .setattr = nfs4_proc_setattr,
7029 .lookup = nfs4_proc_lookup,
7030 .access = nfs4_proc_access,
7031 .readlink = nfs4_proc_readlink,
7032 .create = nfs4_proc_create,
7033 .remove = nfs4_proc_remove,
7034 .unlink_setup = nfs4_proc_unlink_setup,
7035 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
7036 .unlink_done = nfs4_proc_unlink_done,
7037 .rename = nfs4_proc_rename,
7038 .rename_setup = nfs4_proc_rename_setup,
7039 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
7040 .rename_done = nfs4_proc_rename_done,
7041 .link = nfs4_proc_link,
7042 .symlink = nfs4_proc_symlink,
7043 .mkdir = nfs4_proc_mkdir,
7044 .rmdir = nfs4_proc_remove,
7045 .readdir = nfs4_proc_readdir,
7046 .mknod = nfs4_proc_mknod,
7047 .statfs = nfs4_proc_statfs,
7048 .fsinfo = nfs4_proc_fsinfo,
7049 .pathconf = nfs4_proc_pathconf,
7050 .set_capabilities = nfs4_server_capabilities,
7051 .decode_dirent = nfs4_decode_dirent,
7052 .read_setup = nfs4_proc_read_setup,
7053 .read_pageio_init = pnfs_pageio_init_read,
7054 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
7055 .read_done = nfs4_read_done,
7056 .write_setup = nfs4_proc_write_setup,
7057 .write_pageio_init = pnfs_pageio_init_write,
7058 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
7059 .write_done = nfs4_write_done,
7060 .commit_setup = nfs4_proc_commit_setup,
7061 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
7062 .commit_done = nfs4_commit_done,
7063 .lock = nfs4_proc_lock,
7064 .clear_acl_cache = nfs4_zap_acl_attr,
7065 .close_context = nfs4_close_context,
7066 .open_context = nfs4_atomic_open,
7067 .have_delegation = nfs4_have_delegation,
7068 .return_delegation = nfs4_inode_return_delegation,
7069 .alloc_client = nfs4_alloc_client,
7070 .init_client = nfs4_init_client,
7071 .free_client = nfs4_free_client,
7072 .create_server = nfs4_create_server,
7073 .clone_server = nfs_clone_server,
7076 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7077 .prefix = XATTR_NAME_NFSV4_ACL,
7078 .list = nfs4_xattr_list_nfs4_acl,
7079 .get = nfs4_xattr_get_nfs4_acl,
7080 .set = nfs4_xattr_set_nfs4_acl,
7083 const struct xattr_handler *nfs4_xattr_handlers[] = {
7084 &nfs4_xattr_nfs4_acl_handler,