4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/sunrpc/gss_api.h>
47 #include <linux/nfs.h>
48 #include <linux/nfs4.h>
49 #include <linux/nfs_fs.h>
50 #include <linux/nfs_page.h>
51 #include <linux/nfs_mount.h>
52 #include <linux/namei.h>
53 #include <linux/mount.h>
54 #include <linux/module.h>
55 #include <linux/sunrpc/bc_xprt.h>
56 #include <linux/xattr.h>
57 #include <linux/utsname.h>
60 #include "delegation.h"
66 #define NFSDBG_FACILITY NFSDBG_PROC
68 #define NFS4_POLL_RETRY_MIN (HZ/10)
69 #define NFS4_POLL_RETRY_MAX (15*HZ)
71 #define NFS4_MAX_LOOP_ON_RECOVER (10)
74 static int _nfs4_proc_open(struct nfs4_opendata *data);
75 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
76 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
77 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
78 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
79 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
80 struct nfs_fattr *fattr, struct iattr *sattr,
81 struct nfs4_state *state);
82 #ifdef CONFIG_NFS_V4_1
83 static int nfs41_test_stateid(struct nfs_server *, struct nfs4_state *);
84 static int nfs41_free_stateid(struct nfs_server *, struct nfs4_state *);
86 /* Prevent leaks of NFSv4 errors into userland */
87 static int nfs4_map_errors(int err)
92 case -NFS4ERR_RESOURCE:
94 case -NFS4ERR_WRONGSEC:
96 case -NFS4ERR_BADOWNER:
97 case -NFS4ERR_BADNAME:
99 case -NFS4ERR_SHARE_DENIED:
102 dprintk("%s could not handle NFSv4 error %d\n",
110 * This is our standard bitmap for GETATTR requests.
112 const u32 nfs4_fattr_bitmap[2] = {
114 | FATTR4_WORD0_CHANGE
117 | FATTR4_WORD0_FILEID,
119 | FATTR4_WORD1_NUMLINKS
121 | FATTR4_WORD1_OWNER_GROUP
122 | FATTR4_WORD1_RAWDEV
123 | FATTR4_WORD1_SPACE_USED
124 | FATTR4_WORD1_TIME_ACCESS
125 | FATTR4_WORD1_TIME_METADATA
126 | FATTR4_WORD1_TIME_MODIFY
129 const u32 nfs4_statfs_bitmap[2] = {
130 FATTR4_WORD0_FILES_AVAIL
131 | FATTR4_WORD0_FILES_FREE
132 | FATTR4_WORD0_FILES_TOTAL,
133 FATTR4_WORD1_SPACE_AVAIL
134 | FATTR4_WORD1_SPACE_FREE
135 | FATTR4_WORD1_SPACE_TOTAL
138 const u32 nfs4_pathconf_bitmap[2] = {
140 | FATTR4_WORD0_MAXNAME,
144 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
145 | FATTR4_WORD0_MAXREAD
146 | FATTR4_WORD0_MAXWRITE
147 | FATTR4_WORD0_LEASE_TIME,
148 FATTR4_WORD1_TIME_DELTA
149 | FATTR4_WORD1_FS_LAYOUT_TYPES,
150 FATTR4_WORD2_LAYOUT_BLKSIZE
153 const u32 nfs4_fs_locations_bitmap[2] = {
155 | FATTR4_WORD0_CHANGE
158 | FATTR4_WORD0_FILEID
159 | FATTR4_WORD0_FS_LOCATIONS,
161 | FATTR4_WORD1_NUMLINKS
163 | FATTR4_WORD1_OWNER_GROUP
164 | FATTR4_WORD1_RAWDEV
165 | FATTR4_WORD1_SPACE_USED
166 | FATTR4_WORD1_TIME_ACCESS
167 | FATTR4_WORD1_TIME_METADATA
168 | FATTR4_WORD1_TIME_MODIFY
169 | FATTR4_WORD1_MOUNTED_ON_FILEID
172 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
173 struct nfs4_readdir_arg *readdir)
177 BUG_ON(readdir->count < 80);
179 readdir->cookie = cookie;
180 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
185 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
190 * NFSv4 servers do not return entries for '.' and '..'
191 * Therefore, we fake these entries here. We let '.'
192 * have cookie 0 and '..' have cookie 1. Note that
193 * when talking to the server, we always send cookie 0
196 start = p = kmap_atomic(*readdir->pages, KM_USER0);
199 *p++ = xdr_one; /* next */
200 *p++ = xdr_zero; /* cookie, first word */
201 *p++ = xdr_one; /* cookie, second word */
202 *p++ = xdr_one; /* entry len */
203 memcpy(p, ".\0\0\0", 4); /* entry */
205 *p++ = xdr_one; /* bitmap length */
206 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
207 *p++ = htonl(8); /* attribute buffer length */
208 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
211 *p++ = xdr_one; /* next */
212 *p++ = xdr_zero; /* cookie, first word */
213 *p++ = xdr_two; /* cookie, second word */
214 *p++ = xdr_two; /* entry len */
215 memcpy(p, "..\0\0", 4); /* entry */
217 *p++ = xdr_one; /* bitmap length */
218 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
219 *p++ = htonl(8); /* attribute buffer length */
220 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
222 readdir->pgbase = (char *)p - (char *)start;
223 readdir->count -= readdir->pgbase;
224 kunmap_atomic(start, KM_USER0);
227 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
233 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
234 nfs_wait_bit_killable, TASK_KILLABLE);
238 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
245 *timeout = NFS4_POLL_RETRY_MIN;
246 if (*timeout > NFS4_POLL_RETRY_MAX)
247 *timeout = NFS4_POLL_RETRY_MAX;
248 schedule_timeout_killable(*timeout);
249 if (fatal_signal_pending(current))
255 /* This is the error handling routine for processes that are allowed
258 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
260 struct nfs_client *clp = server->nfs_client;
261 struct nfs4_state *state = exception->state;
262 struct inode *inode = exception->inode;
265 exception->retry = 0;
269 case -NFS4ERR_OPENMODE:
270 if (nfs_have_delegation(inode, FMODE_READ)) {
271 nfs_inode_return_delegation(inode);
272 exception->retry = 1;
277 nfs4_schedule_stateid_recovery(server, state);
278 goto wait_on_recovery;
279 case -NFS4ERR_DELEG_REVOKED:
280 case -NFS4ERR_ADMIN_REVOKED:
281 case -NFS4ERR_BAD_STATEID:
283 nfs_remove_bad_delegation(state->inode);
286 nfs4_schedule_stateid_recovery(server, state);
287 goto wait_on_recovery;
288 case -NFS4ERR_EXPIRED:
290 nfs4_schedule_stateid_recovery(server, state);
291 case -NFS4ERR_STALE_STATEID:
292 case -NFS4ERR_STALE_CLIENTID:
293 nfs4_schedule_lease_recovery(clp);
294 goto wait_on_recovery;
295 #if defined(CONFIG_NFS_V4_1)
296 case -NFS4ERR_BADSESSION:
297 case -NFS4ERR_BADSLOT:
298 case -NFS4ERR_BAD_HIGH_SLOT:
299 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
300 case -NFS4ERR_DEADSESSION:
301 case -NFS4ERR_SEQ_FALSE_RETRY:
302 case -NFS4ERR_SEQ_MISORDERED:
303 dprintk("%s ERROR: %d Reset session\n", __func__,
305 nfs4_schedule_session_recovery(clp->cl_session);
306 goto wait_on_recovery;
307 #endif /* defined(CONFIG_NFS_V4_1) */
308 case -NFS4ERR_FILE_OPEN:
309 if (exception->timeout > HZ) {
310 /* We have retried a decent amount, time to
319 ret = nfs4_delay(server->client, &exception->timeout);
322 case -NFS4ERR_RETRY_UNCACHED_REP:
323 case -NFS4ERR_OLD_STATEID:
324 exception->retry = 1;
326 case -NFS4ERR_BADOWNER:
327 /* The following works around a Linux server bug! */
328 case -NFS4ERR_BADNAME:
329 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
330 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
331 exception->retry = 1;
332 printk(KERN_WARNING "NFS: v4 server %s "
333 "does not accept raw "
335 "Reenabling the idmapper.\n",
336 server->nfs_client->cl_hostname);
339 /* We failed to handle the error */
340 return nfs4_map_errors(ret);
342 ret = nfs4_wait_clnt_recover(clp);
344 exception->retry = 1;
349 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
351 spin_lock(&clp->cl_lock);
352 if (time_before(clp->cl_last_renewal,timestamp))
353 clp->cl_last_renewal = timestamp;
354 spin_unlock(&clp->cl_lock);
357 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
359 do_renew_lease(server->nfs_client, timestamp);
362 #if defined(CONFIG_NFS_V4_1)
365 * nfs4_free_slot - free a slot and efficiently update slot table.
367 * freeing a slot is trivially done by clearing its respective bit
369 * If the freed slotid equals highest_used_slotid we want to update it
370 * so that the server would be able to size down the slot table if needed,
371 * otherwise we know that the highest_used_slotid is still in use.
372 * When updating highest_used_slotid there may be "holes" in the bitmap
373 * so we need to scan down from highest_used_slotid to 0 looking for the now
374 * highest slotid in use.
375 * If none found, highest_used_slotid is set to -1.
377 * Must be called while holding tbl->slot_tbl_lock
380 nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
382 int free_slotid = free_slot - tbl->slots;
383 int slotid = free_slotid;
385 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
386 /* clear used bit in bitmap */
387 __clear_bit(slotid, tbl->used_slots);
389 /* update highest_used_slotid when it is freed */
390 if (slotid == tbl->highest_used_slotid) {
391 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
392 if (slotid < tbl->max_slots)
393 tbl->highest_used_slotid = slotid;
395 tbl->highest_used_slotid = -1;
397 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
398 free_slotid, tbl->highest_used_slotid);
402 * Signal state manager thread if session fore channel is drained
404 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
406 struct rpc_task *task;
408 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
409 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
411 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
415 if (ses->fc_slot_table.highest_used_slotid != -1)
418 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
419 complete(&ses->fc_slot_table.complete);
423 * Signal state manager thread if session back channel is drained
425 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
427 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
428 ses->bc_slot_table.highest_used_slotid != -1)
430 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
431 complete(&ses->bc_slot_table.complete);
434 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
436 struct nfs4_slot_table *tbl;
438 tbl = &res->sr_session->fc_slot_table;
440 /* just wake up the next guy waiting since
441 * we may have not consumed a slot after all */
442 dprintk("%s: No slot\n", __func__);
446 spin_lock(&tbl->slot_tbl_lock);
447 nfs4_free_slot(tbl, res->sr_slot);
448 nfs4_check_drain_fc_complete(res->sr_session);
449 spin_unlock(&tbl->slot_tbl_lock);
453 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
455 unsigned long timestamp;
456 struct nfs_client *clp;
459 * sr_status remains 1 if an RPC level error occurred. The server
460 * may or may not have processed the sequence operation..
461 * Proceed as if the server received and processed the sequence
464 if (res->sr_status == 1)
465 res->sr_status = NFS_OK;
467 /* don't increment the sequence number if the task wasn't sent */
468 if (!RPC_WAS_SENT(task))
471 /* Check the SEQUENCE operation status */
472 switch (res->sr_status) {
474 /* Update the slot's sequence and clientid lease timer */
475 ++res->sr_slot->seq_nr;
476 timestamp = res->sr_renewal_time;
477 clp = res->sr_session->clp;
478 do_renew_lease(clp, timestamp);
479 /* Check sequence flags */
480 if (res->sr_status_flags != 0)
481 nfs4_schedule_lease_recovery(clp);
484 /* The server detected a resend of the RPC call and
485 * returned NFS4ERR_DELAY as per Section 2.10.6.2
488 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
490 res->sr_slot - res->sr_session->fc_slot_table.slots,
491 res->sr_slot->seq_nr);
494 /* Just update the slot sequence no. */
495 ++res->sr_slot->seq_nr;
498 /* The session may be reset by one of the error handlers. */
499 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
500 nfs41_sequence_free_slot(res);
503 if (!rpc_restart_call(task))
505 rpc_delay(task, NFS4_POLL_RETRY_MAX);
509 static int nfs4_sequence_done(struct rpc_task *task,
510 struct nfs4_sequence_res *res)
512 if (res->sr_session == NULL)
514 return nfs41_sequence_done(task, res);
518 * nfs4_find_slot - efficiently look for a free slot
520 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
521 * If found, we mark the slot as used, update the highest_used_slotid,
522 * and respectively set up the sequence operation args.
523 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
525 * Note: must be called with under the slot_tbl_lock.
528 nfs4_find_slot(struct nfs4_slot_table *tbl)
531 u8 ret_id = NFS4_MAX_SLOT_TABLE;
532 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
534 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
535 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
537 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
538 if (slotid >= tbl->max_slots)
540 __set_bit(slotid, tbl->used_slots);
541 if (slotid > tbl->highest_used_slotid)
542 tbl->highest_used_slotid = slotid;
545 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
546 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
550 int nfs41_setup_sequence(struct nfs4_session *session,
551 struct nfs4_sequence_args *args,
552 struct nfs4_sequence_res *res,
554 struct rpc_task *task)
556 struct nfs4_slot *slot;
557 struct nfs4_slot_table *tbl;
560 dprintk("--> %s\n", __func__);
561 /* slot already allocated? */
562 if (res->sr_slot != NULL)
565 tbl = &session->fc_slot_table;
567 spin_lock(&tbl->slot_tbl_lock);
568 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
569 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
571 * The state manager will wait until the slot table is empty.
572 * Schedule the reset thread
574 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
575 spin_unlock(&tbl->slot_tbl_lock);
576 dprintk("%s Schedule Session Reset\n", __func__);
580 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
581 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
582 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
583 spin_unlock(&tbl->slot_tbl_lock);
584 dprintk("%s enforce FIFO order\n", __func__);
588 slotid = nfs4_find_slot(tbl);
589 if (slotid == NFS4_MAX_SLOT_TABLE) {
590 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
591 spin_unlock(&tbl->slot_tbl_lock);
592 dprintk("<-- %s: no free slots\n", __func__);
595 spin_unlock(&tbl->slot_tbl_lock);
597 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
598 slot = tbl->slots + slotid;
599 args->sa_session = session;
600 args->sa_slotid = slotid;
601 args->sa_cache_this = cache_reply;
603 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
605 res->sr_session = session;
607 res->sr_renewal_time = jiffies;
608 res->sr_status_flags = 0;
610 * sr_status is only set in decode_sequence, and so will remain
611 * set to 1 if an rpc level failure occurs.
616 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
618 int nfs4_setup_sequence(const struct nfs_server *server,
619 struct nfs4_sequence_args *args,
620 struct nfs4_sequence_res *res,
622 struct rpc_task *task)
624 struct nfs4_session *session = nfs4_get_session(server);
627 if (session == NULL) {
628 args->sa_session = NULL;
629 res->sr_session = NULL;
633 dprintk("--> %s clp %p session %p sr_slot %td\n",
634 __func__, session->clp, session, res->sr_slot ?
635 res->sr_slot - session->fc_slot_table.slots : -1);
637 ret = nfs41_setup_sequence(session, args, res, cache_reply,
640 dprintk("<-- %s status=%d\n", __func__, ret);
644 struct nfs41_call_sync_data {
645 const struct nfs_server *seq_server;
646 struct nfs4_sequence_args *seq_args;
647 struct nfs4_sequence_res *seq_res;
651 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
653 struct nfs41_call_sync_data *data = calldata;
655 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
657 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
658 data->seq_res, data->cache_reply, task))
660 rpc_call_start(task);
663 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
665 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
666 nfs41_call_sync_prepare(task, calldata);
669 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
671 struct nfs41_call_sync_data *data = calldata;
673 nfs41_sequence_done(task, data->seq_res);
676 struct rpc_call_ops nfs41_call_sync_ops = {
677 .rpc_call_prepare = nfs41_call_sync_prepare,
678 .rpc_call_done = nfs41_call_sync_done,
681 struct rpc_call_ops nfs41_call_priv_sync_ops = {
682 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
683 .rpc_call_done = nfs41_call_sync_done,
686 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
687 struct nfs_server *server,
688 struct rpc_message *msg,
689 struct nfs4_sequence_args *args,
690 struct nfs4_sequence_res *res,
695 struct rpc_task *task;
696 struct nfs41_call_sync_data data = {
697 .seq_server = server,
700 .cache_reply = cache_reply,
702 struct rpc_task_setup task_setup = {
705 .callback_ops = &nfs41_call_sync_ops,
706 .callback_data = &data
711 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
712 task = rpc_run_task(&task_setup);
716 ret = task->tk_status;
722 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
723 struct nfs_server *server,
724 struct rpc_message *msg,
725 struct nfs4_sequence_args *args,
726 struct nfs4_sequence_res *res,
729 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
733 static int nfs4_sequence_done(struct rpc_task *task,
734 struct nfs4_sequence_res *res)
738 #endif /* CONFIG_NFS_V4_1 */
740 int _nfs4_call_sync(struct rpc_clnt *clnt,
741 struct nfs_server *server,
742 struct rpc_message *msg,
743 struct nfs4_sequence_args *args,
744 struct nfs4_sequence_res *res,
747 args->sa_session = res->sr_session = NULL;
748 return rpc_call_sync(clnt, msg, 0);
752 int nfs4_call_sync(struct rpc_clnt *clnt,
753 struct nfs_server *server,
754 struct rpc_message *msg,
755 struct nfs4_sequence_args *args,
756 struct nfs4_sequence_res *res,
759 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
760 args, res, cache_reply);
763 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
765 struct nfs_inode *nfsi = NFS_I(dir);
767 spin_lock(&dir->i_lock);
768 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
769 if (!cinfo->atomic || cinfo->before != dir->i_version)
770 nfs_force_lookup_revalidate(dir);
771 dir->i_version = cinfo->after;
772 spin_unlock(&dir->i_lock);
775 struct nfs4_opendata {
777 struct nfs_openargs o_arg;
778 struct nfs_openres o_res;
779 struct nfs_open_confirmargs c_arg;
780 struct nfs_open_confirmres c_res;
781 struct nfs_fattr f_attr;
782 struct nfs_fattr dir_attr;
784 struct dentry *dentry;
785 struct nfs4_state_owner *owner;
786 struct nfs4_state *state;
788 unsigned long timestamp;
789 unsigned int rpc_done : 1;
795 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
797 p->o_res.f_attr = &p->f_attr;
798 p->o_res.dir_attr = &p->dir_attr;
799 p->o_res.seqid = p->o_arg.seqid;
800 p->c_res.seqid = p->c_arg.seqid;
801 p->o_res.server = p->o_arg.server;
802 nfs_fattr_init(&p->f_attr);
803 nfs_fattr_init(&p->dir_attr);
806 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
807 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
808 const struct iattr *attrs,
811 struct dentry *parent = dget_parent(dentry);
812 struct inode *dir = parent->d_inode;
813 struct nfs_server *server = NFS_SERVER(dir);
814 struct nfs4_opendata *p;
816 p = kzalloc(sizeof(*p), gfp_mask);
819 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
820 if (p->o_arg.seqid == NULL)
822 nfs_sb_active(dentry->d_sb);
823 p->dentry = dget(dentry);
826 atomic_inc(&sp->so_count);
827 p->o_arg.fh = NFS_FH(dir);
828 p->o_arg.open_flags = flags;
829 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
830 p->o_arg.clientid = server->nfs_client->cl_clientid;
831 p->o_arg.id = sp->so_owner_id.id;
832 p->o_arg.name = &dentry->d_name;
833 p->o_arg.server = server;
834 p->o_arg.bitmask = server->attr_bitmask;
835 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
836 if (flags & O_CREAT) {
839 p->o_arg.u.attrs = &p->attrs;
840 memcpy(&p->attrs, attrs, sizeof(p->attrs));
841 s = (u32 *) p->o_arg.u.verifier.data;
845 p->c_arg.fh = &p->o_res.fh;
846 p->c_arg.stateid = &p->o_res.stateid;
847 p->c_arg.seqid = p->o_arg.seqid;
848 nfs4_init_opendata_res(p);
858 static void nfs4_opendata_free(struct kref *kref)
860 struct nfs4_opendata *p = container_of(kref,
861 struct nfs4_opendata, kref);
862 struct super_block *sb = p->dentry->d_sb;
864 nfs_free_seqid(p->o_arg.seqid);
865 if (p->state != NULL)
866 nfs4_put_open_state(p->state);
867 nfs4_put_state_owner(p->owner);
874 static void nfs4_opendata_put(struct nfs4_opendata *p)
877 kref_put(&p->kref, nfs4_opendata_free);
880 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
884 ret = rpc_wait_for_completion_task(task);
888 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
892 if (open_mode & O_EXCL)
894 switch (mode & (FMODE_READ|FMODE_WRITE)) {
896 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
897 && state->n_rdonly != 0;
900 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
901 && state->n_wronly != 0;
903 case FMODE_READ|FMODE_WRITE:
904 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
905 && state->n_rdwr != 0;
911 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
913 if (delegation == NULL)
915 if ((delegation->type & fmode) != fmode)
917 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
919 nfs_mark_delegation_referenced(delegation);
923 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
932 case FMODE_READ|FMODE_WRITE:
935 nfs4_state_set_mode_locked(state, state->state | fmode);
938 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
940 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
941 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
942 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
945 set_bit(NFS_O_RDONLY_STATE, &state->flags);
948 set_bit(NFS_O_WRONLY_STATE, &state->flags);
950 case FMODE_READ|FMODE_WRITE:
951 set_bit(NFS_O_RDWR_STATE, &state->flags);
955 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
957 write_seqlock(&state->seqlock);
958 nfs_set_open_stateid_locked(state, stateid, fmode);
959 write_sequnlock(&state->seqlock);
962 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
965 * Protect the call to nfs4_state_set_mode_locked and
966 * serialise the stateid update
968 write_seqlock(&state->seqlock);
969 if (deleg_stateid != NULL) {
970 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
971 set_bit(NFS_DELEGATED_STATE, &state->flags);
973 if (open_stateid != NULL)
974 nfs_set_open_stateid_locked(state, open_stateid, fmode);
975 write_sequnlock(&state->seqlock);
976 spin_lock(&state->owner->so_lock);
977 update_open_stateflags(state, fmode);
978 spin_unlock(&state->owner->so_lock);
981 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
983 struct nfs_inode *nfsi = NFS_I(state->inode);
984 struct nfs_delegation *deleg_cur;
987 fmode &= (FMODE_READ|FMODE_WRITE);
990 deleg_cur = rcu_dereference(nfsi->delegation);
991 if (deleg_cur == NULL)
994 spin_lock(&deleg_cur->lock);
995 if (nfsi->delegation != deleg_cur ||
996 (deleg_cur->type & fmode) != fmode)
997 goto no_delegation_unlock;
999 if (delegation == NULL)
1000 delegation = &deleg_cur->stateid;
1001 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
1002 goto no_delegation_unlock;
1004 nfs_mark_delegation_referenced(deleg_cur);
1005 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1007 no_delegation_unlock:
1008 spin_unlock(&deleg_cur->lock);
1012 if (!ret && open_stateid != NULL) {
1013 __update_open_stateid(state, open_stateid, NULL, fmode);
1021 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1023 struct nfs_delegation *delegation;
1026 delegation = rcu_dereference(NFS_I(inode)->delegation);
1027 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1032 nfs_inode_return_delegation(inode);
1035 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1037 struct nfs4_state *state = opendata->state;
1038 struct nfs_inode *nfsi = NFS_I(state->inode);
1039 struct nfs_delegation *delegation;
1040 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1041 fmode_t fmode = opendata->o_arg.fmode;
1042 nfs4_stateid stateid;
1046 if (can_open_cached(state, fmode, open_mode)) {
1047 spin_lock(&state->owner->so_lock);
1048 if (can_open_cached(state, fmode, open_mode)) {
1049 update_open_stateflags(state, fmode);
1050 spin_unlock(&state->owner->so_lock);
1051 goto out_return_state;
1053 spin_unlock(&state->owner->so_lock);
1056 delegation = rcu_dereference(nfsi->delegation);
1057 if (!can_open_delegated(delegation, fmode)) {
1061 /* Save the delegation */
1062 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1064 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1069 /* Try to update the stateid using the delegation */
1070 if (update_open_stateid(state, NULL, &stateid, fmode))
1071 goto out_return_state;
1074 return ERR_PTR(ret);
1076 atomic_inc(&state->count);
1080 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1082 struct inode *inode;
1083 struct nfs4_state *state = NULL;
1084 struct nfs_delegation *delegation;
1087 if (!data->rpc_done) {
1088 state = nfs4_try_open_cached(data);
1093 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1095 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1096 ret = PTR_ERR(inode);
1100 state = nfs4_get_open_state(inode, data->owner);
1103 if (data->o_res.delegation_type != 0) {
1104 int delegation_flags = 0;
1107 delegation = rcu_dereference(NFS_I(inode)->delegation);
1109 delegation_flags = delegation->flags;
1111 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1112 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1113 "returning a delegation for "
1114 "OPEN(CLAIM_DELEGATE_CUR)\n",
1115 NFS_CLIENT(inode)->cl_server);
1116 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1117 nfs_inode_set_delegation(state->inode,
1118 data->owner->so_cred,
1121 nfs_inode_reclaim_delegation(state->inode,
1122 data->owner->so_cred,
1126 update_open_stateid(state, &data->o_res.stateid, NULL,
1134 return ERR_PTR(ret);
1137 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1139 struct nfs_inode *nfsi = NFS_I(state->inode);
1140 struct nfs_open_context *ctx;
1142 spin_lock(&state->inode->i_lock);
1143 list_for_each_entry(ctx, &nfsi->open_files, list) {
1144 if (ctx->state != state)
1146 get_nfs_open_context(ctx);
1147 spin_unlock(&state->inode->i_lock);
1150 spin_unlock(&state->inode->i_lock);
1151 return ERR_PTR(-ENOENT);
1154 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1156 struct nfs4_opendata *opendata;
1158 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1159 if (opendata == NULL)
1160 return ERR_PTR(-ENOMEM);
1161 opendata->state = state;
1162 atomic_inc(&state->count);
1166 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1168 struct nfs4_state *newstate;
1171 opendata->o_arg.open_flags = 0;
1172 opendata->o_arg.fmode = fmode;
1173 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1174 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1175 nfs4_init_opendata_res(opendata);
1176 ret = _nfs4_recover_proc_open(opendata);
1179 newstate = nfs4_opendata_to_nfs4_state(opendata);
1180 if (IS_ERR(newstate))
1181 return PTR_ERR(newstate);
1182 nfs4_close_state(newstate, fmode);
1187 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1189 struct nfs4_state *newstate;
1192 /* memory barrier prior to reading state->n_* */
1193 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1195 if (state->n_rdwr != 0) {
1196 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1197 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1200 if (newstate != state)
1203 if (state->n_wronly != 0) {
1204 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1205 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1208 if (newstate != state)
1211 if (state->n_rdonly != 0) {
1212 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1213 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1216 if (newstate != state)
1220 * We may have performed cached opens for all three recoveries.
1221 * Check if we need to update the current stateid.
1223 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1224 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1225 write_seqlock(&state->seqlock);
1226 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1227 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1228 write_sequnlock(&state->seqlock);
1235 * reclaim state on the server after a reboot.
1237 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1239 struct nfs_delegation *delegation;
1240 struct nfs4_opendata *opendata;
1241 fmode_t delegation_type = 0;
1244 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1245 if (IS_ERR(opendata))
1246 return PTR_ERR(opendata);
1247 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1248 opendata->o_arg.fh = NFS_FH(state->inode);
1250 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1251 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1252 delegation_type = delegation->type;
1254 opendata->o_arg.u.delegation_type = delegation_type;
1255 status = nfs4_open_recover(opendata, state);
1256 nfs4_opendata_put(opendata);
1260 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1262 struct nfs_server *server = NFS_SERVER(state->inode);
1263 struct nfs4_exception exception = { };
1266 err = _nfs4_do_open_reclaim(ctx, state);
1267 if (err != -NFS4ERR_DELAY)
1269 nfs4_handle_exception(server, err, &exception);
1270 } while (exception.retry);
1274 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1276 struct nfs_open_context *ctx;
1279 ctx = nfs4_state_find_open_context(state);
1281 return PTR_ERR(ctx);
1282 ret = nfs4_do_open_reclaim(ctx, state);
1283 put_nfs_open_context(ctx);
1287 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1289 struct nfs4_opendata *opendata;
1292 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1293 if (IS_ERR(opendata))
1294 return PTR_ERR(opendata);
1295 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1296 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1297 sizeof(opendata->o_arg.u.delegation.data));
1298 ret = nfs4_open_recover(opendata, state);
1299 nfs4_opendata_put(opendata);
1303 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1305 struct nfs4_exception exception = { };
1306 struct nfs_server *server = NFS_SERVER(state->inode);
1309 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1315 case -NFS4ERR_BADSESSION:
1316 case -NFS4ERR_BADSLOT:
1317 case -NFS4ERR_BAD_HIGH_SLOT:
1318 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1319 case -NFS4ERR_DEADSESSION:
1320 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
1322 case -NFS4ERR_STALE_CLIENTID:
1323 case -NFS4ERR_STALE_STATEID:
1324 case -NFS4ERR_EXPIRED:
1325 /* Don't recall a delegation if it was lost */
1326 nfs4_schedule_lease_recovery(server->nfs_client);
1330 * The show must go on: exit, but mark the
1331 * stateid as needing recovery.
1333 case -NFS4ERR_DELEG_REVOKED:
1334 case -NFS4ERR_ADMIN_REVOKED:
1335 case -NFS4ERR_BAD_STATEID:
1336 nfs_inode_find_state_and_recover(state->inode,
1338 nfs4_schedule_stateid_recovery(server, state);
1341 * User RPCSEC_GSS context has expired.
1342 * We cannot recover this stateid now, so
1343 * skip it and allow recovery thread to
1350 err = nfs4_handle_exception(server, err, &exception);
1351 } while (exception.retry);
1356 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1358 struct nfs4_opendata *data = calldata;
1360 data->rpc_status = task->tk_status;
1361 if (data->rpc_status == 0) {
1362 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1363 sizeof(data->o_res.stateid.data));
1364 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1365 renew_lease(data->o_res.server, data->timestamp);
1370 static void nfs4_open_confirm_release(void *calldata)
1372 struct nfs4_opendata *data = calldata;
1373 struct nfs4_state *state = NULL;
1375 /* If this request hasn't been cancelled, do nothing */
1376 if (data->cancelled == 0)
1378 /* In case of error, no cleanup! */
1379 if (!data->rpc_done)
1381 state = nfs4_opendata_to_nfs4_state(data);
1383 nfs4_close_state(state, data->o_arg.fmode);
1385 nfs4_opendata_put(data);
1388 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1389 .rpc_call_done = nfs4_open_confirm_done,
1390 .rpc_release = nfs4_open_confirm_release,
1394 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1396 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1398 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1399 struct rpc_task *task;
1400 struct rpc_message msg = {
1401 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1402 .rpc_argp = &data->c_arg,
1403 .rpc_resp = &data->c_res,
1404 .rpc_cred = data->owner->so_cred,
1406 struct rpc_task_setup task_setup_data = {
1407 .rpc_client = server->client,
1408 .rpc_message = &msg,
1409 .callback_ops = &nfs4_open_confirm_ops,
1410 .callback_data = data,
1411 .workqueue = nfsiod_workqueue,
1412 .flags = RPC_TASK_ASYNC,
1416 kref_get(&data->kref);
1418 data->rpc_status = 0;
1419 data->timestamp = jiffies;
1420 task = rpc_run_task(&task_setup_data);
1422 return PTR_ERR(task);
1423 status = nfs4_wait_for_completion_rpc_task(task);
1425 data->cancelled = 1;
1428 status = data->rpc_status;
1433 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1435 struct nfs4_opendata *data = calldata;
1436 struct nfs4_state_owner *sp = data->owner;
1438 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1441 * Check if we still need to send an OPEN call, or if we can use
1442 * a delegation instead.
1444 if (data->state != NULL) {
1445 struct nfs_delegation *delegation;
1447 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1450 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1451 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1452 can_open_delegated(delegation, data->o_arg.fmode))
1453 goto unlock_no_action;
1456 /* Update sequence id. */
1457 data->o_arg.id = sp->so_owner_id.id;
1458 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1459 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1460 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1461 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1463 data->timestamp = jiffies;
1464 if (nfs4_setup_sequence(data->o_arg.server,
1465 &data->o_arg.seq_args,
1466 &data->o_res.seq_res,
1468 nfs_release_seqid(data->o_arg.seqid);
1470 rpc_call_start(task);
1475 task->tk_action = NULL;
1479 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1481 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1482 nfs4_open_prepare(task, calldata);
1485 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1487 struct nfs4_opendata *data = calldata;
1489 data->rpc_status = task->tk_status;
1491 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1494 if (task->tk_status == 0) {
1495 switch (data->o_res.f_attr->mode & S_IFMT) {
1499 data->rpc_status = -ELOOP;
1502 data->rpc_status = -EISDIR;
1505 data->rpc_status = -ENOTDIR;
1507 renew_lease(data->o_res.server, data->timestamp);
1508 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1509 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1514 static void nfs4_open_release(void *calldata)
1516 struct nfs4_opendata *data = calldata;
1517 struct nfs4_state *state = NULL;
1519 /* If this request hasn't been cancelled, do nothing */
1520 if (data->cancelled == 0)
1522 /* In case of error, no cleanup! */
1523 if (data->rpc_status != 0 || !data->rpc_done)
1525 /* In case we need an open_confirm, no cleanup! */
1526 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1528 state = nfs4_opendata_to_nfs4_state(data);
1530 nfs4_close_state(state, data->o_arg.fmode);
1532 nfs4_opendata_put(data);
1535 static const struct rpc_call_ops nfs4_open_ops = {
1536 .rpc_call_prepare = nfs4_open_prepare,
1537 .rpc_call_done = nfs4_open_done,
1538 .rpc_release = nfs4_open_release,
1541 static const struct rpc_call_ops nfs4_recover_open_ops = {
1542 .rpc_call_prepare = nfs4_recover_open_prepare,
1543 .rpc_call_done = nfs4_open_done,
1544 .rpc_release = nfs4_open_release,
1547 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1549 struct inode *dir = data->dir->d_inode;
1550 struct nfs_server *server = NFS_SERVER(dir);
1551 struct nfs_openargs *o_arg = &data->o_arg;
1552 struct nfs_openres *o_res = &data->o_res;
1553 struct rpc_task *task;
1554 struct rpc_message msg = {
1555 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1558 .rpc_cred = data->owner->so_cred,
1560 struct rpc_task_setup task_setup_data = {
1561 .rpc_client = server->client,
1562 .rpc_message = &msg,
1563 .callback_ops = &nfs4_open_ops,
1564 .callback_data = data,
1565 .workqueue = nfsiod_workqueue,
1566 .flags = RPC_TASK_ASYNC,
1570 kref_get(&data->kref);
1572 data->rpc_status = 0;
1573 data->cancelled = 0;
1575 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1576 task = rpc_run_task(&task_setup_data);
1578 return PTR_ERR(task);
1579 status = nfs4_wait_for_completion_rpc_task(task);
1581 data->cancelled = 1;
1584 status = data->rpc_status;
1590 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1592 struct inode *dir = data->dir->d_inode;
1593 struct nfs_openres *o_res = &data->o_res;
1596 status = nfs4_run_open_task(data, 1);
1597 if (status != 0 || !data->rpc_done)
1600 nfs_refresh_inode(dir, o_res->dir_attr);
1602 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1603 status = _nfs4_proc_open_confirm(data);
1612 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1614 static int _nfs4_proc_open(struct nfs4_opendata *data)
1616 struct inode *dir = data->dir->d_inode;
1617 struct nfs_server *server = NFS_SERVER(dir);
1618 struct nfs_openargs *o_arg = &data->o_arg;
1619 struct nfs_openres *o_res = &data->o_res;
1622 status = nfs4_run_open_task(data, 0);
1623 if (!data->rpc_done)
1626 if (status == -NFS4ERR_BADNAME &&
1627 !(o_arg->open_flags & O_CREAT))
1632 if (o_arg->open_flags & O_CREAT) {
1633 update_changeattr(dir, &o_res->cinfo);
1634 nfs_post_op_update_inode(dir, o_res->dir_attr);
1636 nfs_refresh_inode(dir, o_res->dir_attr);
1637 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1638 server->caps &= ~NFS_CAP_POSIX_LOCK;
1639 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1640 status = _nfs4_proc_open_confirm(data);
1644 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1645 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1649 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1654 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1655 ret = nfs4_wait_clnt_recover(clp);
1658 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1659 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1661 nfs4_schedule_state_manager(clp);
1667 static int nfs4_recover_expired_lease(struct nfs_server *server)
1669 return nfs4_client_recover_expired_lease(server->nfs_client);
1674 * reclaim state on the server after a network partition.
1675 * Assumes caller holds the appropriate lock
1677 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1679 struct nfs4_opendata *opendata;
1682 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1683 if (IS_ERR(opendata))
1684 return PTR_ERR(opendata);
1685 ret = nfs4_open_recover(opendata, state);
1687 d_drop(ctx->dentry);
1688 nfs4_opendata_put(opendata);
1692 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1694 struct nfs_server *server = NFS_SERVER(state->inode);
1695 struct nfs4_exception exception = { };
1699 err = _nfs4_open_expired(ctx, state);
1703 case -NFS4ERR_GRACE:
1704 case -NFS4ERR_DELAY:
1705 nfs4_handle_exception(server, err, &exception);
1708 } while (exception.retry);
1713 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1715 struct nfs_open_context *ctx;
1718 ctx = nfs4_state_find_open_context(state);
1720 return PTR_ERR(ctx);
1721 ret = nfs4_do_open_expired(ctx, state);
1722 put_nfs_open_context(ctx);
1726 #if defined(CONFIG_NFS_V4_1)
1727 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1730 struct nfs_server *server = NFS_SERVER(state->inode);
1732 status = nfs41_test_stateid(server, state);
1733 if (status == NFS_OK)
1735 nfs41_free_stateid(server, state);
1736 return nfs4_open_expired(sp, state);
1741 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1742 * fields corresponding to attributes that were used to store the verifier.
1743 * Make sure we clobber those fields in the later setattr call
1745 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1747 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1748 !(sattr->ia_valid & ATTR_ATIME_SET))
1749 sattr->ia_valid |= ATTR_ATIME;
1751 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1752 !(sattr->ia_valid & ATTR_MTIME_SET))
1753 sattr->ia_valid |= ATTR_MTIME;
1757 * Returns a referenced nfs4_state
1759 static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
1761 struct nfs4_state_owner *sp;
1762 struct nfs4_state *state = NULL;
1763 struct nfs_server *server = NFS_SERVER(dir);
1764 struct nfs4_opendata *opendata;
1767 /* Protect against reboot recovery conflicts */
1769 if (!(sp = nfs4_get_state_owner(server, cred))) {
1770 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1773 status = nfs4_recover_expired_lease(server);
1775 goto err_put_state_owner;
1776 if (dentry->d_inode != NULL)
1777 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1779 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1780 if (opendata == NULL)
1781 goto err_put_state_owner;
1783 if (dentry->d_inode != NULL)
1784 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1786 status = _nfs4_proc_open(opendata);
1788 goto err_opendata_put;
1790 state = nfs4_opendata_to_nfs4_state(opendata);
1791 status = PTR_ERR(state);
1793 goto err_opendata_put;
1794 if (server->caps & NFS_CAP_POSIX_LOCK)
1795 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1797 if (opendata->o_arg.open_flags & O_EXCL) {
1798 nfs4_exclusive_attrset(opendata, sattr);
1800 nfs_fattr_init(opendata->o_res.f_attr);
1801 status = nfs4_do_setattr(state->inode, cred,
1802 opendata->o_res.f_attr, sattr,
1805 nfs_setattr_update_inode(state->inode, sattr);
1806 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1808 nfs_revalidate_inode(server, state->inode);
1809 nfs4_opendata_put(opendata);
1810 nfs4_put_state_owner(sp);
1814 nfs4_opendata_put(opendata);
1815 err_put_state_owner:
1816 nfs4_put_state_owner(sp);
1823 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
1825 struct nfs4_exception exception = { };
1826 struct nfs4_state *res;
1829 fmode &= FMODE_READ|FMODE_WRITE;
1831 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
1834 /* NOTE: BAD_SEQID means the server and client disagree about the
1835 * book-keeping w.r.t. state-changing operations
1836 * (OPEN/CLOSE/LOCK/LOCKU...)
1837 * It is actually a sign of a bug on the client or on the server.
1839 * If we receive a BAD_SEQID error in the particular case of
1840 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1841 * have unhashed the old state_owner for us, and that we can
1842 * therefore safely retry using a new one. We should still warn
1843 * the user though...
1845 if (status == -NFS4ERR_BAD_SEQID) {
1846 pr_warn_ratelimited("NFS: v4 server %s "
1847 " returned a bad sequence-id error!\n",
1848 NFS_SERVER(dir)->nfs_client->cl_hostname);
1849 exception.retry = 1;
1853 * BAD_STATEID on OPEN means that the server cancelled our
1854 * state before it received the OPEN_CONFIRM.
1855 * Recover by retrying the request as per the discussion
1856 * on Page 181 of RFC3530.
1858 if (status == -NFS4ERR_BAD_STATEID) {
1859 exception.retry = 1;
1862 if (status == -EAGAIN) {
1863 /* We must have found a delegation */
1864 exception.retry = 1;
1867 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1868 status, &exception));
1869 } while (exception.retry);
1873 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1874 struct nfs_fattr *fattr, struct iattr *sattr,
1875 struct nfs4_state *state)
1877 struct nfs_server *server = NFS_SERVER(inode);
1878 struct nfs_setattrargs arg = {
1879 .fh = NFS_FH(inode),
1882 .bitmask = server->attr_bitmask,
1884 struct nfs_setattrres res = {
1888 struct rpc_message msg = {
1889 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1894 unsigned long timestamp = jiffies;
1897 nfs_fattr_init(fattr);
1899 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1900 /* Use that stateid */
1901 } else if (state != NULL) {
1902 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
1904 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1906 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1907 if (status == 0 && state != NULL)
1908 renew_lease(server, timestamp);
1912 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1913 struct nfs_fattr *fattr, struct iattr *sattr,
1914 struct nfs4_state *state)
1916 struct nfs_server *server = NFS_SERVER(inode);
1917 struct nfs4_exception exception = {
1923 err = nfs4_handle_exception(server,
1924 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1926 } while (exception.retry);
1930 struct nfs4_closedata {
1931 struct inode *inode;
1932 struct nfs4_state *state;
1933 struct nfs_closeargs arg;
1934 struct nfs_closeres res;
1935 struct nfs_fattr fattr;
1936 unsigned long timestamp;
1941 static void nfs4_free_closedata(void *data)
1943 struct nfs4_closedata *calldata = data;
1944 struct nfs4_state_owner *sp = calldata->state->owner;
1945 struct super_block *sb = calldata->state->inode->i_sb;
1948 pnfs_roc_release(calldata->state->inode);
1949 nfs4_put_open_state(calldata->state);
1950 nfs_free_seqid(calldata->arg.seqid);
1951 nfs4_put_state_owner(sp);
1952 nfs_sb_deactive(sb);
1956 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1959 spin_lock(&state->owner->so_lock);
1960 if (!(fmode & FMODE_READ))
1961 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1962 if (!(fmode & FMODE_WRITE))
1963 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1964 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1965 spin_unlock(&state->owner->so_lock);
1968 static void nfs4_close_done(struct rpc_task *task, void *data)
1970 struct nfs4_closedata *calldata = data;
1971 struct nfs4_state *state = calldata->state;
1972 struct nfs_server *server = NFS_SERVER(calldata->inode);
1974 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1976 /* hmm. we are done with the inode, and in the process of freeing
1977 * the state_owner. we keep this around to process errors
1979 switch (task->tk_status) {
1982 pnfs_roc_set_barrier(state->inode,
1983 calldata->roc_barrier);
1984 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1985 renew_lease(server, calldata->timestamp);
1986 nfs4_close_clear_stateid_flags(state,
1987 calldata->arg.fmode);
1989 case -NFS4ERR_STALE_STATEID:
1990 case -NFS4ERR_OLD_STATEID:
1991 case -NFS4ERR_BAD_STATEID:
1992 case -NFS4ERR_EXPIRED:
1993 if (calldata->arg.fmode == 0)
1996 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1997 rpc_restart_call_prepare(task);
1999 nfs_release_seqid(calldata->arg.seqid);
2000 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2003 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2005 struct nfs4_closedata *calldata = data;
2006 struct nfs4_state *state = calldata->state;
2009 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2012 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2013 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2014 spin_lock(&state->owner->so_lock);
2015 /* Calculate the change in open mode */
2016 if (state->n_rdwr == 0) {
2017 if (state->n_rdonly == 0) {
2018 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2019 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2020 calldata->arg.fmode &= ~FMODE_READ;
2022 if (state->n_wronly == 0) {
2023 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2024 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2025 calldata->arg.fmode &= ~FMODE_WRITE;
2028 spin_unlock(&state->owner->so_lock);
2031 /* Note: exit _without_ calling nfs4_close_done */
2032 task->tk_action = NULL;
2036 if (calldata->arg.fmode == 0) {
2037 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2038 if (calldata->roc &&
2039 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2040 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2046 nfs_fattr_init(calldata->res.fattr);
2047 calldata->timestamp = jiffies;
2048 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
2049 &calldata->arg.seq_args, &calldata->res.seq_res,
2051 nfs_release_seqid(calldata->arg.seqid);
2053 rpc_call_start(task);
2056 static const struct rpc_call_ops nfs4_close_ops = {
2057 .rpc_call_prepare = nfs4_close_prepare,
2058 .rpc_call_done = nfs4_close_done,
2059 .rpc_release = nfs4_free_closedata,
2063 * It is possible for data to be read/written from a mem-mapped file
2064 * after the sys_close call (which hits the vfs layer as a flush).
2065 * This means that we can't safely call nfsv4 close on a file until
2066 * the inode is cleared. This in turn means that we are not good
2067 * NFSv4 citizens - we do not indicate to the server to update the file's
2068 * share state even when we are done with one of the three share
2069 * stateid's in the inode.
2071 * NOTE: Caller must be holding the sp->so_owner semaphore!
2073 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
2075 struct nfs_server *server = NFS_SERVER(state->inode);
2076 struct nfs4_closedata *calldata;
2077 struct nfs4_state_owner *sp = state->owner;
2078 struct rpc_task *task;
2079 struct rpc_message msg = {
2080 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2081 .rpc_cred = state->owner->so_cred,
2083 struct rpc_task_setup task_setup_data = {
2084 .rpc_client = server->client,
2085 .rpc_message = &msg,
2086 .callback_ops = &nfs4_close_ops,
2087 .workqueue = nfsiod_workqueue,
2088 .flags = RPC_TASK_ASYNC,
2090 int status = -ENOMEM;
2092 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2093 if (calldata == NULL)
2095 calldata->inode = state->inode;
2096 calldata->state = state;
2097 calldata->arg.fh = NFS_FH(state->inode);
2098 calldata->arg.stateid = &state->open_stateid;
2099 /* Serialization for the sequence id */
2100 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2101 if (calldata->arg.seqid == NULL)
2102 goto out_free_calldata;
2103 calldata->arg.fmode = 0;
2104 calldata->arg.bitmask = server->cache_consistency_bitmask;
2105 calldata->res.fattr = &calldata->fattr;
2106 calldata->res.seqid = calldata->arg.seqid;
2107 calldata->res.server = server;
2108 calldata->roc = roc;
2109 nfs_sb_active(calldata->inode->i_sb);
2111 msg.rpc_argp = &calldata->arg;
2112 msg.rpc_resp = &calldata->res;
2113 task_setup_data.callback_data = calldata;
2114 task = rpc_run_task(&task_setup_data);
2116 return PTR_ERR(task);
2119 status = rpc_wait_for_completion_task(task);
2126 pnfs_roc_release(state->inode);
2127 nfs4_put_open_state(state);
2128 nfs4_put_state_owner(sp);
2132 static struct inode *
2133 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2135 struct nfs4_state *state;
2137 /* Protect against concurrent sillydeletes */
2138 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
2140 return ERR_CAST(state);
2142 return igrab(state->inode);
2145 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2147 if (ctx->state == NULL)
2150 nfs4_close_sync(ctx->state, ctx->mode);
2152 nfs4_close_state(ctx->state, ctx->mode);
2155 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2157 struct nfs4_server_caps_arg args = {
2160 struct nfs4_server_caps_res res = {};
2161 struct rpc_message msg = {
2162 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2168 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2170 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2171 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2172 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2173 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2174 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2175 NFS_CAP_CTIME|NFS_CAP_MTIME);
2176 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2177 server->caps |= NFS_CAP_ACLS;
2178 if (res.has_links != 0)
2179 server->caps |= NFS_CAP_HARDLINKS;
2180 if (res.has_symlinks != 0)
2181 server->caps |= NFS_CAP_SYMLINKS;
2182 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2183 server->caps |= NFS_CAP_FILEID;
2184 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2185 server->caps |= NFS_CAP_MODE;
2186 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2187 server->caps |= NFS_CAP_NLINK;
2188 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2189 server->caps |= NFS_CAP_OWNER;
2190 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2191 server->caps |= NFS_CAP_OWNER_GROUP;
2192 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2193 server->caps |= NFS_CAP_ATIME;
2194 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2195 server->caps |= NFS_CAP_CTIME;
2196 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2197 server->caps |= NFS_CAP_MTIME;
2199 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2200 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2201 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2202 server->acl_bitmask = res.acl_bitmask;
2208 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2210 struct nfs4_exception exception = { };
2213 err = nfs4_handle_exception(server,
2214 _nfs4_server_capabilities(server, fhandle),
2216 } while (exception.retry);
2220 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2221 struct nfs_fsinfo *info)
2223 struct nfs4_lookup_root_arg args = {
2224 .bitmask = nfs4_fattr_bitmap,
2226 struct nfs4_lookup_res res = {
2228 .fattr = info->fattr,
2231 struct rpc_message msg = {
2232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2237 nfs_fattr_init(info->fattr);
2238 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2241 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2242 struct nfs_fsinfo *info)
2244 struct nfs4_exception exception = { };
2247 err = _nfs4_lookup_root(server, fhandle, info);
2250 case -NFS4ERR_WRONGSEC:
2253 err = nfs4_handle_exception(server, err, &exception);
2255 } while (exception.retry);
2260 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2261 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2263 struct rpc_auth *auth;
2266 auth = rpcauth_create(flavor, server->client);
2271 ret = nfs4_lookup_root(server, fhandle, info);
2276 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2277 struct nfs_fsinfo *info)
2279 int i, len, status = 0;
2280 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2282 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2283 flav_array[len] = RPC_AUTH_NULL;
2286 for (i = 0; i < len; i++) {
2287 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2288 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2293 * -EACCESS could mean that the user doesn't have correct permissions
2294 * to access the mount. It could also mean that we tried to mount
2295 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2296 * existing mount programs don't handle -EACCES very well so it should
2297 * be mapped to -EPERM instead.
2299 if (status == -EACCES)
2305 * get the file handle for the "/" directory on the server
2307 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2308 struct nfs_fsinfo *info)
2310 int minor_version = server->nfs_client->cl_minorversion;
2311 int status = nfs4_lookup_root(server, fhandle, info);
2312 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2314 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2315 * by nfs4_map_errors() as this function exits.
2317 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2319 status = nfs4_server_capabilities(server, fhandle);
2321 status = nfs4_do_fsinfo(server, fhandle, info);
2322 return nfs4_map_errors(status);
2325 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
2327 * Get locations and (maybe) other attributes of a referral.
2328 * Note that we'll actually follow the referral later when
2329 * we detect fsid mismatch in inode revalidation
2331 static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2332 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2334 int status = -ENOMEM;
2335 struct page *page = NULL;
2336 struct nfs4_fs_locations *locations = NULL;
2338 page = alloc_page(GFP_KERNEL);
2341 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2342 if (locations == NULL)
2345 status = nfs4_proc_fs_locations(dir, name, locations, page);
2348 /* Make sure server returned a different fsid for the referral */
2349 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2350 dprintk("%s: server did not return a different fsid for"
2351 " a referral at %s\n", __func__, name->name);
2355 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2356 nfs_fixup_referral_attributes(&locations->fattr);
2358 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2359 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2360 memset(fhandle, 0, sizeof(struct nfs_fh));
2368 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2370 struct nfs4_getattr_arg args = {
2372 .bitmask = server->attr_bitmask,
2374 struct nfs4_getattr_res res = {
2378 struct rpc_message msg = {
2379 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2384 nfs_fattr_init(fattr);
2385 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2388 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2390 struct nfs4_exception exception = { };
2393 err = nfs4_handle_exception(server,
2394 _nfs4_proc_getattr(server, fhandle, fattr),
2396 } while (exception.retry);
2401 * The file is not closed if it is opened due to the a request to change
2402 * the size of the file. The open call will not be needed once the
2403 * VFS layer lookup-intents are implemented.
2405 * Close is called when the inode is destroyed.
2406 * If we haven't opened the file for O_WRONLY, we
2407 * need to in the size_change case to obtain a stateid.
2410 * Because OPEN is always done by name in nfsv4, it is
2411 * possible that we opened a different file by the same
2412 * name. We can recognize this race condition, but we
2413 * can't do anything about it besides returning an error.
2415 * This will be fixed with VFS changes (lookup-intent).
2418 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2419 struct iattr *sattr)
2421 struct inode *inode = dentry->d_inode;
2422 struct rpc_cred *cred = NULL;
2423 struct nfs4_state *state = NULL;
2426 if (pnfs_ld_layoutret_on_setattr(inode))
2427 pnfs_return_layout(inode);
2429 nfs_fattr_init(fattr);
2431 /* Search for an existing open(O_WRITE) file */
2432 if (sattr->ia_valid & ATTR_FILE) {
2433 struct nfs_open_context *ctx;
2435 ctx = nfs_file_open_context(sattr->ia_file);
2442 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2444 nfs_setattr_update_inode(inode, sattr);
2448 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2449 const struct qstr *name, struct nfs_fh *fhandle,
2450 struct nfs_fattr *fattr)
2452 struct nfs_server *server = NFS_SERVER(dir);
2454 struct nfs4_lookup_arg args = {
2455 .bitmask = server->attr_bitmask,
2456 .dir_fh = NFS_FH(dir),
2459 struct nfs4_lookup_res res = {
2464 struct rpc_message msg = {
2465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2470 nfs_fattr_init(fattr);
2472 dprintk("NFS call lookup %s\n", name->name);
2473 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2474 dprintk("NFS reply lookup: %d\n", status);
2478 void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2480 memset(fh, 0, sizeof(struct nfs_fh));
2481 fattr->fsid.major = 1;
2482 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2483 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2484 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2488 static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2489 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2491 struct nfs4_exception exception = { };
2496 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2498 case -NFS4ERR_BADNAME:
2500 case -NFS4ERR_MOVED:
2501 return nfs4_get_referral(dir, name, fattr, fhandle);
2502 case -NFS4ERR_WRONGSEC:
2503 nfs_fixup_secinfo_attributes(fattr, fhandle);
2505 err = nfs4_handle_exception(NFS_SERVER(dir),
2506 status, &exception);
2507 } while (exception.retry);
2511 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2513 struct nfs_server *server = NFS_SERVER(inode);
2514 struct nfs4_accessargs args = {
2515 .fh = NFS_FH(inode),
2516 .bitmask = server->attr_bitmask,
2518 struct nfs4_accessres res = {
2521 struct rpc_message msg = {
2522 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2525 .rpc_cred = entry->cred,
2527 int mode = entry->mask;
2531 * Determine which access bits we want to ask for...
2533 if (mode & MAY_READ)
2534 args.access |= NFS4_ACCESS_READ;
2535 if (S_ISDIR(inode->i_mode)) {
2536 if (mode & MAY_WRITE)
2537 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2538 if (mode & MAY_EXEC)
2539 args.access |= NFS4_ACCESS_LOOKUP;
2541 if (mode & MAY_WRITE)
2542 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2543 if (mode & MAY_EXEC)
2544 args.access |= NFS4_ACCESS_EXECUTE;
2547 res.fattr = nfs_alloc_fattr();
2548 if (res.fattr == NULL)
2551 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2554 if (res.access & NFS4_ACCESS_READ)
2555 entry->mask |= MAY_READ;
2556 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2557 entry->mask |= MAY_WRITE;
2558 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2559 entry->mask |= MAY_EXEC;
2560 nfs_refresh_inode(inode, res.fattr);
2562 nfs_free_fattr(res.fattr);
2566 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2568 struct nfs4_exception exception = { };
2571 err = nfs4_handle_exception(NFS_SERVER(inode),
2572 _nfs4_proc_access(inode, entry),
2574 } while (exception.retry);
2579 * TODO: For the time being, we don't try to get any attributes
2580 * along with any of the zero-copy operations READ, READDIR,
2583 * In the case of the first three, we want to put the GETATTR
2584 * after the read-type operation -- this is because it is hard
2585 * to predict the length of a GETATTR response in v4, and thus
2586 * align the READ data correctly. This means that the GETATTR
2587 * may end up partially falling into the page cache, and we should
2588 * shift it into the 'tail' of the xdr_buf before processing.
2589 * To do this efficiently, we need to know the total length
2590 * of data received, which doesn't seem to be available outside
2593 * In the case of WRITE, we also want to put the GETATTR after
2594 * the operation -- in this case because we want to make sure
2595 * we get the post-operation mtime and size. This means that
2596 * we can't use xdr_encode_pages() as written: we need a variant
2597 * of it which would leave room in the 'tail' iovec.
2599 * Both of these changes to the XDR layer would in fact be quite
2600 * minor, but I decided to leave them for a subsequent patch.
2602 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2603 unsigned int pgbase, unsigned int pglen)
2605 struct nfs4_readlink args = {
2606 .fh = NFS_FH(inode),
2611 struct nfs4_readlink_res res;
2612 struct rpc_message msg = {
2613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2618 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2621 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2622 unsigned int pgbase, unsigned int pglen)
2624 struct nfs4_exception exception = { };
2627 err = nfs4_handle_exception(NFS_SERVER(inode),
2628 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2630 } while (exception.retry);
2636 * We will need to arrange for the VFS layer to provide an atomic open.
2637 * Until then, this create/open method is prone to inefficiency and race
2638 * conditions due to the lookup, create, and open VFS calls from sys_open()
2639 * placed on the wire.
2641 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2642 * The file will be opened again in the subsequent VFS open call
2643 * (nfs4_proc_file_open).
2645 * The open for read will just hang around to be used by any process that
2646 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2650 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2651 int flags, struct nfs_open_context *ctx)
2653 struct dentry *de = dentry;
2654 struct nfs4_state *state;
2655 struct rpc_cred *cred = NULL;
2664 sattr->ia_mode &= ~current_umask();
2665 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
2667 if (IS_ERR(state)) {
2668 status = PTR_ERR(state);
2671 d_add(dentry, igrab(state->inode));
2672 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2676 nfs4_close_sync(state, fmode);
2681 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2683 struct nfs_server *server = NFS_SERVER(dir);
2684 struct nfs_removeargs args = {
2686 .name.len = name->len,
2687 .name.name = name->name,
2688 .bitmask = server->attr_bitmask,
2690 struct nfs_removeres res = {
2693 struct rpc_message msg = {
2694 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2698 int status = -ENOMEM;
2700 res.dir_attr = nfs_alloc_fattr();
2701 if (res.dir_attr == NULL)
2704 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2706 update_changeattr(dir, &res.cinfo);
2707 nfs_post_op_update_inode(dir, res.dir_attr);
2709 nfs_free_fattr(res.dir_attr);
2714 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2716 struct nfs4_exception exception = { };
2719 err = nfs4_handle_exception(NFS_SERVER(dir),
2720 _nfs4_proc_remove(dir, name),
2722 } while (exception.retry);
2726 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2728 struct nfs_server *server = NFS_SERVER(dir);
2729 struct nfs_removeargs *args = msg->rpc_argp;
2730 struct nfs_removeres *res = msg->rpc_resp;
2732 args->bitmask = server->cache_consistency_bitmask;
2733 res->server = server;
2734 res->seq_res.sr_slot = NULL;
2735 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2738 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2740 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2742 if (!nfs4_sequence_done(task, &res->seq_res))
2744 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2746 update_changeattr(dir, &res->cinfo);
2747 nfs_post_op_update_inode(dir, res->dir_attr);
2751 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2753 struct nfs_server *server = NFS_SERVER(dir);
2754 struct nfs_renameargs *arg = msg->rpc_argp;
2755 struct nfs_renameres *res = msg->rpc_resp;
2757 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2758 arg->bitmask = server->attr_bitmask;
2759 res->server = server;
2762 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2763 struct inode *new_dir)
2765 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2767 if (!nfs4_sequence_done(task, &res->seq_res))
2769 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2772 update_changeattr(old_dir, &res->old_cinfo);
2773 nfs_post_op_update_inode(old_dir, res->old_fattr);
2774 update_changeattr(new_dir, &res->new_cinfo);
2775 nfs_post_op_update_inode(new_dir, res->new_fattr);
2779 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2780 struct inode *new_dir, struct qstr *new_name)
2782 struct nfs_server *server = NFS_SERVER(old_dir);
2783 struct nfs_renameargs arg = {
2784 .old_dir = NFS_FH(old_dir),
2785 .new_dir = NFS_FH(new_dir),
2786 .old_name = old_name,
2787 .new_name = new_name,
2788 .bitmask = server->attr_bitmask,
2790 struct nfs_renameres res = {
2793 struct rpc_message msg = {
2794 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2798 int status = -ENOMEM;
2800 res.old_fattr = nfs_alloc_fattr();
2801 res.new_fattr = nfs_alloc_fattr();
2802 if (res.old_fattr == NULL || res.new_fattr == NULL)
2805 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2807 update_changeattr(old_dir, &res.old_cinfo);
2808 nfs_post_op_update_inode(old_dir, res.old_fattr);
2809 update_changeattr(new_dir, &res.new_cinfo);
2810 nfs_post_op_update_inode(new_dir, res.new_fattr);
2813 nfs_free_fattr(res.new_fattr);
2814 nfs_free_fattr(res.old_fattr);
2818 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2819 struct inode *new_dir, struct qstr *new_name)
2821 struct nfs4_exception exception = { };
2824 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2825 _nfs4_proc_rename(old_dir, old_name,
2828 } while (exception.retry);
2832 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2834 struct nfs_server *server = NFS_SERVER(inode);
2835 struct nfs4_link_arg arg = {
2836 .fh = NFS_FH(inode),
2837 .dir_fh = NFS_FH(dir),
2839 .bitmask = server->attr_bitmask,
2841 struct nfs4_link_res res = {
2844 struct rpc_message msg = {
2845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2849 int status = -ENOMEM;
2851 res.fattr = nfs_alloc_fattr();
2852 res.dir_attr = nfs_alloc_fattr();
2853 if (res.fattr == NULL || res.dir_attr == NULL)
2856 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2858 update_changeattr(dir, &res.cinfo);
2859 nfs_post_op_update_inode(dir, res.dir_attr);
2860 nfs_post_op_update_inode(inode, res.fattr);
2863 nfs_free_fattr(res.dir_attr);
2864 nfs_free_fattr(res.fattr);
2868 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2870 struct nfs4_exception exception = { };
2873 err = nfs4_handle_exception(NFS_SERVER(inode),
2874 _nfs4_proc_link(inode, dir, name),
2876 } while (exception.retry);
2880 struct nfs4_createdata {
2881 struct rpc_message msg;
2882 struct nfs4_create_arg arg;
2883 struct nfs4_create_res res;
2885 struct nfs_fattr fattr;
2886 struct nfs_fattr dir_fattr;
2889 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2890 struct qstr *name, struct iattr *sattr, u32 ftype)
2892 struct nfs4_createdata *data;
2894 data = kzalloc(sizeof(*data), GFP_KERNEL);
2896 struct nfs_server *server = NFS_SERVER(dir);
2898 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2899 data->msg.rpc_argp = &data->arg;
2900 data->msg.rpc_resp = &data->res;
2901 data->arg.dir_fh = NFS_FH(dir);
2902 data->arg.server = server;
2903 data->arg.name = name;
2904 data->arg.attrs = sattr;
2905 data->arg.ftype = ftype;
2906 data->arg.bitmask = server->attr_bitmask;
2907 data->res.server = server;
2908 data->res.fh = &data->fh;
2909 data->res.fattr = &data->fattr;
2910 data->res.dir_fattr = &data->dir_fattr;
2911 nfs_fattr_init(data->res.fattr);
2912 nfs_fattr_init(data->res.dir_fattr);
2917 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2919 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
2920 &data->arg.seq_args, &data->res.seq_res, 1);
2922 update_changeattr(dir, &data->res.dir_cinfo);
2923 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2924 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2929 static void nfs4_free_createdata(struct nfs4_createdata *data)
2934 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2935 struct page *page, unsigned int len, struct iattr *sattr)
2937 struct nfs4_createdata *data;
2938 int status = -ENAMETOOLONG;
2940 if (len > NFS4_MAXPATHLEN)
2944 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2948 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2949 data->arg.u.symlink.pages = &page;
2950 data->arg.u.symlink.len = len;
2952 status = nfs4_do_create(dir, dentry, data);
2954 nfs4_free_createdata(data);
2959 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2960 struct page *page, unsigned int len, struct iattr *sattr)
2962 struct nfs4_exception exception = { };
2965 err = nfs4_handle_exception(NFS_SERVER(dir),
2966 _nfs4_proc_symlink(dir, dentry, page,
2969 } while (exception.retry);
2973 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2974 struct iattr *sattr)
2976 struct nfs4_createdata *data;
2977 int status = -ENOMEM;
2979 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2983 status = nfs4_do_create(dir, dentry, data);
2985 nfs4_free_createdata(data);
2990 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2991 struct iattr *sattr)
2993 struct nfs4_exception exception = { };
2996 sattr->ia_mode &= ~current_umask();
2998 err = nfs4_handle_exception(NFS_SERVER(dir),
2999 _nfs4_proc_mkdir(dir, dentry, sattr),
3001 } while (exception.retry);
3005 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3006 u64 cookie, struct page **pages, unsigned int count, int plus)
3008 struct inode *dir = dentry->d_inode;
3009 struct nfs4_readdir_arg args = {
3014 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3017 struct nfs4_readdir_res res;
3018 struct rpc_message msg = {
3019 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3026 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3027 dentry->d_parent->d_name.name,
3028 dentry->d_name.name,
3029 (unsigned long long)cookie);
3030 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3031 res.pgbase = args.pgbase;
3032 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3034 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3035 status += args.pgbase;
3038 nfs_invalidate_atime(dir);
3040 dprintk("%s: returns %d\n", __func__, status);
3044 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3045 u64 cookie, struct page **pages, unsigned int count, int plus)
3047 struct nfs4_exception exception = { };
3050 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3051 _nfs4_proc_readdir(dentry, cred, cookie,
3052 pages, count, plus),
3054 } while (exception.retry);
3058 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3059 struct iattr *sattr, dev_t rdev)
3061 struct nfs4_createdata *data;
3062 int mode = sattr->ia_mode;
3063 int status = -ENOMEM;
3065 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3066 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3068 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3073 data->arg.ftype = NF4FIFO;
3074 else if (S_ISBLK(mode)) {
3075 data->arg.ftype = NF4BLK;
3076 data->arg.u.device.specdata1 = MAJOR(rdev);
3077 data->arg.u.device.specdata2 = MINOR(rdev);
3079 else if (S_ISCHR(mode)) {
3080 data->arg.ftype = NF4CHR;
3081 data->arg.u.device.specdata1 = MAJOR(rdev);
3082 data->arg.u.device.specdata2 = MINOR(rdev);
3085 status = nfs4_do_create(dir, dentry, data);
3087 nfs4_free_createdata(data);
3092 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3093 struct iattr *sattr, dev_t rdev)
3095 struct nfs4_exception exception = { };
3098 sattr->ia_mode &= ~current_umask();
3100 err = nfs4_handle_exception(NFS_SERVER(dir),
3101 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3103 } while (exception.retry);
3107 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3108 struct nfs_fsstat *fsstat)
3110 struct nfs4_statfs_arg args = {
3112 .bitmask = server->attr_bitmask,
3114 struct nfs4_statfs_res res = {
3117 struct rpc_message msg = {
3118 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3123 nfs_fattr_init(fsstat->fattr);
3124 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3127 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3129 struct nfs4_exception exception = { };
3132 err = nfs4_handle_exception(server,
3133 _nfs4_proc_statfs(server, fhandle, fsstat),
3135 } while (exception.retry);
3139 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3140 struct nfs_fsinfo *fsinfo)
3142 struct nfs4_fsinfo_arg args = {
3144 .bitmask = server->attr_bitmask,
3146 struct nfs4_fsinfo_res res = {
3149 struct rpc_message msg = {
3150 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3155 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3158 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3160 struct nfs4_exception exception = { };
3164 err = nfs4_handle_exception(server,
3165 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3167 } while (exception.retry);
3171 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3173 nfs_fattr_init(fsinfo->fattr);
3174 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3177 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3178 struct nfs_pathconf *pathconf)
3180 struct nfs4_pathconf_arg args = {
3182 .bitmask = server->attr_bitmask,
3184 struct nfs4_pathconf_res res = {
3185 .pathconf = pathconf,
3187 struct rpc_message msg = {
3188 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3193 /* None of the pathconf attributes are mandatory to implement */
3194 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3195 memset(pathconf, 0, sizeof(*pathconf));
3199 nfs_fattr_init(pathconf->fattr);
3200 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3203 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3204 struct nfs_pathconf *pathconf)
3206 struct nfs4_exception exception = { };
3210 err = nfs4_handle_exception(server,
3211 _nfs4_proc_pathconf(server, fhandle, pathconf),
3213 } while (exception.retry);
3217 void __nfs4_read_done_cb(struct nfs_read_data *data)
3219 nfs_invalidate_atime(data->inode);
3222 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3224 struct nfs_server *server = NFS_SERVER(data->inode);
3226 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3227 rpc_restart_call_prepare(task);
3231 __nfs4_read_done_cb(data);
3232 if (task->tk_status > 0)
3233 renew_lease(server, data->timestamp);
3237 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3240 dprintk("--> %s\n", __func__);
3242 if (!nfs4_sequence_done(task, &data->res.seq_res))
3245 return data->read_done_cb ? data->read_done_cb(task, data) :
3246 nfs4_read_done_cb(task, data);
3249 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3251 data->timestamp = jiffies;
3252 data->read_done_cb = nfs4_read_done_cb;
3253 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3256 /* Reset the the nfs_read_data to send the read to the MDS. */
3257 void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3259 dprintk("%s Reset task for i/o through\n", __func__);
3260 put_lseg(data->lseg);
3262 /* offsets will differ in the dense stripe case */
3263 data->args.offset = data->mds_offset;
3264 data->ds_clp = NULL;
3265 data->args.fh = NFS_FH(data->inode);
3266 data->read_done_cb = nfs4_read_done_cb;
3267 task->tk_ops = data->mds_ops;
3268 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3270 EXPORT_SYMBOL_GPL(nfs4_reset_read);
3272 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3274 struct inode *inode = data->inode;
3276 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3277 rpc_restart_call_prepare(task);
3280 if (task->tk_status >= 0) {
3281 renew_lease(NFS_SERVER(inode), data->timestamp);
3282 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3287 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3289 if (!nfs4_sequence_done(task, &data->res.seq_res))
3291 return data->write_done_cb ? data->write_done_cb(task, data) :
3292 nfs4_write_done_cb(task, data);
3295 /* Reset the the nfs_write_data to send the write to the MDS. */
3296 void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3298 dprintk("%s Reset task for i/o through\n", __func__);
3299 put_lseg(data->lseg);
3301 data->ds_clp = NULL;
3302 data->write_done_cb = nfs4_write_done_cb;
3303 data->args.fh = NFS_FH(data->inode);
3304 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3305 data->args.offset = data->mds_offset;
3306 data->res.fattr = &data->fattr;
3307 task->tk_ops = data->mds_ops;
3308 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3310 EXPORT_SYMBOL_GPL(nfs4_reset_write);
3312 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3314 struct nfs_server *server = NFS_SERVER(data->inode);
3317 data->args.bitmask = NULL;
3318 data->res.fattr = NULL;
3320 data->args.bitmask = server->cache_consistency_bitmask;
3321 if (!data->write_done_cb)
3322 data->write_done_cb = nfs4_write_done_cb;
3323 data->res.server = server;
3324 data->timestamp = jiffies;
3326 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3329 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3331 struct inode *inode = data->inode;
3333 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3334 rpc_restart_call_prepare(task);
3337 nfs_refresh_inode(inode, data->res.fattr);
3341 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3343 if (!nfs4_sequence_done(task, &data->res.seq_res))
3345 return data->write_done_cb(task, data);
3348 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3350 struct nfs_server *server = NFS_SERVER(data->inode);
3353 data->args.bitmask = NULL;
3354 data->res.fattr = NULL;
3356 data->args.bitmask = server->cache_consistency_bitmask;
3357 if (!data->write_done_cb)
3358 data->write_done_cb = nfs4_commit_done_cb;
3359 data->res.server = server;
3360 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3363 struct nfs4_renewdata {
3364 struct nfs_client *client;
3365 unsigned long timestamp;
3369 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3370 * standalone procedure for queueing an asynchronous RENEW.
3372 static void nfs4_renew_release(void *calldata)
3374 struct nfs4_renewdata *data = calldata;
3375 struct nfs_client *clp = data->client;
3377 if (atomic_read(&clp->cl_count) > 1)
3378 nfs4_schedule_state_renewal(clp);
3379 nfs_put_client(clp);
3383 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3385 struct nfs4_renewdata *data = calldata;
3386 struct nfs_client *clp = data->client;
3387 unsigned long timestamp = data->timestamp;
3389 if (task->tk_status < 0) {
3390 /* Unless we're shutting down, schedule state recovery! */
3391 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3393 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3394 nfs4_schedule_lease_recovery(clp);
3397 nfs4_schedule_path_down_recovery(clp);
3399 do_renew_lease(clp, timestamp);
3402 static const struct rpc_call_ops nfs4_renew_ops = {
3403 .rpc_call_done = nfs4_renew_done,
3404 .rpc_release = nfs4_renew_release,
3407 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3409 struct rpc_message msg = {
3410 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3414 struct nfs4_renewdata *data;
3416 if (renew_flags == 0)
3418 if (!atomic_inc_not_zero(&clp->cl_count))
3420 data = kmalloc(sizeof(*data), GFP_NOFS);
3424 data->timestamp = jiffies;
3425 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3426 &nfs4_renew_ops, data);
3429 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3431 struct rpc_message msg = {
3432 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3436 unsigned long now = jiffies;
3439 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3442 do_renew_lease(clp, now);
3446 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3448 return (server->caps & NFS_CAP_ACLS)
3449 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3450 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3453 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3454 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3457 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3459 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3460 struct page **pages, unsigned int *pgbase)
3462 struct page *newpage, **spages;
3468 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3469 newpage = alloc_page(GFP_KERNEL);
3471 if (newpage == NULL)
3473 memcpy(page_address(newpage), buf, len);
3478 } while (buflen != 0);
3484 __free_page(spages[rc-1]);
3488 struct nfs4_cached_acl {
3494 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3496 struct nfs_inode *nfsi = NFS_I(inode);
3498 spin_lock(&inode->i_lock);
3499 kfree(nfsi->nfs4_acl);
3500 nfsi->nfs4_acl = acl;
3501 spin_unlock(&inode->i_lock);
3504 static void nfs4_zap_acl_attr(struct inode *inode)
3506 nfs4_set_cached_acl(inode, NULL);
3509 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3511 struct nfs_inode *nfsi = NFS_I(inode);
3512 struct nfs4_cached_acl *acl;
3515 spin_lock(&inode->i_lock);
3516 acl = nfsi->nfs4_acl;
3519 if (buf == NULL) /* user is just asking for length */
3521 if (acl->cached == 0)
3523 ret = -ERANGE; /* see getxattr(2) man page */
3524 if (acl->len > buflen)
3526 memcpy(buf, acl->data, acl->len);
3530 spin_unlock(&inode->i_lock);
3534 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3536 struct nfs4_cached_acl *acl;
3538 if (pages && acl_len <= PAGE_SIZE) {
3539 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3543 _copy_from_pages(acl->data, pages, pgbase, acl_len);
3545 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3552 nfs4_set_cached_acl(inode, acl);
3556 * The getxattr API returns the required buffer length when called with a
3557 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3558 * the required buf. On a NULL buf, we send a page of data to the server
3559 * guessing that the ACL request can be serviced by a page. If so, we cache
3560 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3561 * the cache. If not so, we throw away the page, and cache the required
3562 * length. The next getxattr call will then produce another round trip to
3563 * the server, this time with the input buf of the required size.
3565 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3567 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3568 struct nfs_getaclargs args = {
3569 .fh = NFS_FH(inode),
3573 struct nfs_getaclres res = {
3576 struct rpc_message msg = {
3577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3581 int ret = -ENOMEM, npages, i;
3584 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3585 /* As long as we're doing a round trip to the server anyway,
3586 * let's be prepared for a page of acl data. */
3590 /* Add an extra page to handle the bitmap returned */
3593 for (i = 0; i < npages; i++) {
3594 pages[i] = alloc_page(GFP_KERNEL);
3599 /* for decoding across pages */
3600 res.acl_scratch = alloc_page(GFP_KERNEL);
3601 if (!res.acl_scratch)
3604 args.acl_len = npages * PAGE_SIZE;
3605 args.acl_pgbase = 0;
3607 /* Let decode_getfacl know not to fail if the ACL data is larger than
3608 * the page we send as a guess */
3610 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3612 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3613 __func__, buf, buflen, npages, args.acl_len);
3614 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3615 &msg, &args.seq_args, &res.seq_res, 0);
3619 acl_len = res.acl_len - res.acl_data_offset;
3620 if (acl_len > args.acl_len)
3621 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
3623 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
3627 if (acl_len > buflen)
3629 _copy_from_pages(buf, pages, res.acl_data_offset,
3634 for (i = 0; i < npages; i++)
3636 __free_page(pages[i]);
3637 if (res.acl_scratch)
3638 __free_page(res.acl_scratch);
3642 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3644 struct nfs4_exception exception = { };
3647 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3650 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3651 } while (exception.retry);
3655 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3657 struct nfs_server *server = NFS_SERVER(inode);
3660 if (!nfs4_server_supports_acls(server))
3662 ret = nfs_revalidate_inode(server, inode);
3665 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3666 nfs_zap_acl_cache(inode);
3667 ret = nfs4_read_cached_acl(inode, buf, buflen);
3669 /* -ENOENT is returned if there is no ACL or if there is an ACL
3670 * but no cached acl data, just the acl length */
3672 return nfs4_get_acl_uncached(inode, buf, buflen);
3675 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3677 struct nfs_server *server = NFS_SERVER(inode);
3678 struct page *pages[NFS4ACL_MAXPAGES];
3679 struct nfs_setaclargs arg = {
3680 .fh = NFS_FH(inode),
3684 struct nfs_setaclres res;
3685 struct rpc_message msg = {
3686 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3692 if (!nfs4_server_supports_acls(server))
3694 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3697 nfs_inode_return_delegation(inode);
3698 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3701 * Free each page after tx, so the only ref left is
3702 * held by the network stack
3705 put_page(pages[i-1]);
3708 * Acl update can result in inode attribute update.
3709 * so mark the attribute cache invalid.
3711 spin_lock(&inode->i_lock);
3712 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3713 spin_unlock(&inode->i_lock);
3714 nfs_access_zap_cache(inode);
3715 nfs_zap_acl_cache(inode);
3719 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3721 struct nfs4_exception exception = { };
3724 err = nfs4_handle_exception(NFS_SERVER(inode),
3725 __nfs4_proc_set_acl(inode, buf, buflen),
3727 } while (exception.retry);
3732 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3734 struct nfs_client *clp = server->nfs_client;
3736 if (task->tk_status >= 0)
3738 switch(task->tk_status) {
3739 case -NFS4ERR_DELEG_REVOKED:
3740 case -NFS4ERR_ADMIN_REVOKED:
3741 case -NFS4ERR_BAD_STATEID:
3743 nfs_remove_bad_delegation(state->inode);
3744 case -NFS4ERR_OPENMODE:
3747 nfs4_schedule_stateid_recovery(server, state);