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:
100 dprintk("%s could not handle NFSv4 error %d\n",
108 * This is our standard bitmap for GETATTR requests.
110 const u32 nfs4_fattr_bitmap[2] = {
112 | FATTR4_WORD0_CHANGE
115 | FATTR4_WORD0_FILEID,
117 | FATTR4_WORD1_NUMLINKS
119 | FATTR4_WORD1_OWNER_GROUP
120 | FATTR4_WORD1_RAWDEV
121 | FATTR4_WORD1_SPACE_USED
122 | FATTR4_WORD1_TIME_ACCESS
123 | FATTR4_WORD1_TIME_METADATA
124 | FATTR4_WORD1_TIME_MODIFY
127 const u32 nfs4_statfs_bitmap[2] = {
128 FATTR4_WORD0_FILES_AVAIL
129 | FATTR4_WORD0_FILES_FREE
130 | FATTR4_WORD0_FILES_TOTAL,
131 FATTR4_WORD1_SPACE_AVAIL
132 | FATTR4_WORD1_SPACE_FREE
133 | FATTR4_WORD1_SPACE_TOTAL
136 const u32 nfs4_pathconf_bitmap[2] = {
138 | FATTR4_WORD0_MAXNAME,
142 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
143 | FATTR4_WORD0_MAXREAD
144 | FATTR4_WORD0_MAXWRITE
145 | FATTR4_WORD0_LEASE_TIME,
146 FATTR4_WORD1_TIME_DELTA
147 | FATTR4_WORD1_FS_LAYOUT_TYPES,
148 FATTR4_WORD2_LAYOUT_BLKSIZE
151 const u32 nfs4_fs_locations_bitmap[2] = {
153 | FATTR4_WORD0_CHANGE
156 | FATTR4_WORD0_FILEID
157 | FATTR4_WORD0_FS_LOCATIONS,
159 | FATTR4_WORD1_NUMLINKS
161 | FATTR4_WORD1_OWNER_GROUP
162 | FATTR4_WORD1_RAWDEV
163 | FATTR4_WORD1_SPACE_USED
164 | FATTR4_WORD1_TIME_ACCESS
165 | FATTR4_WORD1_TIME_METADATA
166 | FATTR4_WORD1_TIME_MODIFY
167 | FATTR4_WORD1_MOUNTED_ON_FILEID
170 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
171 struct nfs4_readdir_arg *readdir)
175 BUG_ON(readdir->count < 80);
177 readdir->cookie = cookie;
178 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
183 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
188 * NFSv4 servers do not return entries for '.' and '..'
189 * Therefore, we fake these entries here. We let '.'
190 * have cookie 0 and '..' have cookie 1. Note that
191 * when talking to the server, we always send cookie 0
194 start = p = kmap_atomic(*readdir->pages, KM_USER0);
197 *p++ = xdr_one; /* next */
198 *p++ = xdr_zero; /* cookie, first word */
199 *p++ = xdr_one; /* cookie, second word */
200 *p++ = xdr_one; /* entry len */
201 memcpy(p, ".\0\0\0", 4); /* entry */
203 *p++ = xdr_one; /* bitmap length */
204 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
205 *p++ = htonl(8); /* attribute buffer length */
206 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
209 *p++ = xdr_one; /* next */
210 *p++ = xdr_zero; /* cookie, first word */
211 *p++ = xdr_two; /* cookie, second word */
212 *p++ = xdr_two; /* entry len */
213 memcpy(p, "..\0\0", 4); /* entry */
215 *p++ = xdr_one; /* bitmap length */
216 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
217 *p++ = htonl(8); /* attribute buffer length */
218 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
220 readdir->pgbase = (char *)p - (char *)start;
221 readdir->count -= readdir->pgbase;
222 kunmap_atomic(start, KM_USER0);
225 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
231 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
232 nfs_wait_bit_killable, TASK_KILLABLE);
236 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
243 *timeout = NFS4_POLL_RETRY_MIN;
244 if (*timeout > NFS4_POLL_RETRY_MAX)
245 *timeout = NFS4_POLL_RETRY_MAX;
246 schedule_timeout_killable(*timeout);
247 if (fatal_signal_pending(current))
253 /* This is the error handling routine for processes that are allowed
256 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
258 struct nfs_client *clp = server->nfs_client;
259 struct nfs4_state *state = exception->state;
260 struct inode *inode = exception->inode;
263 exception->retry = 0;
267 case -NFS4ERR_OPENMODE:
268 if (nfs_have_delegation(inode, FMODE_READ)) {
269 nfs_inode_return_delegation(inode);
270 exception->retry = 1;
275 nfs4_schedule_stateid_recovery(server, state);
276 goto wait_on_recovery;
277 case -NFS4ERR_DELEG_REVOKED:
278 case -NFS4ERR_ADMIN_REVOKED:
279 case -NFS4ERR_BAD_STATEID:
281 nfs_remove_bad_delegation(state->inode);
284 nfs4_schedule_stateid_recovery(server, state);
285 goto wait_on_recovery;
286 case -NFS4ERR_EXPIRED:
288 nfs4_schedule_stateid_recovery(server, state);
289 case -NFS4ERR_STALE_STATEID:
290 case -NFS4ERR_STALE_CLIENTID:
291 nfs4_schedule_lease_recovery(clp);
292 goto wait_on_recovery;
293 #if defined(CONFIG_NFS_V4_1)
294 case -NFS4ERR_BADSESSION:
295 case -NFS4ERR_BADSLOT:
296 case -NFS4ERR_BAD_HIGH_SLOT:
297 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
298 case -NFS4ERR_DEADSESSION:
299 case -NFS4ERR_SEQ_FALSE_RETRY:
300 case -NFS4ERR_SEQ_MISORDERED:
301 dprintk("%s ERROR: %d Reset session\n", __func__,
303 nfs4_schedule_session_recovery(clp->cl_session);
304 exception->retry = 1;
306 #endif /* defined(CONFIG_NFS_V4_1) */
307 case -NFS4ERR_FILE_OPEN:
308 if (exception->timeout > HZ) {
309 /* We have retried a decent amount, time to
318 ret = nfs4_delay(server->client, &exception->timeout);
321 case -NFS4ERR_RETRY_UNCACHED_REP:
322 case -NFS4ERR_OLD_STATEID:
323 exception->retry = 1;
325 case -NFS4ERR_BADOWNER:
326 /* The following works around a Linux server bug! */
327 case -NFS4ERR_BADNAME:
328 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
329 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
330 exception->retry = 1;
331 printk(KERN_WARNING "NFS: v4 server %s "
332 "does not accept raw "
334 "Reenabling the idmapper.\n",
335 server->nfs_client->cl_hostname);
338 /* We failed to handle the error */
339 return nfs4_map_errors(ret);
341 ret = nfs4_wait_clnt_recover(clp);
343 exception->retry = 1;
348 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
350 spin_lock(&clp->cl_lock);
351 if (time_before(clp->cl_last_renewal,timestamp))
352 clp->cl_last_renewal = timestamp;
353 spin_unlock(&clp->cl_lock);
356 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
358 do_renew_lease(server->nfs_client, timestamp);
361 #if defined(CONFIG_NFS_V4_1)
364 * nfs4_free_slot - free a slot and efficiently update slot table.
366 * freeing a slot is trivially done by clearing its respective bit
368 * If the freed slotid equals highest_used_slotid we want to update it
369 * so that the server would be able to size down the slot table if needed,
370 * otherwise we know that the highest_used_slotid is still in use.
371 * When updating highest_used_slotid there may be "holes" in the bitmap
372 * so we need to scan down from highest_used_slotid to 0 looking for the now
373 * highest slotid in use.
374 * If none found, highest_used_slotid is set to -1.
376 * Must be called while holding tbl->slot_tbl_lock
379 nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
381 int free_slotid = free_slot - tbl->slots;
382 int slotid = free_slotid;
384 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
385 /* clear used bit in bitmap */
386 __clear_bit(slotid, tbl->used_slots);
388 /* update highest_used_slotid when it is freed */
389 if (slotid == tbl->highest_used_slotid) {
390 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
391 if (slotid < tbl->max_slots)
392 tbl->highest_used_slotid = slotid;
394 tbl->highest_used_slotid = -1;
396 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
397 free_slotid, tbl->highest_used_slotid);
401 * Signal state manager thread if session fore channel is drained
403 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
405 struct rpc_task *task;
407 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
408 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
410 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
414 if (ses->fc_slot_table.highest_used_slotid != -1)
417 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
418 complete(&ses->fc_slot_table.complete);
422 * Signal state manager thread if session back channel is drained
424 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
426 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
427 ses->bc_slot_table.highest_used_slotid != -1)
429 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
430 complete(&ses->bc_slot_table.complete);
433 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
435 struct nfs4_slot_table *tbl;
437 tbl = &res->sr_session->fc_slot_table;
439 /* just wake up the next guy waiting since
440 * we may have not consumed a slot after all */
441 dprintk("%s: No slot\n", __func__);
445 spin_lock(&tbl->slot_tbl_lock);
446 nfs4_free_slot(tbl, res->sr_slot);
447 nfs4_check_drain_fc_complete(res->sr_session);
448 spin_unlock(&tbl->slot_tbl_lock);
452 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
454 unsigned long timestamp;
455 struct nfs_client *clp;
458 * sr_status remains 1 if an RPC level error occurred. The server
459 * may or may not have processed the sequence operation..
460 * Proceed as if the server received and processed the sequence
463 if (res->sr_status == 1)
464 res->sr_status = NFS_OK;
466 /* don't increment the sequence number if the task wasn't sent */
467 if (!RPC_WAS_SENT(task))
470 /* Check the SEQUENCE operation status */
471 switch (res->sr_status) {
473 /* Update the slot's sequence and clientid lease timer */
474 ++res->sr_slot->seq_nr;
475 timestamp = res->sr_renewal_time;
476 clp = res->sr_session->clp;
477 do_renew_lease(clp, timestamp);
478 /* Check sequence flags */
479 if (res->sr_status_flags != 0)
480 nfs4_schedule_lease_recovery(clp);
483 /* The server detected a resend of the RPC call and
484 * returned NFS4ERR_DELAY as per Section 2.10.6.2
487 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
489 res->sr_slot - res->sr_session->fc_slot_table.slots,
490 res->sr_slot->seq_nr);
493 /* Just update the slot sequence no. */
494 ++res->sr_slot->seq_nr;
497 /* The session may be reset by one of the error handlers. */
498 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
499 nfs41_sequence_free_slot(res);
502 if (!rpc_restart_call(task))
504 rpc_delay(task, NFS4_POLL_RETRY_MAX);
508 static int nfs4_sequence_done(struct rpc_task *task,
509 struct nfs4_sequence_res *res)
511 if (res->sr_session == NULL)
513 return nfs41_sequence_done(task, res);
517 * nfs4_find_slot - efficiently look for a free slot
519 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
520 * If found, we mark the slot as used, update the highest_used_slotid,
521 * and respectively set up the sequence operation args.
522 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
524 * Note: must be called with under the slot_tbl_lock.
527 nfs4_find_slot(struct nfs4_slot_table *tbl)
530 u8 ret_id = NFS4_MAX_SLOT_TABLE;
531 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
533 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
534 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
536 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
537 if (slotid >= tbl->max_slots)
539 __set_bit(slotid, tbl->used_slots);
540 if (slotid > tbl->highest_used_slotid)
541 tbl->highest_used_slotid = slotid;
544 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
545 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
549 int nfs41_setup_sequence(struct nfs4_session *session,
550 struct nfs4_sequence_args *args,
551 struct nfs4_sequence_res *res,
553 struct rpc_task *task)
555 struct nfs4_slot *slot;
556 struct nfs4_slot_table *tbl;
559 dprintk("--> %s\n", __func__);
560 /* slot already allocated? */
561 if (res->sr_slot != NULL)
564 tbl = &session->fc_slot_table;
566 spin_lock(&tbl->slot_tbl_lock);
567 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
568 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
570 * The state manager will wait until the slot table is empty.
571 * Schedule the reset thread
573 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
574 spin_unlock(&tbl->slot_tbl_lock);
575 dprintk("%s Schedule Session Reset\n", __func__);
579 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
580 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
581 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
582 spin_unlock(&tbl->slot_tbl_lock);
583 dprintk("%s enforce FIFO order\n", __func__);
587 slotid = nfs4_find_slot(tbl);
588 if (slotid == NFS4_MAX_SLOT_TABLE) {
589 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
590 spin_unlock(&tbl->slot_tbl_lock);
591 dprintk("<-- %s: no free slots\n", __func__);
594 spin_unlock(&tbl->slot_tbl_lock);
596 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
597 slot = tbl->slots + slotid;
598 args->sa_session = session;
599 args->sa_slotid = slotid;
600 args->sa_cache_this = cache_reply;
602 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
604 res->sr_session = session;
606 res->sr_renewal_time = jiffies;
607 res->sr_status_flags = 0;
609 * sr_status is only set in decode_sequence, and so will remain
610 * set to 1 if an rpc level failure occurs.
615 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
617 int nfs4_setup_sequence(const struct nfs_server *server,
618 struct nfs4_sequence_args *args,
619 struct nfs4_sequence_res *res,
621 struct rpc_task *task)
623 struct nfs4_session *session = nfs4_get_session(server);
626 if (session == NULL) {
627 args->sa_session = NULL;
628 res->sr_session = NULL;
632 dprintk("--> %s clp %p session %p sr_slot %td\n",
633 __func__, session->clp, session, res->sr_slot ?
634 res->sr_slot - session->fc_slot_table.slots : -1);
636 ret = nfs41_setup_sequence(session, args, res, cache_reply,
639 dprintk("<-- %s status=%d\n", __func__, ret);
643 struct nfs41_call_sync_data {
644 const struct nfs_server *seq_server;
645 struct nfs4_sequence_args *seq_args;
646 struct nfs4_sequence_res *seq_res;
650 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
652 struct nfs41_call_sync_data *data = calldata;
654 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
656 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
657 data->seq_res, data->cache_reply, task))
659 rpc_call_start(task);
662 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
664 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
665 nfs41_call_sync_prepare(task, calldata);
668 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
670 struct nfs41_call_sync_data *data = calldata;
672 nfs41_sequence_done(task, data->seq_res);
675 struct rpc_call_ops nfs41_call_sync_ops = {
676 .rpc_call_prepare = nfs41_call_sync_prepare,
677 .rpc_call_done = nfs41_call_sync_done,
680 struct rpc_call_ops nfs41_call_priv_sync_ops = {
681 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
682 .rpc_call_done = nfs41_call_sync_done,
685 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
686 struct nfs_server *server,
687 struct rpc_message *msg,
688 struct nfs4_sequence_args *args,
689 struct nfs4_sequence_res *res,
694 struct rpc_task *task;
695 struct nfs41_call_sync_data data = {
696 .seq_server = server,
699 .cache_reply = cache_reply,
701 struct rpc_task_setup task_setup = {
704 .callback_ops = &nfs41_call_sync_ops,
705 .callback_data = &data
710 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
711 task = rpc_run_task(&task_setup);
715 ret = task->tk_status;
721 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
722 struct nfs_server *server,
723 struct rpc_message *msg,
724 struct nfs4_sequence_args *args,
725 struct nfs4_sequence_res *res,
728 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
732 static int nfs4_sequence_done(struct rpc_task *task,
733 struct nfs4_sequence_res *res)
737 #endif /* CONFIG_NFS_V4_1 */
739 int _nfs4_call_sync(struct rpc_clnt *clnt,
740 struct nfs_server *server,
741 struct rpc_message *msg,
742 struct nfs4_sequence_args *args,
743 struct nfs4_sequence_res *res,
746 args->sa_session = res->sr_session = NULL;
747 return rpc_call_sync(clnt, msg, 0);
751 int nfs4_call_sync(struct rpc_clnt *clnt,
752 struct nfs_server *server,
753 struct rpc_message *msg,
754 struct nfs4_sequence_args *args,
755 struct nfs4_sequence_res *res,
758 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
759 args, res, cache_reply);
762 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
764 struct nfs_inode *nfsi = NFS_I(dir);
766 spin_lock(&dir->i_lock);
767 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
768 if (!cinfo->atomic || cinfo->before != dir->i_version)
769 nfs_force_lookup_revalidate(dir);
770 dir->i_version = cinfo->after;
771 spin_unlock(&dir->i_lock);
774 struct nfs4_opendata {
776 struct nfs_openargs o_arg;
777 struct nfs_openres o_res;
778 struct nfs_open_confirmargs c_arg;
779 struct nfs_open_confirmres c_res;
780 struct nfs_fattr f_attr;
781 struct nfs_fattr dir_attr;
783 struct dentry *dentry;
784 struct nfs4_state_owner *owner;
785 struct nfs4_state *state;
787 unsigned long timestamp;
788 unsigned int rpc_done : 1;
794 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
796 p->o_res.f_attr = &p->f_attr;
797 p->o_res.dir_attr = &p->dir_attr;
798 p->o_res.seqid = p->o_arg.seqid;
799 p->c_res.seqid = p->c_arg.seqid;
800 p->o_res.server = p->o_arg.server;
801 nfs_fattr_init(&p->f_attr);
802 nfs_fattr_init(&p->dir_attr);
805 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
806 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
807 const struct iattr *attrs,
810 struct dentry *parent = dget_parent(dentry);
811 struct inode *dir = parent->d_inode;
812 struct nfs_server *server = NFS_SERVER(dir);
813 struct nfs4_opendata *p;
815 p = kzalloc(sizeof(*p), gfp_mask);
818 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
819 if (p->o_arg.seqid == NULL)
821 nfs_sb_active(dentry->d_sb);
822 p->dentry = dget(dentry);
825 atomic_inc(&sp->so_count);
826 p->o_arg.fh = NFS_FH(dir);
827 p->o_arg.open_flags = flags;
828 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
829 p->o_arg.clientid = server->nfs_client->cl_clientid;
830 p->o_arg.id = sp->so_owner_id.id;
831 p->o_arg.name = &dentry->d_name;
832 p->o_arg.server = server;
833 p->o_arg.bitmask = server->attr_bitmask;
834 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
835 if (flags & O_CREAT) {
838 p->o_arg.u.attrs = &p->attrs;
839 memcpy(&p->attrs, attrs, sizeof(p->attrs));
840 s = (u32 *) p->o_arg.u.verifier.data;
844 p->c_arg.fh = &p->o_res.fh;
845 p->c_arg.stateid = &p->o_res.stateid;
846 p->c_arg.seqid = p->o_arg.seqid;
847 nfs4_init_opendata_res(p);
857 static void nfs4_opendata_free(struct kref *kref)
859 struct nfs4_opendata *p = container_of(kref,
860 struct nfs4_opendata, kref);
861 struct super_block *sb = p->dentry->d_sb;
863 nfs_free_seqid(p->o_arg.seqid);
864 if (p->state != NULL)
865 nfs4_put_open_state(p->state);
866 nfs4_put_state_owner(p->owner);
873 static void nfs4_opendata_put(struct nfs4_opendata *p)
876 kref_put(&p->kref, nfs4_opendata_free);
879 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
883 ret = rpc_wait_for_completion_task(task);
887 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
891 if (open_mode & O_EXCL)
893 switch (mode & (FMODE_READ|FMODE_WRITE)) {
895 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
896 && state->n_rdonly != 0;
899 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
900 && state->n_wronly != 0;
902 case FMODE_READ|FMODE_WRITE:
903 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
904 && state->n_rdwr != 0;
910 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
912 if (delegation == NULL)
914 if ((delegation->type & fmode) != fmode)
916 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
918 nfs_mark_delegation_referenced(delegation);
922 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
931 case FMODE_READ|FMODE_WRITE:
934 nfs4_state_set_mode_locked(state, state->state | fmode);
937 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
939 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
940 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
941 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
944 set_bit(NFS_O_RDONLY_STATE, &state->flags);
947 set_bit(NFS_O_WRONLY_STATE, &state->flags);
949 case FMODE_READ|FMODE_WRITE:
950 set_bit(NFS_O_RDWR_STATE, &state->flags);
954 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
956 write_seqlock(&state->seqlock);
957 nfs_set_open_stateid_locked(state, stateid, fmode);
958 write_sequnlock(&state->seqlock);
961 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
964 * Protect the call to nfs4_state_set_mode_locked and
965 * serialise the stateid update
967 write_seqlock(&state->seqlock);
968 if (deleg_stateid != NULL) {
969 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
970 set_bit(NFS_DELEGATED_STATE, &state->flags);
972 if (open_stateid != NULL)
973 nfs_set_open_stateid_locked(state, open_stateid, fmode);
974 write_sequnlock(&state->seqlock);
975 spin_lock(&state->owner->so_lock);
976 update_open_stateflags(state, fmode);
977 spin_unlock(&state->owner->so_lock);
980 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
982 struct nfs_inode *nfsi = NFS_I(state->inode);
983 struct nfs_delegation *deleg_cur;
986 fmode &= (FMODE_READ|FMODE_WRITE);
989 deleg_cur = rcu_dereference(nfsi->delegation);
990 if (deleg_cur == NULL)
993 spin_lock(&deleg_cur->lock);
994 if (nfsi->delegation != deleg_cur ||
995 (deleg_cur->type & fmode) != fmode)
996 goto no_delegation_unlock;
998 if (delegation == NULL)
999 delegation = &deleg_cur->stateid;
1000 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
1001 goto no_delegation_unlock;
1003 nfs_mark_delegation_referenced(deleg_cur);
1004 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1006 no_delegation_unlock:
1007 spin_unlock(&deleg_cur->lock);
1011 if (!ret && open_stateid != NULL) {
1012 __update_open_stateid(state, open_stateid, NULL, fmode);
1020 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1022 struct nfs_delegation *delegation;
1025 delegation = rcu_dereference(NFS_I(inode)->delegation);
1026 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1031 nfs_inode_return_delegation(inode);
1034 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1036 struct nfs4_state *state = opendata->state;
1037 struct nfs_inode *nfsi = NFS_I(state->inode);
1038 struct nfs_delegation *delegation;
1039 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1040 fmode_t fmode = opendata->o_arg.fmode;
1041 nfs4_stateid stateid;
1045 if (can_open_cached(state, fmode, open_mode)) {
1046 spin_lock(&state->owner->so_lock);
1047 if (can_open_cached(state, fmode, open_mode)) {
1048 update_open_stateflags(state, fmode);
1049 spin_unlock(&state->owner->so_lock);
1050 goto out_return_state;
1052 spin_unlock(&state->owner->so_lock);
1055 delegation = rcu_dereference(nfsi->delegation);
1056 if (!can_open_delegated(delegation, fmode)) {
1060 /* Save the delegation */
1061 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1063 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1068 /* Try to update the stateid using the delegation */
1069 if (update_open_stateid(state, NULL, &stateid, fmode))
1070 goto out_return_state;
1073 return ERR_PTR(ret);
1075 atomic_inc(&state->count);
1079 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1081 struct inode *inode;
1082 struct nfs4_state *state = NULL;
1083 struct nfs_delegation *delegation;
1086 if (!data->rpc_done) {
1087 state = nfs4_try_open_cached(data);
1092 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1094 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1095 ret = PTR_ERR(inode);
1099 state = nfs4_get_open_state(inode, data->owner);
1102 if (data->o_res.delegation_type != 0) {
1103 int delegation_flags = 0;
1106 delegation = rcu_dereference(NFS_I(inode)->delegation);
1108 delegation_flags = delegation->flags;
1110 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1111 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1112 "returning a delegation for "
1113 "OPEN(CLAIM_DELEGATE_CUR)\n",
1114 NFS_CLIENT(inode)->cl_server);
1115 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1116 nfs_inode_set_delegation(state->inode,
1117 data->owner->so_cred,
1120 nfs_inode_reclaim_delegation(state->inode,
1121 data->owner->so_cred,
1125 update_open_stateid(state, &data->o_res.stateid, NULL,
1133 return ERR_PTR(ret);
1136 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1138 struct nfs_inode *nfsi = NFS_I(state->inode);
1139 struct nfs_open_context *ctx;
1141 spin_lock(&state->inode->i_lock);
1142 list_for_each_entry(ctx, &nfsi->open_files, list) {
1143 if (ctx->state != state)
1145 get_nfs_open_context(ctx);
1146 spin_unlock(&state->inode->i_lock);
1149 spin_unlock(&state->inode->i_lock);
1150 return ERR_PTR(-ENOENT);
1153 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1155 struct nfs4_opendata *opendata;
1157 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1158 if (opendata == NULL)
1159 return ERR_PTR(-ENOMEM);
1160 opendata->state = state;
1161 atomic_inc(&state->count);
1165 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1167 struct nfs4_state *newstate;
1170 opendata->o_arg.open_flags = 0;
1171 opendata->o_arg.fmode = fmode;
1172 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1173 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1174 nfs4_init_opendata_res(opendata);
1175 ret = _nfs4_recover_proc_open(opendata);
1178 newstate = nfs4_opendata_to_nfs4_state(opendata);
1179 if (IS_ERR(newstate))
1180 return PTR_ERR(newstate);
1181 nfs4_close_state(newstate, fmode);
1186 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1188 struct nfs4_state *newstate;
1191 /* memory barrier prior to reading state->n_* */
1192 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1194 if (state->n_rdwr != 0) {
1195 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1196 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1199 if (newstate != state)
1202 if (state->n_wronly != 0) {
1203 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1204 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1207 if (newstate != state)
1210 if (state->n_rdonly != 0) {
1211 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1212 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1215 if (newstate != state)
1219 * We may have performed cached opens for all three recoveries.
1220 * Check if we need to update the current stateid.
1222 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1223 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1224 write_seqlock(&state->seqlock);
1225 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1226 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1227 write_sequnlock(&state->seqlock);
1234 * reclaim state on the server after a reboot.
1236 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1238 struct nfs_delegation *delegation;
1239 struct nfs4_opendata *opendata;
1240 fmode_t delegation_type = 0;
1243 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1244 if (IS_ERR(opendata))
1245 return PTR_ERR(opendata);
1246 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1247 opendata->o_arg.fh = NFS_FH(state->inode);
1249 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1250 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1251 delegation_type = delegation->type;
1253 opendata->o_arg.u.delegation_type = delegation_type;
1254 status = nfs4_open_recover(opendata, state);
1255 nfs4_opendata_put(opendata);
1259 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1261 struct nfs_server *server = NFS_SERVER(state->inode);
1262 struct nfs4_exception exception = { };
1265 err = _nfs4_do_open_reclaim(ctx, state);
1266 if (err != -NFS4ERR_DELAY)
1268 nfs4_handle_exception(server, err, &exception);
1269 } while (exception.retry);
1273 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1275 struct nfs_open_context *ctx;
1278 ctx = nfs4_state_find_open_context(state);
1280 return PTR_ERR(ctx);
1281 ret = nfs4_do_open_reclaim(ctx, state);
1282 put_nfs_open_context(ctx);
1286 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1288 struct nfs4_opendata *opendata;
1291 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1292 if (IS_ERR(opendata))
1293 return PTR_ERR(opendata);
1294 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1295 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1296 sizeof(opendata->o_arg.u.delegation.data));
1297 ret = nfs4_open_recover(opendata, state);
1298 nfs4_opendata_put(opendata);
1302 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1304 struct nfs4_exception exception = { };
1305 struct nfs_server *server = NFS_SERVER(state->inode);
1308 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1314 case -NFS4ERR_BADSESSION:
1315 case -NFS4ERR_BADSLOT:
1316 case -NFS4ERR_BAD_HIGH_SLOT:
1317 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1318 case -NFS4ERR_DEADSESSION:
1319 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
1321 case -NFS4ERR_STALE_CLIENTID:
1322 case -NFS4ERR_STALE_STATEID:
1323 case -NFS4ERR_EXPIRED:
1324 /* Don't recall a delegation if it was lost */
1325 nfs4_schedule_lease_recovery(server->nfs_client);
1329 * The show must go on: exit, but mark the
1330 * stateid as needing recovery.
1332 case -NFS4ERR_DELEG_REVOKED:
1333 case -NFS4ERR_ADMIN_REVOKED:
1334 case -NFS4ERR_BAD_STATEID:
1335 nfs_inode_find_state_and_recover(state->inode,
1337 nfs4_schedule_stateid_recovery(server, state);
1340 * User RPCSEC_GSS context has expired.
1341 * We cannot recover this stateid now, so
1342 * skip it and allow recovery thread to
1349 err = nfs4_handle_exception(server, err, &exception);
1350 } while (exception.retry);
1355 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1357 struct nfs4_opendata *data = calldata;
1359 data->rpc_status = task->tk_status;
1360 if (data->rpc_status == 0) {
1361 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1362 sizeof(data->o_res.stateid.data));
1363 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1364 renew_lease(data->o_res.server, data->timestamp);
1369 static void nfs4_open_confirm_release(void *calldata)
1371 struct nfs4_opendata *data = calldata;
1372 struct nfs4_state *state = NULL;
1374 /* If this request hasn't been cancelled, do nothing */
1375 if (data->cancelled == 0)
1377 /* In case of error, no cleanup! */
1378 if (!data->rpc_done)
1380 state = nfs4_opendata_to_nfs4_state(data);
1382 nfs4_close_state(state, data->o_arg.fmode);
1384 nfs4_opendata_put(data);
1387 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1388 .rpc_call_done = nfs4_open_confirm_done,
1389 .rpc_release = nfs4_open_confirm_release,
1393 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1395 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1397 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1398 struct rpc_task *task;
1399 struct rpc_message msg = {
1400 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1401 .rpc_argp = &data->c_arg,
1402 .rpc_resp = &data->c_res,
1403 .rpc_cred = data->owner->so_cred,
1405 struct rpc_task_setup task_setup_data = {
1406 .rpc_client = server->client,
1407 .rpc_message = &msg,
1408 .callback_ops = &nfs4_open_confirm_ops,
1409 .callback_data = data,
1410 .workqueue = nfsiod_workqueue,
1411 .flags = RPC_TASK_ASYNC,
1415 kref_get(&data->kref);
1417 data->rpc_status = 0;
1418 data->timestamp = jiffies;
1419 task = rpc_run_task(&task_setup_data);
1421 return PTR_ERR(task);
1422 status = nfs4_wait_for_completion_rpc_task(task);
1424 data->cancelled = 1;
1427 status = data->rpc_status;
1432 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1434 struct nfs4_opendata *data = calldata;
1435 struct nfs4_state_owner *sp = data->owner;
1437 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1440 * Check if we still need to send an OPEN call, or if we can use
1441 * a delegation instead.
1443 if (data->state != NULL) {
1444 struct nfs_delegation *delegation;
1446 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1449 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1450 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1451 can_open_delegated(delegation, data->o_arg.fmode))
1452 goto unlock_no_action;
1455 /* Update sequence id. */
1456 data->o_arg.id = sp->so_owner_id.id;
1457 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1458 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1459 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1460 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1462 data->timestamp = jiffies;
1463 if (nfs4_setup_sequence(data->o_arg.server,
1464 &data->o_arg.seq_args,
1465 &data->o_res.seq_res, 1, task))
1467 rpc_call_start(task);
1472 task->tk_action = NULL;
1476 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1478 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1479 nfs4_open_prepare(task, calldata);
1482 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1484 struct nfs4_opendata *data = calldata;
1486 data->rpc_status = task->tk_status;
1488 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1491 if (task->tk_status == 0) {
1492 switch (data->o_res.f_attr->mode & S_IFMT) {
1496 data->rpc_status = -ELOOP;
1499 data->rpc_status = -EISDIR;
1502 data->rpc_status = -ENOTDIR;
1504 renew_lease(data->o_res.server, data->timestamp);
1505 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1506 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1511 static void nfs4_open_release(void *calldata)
1513 struct nfs4_opendata *data = calldata;
1514 struct nfs4_state *state = NULL;
1516 /* If this request hasn't been cancelled, do nothing */
1517 if (data->cancelled == 0)
1519 /* In case of error, no cleanup! */
1520 if (data->rpc_status != 0 || !data->rpc_done)
1522 /* In case we need an open_confirm, no cleanup! */
1523 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1525 state = nfs4_opendata_to_nfs4_state(data);
1527 nfs4_close_state(state, data->o_arg.fmode);
1529 nfs4_opendata_put(data);
1532 static const struct rpc_call_ops nfs4_open_ops = {
1533 .rpc_call_prepare = nfs4_open_prepare,
1534 .rpc_call_done = nfs4_open_done,
1535 .rpc_release = nfs4_open_release,
1538 static const struct rpc_call_ops nfs4_recover_open_ops = {
1539 .rpc_call_prepare = nfs4_recover_open_prepare,
1540 .rpc_call_done = nfs4_open_done,
1541 .rpc_release = nfs4_open_release,
1544 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1546 struct inode *dir = data->dir->d_inode;
1547 struct nfs_server *server = NFS_SERVER(dir);
1548 struct nfs_openargs *o_arg = &data->o_arg;
1549 struct nfs_openres *o_res = &data->o_res;
1550 struct rpc_task *task;
1551 struct rpc_message msg = {
1552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1555 .rpc_cred = data->owner->so_cred,
1557 struct rpc_task_setup task_setup_data = {
1558 .rpc_client = server->client,
1559 .rpc_message = &msg,
1560 .callback_ops = &nfs4_open_ops,
1561 .callback_data = data,
1562 .workqueue = nfsiod_workqueue,
1563 .flags = RPC_TASK_ASYNC,
1567 kref_get(&data->kref);
1569 data->rpc_status = 0;
1570 data->cancelled = 0;
1572 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1573 task = rpc_run_task(&task_setup_data);
1575 return PTR_ERR(task);
1576 status = nfs4_wait_for_completion_rpc_task(task);
1578 data->cancelled = 1;
1581 status = data->rpc_status;
1587 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1589 struct inode *dir = data->dir->d_inode;
1590 struct nfs_openres *o_res = &data->o_res;
1593 status = nfs4_run_open_task(data, 1);
1594 if (status != 0 || !data->rpc_done)
1597 nfs_refresh_inode(dir, o_res->dir_attr);
1599 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1600 status = _nfs4_proc_open_confirm(data);
1609 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1611 static int _nfs4_proc_open(struct nfs4_opendata *data)
1613 struct inode *dir = data->dir->d_inode;
1614 struct nfs_server *server = NFS_SERVER(dir);
1615 struct nfs_openargs *o_arg = &data->o_arg;
1616 struct nfs_openres *o_res = &data->o_res;
1619 status = nfs4_run_open_task(data, 0);
1620 if (!data->rpc_done)
1623 if (status == -NFS4ERR_BADNAME &&
1624 !(o_arg->open_flags & O_CREAT))
1629 if (o_arg->open_flags & O_CREAT) {
1630 update_changeattr(dir, &o_res->cinfo);
1631 nfs_post_op_update_inode(dir, o_res->dir_attr);
1633 nfs_refresh_inode(dir, o_res->dir_attr);
1634 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1635 server->caps &= ~NFS_CAP_POSIX_LOCK;
1636 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1637 status = _nfs4_proc_open_confirm(data);
1641 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1642 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1646 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1651 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1652 ret = nfs4_wait_clnt_recover(clp);
1655 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1656 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1658 nfs4_schedule_state_manager(clp);
1664 static int nfs4_recover_expired_lease(struct nfs_server *server)
1666 return nfs4_client_recover_expired_lease(server->nfs_client);
1671 * reclaim state on the server after a network partition.
1672 * Assumes caller holds the appropriate lock
1674 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1676 struct nfs4_opendata *opendata;
1679 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1680 if (IS_ERR(opendata))
1681 return PTR_ERR(opendata);
1682 ret = nfs4_open_recover(opendata, state);
1684 d_drop(ctx->dentry);
1685 nfs4_opendata_put(opendata);
1689 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1691 struct nfs_server *server = NFS_SERVER(state->inode);
1692 struct nfs4_exception exception = { };
1696 err = _nfs4_open_expired(ctx, state);
1700 case -NFS4ERR_GRACE:
1701 case -NFS4ERR_DELAY:
1702 nfs4_handle_exception(server, err, &exception);
1705 } while (exception.retry);
1710 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1712 struct nfs_open_context *ctx;
1715 ctx = nfs4_state_find_open_context(state);
1717 return PTR_ERR(ctx);
1718 ret = nfs4_do_open_expired(ctx, state);
1719 put_nfs_open_context(ctx);
1723 #if defined(CONFIG_NFS_V4_1)
1724 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1727 struct nfs_server *server = NFS_SERVER(state->inode);
1729 status = nfs41_test_stateid(server, state);
1730 if (status == NFS_OK)
1732 nfs41_free_stateid(server, state);
1733 return nfs4_open_expired(sp, state);
1738 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1739 * fields corresponding to attributes that were used to store the verifier.
1740 * Make sure we clobber those fields in the later setattr call
1742 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1744 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1745 !(sattr->ia_valid & ATTR_ATIME_SET))
1746 sattr->ia_valid |= ATTR_ATIME;
1748 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1749 !(sattr->ia_valid & ATTR_MTIME_SET))
1750 sattr->ia_valid |= ATTR_MTIME;
1754 * Returns a referenced nfs4_state
1756 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)
1758 struct nfs4_state_owner *sp;
1759 struct nfs4_state *state = NULL;
1760 struct nfs_server *server = NFS_SERVER(dir);
1761 struct nfs4_opendata *opendata;
1764 /* Protect against reboot recovery conflicts */
1766 if (!(sp = nfs4_get_state_owner(server, cred))) {
1767 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1770 status = nfs4_recover_expired_lease(server);
1772 goto err_put_state_owner;
1773 if (dentry->d_inode != NULL)
1774 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1776 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1777 if (opendata == NULL)
1778 goto err_put_state_owner;
1780 if (dentry->d_inode != NULL)
1781 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1783 status = _nfs4_proc_open(opendata);
1785 goto err_opendata_put;
1787 state = nfs4_opendata_to_nfs4_state(opendata);
1788 status = PTR_ERR(state);
1790 goto err_opendata_put;
1791 if (server->caps & NFS_CAP_POSIX_LOCK)
1792 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1794 if (opendata->o_arg.open_flags & O_EXCL) {
1795 nfs4_exclusive_attrset(opendata, sattr);
1797 nfs_fattr_init(opendata->o_res.f_attr);
1798 status = nfs4_do_setattr(state->inode, cred,
1799 opendata->o_res.f_attr, sattr,
1802 nfs_setattr_update_inode(state->inode, sattr);
1803 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1805 nfs4_opendata_put(opendata);
1806 nfs4_put_state_owner(sp);
1810 nfs4_opendata_put(opendata);
1811 err_put_state_owner:
1812 nfs4_put_state_owner(sp);
1819 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)
1821 struct nfs4_exception exception = { };
1822 struct nfs4_state *res;
1826 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
1829 /* NOTE: BAD_SEQID means the server and client disagree about the
1830 * book-keeping w.r.t. state-changing operations
1831 * (OPEN/CLOSE/LOCK/LOCKU...)
1832 * It is actually a sign of a bug on the client or on the server.
1834 * If we receive a BAD_SEQID error in the particular case of
1835 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1836 * have unhashed the old state_owner for us, and that we can
1837 * therefore safely retry using a new one. We should still warn
1838 * the user though...
1840 if (status == -NFS4ERR_BAD_SEQID) {
1841 pr_warn_ratelimited("NFS: v4 server %s "
1842 " returned a bad sequence-id error!\n",
1843 NFS_SERVER(dir)->nfs_client->cl_hostname);
1844 exception.retry = 1;
1848 * BAD_STATEID on OPEN means that the server cancelled our
1849 * state before it received the OPEN_CONFIRM.
1850 * Recover by retrying the request as per the discussion
1851 * on Page 181 of RFC3530.
1853 if (status == -NFS4ERR_BAD_STATEID) {
1854 exception.retry = 1;
1857 if (status == -EAGAIN) {
1858 /* We must have found a delegation */
1859 exception.retry = 1;
1862 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1863 status, &exception));
1864 } while (exception.retry);
1868 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1869 struct nfs_fattr *fattr, struct iattr *sattr,
1870 struct nfs4_state *state)
1872 struct nfs_server *server = NFS_SERVER(inode);
1873 struct nfs_setattrargs arg = {
1874 .fh = NFS_FH(inode),
1877 .bitmask = server->attr_bitmask,
1879 struct nfs_setattrres res = {
1883 struct rpc_message msg = {
1884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1889 unsigned long timestamp = jiffies;
1892 nfs_fattr_init(fattr);
1894 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1895 /* Use that stateid */
1896 } else if (state != NULL) {
1897 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
1899 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1901 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1902 if (status == 0 && state != NULL)
1903 renew_lease(server, timestamp);
1907 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1908 struct nfs_fattr *fattr, struct iattr *sattr,
1909 struct nfs4_state *state)
1911 struct nfs_server *server = NFS_SERVER(inode);
1912 struct nfs4_exception exception = {
1918 err = nfs4_handle_exception(server,
1919 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1921 } while (exception.retry);
1925 struct nfs4_closedata {
1926 struct inode *inode;
1927 struct nfs4_state *state;
1928 struct nfs_closeargs arg;
1929 struct nfs_closeres res;
1930 struct nfs_fattr fattr;
1931 unsigned long timestamp;
1936 static void nfs4_free_closedata(void *data)
1938 struct nfs4_closedata *calldata = data;
1939 struct nfs4_state_owner *sp = calldata->state->owner;
1940 struct super_block *sb = calldata->state->inode->i_sb;
1943 pnfs_roc_release(calldata->state->inode);
1944 nfs4_put_open_state(calldata->state);
1945 nfs_free_seqid(calldata->arg.seqid);
1946 nfs4_put_state_owner(sp);
1947 nfs_sb_deactive(sb);
1951 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1954 spin_lock(&state->owner->so_lock);
1955 if (!(fmode & FMODE_READ))
1956 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1957 if (!(fmode & FMODE_WRITE))
1958 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1959 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1960 spin_unlock(&state->owner->so_lock);
1963 static void nfs4_close_done(struct rpc_task *task, void *data)
1965 struct nfs4_closedata *calldata = data;
1966 struct nfs4_state *state = calldata->state;
1967 struct nfs_server *server = NFS_SERVER(calldata->inode);
1969 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1971 /* hmm. we are done with the inode, and in the process of freeing
1972 * the state_owner. we keep this around to process errors
1974 switch (task->tk_status) {
1977 pnfs_roc_set_barrier(state->inode,
1978 calldata->roc_barrier);
1979 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1980 renew_lease(server, calldata->timestamp);
1981 nfs4_close_clear_stateid_flags(state,
1982 calldata->arg.fmode);
1984 case -NFS4ERR_STALE_STATEID:
1985 case -NFS4ERR_OLD_STATEID:
1986 case -NFS4ERR_BAD_STATEID:
1987 case -NFS4ERR_EXPIRED:
1988 if (calldata->arg.fmode == 0)
1991 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1992 rpc_restart_call_prepare(task);
1994 nfs_release_seqid(calldata->arg.seqid);
1995 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1998 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2000 struct nfs4_closedata *calldata = data;
2001 struct nfs4_state *state = calldata->state;
2004 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2007 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2008 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2009 spin_lock(&state->owner->so_lock);
2010 /* Calculate the change in open mode */
2011 if (state->n_rdwr == 0) {
2012 if (state->n_rdonly == 0) {
2013 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2014 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2015 calldata->arg.fmode &= ~FMODE_READ;
2017 if (state->n_wronly == 0) {
2018 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2019 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2020 calldata->arg.fmode &= ~FMODE_WRITE;
2023 spin_unlock(&state->owner->so_lock);
2026 /* Note: exit _without_ calling nfs4_close_done */
2027 task->tk_action = NULL;
2031 if (calldata->arg.fmode == 0) {
2032 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2033 if (calldata->roc &&
2034 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2035 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2041 nfs_fattr_init(calldata->res.fattr);
2042 calldata->timestamp = jiffies;
2043 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
2044 &calldata->arg.seq_args, &calldata->res.seq_res,
2047 rpc_call_start(task);
2050 static const struct rpc_call_ops nfs4_close_ops = {
2051 .rpc_call_prepare = nfs4_close_prepare,
2052 .rpc_call_done = nfs4_close_done,
2053 .rpc_release = nfs4_free_closedata,
2057 * It is possible for data to be read/written from a mem-mapped file
2058 * after the sys_close call (which hits the vfs layer as a flush).
2059 * This means that we can't safely call nfsv4 close on a file until
2060 * the inode is cleared. This in turn means that we are not good
2061 * NFSv4 citizens - we do not indicate to the server to update the file's
2062 * share state even when we are done with one of the three share
2063 * stateid's in the inode.
2065 * NOTE: Caller must be holding the sp->so_owner semaphore!
2067 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
2069 struct nfs_server *server = NFS_SERVER(state->inode);
2070 struct nfs4_closedata *calldata;
2071 struct nfs4_state_owner *sp = state->owner;
2072 struct rpc_task *task;
2073 struct rpc_message msg = {
2074 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2075 .rpc_cred = state->owner->so_cred,
2077 struct rpc_task_setup task_setup_data = {
2078 .rpc_client = server->client,
2079 .rpc_message = &msg,
2080 .callback_ops = &nfs4_close_ops,
2081 .workqueue = nfsiod_workqueue,
2082 .flags = RPC_TASK_ASYNC,
2084 int status = -ENOMEM;
2086 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2087 if (calldata == NULL)
2089 calldata->inode = state->inode;
2090 calldata->state = state;
2091 calldata->arg.fh = NFS_FH(state->inode);
2092 calldata->arg.stateid = &state->open_stateid;
2093 /* Serialization for the sequence id */
2094 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2095 if (calldata->arg.seqid == NULL)
2096 goto out_free_calldata;
2097 calldata->arg.fmode = 0;
2098 calldata->arg.bitmask = server->cache_consistency_bitmask;
2099 calldata->res.fattr = &calldata->fattr;
2100 calldata->res.seqid = calldata->arg.seqid;
2101 calldata->res.server = server;
2102 calldata->roc = roc;
2103 nfs_sb_active(calldata->inode->i_sb);
2105 msg.rpc_argp = &calldata->arg;
2106 msg.rpc_resp = &calldata->res;
2107 task_setup_data.callback_data = calldata;
2108 task = rpc_run_task(&task_setup_data);
2110 return PTR_ERR(task);
2113 status = rpc_wait_for_completion_task(task);
2120 pnfs_roc_release(state->inode);
2121 nfs4_put_open_state(state);
2122 nfs4_put_state_owner(sp);
2126 static struct inode *
2127 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2129 struct nfs4_state *state;
2131 /* Protect against concurrent sillydeletes */
2132 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
2134 return ERR_CAST(state);
2136 return igrab(state->inode);
2139 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2141 if (ctx->state == NULL)
2144 nfs4_close_sync(ctx->state, ctx->mode);
2146 nfs4_close_state(ctx->state, ctx->mode);
2149 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2151 struct nfs4_server_caps_arg args = {
2154 struct nfs4_server_caps_res res = {};
2155 struct rpc_message msg = {
2156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2162 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2164 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2165 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2166 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2167 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2168 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2169 NFS_CAP_CTIME|NFS_CAP_MTIME);
2170 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2171 server->caps |= NFS_CAP_ACLS;
2172 if (res.has_links != 0)
2173 server->caps |= NFS_CAP_HARDLINKS;
2174 if (res.has_symlinks != 0)
2175 server->caps |= NFS_CAP_SYMLINKS;
2176 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2177 server->caps |= NFS_CAP_FILEID;
2178 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2179 server->caps |= NFS_CAP_MODE;
2180 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2181 server->caps |= NFS_CAP_NLINK;
2182 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2183 server->caps |= NFS_CAP_OWNER;
2184 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2185 server->caps |= NFS_CAP_OWNER_GROUP;
2186 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2187 server->caps |= NFS_CAP_ATIME;
2188 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2189 server->caps |= NFS_CAP_CTIME;
2190 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2191 server->caps |= NFS_CAP_MTIME;
2193 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2194 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2195 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2196 server->acl_bitmask = res.acl_bitmask;
2202 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2204 struct nfs4_exception exception = { };
2207 err = nfs4_handle_exception(server,
2208 _nfs4_server_capabilities(server, fhandle),
2210 } while (exception.retry);
2214 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2215 struct nfs_fsinfo *info)
2217 struct nfs4_lookup_root_arg args = {
2218 .bitmask = nfs4_fattr_bitmap,
2220 struct nfs4_lookup_res res = {
2222 .fattr = info->fattr,
2225 struct rpc_message msg = {
2226 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2231 nfs_fattr_init(info->fattr);
2232 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2235 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2236 struct nfs_fsinfo *info)
2238 struct nfs4_exception exception = { };
2241 err = _nfs4_lookup_root(server, fhandle, info);
2244 case -NFS4ERR_WRONGSEC:
2247 err = nfs4_handle_exception(server, err, &exception);
2249 } while (exception.retry);
2254 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2255 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2257 struct rpc_auth *auth;
2260 auth = rpcauth_create(flavor, server->client);
2265 ret = nfs4_lookup_root(server, fhandle, info);
2270 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2271 struct nfs_fsinfo *info)
2273 int i, len, status = 0;
2274 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2276 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2277 flav_array[len] = RPC_AUTH_NULL;
2280 for (i = 0; i < len; i++) {
2281 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2282 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2287 * -EACCESS could mean that the user doesn't have correct permissions
2288 * to access the mount. It could also mean that we tried to mount
2289 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2290 * existing mount programs don't handle -EACCES very well so it should
2291 * be mapped to -EPERM instead.
2293 if (status == -EACCES)
2299 * get the file handle for the "/" directory on the server
2301 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2302 struct nfs_fsinfo *info)
2304 int minor_version = server->nfs_client->cl_minorversion;
2305 int status = nfs4_lookup_root(server, fhandle, info);
2306 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2308 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2309 * by nfs4_map_errors() as this function exits.
2311 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2313 status = nfs4_server_capabilities(server, fhandle);
2315 status = nfs4_do_fsinfo(server, fhandle, info);
2316 return nfs4_map_errors(status);
2319 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
2321 * Get locations and (maybe) other attributes of a referral.
2322 * Note that we'll actually follow the referral later when
2323 * we detect fsid mismatch in inode revalidation
2325 static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2326 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2328 int status = -ENOMEM;
2329 struct page *page = NULL;
2330 struct nfs4_fs_locations *locations = NULL;
2332 page = alloc_page(GFP_KERNEL);
2335 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2336 if (locations == NULL)
2339 status = nfs4_proc_fs_locations(dir, name, locations, page);
2342 /* Make sure server returned a different fsid for the referral */
2343 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2344 dprintk("%s: server did not return a different fsid for"
2345 " a referral at %s\n", __func__, name->name);
2349 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2350 nfs_fixup_referral_attributes(&locations->fattr);
2352 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2353 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2354 memset(fhandle, 0, sizeof(struct nfs_fh));
2362 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2364 struct nfs4_getattr_arg args = {
2366 .bitmask = server->attr_bitmask,
2368 struct nfs4_getattr_res res = {
2372 struct rpc_message msg = {
2373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2378 nfs_fattr_init(fattr);
2379 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2382 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2384 struct nfs4_exception exception = { };
2387 err = nfs4_handle_exception(server,
2388 _nfs4_proc_getattr(server, fhandle, fattr),
2390 } while (exception.retry);
2395 * The file is not closed if it is opened due to the a request to change
2396 * the size of the file. The open call will not be needed once the
2397 * VFS layer lookup-intents are implemented.
2399 * Close is called when the inode is destroyed.
2400 * If we haven't opened the file for O_WRONLY, we
2401 * need to in the size_change case to obtain a stateid.
2404 * Because OPEN is always done by name in nfsv4, it is
2405 * possible that we opened a different file by the same
2406 * name. We can recognize this race condition, but we
2407 * can't do anything about it besides returning an error.
2409 * This will be fixed with VFS changes (lookup-intent).
2412 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2413 struct iattr *sattr)
2415 struct inode *inode = dentry->d_inode;
2416 struct rpc_cred *cred = NULL;
2417 struct nfs4_state *state = NULL;
2420 if (pnfs_ld_layoutret_on_setattr(inode))
2421 pnfs_return_layout(inode);
2423 nfs_fattr_init(fattr);
2425 /* Search for an existing open(O_WRITE) file */
2426 if (sattr->ia_valid & ATTR_FILE) {
2427 struct nfs_open_context *ctx;
2429 ctx = nfs_file_open_context(sattr->ia_file);
2436 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2438 nfs_setattr_update_inode(inode, sattr);
2442 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2443 const struct qstr *name, struct nfs_fh *fhandle,
2444 struct nfs_fattr *fattr)
2446 struct nfs_server *server = NFS_SERVER(dir);
2448 struct nfs4_lookup_arg args = {
2449 .bitmask = server->attr_bitmask,
2450 .dir_fh = NFS_FH(dir),
2453 struct nfs4_lookup_res res = {
2458 struct rpc_message msg = {
2459 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2464 nfs_fattr_init(fattr);
2466 dprintk("NFS call lookup %s\n", name->name);
2467 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2468 dprintk("NFS reply lookup: %d\n", status);
2472 void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2474 memset(fh, 0, sizeof(struct nfs_fh));
2475 fattr->fsid.major = 1;
2476 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2477 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2478 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2482 static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2483 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2485 struct nfs4_exception exception = { };
2490 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2492 case -NFS4ERR_BADNAME:
2494 case -NFS4ERR_MOVED:
2495 return nfs4_get_referral(dir, name, fattr, fhandle);
2496 case -NFS4ERR_WRONGSEC:
2497 nfs_fixup_secinfo_attributes(fattr, fhandle);
2499 err = nfs4_handle_exception(NFS_SERVER(dir),
2500 status, &exception);
2501 } while (exception.retry);
2505 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2507 struct nfs_server *server = NFS_SERVER(inode);
2508 struct nfs4_accessargs args = {
2509 .fh = NFS_FH(inode),
2510 .bitmask = server->attr_bitmask,
2512 struct nfs4_accessres res = {
2515 struct rpc_message msg = {
2516 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2519 .rpc_cred = entry->cred,
2521 int mode = entry->mask;
2525 * Determine which access bits we want to ask for...
2527 if (mode & MAY_READ)
2528 args.access |= NFS4_ACCESS_READ;
2529 if (S_ISDIR(inode->i_mode)) {
2530 if (mode & MAY_WRITE)
2531 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2532 if (mode & MAY_EXEC)
2533 args.access |= NFS4_ACCESS_LOOKUP;
2535 if (mode & MAY_WRITE)
2536 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2537 if (mode & MAY_EXEC)
2538 args.access |= NFS4_ACCESS_EXECUTE;
2541 res.fattr = nfs_alloc_fattr();
2542 if (res.fattr == NULL)
2545 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2548 if (res.access & NFS4_ACCESS_READ)
2549 entry->mask |= MAY_READ;
2550 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2551 entry->mask |= MAY_WRITE;
2552 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2553 entry->mask |= MAY_EXEC;
2554 nfs_refresh_inode(inode, res.fattr);
2556 nfs_free_fattr(res.fattr);
2560 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2562 struct nfs4_exception exception = { };
2565 err = nfs4_handle_exception(NFS_SERVER(inode),
2566 _nfs4_proc_access(inode, entry),
2568 } while (exception.retry);
2573 * TODO: For the time being, we don't try to get any attributes
2574 * along with any of the zero-copy operations READ, READDIR,
2577 * In the case of the first three, we want to put the GETATTR
2578 * after the read-type operation -- this is because it is hard
2579 * to predict the length of a GETATTR response in v4, and thus
2580 * align the READ data correctly. This means that the GETATTR
2581 * may end up partially falling into the page cache, and we should
2582 * shift it into the 'tail' of the xdr_buf before processing.
2583 * To do this efficiently, we need to know the total length
2584 * of data received, which doesn't seem to be available outside
2587 * In the case of WRITE, we also want to put the GETATTR after
2588 * the operation -- in this case because we want to make sure
2589 * we get the post-operation mtime and size. This means that
2590 * we can't use xdr_encode_pages() as written: we need a variant
2591 * of it which would leave room in the 'tail' iovec.
2593 * Both of these changes to the XDR layer would in fact be quite
2594 * minor, but I decided to leave them for a subsequent patch.
2596 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2597 unsigned int pgbase, unsigned int pglen)
2599 struct nfs4_readlink args = {
2600 .fh = NFS_FH(inode),
2605 struct nfs4_readlink_res res;
2606 struct rpc_message msg = {
2607 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2612 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2615 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2616 unsigned int pgbase, unsigned int pglen)
2618 struct nfs4_exception exception = { };
2621 err = nfs4_handle_exception(NFS_SERVER(inode),
2622 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2624 } while (exception.retry);
2630 * We will need to arrange for the VFS layer to provide an atomic open.
2631 * Until then, this create/open method is prone to inefficiency and race
2632 * conditions due to the lookup, create, and open VFS calls from sys_open()
2633 * placed on the wire.
2635 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2636 * The file will be opened again in the subsequent VFS open call
2637 * (nfs4_proc_file_open).
2639 * The open for read will just hang around to be used by any process that
2640 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2644 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2645 int flags, struct nfs_open_context *ctx)
2647 struct dentry *de = dentry;
2648 struct nfs4_state *state;
2649 struct rpc_cred *cred = NULL;
2658 sattr->ia_mode &= ~current_umask();
2659 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
2661 if (IS_ERR(state)) {
2662 status = PTR_ERR(state);
2665 d_add(dentry, igrab(state->inode));
2666 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2670 nfs4_close_sync(state, fmode);
2675 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2677 struct nfs_server *server = NFS_SERVER(dir);
2678 struct nfs_removeargs args = {
2680 .name.len = name->len,
2681 .name.name = name->name,
2682 .bitmask = server->attr_bitmask,
2684 struct nfs_removeres res = {
2687 struct rpc_message msg = {
2688 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2692 int status = -ENOMEM;
2694 res.dir_attr = nfs_alloc_fattr();
2695 if (res.dir_attr == NULL)
2698 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2700 update_changeattr(dir, &res.cinfo);
2701 nfs_post_op_update_inode(dir, res.dir_attr);
2703 nfs_free_fattr(res.dir_attr);
2708 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2710 struct nfs4_exception exception = { };
2713 err = nfs4_handle_exception(NFS_SERVER(dir),
2714 _nfs4_proc_remove(dir, name),
2716 } while (exception.retry);
2720 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2722 struct nfs_server *server = NFS_SERVER(dir);
2723 struct nfs_removeargs *args = msg->rpc_argp;
2724 struct nfs_removeres *res = msg->rpc_resp;
2726 args->bitmask = server->cache_consistency_bitmask;
2727 res->server = server;
2728 res->seq_res.sr_slot = NULL;
2729 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2732 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2734 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2736 if (!nfs4_sequence_done(task, &res->seq_res))
2738 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2740 update_changeattr(dir, &res->cinfo);
2741 nfs_post_op_update_inode(dir, res->dir_attr);
2745 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2747 struct nfs_server *server = NFS_SERVER(dir);
2748 struct nfs_renameargs *arg = msg->rpc_argp;
2749 struct nfs_renameres *res = msg->rpc_resp;
2751 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2752 arg->bitmask = server->attr_bitmask;
2753 res->server = server;
2756 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2757 struct inode *new_dir)
2759 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2761 if (!nfs4_sequence_done(task, &res->seq_res))
2763 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2766 update_changeattr(old_dir, &res->old_cinfo);
2767 nfs_post_op_update_inode(old_dir, res->old_fattr);
2768 update_changeattr(new_dir, &res->new_cinfo);
2769 nfs_post_op_update_inode(new_dir, res->new_fattr);
2773 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2774 struct inode *new_dir, struct qstr *new_name)
2776 struct nfs_server *server = NFS_SERVER(old_dir);
2777 struct nfs_renameargs arg = {
2778 .old_dir = NFS_FH(old_dir),
2779 .new_dir = NFS_FH(new_dir),
2780 .old_name = old_name,
2781 .new_name = new_name,
2782 .bitmask = server->attr_bitmask,
2784 struct nfs_renameres res = {
2787 struct rpc_message msg = {
2788 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2792 int status = -ENOMEM;
2794 res.old_fattr = nfs_alloc_fattr();
2795 res.new_fattr = nfs_alloc_fattr();
2796 if (res.old_fattr == NULL || res.new_fattr == NULL)
2799 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2801 update_changeattr(old_dir, &res.old_cinfo);
2802 nfs_post_op_update_inode(old_dir, res.old_fattr);
2803 update_changeattr(new_dir, &res.new_cinfo);
2804 nfs_post_op_update_inode(new_dir, res.new_fattr);
2807 nfs_free_fattr(res.new_fattr);
2808 nfs_free_fattr(res.old_fattr);
2812 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2813 struct inode *new_dir, struct qstr *new_name)
2815 struct nfs4_exception exception = { };
2818 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2819 _nfs4_proc_rename(old_dir, old_name,
2822 } while (exception.retry);
2826 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2828 struct nfs_server *server = NFS_SERVER(inode);
2829 struct nfs4_link_arg arg = {
2830 .fh = NFS_FH(inode),
2831 .dir_fh = NFS_FH(dir),
2833 .bitmask = server->attr_bitmask,
2835 struct nfs4_link_res res = {
2838 struct rpc_message msg = {
2839 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2843 int status = -ENOMEM;
2845 res.fattr = nfs_alloc_fattr();
2846 res.dir_attr = nfs_alloc_fattr();
2847 if (res.fattr == NULL || res.dir_attr == NULL)
2850 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2852 update_changeattr(dir, &res.cinfo);
2853 nfs_post_op_update_inode(dir, res.dir_attr);
2854 nfs_post_op_update_inode(inode, res.fattr);
2857 nfs_free_fattr(res.dir_attr);
2858 nfs_free_fattr(res.fattr);
2862 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2864 struct nfs4_exception exception = { };
2867 err = nfs4_handle_exception(NFS_SERVER(inode),
2868 _nfs4_proc_link(inode, dir, name),
2870 } while (exception.retry);
2874 struct nfs4_createdata {
2875 struct rpc_message msg;
2876 struct nfs4_create_arg arg;
2877 struct nfs4_create_res res;
2879 struct nfs_fattr fattr;
2880 struct nfs_fattr dir_fattr;
2883 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2884 struct qstr *name, struct iattr *sattr, u32 ftype)
2886 struct nfs4_createdata *data;
2888 data = kzalloc(sizeof(*data), GFP_KERNEL);
2890 struct nfs_server *server = NFS_SERVER(dir);
2892 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2893 data->msg.rpc_argp = &data->arg;
2894 data->msg.rpc_resp = &data->res;
2895 data->arg.dir_fh = NFS_FH(dir);
2896 data->arg.server = server;
2897 data->arg.name = name;
2898 data->arg.attrs = sattr;
2899 data->arg.ftype = ftype;
2900 data->arg.bitmask = server->attr_bitmask;
2901 data->res.server = server;
2902 data->res.fh = &data->fh;
2903 data->res.fattr = &data->fattr;
2904 data->res.dir_fattr = &data->dir_fattr;
2905 nfs_fattr_init(data->res.fattr);
2906 nfs_fattr_init(data->res.dir_fattr);
2911 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2913 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
2914 &data->arg.seq_args, &data->res.seq_res, 1);
2916 update_changeattr(dir, &data->res.dir_cinfo);
2917 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2918 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2923 static void nfs4_free_createdata(struct nfs4_createdata *data)
2928 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2929 struct page *page, unsigned int len, struct iattr *sattr)
2931 struct nfs4_createdata *data;
2932 int status = -ENAMETOOLONG;
2934 if (len > NFS4_MAXPATHLEN)
2938 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2942 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2943 data->arg.u.symlink.pages = &page;
2944 data->arg.u.symlink.len = len;
2946 status = nfs4_do_create(dir, dentry, data);
2948 nfs4_free_createdata(data);
2953 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2954 struct page *page, unsigned int len, struct iattr *sattr)
2956 struct nfs4_exception exception = { };
2959 err = nfs4_handle_exception(NFS_SERVER(dir),
2960 _nfs4_proc_symlink(dir, dentry, page,
2963 } while (exception.retry);
2967 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2968 struct iattr *sattr)
2970 struct nfs4_createdata *data;
2971 int status = -ENOMEM;
2973 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2977 status = nfs4_do_create(dir, dentry, data);
2979 nfs4_free_createdata(data);
2984 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2985 struct iattr *sattr)
2987 struct nfs4_exception exception = { };
2990 sattr->ia_mode &= ~current_umask();
2992 err = nfs4_handle_exception(NFS_SERVER(dir),
2993 _nfs4_proc_mkdir(dir, dentry, sattr),
2995 } while (exception.retry);
2999 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3000 u64 cookie, struct page **pages, unsigned int count, int plus)
3002 struct inode *dir = dentry->d_inode;
3003 struct nfs4_readdir_arg args = {
3008 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3011 struct nfs4_readdir_res res;
3012 struct rpc_message msg = {
3013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3020 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3021 dentry->d_parent->d_name.name,
3022 dentry->d_name.name,
3023 (unsigned long long)cookie);
3024 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3025 res.pgbase = args.pgbase;
3026 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3028 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
3029 status += args.pgbase;
3032 nfs_invalidate_atime(dir);
3034 dprintk("%s: returns %d\n", __func__, status);
3038 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3039 u64 cookie, struct page **pages, unsigned int count, int plus)
3041 struct nfs4_exception exception = { };
3044 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3045 _nfs4_proc_readdir(dentry, cred, cookie,
3046 pages, count, plus),
3048 } while (exception.retry);
3052 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3053 struct iattr *sattr, dev_t rdev)
3055 struct nfs4_createdata *data;
3056 int mode = sattr->ia_mode;
3057 int status = -ENOMEM;
3059 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3060 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3062 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3067 data->arg.ftype = NF4FIFO;
3068 else if (S_ISBLK(mode)) {
3069 data->arg.ftype = NF4BLK;
3070 data->arg.u.device.specdata1 = MAJOR(rdev);
3071 data->arg.u.device.specdata2 = MINOR(rdev);
3073 else if (S_ISCHR(mode)) {
3074 data->arg.ftype = NF4CHR;
3075 data->arg.u.device.specdata1 = MAJOR(rdev);
3076 data->arg.u.device.specdata2 = MINOR(rdev);
3079 status = nfs4_do_create(dir, dentry, data);
3081 nfs4_free_createdata(data);
3086 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3087 struct iattr *sattr, dev_t rdev)
3089 struct nfs4_exception exception = { };
3092 sattr->ia_mode &= ~current_umask();
3094 err = nfs4_handle_exception(NFS_SERVER(dir),
3095 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3097 } while (exception.retry);
3101 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3102 struct nfs_fsstat *fsstat)
3104 struct nfs4_statfs_arg args = {
3106 .bitmask = server->attr_bitmask,
3108 struct nfs4_statfs_res res = {
3111 struct rpc_message msg = {
3112 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3117 nfs_fattr_init(fsstat->fattr);
3118 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3121 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3123 struct nfs4_exception exception = { };
3126 err = nfs4_handle_exception(server,
3127 _nfs4_proc_statfs(server, fhandle, fsstat),
3129 } while (exception.retry);
3133 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3134 struct nfs_fsinfo *fsinfo)
3136 struct nfs4_fsinfo_arg args = {
3138 .bitmask = server->attr_bitmask,
3140 struct nfs4_fsinfo_res res = {
3143 struct rpc_message msg = {
3144 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3149 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3152 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3154 struct nfs4_exception exception = { };
3158 err = nfs4_handle_exception(server,
3159 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3161 } while (exception.retry);
3165 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3167 nfs_fattr_init(fsinfo->fattr);
3168 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3171 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3172 struct nfs_pathconf *pathconf)
3174 struct nfs4_pathconf_arg args = {
3176 .bitmask = server->attr_bitmask,
3178 struct nfs4_pathconf_res res = {
3179 .pathconf = pathconf,
3181 struct rpc_message msg = {
3182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3187 /* None of the pathconf attributes are mandatory to implement */
3188 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3189 memset(pathconf, 0, sizeof(*pathconf));
3193 nfs_fattr_init(pathconf->fattr);
3194 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3197 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3198 struct nfs_pathconf *pathconf)
3200 struct nfs4_exception exception = { };
3204 err = nfs4_handle_exception(server,
3205 _nfs4_proc_pathconf(server, fhandle, pathconf),
3207 } while (exception.retry);
3211 void __nfs4_read_done_cb(struct nfs_read_data *data)
3213 nfs_invalidate_atime(data->inode);
3216 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3218 struct nfs_server *server = NFS_SERVER(data->inode);
3220 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3221 rpc_restart_call_prepare(task);
3225 __nfs4_read_done_cb(data);
3226 if (task->tk_status > 0)
3227 renew_lease(server, data->timestamp);
3231 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3234 dprintk("--> %s\n", __func__);
3236 if (!nfs4_sequence_done(task, &data->res.seq_res))
3239 return data->read_done_cb ? data->read_done_cb(task, data) :
3240 nfs4_read_done_cb(task, data);
3243 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3245 data->timestamp = jiffies;
3246 data->read_done_cb = nfs4_read_done_cb;
3247 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3250 /* Reset the the nfs_read_data to send the read to the MDS. */
3251 void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3253 dprintk("%s Reset task for i/o through\n", __func__);
3254 put_lseg(data->lseg);
3256 /* offsets will differ in the dense stripe case */
3257 data->args.offset = data->mds_offset;
3258 data->ds_clp = NULL;
3259 data->args.fh = NFS_FH(data->inode);
3260 data->read_done_cb = nfs4_read_done_cb;
3261 task->tk_ops = data->mds_ops;
3262 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3264 EXPORT_SYMBOL_GPL(nfs4_reset_read);
3266 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3268 struct inode *inode = data->inode;
3270 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3271 rpc_restart_call_prepare(task);
3274 if (task->tk_status >= 0) {
3275 renew_lease(NFS_SERVER(inode), data->timestamp);
3276 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3281 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3283 if (!nfs4_sequence_done(task, &data->res.seq_res))
3285 return data->write_done_cb ? data->write_done_cb(task, data) :
3286 nfs4_write_done_cb(task, data);
3289 /* Reset the the nfs_write_data to send the write to the MDS. */
3290 void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3292 dprintk("%s Reset task for i/o through\n", __func__);
3293 put_lseg(data->lseg);
3295 data->ds_clp = NULL;
3296 data->write_done_cb = nfs4_write_done_cb;
3297 data->args.fh = NFS_FH(data->inode);
3298 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3299 data->args.offset = data->mds_offset;
3300 data->res.fattr = &data->fattr;
3301 task->tk_ops = data->mds_ops;
3302 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3304 EXPORT_SYMBOL_GPL(nfs4_reset_write);
3306 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3308 struct nfs_server *server = NFS_SERVER(data->inode);
3311 data->args.bitmask = NULL;
3312 data->res.fattr = NULL;
3314 data->args.bitmask = server->cache_consistency_bitmask;
3315 if (!data->write_done_cb)
3316 data->write_done_cb = nfs4_write_done_cb;
3317 data->res.server = server;
3318 data->timestamp = jiffies;
3320 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3323 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3325 struct inode *inode = data->inode;
3327 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3328 rpc_restart_call_prepare(task);
3331 nfs_refresh_inode(inode, data->res.fattr);
3335 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3337 if (!nfs4_sequence_done(task, &data->res.seq_res))
3339 return data->write_done_cb(task, data);
3342 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3344 struct nfs_server *server = NFS_SERVER(data->inode);
3347 data->args.bitmask = NULL;
3348 data->res.fattr = NULL;
3350 data->args.bitmask = server->cache_consistency_bitmask;
3351 if (!data->write_done_cb)
3352 data->write_done_cb = nfs4_commit_done_cb;
3353 data->res.server = server;
3354 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3357 struct nfs4_renewdata {
3358 struct nfs_client *client;
3359 unsigned long timestamp;
3363 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3364 * standalone procedure for queueing an asynchronous RENEW.
3366 static void nfs4_renew_release(void *calldata)
3368 struct nfs4_renewdata *data = calldata;
3369 struct nfs_client *clp = data->client;
3371 if (atomic_read(&clp->cl_count) > 1)
3372 nfs4_schedule_state_renewal(clp);
3373 nfs_put_client(clp);
3377 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3379 struct nfs4_renewdata *data = calldata;
3380 struct nfs_client *clp = data->client;
3381 unsigned long timestamp = data->timestamp;
3383 if (task->tk_status < 0) {
3384 /* Unless we're shutting down, schedule state recovery! */
3385 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3387 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3388 nfs4_schedule_lease_recovery(clp);
3391 nfs4_schedule_path_down_recovery(clp);
3393 do_renew_lease(clp, timestamp);
3396 static const struct rpc_call_ops nfs4_renew_ops = {
3397 .rpc_call_done = nfs4_renew_done,
3398 .rpc_release = nfs4_renew_release,
3401 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3403 struct rpc_message msg = {
3404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3408 struct nfs4_renewdata *data;
3410 if (renew_flags == 0)
3412 if (!atomic_inc_not_zero(&clp->cl_count))
3414 data = kmalloc(sizeof(*data), GFP_NOFS);
3418 data->timestamp = jiffies;
3419 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3420 &nfs4_renew_ops, data);
3423 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3425 struct rpc_message msg = {
3426 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3430 unsigned long now = jiffies;
3433 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3436 do_renew_lease(clp, now);
3440 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3442 return (server->caps & NFS_CAP_ACLS)
3443 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3444 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3447 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3448 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3451 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3453 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3454 struct page **pages, unsigned int *pgbase)
3456 struct page *newpage, **spages;
3462 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3463 newpage = alloc_page(GFP_KERNEL);
3465 if (newpage == NULL)
3467 memcpy(page_address(newpage), buf, len);
3472 } while (buflen != 0);
3478 __free_page(spages[rc-1]);
3482 struct nfs4_cached_acl {
3488 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3490 struct nfs_inode *nfsi = NFS_I(inode);
3492 spin_lock(&inode->i_lock);
3493 kfree(nfsi->nfs4_acl);
3494 nfsi->nfs4_acl = acl;
3495 spin_unlock(&inode->i_lock);
3498 static void nfs4_zap_acl_attr(struct inode *inode)
3500 nfs4_set_cached_acl(inode, NULL);
3503 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3505 struct nfs_inode *nfsi = NFS_I(inode);
3506 struct nfs4_cached_acl *acl;
3509 spin_lock(&inode->i_lock);
3510 acl = nfsi->nfs4_acl;
3513 if (buf == NULL) /* user is just asking for length */
3515 if (acl->cached == 0)
3517 ret = -ERANGE; /* see getxattr(2) man page */
3518 if (acl->len > buflen)
3520 memcpy(buf, acl->data, acl->len);
3524 spin_unlock(&inode->i_lock);
3528 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3530 struct nfs4_cached_acl *acl;
3532 if (buf && acl_len <= PAGE_SIZE) {
3533 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3537 memcpy(acl->data, buf, acl_len);
3539 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3546 nfs4_set_cached_acl(inode, acl);
3550 * The getxattr API returns the required buffer length when called with a
3551 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3552 * the required buf. On a NULL buf, we send a page of data to the server
3553 * guessing that the ACL request can be serviced by a page. If so, we cache
3554 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3555 * the cache. If not so, we throw away the page, and cache the required
3556 * length. The next getxattr call will then produce another round trip to
3557 * the server, this time with the input buf of the required size.
3559 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3561 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3562 struct nfs_getaclargs args = {
3563 .fh = NFS_FH(inode),
3567 struct nfs_getaclres res = {
3571 struct rpc_message msg = {
3572 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3576 int ret = -ENOMEM, npages, i, acl_len = 0;
3578 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3579 /* As long as we're doing a round trip to the server anyway,
3580 * let's be prepared for a page of acl data. */
3584 for (i = 0; i < npages; i++) {
3585 pages[i] = alloc_page(GFP_KERNEL);
3590 /* for decoding across pages */
3591 res.acl_scratch = alloc_page(GFP_KERNEL);
3592 if (!res.acl_scratch)
3595 args.acl_len = npages * PAGE_SIZE;
3596 args.acl_pgbase = 0;
3597 /* Let decode_getfacl know not to fail if the ACL data is larger than
3598 * the page we send as a guess */
3600 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3601 resp_buf = page_address(pages[0]);
3603 dprintk("%s buf %p buflen %ld npages %d args.acl_len %ld\n",
3604 __func__, buf, buflen, npages, args.acl_len);
3605 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3606 &msg, &args.seq_args, &res.seq_res, 0);
3610 acl_len = res.acl_len - res.acl_data_offset;
3611 if (acl_len > args.acl_len)
3612 nfs4_write_cached_acl(inode, NULL, acl_len);
3614 nfs4_write_cached_acl(inode, resp_buf + res.acl_data_offset,
3618 if (acl_len > buflen)
3620 _copy_from_pages(buf, pages, res.acl_data_offset,
3625 for (i = 0; i < npages; i++)
3627 __free_page(pages[i]);
3628 if (res.acl_scratch)
3629 __free_page(res.acl_scratch);
3633 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3635 struct nfs4_exception exception = { };
3638 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3641 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3642 } while (exception.retry);
3646 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3648 struct nfs_server *server = NFS_SERVER(inode);
3651 if (!nfs4_server_supports_acls(server))
3653 ret = nfs_revalidate_inode(server, inode);
3656 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3657 nfs_zap_acl_cache(inode);
3658 ret = nfs4_read_cached_acl(inode, buf, buflen);
3660 /* -ENOENT is returned if there is no ACL or if there is an ACL
3661 * but no cached acl data, just the acl length */
3663 return nfs4_get_acl_uncached(inode, buf, buflen);
3666 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3668 struct nfs_server *server = NFS_SERVER(inode);
3669 struct page *pages[NFS4ACL_MAXPAGES];
3670 struct nfs_setaclargs arg = {
3671 .fh = NFS_FH(inode),
3675 struct nfs_setaclres res;
3676 struct rpc_message msg = {
3677 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3683 if (!nfs4_server_supports_acls(server))
3685 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3688 nfs_inode_return_delegation(inode);
3689 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3692 * Free each page after tx, so the only ref left is
3693 * held by the network stack
3696 put_page(pages[i-1]);
3699 * Acl update can result in inode attribute update.
3700 * so mark the attribute cache invalid.
3702 spin_lock(&inode->i_lock);
3703 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3704 spin_unlock(&inode->i_lock);
3705 nfs_access_zap_cache(inode);
3706 nfs_zap_acl_cache(inode);
3710 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3712 struct nfs4_exception exception = { };
3715 err = nfs4_handle_exception(NFS_SERVER(inode),
3716 __nfs4_proc_set_acl(inode, buf, buflen),
3718 } while (exception.retry);
3723 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3725 struct nfs_client *clp = server->nfs_client;
3727 if (task->tk_status >= 0)
3729 switch(task->tk_status) {
3730 case -NFS4ERR_DELEG_REVOKED:
3731 case -NFS4ERR_ADMIN_REVOKED:
3732 case -NFS4ERR_BAD_STATEID:
3734 nfs_remove_bad_delegation(state->inode);
3735 case -NFS4ERR_OPENMODE:
3738 nfs4_schedule_stateid_recovery(server, state);
3739 goto wait_on_recovery;
3740 case -NFS4ERR_EXPIRED:
3742 nfs4_schedule_stateid_recovery(server, state);
3743 case -NFS4ERR_STALE_STATEID:
3744 case -NFS4ERR_STALE_CLIENTID:
3745 nfs4_schedule_lease_recovery(clp);