4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/nfs_idmap.h>
55 #include <linux/sunrpc/bc_xprt.h>
56 #include <linux/xattr.h>
57 #include <linux/utsname.h>
58 #include <linux/freezer.h>
61 #include "delegation.h"
68 #define NFSDBG_FACILITY NFSDBG_PROC
70 #define NFS4_POLL_RETRY_MIN (HZ/10)
71 #define NFS4_POLL_RETRY_MAX (15*HZ)
73 #define NFS4_MAX_LOOP_ON_RECOVER (10)
76 static int _nfs4_proc_open(struct nfs4_opendata *data);
77 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
78 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
79 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
80 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
81 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
82 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
83 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
84 struct nfs_fattr *fattr, struct iattr *sattr,
85 struct nfs4_state *state);
86 #ifdef CONFIG_NFS_V4_1
87 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
88 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
90 /* Prevent leaks of NFSv4 errors into userland */
91 static int nfs4_map_errors(int err)
96 case -NFS4ERR_RESOURCE:
98 case -NFS4ERR_WRONGSEC:
100 case -NFS4ERR_BADOWNER:
101 case -NFS4ERR_BADNAME:
103 case -NFS4ERR_SHARE_DENIED:
105 case -NFS4ERR_MINOR_VERS_MISMATCH:
106 return -EPROTONOSUPPORT;
107 case -NFS4ERR_ACCESS:
110 dprintk("%s could not handle NFSv4 error %d\n",
118 * This is our standard bitmap for GETATTR requests.
120 const u32 nfs4_fattr_bitmap[3] = {
122 | FATTR4_WORD0_CHANGE
125 | FATTR4_WORD0_FILEID,
127 | FATTR4_WORD1_NUMLINKS
129 | FATTR4_WORD1_OWNER_GROUP
130 | FATTR4_WORD1_RAWDEV
131 | FATTR4_WORD1_SPACE_USED
132 | FATTR4_WORD1_TIME_ACCESS
133 | FATTR4_WORD1_TIME_METADATA
134 | FATTR4_WORD1_TIME_MODIFY
137 static const u32 nfs4_pnfs_open_bitmap[3] = {
139 | FATTR4_WORD0_CHANGE
142 | FATTR4_WORD0_FILEID,
144 | FATTR4_WORD1_NUMLINKS
146 | FATTR4_WORD1_OWNER_GROUP
147 | FATTR4_WORD1_RAWDEV
148 | FATTR4_WORD1_SPACE_USED
149 | FATTR4_WORD1_TIME_ACCESS
150 | FATTR4_WORD1_TIME_METADATA
151 | FATTR4_WORD1_TIME_MODIFY,
152 FATTR4_WORD2_MDSTHRESHOLD
155 static const u32 nfs4_open_noattr_bitmap[3] = {
157 | FATTR4_WORD0_CHANGE
158 | FATTR4_WORD0_FILEID,
161 const u32 nfs4_statfs_bitmap[2] = {
162 FATTR4_WORD0_FILES_AVAIL
163 | FATTR4_WORD0_FILES_FREE
164 | FATTR4_WORD0_FILES_TOTAL,
165 FATTR4_WORD1_SPACE_AVAIL
166 | FATTR4_WORD1_SPACE_FREE
167 | FATTR4_WORD1_SPACE_TOTAL
170 const u32 nfs4_pathconf_bitmap[2] = {
172 | FATTR4_WORD0_MAXNAME,
176 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
177 | FATTR4_WORD0_MAXREAD
178 | FATTR4_WORD0_MAXWRITE
179 | FATTR4_WORD0_LEASE_TIME,
180 FATTR4_WORD1_TIME_DELTA
181 | FATTR4_WORD1_FS_LAYOUT_TYPES,
182 FATTR4_WORD2_LAYOUT_BLKSIZE
185 const u32 nfs4_fs_locations_bitmap[2] = {
187 | FATTR4_WORD0_CHANGE
190 | FATTR4_WORD0_FILEID
191 | FATTR4_WORD0_FS_LOCATIONS,
193 | FATTR4_WORD1_NUMLINKS
195 | FATTR4_WORD1_OWNER_GROUP
196 | FATTR4_WORD1_RAWDEV
197 | FATTR4_WORD1_SPACE_USED
198 | FATTR4_WORD1_TIME_ACCESS
199 | FATTR4_WORD1_TIME_METADATA
200 | FATTR4_WORD1_TIME_MODIFY
201 | FATTR4_WORD1_MOUNTED_ON_FILEID
204 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
205 struct nfs4_readdir_arg *readdir)
210 readdir->cookie = cookie;
211 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
216 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
221 * NFSv4 servers do not return entries for '.' and '..'
222 * Therefore, we fake these entries here. We let '.'
223 * have cookie 0 and '..' have cookie 1. Note that
224 * when talking to the server, we always send cookie 0
227 start = p = kmap_atomic(*readdir->pages);
230 *p++ = xdr_one; /* next */
231 *p++ = xdr_zero; /* cookie, first word */
232 *p++ = xdr_one; /* cookie, second word */
233 *p++ = xdr_one; /* entry len */
234 memcpy(p, ".\0\0\0", 4); /* entry */
236 *p++ = xdr_one; /* bitmap length */
237 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
238 *p++ = htonl(8); /* attribute buffer length */
239 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
242 *p++ = xdr_one; /* next */
243 *p++ = xdr_zero; /* cookie, first word */
244 *p++ = xdr_two; /* cookie, second word */
245 *p++ = xdr_two; /* entry len */
246 memcpy(p, "..\0\0", 4); /* entry */
248 *p++ = xdr_one; /* bitmap length */
249 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
250 *p++ = htonl(8); /* attribute buffer length */
251 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
253 readdir->pgbase = (char *)p - (char *)start;
254 readdir->count -= readdir->pgbase;
255 kunmap_atomic(start);
258 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
264 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
265 nfs_wait_bit_killable, TASK_KILLABLE);
269 if (clp->cl_cons_state < 0)
270 return clp->cl_cons_state;
274 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
281 *timeout = NFS4_POLL_RETRY_MIN;
282 if (*timeout > NFS4_POLL_RETRY_MAX)
283 *timeout = NFS4_POLL_RETRY_MAX;
284 freezable_schedule_timeout_killable(*timeout);
285 if (fatal_signal_pending(current))
291 /* This is the error handling routine for processes that are allowed
294 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
296 struct nfs_client *clp = server->nfs_client;
297 struct nfs4_state *state = exception->state;
298 struct inode *inode = exception->inode;
301 exception->retry = 0;
305 case -NFS4ERR_OPENMODE:
306 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
307 nfs4_inode_return_delegation(inode);
308 exception->retry = 1;
313 nfs4_schedule_stateid_recovery(server, state);
314 goto wait_on_recovery;
315 case -NFS4ERR_DELEG_REVOKED:
316 case -NFS4ERR_ADMIN_REVOKED:
317 case -NFS4ERR_BAD_STATEID:
320 nfs_remove_bad_delegation(state->inode);
321 nfs4_schedule_stateid_recovery(server, state);
322 goto wait_on_recovery;
323 case -NFS4ERR_EXPIRED:
325 nfs4_schedule_stateid_recovery(server, state);
326 case -NFS4ERR_STALE_STATEID:
327 case -NFS4ERR_STALE_CLIENTID:
328 nfs4_schedule_lease_recovery(clp);
329 goto wait_on_recovery;
330 #if defined(CONFIG_NFS_V4_1)
331 case -NFS4ERR_BADSESSION:
332 case -NFS4ERR_BADSLOT:
333 case -NFS4ERR_BAD_HIGH_SLOT:
334 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
335 case -NFS4ERR_DEADSESSION:
336 case -NFS4ERR_SEQ_FALSE_RETRY:
337 case -NFS4ERR_SEQ_MISORDERED:
338 dprintk("%s ERROR: %d Reset session\n", __func__,
340 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
341 goto wait_on_recovery;
342 #endif /* defined(CONFIG_NFS_V4_1) */
343 case -NFS4ERR_FILE_OPEN:
344 if (exception->timeout > HZ) {
345 /* We have retried a decent amount, time to
354 ret = nfs4_delay(server->client, &exception->timeout);
357 case -NFS4ERR_RETRY_UNCACHED_REP:
358 case -NFS4ERR_OLD_STATEID:
359 exception->retry = 1;
361 case -NFS4ERR_BADOWNER:
362 /* The following works around a Linux server bug! */
363 case -NFS4ERR_BADNAME:
364 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
365 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
366 exception->retry = 1;
367 printk(KERN_WARNING "NFS: v4 server %s "
368 "does not accept raw "
370 "Reenabling the idmapper.\n",
371 server->nfs_client->cl_hostname);
374 /* We failed to handle the error */
375 return nfs4_map_errors(ret);
377 ret = nfs4_wait_clnt_recover(clp);
379 exception->retry = 1;
384 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
386 spin_lock(&clp->cl_lock);
387 if (time_before(clp->cl_last_renewal,timestamp))
388 clp->cl_last_renewal = timestamp;
389 spin_unlock(&clp->cl_lock);
392 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
394 do_renew_lease(server->nfs_client, timestamp);
397 #if defined(CONFIG_NFS_V4_1)
400 * nfs4_free_slot - free a slot and efficiently update slot table.
402 * freeing a slot is trivially done by clearing its respective bit
404 * If the freed slotid equals highest_used_slotid we want to update it
405 * so that the server would be able to size down the slot table if needed,
406 * otherwise we know that the highest_used_slotid is still in use.
407 * When updating highest_used_slotid there may be "holes" in the bitmap
408 * so we need to scan down from highest_used_slotid to 0 looking for the now
409 * highest slotid in use.
410 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
412 * Must be called while holding tbl->slot_tbl_lock
415 nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
417 /* clear used bit in bitmap */
418 __clear_bit(slotid, tbl->used_slots);
420 /* update highest_used_slotid when it is freed */
421 if (slotid == tbl->highest_used_slotid) {
422 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
423 if (slotid < tbl->max_slots)
424 tbl->highest_used_slotid = slotid;
426 tbl->highest_used_slotid = NFS4_NO_SLOT;
428 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
429 slotid, tbl->highest_used_slotid);
432 bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
434 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
439 * Signal state manager thread if session fore channel is drained
441 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
443 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
444 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
445 nfs4_set_task_privileged, NULL);
449 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
452 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
453 complete(&ses->fc_slot_table.complete);
457 * Signal state manager thread if session back channel is drained
459 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
461 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
462 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
464 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
465 complete(&ses->bc_slot_table.complete);
468 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
470 struct nfs4_session *session;
471 struct nfs4_slot_table *tbl;
474 /* just wake up the next guy waiting since
475 * we may have not consumed a slot after all */
476 dprintk("%s: No slot\n", __func__);
479 tbl = res->sr_slot->table;
480 session = tbl->session;
482 spin_lock(&tbl->slot_tbl_lock);
483 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
484 nfs4_check_drain_fc_complete(session);
485 spin_unlock(&tbl->slot_tbl_lock);
489 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
491 struct nfs4_session *session;
492 struct nfs4_slot *slot;
493 unsigned long timestamp;
494 struct nfs_client *clp;
497 * sr_status remains 1 if an RPC level error occurred. The server
498 * may or may not have processed the sequence operation..
499 * Proceed as if the server received and processed the sequence
502 if (res->sr_status == 1)
503 res->sr_status = NFS_OK;
505 /* don't increment the sequence number if the task wasn't sent */
506 if (!RPC_WAS_SENT(task))
510 session = slot->table->session;
512 /* Check the SEQUENCE operation status */
513 switch (res->sr_status) {
515 /* Update the slot's sequence and clientid lease timer */
517 timestamp = slot->renewal_time;
519 do_renew_lease(clp, timestamp);
520 /* Check sequence flags */
521 if (res->sr_status_flags != 0)
522 nfs4_schedule_lease_recovery(clp);
525 /* The server detected a resend of the RPC call and
526 * returned NFS4ERR_DELAY as per Section 2.10.6.2
529 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
535 /* Just update the slot sequence no. */
539 /* The session may be reset by one of the error handlers. */
540 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
541 nfs41_sequence_free_slot(res);
544 if (!rpc_restart_call(task))
546 rpc_delay(task, NFS4_POLL_RETRY_MAX);
550 static int nfs4_sequence_done(struct rpc_task *task,
551 struct nfs4_sequence_res *res)
553 if (res->sr_slot == NULL)
555 return nfs41_sequence_done(task, res);
559 * nfs4_alloc_slot - efficiently look for a free slot
561 * nfs4_alloc_slot looks for an unset bit in the used_slots bitmap.
562 * If found, we mark the slot as used, update the highest_used_slotid,
563 * and respectively set up the sequence operation args.
565 * Note: must be called with under the slot_tbl_lock.
567 static struct nfs4_slot *nfs4_alloc_slot(struct nfs4_slot_table *tbl)
569 struct nfs4_slot *ret = NULL;
572 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
573 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
575 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
576 if (slotid >= tbl->max_slots)
578 __set_bit(slotid, tbl->used_slots);
579 if (slotid > tbl->highest_used_slotid ||
580 tbl->highest_used_slotid == NFS4_NO_SLOT)
581 tbl->highest_used_slotid = slotid;
582 ret = &tbl->slots[slotid];
583 ret->renewal_time = jiffies;
586 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
587 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
588 ret ? ret->slot_nr : -1);
592 static void nfs41_init_sequence(struct nfs4_sequence_args *args,
593 struct nfs4_sequence_res *res, int cache_reply)
595 args->sa_slot = NULL;
596 args->sa_cache_this = 0;
598 args->sa_cache_this = 1;
602 int nfs41_setup_sequence(struct nfs4_session *session,
603 struct nfs4_sequence_args *args,
604 struct nfs4_sequence_res *res,
605 struct rpc_task *task)
607 struct nfs4_slot *slot;
608 struct nfs4_slot_table *tbl;
610 dprintk("--> %s\n", __func__);
611 /* slot already allocated? */
612 if (res->sr_slot != NULL)
615 tbl = &session->fc_slot_table;
617 spin_lock(&tbl->slot_tbl_lock);
618 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
619 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
620 /* The state manager will wait until the slot table is empty */
621 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
622 spin_unlock(&tbl->slot_tbl_lock);
623 dprintk("%s session is draining\n", __func__);
627 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
628 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
629 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
630 spin_unlock(&tbl->slot_tbl_lock);
631 dprintk("%s enforce FIFO order\n", __func__);
635 slot = nfs4_alloc_slot(tbl);
637 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
638 spin_unlock(&tbl->slot_tbl_lock);
639 dprintk("<-- %s: no free slots\n", __func__);
642 spin_unlock(&tbl->slot_tbl_lock);
644 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
646 args->sa_slot = slot;
648 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
649 slot->slot_nr, slot->seq_nr);
652 res->sr_status_flags = 0;
654 * sr_status is only set in decode_sequence, and so will remain
655 * set to 1 if an rpc level failure occurs.
660 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
662 int nfs4_setup_sequence(const struct nfs_server *server,
663 struct nfs4_sequence_args *args,
664 struct nfs4_sequence_res *res,
665 struct rpc_task *task)
667 struct nfs4_session *session = nfs4_get_session(server);
673 dprintk("--> %s clp %p session %p sr_slot %d\n",
674 __func__, session->clp, session, res->sr_slot ?
675 res->sr_slot->slot_nr : -1);
677 ret = nfs41_setup_sequence(session, args, res, task);
679 dprintk("<-- %s status=%d\n", __func__, ret);
683 struct nfs41_call_sync_data {
684 const struct nfs_server *seq_server;
685 struct nfs4_sequence_args *seq_args;
686 struct nfs4_sequence_res *seq_res;
689 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
691 struct nfs41_call_sync_data *data = calldata;
693 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
695 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
696 data->seq_res, task))
698 rpc_call_start(task);
701 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
703 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
704 nfs41_call_sync_prepare(task, calldata);
707 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
709 struct nfs41_call_sync_data *data = calldata;
711 nfs41_sequence_done(task, data->seq_res);
714 static const struct rpc_call_ops nfs41_call_sync_ops = {
715 .rpc_call_prepare = nfs41_call_sync_prepare,
716 .rpc_call_done = nfs41_call_sync_done,
719 static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
720 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
721 .rpc_call_done = nfs41_call_sync_done,
724 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
725 struct nfs_server *server,
726 struct rpc_message *msg,
727 struct nfs4_sequence_args *args,
728 struct nfs4_sequence_res *res,
732 struct rpc_task *task;
733 struct nfs41_call_sync_data data = {
734 .seq_server = server,
738 struct rpc_task_setup task_setup = {
741 .callback_ops = &nfs41_call_sync_ops,
742 .callback_data = &data
746 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
747 task = rpc_run_task(&task_setup);
751 ret = task->tk_status;
757 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
758 struct nfs_server *server,
759 struct rpc_message *msg,
760 struct nfs4_sequence_args *args,
761 struct nfs4_sequence_res *res,
764 nfs41_init_sequence(args, res, cache_reply);
765 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
770 void nfs41_init_sequence(struct nfs4_sequence_args *args,
771 struct nfs4_sequence_res *res, int cache_reply)
775 static int nfs4_sequence_done(struct rpc_task *task,
776 struct nfs4_sequence_res *res)
780 #endif /* CONFIG_NFS_V4_1 */
782 int _nfs4_call_sync(struct rpc_clnt *clnt,
783 struct nfs_server *server,
784 struct rpc_message *msg,
785 struct nfs4_sequence_args *args,
786 struct nfs4_sequence_res *res,
789 nfs41_init_sequence(args, res, cache_reply);
790 return rpc_call_sync(clnt, msg, 0);
794 int nfs4_call_sync(struct rpc_clnt *clnt,
795 struct nfs_server *server,
796 struct rpc_message *msg,
797 struct nfs4_sequence_args *args,
798 struct nfs4_sequence_res *res,
801 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
802 args, res, cache_reply);
805 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
807 struct nfs_inode *nfsi = NFS_I(dir);
809 spin_lock(&dir->i_lock);
810 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
811 if (!cinfo->atomic || cinfo->before != dir->i_version)
812 nfs_force_lookup_revalidate(dir);
813 dir->i_version = cinfo->after;
814 spin_unlock(&dir->i_lock);
817 struct nfs4_opendata {
819 struct nfs_openargs o_arg;
820 struct nfs_openres o_res;
821 struct nfs_open_confirmargs c_arg;
822 struct nfs_open_confirmres c_res;
823 struct nfs4_string owner_name;
824 struct nfs4_string group_name;
825 struct nfs_fattr f_attr;
827 struct dentry *dentry;
828 struct nfs4_state_owner *owner;
829 struct nfs4_state *state;
831 unsigned long timestamp;
832 unsigned int rpc_done : 1;
838 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
840 p->o_res.f_attr = &p->f_attr;
841 p->o_res.seqid = p->o_arg.seqid;
842 p->c_res.seqid = p->c_arg.seqid;
843 p->o_res.server = p->o_arg.server;
844 p->o_res.access_request = p->o_arg.access;
845 nfs_fattr_init(&p->f_attr);
846 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
849 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
850 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
851 const struct iattr *attrs,
854 struct dentry *parent = dget_parent(dentry);
855 struct inode *dir = parent->d_inode;
856 struct nfs_server *server = NFS_SERVER(dir);
857 struct nfs4_opendata *p;
859 p = kzalloc(sizeof(*p), gfp_mask);
862 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
863 if (p->o_arg.seqid == NULL)
865 nfs_sb_active(dentry->d_sb);
866 p->dentry = dget(dentry);
869 atomic_inc(&sp->so_count);
870 p->o_arg.fh = NFS_FH(dir);
871 p->o_arg.open_flags = flags;
872 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
873 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
874 * will return permission denied for all bits until close */
875 if (!(flags & O_EXCL)) {
876 /* ask server to check for all possible rights as results
878 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
879 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
881 p->o_arg.clientid = server->nfs_client->cl_clientid;
882 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
883 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
884 p->o_arg.name = &dentry->d_name;
885 p->o_arg.server = server;
886 p->o_arg.bitmask = server->attr_bitmask;
887 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
888 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
889 if (attrs != NULL && attrs->ia_valid != 0) {
892 p->o_arg.u.attrs = &p->attrs;
893 memcpy(&p->attrs, attrs, sizeof(p->attrs));
896 verf[1] = current->pid;
897 memcpy(p->o_arg.u.verifier.data, verf,
898 sizeof(p->o_arg.u.verifier.data));
900 p->c_arg.fh = &p->o_res.fh;
901 p->c_arg.stateid = &p->o_res.stateid;
902 p->c_arg.seqid = p->o_arg.seqid;
903 nfs4_init_opendata_res(p);
913 static void nfs4_opendata_free(struct kref *kref)
915 struct nfs4_opendata *p = container_of(kref,
916 struct nfs4_opendata, kref);
917 struct super_block *sb = p->dentry->d_sb;
919 nfs_free_seqid(p->o_arg.seqid);
920 if (p->state != NULL)
921 nfs4_put_open_state(p->state);
922 nfs4_put_state_owner(p->owner);
926 nfs_fattr_free_names(&p->f_attr);
930 static void nfs4_opendata_put(struct nfs4_opendata *p)
933 kref_put(&p->kref, nfs4_opendata_free);
936 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
940 ret = rpc_wait_for_completion_task(task);
944 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
948 if (open_mode & (O_EXCL|O_TRUNC))
950 switch (mode & (FMODE_READ|FMODE_WRITE)) {
952 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
953 && state->n_rdonly != 0;
956 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
957 && state->n_wronly != 0;
959 case FMODE_READ|FMODE_WRITE:
960 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
961 && state->n_rdwr != 0;
967 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
969 if (delegation == NULL)
971 if ((delegation->type & fmode) != fmode)
973 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
975 nfs_mark_delegation_referenced(delegation);
979 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
988 case FMODE_READ|FMODE_WRITE:
991 nfs4_state_set_mode_locked(state, state->state | fmode);
994 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
996 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
997 nfs4_stateid_copy(&state->stateid, stateid);
998 nfs4_stateid_copy(&state->open_stateid, stateid);
1001 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1004 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1006 case FMODE_READ|FMODE_WRITE:
1007 set_bit(NFS_O_RDWR_STATE, &state->flags);
1011 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1013 write_seqlock(&state->seqlock);
1014 nfs_set_open_stateid_locked(state, stateid, fmode);
1015 write_sequnlock(&state->seqlock);
1018 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
1021 * Protect the call to nfs4_state_set_mode_locked and
1022 * serialise the stateid update
1024 write_seqlock(&state->seqlock);
1025 if (deleg_stateid != NULL) {
1026 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1027 set_bit(NFS_DELEGATED_STATE, &state->flags);
1029 if (open_stateid != NULL)
1030 nfs_set_open_stateid_locked(state, open_stateid, fmode);
1031 write_sequnlock(&state->seqlock);
1032 spin_lock(&state->owner->so_lock);
1033 update_open_stateflags(state, fmode);
1034 spin_unlock(&state->owner->so_lock);
1037 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
1039 struct nfs_inode *nfsi = NFS_I(state->inode);
1040 struct nfs_delegation *deleg_cur;
1043 fmode &= (FMODE_READ|FMODE_WRITE);
1046 deleg_cur = rcu_dereference(nfsi->delegation);
1047 if (deleg_cur == NULL)
1050 spin_lock(&deleg_cur->lock);
1051 if (nfsi->delegation != deleg_cur ||
1052 (deleg_cur->type & fmode) != fmode)
1053 goto no_delegation_unlock;
1055 if (delegation == NULL)
1056 delegation = &deleg_cur->stateid;
1057 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1058 goto no_delegation_unlock;
1060 nfs_mark_delegation_referenced(deleg_cur);
1061 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1063 no_delegation_unlock:
1064 spin_unlock(&deleg_cur->lock);
1068 if (!ret && open_stateid != NULL) {
1069 __update_open_stateid(state, open_stateid, NULL, fmode);
1077 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1079 struct nfs_delegation *delegation;
1082 delegation = rcu_dereference(NFS_I(inode)->delegation);
1083 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1088 nfs4_inode_return_delegation(inode);
1091 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1093 struct nfs4_state *state = opendata->state;
1094 struct nfs_inode *nfsi = NFS_I(state->inode);
1095 struct nfs_delegation *delegation;
1096 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
1097 fmode_t fmode = opendata->o_arg.fmode;
1098 nfs4_stateid stateid;
1102 if (can_open_cached(state, fmode, open_mode)) {
1103 spin_lock(&state->owner->so_lock);
1104 if (can_open_cached(state, fmode, open_mode)) {
1105 update_open_stateflags(state, fmode);
1106 spin_unlock(&state->owner->so_lock);
1107 goto out_return_state;
1109 spin_unlock(&state->owner->so_lock);
1112 delegation = rcu_dereference(nfsi->delegation);
1113 if (!can_open_delegated(delegation, fmode)) {
1117 /* Save the delegation */
1118 nfs4_stateid_copy(&stateid, &delegation->stateid);
1120 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1125 /* Try to update the stateid using the delegation */
1126 if (update_open_stateid(state, NULL, &stateid, fmode))
1127 goto out_return_state;
1130 return ERR_PTR(ret);
1132 atomic_inc(&state->count);
1137 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1139 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1140 struct nfs_delegation *delegation;
1141 int delegation_flags = 0;
1144 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1146 delegation_flags = delegation->flags;
1148 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1149 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1150 "returning a delegation for "
1151 "OPEN(CLAIM_DELEGATE_CUR)\n",
1153 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1154 nfs_inode_set_delegation(state->inode,
1155 data->owner->so_cred,
1158 nfs_inode_reclaim_delegation(state->inode,
1159 data->owner->so_cred,
1164 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1165 * and update the nfs4_state.
1167 static struct nfs4_state *
1168 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1170 struct inode *inode = data->state->inode;
1171 struct nfs4_state *state = data->state;
1174 if (!data->rpc_done) {
1175 ret = data->rpc_status;
1180 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1181 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1182 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1186 state = nfs4_get_open_state(inode, data->owner);
1190 ret = nfs_refresh_inode(inode, &data->f_attr);
1194 if (data->o_res.delegation_type != 0)
1195 nfs4_opendata_check_deleg(data, state);
1196 update_open_stateid(state, &data->o_res.stateid, NULL,
1201 return ERR_PTR(ret);
1205 static struct nfs4_state *
1206 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1208 struct inode *inode;
1209 struct nfs4_state *state = NULL;
1212 if (!data->rpc_done) {
1213 state = nfs4_try_open_cached(data);
1218 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1220 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1221 ret = PTR_ERR(inode);
1225 state = nfs4_get_open_state(inode, data->owner);
1228 if (data->o_res.delegation_type != 0)
1229 nfs4_opendata_check_deleg(data, state);
1230 update_open_stateid(state, &data->o_res.stateid, NULL,
1238 return ERR_PTR(ret);
1241 static struct nfs4_state *
1242 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1244 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1245 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1246 return _nfs4_opendata_to_nfs4_state(data);
1249 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1251 struct nfs_inode *nfsi = NFS_I(state->inode);
1252 struct nfs_open_context *ctx;
1254 spin_lock(&state->inode->i_lock);
1255 list_for_each_entry(ctx, &nfsi->open_files, list) {
1256 if (ctx->state != state)
1258 get_nfs_open_context(ctx);
1259 spin_unlock(&state->inode->i_lock);
1262 spin_unlock(&state->inode->i_lock);
1263 return ERR_PTR(-ENOENT);
1266 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1268 struct nfs4_opendata *opendata;
1270 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1271 if (opendata == NULL)
1272 return ERR_PTR(-ENOMEM);
1273 opendata->state = state;
1274 atomic_inc(&state->count);
1278 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1280 struct nfs4_state *newstate;
1283 opendata->o_arg.open_flags = 0;
1284 opendata->o_arg.fmode = fmode;
1285 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1286 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1287 nfs4_init_opendata_res(opendata);
1288 ret = _nfs4_recover_proc_open(opendata);
1291 newstate = nfs4_opendata_to_nfs4_state(opendata);
1292 if (IS_ERR(newstate))
1293 return PTR_ERR(newstate);
1294 nfs4_close_state(newstate, fmode);
1299 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1301 struct nfs4_state *newstate;
1304 /* memory barrier prior to reading state->n_* */
1305 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1307 if (state->n_rdwr != 0) {
1308 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1309 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1312 if (newstate != state)
1315 if (state->n_wronly != 0) {
1316 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1317 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1320 if (newstate != state)
1323 if (state->n_rdonly != 0) {
1324 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1325 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1328 if (newstate != state)
1332 * We may have performed cached opens for all three recoveries.
1333 * Check if we need to update the current stateid.
1335 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1336 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1337 write_seqlock(&state->seqlock);
1338 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1339 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1340 write_sequnlock(&state->seqlock);
1347 * reclaim state on the server after a reboot.
1349 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1351 struct nfs_delegation *delegation;
1352 struct nfs4_opendata *opendata;
1353 fmode_t delegation_type = 0;
1356 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1357 if (IS_ERR(opendata))
1358 return PTR_ERR(opendata);
1359 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1360 opendata->o_arg.fh = NFS_FH(state->inode);
1362 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1363 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1364 delegation_type = delegation->type;
1366 opendata->o_arg.u.delegation_type = delegation_type;
1367 status = nfs4_open_recover(opendata, state);
1368 nfs4_opendata_put(opendata);
1372 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1374 struct nfs_server *server = NFS_SERVER(state->inode);
1375 struct nfs4_exception exception = { };
1378 err = _nfs4_do_open_reclaim(ctx, state);
1379 if (err != -NFS4ERR_DELAY)
1381 nfs4_handle_exception(server, err, &exception);
1382 } while (exception.retry);
1386 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1388 struct nfs_open_context *ctx;
1391 ctx = nfs4_state_find_open_context(state);
1393 return PTR_ERR(ctx);
1394 ret = nfs4_do_open_reclaim(ctx, state);
1395 put_nfs_open_context(ctx);
1399 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1401 struct nfs4_opendata *opendata;
1404 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1405 if (IS_ERR(opendata))
1406 return PTR_ERR(opendata);
1407 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1408 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1409 ret = nfs4_open_recover(opendata, state);
1410 nfs4_opendata_put(opendata);
1414 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1416 struct nfs4_exception exception = { };
1417 struct nfs_server *server = NFS_SERVER(state->inode);
1420 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1426 case -NFS4ERR_BADSESSION:
1427 case -NFS4ERR_BADSLOT:
1428 case -NFS4ERR_BAD_HIGH_SLOT:
1429 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1430 case -NFS4ERR_DEADSESSION:
1431 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1433 case -NFS4ERR_STALE_CLIENTID:
1434 case -NFS4ERR_STALE_STATEID:
1435 case -NFS4ERR_EXPIRED:
1436 /* Don't recall a delegation if it was lost */
1437 nfs4_schedule_lease_recovery(server->nfs_client);
1441 * The show must go on: exit, but mark the
1442 * stateid as needing recovery.
1444 case -NFS4ERR_DELEG_REVOKED:
1445 case -NFS4ERR_ADMIN_REVOKED:
1446 case -NFS4ERR_BAD_STATEID:
1447 nfs_inode_find_state_and_recover(state->inode,
1449 nfs4_schedule_stateid_recovery(server, state);
1452 * User RPCSEC_GSS context has expired.
1453 * We cannot recover this stateid now, so
1454 * skip it and allow recovery thread to
1461 err = nfs4_handle_exception(server, err, &exception);
1462 } while (exception.retry);
1467 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1469 struct nfs4_opendata *data = calldata;
1471 data->rpc_status = task->tk_status;
1472 if (data->rpc_status == 0) {
1473 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1474 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1475 renew_lease(data->o_res.server, data->timestamp);
1480 static void nfs4_open_confirm_release(void *calldata)
1482 struct nfs4_opendata *data = calldata;
1483 struct nfs4_state *state = NULL;
1485 /* If this request hasn't been cancelled, do nothing */
1486 if (data->cancelled == 0)
1488 /* In case of error, no cleanup! */
1489 if (!data->rpc_done)
1491 state = nfs4_opendata_to_nfs4_state(data);
1493 nfs4_close_state(state, data->o_arg.fmode);
1495 nfs4_opendata_put(data);
1498 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1499 .rpc_call_done = nfs4_open_confirm_done,
1500 .rpc_release = nfs4_open_confirm_release,
1504 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1506 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1508 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1509 struct rpc_task *task;
1510 struct rpc_message msg = {
1511 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1512 .rpc_argp = &data->c_arg,
1513 .rpc_resp = &data->c_res,
1514 .rpc_cred = data->owner->so_cred,
1516 struct rpc_task_setup task_setup_data = {
1517 .rpc_client = server->client,
1518 .rpc_message = &msg,
1519 .callback_ops = &nfs4_open_confirm_ops,
1520 .callback_data = data,
1521 .workqueue = nfsiod_workqueue,
1522 .flags = RPC_TASK_ASYNC,
1526 kref_get(&data->kref);
1528 data->rpc_status = 0;
1529 data->timestamp = jiffies;
1530 task = rpc_run_task(&task_setup_data);
1532 return PTR_ERR(task);
1533 status = nfs4_wait_for_completion_rpc_task(task);
1535 data->cancelled = 1;
1538 status = data->rpc_status;
1543 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1545 struct nfs4_opendata *data = calldata;
1546 struct nfs4_state_owner *sp = data->owner;
1548 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1551 * Check if we still need to send an OPEN call, or if we can use
1552 * a delegation instead.
1554 if (data->state != NULL) {
1555 struct nfs_delegation *delegation;
1557 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1560 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1561 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1562 can_open_delegated(delegation, data->o_arg.fmode))
1563 goto unlock_no_action;
1566 /* Update client id. */
1567 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1568 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1569 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1570 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1571 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1573 data->timestamp = jiffies;
1574 if (nfs4_setup_sequence(data->o_arg.server,
1575 &data->o_arg.seq_args,
1576 &data->o_res.seq_res,
1578 nfs_release_seqid(data->o_arg.seqid);
1580 rpc_call_start(task);
1585 task->tk_action = NULL;
1589 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1591 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1592 nfs4_open_prepare(task, calldata);
1595 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1597 struct nfs4_opendata *data = calldata;
1599 data->rpc_status = task->tk_status;
1601 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1604 if (task->tk_status == 0) {
1605 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1606 switch (data->o_res.f_attr->mode & S_IFMT) {
1610 data->rpc_status = -ELOOP;
1613 data->rpc_status = -EISDIR;
1616 data->rpc_status = -ENOTDIR;
1619 renew_lease(data->o_res.server, data->timestamp);
1620 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1621 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1626 static void nfs4_open_release(void *calldata)
1628 struct nfs4_opendata *data = calldata;
1629 struct nfs4_state *state = NULL;
1631 /* If this request hasn't been cancelled, do nothing */
1632 if (data->cancelled == 0)
1634 /* In case of error, no cleanup! */
1635 if (data->rpc_status != 0 || !data->rpc_done)
1637 /* In case we need an open_confirm, no cleanup! */
1638 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1640 state = nfs4_opendata_to_nfs4_state(data);
1642 nfs4_close_state(state, data->o_arg.fmode);
1644 nfs4_opendata_put(data);
1647 static const struct rpc_call_ops nfs4_open_ops = {
1648 .rpc_call_prepare = nfs4_open_prepare,
1649 .rpc_call_done = nfs4_open_done,
1650 .rpc_release = nfs4_open_release,
1653 static const struct rpc_call_ops nfs4_recover_open_ops = {
1654 .rpc_call_prepare = nfs4_recover_open_prepare,
1655 .rpc_call_done = nfs4_open_done,
1656 .rpc_release = nfs4_open_release,
1659 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1661 struct inode *dir = data->dir->d_inode;
1662 struct nfs_server *server = NFS_SERVER(dir);
1663 struct nfs_openargs *o_arg = &data->o_arg;
1664 struct nfs_openres *o_res = &data->o_res;
1665 struct rpc_task *task;
1666 struct rpc_message msg = {
1667 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1670 .rpc_cred = data->owner->so_cred,
1672 struct rpc_task_setup task_setup_data = {
1673 .rpc_client = server->client,
1674 .rpc_message = &msg,
1675 .callback_ops = &nfs4_open_ops,
1676 .callback_data = data,
1677 .workqueue = nfsiod_workqueue,
1678 .flags = RPC_TASK_ASYNC,
1682 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1683 kref_get(&data->kref);
1685 data->rpc_status = 0;
1686 data->cancelled = 0;
1688 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1689 task = rpc_run_task(&task_setup_data);
1691 return PTR_ERR(task);
1692 status = nfs4_wait_for_completion_rpc_task(task);
1694 data->cancelled = 1;
1697 status = data->rpc_status;
1703 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1705 struct inode *dir = data->dir->d_inode;
1706 struct nfs_openres *o_res = &data->o_res;
1709 status = nfs4_run_open_task(data, 1);
1710 if (status != 0 || !data->rpc_done)
1713 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1715 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1716 status = _nfs4_proc_open_confirm(data);
1724 static int nfs4_opendata_access(struct rpc_cred *cred,
1725 struct nfs4_opendata *opendata,
1726 struct nfs4_state *state, fmode_t fmode)
1728 struct nfs_access_entry cache;
1731 /* access call failed or for some reason the server doesn't
1732 * support any access modes -- defer access call until later */
1733 if (opendata->o_res.access_supported == 0)
1737 /* don't check MAY_WRITE - a newly created file may not have
1738 * write mode bits, but POSIX allows the creating process to write */
1739 if (fmode & FMODE_READ)
1741 if (fmode & FMODE_EXEC)
1745 cache.jiffies = jiffies;
1746 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1747 nfs_access_add_cache(state->inode, &cache);
1749 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1752 /* even though OPEN succeeded, access is denied. Close the file */
1753 nfs4_close_state(state, fmode);
1758 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1760 static int _nfs4_proc_open(struct nfs4_opendata *data)
1762 struct inode *dir = data->dir->d_inode;
1763 struct nfs_server *server = NFS_SERVER(dir);
1764 struct nfs_openargs *o_arg = &data->o_arg;
1765 struct nfs_openres *o_res = &data->o_res;
1768 status = nfs4_run_open_task(data, 0);
1769 if (!data->rpc_done)
1772 if (status == -NFS4ERR_BADNAME &&
1773 !(o_arg->open_flags & O_CREAT))
1778 nfs_fattr_map_and_free_names(server, &data->f_attr);
1780 if (o_arg->open_flags & O_CREAT)
1781 update_changeattr(dir, &o_res->cinfo);
1782 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1783 server->caps &= ~NFS_CAP_POSIX_LOCK;
1784 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1785 status = _nfs4_proc_open_confirm(data);
1789 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1790 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1794 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1799 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1800 ret = nfs4_wait_clnt_recover(clp);
1803 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1804 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1806 nfs4_schedule_state_manager(clp);
1812 static int nfs4_recover_expired_lease(struct nfs_server *server)
1814 return nfs4_client_recover_expired_lease(server->nfs_client);
1819 * reclaim state on the server after a network partition.
1820 * Assumes caller holds the appropriate lock
1822 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1824 struct nfs4_opendata *opendata;
1827 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1828 if (IS_ERR(opendata))
1829 return PTR_ERR(opendata);
1830 ret = nfs4_open_recover(opendata, state);
1832 d_drop(ctx->dentry);
1833 nfs4_opendata_put(opendata);
1837 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1839 struct nfs_server *server = NFS_SERVER(state->inode);
1840 struct nfs4_exception exception = { };
1844 err = _nfs4_open_expired(ctx, state);
1848 case -NFS4ERR_GRACE:
1849 case -NFS4ERR_DELAY:
1850 nfs4_handle_exception(server, err, &exception);
1853 } while (exception.retry);
1858 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1860 struct nfs_open_context *ctx;
1863 ctx = nfs4_state_find_open_context(state);
1865 return PTR_ERR(ctx);
1866 ret = nfs4_do_open_expired(ctx, state);
1867 put_nfs_open_context(ctx);
1871 #if defined(CONFIG_NFS_V4_1)
1872 static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
1874 struct nfs_server *server = NFS_SERVER(state->inode);
1875 nfs4_stateid *stateid = &state->stateid;
1878 /* If a state reset has been done, test_stateid is unneeded */
1879 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1882 status = nfs41_test_stateid(server, stateid);
1883 if (status != NFS_OK) {
1884 /* Free the stateid unless the server explicitly
1885 * informs us the stateid is unrecognized. */
1886 if (status != -NFS4ERR_BAD_STATEID)
1887 nfs41_free_stateid(server, stateid);
1888 nfs_remove_bad_delegation(state->inode);
1890 write_seqlock(&state->seqlock);
1891 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1892 write_sequnlock(&state->seqlock);
1893 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1898 * nfs41_check_open_stateid - possibly free an open stateid
1900 * @state: NFSv4 state for an inode
1902 * Returns NFS_OK if recovery for this stateid is now finished.
1903 * Otherwise a negative NFS4ERR value is returned.
1905 static int nfs41_check_open_stateid(struct nfs4_state *state)
1907 struct nfs_server *server = NFS_SERVER(state->inode);
1908 nfs4_stateid *stateid = &state->open_stateid;
1911 /* If a state reset has been done, test_stateid is unneeded */
1912 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1913 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1914 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1915 return -NFS4ERR_BAD_STATEID;
1917 status = nfs41_test_stateid(server, stateid);
1918 if (status != NFS_OK) {
1919 /* Free the stateid unless the server explicitly
1920 * informs us the stateid is unrecognized. */
1921 if (status != -NFS4ERR_BAD_STATEID)
1922 nfs41_free_stateid(server, stateid);
1924 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1925 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1926 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1931 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1935 nfs41_clear_delegation_stateid(state);
1936 status = nfs41_check_open_stateid(state);
1937 if (status != NFS_OK)
1938 status = nfs4_open_expired(sp, state);
1944 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1945 * fields corresponding to attributes that were used to store the verifier.
1946 * Make sure we clobber those fields in the later setattr call
1948 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1950 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1951 !(sattr->ia_valid & ATTR_ATIME_SET))
1952 sattr->ia_valid |= ATTR_ATIME;
1954 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1955 !(sattr->ia_valid & ATTR_MTIME_SET))
1956 sattr->ia_valid |= ATTR_MTIME;
1960 * Returns a referenced nfs4_state
1962 static int _nfs4_do_open(struct inode *dir,
1963 struct dentry *dentry,
1966 struct iattr *sattr,
1967 struct rpc_cred *cred,
1968 struct nfs4_state **res,
1969 struct nfs4_threshold **ctx_th)
1971 struct nfs4_state_owner *sp;
1972 struct nfs4_state *state = NULL;
1973 struct nfs_server *server = NFS_SERVER(dir);
1974 struct nfs4_opendata *opendata;
1977 /* Protect against reboot recovery conflicts */
1979 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1981 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1984 status = nfs4_recover_expired_lease(server);
1986 goto err_put_state_owner;
1987 if (dentry->d_inode != NULL)
1988 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1990 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1991 if (opendata == NULL)
1992 goto err_put_state_owner;
1994 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1995 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1996 if (!opendata->f_attr.mdsthreshold)
1997 goto err_opendata_put;
1998 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2000 if (dentry->d_inode != NULL)
2001 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
2003 status = _nfs4_proc_open(opendata);
2005 goto err_opendata_put;
2007 state = nfs4_opendata_to_nfs4_state(opendata);
2008 status = PTR_ERR(state);
2010 goto err_opendata_put;
2011 if (server->caps & NFS_CAP_POSIX_LOCK)
2012 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2014 status = nfs4_opendata_access(cred, opendata, state, fmode);
2016 goto err_opendata_put;
2018 if (opendata->o_arg.open_flags & O_EXCL) {
2019 nfs4_exclusive_attrset(opendata, sattr);
2021 nfs_fattr_init(opendata->o_res.f_attr);
2022 status = nfs4_do_setattr(state->inode, cred,
2023 opendata->o_res.f_attr, sattr,
2026 nfs_setattr_update_inode(state->inode, sattr);
2027 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
2030 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2031 *ctx_th = opendata->f_attr.mdsthreshold;
2033 kfree(opendata->f_attr.mdsthreshold);
2034 opendata->f_attr.mdsthreshold = NULL;
2036 nfs4_opendata_put(opendata);
2037 nfs4_put_state_owner(sp);
2041 kfree(opendata->f_attr.mdsthreshold);
2042 nfs4_opendata_put(opendata);
2043 err_put_state_owner:
2044 nfs4_put_state_owner(sp);
2051 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2052 struct dentry *dentry,
2055 struct iattr *sattr,
2056 struct rpc_cred *cred,
2057 struct nfs4_threshold **ctx_th)
2059 struct nfs4_exception exception = { };
2060 struct nfs4_state *res;
2063 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
2065 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
2069 /* NOTE: BAD_SEQID means the server and client disagree about the
2070 * book-keeping w.r.t. state-changing operations
2071 * (OPEN/CLOSE/LOCK/LOCKU...)
2072 * It is actually a sign of a bug on the client or on the server.
2074 * If we receive a BAD_SEQID error in the particular case of
2075 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2076 * have unhashed the old state_owner for us, and that we can
2077 * therefore safely retry using a new one. We should still warn
2078 * the user though...
2080 if (status == -NFS4ERR_BAD_SEQID) {
2081 pr_warn_ratelimited("NFS: v4 server %s "
2082 " returned a bad sequence-id error!\n",
2083 NFS_SERVER(dir)->nfs_client->cl_hostname);
2084 exception.retry = 1;
2088 * BAD_STATEID on OPEN means that the server cancelled our
2089 * state before it received the OPEN_CONFIRM.
2090 * Recover by retrying the request as per the discussion
2091 * on Page 181 of RFC3530.
2093 if (status == -NFS4ERR_BAD_STATEID) {
2094 exception.retry = 1;
2097 if (status == -EAGAIN) {
2098 /* We must have found a delegation */
2099 exception.retry = 1;
2102 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2103 status, &exception));
2104 } while (exception.retry);
2108 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2109 struct nfs_fattr *fattr, struct iattr *sattr,
2110 struct nfs4_state *state)
2112 struct nfs_server *server = NFS_SERVER(inode);
2113 struct nfs_setattrargs arg = {
2114 .fh = NFS_FH(inode),
2117 .bitmask = server->attr_bitmask,
2119 struct nfs_setattrres res = {
2123 struct rpc_message msg = {
2124 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2129 unsigned long timestamp = jiffies;
2132 nfs_fattr_init(fattr);
2134 if (state != NULL) {
2135 struct nfs_lockowner lockowner = {
2136 .l_owner = current->files,
2137 .l_pid = current->tgid,
2139 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2141 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2143 /* Use that stateid */
2145 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2147 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2148 if (status == 0 && state != NULL)
2149 renew_lease(server, timestamp);
2153 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2154 struct nfs_fattr *fattr, struct iattr *sattr,
2155 struct nfs4_state *state)
2157 struct nfs_server *server = NFS_SERVER(inode);
2158 struct nfs4_exception exception = {
2164 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2166 case -NFS4ERR_OPENMODE:
2167 if (state && !(state->state & FMODE_WRITE)) {
2169 if (sattr->ia_valid & ATTR_OPEN)
2174 err = nfs4_handle_exception(server, err, &exception);
2175 } while (exception.retry);
2180 struct nfs4_closedata {
2181 struct inode *inode;
2182 struct nfs4_state *state;
2183 struct nfs_closeargs arg;
2184 struct nfs_closeres res;
2185 struct nfs_fattr fattr;
2186 unsigned long timestamp;
2191 static void nfs4_free_closedata(void *data)
2193 struct nfs4_closedata *calldata = data;
2194 struct nfs4_state_owner *sp = calldata->state->owner;
2195 struct super_block *sb = calldata->state->inode->i_sb;
2198 pnfs_roc_release(calldata->state->inode);
2199 nfs4_put_open_state(calldata->state);
2200 nfs_free_seqid(calldata->arg.seqid);
2201 nfs4_put_state_owner(sp);
2202 nfs_sb_deactive_async(sb);
2206 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2209 spin_lock(&state->owner->so_lock);
2210 if (!(fmode & FMODE_READ))
2211 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2212 if (!(fmode & FMODE_WRITE))
2213 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2214 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2215 spin_unlock(&state->owner->so_lock);
2218 static void nfs4_close_done(struct rpc_task *task, void *data)
2220 struct nfs4_closedata *calldata = data;
2221 struct nfs4_state *state = calldata->state;
2222 struct nfs_server *server = NFS_SERVER(calldata->inode);
2224 dprintk("%s: begin!\n", __func__);
2225 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2227 /* hmm. we are done with the inode, and in the process of freeing
2228 * the state_owner. we keep this around to process errors
2230 switch (task->tk_status) {
2233 pnfs_roc_set_barrier(state->inode,
2234 calldata->roc_barrier);
2235 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2236 renew_lease(server, calldata->timestamp);
2237 nfs4_close_clear_stateid_flags(state,
2238 calldata->arg.fmode);
2240 case -NFS4ERR_STALE_STATEID:
2241 case -NFS4ERR_OLD_STATEID:
2242 case -NFS4ERR_BAD_STATEID:
2243 case -NFS4ERR_EXPIRED:
2244 if (calldata->arg.fmode == 0)
2247 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2248 rpc_restart_call_prepare(task);
2250 nfs_release_seqid(calldata->arg.seqid);
2251 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2252 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2255 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2257 struct nfs4_closedata *calldata = data;
2258 struct nfs4_state *state = calldata->state;
2259 struct inode *inode = calldata->inode;
2262 dprintk("%s: begin!\n", __func__);
2263 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2266 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2267 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2268 spin_lock(&state->owner->so_lock);
2269 /* Calculate the change in open mode */
2270 if (state->n_rdwr == 0) {
2271 if (state->n_rdonly == 0) {
2272 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2273 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2274 calldata->arg.fmode &= ~FMODE_READ;
2276 if (state->n_wronly == 0) {
2277 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2278 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2279 calldata->arg.fmode &= ~FMODE_WRITE;
2282 spin_unlock(&state->owner->so_lock);
2285 /* Note: exit _without_ calling nfs4_close_done */
2286 task->tk_action = NULL;
2290 if (calldata->arg.fmode == 0) {
2291 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2292 if (calldata->roc &&
2293 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
2297 nfs_fattr_init(calldata->res.fattr);
2298 calldata->timestamp = jiffies;
2299 if (nfs4_setup_sequence(NFS_SERVER(inode),
2300 &calldata->arg.seq_args,
2301 &calldata->res.seq_res,
2303 nfs_release_seqid(calldata->arg.seqid);
2305 rpc_call_start(task);
2307 dprintk("%s: done!\n", __func__);
2310 static const struct rpc_call_ops nfs4_close_ops = {
2311 .rpc_call_prepare = nfs4_close_prepare,
2312 .rpc_call_done = nfs4_close_done,
2313 .rpc_release = nfs4_free_closedata,
2317 * It is possible for data to be read/written from a mem-mapped file
2318 * after the sys_close call (which hits the vfs layer as a flush).
2319 * This means that we can't safely call nfsv4 close on a file until
2320 * the inode is cleared. This in turn means that we are not good
2321 * NFSv4 citizens - we do not indicate to the server to update the file's
2322 * share state even when we are done with one of the three share
2323 * stateid's in the inode.
2325 * NOTE: Caller must be holding the sp->so_owner semaphore!
2327 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2329 struct nfs_server *server = NFS_SERVER(state->inode);
2330 struct nfs4_closedata *calldata;
2331 struct nfs4_state_owner *sp = state->owner;
2332 struct rpc_task *task;
2333 struct rpc_message msg = {
2334 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2335 .rpc_cred = state->owner->so_cred,
2337 struct rpc_task_setup task_setup_data = {
2338 .rpc_client = server->client,
2339 .rpc_message = &msg,
2340 .callback_ops = &nfs4_close_ops,
2341 .workqueue = nfsiod_workqueue,
2342 .flags = RPC_TASK_ASYNC,
2344 int status = -ENOMEM;
2346 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2347 if (calldata == NULL)
2349 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2350 calldata->inode = state->inode;
2351 calldata->state = state;
2352 calldata->arg.fh = NFS_FH(state->inode);
2353 calldata->arg.stateid = &state->open_stateid;
2354 /* Serialization for the sequence id */
2355 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2356 if (calldata->arg.seqid == NULL)
2357 goto out_free_calldata;
2358 calldata->arg.fmode = 0;
2359 calldata->arg.bitmask = server->cache_consistency_bitmask;
2360 calldata->res.fattr = &calldata->fattr;
2361 calldata->res.seqid = calldata->arg.seqid;
2362 calldata->res.server = server;
2363 calldata->roc = pnfs_roc(state->inode);
2364 nfs_sb_active(calldata->inode->i_sb);
2366 msg.rpc_argp = &calldata->arg;
2367 msg.rpc_resp = &calldata->res;
2368 task_setup_data.callback_data = calldata;
2369 task = rpc_run_task(&task_setup_data);
2371 return PTR_ERR(task);
2374 status = rpc_wait_for_completion_task(task);
2380 nfs4_put_open_state(state);
2381 nfs4_put_state_owner(sp);
2385 static struct inode *
2386 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2388 struct nfs4_state *state;
2390 /* Protect against concurrent sillydeletes */
2391 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2392 ctx->cred, &ctx->mdsthreshold);
2394 return ERR_CAST(state);
2396 return igrab(state->inode);
2399 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2401 if (ctx->state == NULL)
2404 nfs4_close_sync(ctx->state, ctx->mode);
2406 nfs4_close_state(ctx->state, ctx->mode);
2409 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2411 struct nfs4_server_caps_arg args = {
2414 struct nfs4_server_caps_res res = {};
2415 struct rpc_message msg = {
2416 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2422 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2424 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2425 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2426 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2427 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2428 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2429 NFS_CAP_CTIME|NFS_CAP_MTIME);
2430 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2431 server->caps |= NFS_CAP_ACLS;
2432 if (res.has_links != 0)
2433 server->caps |= NFS_CAP_HARDLINKS;
2434 if (res.has_symlinks != 0)
2435 server->caps |= NFS_CAP_SYMLINKS;
2436 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2437 server->caps |= NFS_CAP_FILEID;
2438 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2439 server->caps |= NFS_CAP_MODE;
2440 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2441 server->caps |= NFS_CAP_NLINK;
2442 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2443 server->caps |= NFS_CAP_OWNER;
2444 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2445 server->caps |= NFS_CAP_OWNER_GROUP;
2446 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2447 server->caps |= NFS_CAP_ATIME;
2448 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2449 server->caps |= NFS_CAP_CTIME;
2450 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2451 server->caps |= NFS_CAP_MTIME;
2453 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2454 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2455 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2456 server->acl_bitmask = res.acl_bitmask;
2457 server->fh_expire_type = res.fh_expire_type;
2463 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2465 struct nfs4_exception exception = { };
2468 err = nfs4_handle_exception(server,
2469 _nfs4_server_capabilities(server, fhandle),
2471 } while (exception.retry);
2475 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2476 struct nfs_fsinfo *info)
2478 struct nfs4_lookup_root_arg args = {
2479 .bitmask = nfs4_fattr_bitmap,
2481 struct nfs4_lookup_res res = {
2483 .fattr = info->fattr,
2486 struct rpc_message msg = {
2487 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2492 nfs_fattr_init(info->fattr);
2493 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2496 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2497 struct nfs_fsinfo *info)
2499 struct nfs4_exception exception = { };
2502 err = _nfs4_lookup_root(server, fhandle, info);
2505 case -NFS4ERR_WRONGSEC:
2508 err = nfs4_handle_exception(server, err, &exception);
2510 } while (exception.retry);
2515 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2516 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2518 struct rpc_auth *auth;
2521 auth = rpcauth_create(flavor, server->client);
2526 ret = nfs4_lookup_root(server, fhandle, info);
2531 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2532 struct nfs_fsinfo *info)
2534 int i, len, status = 0;
2535 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2537 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2541 for (i = 0; i < len; i++) {
2542 /* AUTH_UNIX is the default flavor if none was specified,
2543 * thus has already been tried. */
2544 if (flav_array[i] == RPC_AUTH_UNIX)
2547 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2548 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2553 * -EACCESS could mean that the user doesn't have correct permissions
2554 * to access the mount. It could also mean that we tried to mount
2555 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2556 * existing mount programs don't handle -EACCES very well so it should
2557 * be mapped to -EPERM instead.
2559 if (status == -EACCES)
2565 * get the file handle for the "/" directory on the server
2567 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2568 struct nfs_fsinfo *info)
2570 int minor_version = server->nfs_client->cl_minorversion;
2571 int status = nfs4_lookup_root(server, fhandle, info);
2572 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2574 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2575 * by nfs4_map_errors() as this function exits.
2577 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2579 status = nfs4_server_capabilities(server, fhandle);
2581 status = nfs4_do_fsinfo(server, fhandle, info);
2582 return nfs4_map_errors(status);
2585 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2586 struct nfs_fsinfo *info)
2589 struct nfs_fattr *fattr = info->fattr;
2591 error = nfs4_server_capabilities(server, mntfh);
2593 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2597 error = nfs4_proc_getattr(server, mntfh, fattr);
2599 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2603 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2604 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2605 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2611 * Get locations and (maybe) other attributes of a referral.
2612 * Note that we'll actually follow the referral later when
2613 * we detect fsid mismatch in inode revalidation
2615 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2616 const struct qstr *name, struct nfs_fattr *fattr,
2617 struct nfs_fh *fhandle)
2619 int status = -ENOMEM;
2620 struct page *page = NULL;
2621 struct nfs4_fs_locations *locations = NULL;
2623 page = alloc_page(GFP_KERNEL);
2626 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2627 if (locations == NULL)
2630 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2633 /* Make sure server returned a different fsid for the referral */
2634 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2635 dprintk("%s: server did not return a different fsid for"
2636 " a referral at %s\n", __func__, name->name);
2640 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2641 nfs_fixup_referral_attributes(&locations->fattr);
2643 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2644 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2645 memset(fhandle, 0, sizeof(struct nfs_fh));
2653 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2655 struct nfs4_getattr_arg args = {
2657 .bitmask = server->attr_bitmask,
2659 struct nfs4_getattr_res res = {
2663 struct rpc_message msg = {
2664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2669 nfs_fattr_init(fattr);
2670 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2673 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2675 struct nfs4_exception exception = { };
2678 err = nfs4_handle_exception(server,
2679 _nfs4_proc_getattr(server, fhandle, fattr),
2681 } while (exception.retry);
2686 * The file is not closed if it is opened due to the a request to change
2687 * the size of the file. The open call will not be needed once the
2688 * VFS layer lookup-intents are implemented.
2690 * Close is called when the inode is destroyed.
2691 * If we haven't opened the file for O_WRONLY, we
2692 * need to in the size_change case to obtain a stateid.
2695 * Because OPEN is always done by name in nfsv4, it is
2696 * possible that we opened a different file by the same
2697 * name. We can recognize this race condition, but we
2698 * can't do anything about it besides returning an error.
2700 * This will be fixed with VFS changes (lookup-intent).
2703 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2704 struct iattr *sattr)
2706 struct inode *inode = dentry->d_inode;
2707 struct rpc_cred *cred = NULL;
2708 struct nfs4_state *state = NULL;
2711 if (pnfs_ld_layoutret_on_setattr(inode))
2712 pnfs_return_layout(inode);
2714 nfs_fattr_init(fattr);
2716 /* Deal with open(O_TRUNC) */
2717 if (sattr->ia_valid & ATTR_OPEN)
2718 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2720 /* Optimization: if the end result is no change, don't RPC */
2721 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2724 /* Search for an existing open(O_WRITE) file */
2725 if (sattr->ia_valid & ATTR_FILE) {
2726 struct nfs_open_context *ctx;
2728 ctx = nfs_file_open_context(sattr->ia_file);
2735 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2737 nfs_setattr_update_inode(inode, sattr);
2741 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2742 const struct qstr *name, struct nfs_fh *fhandle,
2743 struct nfs_fattr *fattr)
2745 struct nfs_server *server = NFS_SERVER(dir);
2747 struct nfs4_lookup_arg args = {
2748 .bitmask = server->attr_bitmask,
2749 .dir_fh = NFS_FH(dir),
2752 struct nfs4_lookup_res res = {
2757 struct rpc_message msg = {
2758 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2763 nfs_fattr_init(fattr);
2765 dprintk("NFS call lookup %s\n", name->name);
2766 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2767 dprintk("NFS reply lookup: %d\n", status);
2771 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2773 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2774 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2775 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2779 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2780 struct qstr *name, struct nfs_fh *fhandle,
2781 struct nfs_fattr *fattr)
2783 struct nfs4_exception exception = { };
2784 struct rpc_clnt *client = *clnt;
2787 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2789 case -NFS4ERR_BADNAME:
2792 case -NFS4ERR_MOVED:
2793 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
2795 case -NFS4ERR_WRONGSEC:
2797 if (client != *clnt)
2800 client = nfs4_create_sec_client(client, dir, name);
2802 return PTR_ERR(client);
2804 exception.retry = 1;
2807 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2809 } while (exception.retry);
2814 else if (client != *clnt)
2815 rpc_shutdown_client(client);
2820 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
2821 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2824 struct rpc_clnt *client = NFS_CLIENT(dir);
2826 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2827 if (client != NFS_CLIENT(dir)) {
2828 rpc_shutdown_client(client);
2829 nfs_fixup_secinfo_attributes(fattr);
2835 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2836 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2839 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2841 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2843 rpc_shutdown_client(client);
2844 return ERR_PTR(status);
2849 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2851 struct nfs_server *server = NFS_SERVER(inode);
2852 struct nfs4_accessargs args = {
2853 .fh = NFS_FH(inode),
2854 .bitmask = server->cache_consistency_bitmask,
2856 struct nfs4_accessres res = {
2859 struct rpc_message msg = {
2860 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2863 .rpc_cred = entry->cred,
2865 int mode = entry->mask;
2869 * Determine which access bits we want to ask for...
2871 if (mode & MAY_READ)
2872 args.access |= NFS4_ACCESS_READ;
2873 if (S_ISDIR(inode->i_mode)) {
2874 if (mode & MAY_WRITE)
2875 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2876 if (mode & MAY_EXEC)
2877 args.access |= NFS4_ACCESS_LOOKUP;
2879 if (mode & MAY_WRITE)
2880 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2881 if (mode & MAY_EXEC)
2882 args.access |= NFS4_ACCESS_EXECUTE;
2885 res.fattr = nfs_alloc_fattr();
2886 if (res.fattr == NULL)
2889 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2891 nfs_access_set_mask(entry, res.access);
2892 nfs_refresh_inode(inode, res.fattr);
2894 nfs_free_fattr(res.fattr);
2898 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2900 struct nfs4_exception exception = { };
2903 err = nfs4_handle_exception(NFS_SERVER(inode),
2904 _nfs4_proc_access(inode, entry),
2906 } while (exception.retry);
2911 * TODO: For the time being, we don't try to get any attributes
2912 * along with any of the zero-copy operations READ, READDIR,
2915 * In the case of the first three, we want to put the GETATTR
2916 * after the read-type operation -- this is because it is hard
2917 * to predict the length of a GETATTR response in v4, and thus
2918 * align the READ data correctly. This means that the GETATTR
2919 * may end up partially falling into the page cache, and we should
2920 * shift it into the 'tail' of the xdr_buf before processing.
2921 * To do this efficiently, we need to know the total length
2922 * of data received, which doesn't seem to be available outside
2925 * In the case of WRITE, we also want to put the GETATTR after
2926 * the operation -- in this case because we want to make sure
2927 * we get the post-operation mtime and size.
2929 * Both of these changes to the XDR layer would in fact be quite
2930 * minor, but I decided to leave them for a subsequent patch.
2932 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2933 unsigned int pgbase, unsigned int pglen)
2935 struct nfs4_readlink args = {
2936 .fh = NFS_FH(inode),
2941 struct nfs4_readlink_res res;
2942 struct rpc_message msg = {
2943 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2948 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2951 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2952 unsigned int pgbase, unsigned int pglen)
2954 struct nfs4_exception exception = { };
2957 err = nfs4_handle_exception(NFS_SERVER(inode),
2958 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2960 } while (exception.retry);
2965 * This is just for mknod. open(O_CREAT) will always do ->open_context().
2968 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2971 struct nfs_open_context *ctx;
2972 struct nfs4_state *state;
2975 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2977 return PTR_ERR(ctx);
2979 sattr->ia_mode &= ~current_umask();
2980 state = nfs4_do_open(dir, dentry, ctx->mode,
2981 flags, sattr, ctx->cred,
2982 &ctx->mdsthreshold);
2984 if (IS_ERR(state)) {
2985 status = PTR_ERR(state);
2988 d_add(dentry, igrab(state->inode));
2989 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2992 put_nfs_open_context(ctx);
2996 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2998 struct nfs_server *server = NFS_SERVER(dir);
2999 struct nfs_removeargs args = {
3003 struct nfs_removeres res = {
3006 struct rpc_message msg = {
3007 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3013 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
3015 update_changeattr(dir, &res.cinfo);
3019 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3021 struct nfs4_exception exception = { };
3024 err = nfs4_handle_exception(NFS_SERVER(dir),
3025 _nfs4_proc_remove(dir, name),
3027 } while (exception.retry);
3031 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
3033 struct nfs_server *server = NFS_SERVER(dir);
3034 struct nfs_removeargs *args = msg->rpc_argp;
3035 struct nfs_removeres *res = msg->rpc_resp;
3037 res->server = server;
3038 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
3039 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
3042 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3044 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
3045 &data->args.seq_args,
3049 rpc_call_start(task);
3052 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
3054 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3056 if (!nfs4_sequence_done(task, &res->seq_res))
3058 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3060 update_changeattr(dir, &res->cinfo);
3064 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3066 struct nfs_server *server = NFS_SERVER(dir);
3067 struct nfs_renameargs *arg = msg->rpc_argp;
3068 struct nfs_renameres *res = msg->rpc_resp;
3070 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
3071 res->server = server;
3072 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
3075 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3077 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3078 &data->args.seq_args,
3082 rpc_call_start(task);
3085 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3086 struct inode *new_dir)
3088 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3090 if (!nfs4_sequence_done(task, &res->seq_res))
3092 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3095 update_changeattr(old_dir, &res->old_cinfo);
3096 update_changeattr(new_dir, &res->new_cinfo);
3100 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3101 struct inode *new_dir, struct qstr *new_name)
3103 struct nfs_server *server = NFS_SERVER(old_dir);
3104 struct nfs_renameargs arg = {
3105 .old_dir = NFS_FH(old_dir),
3106 .new_dir = NFS_FH(new_dir),
3107 .old_name = old_name,
3108 .new_name = new_name,
3110 struct nfs_renameres res = {
3113 struct rpc_message msg = {
3114 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3118 int status = -ENOMEM;
3120 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3122 update_changeattr(old_dir, &res.old_cinfo);
3123 update_changeattr(new_dir, &res.new_cinfo);
3128 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3129 struct inode *new_dir, struct qstr *new_name)
3131 struct nfs4_exception exception = { };
3134 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3135 _nfs4_proc_rename(old_dir, old_name,
3138 } while (exception.retry);
3142 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3144 struct nfs_server *server = NFS_SERVER(inode);
3145 struct nfs4_link_arg arg = {
3146 .fh = NFS_FH(inode),
3147 .dir_fh = NFS_FH(dir),
3149 .bitmask = server->attr_bitmask,
3151 struct nfs4_link_res res = {
3154 struct rpc_message msg = {
3155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3159 int status = -ENOMEM;
3161 res.fattr = nfs_alloc_fattr();
3162 if (res.fattr == NULL)
3165 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3167 update_changeattr(dir, &res.cinfo);
3168 nfs_post_op_update_inode(inode, res.fattr);
3171 nfs_free_fattr(res.fattr);
3175 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3177 struct nfs4_exception exception = { };
3180 err = nfs4_handle_exception(NFS_SERVER(inode),
3181 _nfs4_proc_link(inode, dir, name),
3183 } while (exception.retry);
3187 struct nfs4_createdata {
3188 struct rpc_message msg;
3189 struct nfs4_create_arg arg;
3190 struct nfs4_create_res res;
3192 struct nfs_fattr fattr;
3195 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3196 struct qstr *name, struct iattr *sattr, u32 ftype)
3198 struct nfs4_createdata *data;
3200 data = kzalloc(sizeof(*data), GFP_KERNEL);
3202 struct nfs_server *server = NFS_SERVER(dir);
3204 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3205 data->msg.rpc_argp = &data->arg;
3206 data->msg.rpc_resp = &data->res;
3207 data->arg.dir_fh = NFS_FH(dir);
3208 data->arg.server = server;
3209 data->arg.name = name;
3210 data->arg.attrs = sattr;
3211 data->arg.ftype = ftype;
3212 data->arg.bitmask = server->attr_bitmask;
3213 data->res.server = server;
3214 data->res.fh = &data->fh;
3215 data->res.fattr = &data->fattr;
3216 nfs_fattr_init(data->res.fattr);
3221 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3223 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3224 &data->arg.seq_args, &data->res.seq_res, 1);
3226 update_changeattr(dir, &data->res.dir_cinfo);
3227 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3232 static void nfs4_free_createdata(struct nfs4_createdata *data)
3237 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3238 struct page *page, unsigned int len, struct iattr *sattr)
3240 struct nfs4_createdata *data;
3241 int status = -ENAMETOOLONG;
3243 if (len > NFS4_MAXPATHLEN)
3247 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3251 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3252 data->arg.u.symlink.pages = &page;
3253 data->arg.u.symlink.len = len;
3255 status = nfs4_do_create(dir, dentry, data);
3257 nfs4_free_createdata(data);
3262 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3263 struct page *page, unsigned int len, struct iattr *sattr)
3265 struct nfs4_exception exception = { };
3268 err = nfs4_handle_exception(NFS_SERVER(dir),
3269 _nfs4_proc_symlink(dir, dentry, page,
3272 } while (exception.retry);
3276 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3277 struct iattr *sattr)
3279 struct nfs4_createdata *data;
3280 int status = -ENOMEM;
3282 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3286 status = nfs4_do_create(dir, dentry, data);
3288 nfs4_free_createdata(data);
3293 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3294 struct iattr *sattr)
3296 struct nfs4_exception exception = { };
3299 sattr->ia_mode &= ~current_umask();
3301 err = nfs4_handle_exception(NFS_SERVER(dir),
3302 _nfs4_proc_mkdir(dir, dentry, sattr),
3304 } while (exception.retry);
3308 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3309 u64 cookie, struct page **pages, unsigned int count, int plus)
3311 struct inode *dir = dentry->d_inode;
3312 struct nfs4_readdir_arg args = {
3317 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3320 struct nfs4_readdir_res res;
3321 struct rpc_message msg = {
3322 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3329 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3330 dentry->d_parent->d_name.name,
3331 dentry->d_name.name,
3332 (unsigned long long)cookie);
3333 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3334 res.pgbase = args.pgbase;
3335 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3337 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3338 status += args.pgbase;
3341 nfs_invalidate_atime(dir);
3343 dprintk("%s: returns %d\n", __func__, status);
3347 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3348 u64 cookie, struct page **pages, unsigned int count, int plus)
3350 struct nfs4_exception exception = { };
3353 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3354 _nfs4_proc_readdir(dentry, cred, cookie,
3355 pages, count, plus),
3357 } while (exception.retry);
3361 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3362 struct iattr *sattr, dev_t rdev)
3364 struct nfs4_createdata *data;
3365 int mode = sattr->ia_mode;
3366 int status = -ENOMEM;
3368 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3373 data->arg.ftype = NF4FIFO;
3374 else if (S_ISBLK(mode)) {
3375 data->arg.ftype = NF4BLK;
3376 data->arg.u.device.specdata1 = MAJOR(rdev);
3377 data->arg.u.device.specdata2 = MINOR(rdev);
3379 else if (S_ISCHR(mode)) {
3380 data->arg.ftype = NF4CHR;
3381 data->arg.u.device.specdata1 = MAJOR(rdev);
3382 data->arg.u.device.specdata2 = MINOR(rdev);
3383 } else if (!S_ISSOCK(mode)) {
3388 status = nfs4_do_create(dir, dentry, data);
3390 nfs4_free_createdata(data);
3395 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3396 struct iattr *sattr, dev_t rdev)
3398 struct nfs4_exception exception = { };
3401 sattr->ia_mode &= ~current_umask();
3403 err = nfs4_handle_exception(NFS_SERVER(dir),
3404 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3406 } while (exception.retry);
3410 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3411 struct nfs_fsstat *fsstat)
3413 struct nfs4_statfs_arg args = {
3415 .bitmask = server->attr_bitmask,
3417 struct nfs4_statfs_res res = {
3420 struct rpc_message msg = {
3421 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3426 nfs_fattr_init(fsstat->fattr);
3427 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3430 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3432 struct nfs4_exception exception = { };
3435 err = nfs4_handle_exception(server,
3436 _nfs4_proc_statfs(server, fhandle, fsstat),
3438 } while (exception.retry);
3442 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3443 struct nfs_fsinfo *fsinfo)
3445 struct nfs4_fsinfo_arg args = {
3447 .bitmask = server->attr_bitmask,
3449 struct nfs4_fsinfo_res res = {
3452 struct rpc_message msg = {
3453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3458 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3461 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3463 struct nfs4_exception exception = { };
3467 err = nfs4_handle_exception(server,
3468 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3470 } while (exception.retry);
3474 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3478 nfs_fattr_init(fsinfo->fattr);
3479 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3481 /* block layout checks this! */
3482 server->pnfs_blksize = fsinfo->blksize;
3483 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3489 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3490 struct nfs_pathconf *pathconf)
3492 struct nfs4_pathconf_arg args = {
3494 .bitmask = server->attr_bitmask,
3496 struct nfs4_pathconf_res res = {
3497 .pathconf = pathconf,
3499 struct rpc_message msg = {
3500 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3505 /* None of the pathconf attributes are mandatory to implement */
3506 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3507 memset(pathconf, 0, sizeof(*pathconf));
3511 nfs_fattr_init(pathconf->fattr);
3512 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3515 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3516 struct nfs_pathconf *pathconf)
3518 struct nfs4_exception exception = { };
3522 err = nfs4_handle_exception(server,
3523 _nfs4_proc_pathconf(server, fhandle, pathconf),
3525 } while (exception.retry);
3529 void __nfs4_read_done_cb(struct nfs_read_data *data)
3531 nfs_invalidate_atime(data->header->inode);
3534 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3536 struct nfs_server *server = NFS_SERVER(data->header->inode);
3538 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3539 rpc_restart_call_prepare(task);
3543 __nfs4_read_done_cb(data);
3544 if (task->tk_status > 0)
3545 renew_lease(server, data->timestamp);
3549 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3552 dprintk("--> %s\n", __func__);
3554 if (!nfs4_sequence_done(task, &data->res.seq_res))
3557 return data->read_done_cb ? data->read_done_cb(task, data) :
3558 nfs4_read_done_cb(task, data);
3561 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3563 data->timestamp = jiffies;
3564 data->read_done_cb = nfs4_read_done_cb;
3565 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3566 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3569 static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3571 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3572 &data->args.seq_args,
3576 rpc_call_start(task);
3579 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3581 struct inode *inode = data->header->inode;
3583 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3584 rpc_restart_call_prepare(task);
3587 if (task->tk_status >= 0) {
3588 renew_lease(NFS_SERVER(inode), data->timestamp);
3589 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
3594 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3596 if (!nfs4_sequence_done(task, &data->res.seq_res))
3598 return data->write_done_cb ? data->write_done_cb(task, data) :
3599 nfs4_write_done_cb(task, data);
3603 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3605 const struct nfs_pgio_header *hdr = data->header;
3607 /* Don't request attributes for pNFS or O_DIRECT writes */
3608 if (data->ds_clp != NULL || hdr->dreq != NULL)
3610 /* Otherwise, request attributes if and only if we don't hold
3613 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
3616 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3618 struct nfs_server *server = NFS_SERVER(data->header->inode);
3620 if (!nfs4_write_need_cache_consistency_data(data)) {
3621 data->args.bitmask = NULL;
3622 data->res.fattr = NULL;
3624 data->args.bitmask = server->cache_consistency_bitmask;
3626 if (!data->write_done_cb)
3627 data->write_done_cb = nfs4_write_done_cb;
3628 data->res.server = server;
3629 data->timestamp = jiffies;
3631 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3632 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3635 static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3637 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3638 &data->args.seq_args,
3642 rpc_call_start(task);
3645 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3647 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3648 &data->args.seq_args,
3652 rpc_call_start(task);
3655 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
3657 struct inode *inode = data->inode;
3659 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3660 rpc_restart_call_prepare(task);
3666 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
3668 if (!nfs4_sequence_done(task, &data->res.seq_res))
3670 return data->commit_done_cb(task, data);
3673 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
3675 struct nfs_server *server = NFS_SERVER(data->inode);
3677 if (data->commit_done_cb == NULL)
3678 data->commit_done_cb = nfs4_commit_done_cb;
3679 data->res.server = server;
3680 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3681 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3684 struct nfs4_renewdata {
3685 struct nfs_client *client;
3686 unsigned long timestamp;
3690 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3691 * standalone procedure for queueing an asynchronous RENEW.
3693 static void nfs4_renew_release(void *calldata)
3695 struct nfs4_renewdata *data = calldata;
3696 struct nfs_client *clp = data->client;
3698 if (atomic_read(&clp->cl_count) > 1)
3699 nfs4_schedule_state_renewal(clp);
3700 nfs_put_client(clp);
3704 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3706 struct nfs4_renewdata *data = calldata;
3707 struct nfs_client *clp = data->client;
3708 unsigned long timestamp = data->timestamp;
3710 if (task->tk_status < 0) {
3711 /* Unless we're shutting down, schedule state recovery! */
3712 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3714 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3715 nfs4_schedule_lease_recovery(clp);
3718 nfs4_schedule_path_down_recovery(clp);
3720 do_renew_lease(clp, timestamp);
3723 static const struct rpc_call_ops nfs4_renew_ops = {
3724 .rpc_call_done = nfs4_renew_done,
3725 .rpc_release = nfs4_renew_release,
3728 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3730 struct rpc_message msg = {
3731 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3735 struct nfs4_renewdata *data;
3737 if (renew_flags == 0)
3739 if (!atomic_inc_not_zero(&clp->cl_count))
3741 data = kmalloc(sizeof(*data), GFP_NOFS);
3745 data->timestamp = jiffies;
3746 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3747 &nfs4_renew_ops, data);
3750 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3752 struct rpc_message msg = {
3753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3757 unsigned long now = jiffies;
3760 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3763 do_renew_lease(clp, now);
3767 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3769 return (server->caps & NFS_CAP_ACLS)
3770 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3771 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3774 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3775 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
3778 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
3780 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3781 struct page **pages, unsigned int *pgbase)
3783 struct page *newpage, **spages;
3789 len = min_t(size_t, PAGE_SIZE, buflen);
3790 newpage = alloc_page(GFP_KERNEL);
3792 if (newpage == NULL)
3794 memcpy(page_address(newpage), buf, len);
3799 } while (buflen != 0);
3805 __free_page(spages[rc-1]);
3809 struct nfs4_cached_acl {
3815 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3817 struct nfs_inode *nfsi = NFS_I(inode);
3819 spin_lock(&inode->i_lock);
3820 kfree(nfsi->nfs4_acl);
3821 nfsi->nfs4_acl = acl;
3822 spin_unlock(&inode->i_lock);
3825 static void nfs4_zap_acl_attr(struct inode *inode)
3827 nfs4_set_cached_acl(inode, NULL);
3830 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3832 struct nfs_inode *nfsi = NFS_I(inode);
3833 struct nfs4_cached_acl *acl;
3836 spin_lock(&inode->i_lock);
3837 acl = nfsi->nfs4_acl;
3840 if (buf == NULL) /* user is just asking for length */
3842 if (acl->cached == 0)
3844 ret = -ERANGE; /* see getxattr(2) man page */
3845 if (acl->len > buflen)
3847 memcpy(buf, acl->data, acl->len);
3851 spin_unlock(&inode->i_lock);
3855 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3857 struct nfs4_cached_acl *acl;
3858 size_t buflen = sizeof(*acl) + acl_len;
3860 if (buflen <= PAGE_SIZE) {
3861 acl = kmalloc(buflen, GFP_KERNEL);
3865 _copy_from_pages(acl->data, pages, pgbase, acl_len);
3867 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3874 nfs4_set_cached_acl(inode, acl);
3878 * The getxattr API returns the required buffer length when called with a
3879 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3880 * the required buf. On a NULL buf, we send a page of data to the server
3881 * guessing that the ACL request can be serviced by a page. If so, we cache
3882 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3883 * the cache. If not so, we throw away the page, and cache the required
3884 * length. The next getxattr call will then produce another round trip to
3885 * the server, this time with the input buf of the required size.
3887 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3889 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3890 struct nfs_getaclargs args = {
3891 .fh = NFS_FH(inode),
3895 struct nfs_getaclres res = {
3898 struct rpc_message msg = {
3899 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3903 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3904 int ret = -ENOMEM, i;
3906 /* As long as we're doing a round trip to the server anyway,
3907 * let's be prepared for a page of acl data. */
3910 if (npages > ARRAY_SIZE(pages))
3913 for (i = 0; i < npages; i++) {
3914 pages[i] = alloc_page(GFP_KERNEL);
3919 /* for decoding across pages */
3920 res.acl_scratch = alloc_page(GFP_KERNEL);
3921 if (!res.acl_scratch)
3924 args.acl_len = npages * PAGE_SIZE;
3925 args.acl_pgbase = 0;
3927 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3928 __func__, buf, buflen, npages, args.acl_len);
3929 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3930 &msg, &args.seq_args, &res.seq_res, 0);
3934 /* Handle the case where the passed-in buffer is too short */
3935 if (res.acl_flags & NFS4_ACL_TRUNC) {
3936 /* Did the user only issue a request for the acl length? */
3942 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3944 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3948 for (i = 0; i < npages; i++)
3950 __free_page(pages[i]);
3951 if (res.acl_scratch)
3952 __free_page(res.acl_scratch);
3956 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3958 struct nfs4_exception exception = { };
3961 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3964 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3965 } while (exception.retry);
3969 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3971 struct nfs_server *server = NFS_SERVER(inode);
3974 if (!nfs4_server_supports_acls(server))
3976 ret = nfs_revalidate_inode(server, inode);
3979 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3980 nfs_zap_acl_cache(inode);
3981 ret = nfs4_read_cached_acl(inode, buf, buflen);
3983 /* -ENOENT is returned if there is no ACL or if there is an ACL
3984 * but no cached acl data, just the acl length */
3986 return nfs4_get_acl_uncached(inode, buf, buflen);
3989 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3991 struct nfs_server *server = NFS_SERVER(inode);
3992 struct page *pages[NFS4ACL_MAXPAGES];
3993 struct nfs_setaclargs arg = {
3994 .fh = NFS_FH(inode),
3998 struct nfs_setaclres res;
3999 struct rpc_message msg = {
4000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4004 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4007 if (!nfs4_server_supports_acls(server))
4009 if (npages > ARRAY_SIZE(pages))
4011 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4014 nfs4_inode_return_delegation(inode);
4015 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4018 * Free each page after tx, so the only ref left is
4019 * held by the network stack
4022 put_page(pages[i-1]);
4025 * Acl update can result in inode attribute update.
4026 * so mark the attribute cache invalid.
4028 spin_lock(&inode->i_lock);
4029 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4030 spin_unlock(&inode->i_lock);
4031 nfs_access_zap_cache(inode);
4032 nfs_zap_acl_cache(inode);
4036 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4038 struct nfs4_exception exception = { };
4041 err = nfs4_handle_exception(NFS_SERVER(inode),
4042 __nfs4_proc_set_acl(inode, buf, buflen),
4044 } while (exception.retry);
4049 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
4051 struct nfs_client *clp = server->nfs_client;
4053 if (task->tk_status >= 0)
4055 switch(task->tk_status) {
4056 case -NFS4ERR_DELEG_REVOKED:
4057 case -NFS4ERR_ADMIN_REVOKED:
4058 case -NFS4ERR_BAD_STATEID:
4061 nfs_remove_bad_delegation(state->inode);
4062 case -NFS4ERR_OPENMODE:
4065 nfs4_schedule_stateid_recovery(server, state);
4066 goto wait_on_recovery;
4067 case -NFS4ERR_EXPIRED:
4069 nfs4_schedule_stateid_recovery(server, state);
4070 case -NFS4ERR_STALE_STATEID:
4071 case -NFS4ERR_STALE_CLIENTID:
4072 nfs4_schedule_lease_recovery(clp);
4073 goto wait_on_recovery;
4074 #if defined(CONFIG_NFS_V4_1)
4075 case -NFS4ERR_BADSESSION:
4076 case -NFS4ERR_BADSLOT:
4077 case -NFS4ERR_BAD_HIGH_SLOT:
4078 case -NFS4ERR_DEADSESSION:
4079 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4080 case -NFS4ERR_SEQ_FALSE_RETRY:
4081 case -NFS4ERR_SEQ_MISORDERED:
4082 dprintk("%s ERROR %d, Reset session\n", __func__,
4084 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4085 task->tk_status = 0;
4087 #endif /* CONFIG_NFS_V4_1 */
4088 case -NFS4ERR_DELAY:
4089 nfs_inc_server_stats(server, NFSIOS_DELAY);
4090 case -NFS4ERR_GRACE:
4092 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4093 task->tk_status = 0;
4095 case -NFS4ERR_RETRY_UNCACHED_REP:
4096 case -NFS4ERR_OLD_STATEID:
4097 task->tk_status = 0;
4100 task->tk_status = nfs4_map_errors(task->tk_status);
4103 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4104 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4105 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4106 task->tk_status = 0;
4110 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4111 nfs4_verifier *bootverf)
4115 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4116 /* An impossible timestamp guarantees this value
4117 * will never match a generated boot time. */
4119 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4121 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4122 verf[0] = (__be32)nn->boot_time.tv_sec;
4123 verf[1] = (__be32)nn->boot_time.tv_nsec;
4125 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4129 nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4130 char *buf, size_t len)
4132 unsigned int result;
4135 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4137 rpc_peeraddr2str(clp->cl_rpcclient,
4139 rpc_peeraddr2str(clp->cl_rpcclient,
4140 RPC_DISPLAY_PROTO));
4146 nfs4_init_uniform_client_string(const struct nfs_client *clp,
4147 char *buf, size_t len)
4149 char *nodename = clp->cl_rpcclient->cl_nodename;
4151 if (nfs4_client_id_uniquifier[0] != '\0')
4152 nodename = nfs4_client_id_uniquifier;
4153 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4154 clp->rpc_ops->version, clp->cl_minorversion,
4159 * nfs4_proc_setclientid - Negotiate client ID
4160 * @clp: state data structure
4161 * @program: RPC program for NFSv4 callback service
4162 * @port: IP port number for NFS4 callback service
4163 * @cred: RPC credential to use for this call
4164 * @res: where to place the result
4166 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4168 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4169 unsigned short port, struct rpc_cred *cred,
4170 struct nfs4_setclientid_res *res)
4172 nfs4_verifier sc_verifier;
4173 struct nfs4_setclientid setclientid = {
4174 .sc_verifier = &sc_verifier,
4176 .sc_cb_ident = clp->cl_cb_ident,
4178 struct rpc_message msg = {
4179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4180 .rpc_argp = &setclientid,
4186 /* nfs_client_id4 */
4187 nfs4_init_boot_verifier(clp, &sc_verifier);
4188 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4189 setclientid.sc_name_len =
4190 nfs4_init_uniform_client_string(clp,
4191 setclientid.sc_name,
4192 sizeof(setclientid.sc_name));
4194 setclientid.sc_name_len =
4195 nfs4_init_nonuniform_client_string(clp,
4196 setclientid.sc_name,
4197 sizeof(setclientid.sc_name));
4200 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4201 sizeof(setclientid.sc_netid),
4202 rpc_peeraddr2str(clp->cl_rpcclient,
4203 RPC_DISPLAY_NETID));
4205 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4206 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4207 clp->cl_ipaddr, port >> 8, port & 255);
4209 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4210 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4211 setclientid.sc_name_len, setclientid.sc_name);
4212 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4213 dprintk("NFS reply setclientid: %d\n", status);
4218 * nfs4_proc_setclientid_confirm - Confirm client ID
4219 * @clp: state data structure
4220 * @res: result of a previous SETCLIENTID
4221 * @cred: RPC credential to use for this call
4223 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4225 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4226 struct nfs4_setclientid_res *arg,
4227 struct rpc_cred *cred)
4229 struct nfs_fsinfo fsinfo;
4230 struct rpc_message msg = {
4231 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4233 .rpc_resp = &fsinfo,
4239 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4240 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4243 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4245 spin_lock(&clp->cl_lock);
4246 clp->cl_lease_time = fsinfo.lease_time * HZ;
4247 clp->cl_last_renewal = now;
4248 spin_unlock(&clp->cl_lock);
4250 dprintk("NFS reply setclientid_confirm: %d\n", status);
4254 struct nfs4_delegreturndata {
4255 struct nfs4_delegreturnargs args;
4256 struct nfs4_delegreturnres res;
4258 nfs4_stateid stateid;
4259 unsigned long timestamp;
4260 struct nfs_fattr fattr;
4264 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4266 struct nfs4_delegreturndata *data = calldata;
4268 if (!nfs4_sequence_done(task, &data->res.seq_res))
4271 switch (task->tk_status) {
4272 case -NFS4ERR_STALE_STATEID:
4273 case -NFS4ERR_EXPIRED:
4275 renew_lease(data->res.server, data->timestamp);
4278 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4280 rpc_restart_call_prepare(task);
4284 data->rpc_status = task->tk_status;
4287 static void nfs4_delegreturn_release(void *calldata)
4292 #if defined(CONFIG_NFS_V4_1)
4293 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4295 struct nfs4_delegreturndata *d_data;
4297 d_data = (struct nfs4_delegreturndata *)data;
4299 if (nfs4_setup_sequence(d_data->res.server,
4300 &d_data->args.seq_args,
4301 &d_data->res.seq_res, task))
4303 rpc_call_start(task);
4305 #endif /* CONFIG_NFS_V4_1 */
4307 static const struct rpc_call_ops nfs4_delegreturn_ops = {
4308 #if defined(CONFIG_NFS_V4_1)
4309 .rpc_call_prepare = nfs4_delegreturn_prepare,
4310 #endif /* CONFIG_NFS_V4_1 */
4311 .rpc_call_done = nfs4_delegreturn_done,
4312 .rpc_release = nfs4_delegreturn_release,
4315 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4317 struct nfs4_delegreturndata *data;
4318 struct nfs_server *server = NFS_SERVER(inode);
4319 struct rpc_task *task;
4320 struct rpc_message msg = {
4321 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4324 struct rpc_task_setup task_setup_data = {
4325 .rpc_client = server->client,
4326 .rpc_message = &msg,
4327 .callback_ops = &nfs4_delegreturn_ops,
4328 .flags = RPC_TASK_ASYNC,
4332 data = kzalloc(sizeof(*data), GFP_NOFS);
4335 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4336 data->args.fhandle = &data->fh;
4337 data->args.stateid = &data->stateid;
4338 data->args.bitmask = server->cache_consistency_bitmask;
4339 nfs_copy_fh(&data->fh, NFS_FH(inode));
4340 nfs4_stateid_copy(&data->stateid, stateid);
4341 data->res.fattr = &data->fattr;
4342 data->res.server = server;
4343 nfs_fattr_init(data->res.fattr);
4344 data->timestamp = jiffies;
4345 data->rpc_status = 0;
4347 task_setup_data.callback_data = data;
4348 msg.rpc_argp = &data->args;
4349 msg.rpc_resp = &data->res;
4350 task = rpc_run_task(&task_setup_data);
4352 return PTR_ERR(task);
4355 status = nfs4_wait_for_completion_rpc_task(task);
4358 status = data->rpc_status;
4360 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4362 nfs_refresh_inode(inode, &data->fattr);
4368 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4370 struct nfs_server *server = NFS_SERVER(inode);
4371 struct nfs4_exception exception = { };
4374 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4376 case -NFS4ERR_STALE_STATEID:
4377 case -NFS4ERR_EXPIRED:
4381 err = nfs4_handle_exception(server, err, &exception);
4382 } while (exception.retry);
4386 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4387 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4390 * sleep, with exponential backoff, and retry the LOCK operation.
4392 static unsigned long
4393 nfs4_set_lock_task_retry(unsigned long timeout)
4395 freezable_schedule_timeout_killable(timeout);
4397 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4398 return NFS4_LOCK_MAXTIMEOUT;
4402 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4404 struct inode *inode = state->inode;
4405 struct nfs_server *server = NFS_SERVER(inode);
4406 struct nfs_client *clp = server->nfs_client;
4407 struct nfs_lockt_args arg = {
4408 .fh = NFS_FH(inode),
4411 struct nfs_lockt_res res = {
4414 struct rpc_message msg = {
4415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4418 .rpc_cred = state->owner->so_cred,
4420 struct nfs4_lock_state *lsp;
4423 arg.lock_owner.clientid = clp->cl_clientid;
4424 status = nfs4_set_lock_state(state, request);
4427 lsp = request->fl_u.nfs4_fl.owner;
4428 arg.lock_owner.id = lsp->ls_seqid.owner_id;
4429 arg.lock_owner.s_dev = server->s_dev;
4430 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4433 request->fl_type = F_UNLCK;
4435 case -NFS4ERR_DENIED:
4438 request->fl_ops->fl_release_private(request);
4443 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4445 struct nfs4_exception exception = { };
4449 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4450 _nfs4_proc_getlk(state, cmd, request),
4452 } while (exception.retry);
4456 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4459 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4461 res = posix_lock_file_wait(file, fl);
4464 res = flock_lock_file_wait(file, fl);
4472 struct nfs4_unlockdata {
4473 struct nfs_locku_args arg;
4474 struct nfs_locku_res res;
4475 struct nfs4_lock_state *lsp;
4476 struct nfs_open_context *ctx;
4477 struct file_lock fl;
4478 const struct nfs_server *server;
4479 unsigned long timestamp;
4482 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4483 struct nfs_open_context *ctx,
4484 struct nfs4_lock_state *lsp,
4485 struct nfs_seqid *seqid)
4487 struct nfs4_unlockdata *p;
4488 struct inode *inode = lsp->ls_state->inode;
4490 p = kzalloc(sizeof(*p), GFP_NOFS);
4493 p->arg.fh = NFS_FH(inode);
4495 p->arg.seqid = seqid;
4496 p->res.seqid = seqid;
4497 p->arg.stateid = &lsp->ls_stateid;
4499 atomic_inc(&lsp->ls_count);
4500 /* Ensure we don't close file until we're done freeing locks! */
4501 p->ctx = get_nfs_open_context(ctx);
4502 memcpy(&p->fl, fl, sizeof(p->fl));
4503 p->server = NFS_SERVER(inode);
4507 static void nfs4_locku_release_calldata(void *data)
4509 struct nfs4_unlockdata *calldata = data;
4510 nfs_free_seqid(calldata->arg.seqid);
4511 nfs4_put_lock_state(calldata->lsp);
4512 put_nfs_open_context(calldata->ctx);
4516 static void nfs4_locku_done(struct rpc_task *task, void *data)
4518 struct nfs4_unlockdata *calldata = data;
4520 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4522 switch (task->tk_status) {
4524 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4525 &calldata->res.stateid);
4526 renew_lease(calldata->server, calldata->timestamp);
4528 case -NFS4ERR_BAD_STATEID:
4529 case -NFS4ERR_OLD_STATEID:
4530 case -NFS4ERR_STALE_STATEID:
4531 case -NFS4ERR_EXPIRED:
4534 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4535 rpc_restart_call_prepare(task);
4537 nfs_release_seqid(calldata->arg.seqid);
4540 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4542 struct nfs4_unlockdata *calldata = data;
4544 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4546 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
4547 /* Note: exit _without_ running nfs4_locku_done */
4548 task->tk_action = NULL;
4551 calldata->timestamp = jiffies;
4552 if (nfs4_setup_sequence(calldata->server,
4553 &calldata->arg.seq_args,
4554 &calldata->res.seq_res,
4556 nfs_release_seqid(calldata->arg.seqid);
4558 rpc_call_start(task);
4561 static const struct rpc_call_ops nfs4_locku_ops = {
4562 .rpc_call_prepare = nfs4_locku_prepare,
4563 .rpc_call_done = nfs4_locku_done,
4564 .rpc_release = nfs4_locku_release_calldata,
4567 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4568 struct nfs_open_context *ctx,
4569 struct nfs4_lock_state *lsp,
4570 struct nfs_seqid *seqid)
4572 struct nfs4_unlockdata *data;
4573 struct rpc_message msg = {
4574 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4575 .rpc_cred = ctx->cred,
4577 struct rpc_task_setup task_setup_data = {
4578 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4579 .rpc_message = &msg,
4580 .callback_ops = &nfs4_locku_ops,
4581 .workqueue = nfsiod_workqueue,
4582 .flags = RPC_TASK_ASYNC,
4585 /* Ensure this is an unlock - when canceling a lock, the
4586 * canceled lock is passed in, and it won't be an unlock.
4588 fl->fl_type = F_UNLCK;
4590 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4592 nfs_free_seqid(seqid);
4593 return ERR_PTR(-ENOMEM);
4596 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4597 msg.rpc_argp = &data->arg;
4598 msg.rpc_resp = &data->res;
4599 task_setup_data.callback_data = data;
4600 return rpc_run_task(&task_setup_data);
4603 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4605 struct nfs_inode *nfsi = NFS_I(state->inode);
4606 struct nfs_seqid *seqid;
4607 struct nfs4_lock_state *lsp;
4608 struct rpc_task *task;
4610 unsigned char fl_flags = request->fl_flags;
4612 status = nfs4_set_lock_state(state, request);
4613 /* Unlock _before_ we do the RPC call */
4614 request->fl_flags |= FL_EXISTS;
4615 down_read(&nfsi->rwsem);
4616 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4617 up_read(&nfsi->rwsem);
4620 up_read(&nfsi->rwsem);
4623 /* Is this a delegated lock? */
4624 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4626 lsp = request->fl_u.nfs4_fl.owner;
4627 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4631 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4632 status = PTR_ERR(task);
4635 status = nfs4_wait_for_completion_rpc_task(task);
4638 request->fl_flags = fl_flags;
4642 struct nfs4_lockdata {
4643 struct nfs_lock_args arg;
4644 struct nfs_lock_res res;
4645 struct nfs4_lock_state *lsp;
4646 struct nfs_open_context *ctx;
4647 struct file_lock fl;
4648 unsigned long timestamp;
4651 struct nfs_server *server;
4654 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4655 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4658 struct nfs4_lockdata *p;
4659 struct inode *inode = lsp->ls_state->inode;
4660 struct nfs_server *server = NFS_SERVER(inode);
4662 p = kzalloc(sizeof(*p), gfp_mask);
4666 p->arg.fh = NFS_FH(inode);
4668 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4669 if (p->arg.open_seqid == NULL)
4671 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4672 if (p->arg.lock_seqid == NULL)
4673 goto out_free_seqid;
4674 p->arg.lock_stateid = &lsp->ls_stateid;
4675 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4676 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4677 p->arg.lock_owner.s_dev = server->s_dev;
4678 p->res.lock_seqid = p->arg.lock_seqid;
4681 atomic_inc(&lsp->ls_count);
4682 p->ctx = get_nfs_open_context(ctx);
4683 memcpy(&p->fl, fl, sizeof(p->fl));
4686 nfs_free_seqid(p->arg.open_seqid);
4692 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4694 struct nfs4_lockdata *data = calldata;
4695 struct nfs4_state *state = data->lsp->ls_state;
4697 dprintk("%s: begin!\n", __func__);
4698 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4700 /* Do we need to do an open_to_lock_owner? */
4701 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4702 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4703 goto out_release_lock_seqid;
4704 data->arg.open_stateid = &state->stateid;
4705 data->arg.new_lock_owner = 1;
4706 data->res.open_seqid = data->arg.open_seqid;
4708 data->arg.new_lock_owner = 0;
4709 data->timestamp = jiffies;
4710 if (nfs4_setup_sequence(data->server,
4711 &data->arg.seq_args,
4714 rpc_call_start(task);
4717 nfs_release_seqid(data->arg.open_seqid);
4718 out_release_lock_seqid:
4719 nfs_release_seqid(data->arg.lock_seqid);
4720 dprintk("%s: done!, ret = %d\n", __func__, task->tk_status);
4723 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4725 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4726 nfs4_lock_prepare(task, calldata);
4729 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4731 struct nfs4_lockdata *data = calldata;
4733 dprintk("%s: begin!\n", __func__);
4735 if (!nfs4_sequence_done(task, &data->res.seq_res))
4738 data->rpc_status = task->tk_status;
4739 if (data->arg.new_lock_owner != 0) {
4740 if (data->rpc_status == 0)
4741 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4745 if (data->rpc_status == 0) {
4746 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4747 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
4748 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4751 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4754 static void nfs4_lock_release(void *calldata)
4756 struct nfs4_lockdata *data = calldata;
4758 dprintk("%s: begin!\n", __func__);
4759 nfs_free_seqid(data->arg.open_seqid);
4760 if (data->cancelled != 0) {
4761 struct rpc_task *task;
4762 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4763 data->arg.lock_seqid);
4765 rpc_put_task_async(task);
4766 dprintk("%s: cancelling lock!\n", __func__);
4768 nfs_free_seqid(data->arg.lock_seqid);
4769 nfs4_put_lock_state(data->lsp);
4770 put_nfs_open_context(data->ctx);
4772 dprintk("%s: done!\n", __func__);
4775 static const struct rpc_call_ops nfs4_lock_ops = {
4776 .rpc_call_prepare = nfs4_lock_prepare,
4777 .rpc_call_done = nfs4_lock_done,
4778 .rpc_release = nfs4_lock_release,
4781 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4782 .rpc_call_prepare = nfs4_recover_lock_prepare,
4783 .rpc_call_done = nfs4_lock_done,
4784 .rpc_release = nfs4_lock_release,
4787 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4790 case -NFS4ERR_ADMIN_REVOKED:
4791 case -NFS4ERR_BAD_STATEID:
4792 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4793 if (new_lock_owner != 0 ||
4794 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
4795 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4797 case -NFS4ERR_STALE_STATEID:
4798 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4799 case -NFS4ERR_EXPIRED:
4800 nfs4_schedule_lease_recovery(server->nfs_client);
4804 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4806 struct nfs4_lockdata *data;
4807 struct rpc_task *task;
4808 struct rpc_message msg = {
4809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4810 .rpc_cred = state->owner->so_cred,
4812 struct rpc_task_setup task_setup_data = {
4813 .rpc_client = NFS_CLIENT(state->inode),
4814 .rpc_message = &msg,
4815 .callback_ops = &nfs4_lock_ops,
4816 .workqueue = nfsiod_workqueue,
4817 .flags = RPC_TASK_ASYNC,
4821 dprintk("%s: begin!\n", __func__);
4822 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4823 fl->fl_u.nfs4_fl.owner,
4824 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4828 data->arg.block = 1;
4829 if (recovery_type > NFS_LOCK_NEW) {
4830 if (recovery_type == NFS_LOCK_RECLAIM)
4831 data->arg.reclaim = NFS_LOCK_RECLAIM;
4832 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4834 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4835 msg.rpc_argp = &data->arg;
4836 msg.rpc_resp = &data->res;
4837 task_setup_data.callback_data = data;
4838 task = rpc_run_task(&task_setup_data);
4840 return PTR_ERR(task);
4841 ret = nfs4_wait_for_completion_rpc_task(task);
4843 ret = data->rpc_status;
4845 nfs4_handle_setlk_error(data->server, data->lsp,
4846 data->arg.new_lock_owner, ret);
4848 data->cancelled = 1;
4850 dprintk("%s: done, ret = %d!\n", __func__, ret);
4854 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4856 struct nfs_server *server = NFS_SERVER(state->inode);
4857 struct nfs4_exception exception = {
4858 .inode = state->inode,
4863 /* Cache the lock if possible... */
4864 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4866 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4867 if (err != -NFS4ERR_DELAY)
4869 nfs4_handle_exception(server, err, &exception);
4870 } while (exception.retry);
4874 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4876 struct nfs_server *server = NFS_SERVER(state->inode);
4877 struct nfs4_exception exception = {
4878 .inode = state->inode,
4882 err = nfs4_set_lock_state(state, request);
4886 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4888 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4892 case -NFS4ERR_GRACE:
4893 case -NFS4ERR_DELAY:
4894 nfs4_handle_exception(server, err, &exception);
4897 } while (exception.retry);
4902 #if defined(CONFIG_NFS_V4_1)
4904 * nfs41_check_expired_locks - possibly free a lock stateid
4906 * @state: NFSv4 state for an inode
4908 * Returns NFS_OK if recovery for this stateid is now finished.
4909 * Otherwise a negative NFS4ERR value is returned.
4911 static int nfs41_check_expired_locks(struct nfs4_state *state)
4913 int status, ret = -NFS4ERR_BAD_STATEID;
4914 struct nfs4_lock_state *lsp;
4915 struct nfs_server *server = NFS_SERVER(state->inode);
4917 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4918 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
4919 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4920 if (status != NFS_OK) {
4921 /* Free the stateid unless the server
4922 * informs us the stateid is unrecognized. */
4923 if (status != -NFS4ERR_BAD_STATEID)
4924 nfs41_free_stateid(server,
4926 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
4935 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4937 int status = NFS_OK;
4939 if (test_bit(LK_STATE_IN_USE, &state->flags))
4940 status = nfs41_check_expired_locks(state);
4941 if (status != NFS_OK)
4942 status = nfs4_lock_expired(state, request);
4947 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4949 struct nfs_inode *nfsi = NFS_I(state->inode);
4950 unsigned char fl_flags = request->fl_flags;
4951 int status = -ENOLCK;
4953 if ((fl_flags & FL_POSIX) &&
4954 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4956 /* Is this a delegated open? */
4957 status = nfs4_set_lock_state(state, request);
4960 request->fl_flags |= FL_ACCESS;
4961 status = do_vfs_lock(request->fl_file, request);
4964 down_read(&nfsi->rwsem);
4965 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4966 /* Yes: cache locks! */
4967 /* ...but avoid races with delegation recall... */
4968 request->fl_flags = fl_flags & ~FL_SLEEP;
4969 status = do_vfs_lock(request->fl_file, request);
4972 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4975 /* Note: we always want to sleep here! */
4976 request->fl_flags = fl_flags | FL_SLEEP;
4977 if (do_vfs_lock(request->fl_file, request) < 0)
4978 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4979 "manager!\n", __func__);
4981 up_read(&nfsi->rwsem);
4983 request->fl_flags = fl_flags;
4987 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4989 struct nfs4_exception exception = {
4991 .inode = state->inode,
4996 err = _nfs4_proc_setlk(state, cmd, request);
4997 if (err == -NFS4ERR_DENIED)
4999 err = nfs4_handle_exception(NFS_SERVER(state->inode),
5001 } while (exception.retry);
5006 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5008 struct nfs_open_context *ctx;
5009 struct nfs4_state *state;
5010 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5013 /* verify open state */
5014 ctx = nfs_file_open_context(filp);
5017 if (request->fl_start < 0 || request->fl_end < 0)
5020 if (IS_GETLK(cmd)) {
5022 return nfs4_proc_getlk(state, F_GETLK, request);
5026 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5029 if (request->fl_type == F_UNLCK) {
5031 return nfs4_proc_unlck(state, cmd, request);
5038 * Don't rely on the VFS having checked the file open mode,
5039 * since it won't do this for flock() locks.
5041 switch (request->fl_type) {
5043 if (!(filp->f_mode & FMODE_READ))
5047 if (!(filp->f_mode & FMODE_WRITE))
5052 status = nfs4_proc_setlk(state, cmd, request);
5053 if ((status != -EAGAIN) || IS_SETLK(cmd))
5055 timeout = nfs4_set_lock_task_retry(timeout);
5056 status = -ERESTARTSYS;
5059 } while(status < 0);
5063 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
5065 struct nfs_server *server = NFS_SERVER(state->inode);
5066 struct nfs4_exception exception = { };
5069 err = nfs4_set_lock_state(state, fl);
5073 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
5076 printk(KERN_ERR "NFS: %s: unhandled error "
5077 "%d.\n", __func__, err);
5081 case -NFS4ERR_EXPIRED:
5082 nfs4_schedule_stateid_recovery(server, state);
5083 case -NFS4ERR_STALE_CLIENTID:
5084 case -NFS4ERR_STALE_STATEID:
5085 nfs4_schedule_lease_recovery(server->nfs_client);
5087 case -NFS4ERR_BADSESSION:
5088 case -NFS4ERR_BADSLOT:
5089 case -NFS4ERR_BAD_HIGH_SLOT:
5090 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5091 case -NFS4ERR_DEADSESSION:
5092 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
5096 * The show must go on: exit, but mark the
5097 * stateid as needing recovery.
5099 case -NFS4ERR_DELEG_REVOKED:
5100 case -NFS4ERR_ADMIN_REVOKED:
5101 case -NFS4ERR_BAD_STATEID:
5102 case -NFS4ERR_OPENMODE:
5103 nfs4_schedule_stateid_recovery(server, state);
5108 * User RPCSEC_GSS context has expired.
5109 * We cannot recover this stateid now, so
5110 * skip it and allow recovery thread to
5116 case -NFS4ERR_DENIED:
5117 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5120 case -NFS4ERR_DELAY:
5123 err = nfs4_handle_exception(server, err, &exception);
5124 } while (exception.retry);
5129 struct nfs_release_lockowner_data {
5130 struct nfs4_lock_state *lsp;
5131 struct nfs_server *server;
5132 struct nfs_release_lockowner_args args;
5135 static void nfs4_release_lockowner_release(void *calldata)
5137 struct nfs_release_lockowner_data *data = calldata;
5138 nfs4_free_lock_state(data->server, data->lsp);
5142 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5143 .rpc_release = nfs4_release_lockowner_release,
5146 int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
5148 struct nfs_server *server = lsp->ls_state->owner->so_server;
5149 struct nfs_release_lockowner_data *data;
5150 struct rpc_message msg = {
5151 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5154 if (server->nfs_client->cl_mvops->minor_version != 0)
5156 data = kmalloc(sizeof(*data), GFP_NOFS);
5160 data->server = server;
5161 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5162 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5163 data->args.lock_owner.s_dev = server->s_dev;
5164 msg.rpc_argp = &data->args;
5165 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5169 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5171 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5172 const void *buf, size_t buflen,
5173 int flags, int type)
5175 if (strcmp(key, "") != 0)
5178 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5181 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5182 void *buf, size_t buflen, int type)
5184 if (strcmp(key, "") != 0)
5187 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5190 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5191 size_t list_len, const char *name,
5192 size_t name_len, int type)
5194 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5196 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5199 if (list && len <= list_len)
5200 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5205 * nfs_fhget will use either the mounted_on_fileid or the fileid
5207 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5209 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5210 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5211 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
5212 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
5215 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
5216 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
5217 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5221 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5222 const struct qstr *name,
5223 struct nfs4_fs_locations *fs_locations,
5226 struct nfs_server *server = NFS_SERVER(dir);
5228 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
5230 struct nfs4_fs_locations_arg args = {
5231 .dir_fh = NFS_FH(dir),
5236 struct nfs4_fs_locations_res res = {
5237 .fs_locations = fs_locations,
5239 struct rpc_message msg = {
5240 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5246 dprintk("%s: start\n", __func__);
5248 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5249 * is not supported */
5250 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5251 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5253 bitmask[0] |= FATTR4_WORD0_FILEID;
5255 nfs_fattr_init(&fs_locations->fattr);
5256 fs_locations->server = server;
5257 fs_locations->nlocations = 0;
5258 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
5259 dprintk("%s: returned status = %d\n", __func__, status);
5263 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5264 const struct qstr *name,
5265 struct nfs4_fs_locations *fs_locations,
5268 struct nfs4_exception exception = { };
5271 err = nfs4_handle_exception(NFS_SERVER(dir),
5272 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
5274 } while (exception.retry);
5278 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5281 struct nfs4_secinfo_arg args = {
5282 .dir_fh = NFS_FH(dir),
5285 struct nfs4_secinfo_res res = {
5288 struct rpc_message msg = {
5289 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5294 dprintk("NFS call secinfo %s\n", name->name);
5295 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5296 dprintk("NFS reply secinfo: %d\n", status);
5300 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5301 struct nfs4_secinfo_flavors *flavors)
5303 struct nfs4_exception exception = { };
5306 err = nfs4_handle_exception(NFS_SERVER(dir),
5307 _nfs4_proc_secinfo(dir, name, flavors),
5309 } while (exception.retry);
5313 #ifdef CONFIG_NFS_V4_1
5315 * Check the exchange flags returned by the server for invalid flags, having
5316 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5319 static int nfs4_check_cl_exchange_flags(u32 flags)
5321 if (flags & ~EXCHGID4_FLAG_MASK_R)
5323 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5324 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5326 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5330 return -NFS4ERR_INVAL;
5334 nfs41_same_server_scope(struct nfs41_server_scope *a,
5335 struct nfs41_server_scope *b)
5337 if (a->server_scope_sz == b->server_scope_sz &&
5338 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5345 * nfs4_proc_bind_conn_to_session()
5347 * The 4.1 client currently uses the same TCP connection for the
5348 * fore and backchannel.
5350 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
5353 struct nfs41_bind_conn_to_session_res res;
5354 struct rpc_message msg = {
5356 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5362 dprintk("--> %s\n", __func__);
5364 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5365 if (unlikely(res.session == NULL)) {
5370 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5372 if (memcmp(res.session->sess_id.data,
5373 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5374 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5378 if (res.dir != NFS4_CDFS4_BOTH) {
5379 dprintk("NFS: %s: Unexpected direction from server\n",
5384 if (res.use_conn_in_rdma_mode) {
5385 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5394 dprintk("<-- %s status= %d\n", __func__, status);
5399 * nfs4_proc_exchange_id()
5401 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5403 * Since the clientid has expired, all compounds using sessions
5404 * associated with the stale clientid will be returning
5405 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5406 * be in some phase of session reset.
5408 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
5410 nfs4_verifier verifier;
5411 struct nfs41_exchange_id_args args = {
5412 .verifier = &verifier,
5414 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
5416 struct nfs41_exchange_id_res res = {
5420 struct rpc_message msg = {
5421 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5427 nfs4_init_boot_verifier(clp, &verifier);
5428 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5430 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5431 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5432 args.id_len, args.id);
5434 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
5436 if (unlikely(res.server_owner == NULL)) {
5441 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
5443 if (unlikely(res.server_scope == NULL)) {
5445 goto out_server_owner;
5448 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
5449 if (unlikely(res.impl_id == NULL)) {
5451 goto out_server_scope;
5454 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5456 status = nfs4_check_cl_exchange_flags(res.flags);
5459 clp->cl_clientid = res.clientid;
5460 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5461 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5462 clp->cl_seqid = res.seqid;
5464 kfree(clp->cl_serverowner);
5465 clp->cl_serverowner = res.server_owner;
5466 res.server_owner = NULL;
5468 /* use the most recent implementation id */
5469 kfree(clp->cl_implid);
5470 clp->cl_implid = res.impl_id;
5472 if (clp->cl_serverscope != NULL &&
5473 !nfs41_same_server_scope(clp->cl_serverscope,
5474 res.server_scope)) {
5475 dprintk("%s: server_scope mismatch detected\n",
5477 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5478 kfree(clp->cl_serverscope);
5479 clp->cl_serverscope = NULL;
5482 if (clp->cl_serverscope == NULL) {
5483 clp->cl_serverscope = res.server_scope;
5490 kfree(res.server_owner);
5492 kfree(res.server_scope);
5494 if (clp->cl_implid != NULL)
5495 dprintk("NFS reply exchange_id: Server Implementation ID: "
5496 "domain: %s, name: %s, date: %llu,%u\n",
5497 clp->cl_implid->domain, clp->cl_implid->name,
5498 clp->cl_implid->date.seconds,
5499 clp->cl_implid->date.nseconds);
5500 dprintk("NFS reply exchange_id: %d\n", status);
5504 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5505 struct rpc_cred *cred)
5507 struct rpc_message msg = {
5508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5514 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5516 dprintk("NFS: Got error %d from the server %s on "
5517 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5521 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5522 struct rpc_cred *cred)
5527 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5528 ret = _nfs4_proc_destroy_clientid(clp, cred);
5530 case -NFS4ERR_DELAY:
5531 case -NFS4ERR_CLIENTID_BUSY:
5541 int nfs4_destroy_clientid(struct nfs_client *clp)
5543 struct rpc_cred *cred;
5546 if (clp->cl_mvops->minor_version < 1)
5548 if (clp->cl_exchange_flags == 0)
5550 if (clp->cl_preserve_clid)
5552 cred = nfs4_get_exchange_id_cred(clp);
5553 ret = nfs4_proc_destroy_clientid(clp, cred);
5558 case -NFS4ERR_STALE_CLIENTID:
5559 clp->cl_exchange_flags = 0;
5565 struct nfs4_get_lease_time_data {
5566 struct nfs4_get_lease_time_args *args;
5567 struct nfs4_get_lease_time_res *res;
5568 struct nfs_client *clp;
5571 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5575 struct nfs4_get_lease_time_data *data =
5576 (struct nfs4_get_lease_time_data *)calldata;
5578 dprintk("--> %s\n", __func__);
5579 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5580 /* just setup sequence, do not trigger session recovery
5581 since we're invoked within one */
5582 ret = nfs41_setup_sequence(data->clp->cl_session,
5583 &data->args->la_seq_args,
5584 &data->res->lr_seq_res, task);
5587 rpc_call_start(task);
5588 dprintk("<-- %s\n", __func__);
5592 * Called from nfs4_state_manager thread for session setup, so don't recover
5593 * from sequence operation or clientid errors.
5595 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5597 struct nfs4_get_lease_time_data *data =
5598 (struct nfs4_get_lease_time_data *)calldata;
5600 dprintk("--> %s\n", __func__);
5601 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5603 switch (task->tk_status) {
5604 case -NFS4ERR_DELAY:
5605 case -NFS4ERR_GRACE:
5606 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5607 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5608 task->tk_status = 0;
5610 case -NFS4ERR_RETRY_UNCACHED_REP:
5611 rpc_restart_call_prepare(task);
5614 dprintk("<-- %s\n", __func__);
5617 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5618 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5619 .rpc_call_done = nfs4_get_lease_time_done,
5622 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5624 struct rpc_task *task;
5625 struct nfs4_get_lease_time_args args;
5626 struct nfs4_get_lease_time_res res = {
5627 .lr_fsinfo = fsinfo,
5629 struct nfs4_get_lease_time_data data = {
5634 struct rpc_message msg = {
5635 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5639 struct rpc_task_setup task_setup = {
5640 .rpc_client = clp->cl_rpcclient,
5641 .rpc_message = &msg,
5642 .callback_ops = &nfs4_get_lease_time_ops,
5643 .callback_data = &data,
5644 .flags = RPC_TASK_TIMEOUT,
5648 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5649 dprintk("--> %s\n", __func__);
5650 task = rpc_run_task(&task_setup);
5653 status = PTR_ERR(task);
5655 status = task->tk_status;
5658 dprintk("<-- %s return %d\n", __func__, status);
5663 struct nfs4_slot *nfs4_alloc_slots(struct nfs4_slot_table *table,
5664 u32 max_slots, gfp_t gfp_flags)
5666 struct nfs4_slot *tbl;
5669 tbl = kmalloc_array(max_slots, sizeof(*tbl), gfp_flags);
5671 for (i = 0; i < max_slots; i++) {
5672 tbl[i].table = table;
5679 static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5680 struct nfs4_slot *new,
5684 struct nfs4_slot *old = NULL;
5687 spin_lock(&tbl->slot_tbl_lock);
5691 tbl->max_slots = max_slots;
5693 tbl->highest_used_slotid = NFS4_NO_SLOT;
5694 for (i = 0; i < tbl->max_slots; i++)
5695 tbl->slots[i].seq_nr = ivalue;
5696 spin_unlock(&tbl->slot_tbl_lock);
5701 * (re)Initialise a slot table
5703 static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5706 struct nfs4_slot *new = NULL;
5709 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5710 max_reqs, tbl->max_slots);
5712 /* Does the newly negotiated max_reqs match the existing slot table? */
5713 if (max_reqs != tbl->max_slots) {
5714 new = nfs4_alloc_slots(tbl, max_reqs, GFP_NOFS);
5720 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
5721 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5722 tbl, tbl->slots, tbl->max_slots);
5724 dprintk("<-- %s: return %d\n", __func__, ret);
5728 /* Destroy the slot table */
5729 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5731 if (session->fc_slot_table.slots != NULL) {
5732 kfree(session->fc_slot_table.slots);
5733 session->fc_slot_table.slots = NULL;
5735 if (session->bc_slot_table.slots != NULL) {
5736 kfree(session->bc_slot_table.slots);
5737 session->bc_slot_table.slots = NULL;
5743 * Initialize or reset the forechannel and backchannel tables
5745 static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
5747 struct nfs4_slot_table *tbl;
5750 dprintk("--> %s\n", __func__);
5752 tbl = &ses->fc_slot_table;
5754 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5755 if (status) /* -ENOMEM */
5758 tbl = &ses->bc_slot_table;
5760 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5761 if (status && tbl->slots == NULL)
5762 /* Fore and back channel share a connection so get
5763 * both slot tables or neither */
5764 nfs4_destroy_slot_tables(ses);
5768 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5770 struct nfs4_session *session;
5771 struct nfs4_slot_table *tbl;
5773 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5777 tbl = &session->fc_slot_table;
5778 tbl->highest_used_slotid = NFS4_NO_SLOT;
5779 spin_lock_init(&tbl->slot_tbl_lock);
5780 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5781 init_completion(&tbl->complete);
5783 tbl = &session->bc_slot_table;
5784 tbl->highest_used_slotid = NFS4_NO_SLOT;
5785 spin_lock_init(&tbl->slot_tbl_lock);
5786 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5787 init_completion(&tbl->complete);
5789 session->session_state = 1<<NFS4_SESSION_INITING;
5795 void nfs4_destroy_session(struct nfs4_session *session)
5797 struct rpc_xprt *xprt;
5798 struct rpc_cred *cred;
5800 cred = nfs4_get_exchange_id_cred(session->clp);
5801 nfs4_proc_destroy_session(session, cred);
5806 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5808 dprintk("%s Destroy backchannel for xprt %p\n",
5810 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
5811 nfs4_destroy_slot_tables(session);
5816 * Initialize the values to be used by the client in CREATE_SESSION
5817 * If nfs4_init_session set the fore channel request and response sizes,
5820 * Set the back channel max_resp_sz_cached to zero to force the client to
5821 * always set csa_cachethis to FALSE because the current implementation
5822 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5824 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5826 struct nfs4_session *session = args->client->cl_session;
5827 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5828 mxresp_sz = session->fc_target_max_resp_sz;
5831 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5833 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5834 /* Fore channel attributes */
5835 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5836 args->fc_attrs.max_resp_sz = mxresp_sz;
5837 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5838 args->fc_attrs.max_reqs = max_session_slots;
5840 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5841 "max_ops=%u max_reqs=%u\n",
5843 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5844 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5846 /* Back channel attributes */
5847 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5848 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5849 args->bc_attrs.max_resp_sz_cached = 0;
5850 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5851 args->bc_attrs.max_reqs = 1;
5853 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5854 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5856 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5857 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5858 args->bc_attrs.max_reqs);
5861 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5863 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5864 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5866 if (rcvd->max_resp_sz > sent->max_resp_sz)
5869 * Our requested max_ops is the minimum we need; we're not
5870 * prepared to break up compounds into smaller pieces than that.
5871 * So, no point even trying to continue if the server won't
5874 if (rcvd->max_ops < sent->max_ops)
5876 if (rcvd->max_reqs == 0)
5878 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5879 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5883 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5885 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5886 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5888 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5890 if (rcvd->max_resp_sz < sent->max_resp_sz)
5892 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5894 /* These would render the backchannel useless: */
5895 if (rcvd->max_ops != sent->max_ops)
5897 if (rcvd->max_reqs != sent->max_reqs)
5902 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5903 struct nfs4_session *session)
5907 ret = nfs4_verify_fore_channel_attrs(args, session);
5910 return nfs4_verify_back_channel_attrs(args, session);
5913 static int _nfs4_proc_create_session(struct nfs_client *clp,
5914 struct rpc_cred *cred)
5916 struct nfs4_session *session = clp->cl_session;
5917 struct nfs41_create_session_args args = {
5919 .cb_program = NFS4_CALLBACK,
5921 struct nfs41_create_session_res res = {
5924 struct rpc_message msg = {
5925 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5932 nfs4_init_channel_attrs(&args);
5933 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5935 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5938 /* Verify the session's negotiated channel_attrs values */
5939 status = nfs4_verify_channel_attrs(&args, session);
5940 /* Increment the clientid slot sequence id */
5948 * Issues a CREATE_SESSION operation to the server.
5949 * It is the responsibility of the caller to verify the session is
5950 * expired before calling this routine.
5952 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
5956 struct nfs4_session *session = clp->cl_session;
5958 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5960 status = _nfs4_proc_create_session(clp, cred);
5964 /* Init or reset the session slot tables */
5965 status = nfs4_setup_session_slot_tables(session);
5966 dprintk("slot table setup returned %d\n", status);
5970 ptr = (unsigned *)&session->sess_id.data[0];
5971 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5972 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5974 dprintk("<-- %s\n", __func__);
5979 * Issue the over-the-wire RPC DESTROY_SESSION.
5980 * The caller must serialize access to this routine.
5982 int nfs4_proc_destroy_session(struct nfs4_session *session,
5983 struct rpc_cred *cred)
5985 struct rpc_message msg = {
5986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5987 .rpc_argp = session,
5992 dprintk("--> nfs4_proc_destroy_session\n");
5994 /* session is still being setup */
5995 if (session->clp->cl_cons_state != NFS_CS_READY)
5998 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6001 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
6002 "Session has been destroyed regardless...\n", status);
6004 dprintk("<-- nfs4_proc_destroy_session\n");
6009 * With sessions, the client is not marked ready until after a
6010 * successful EXCHANGE_ID and CREATE_SESSION.
6012 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
6013 * other versions of NFS can be tried.
6015 static int nfs41_check_session_ready(struct nfs_client *clp)
6019 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
6020 ret = nfs4_client_recover_expired_lease(clp);
6024 if (clp->cl_cons_state < NFS_CS_READY)
6025 return -EPROTONOSUPPORT;
6030 int nfs4_init_session(struct nfs_server *server)
6032 struct nfs_client *clp = server->nfs_client;
6033 struct nfs4_session *session;
6034 unsigned int target_max_rqst_sz = NFS_MAX_FILE_IO_SIZE;
6035 unsigned int target_max_resp_sz = NFS_MAX_FILE_IO_SIZE;
6037 if (!nfs4_has_session(clp))
6040 if (server->rsize != 0)
6041 target_max_resp_sz = server->rsize;
6042 target_max_resp_sz += nfs41_maxread_overhead;
6044 if (server->wsize != 0)
6045 target_max_rqst_sz = server->wsize;
6046 target_max_rqst_sz += nfs41_maxwrite_overhead;
6048 session = clp->cl_session;
6049 spin_lock(&clp->cl_lock);
6050 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6051 /* Initialise targets and channel attributes */
6052 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6053 session->fc_attrs.max_rqst_sz = target_max_rqst_sz;
6054 session->fc_target_max_resp_sz = target_max_resp_sz;
6055 session->fc_attrs.max_resp_sz = target_max_resp_sz;
6057 /* Just adjust the targets */
6058 if (target_max_rqst_sz > session->fc_target_max_rqst_sz) {
6059 session->fc_target_max_rqst_sz = target_max_rqst_sz;
6060 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6062 if (target_max_resp_sz > session->fc_target_max_resp_sz) {
6063 session->fc_target_max_resp_sz = target_max_resp_sz;
6064 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
6067 spin_unlock(&clp->cl_lock);
6069 if (test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
6070 nfs4_schedule_lease_recovery(clp);
6072 return nfs41_check_session_ready(clp);
6075 int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
6077 struct nfs4_session *session = clp->cl_session;
6080 spin_lock(&clp->cl_lock);
6081 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
6083 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
6084 * DS lease to be equal to the MDS lease.
6086 clp->cl_lease_time = lease_time;
6087 clp->cl_last_renewal = jiffies;
6089 spin_unlock(&clp->cl_lock);
6091 ret = nfs41_check_session_ready(clp);
6094 /* Test for the DS role */
6095 if (!is_ds_client(clp))
6099 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
6103 * Renew the cl_session lease.
6105 struct nfs4_sequence_data {
6106 struct nfs_client *clp;
6107 struct nfs4_sequence_args args;
6108 struct nfs4_sequence_res res;
6111 static void nfs41_sequence_release(void *data)
6113 struct nfs4_sequence_data *calldata = data;
6114 struct nfs_client *clp = calldata->clp;
6116 if (atomic_read(&clp->cl_count) > 1)
6117 nfs4_schedule_state_renewal(clp);
6118 nfs_put_client(clp);
6122 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6124 switch(task->tk_status) {
6125 case -NFS4ERR_DELAY:
6126 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6129 nfs4_schedule_lease_recovery(clp);
6134 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
6136 struct nfs4_sequence_data *calldata = data;
6137 struct nfs_client *clp = calldata->clp;
6139 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6142 if (task->tk_status < 0) {
6143 dprintk("%s ERROR %d\n", __func__, task->tk_status);
6144 if (atomic_read(&clp->cl_count) == 1)
6147 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6148 rpc_restart_call_prepare(task);
6152 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
6154 dprintk("<-- %s\n", __func__);
6157 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6159 struct nfs4_sequence_data *calldata = data;
6160 struct nfs_client *clp = calldata->clp;
6161 struct nfs4_sequence_args *args;
6162 struct nfs4_sequence_res *res;
6164 args = task->tk_msg.rpc_argp;
6165 res = task->tk_msg.rpc_resp;
6167 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
6169 rpc_call_start(task);
6172 static void nfs41_sequence_prepare_privileged(struct rpc_task *task, void *data)
6174 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6175 nfs41_sequence_prepare(task, data);
6178 static const struct rpc_call_ops nfs41_sequence_ops = {
6179 .rpc_call_done = nfs41_sequence_call_done,
6180 .rpc_call_prepare = nfs41_sequence_prepare,
6181 .rpc_release = nfs41_sequence_release,
6184 static const struct rpc_call_ops nfs41_sequence_privileged_ops = {
6185 .rpc_call_done = nfs41_sequence_call_done,
6186 .rpc_call_prepare = nfs41_sequence_prepare_privileged,
6187 .rpc_release = nfs41_sequence_release,
6190 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred,
6191 const struct rpc_call_ops *seq_ops)
6193 struct nfs4_sequence_data *calldata;
6194 struct rpc_message msg = {
6195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6198 struct rpc_task_setup task_setup_data = {
6199 .rpc_client = clp->cl_rpcclient,
6200 .rpc_message = &msg,
6201 .callback_ops = seq_ops,
6202 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
6205 if (!atomic_inc_not_zero(&clp->cl_count))
6206 return ERR_PTR(-EIO);
6207 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6208 if (calldata == NULL) {
6209 nfs_put_client(clp);
6210 return ERR_PTR(-ENOMEM);
6212 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
6213 msg.rpc_argp = &calldata->args;
6214 msg.rpc_resp = &calldata->res;
6215 calldata->clp = clp;
6216 task_setup_data.callback_data = calldata;
6218 return rpc_run_task(&task_setup_data);
6221 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
6223 struct rpc_task *task;
6226 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6228 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_ops);
6230 ret = PTR_ERR(task);
6232 rpc_put_task_async(task);
6233 dprintk("<-- %s status=%d\n", __func__, ret);
6237 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6239 struct rpc_task *task;
6242 task = _nfs41_proc_sequence(clp, cred, &nfs41_sequence_privileged_ops);
6244 ret = PTR_ERR(task);
6247 ret = rpc_wait_for_completion_task(task);
6249 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6251 if (task->tk_status == 0)
6252 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
6253 ret = task->tk_status;
6257 dprintk("<-- %s status=%d\n", __func__, ret);
6261 struct nfs4_reclaim_complete_data {
6262 struct nfs_client *clp;
6263 struct nfs41_reclaim_complete_args arg;
6264 struct nfs41_reclaim_complete_res res;
6267 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6269 struct nfs4_reclaim_complete_data *calldata = data;
6271 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
6272 if (nfs41_setup_sequence(calldata->clp->cl_session,
6273 &calldata->arg.seq_args,
6274 &calldata->res.seq_res, task))
6277 rpc_call_start(task);
6280 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6282 switch(task->tk_status) {
6284 case -NFS4ERR_COMPLETE_ALREADY:
6285 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6287 case -NFS4ERR_DELAY:
6288 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6290 case -NFS4ERR_RETRY_UNCACHED_REP:
6293 nfs4_schedule_lease_recovery(clp);
6298 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6300 struct nfs4_reclaim_complete_data *calldata = data;
6301 struct nfs_client *clp = calldata->clp;
6302 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6304 dprintk("--> %s\n", __func__);
6305 if (!nfs41_sequence_done(task, res))
6308 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6309 rpc_restart_call_prepare(task);
6312 dprintk("<-- %s\n", __func__);
6315 static void nfs4_free_reclaim_complete_data(void *data)
6317 struct nfs4_reclaim_complete_data *calldata = data;
6322 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6323 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6324 .rpc_call_done = nfs4_reclaim_complete_done,
6325 .rpc_release = nfs4_free_reclaim_complete_data,
6329 * Issue a global reclaim complete.
6331 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6333 struct nfs4_reclaim_complete_data *calldata;
6334 struct rpc_task *task;
6335 struct rpc_message msg = {
6336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6338 struct rpc_task_setup task_setup_data = {
6339 .rpc_client = clp->cl_rpcclient,
6340 .rpc_message = &msg,
6341 .callback_ops = &nfs4_reclaim_complete_call_ops,
6342 .flags = RPC_TASK_ASYNC,
6344 int status = -ENOMEM;
6346 dprintk("--> %s\n", __func__);
6347 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
6348 if (calldata == NULL)
6350 calldata->clp = clp;
6351 calldata->arg.one_fs = 0;
6353 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
6354 msg.rpc_argp = &calldata->arg;
6355 msg.rpc_resp = &calldata->res;
6356 task_setup_data.callback_data = calldata;
6357 task = rpc_run_task(&task_setup_data);
6359 status = PTR_ERR(task);
6362 status = nfs4_wait_for_completion_rpc_task(task);
6364 status = task->tk_status;
6368 dprintk("<-- %s status=%d\n", __func__, status);
6373 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6375 struct nfs4_layoutget *lgp = calldata;
6376 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6378 dprintk("--> %s\n", __func__);
6379 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6380 * right now covering the LAYOUTGET we are about to send.
6381 * However, that is not so catastrophic, and there seems
6382 * to be no way to prevent it completely.
6384 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
6385 &lgp->res.seq_res, task))
6387 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6388 NFS_I(lgp->args.inode)->layout,
6389 lgp->args.ctx->state)) {
6390 rpc_exit(task, NFS4_OK);
6393 rpc_call_start(task);
6396 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6398 struct nfs4_layoutget *lgp = calldata;
6399 struct inode *inode = lgp->args.inode;
6400 struct nfs_server *server = NFS_SERVER(inode);
6401 struct pnfs_layout_hdr *lo;
6402 struct nfs4_state *state = NULL;
6404 dprintk("--> %s\n", __func__);
6406 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6409 switch (task->tk_status) {
6412 case -NFS4ERR_LAYOUTTRYLATER:
6413 case -NFS4ERR_RECALLCONFLICT:
6414 task->tk_status = -NFS4ERR_DELAY;
6416 case -NFS4ERR_EXPIRED:
6417 case -NFS4ERR_BAD_STATEID:
6418 spin_lock(&inode->i_lock);
6419 lo = NFS_I(inode)->layout;
6420 if (!lo || list_empty(&lo->plh_segs)) {
6421 spin_unlock(&inode->i_lock);
6422 /* If the open stateid was bad, then recover it. */
6423 state = lgp->args.ctx->state;
6427 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6428 spin_unlock(&inode->i_lock);
6429 /* Mark the bad layout state as invalid, then
6430 * retry using the open stateid. */
6431 pnfs_free_lseg_list(&head);
6434 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6435 rpc_restart_call_prepare(task);
6437 dprintk("<-- %s\n", __func__);
6440 static size_t max_response_pages(struct nfs_server *server)
6442 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6443 return nfs_page_array_len(0, max_resp_sz);
6446 static void nfs4_free_pages(struct page **pages, size_t size)
6453 for (i = 0; i < size; i++) {
6456 __free_page(pages[i]);
6461 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6463 struct page **pages;
6466 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6468 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6472 for (i = 0; i < size; i++) {
6473 pages[i] = alloc_page(gfp_flags);
6475 dprintk("%s: failed to allocate page\n", __func__);
6476 nfs4_free_pages(pages, size);
6484 static void nfs4_layoutget_release(void *calldata)
6486 struct nfs4_layoutget *lgp = calldata;
6487 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6488 size_t max_pages = max_response_pages(server);
6490 dprintk("--> %s\n", __func__);
6491 nfs4_free_pages(lgp->args.layout.pages, max_pages);
6492 put_nfs_open_context(lgp->args.ctx);
6494 dprintk("<-- %s\n", __func__);
6497 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6498 .rpc_call_prepare = nfs4_layoutget_prepare,
6499 .rpc_call_done = nfs4_layoutget_done,
6500 .rpc_release = nfs4_layoutget_release,
6503 struct pnfs_layout_segment *
6504 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
6506 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6507 size_t max_pages = max_response_pages(server);
6508 struct rpc_task *task;
6509 struct rpc_message msg = {
6510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6511 .rpc_argp = &lgp->args,
6512 .rpc_resp = &lgp->res,
6514 struct rpc_task_setup task_setup_data = {
6515 .rpc_client = server->client,
6516 .rpc_message = &msg,
6517 .callback_ops = &nfs4_layoutget_call_ops,
6518 .callback_data = lgp,
6519 .flags = RPC_TASK_ASYNC,
6521 struct pnfs_layout_segment *lseg = NULL;
6524 dprintk("--> %s\n", __func__);
6526 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6527 if (!lgp->args.layout.pages) {
6528 nfs4_layoutget_release(lgp);
6529 return ERR_PTR(-ENOMEM);
6531 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6533 lgp->res.layoutp = &lgp->args.layout;
6534 lgp->res.seq_res.sr_slot = NULL;
6535 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
6536 task = rpc_run_task(&task_setup_data);
6538 return ERR_CAST(task);
6539 status = nfs4_wait_for_completion_rpc_task(task);
6541 status = task->tk_status;
6543 lseg = pnfs_layout_process(lgp);
6545 dprintk("<-- %s status=%d\n", __func__, status);
6547 return ERR_PTR(status);
6552 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6554 struct nfs4_layoutreturn *lrp = calldata;
6556 dprintk("--> %s\n", __func__);
6557 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
6558 &lrp->res.seq_res, task))
6560 rpc_call_start(task);
6563 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6565 struct nfs4_layoutreturn *lrp = calldata;
6566 struct nfs_server *server;
6568 dprintk("--> %s\n", __func__);
6570 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6573 server = NFS_SERVER(lrp->args.inode);
6574 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6575 rpc_restart_call_prepare(task);
6578 dprintk("<-- %s\n", __func__);
6581 static void nfs4_layoutreturn_release(void *calldata)
6583 struct nfs4_layoutreturn *lrp = calldata;
6584 struct pnfs_layout_hdr *lo = lrp->args.layout;
6586 dprintk("--> %s\n", __func__);
6587 spin_lock(&lo->plh_inode->i_lock);
6588 if (lrp->res.lrs_present)
6589 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6590 lo->plh_block_lgets--;
6591 spin_unlock(&lo->plh_inode->i_lock);
6592 pnfs_put_layout_hdr(lrp->args.layout);
6594 dprintk("<-- %s\n", __func__);
6597 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6598 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6599 .rpc_call_done = nfs4_layoutreturn_done,
6600 .rpc_release = nfs4_layoutreturn_release,
6603 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6605 struct rpc_task *task;
6606 struct rpc_message msg = {
6607 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6608 .rpc_argp = &lrp->args,
6609 .rpc_resp = &lrp->res,
6611 struct rpc_task_setup task_setup_data = {
6612 .rpc_client = lrp->clp->cl_rpcclient,
6613 .rpc_message = &msg,
6614 .callback_ops = &nfs4_layoutreturn_call_ops,
6615 .callback_data = lrp,
6619 dprintk("--> %s\n", __func__);
6620 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
6621 task = rpc_run_task(&task_setup_data);
6623 return PTR_ERR(task);
6624 status = task->tk_status;
6625 dprintk("<-- %s status=%d\n", __func__, status);
6631 * Retrieve the list of Data Server devices from the MDS.
6633 static int _nfs4_getdevicelist(struct nfs_server *server,
6634 const struct nfs_fh *fh,
6635 struct pnfs_devicelist *devlist)
6637 struct nfs4_getdevicelist_args args = {
6639 .layoutclass = server->pnfs_curr_ld->id,
6641 struct nfs4_getdevicelist_res res = {
6644 struct rpc_message msg = {
6645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6651 dprintk("--> %s\n", __func__);
6652 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6654 dprintk("<-- %s status=%d\n", __func__, status);
6658 int nfs4_proc_getdevicelist(struct nfs_server *server,
6659 const struct nfs_fh *fh,
6660 struct pnfs_devicelist *devlist)
6662 struct nfs4_exception exception = { };
6666 err = nfs4_handle_exception(server,
6667 _nfs4_getdevicelist(server, fh, devlist),
6669 } while (exception.retry);
6671 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6672 err, devlist->num_devs);
6676 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6679 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6681 struct nfs4_getdeviceinfo_args args = {
6684 struct nfs4_getdeviceinfo_res res = {
6687 struct rpc_message msg = {
6688 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6694 dprintk("--> %s\n", __func__);
6695 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6696 dprintk("<-- %s status=%d\n", __func__, status);
6701 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6703 struct nfs4_exception exception = { };
6707 err = nfs4_handle_exception(server,
6708 _nfs4_proc_getdeviceinfo(server, pdev),
6710 } while (exception.retry);
6713 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6715 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6717 struct nfs4_layoutcommit_data *data = calldata;
6718 struct nfs_server *server = NFS_SERVER(data->args.inode);
6720 if (nfs4_setup_sequence(server, &data->args.seq_args,
6721 &data->res.seq_res, task))
6723 rpc_call_start(task);
6727 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6729 struct nfs4_layoutcommit_data *data = calldata;
6730 struct nfs_server *server = NFS_SERVER(data->args.inode);
6732 if (!nfs4_sequence_done(task, &data->res.seq_res))
6735 switch (task->tk_status) { /* Just ignore these failures */
6736 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6737 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6738 case -NFS4ERR_BADLAYOUT: /* no layout */
6739 case -NFS4ERR_GRACE: /* loca_recalim always false */
6740 task->tk_status = 0;
6743 nfs_post_op_update_inode_force_wcc(data->args.inode,
6747 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6748 rpc_restart_call_prepare(task);
6754 static void nfs4_layoutcommit_release(void *calldata)
6756 struct nfs4_layoutcommit_data *data = calldata;
6757 struct pnfs_layout_segment *lseg, *tmp;
6758 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
6760 pnfs_cleanup_layoutcommit(data);
6761 /* Matched by references in pnfs_set_layoutcommit */
6762 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6763 list_del_init(&lseg->pls_lc_list);
6764 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6766 pnfs_put_lseg(lseg);
6769 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6770 smp_mb__after_clear_bit();
6771 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6773 put_rpccred(data->cred);
6777 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6778 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6779 .rpc_call_done = nfs4_layoutcommit_done,
6780 .rpc_release = nfs4_layoutcommit_release,
6784 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6786 struct rpc_message msg = {
6787 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6788 .rpc_argp = &data->args,
6789 .rpc_resp = &data->res,
6790 .rpc_cred = data->cred,
6792 struct rpc_task_setup task_setup_data = {
6793 .task = &data->task,
6794 .rpc_client = NFS_CLIENT(data->args.inode),
6795 .rpc_message = &msg,
6796 .callback_ops = &nfs4_layoutcommit_ops,
6797 .callback_data = data,
6798 .flags = RPC_TASK_ASYNC,
6800 struct rpc_task *task;
6803 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6804 "lbw: %llu inode %lu\n",
6805 data->task.tk_pid, sync,
6806 data->args.lastbytewritten,
6807 data->args.inode->i_ino);
6809 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6810 task = rpc_run_task(&task_setup_data);
6812 return PTR_ERR(task);
6815 status = nfs4_wait_for_completion_rpc_task(task);
6818 status = task->tk_status;
6820 dprintk("%s: status %d\n", __func__, status);
6826 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6827 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6829 struct nfs41_secinfo_no_name_args args = {
6830 .style = SECINFO_STYLE_CURRENT_FH,
6832 struct nfs4_secinfo_res res = {
6835 struct rpc_message msg = {
6836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6840 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6844 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6845 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6847 struct nfs4_exception exception = { };
6850 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6853 case -NFS4ERR_WRONGSEC:
6854 case -NFS4ERR_NOTSUPP:
6857 err = nfs4_handle_exception(server, err, &exception);
6859 } while (exception.retry);
6865 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6866 struct nfs_fsinfo *info)
6870 rpc_authflavor_t flavor;
6871 struct nfs4_secinfo_flavors *flavors;
6873 page = alloc_page(GFP_KERNEL);
6879 flavors = page_address(page);
6880 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6883 * Fall back on "guess and check" method if
6884 * the server doesn't support SECINFO_NO_NAME
6886 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6887 err = nfs4_find_root_sec(server, fhandle, info);
6893 flavor = nfs_find_best_sec(flavors);
6895 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6905 static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6908 struct nfs41_test_stateid_args args = {
6911 struct nfs41_test_stateid_res res;
6912 struct rpc_message msg = {
6913 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6918 dprintk("NFS call test_stateid %p\n", stateid);
6919 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6920 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6921 if (status != NFS_OK) {
6922 dprintk("NFS reply test_stateid: failed, %d\n", status);
6925 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
6930 * nfs41_test_stateid - perform a TEST_STATEID operation
6932 * @server: server / transport on which to perform the operation
6933 * @stateid: state ID to test
6935 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6936 * Otherwise a negative NFS4ERR value is returned if the operation
6937 * failed or the state ID is not currently valid.
6939 static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6941 struct nfs4_exception exception = { };
6944 err = _nfs41_test_stateid(server, stateid);
6945 if (err != -NFS4ERR_DELAY)
6947 nfs4_handle_exception(server, err, &exception);
6948 } while (exception.retry);
6952 static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6954 struct nfs41_free_stateid_args args = {
6957 struct nfs41_free_stateid_res res;
6958 struct rpc_message msg = {
6959 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6965 dprintk("NFS call free_stateid %p\n", stateid);
6966 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6967 status = nfs4_call_sync_sequence(server->client, server, &msg,
6968 &args.seq_args, &res.seq_res, 1);
6969 dprintk("NFS reply free_stateid: %d\n", status);
6974 * nfs41_free_stateid - perform a FREE_STATEID operation
6976 * @server: server / transport on which to perform the operation
6977 * @stateid: state ID to release
6979 * Returns NFS_OK if the server freed "stateid". Otherwise a
6980 * negative NFS4ERR value is returned.
6982 static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6984 struct nfs4_exception exception = { };
6987 err = _nfs4_free_stateid(server, stateid);
6988 if (err != -NFS4ERR_DELAY)
6990 nfs4_handle_exception(server, err, &exception);
6991 } while (exception.retry);
6995 static bool nfs41_match_stateid(const nfs4_stateid *s1,
6996 const nfs4_stateid *s2)
6998 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
7001 if (s1->seqid == s2->seqid)
7003 if (s1->seqid == 0 || s2->seqid == 0)
7009 #endif /* CONFIG_NFS_V4_1 */
7011 static bool nfs4_match_stateid(const nfs4_stateid *s1,
7012 const nfs4_stateid *s2)
7014 return nfs4_stateid_match(s1, s2);
7018 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
7019 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7020 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7021 .recover_open = nfs4_open_reclaim,
7022 .recover_lock = nfs4_lock_reclaim,
7023 .establish_clid = nfs4_init_clientid,
7024 .get_clid_cred = nfs4_get_setclientid_cred,
7025 .detect_trunking = nfs40_discover_server_trunking,
7028 #if defined(CONFIG_NFS_V4_1)
7029 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
7030 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7031 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7032 .recover_open = nfs4_open_reclaim,
7033 .recover_lock = nfs4_lock_reclaim,
7034 .establish_clid = nfs41_init_clientid,
7035 .get_clid_cred = nfs4_get_exchange_id_cred,
7036 .reclaim_complete = nfs41_proc_reclaim_complete,
7037 .detect_trunking = nfs41_discover_server_trunking,
7039 #endif /* CONFIG_NFS_V4_1 */
7041 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
7042 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7043 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
7044 .recover_open = nfs4_open_expired,
7045 .recover_lock = nfs4_lock_expired,
7046 .establish_clid = nfs4_init_clientid,
7047 .get_clid_cred = nfs4_get_setclientid_cred,
7050 #if defined(CONFIG_NFS_V4_1)
7051 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
7052 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7053 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
7054 .recover_open = nfs41_open_expired,
7055 .recover_lock = nfs41_lock_expired,
7056 .establish_clid = nfs41_init_clientid,
7057 .get_clid_cred = nfs4_get_exchange_id_cred,
7059 #endif /* CONFIG_NFS_V4_1 */
7061 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
7062 .sched_state_renewal = nfs4_proc_async_renew,
7063 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
7064 .renew_lease = nfs4_proc_renew,
7067 #if defined(CONFIG_NFS_V4_1)
7068 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
7069 .sched_state_renewal = nfs41_proc_async_sequence,
7070 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
7071 .renew_lease = nfs4_proc_sequence,
7075 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7077 .call_sync = _nfs4_call_sync,
7078 .match_stateid = nfs4_match_stateid,
7079 .find_root_sec = nfs4_find_root_sec,
7080 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7081 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7082 .state_renewal_ops = &nfs40_state_renewal_ops,
7085 #if defined(CONFIG_NFS_V4_1)
7086 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7088 .call_sync = _nfs4_call_sync_session,
7089 .match_stateid = nfs41_match_stateid,
7090 .find_root_sec = nfs41_find_root_sec,
7091 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7092 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7093 .state_renewal_ops = &nfs41_state_renewal_ops,
7097 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7098 [0] = &nfs_v4_0_minor_ops,
7099 #if defined(CONFIG_NFS_V4_1)
7100 [1] = &nfs_v4_1_minor_ops,
7104 const struct inode_operations nfs4_dir_inode_operations = {
7105 .create = nfs_create,
7106 .lookup = nfs_lookup,
7107 .atomic_open = nfs_atomic_open,
7109 .unlink = nfs_unlink,
7110 .symlink = nfs_symlink,
7114 .rename = nfs_rename,
7115 .permission = nfs_permission,
7116 .getattr = nfs_getattr,
7117 .setattr = nfs_setattr,
7118 .getxattr = generic_getxattr,
7119 .setxattr = generic_setxattr,
7120 .listxattr = generic_listxattr,
7121 .removexattr = generic_removexattr,
7124 static const struct inode_operations nfs4_file_inode_operations = {
7125 .permission = nfs_permission,
7126 .getattr = nfs_getattr,
7127 .setattr = nfs_setattr,
7128 .getxattr = generic_getxattr,
7129 .setxattr = generic_setxattr,
7130 .listxattr = generic_listxattr,
7131 .removexattr = generic_removexattr,
7134 const struct nfs_rpc_ops nfs_v4_clientops = {
7135 .version = 4, /* protocol version */
7136 .dentry_ops = &nfs4_dentry_operations,
7137 .dir_inode_ops = &nfs4_dir_inode_operations,
7138 .file_inode_ops = &nfs4_file_inode_operations,
7139 .file_ops = &nfs4_file_operations,
7140 .getroot = nfs4_proc_get_root,
7141 .submount = nfs4_submount,
7142 .try_mount = nfs4_try_mount,
7143 .getattr = nfs4_proc_getattr,
7144 .setattr = nfs4_proc_setattr,
7145 .lookup = nfs4_proc_lookup,
7146 .access = nfs4_proc_access,
7147 .readlink = nfs4_proc_readlink,
7148 .create = nfs4_proc_create,
7149 .remove = nfs4_proc_remove,
7150 .unlink_setup = nfs4_proc_unlink_setup,
7151 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
7152 .unlink_done = nfs4_proc_unlink_done,
7153 .rename = nfs4_proc_rename,
7154 .rename_setup = nfs4_proc_rename_setup,
7155 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
7156 .rename_done = nfs4_proc_rename_done,
7157 .link = nfs4_proc_link,
7158 .symlink = nfs4_proc_symlink,
7159 .mkdir = nfs4_proc_mkdir,
7160 .rmdir = nfs4_proc_remove,
7161 .readdir = nfs4_proc_readdir,
7162 .mknod = nfs4_proc_mknod,
7163 .statfs = nfs4_proc_statfs,
7164 .fsinfo = nfs4_proc_fsinfo,
7165 .pathconf = nfs4_proc_pathconf,
7166 .set_capabilities = nfs4_server_capabilities,
7167 .decode_dirent = nfs4_decode_dirent,
7168 .read_setup = nfs4_proc_read_setup,
7169 .read_pageio_init = pnfs_pageio_init_read,
7170 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
7171 .read_done = nfs4_read_done,
7172 .write_setup = nfs4_proc_write_setup,
7173 .write_pageio_init = pnfs_pageio_init_write,
7174 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
7175 .write_done = nfs4_write_done,
7176 .commit_setup = nfs4_proc_commit_setup,
7177 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
7178 .commit_done = nfs4_commit_done,
7179 .lock = nfs4_proc_lock,
7180 .clear_acl_cache = nfs4_zap_acl_attr,
7181 .close_context = nfs4_close_context,
7182 .open_context = nfs4_atomic_open,
7183 .have_delegation = nfs4_have_delegation,
7184 .return_delegation = nfs4_inode_return_delegation,
7185 .alloc_client = nfs4_alloc_client,
7186 .init_client = nfs4_init_client,
7187 .free_client = nfs4_free_client,
7188 .create_server = nfs4_create_server,
7189 .clone_server = nfs_clone_server,
7192 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7193 .prefix = XATTR_NAME_NFSV4_ACL,
7194 .list = nfs4_xattr_list_nfs4_acl,
7195 .get = nfs4_xattr_get_nfs4_acl,
7196 .set = nfs4_xattr_set_nfs4_acl,
7199 const struct xattr_handler *nfs4_xattr_handlers[] = {
7200 &nfs4_xattr_nfs4_acl_handler,