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/slab.h>
43 #include <linux/sunrpc/clnt.h>
44 #include <linux/nfs.h>
45 #include <linux/nfs4.h>
46 #include <linux/nfs_fs.h>
47 #include <linux/nfs_page.h>
48 #include <linux/namei.h>
49 #include <linux/mount.h>
50 #include <linux/module.h>
51 #include <linux/sunrpc/bc_xprt.h>
52 #include <linux/xattr.h>
53 #include <linux/utsname.h>
56 #include "delegation.h"
62 #define NFSDBG_FACILITY NFSDBG_PROC
64 #define NFS4_POLL_RETRY_MIN (HZ/10)
65 #define NFS4_POLL_RETRY_MAX (15*HZ)
67 #define NFS4_MAX_LOOP_ON_RECOVER (10)
70 static int _nfs4_proc_open(struct nfs4_opendata *data);
71 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
72 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
73 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
74 static int _nfs4_proc_lookup(struct rpc_clnt *client, struct inode *dir,
75 const struct qstr *name, struct nfs_fh *fhandle,
76 struct nfs_fattr *fattr);
77 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
78 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
79 struct nfs_fattr *fattr, struct iattr *sattr,
80 struct nfs4_state *state);
82 /* Prevent leaks of NFSv4 errors into userland */
83 static int nfs4_map_errors(int err)
88 case -NFS4ERR_RESOURCE:
90 case -NFS4ERR_WRONGSEC:
92 case -NFS4ERR_BADOWNER:
93 case -NFS4ERR_BADNAME:
96 dprintk("%s could not handle NFSv4 error %d\n",
104 * This is our standard bitmap for GETATTR requests.
106 const u32 nfs4_fattr_bitmap[2] = {
108 | FATTR4_WORD0_CHANGE
111 | FATTR4_WORD0_FILEID,
113 | FATTR4_WORD1_NUMLINKS
115 | FATTR4_WORD1_OWNER_GROUP
116 | FATTR4_WORD1_RAWDEV
117 | FATTR4_WORD1_SPACE_USED
118 | FATTR4_WORD1_TIME_ACCESS
119 | FATTR4_WORD1_TIME_METADATA
120 | FATTR4_WORD1_TIME_MODIFY
123 const u32 nfs4_statfs_bitmap[2] = {
124 FATTR4_WORD0_FILES_AVAIL
125 | FATTR4_WORD0_FILES_FREE
126 | FATTR4_WORD0_FILES_TOTAL,
127 FATTR4_WORD1_SPACE_AVAIL
128 | FATTR4_WORD1_SPACE_FREE
129 | FATTR4_WORD1_SPACE_TOTAL
132 const u32 nfs4_pathconf_bitmap[2] = {
134 | FATTR4_WORD0_MAXNAME,
138 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
139 | FATTR4_WORD0_MAXREAD
140 | FATTR4_WORD0_MAXWRITE
141 | FATTR4_WORD0_LEASE_TIME,
142 FATTR4_WORD1_TIME_DELTA
143 | FATTR4_WORD1_FS_LAYOUT_TYPES
146 const u32 nfs4_fs_locations_bitmap[2] = {
148 | FATTR4_WORD0_CHANGE
151 | FATTR4_WORD0_FILEID
152 | FATTR4_WORD0_FS_LOCATIONS,
154 | FATTR4_WORD1_NUMLINKS
156 | FATTR4_WORD1_OWNER_GROUP
157 | FATTR4_WORD1_RAWDEV
158 | FATTR4_WORD1_SPACE_USED
159 | FATTR4_WORD1_TIME_ACCESS
160 | FATTR4_WORD1_TIME_METADATA
161 | FATTR4_WORD1_TIME_MODIFY
162 | FATTR4_WORD1_MOUNTED_ON_FILEID
165 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
166 struct nfs4_readdir_arg *readdir)
170 BUG_ON(readdir->count < 80);
172 readdir->cookie = cookie;
173 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
178 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
183 * NFSv4 servers do not return entries for '.' and '..'
184 * Therefore, we fake these entries here. We let '.'
185 * have cookie 0 and '..' have cookie 1. Note that
186 * when talking to the server, we always send cookie 0
189 start = p = kmap_atomic(*readdir->pages, KM_USER0);
192 *p++ = xdr_one; /* next */
193 *p++ = xdr_zero; /* cookie, first word */
194 *p++ = xdr_one; /* cookie, second word */
195 *p++ = xdr_one; /* entry len */
196 memcpy(p, ".\0\0\0", 4); /* entry */
198 *p++ = xdr_one; /* bitmap length */
199 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
200 *p++ = htonl(8); /* attribute buffer length */
201 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
204 *p++ = xdr_one; /* next */
205 *p++ = xdr_zero; /* cookie, first word */
206 *p++ = xdr_two; /* cookie, second word */
207 *p++ = xdr_two; /* entry len */
208 memcpy(p, "..\0\0", 4); /* entry */
210 *p++ = xdr_one; /* bitmap length */
211 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
212 *p++ = htonl(8); /* attribute buffer length */
213 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
215 readdir->pgbase = (char *)p - (char *)start;
216 readdir->count -= readdir->pgbase;
217 kunmap_atomic(start, KM_USER0);
220 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
226 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
227 nfs_wait_bit_killable, TASK_KILLABLE);
231 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
238 *timeout = NFS4_POLL_RETRY_MIN;
239 if (*timeout > NFS4_POLL_RETRY_MAX)
240 *timeout = NFS4_POLL_RETRY_MAX;
241 schedule_timeout_killable(*timeout);
242 if (fatal_signal_pending(current))
248 /* This is the error handling routine for processes that are allowed
251 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
253 struct nfs_client *clp = server->nfs_client;
254 struct nfs4_state *state = exception->state;
257 exception->retry = 0;
261 case -NFS4ERR_ADMIN_REVOKED:
262 case -NFS4ERR_BAD_STATEID:
263 case -NFS4ERR_OPENMODE:
266 nfs4_schedule_stateid_recovery(server, state);
267 goto wait_on_recovery;
268 case -NFS4ERR_STALE_STATEID:
269 case -NFS4ERR_STALE_CLIENTID:
270 case -NFS4ERR_EXPIRED:
271 nfs4_schedule_lease_recovery(clp);
272 goto wait_on_recovery;
273 #if defined(CONFIG_NFS_V4_1)
274 case -NFS4ERR_BADSESSION:
275 case -NFS4ERR_BADSLOT:
276 case -NFS4ERR_BAD_HIGH_SLOT:
277 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
278 case -NFS4ERR_DEADSESSION:
279 case -NFS4ERR_SEQ_FALSE_RETRY:
280 case -NFS4ERR_SEQ_MISORDERED:
281 dprintk("%s ERROR: %d Reset session\n", __func__,
283 nfs4_schedule_session_recovery(clp->cl_session);
284 exception->retry = 1;
286 #endif /* defined(CONFIG_NFS_V4_1) */
287 case -NFS4ERR_FILE_OPEN:
288 if (exception->timeout > HZ) {
289 /* We have retried a decent amount, time to
298 ret = nfs4_delay(server->client, &exception->timeout);
301 case -NFS4ERR_OLD_STATEID:
302 exception->retry = 1;
304 case -NFS4ERR_BADOWNER:
305 /* The following works around a Linux server bug! */
306 case -NFS4ERR_BADNAME:
307 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
308 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
309 exception->retry = 1;
310 printk(KERN_WARNING "NFS: v4 server %s "
311 "does not accept raw "
313 "Reenabling the idmapper.\n",
314 server->nfs_client->cl_hostname);
317 /* We failed to handle the error */
318 return nfs4_map_errors(ret);
320 ret = nfs4_wait_clnt_recover(clp);
322 exception->retry = 1;
327 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
329 spin_lock(&clp->cl_lock);
330 if (time_before(clp->cl_last_renewal,timestamp))
331 clp->cl_last_renewal = timestamp;
332 spin_unlock(&clp->cl_lock);
335 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
337 do_renew_lease(server->nfs_client, timestamp);
340 #if defined(CONFIG_NFS_V4_1)
343 * nfs4_free_slot - free a slot and efficiently update slot table.
345 * freeing a slot is trivially done by clearing its respective bit
347 * If the freed slotid equals highest_used_slotid we want to update it
348 * so that the server would be able to size down the slot table if needed,
349 * otherwise we know that the highest_used_slotid is still in use.
350 * When updating highest_used_slotid there may be "holes" in the bitmap
351 * so we need to scan down from highest_used_slotid to 0 looking for the now
352 * highest slotid in use.
353 * If none found, highest_used_slotid is set to -1.
355 * Must be called while holding tbl->slot_tbl_lock
358 nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
360 int free_slotid = free_slot - tbl->slots;
361 int slotid = free_slotid;
363 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
364 /* clear used bit in bitmap */
365 __clear_bit(slotid, tbl->used_slots);
367 /* update highest_used_slotid when it is freed */
368 if (slotid == tbl->highest_used_slotid) {
369 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
370 if (slotid < tbl->max_slots)
371 tbl->highest_used_slotid = slotid;
373 tbl->highest_used_slotid = -1;
375 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
376 free_slotid, tbl->highest_used_slotid);
380 * Signal state manager thread if session fore channel is drained
382 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
384 struct rpc_task *task;
386 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
387 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
389 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
393 if (ses->fc_slot_table.highest_used_slotid != -1)
396 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
397 complete(&ses->fc_slot_table.complete);
401 * Signal state manager thread if session back channel is drained
403 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
405 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
406 ses->bc_slot_table.highest_used_slotid != -1)
408 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
409 complete(&ses->bc_slot_table.complete);
412 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
414 struct nfs4_slot_table *tbl;
416 tbl = &res->sr_session->fc_slot_table;
418 /* just wake up the next guy waiting since
419 * we may have not consumed a slot after all */
420 dprintk("%s: No slot\n", __func__);
424 spin_lock(&tbl->slot_tbl_lock);
425 nfs4_free_slot(tbl, res->sr_slot);
426 nfs4_check_drain_fc_complete(res->sr_session);
427 spin_unlock(&tbl->slot_tbl_lock);
431 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
433 unsigned long timestamp;
434 struct nfs_client *clp;
437 * sr_status remains 1 if an RPC level error occurred. The server
438 * may or may not have processed the sequence operation..
439 * Proceed as if the server received and processed the sequence
442 if (res->sr_status == 1)
443 res->sr_status = NFS_OK;
445 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
449 /* Check the SEQUENCE operation status */
450 switch (res->sr_status) {
452 /* Update the slot's sequence and clientid lease timer */
453 ++res->sr_slot->seq_nr;
454 timestamp = res->sr_renewal_time;
455 clp = res->sr_session->clp;
456 do_renew_lease(clp, timestamp);
457 /* Check sequence flags */
458 if (res->sr_status_flags != 0)
459 nfs4_schedule_lease_recovery(clp);
462 /* The server detected a resend of the RPC call and
463 * returned NFS4ERR_DELAY as per Section 2.10.6.2
466 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
468 res->sr_slot - res->sr_session->fc_slot_table.slots,
469 res->sr_slot->seq_nr);
472 /* Just update the slot sequence no. */
473 ++res->sr_slot->seq_nr;
476 /* The session may be reset by one of the error handlers. */
477 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
478 nfs41_sequence_free_slot(res);
481 if (!rpc_restart_call(task))
483 rpc_delay(task, NFS4_POLL_RETRY_MAX);
487 static int nfs4_sequence_done(struct rpc_task *task,
488 struct nfs4_sequence_res *res)
490 if (res->sr_session == NULL)
492 return nfs41_sequence_done(task, res);
496 * nfs4_find_slot - efficiently look for a free slot
498 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
499 * If found, we mark the slot as used, update the highest_used_slotid,
500 * and respectively set up the sequence operation args.
501 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
503 * Note: must be called with under the slot_tbl_lock.
506 nfs4_find_slot(struct nfs4_slot_table *tbl)
509 u8 ret_id = NFS4_MAX_SLOT_TABLE;
510 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
512 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
513 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
515 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
516 if (slotid >= tbl->max_slots)
518 __set_bit(slotid, tbl->used_slots);
519 if (slotid > tbl->highest_used_slotid)
520 tbl->highest_used_slotid = slotid;
523 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
524 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
528 int nfs41_setup_sequence(struct nfs4_session *session,
529 struct nfs4_sequence_args *args,
530 struct nfs4_sequence_res *res,
532 struct rpc_task *task)
534 struct nfs4_slot *slot;
535 struct nfs4_slot_table *tbl;
538 dprintk("--> %s\n", __func__);
539 /* slot already allocated? */
540 if (res->sr_slot != NULL)
543 tbl = &session->fc_slot_table;
545 spin_lock(&tbl->slot_tbl_lock);
546 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
547 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
549 * The state manager will wait until the slot table is empty.
550 * Schedule the reset thread
552 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
553 spin_unlock(&tbl->slot_tbl_lock);
554 dprintk("%s Schedule Session Reset\n", __func__);
558 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
559 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
560 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
561 spin_unlock(&tbl->slot_tbl_lock);
562 dprintk("%s enforce FIFO order\n", __func__);
566 slotid = nfs4_find_slot(tbl);
567 if (slotid == NFS4_MAX_SLOT_TABLE) {
568 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
569 spin_unlock(&tbl->slot_tbl_lock);
570 dprintk("<-- %s: no free slots\n", __func__);
573 spin_unlock(&tbl->slot_tbl_lock);
575 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
576 slot = tbl->slots + slotid;
577 args->sa_session = session;
578 args->sa_slotid = slotid;
579 args->sa_cache_this = cache_reply;
581 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
583 res->sr_session = session;
585 res->sr_renewal_time = jiffies;
586 res->sr_status_flags = 0;
588 * sr_status is only set in decode_sequence, and so will remain
589 * set to 1 if an rpc level failure occurs.
594 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
596 int nfs4_setup_sequence(const struct nfs_server *server,
597 struct nfs4_sequence_args *args,
598 struct nfs4_sequence_res *res,
600 struct rpc_task *task)
602 struct nfs4_session *session = nfs4_get_session(server);
605 if (session == NULL) {
606 args->sa_session = NULL;
607 res->sr_session = NULL;
611 dprintk("--> %s clp %p session %p sr_slot %td\n",
612 __func__, session->clp, session, res->sr_slot ?
613 res->sr_slot - session->fc_slot_table.slots : -1);
615 ret = nfs41_setup_sequence(session, args, res, cache_reply,
618 dprintk("<-- %s status=%d\n", __func__, ret);
622 struct nfs41_call_sync_data {
623 const struct nfs_server *seq_server;
624 struct nfs4_sequence_args *seq_args;
625 struct nfs4_sequence_res *seq_res;
629 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
631 struct nfs41_call_sync_data *data = calldata;
633 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
635 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
636 data->seq_res, data->cache_reply, task))
638 rpc_call_start(task);
641 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
643 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
644 nfs41_call_sync_prepare(task, calldata);
647 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
649 struct nfs41_call_sync_data *data = calldata;
651 nfs41_sequence_done(task, data->seq_res);
654 struct rpc_call_ops nfs41_call_sync_ops = {
655 .rpc_call_prepare = nfs41_call_sync_prepare,
656 .rpc_call_done = nfs41_call_sync_done,
659 struct rpc_call_ops nfs41_call_priv_sync_ops = {
660 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
661 .rpc_call_done = nfs41_call_sync_done,
664 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
665 struct nfs_server *server,
666 struct rpc_message *msg,
667 struct nfs4_sequence_args *args,
668 struct nfs4_sequence_res *res,
673 struct rpc_task *task;
674 struct nfs41_call_sync_data data = {
675 .seq_server = server,
678 .cache_reply = cache_reply,
680 struct rpc_task_setup task_setup = {
683 .callback_ops = &nfs41_call_sync_ops,
684 .callback_data = &data
689 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
690 task = rpc_run_task(&task_setup);
694 ret = task->tk_status;
700 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
701 struct nfs_server *server,
702 struct rpc_message *msg,
703 struct nfs4_sequence_args *args,
704 struct nfs4_sequence_res *res,
707 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
711 static int nfs4_sequence_done(struct rpc_task *task,
712 struct nfs4_sequence_res *res)
716 #endif /* CONFIG_NFS_V4_1 */
718 int _nfs4_call_sync(struct rpc_clnt *clnt,
719 struct nfs_server *server,
720 struct rpc_message *msg,
721 struct nfs4_sequence_args *args,
722 struct nfs4_sequence_res *res,
725 args->sa_session = res->sr_session = NULL;
726 return rpc_call_sync(clnt, msg, 0);
730 int nfs4_call_sync(struct rpc_clnt *clnt,
731 struct nfs_server *server,
732 struct rpc_message *msg,
733 struct nfs4_sequence_args *args,
734 struct nfs4_sequence_res *res,
737 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
738 args, res, cache_reply);
741 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
743 struct nfs_inode *nfsi = NFS_I(dir);
745 spin_lock(&dir->i_lock);
746 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
747 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
748 nfs_force_lookup_revalidate(dir);
749 nfsi->change_attr = cinfo->after;
750 spin_unlock(&dir->i_lock);
753 struct nfs4_opendata {
755 struct nfs_openargs o_arg;
756 struct nfs_openres o_res;
757 struct nfs_open_confirmargs c_arg;
758 struct nfs_open_confirmres c_res;
759 struct nfs_fattr f_attr;
760 struct nfs_fattr dir_attr;
763 struct nfs4_state_owner *owner;
764 struct nfs4_state *state;
766 unsigned long timestamp;
767 unsigned int rpc_done : 1;
773 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
775 p->o_res.f_attr = &p->f_attr;
776 p->o_res.dir_attr = &p->dir_attr;
777 p->o_res.seqid = p->o_arg.seqid;
778 p->c_res.seqid = p->c_arg.seqid;
779 p->o_res.server = p->o_arg.server;
780 nfs_fattr_init(&p->f_attr);
781 nfs_fattr_init(&p->dir_attr);
784 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
785 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
786 const struct iattr *attrs,
789 struct dentry *parent = dget_parent(path->dentry);
790 struct inode *dir = parent->d_inode;
791 struct nfs_server *server = NFS_SERVER(dir);
792 struct nfs4_opendata *p;
794 p = kzalloc(sizeof(*p), gfp_mask);
797 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
798 if (p->o_arg.seqid == NULL)
804 atomic_inc(&sp->so_count);
805 p->o_arg.fh = NFS_FH(dir);
806 p->o_arg.open_flags = flags;
807 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
808 p->o_arg.clientid = server->nfs_client->cl_clientid;
809 p->o_arg.id = sp->so_owner_id.id;
810 p->o_arg.name = &p->path.dentry->d_name;
811 p->o_arg.server = server;
812 p->o_arg.bitmask = server->attr_bitmask;
813 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
814 if (flags & O_CREAT) {
817 p->o_arg.u.attrs = &p->attrs;
818 memcpy(&p->attrs, attrs, sizeof(p->attrs));
819 s = (u32 *) p->o_arg.u.verifier.data;
823 p->c_arg.fh = &p->o_res.fh;
824 p->c_arg.stateid = &p->o_res.stateid;
825 p->c_arg.seqid = p->o_arg.seqid;
826 nfs4_init_opendata_res(p);
836 static void nfs4_opendata_free(struct kref *kref)
838 struct nfs4_opendata *p = container_of(kref,
839 struct nfs4_opendata, kref);
841 nfs_free_seqid(p->o_arg.seqid);
842 if (p->state != NULL)
843 nfs4_put_open_state(p->state);
844 nfs4_put_state_owner(p->owner);
850 static void nfs4_opendata_put(struct nfs4_opendata *p)
853 kref_put(&p->kref, nfs4_opendata_free);
856 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
860 ret = rpc_wait_for_completion_task(task);
864 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
868 if (open_mode & O_EXCL)
870 switch (mode & (FMODE_READ|FMODE_WRITE)) {
872 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
873 && state->n_rdonly != 0;
876 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
877 && state->n_wronly != 0;
879 case FMODE_READ|FMODE_WRITE:
880 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
881 && state->n_rdwr != 0;
887 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
889 if ((delegation->type & fmode) != fmode)
891 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
893 nfs_mark_delegation_referenced(delegation);
897 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
906 case FMODE_READ|FMODE_WRITE:
909 nfs4_state_set_mode_locked(state, state->state | fmode);
912 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
914 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
915 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
916 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
919 set_bit(NFS_O_RDONLY_STATE, &state->flags);
922 set_bit(NFS_O_WRONLY_STATE, &state->flags);
924 case FMODE_READ|FMODE_WRITE:
925 set_bit(NFS_O_RDWR_STATE, &state->flags);
929 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
931 write_seqlock(&state->seqlock);
932 nfs_set_open_stateid_locked(state, stateid, fmode);
933 write_sequnlock(&state->seqlock);
936 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
939 * Protect the call to nfs4_state_set_mode_locked and
940 * serialise the stateid update
942 write_seqlock(&state->seqlock);
943 if (deleg_stateid != NULL) {
944 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
945 set_bit(NFS_DELEGATED_STATE, &state->flags);
947 if (open_stateid != NULL)
948 nfs_set_open_stateid_locked(state, open_stateid, fmode);
949 write_sequnlock(&state->seqlock);
950 spin_lock(&state->owner->so_lock);
951 update_open_stateflags(state, fmode);
952 spin_unlock(&state->owner->so_lock);
955 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
957 struct nfs_inode *nfsi = NFS_I(state->inode);
958 struct nfs_delegation *deleg_cur;
961 fmode &= (FMODE_READ|FMODE_WRITE);
964 deleg_cur = rcu_dereference(nfsi->delegation);
965 if (deleg_cur == NULL)
968 spin_lock(&deleg_cur->lock);
969 if (nfsi->delegation != deleg_cur ||
970 (deleg_cur->type & fmode) != fmode)
971 goto no_delegation_unlock;
973 if (delegation == NULL)
974 delegation = &deleg_cur->stateid;
975 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
976 goto no_delegation_unlock;
978 nfs_mark_delegation_referenced(deleg_cur);
979 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
981 no_delegation_unlock:
982 spin_unlock(&deleg_cur->lock);
986 if (!ret && open_stateid != NULL) {
987 __update_open_stateid(state, open_stateid, NULL, fmode);
995 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
997 struct nfs_delegation *delegation;
1000 delegation = rcu_dereference(NFS_I(inode)->delegation);
1001 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1006 nfs_inode_return_delegation(inode);
1009 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1011 struct nfs4_state *state = opendata->state;
1012 struct nfs_inode *nfsi = NFS_I(state->inode);
1013 struct nfs_delegation *delegation;
1014 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1015 fmode_t fmode = opendata->o_arg.fmode;
1016 nfs4_stateid stateid;
1020 if (can_open_cached(state, fmode, open_mode)) {
1021 spin_lock(&state->owner->so_lock);
1022 if (can_open_cached(state, fmode, open_mode)) {
1023 update_open_stateflags(state, fmode);
1024 spin_unlock(&state->owner->so_lock);
1025 goto out_return_state;
1027 spin_unlock(&state->owner->so_lock);
1030 delegation = rcu_dereference(nfsi->delegation);
1031 if (delegation == NULL ||
1032 !can_open_delegated(delegation, fmode)) {
1036 /* Save the delegation */
1037 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1039 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1044 /* Try to update the stateid using the delegation */
1045 if (update_open_stateid(state, NULL, &stateid, fmode))
1046 goto out_return_state;
1049 return ERR_PTR(ret);
1051 atomic_inc(&state->count);
1055 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1057 struct inode *inode;
1058 struct nfs4_state *state = NULL;
1059 struct nfs_delegation *delegation;
1062 if (!data->rpc_done) {
1063 state = nfs4_try_open_cached(data);
1068 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1070 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1071 ret = PTR_ERR(inode);
1075 state = nfs4_get_open_state(inode, data->owner);
1078 if (data->o_res.delegation_type != 0) {
1079 int delegation_flags = 0;
1082 delegation = rcu_dereference(NFS_I(inode)->delegation);
1084 delegation_flags = delegation->flags;
1086 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1087 nfs_inode_set_delegation(state->inode,
1088 data->owner->so_cred,
1091 nfs_inode_reclaim_delegation(state->inode,
1092 data->owner->so_cred,
1096 update_open_stateid(state, &data->o_res.stateid, NULL,
1104 return ERR_PTR(ret);
1107 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1109 struct nfs_inode *nfsi = NFS_I(state->inode);
1110 struct nfs_open_context *ctx;
1112 spin_lock(&state->inode->i_lock);
1113 list_for_each_entry(ctx, &nfsi->open_files, list) {
1114 if (ctx->state != state)
1116 get_nfs_open_context(ctx);
1117 spin_unlock(&state->inode->i_lock);
1120 spin_unlock(&state->inode->i_lock);
1121 return ERR_PTR(-ENOENT);
1124 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1126 struct nfs4_opendata *opendata;
1128 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
1129 if (opendata == NULL)
1130 return ERR_PTR(-ENOMEM);
1131 opendata->state = state;
1132 atomic_inc(&state->count);
1136 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1138 struct nfs4_state *newstate;
1141 opendata->o_arg.open_flags = 0;
1142 opendata->o_arg.fmode = fmode;
1143 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1144 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1145 nfs4_init_opendata_res(opendata);
1146 ret = _nfs4_recover_proc_open(opendata);
1149 newstate = nfs4_opendata_to_nfs4_state(opendata);
1150 if (IS_ERR(newstate))
1151 return PTR_ERR(newstate);
1152 nfs4_close_state(&opendata->path, newstate, fmode);
1157 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1159 struct nfs4_state *newstate;
1162 /* memory barrier prior to reading state->n_* */
1163 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1165 if (state->n_rdwr != 0) {
1166 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1167 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1170 if (newstate != state)
1173 if (state->n_wronly != 0) {
1174 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1175 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1178 if (newstate != state)
1181 if (state->n_rdonly != 0) {
1182 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1183 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1186 if (newstate != state)
1190 * We may have performed cached opens for all three recoveries.
1191 * Check if we need to update the current stateid.
1193 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1194 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1195 write_seqlock(&state->seqlock);
1196 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1197 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1198 write_sequnlock(&state->seqlock);
1205 * reclaim state on the server after a reboot.
1207 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1209 struct nfs_delegation *delegation;
1210 struct nfs4_opendata *opendata;
1211 fmode_t delegation_type = 0;
1214 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1215 if (IS_ERR(opendata))
1216 return PTR_ERR(opendata);
1217 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1218 opendata->o_arg.fh = NFS_FH(state->inode);
1220 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1221 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1222 delegation_type = delegation->type;
1224 opendata->o_arg.u.delegation_type = delegation_type;
1225 status = nfs4_open_recover(opendata, state);
1226 nfs4_opendata_put(opendata);
1230 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1232 struct nfs_server *server = NFS_SERVER(state->inode);
1233 struct nfs4_exception exception = { };
1236 err = _nfs4_do_open_reclaim(ctx, state);
1237 if (err != -NFS4ERR_DELAY)
1239 nfs4_handle_exception(server, err, &exception);
1240 } while (exception.retry);
1244 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1246 struct nfs_open_context *ctx;
1249 ctx = nfs4_state_find_open_context(state);
1251 return PTR_ERR(ctx);
1252 ret = nfs4_do_open_reclaim(ctx, state);
1253 put_nfs_open_context(ctx);
1257 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1259 struct nfs4_opendata *opendata;
1262 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1263 if (IS_ERR(opendata))
1264 return PTR_ERR(opendata);
1265 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1266 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1267 sizeof(opendata->o_arg.u.delegation.data));
1268 ret = nfs4_open_recover(opendata, state);
1269 nfs4_opendata_put(opendata);
1273 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1275 struct nfs4_exception exception = { };
1276 struct nfs_server *server = NFS_SERVER(state->inode);
1279 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1285 case -NFS4ERR_BADSESSION:
1286 case -NFS4ERR_BADSLOT:
1287 case -NFS4ERR_BAD_HIGH_SLOT:
1288 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1289 case -NFS4ERR_DEADSESSION:
1290 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
1292 case -NFS4ERR_STALE_CLIENTID:
1293 case -NFS4ERR_STALE_STATEID:
1294 case -NFS4ERR_EXPIRED:
1295 /* Don't recall a delegation if it was lost */
1296 nfs4_schedule_lease_recovery(server->nfs_client);
1300 * The show must go on: exit, but mark the
1301 * stateid as needing recovery.
1303 case -NFS4ERR_ADMIN_REVOKED:
1304 case -NFS4ERR_BAD_STATEID:
1305 nfs4_schedule_stateid_recovery(server, state);
1308 * User RPCSEC_GSS context has expired.
1309 * We cannot recover this stateid now, so
1310 * skip it and allow recovery thread to
1317 err = nfs4_handle_exception(server, err, &exception);
1318 } while (exception.retry);
1323 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1325 struct nfs4_opendata *data = calldata;
1327 data->rpc_status = task->tk_status;
1328 if (data->rpc_status == 0) {
1329 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1330 sizeof(data->o_res.stateid.data));
1331 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1332 renew_lease(data->o_res.server, data->timestamp);
1337 static void nfs4_open_confirm_release(void *calldata)
1339 struct nfs4_opendata *data = calldata;
1340 struct nfs4_state *state = NULL;
1342 /* If this request hasn't been cancelled, do nothing */
1343 if (data->cancelled == 0)
1345 /* In case of error, no cleanup! */
1346 if (!data->rpc_done)
1348 state = nfs4_opendata_to_nfs4_state(data);
1350 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1352 nfs4_opendata_put(data);
1355 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1356 .rpc_call_done = nfs4_open_confirm_done,
1357 .rpc_release = nfs4_open_confirm_release,
1361 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1363 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1365 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1366 struct rpc_task *task;
1367 struct rpc_message msg = {
1368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1369 .rpc_argp = &data->c_arg,
1370 .rpc_resp = &data->c_res,
1371 .rpc_cred = data->owner->so_cred,
1373 struct rpc_task_setup task_setup_data = {
1374 .rpc_client = server->client,
1375 .rpc_message = &msg,
1376 .callback_ops = &nfs4_open_confirm_ops,
1377 .callback_data = data,
1378 .workqueue = nfsiod_workqueue,
1379 .flags = RPC_TASK_ASYNC,
1383 kref_get(&data->kref);
1385 data->rpc_status = 0;
1386 data->timestamp = jiffies;
1387 task = rpc_run_task(&task_setup_data);
1389 return PTR_ERR(task);
1390 status = nfs4_wait_for_completion_rpc_task(task);
1392 data->cancelled = 1;
1395 status = data->rpc_status;
1400 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1402 struct nfs4_opendata *data = calldata;
1403 struct nfs4_state_owner *sp = data->owner;
1405 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1408 * Check if we still need to send an OPEN call, or if we can use
1409 * a delegation instead.
1411 if (data->state != NULL) {
1412 struct nfs_delegation *delegation;
1414 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1417 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1418 if (delegation != NULL &&
1419 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1425 /* Update sequence id. */
1426 data->o_arg.id = sp->so_owner_id.id;
1427 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1428 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1429 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1430 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1432 data->timestamp = jiffies;
1433 if (nfs4_setup_sequence(data->o_arg.server,
1434 &data->o_arg.seq_args,
1435 &data->o_res.seq_res, 1, task))
1437 rpc_call_start(task);
1440 task->tk_action = NULL;
1444 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1446 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1447 nfs4_open_prepare(task, calldata);
1450 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1452 struct nfs4_opendata *data = calldata;
1454 data->rpc_status = task->tk_status;
1456 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1459 if (task->tk_status == 0) {
1460 switch (data->o_res.f_attr->mode & S_IFMT) {
1464 data->rpc_status = -ELOOP;
1467 data->rpc_status = -EISDIR;
1470 data->rpc_status = -ENOTDIR;
1472 renew_lease(data->o_res.server, data->timestamp);
1473 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1474 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1479 static void nfs4_open_release(void *calldata)
1481 struct nfs4_opendata *data = calldata;
1482 struct nfs4_state *state = NULL;
1484 /* If this request hasn't been cancelled, do nothing */
1485 if (data->cancelled == 0)
1487 /* In case of error, no cleanup! */
1488 if (data->rpc_status != 0 || !data->rpc_done)
1490 /* In case we need an open_confirm, no cleanup! */
1491 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1493 state = nfs4_opendata_to_nfs4_state(data);
1495 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1497 nfs4_opendata_put(data);
1500 static const struct rpc_call_ops nfs4_open_ops = {
1501 .rpc_call_prepare = nfs4_open_prepare,
1502 .rpc_call_done = nfs4_open_done,
1503 .rpc_release = nfs4_open_release,
1506 static const struct rpc_call_ops nfs4_recover_open_ops = {
1507 .rpc_call_prepare = nfs4_recover_open_prepare,
1508 .rpc_call_done = nfs4_open_done,
1509 .rpc_release = nfs4_open_release,
1512 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1514 struct inode *dir = data->dir->d_inode;
1515 struct nfs_server *server = NFS_SERVER(dir);
1516 struct nfs_openargs *o_arg = &data->o_arg;
1517 struct nfs_openres *o_res = &data->o_res;
1518 struct rpc_task *task;
1519 struct rpc_message msg = {
1520 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1523 .rpc_cred = data->owner->so_cred,
1525 struct rpc_task_setup task_setup_data = {
1526 .rpc_client = server->client,
1527 .rpc_message = &msg,
1528 .callback_ops = &nfs4_open_ops,
1529 .callback_data = data,
1530 .workqueue = nfsiod_workqueue,
1531 .flags = RPC_TASK_ASYNC,
1535 kref_get(&data->kref);
1537 data->rpc_status = 0;
1538 data->cancelled = 0;
1540 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1541 task = rpc_run_task(&task_setup_data);
1543 return PTR_ERR(task);
1544 status = nfs4_wait_for_completion_rpc_task(task);
1546 data->cancelled = 1;
1549 status = data->rpc_status;
1555 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1557 struct inode *dir = data->dir->d_inode;
1558 struct nfs_openres *o_res = &data->o_res;
1561 status = nfs4_run_open_task(data, 1);
1562 if (status != 0 || !data->rpc_done)
1565 nfs_refresh_inode(dir, o_res->dir_attr);
1567 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1568 status = _nfs4_proc_open_confirm(data);
1577 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1579 static int _nfs4_proc_open(struct nfs4_opendata *data)
1581 struct inode *dir = data->dir->d_inode;
1582 struct nfs_server *server = NFS_SERVER(dir);
1583 struct nfs_openargs *o_arg = &data->o_arg;
1584 struct nfs_openres *o_res = &data->o_res;
1587 status = nfs4_run_open_task(data, 0);
1588 if (status != 0 || !data->rpc_done)
1591 if (o_arg->open_flags & O_CREAT) {
1592 update_changeattr(dir, &o_res->cinfo);
1593 nfs_post_op_update_inode(dir, o_res->dir_attr);
1595 nfs_refresh_inode(dir, o_res->dir_attr);
1596 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1597 server->caps &= ~NFS_CAP_POSIX_LOCK;
1598 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1599 status = _nfs4_proc_open_confirm(data);
1603 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1604 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1608 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1613 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1614 ret = nfs4_wait_clnt_recover(clp);
1617 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1618 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1620 nfs4_schedule_state_manager(clp);
1626 static int nfs4_recover_expired_lease(struct nfs_server *server)
1628 return nfs4_client_recover_expired_lease(server->nfs_client);
1633 * reclaim state on the server after a network partition.
1634 * Assumes caller holds the appropriate lock
1636 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1638 struct nfs4_opendata *opendata;
1641 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1642 if (IS_ERR(opendata))
1643 return PTR_ERR(opendata);
1644 ret = nfs4_open_recover(opendata, state);
1646 d_drop(ctx->path.dentry);
1647 nfs4_opendata_put(opendata);
1651 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1653 struct nfs_server *server = NFS_SERVER(state->inode);
1654 struct nfs4_exception exception = { };
1658 err = _nfs4_open_expired(ctx, state);
1662 case -NFS4ERR_GRACE:
1663 case -NFS4ERR_DELAY:
1664 nfs4_handle_exception(server, err, &exception);
1667 } while (exception.retry);
1672 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1674 struct nfs_open_context *ctx;
1677 ctx = nfs4_state_find_open_context(state);
1679 return PTR_ERR(ctx);
1680 ret = nfs4_do_open_expired(ctx, state);
1681 put_nfs_open_context(ctx);
1686 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1687 * fields corresponding to attributes that were used to store the verifier.
1688 * Make sure we clobber those fields in the later setattr call
1690 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1692 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1693 !(sattr->ia_valid & ATTR_ATIME_SET))
1694 sattr->ia_valid |= ATTR_ATIME;
1696 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1697 !(sattr->ia_valid & ATTR_MTIME_SET))
1698 sattr->ia_valid |= ATTR_MTIME;
1702 * Returns a referenced nfs4_state
1704 static int _nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
1706 struct nfs4_state_owner *sp;
1707 struct nfs4_state *state = NULL;
1708 struct nfs_server *server = NFS_SERVER(dir);
1709 struct nfs4_opendata *opendata;
1712 /* Protect against reboot recovery conflicts */
1714 if (!(sp = nfs4_get_state_owner(server, cred))) {
1715 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1718 status = nfs4_recover_expired_lease(server);
1720 goto err_put_state_owner;
1721 if (path->dentry->d_inode != NULL)
1722 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1724 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
1725 if (opendata == NULL)
1726 goto err_put_state_owner;
1728 if (path->dentry->d_inode != NULL)
1729 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1731 status = _nfs4_proc_open(opendata);
1733 goto err_opendata_put;
1735 state = nfs4_opendata_to_nfs4_state(opendata);
1736 status = PTR_ERR(state);
1738 goto err_opendata_put;
1739 if (server->caps & NFS_CAP_POSIX_LOCK)
1740 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1742 if (opendata->o_arg.open_flags & O_EXCL) {
1743 nfs4_exclusive_attrset(opendata, sattr);
1745 nfs_fattr_init(opendata->o_res.f_attr);
1746 status = nfs4_do_setattr(state->inode, cred,
1747 opendata->o_res.f_attr, sattr,
1750 nfs_setattr_update_inode(state->inode, sattr);
1751 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1753 nfs4_opendata_put(opendata);
1754 nfs4_put_state_owner(sp);
1758 nfs4_opendata_put(opendata);
1759 err_put_state_owner:
1760 nfs4_put_state_owner(sp);
1767 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
1769 struct nfs4_exception exception = { };
1770 struct nfs4_state *res;
1774 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1777 /* NOTE: BAD_SEQID means the server and client disagree about the
1778 * book-keeping w.r.t. state-changing operations
1779 * (OPEN/CLOSE/LOCK/LOCKU...)
1780 * It is actually a sign of a bug on the client or on the server.
1782 * If we receive a BAD_SEQID error in the particular case of
1783 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1784 * have unhashed the old state_owner for us, and that we can
1785 * therefore safely retry using a new one. We should still warn
1786 * the user though...
1788 if (status == -NFS4ERR_BAD_SEQID) {
1789 printk(KERN_WARNING "NFS: v4 server %s "
1790 " returned a bad sequence-id error!\n",
1791 NFS_SERVER(dir)->nfs_client->cl_hostname);
1792 exception.retry = 1;
1796 * BAD_STATEID on OPEN means that the server cancelled our
1797 * state before it received the OPEN_CONFIRM.
1798 * Recover by retrying the request as per the discussion
1799 * on Page 181 of RFC3530.
1801 if (status == -NFS4ERR_BAD_STATEID) {
1802 exception.retry = 1;
1805 if (status == -EAGAIN) {
1806 /* We must have found a delegation */
1807 exception.retry = 1;
1810 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1811 status, &exception));
1812 } while (exception.retry);
1816 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1817 struct nfs_fattr *fattr, struct iattr *sattr,
1818 struct nfs4_state *state)
1820 struct nfs_server *server = NFS_SERVER(inode);
1821 struct nfs_setattrargs arg = {
1822 .fh = NFS_FH(inode),
1825 .bitmask = server->attr_bitmask,
1827 struct nfs_setattrres res = {
1831 struct rpc_message msg = {
1832 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1837 unsigned long timestamp = jiffies;
1840 nfs_fattr_init(fattr);
1842 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1843 /* Use that stateid */
1844 } else if (state != NULL) {
1845 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
1847 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1849 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1850 if (status == 0 && state != NULL)
1851 renew_lease(server, timestamp);
1855 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1856 struct nfs_fattr *fattr, struct iattr *sattr,
1857 struct nfs4_state *state)
1859 struct nfs_server *server = NFS_SERVER(inode);
1860 struct nfs4_exception exception = { };
1863 err = nfs4_handle_exception(server,
1864 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1866 } while (exception.retry);
1870 struct nfs4_closedata {
1872 struct inode *inode;
1873 struct nfs4_state *state;
1874 struct nfs_closeargs arg;
1875 struct nfs_closeres res;
1876 struct nfs_fattr fattr;
1877 unsigned long timestamp;
1882 static void nfs4_free_closedata(void *data)
1884 struct nfs4_closedata *calldata = data;
1885 struct nfs4_state_owner *sp = calldata->state->owner;
1888 pnfs_roc_release(calldata->state->inode);
1889 nfs4_put_open_state(calldata->state);
1890 nfs_free_seqid(calldata->arg.seqid);
1891 nfs4_put_state_owner(sp);
1892 path_put(&calldata->path);
1896 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1899 spin_lock(&state->owner->so_lock);
1900 if (!(fmode & FMODE_READ))
1901 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1902 if (!(fmode & FMODE_WRITE))
1903 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1904 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1905 spin_unlock(&state->owner->so_lock);
1908 static void nfs4_close_done(struct rpc_task *task, void *data)
1910 struct nfs4_closedata *calldata = data;
1911 struct nfs4_state *state = calldata->state;
1912 struct nfs_server *server = NFS_SERVER(calldata->inode);
1914 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1916 /* hmm. we are done with the inode, and in the process of freeing
1917 * the state_owner. we keep this around to process errors
1919 switch (task->tk_status) {
1922 pnfs_roc_set_barrier(state->inode,
1923 calldata->roc_barrier);
1924 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1925 renew_lease(server, calldata->timestamp);
1926 nfs4_close_clear_stateid_flags(state,
1927 calldata->arg.fmode);
1929 case -NFS4ERR_STALE_STATEID:
1930 case -NFS4ERR_OLD_STATEID:
1931 case -NFS4ERR_BAD_STATEID:
1932 case -NFS4ERR_EXPIRED:
1933 if (calldata->arg.fmode == 0)
1936 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1937 rpc_restart_call_prepare(task);
1939 nfs_release_seqid(calldata->arg.seqid);
1940 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1943 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1945 struct nfs4_closedata *calldata = data;
1946 struct nfs4_state *state = calldata->state;
1949 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1952 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1953 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
1954 spin_lock(&state->owner->so_lock);
1955 /* Calculate the change in open mode */
1956 if (state->n_rdwr == 0) {
1957 if (state->n_rdonly == 0) {
1958 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1959 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1960 calldata->arg.fmode &= ~FMODE_READ;
1962 if (state->n_wronly == 0) {
1963 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1964 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1965 calldata->arg.fmode &= ~FMODE_WRITE;
1968 spin_unlock(&state->owner->so_lock);
1971 /* Note: exit _without_ calling nfs4_close_done */
1972 task->tk_action = NULL;
1976 if (calldata->arg.fmode == 0) {
1977 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1978 if (calldata->roc &&
1979 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
1980 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
1986 nfs_fattr_init(calldata->res.fattr);
1987 calldata->timestamp = jiffies;
1988 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
1989 &calldata->arg.seq_args, &calldata->res.seq_res,
1992 rpc_call_start(task);
1995 static const struct rpc_call_ops nfs4_close_ops = {
1996 .rpc_call_prepare = nfs4_close_prepare,
1997 .rpc_call_done = nfs4_close_done,
1998 .rpc_release = nfs4_free_closedata,
2002 * It is possible for data to be read/written from a mem-mapped file
2003 * after the sys_close call (which hits the vfs layer as a flush).
2004 * This means that we can't safely call nfsv4 close on a file until
2005 * the inode is cleared. This in turn means that we are not good
2006 * NFSv4 citizens - we do not indicate to the server to update the file's
2007 * share state even when we are done with one of the three share
2008 * stateid's in the inode.
2010 * NOTE: Caller must be holding the sp->so_owner semaphore!
2012 int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
2014 struct nfs_server *server = NFS_SERVER(state->inode);
2015 struct nfs4_closedata *calldata;
2016 struct nfs4_state_owner *sp = state->owner;
2017 struct rpc_task *task;
2018 struct rpc_message msg = {
2019 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2020 .rpc_cred = state->owner->so_cred,
2022 struct rpc_task_setup task_setup_data = {
2023 .rpc_client = server->client,
2024 .rpc_message = &msg,
2025 .callback_ops = &nfs4_close_ops,
2026 .workqueue = nfsiod_workqueue,
2027 .flags = RPC_TASK_ASYNC,
2029 int status = -ENOMEM;
2031 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2032 if (calldata == NULL)
2034 calldata->inode = state->inode;
2035 calldata->state = state;
2036 calldata->arg.fh = NFS_FH(state->inode);
2037 calldata->arg.stateid = &state->open_stateid;
2038 /* Serialization for the sequence id */
2039 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2040 if (calldata->arg.seqid == NULL)
2041 goto out_free_calldata;
2042 calldata->arg.fmode = 0;
2043 calldata->arg.bitmask = server->cache_consistency_bitmask;
2044 calldata->res.fattr = &calldata->fattr;
2045 calldata->res.seqid = calldata->arg.seqid;
2046 calldata->res.server = server;
2047 calldata->roc = roc;
2049 calldata->path = *path;
2051 msg.rpc_argp = &calldata->arg;
2052 msg.rpc_resp = &calldata->res;
2053 task_setup_data.callback_data = calldata;
2054 task = rpc_run_task(&task_setup_data);
2056 return PTR_ERR(task);
2059 status = rpc_wait_for_completion_task(task);
2066 pnfs_roc_release(state->inode);
2067 nfs4_put_open_state(state);
2068 nfs4_put_state_owner(sp);
2072 static struct inode *
2073 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2075 struct nfs4_state *state;
2077 /* Protect against concurrent sillydeletes */
2078 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
2080 return ERR_CAST(state);
2082 return igrab(state->inode);
2085 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2087 if (ctx->state == NULL)
2090 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2092 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2095 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2097 struct nfs4_server_caps_arg args = {
2100 struct nfs4_server_caps_res res = {};
2101 struct rpc_message msg = {
2102 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2108 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2110 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2111 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2112 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2113 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2114 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2115 NFS_CAP_CTIME|NFS_CAP_MTIME);
2116 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2117 server->caps |= NFS_CAP_ACLS;
2118 if (res.has_links != 0)
2119 server->caps |= NFS_CAP_HARDLINKS;
2120 if (res.has_symlinks != 0)
2121 server->caps |= NFS_CAP_SYMLINKS;
2122 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2123 server->caps |= NFS_CAP_FILEID;
2124 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2125 server->caps |= NFS_CAP_MODE;
2126 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2127 server->caps |= NFS_CAP_NLINK;
2128 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2129 server->caps |= NFS_CAP_OWNER;
2130 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2131 server->caps |= NFS_CAP_OWNER_GROUP;
2132 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2133 server->caps |= NFS_CAP_ATIME;
2134 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2135 server->caps |= NFS_CAP_CTIME;
2136 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2137 server->caps |= NFS_CAP_MTIME;
2139 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2140 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2141 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2142 server->acl_bitmask = res.acl_bitmask;
2148 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2150 struct nfs4_exception exception = { };
2153 err = nfs4_handle_exception(server,
2154 _nfs4_server_capabilities(server, fhandle),
2156 } while (exception.retry);
2160 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2161 struct nfs_fsinfo *info)
2163 struct nfs4_lookup_root_arg args = {
2164 .bitmask = nfs4_fattr_bitmap,
2166 struct nfs4_lookup_res res = {
2168 .fattr = info->fattr,
2171 struct rpc_message msg = {
2172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2177 nfs_fattr_init(info->fattr);
2178 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2181 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2182 struct nfs_fsinfo *info)
2184 struct nfs4_exception exception = { };
2187 err = nfs4_handle_exception(server,
2188 _nfs4_lookup_root(server, fhandle, info),
2190 } while (exception.retry);
2195 * get the file handle for the "/" directory on the server
2197 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2198 struct nfs_fsinfo *info)
2202 status = nfs4_lookup_root(server, fhandle, info);
2204 status = nfs4_server_capabilities(server, fhandle);
2206 status = nfs4_do_fsinfo(server, fhandle, info);
2207 return nfs4_map_errors(status);
2211 * Get locations and (maybe) other attributes of a referral.
2212 * Note that we'll actually follow the referral later when
2213 * we detect fsid mismatch in inode revalidation
2215 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2217 int status = -ENOMEM;
2218 struct page *page = NULL;
2219 struct nfs4_fs_locations *locations = NULL;
2221 page = alloc_page(GFP_KERNEL);
2224 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2225 if (locations == NULL)
2228 status = nfs4_proc_fs_locations(dir, name, locations, page);
2231 /* Make sure server returned a different fsid for the referral */
2232 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2233 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
2238 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2239 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2241 fattr->mode = S_IFDIR;
2242 memset(fhandle, 0, sizeof(struct nfs_fh));
2250 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2252 struct nfs4_getattr_arg args = {
2254 .bitmask = server->attr_bitmask,
2256 struct nfs4_getattr_res res = {
2260 struct rpc_message msg = {
2261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2266 nfs_fattr_init(fattr);
2267 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2270 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2272 struct nfs4_exception exception = { };
2275 err = nfs4_handle_exception(server,
2276 _nfs4_proc_getattr(server, fhandle, fattr),
2278 } while (exception.retry);
2283 * The file is not closed if it is opened due to the a request to change
2284 * the size of the file. The open call will not be needed once the
2285 * VFS layer lookup-intents are implemented.
2287 * Close is called when the inode is destroyed.
2288 * If we haven't opened the file for O_WRONLY, we
2289 * need to in the size_change case to obtain a stateid.
2292 * Because OPEN is always done by name in nfsv4, it is
2293 * possible that we opened a different file by the same
2294 * name. We can recognize this race condition, but we
2295 * can't do anything about it besides returning an error.
2297 * This will be fixed with VFS changes (lookup-intent).
2300 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2301 struct iattr *sattr)
2303 struct inode *inode = dentry->d_inode;
2304 struct rpc_cred *cred = NULL;
2305 struct nfs4_state *state = NULL;
2308 nfs_fattr_init(fattr);
2310 /* Search for an existing open(O_WRITE) file */
2311 if (sattr->ia_valid & ATTR_FILE) {
2312 struct nfs_open_context *ctx;
2314 ctx = nfs_file_open_context(sattr->ia_file);
2321 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2323 nfs_setattr_update_inode(inode, sattr);
2327 static int _nfs4_proc_lookupfh(struct rpc_clnt *clnt, struct nfs_server *server,
2328 const struct nfs_fh *dirfh, const struct qstr *name,
2329 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2332 struct nfs4_lookup_arg args = {
2333 .bitmask = server->attr_bitmask,
2337 struct nfs4_lookup_res res = {
2342 struct rpc_message msg = {
2343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2348 nfs_fattr_init(fattr);
2350 dprintk("NFS call lookupfh %s\n", name->name);
2351 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2352 dprintk("NFS reply lookupfh: %d\n", status);
2356 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2357 struct qstr *name, struct nfs_fh *fhandle,
2358 struct nfs_fattr *fattr)
2360 struct nfs4_exception exception = { };
2363 err = _nfs4_proc_lookupfh(server->client, server, dirfh, name, fhandle, fattr);
2365 if (err == -NFS4ERR_MOVED) {
2369 err = nfs4_handle_exception(server, err, &exception);
2370 } while (exception.retry);
2374 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2375 const struct qstr *name, struct nfs_fh *fhandle,
2376 struct nfs_fattr *fattr)
2380 dprintk("NFS call lookup %s\n", name->name);
2381 status = _nfs4_proc_lookupfh(clnt, NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2382 if (status == -NFS4ERR_MOVED)
2383 status = nfs4_get_referral(dir, name, fattr, fhandle);
2384 dprintk("NFS reply lookup: %d\n", status);
2388 void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2390 memset(fh, 0, sizeof(struct nfs_fh));
2391 fattr->fsid.major = 1;
2392 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2393 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2394 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2398 static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2399 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2401 struct nfs4_exception exception = { };
2404 err = nfs4_handle_exception(NFS_SERVER(dir),
2405 _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr),
2408 nfs_fixup_secinfo_attributes(fattr, fhandle);
2409 } while (exception.retry);
2413 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2415 struct nfs_server *server = NFS_SERVER(inode);
2416 struct nfs4_accessargs args = {
2417 .fh = NFS_FH(inode),
2418 .bitmask = server->attr_bitmask,
2420 struct nfs4_accessres res = {
2423 struct rpc_message msg = {
2424 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2427 .rpc_cred = entry->cred,
2429 int mode = entry->mask;
2433 * Determine which access bits we want to ask for...
2435 if (mode & MAY_READ)
2436 args.access |= NFS4_ACCESS_READ;
2437 if (S_ISDIR(inode->i_mode)) {
2438 if (mode & MAY_WRITE)
2439 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2440 if (mode & MAY_EXEC)
2441 args.access |= NFS4_ACCESS_LOOKUP;
2443 if (mode & MAY_WRITE)
2444 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2445 if (mode & MAY_EXEC)
2446 args.access |= NFS4_ACCESS_EXECUTE;
2449 res.fattr = nfs_alloc_fattr();
2450 if (res.fattr == NULL)
2453 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2456 if (res.access & NFS4_ACCESS_READ)
2457 entry->mask |= MAY_READ;
2458 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2459 entry->mask |= MAY_WRITE;
2460 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2461 entry->mask |= MAY_EXEC;
2462 nfs_refresh_inode(inode, res.fattr);
2464 nfs_free_fattr(res.fattr);
2468 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2470 struct nfs4_exception exception = { };
2473 err = nfs4_handle_exception(NFS_SERVER(inode),
2474 _nfs4_proc_access(inode, entry),
2476 } while (exception.retry);
2481 * TODO: For the time being, we don't try to get any attributes
2482 * along with any of the zero-copy operations READ, READDIR,
2485 * In the case of the first three, we want to put the GETATTR
2486 * after the read-type operation -- this is because it is hard
2487 * to predict the length of a GETATTR response in v4, and thus
2488 * align the READ data correctly. This means that the GETATTR
2489 * may end up partially falling into the page cache, and we should
2490 * shift it into the 'tail' of the xdr_buf before processing.
2491 * To do this efficiently, we need to know the total length
2492 * of data received, which doesn't seem to be available outside
2495 * In the case of WRITE, we also want to put the GETATTR after
2496 * the operation -- in this case because we want to make sure
2497 * we get the post-operation mtime and size. This means that
2498 * we can't use xdr_encode_pages() as written: we need a variant
2499 * of it which would leave room in the 'tail' iovec.
2501 * Both of these changes to the XDR layer would in fact be quite
2502 * minor, but I decided to leave them for a subsequent patch.
2504 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2505 unsigned int pgbase, unsigned int pglen)
2507 struct nfs4_readlink args = {
2508 .fh = NFS_FH(inode),
2513 struct nfs4_readlink_res res;
2514 struct rpc_message msg = {
2515 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2520 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2523 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2524 unsigned int pgbase, unsigned int pglen)
2526 struct nfs4_exception exception = { };
2529 err = nfs4_handle_exception(NFS_SERVER(inode),
2530 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2532 } while (exception.retry);
2538 * We will need to arrange for the VFS layer to provide an atomic open.
2539 * Until then, this create/open method is prone to inefficiency and race
2540 * conditions due to the lookup, create, and open VFS calls from sys_open()
2541 * placed on the wire.
2543 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2544 * The file will be opened again in the subsequent VFS open call
2545 * (nfs4_proc_file_open).
2547 * The open for read will just hang around to be used by any process that
2548 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2552 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2553 int flags, struct nfs_open_context *ctx)
2555 struct path my_path = {
2558 struct path *path = &my_path;
2559 struct nfs4_state *state;
2560 struct rpc_cred *cred = NULL;
2569 sattr->ia_mode &= ~current_umask();
2570 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
2572 if (IS_ERR(state)) {
2573 status = PTR_ERR(state);
2576 d_add(dentry, igrab(state->inode));
2577 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2581 nfs4_close_sync(path, state, fmode);
2586 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2588 struct nfs_server *server = NFS_SERVER(dir);
2589 struct nfs_removeargs args = {
2591 .name.len = name->len,
2592 .name.name = name->name,
2593 .bitmask = server->attr_bitmask,
2595 struct nfs_removeres res = {
2598 struct rpc_message msg = {
2599 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2603 int status = -ENOMEM;
2605 res.dir_attr = nfs_alloc_fattr();
2606 if (res.dir_attr == NULL)
2609 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2611 update_changeattr(dir, &res.cinfo);
2612 nfs_post_op_update_inode(dir, res.dir_attr);
2614 nfs_free_fattr(res.dir_attr);
2619 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2621 struct nfs4_exception exception = { };
2624 err = nfs4_handle_exception(NFS_SERVER(dir),
2625 _nfs4_proc_remove(dir, name),
2627 } while (exception.retry);
2631 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2633 struct nfs_server *server = NFS_SERVER(dir);
2634 struct nfs_removeargs *args = msg->rpc_argp;
2635 struct nfs_removeres *res = msg->rpc_resp;
2637 args->bitmask = server->cache_consistency_bitmask;
2638 res->server = server;
2639 res->seq_res.sr_slot = NULL;
2640 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2643 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2645 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2647 if (!nfs4_sequence_done(task, &res->seq_res))
2649 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2651 update_changeattr(dir, &res->cinfo);
2652 nfs_post_op_update_inode(dir, res->dir_attr);
2656 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2658 struct nfs_server *server = NFS_SERVER(dir);
2659 struct nfs_renameargs *arg = msg->rpc_argp;
2660 struct nfs_renameres *res = msg->rpc_resp;
2662 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2663 arg->bitmask = server->attr_bitmask;
2664 res->server = server;
2667 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2668 struct inode *new_dir)
2670 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2672 if (!nfs4_sequence_done(task, &res->seq_res))
2674 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2677 update_changeattr(old_dir, &res->old_cinfo);
2678 nfs_post_op_update_inode(old_dir, res->old_fattr);
2679 update_changeattr(new_dir, &res->new_cinfo);
2680 nfs_post_op_update_inode(new_dir, res->new_fattr);
2684 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2685 struct inode *new_dir, struct qstr *new_name)
2687 struct nfs_server *server = NFS_SERVER(old_dir);
2688 struct nfs_renameargs arg = {
2689 .old_dir = NFS_FH(old_dir),
2690 .new_dir = NFS_FH(new_dir),
2691 .old_name = old_name,
2692 .new_name = new_name,
2693 .bitmask = server->attr_bitmask,
2695 struct nfs_renameres res = {
2698 struct rpc_message msg = {
2699 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2703 int status = -ENOMEM;
2705 res.old_fattr = nfs_alloc_fattr();
2706 res.new_fattr = nfs_alloc_fattr();
2707 if (res.old_fattr == NULL || res.new_fattr == NULL)
2710 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2712 update_changeattr(old_dir, &res.old_cinfo);
2713 nfs_post_op_update_inode(old_dir, res.old_fattr);
2714 update_changeattr(new_dir, &res.new_cinfo);
2715 nfs_post_op_update_inode(new_dir, res.new_fattr);
2718 nfs_free_fattr(res.new_fattr);
2719 nfs_free_fattr(res.old_fattr);
2723 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2724 struct inode *new_dir, struct qstr *new_name)
2726 struct nfs4_exception exception = { };
2729 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2730 _nfs4_proc_rename(old_dir, old_name,
2733 } while (exception.retry);
2737 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2739 struct nfs_server *server = NFS_SERVER(inode);
2740 struct nfs4_link_arg arg = {
2741 .fh = NFS_FH(inode),
2742 .dir_fh = NFS_FH(dir),
2744 .bitmask = server->attr_bitmask,
2746 struct nfs4_link_res res = {
2749 struct rpc_message msg = {
2750 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2754 int status = -ENOMEM;
2756 res.fattr = nfs_alloc_fattr();
2757 res.dir_attr = nfs_alloc_fattr();
2758 if (res.fattr == NULL || res.dir_attr == NULL)
2761 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2763 update_changeattr(dir, &res.cinfo);
2764 nfs_post_op_update_inode(dir, res.dir_attr);
2765 nfs_post_op_update_inode(inode, res.fattr);
2768 nfs_free_fattr(res.dir_attr);
2769 nfs_free_fattr(res.fattr);
2773 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2775 struct nfs4_exception exception = { };
2778 err = nfs4_handle_exception(NFS_SERVER(inode),
2779 _nfs4_proc_link(inode, dir, name),
2781 } while (exception.retry);
2785 struct nfs4_createdata {
2786 struct rpc_message msg;
2787 struct nfs4_create_arg arg;
2788 struct nfs4_create_res res;
2790 struct nfs_fattr fattr;
2791 struct nfs_fattr dir_fattr;
2794 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2795 struct qstr *name, struct iattr *sattr, u32 ftype)
2797 struct nfs4_createdata *data;
2799 data = kzalloc(sizeof(*data), GFP_KERNEL);
2801 struct nfs_server *server = NFS_SERVER(dir);
2803 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2804 data->msg.rpc_argp = &data->arg;
2805 data->msg.rpc_resp = &data->res;
2806 data->arg.dir_fh = NFS_FH(dir);
2807 data->arg.server = server;
2808 data->arg.name = name;
2809 data->arg.attrs = sattr;
2810 data->arg.ftype = ftype;
2811 data->arg.bitmask = server->attr_bitmask;
2812 data->res.server = server;
2813 data->res.fh = &data->fh;
2814 data->res.fattr = &data->fattr;
2815 data->res.dir_fattr = &data->dir_fattr;
2816 nfs_fattr_init(data->res.fattr);
2817 nfs_fattr_init(data->res.dir_fattr);
2822 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2824 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
2825 &data->arg.seq_args, &data->res.seq_res, 1);
2827 update_changeattr(dir, &data->res.dir_cinfo);
2828 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2829 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2834 static void nfs4_free_createdata(struct nfs4_createdata *data)
2839 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2840 struct page *page, unsigned int len, struct iattr *sattr)
2842 struct nfs4_createdata *data;
2843 int status = -ENAMETOOLONG;
2845 if (len > NFS4_MAXPATHLEN)
2849 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2853 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2854 data->arg.u.symlink.pages = &page;
2855 data->arg.u.symlink.len = len;
2857 status = nfs4_do_create(dir, dentry, data);
2859 nfs4_free_createdata(data);
2864 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2865 struct page *page, unsigned int len, struct iattr *sattr)
2867 struct nfs4_exception exception = { };
2870 err = nfs4_handle_exception(NFS_SERVER(dir),
2871 _nfs4_proc_symlink(dir, dentry, page,
2874 } while (exception.retry);
2878 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2879 struct iattr *sattr)
2881 struct nfs4_createdata *data;
2882 int status = -ENOMEM;
2884 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2888 status = nfs4_do_create(dir, dentry, data);
2890 nfs4_free_createdata(data);
2895 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2896 struct iattr *sattr)
2898 struct nfs4_exception exception = { };
2901 sattr->ia_mode &= ~current_umask();
2903 err = nfs4_handle_exception(NFS_SERVER(dir),
2904 _nfs4_proc_mkdir(dir, dentry, sattr),
2906 } while (exception.retry);
2910 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2911 u64 cookie, struct page **pages, unsigned int count, int plus)
2913 struct inode *dir = dentry->d_inode;
2914 struct nfs4_readdir_arg args = {
2919 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2922 struct nfs4_readdir_res res;
2923 struct rpc_message msg = {
2924 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2931 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2932 dentry->d_parent->d_name.name,
2933 dentry->d_name.name,
2934 (unsigned long long)cookie);
2935 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2936 res.pgbase = args.pgbase;
2937 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
2939 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2940 status += args.pgbase;
2943 nfs_invalidate_atime(dir);
2945 dprintk("%s: returns %d\n", __func__, status);
2949 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2950 u64 cookie, struct page **pages, unsigned int count, int plus)
2952 struct nfs4_exception exception = { };
2955 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2956 _nfs4_proc_readdir(dentry, cred, cookie,
2957 pages, count, plus),
2959 } while (exception.retry);
2963 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2964 struct iattr *sattr, dev_t rdev)
2966 struct nfs4_createdata *data;
2967 int mode = sattr->ia_mode;
2968 int status = -ENOMEM;
2970 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2971 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2973 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2978 data->arg.ftype = NF4FIFO;
2979 else if (S_ISBLK(mode)) {
2980 data->arg.ftype = NF4BLK;
2981 data->arg.u.device.specdata1 = MAJOR(rdev);
2982 data->arg.u.device.specdata2 = MINOR(rdev);
2984 else if (S_ISCHR(mode)) {
2985 data->arg.ftype = NF4CHR;
2986 data->arg.u.device.specdata1 = MAJOR(rdev);
2987 data->arg.u.device.specdata2 = MINOR(rdev);
2990 status = nfs4_do_create(dir, dentry, data);
2992 nfs4_free_createdata(data);
2997 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2998 struct iattr *sattr, dev_t rdev)
3000 struct nfs4_exception exception = { };
3003 sattr->ia_mode &= ~current_umask();
3005 err = nfs4_handle_exception(NFS_SERVER(dir),
3006 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3008 } while (exception.retry);
3012 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3013 struct nfs_fsstat *fsstat)
3015 struct nfs4_statfs_arg args = {
3017 .bitmask = server->attr_bitmask,
3019 struct nfs4_statfs_res res = {
3022 struct rpc_message msg = {
3023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3028 nfs_fattr_init(fsstat->fattr);
3029 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3032 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3034 struct nfs4_exception exception = { };
3037 err = nfs4_handle_exception(server,
3038 _nfs4_proc_statfs(server, fhandle, fsstat),
3040 } while (exception.retry);
3044 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3045 struct nfs_fsinfo *fsinfo)
3047 struct nfs4_fsinfo_arg args = {
3049 .bitmask = server->attr_bitmask,
3051 struct nfs4_fsinfo_res res = {
3054 struct rpc_message msg = {
3055 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3060 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3063 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3065 struct nfs4_exception exception = { };
3069 err = nfs4_handle_exception(server,
3070 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3072 } while (exception.retry);
3076 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3078 nfs_fattr_init(fsinfo->fattr);
3079 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3082 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3083 struct nfs_pathconf *pathconf)
3085 struct nfs4_pathconf_arg args = {
3087 .bitmask = server->attr_bitmask,
3089 struct nfs4_pathconf_res res = {
3090 .pathconf = pathconf,
3092 struct rpc_message msg = {
3093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3098 /* None of the pathconf attributes are mandatory to implement */
3099 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3100 memset(pathconf, 0, sizeof(*pathconf));
3104 nfs_fattr_init(pathconf->fattr);
3105 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3108 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3109 struct nfs_pathconf *pathconf)
3111 struct nfs4_exception exception = { };
3115 err = nfs4_handle_exception(server,
3116 _nfs4_proc_pathconf(server, fhandle, pathconf),
3118 } while (exception.retry);
3122 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3124 struct nfs_server *server = NFS_SERVER(data->inode);
3126 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3127 nfs_restart_rpc(task, server->nfs_client);
3131 nfs_invalidate_atime(data->inode);
3132 if (task->tk_status > 0)
3133 renew_lease(server, data->timestamp);
3137 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3140 dprintk("--> %s\n", __func__);
3142 if (!nfs4_sequence_done(task, &data->res.seq_res))
3145 return data->read_done_cb(task, data);
3148 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3150 data->timestamp = jiffies;
3151 data->read_done_cb = nfs4_read_done_cb;
3152 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3155 /* Reset the the nfs_read_data to send the read to the MDS. */
3156 void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3158 dprintk("%s Reset task for i/o through\n", __func__);
3159 put_lseg(data->lseg);
3161 /* offsets will differ in the dense stripe case */
3162 data->args.offset = data->mds_offset;
3163 data->ds_clp = NULL;
3164 data->args.fh = NFS_FH(data->inode);
3165 data->read_done_cb = nfs4_read_done_cb;
3166 task->tk_ops = data->mds_ops;
3167 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3169 EXPORT_SYMBOL_GPL(nfs4_reset_read);
3171 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3173 struct inode *inode = data->inode;
3175 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3176 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3179 if (task->tk_status >= 0) {
3180 renew_lease(NFS_SERVER(inode), data->timestamp);
3181 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3186 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3188 if (!nfs4_sequence_done(task, &data->res.seq_res))
3190 return data->write_done_cb(task, data);
3193 /* Reset the the nfs_write_data to send the write to the MDS. */
3194 void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3196 dprintk("%s Reset task for i/o through\n", __func__);
3197 put_lseg(data->lseg);
3199 data->ds_clp = NULL;
3200 data->write_done_cb = nfs4_write_done_cb;
3201 data->args.fh = NFS_FH(data->inode);
3202 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3203 data->args.offset = data->mds_offset;
3204 data->res.fattr = &data->fattr;
3205 task->tk_ops = data->mds_ops;
3206 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3208 EXPORT_SYMBOL_GPL(nfs4_reset_write);
3210 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3212 struct nfs_server *server = NFS_SERVER(data->inode);
3215 data->args.bitmask = NULL;
3216 data->res.fattr = NULL;
3218 data->args.bitmask = server->cache_consistency_bitmask;
3219 if (!data->write_done_cb)
3220 data->write_done_cb = nfs4_write_done_cb;
3221 data->res.server = server;
3222 data->timestamp = jiffies;
3224 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3227 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3229 struct inode *inode = data->inode;
3231 if (!nfs4_sequence_done(task, &data->res.seq_res))
3234 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3235 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3238 nfs_refresh_inode(inode, data->res.fattr);
3242 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3244 struct nfs_server *server = NFS_SERVER(data->inode);
3246 data->args.bitmask = server->cache_consistency_bitmask;
3247 data->res.server = server;
3248 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3251 struct nfs4_renewdata {
3252 struct nfs_client *client;
3253 unsigned long timestamp;
3257 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3258 * standalone procedure for queueing an asynchronous RENEW.
3260 static void nfs4_renew_release(void *calldata)
3262 struct nfs4_renewdata *data = calldata;
3263 struct nfs_client *clp = data->client;
3265 if (atomic_read(&clp->cl_count) > 1)
3266 nfs4_schedule_state_renewal(clp);
3267 nfs_put_client(clp);
3271 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3273 struct nfs4_renewdata *data = calldata;
3274 struct nfs_client *clp = data->client;
3275 unsigned long timestamp = data->timestamp;
3277 if (task->tk_status < 0) {
3278 /* Unless we're shutting down, schedule state recovery! */
3279 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3280 nfs4_schedule_lease_recovery(clp);
3283 do_renew_lease(clp, timestamp);
3286 static const struct rpc_call_ops nfs4_renew_ops = {
3287 .rpc_call_done = nfs4_renew_done,
3288 .rpc_release = nfs4_renew_release,
3291 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3293 struct rpc_message msg = {
3294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3298 struct nfs4_renewdata *data;
3300 if (!atomic_inc_not_zero(&clp->cl_count))
3302 data = kmalloc(sizeof(*data), GFP_KERNEL);
3306 data->timestamp = jiffies;
3307 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3308 &nfs4_renew_ops, data);
3311 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3313 struct rpc_message msg = {
3314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3318 unsigned long now = jiffies;
3321 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3324 do_renew_lease(clp, now);
3328 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3330 return (server->caps & NFS_CAP_ACLS)
3331 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3332 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3335 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3336 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3339 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3341 static void buf_to_pages(const void *buf, size_t buflen,
3342 struct page **pages, unsigned int *pgbase)
3344 const void *p = buf;
3346 *pgbase = offset_in_page(buf);
3348 while (p < buf + buflen) {
3349 *(pages++) = virt_to_page(p);
3350 p += PAGE_CACHE_SIZE;
3354 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3355 struct page **pages, unsigned int *pgbase)
3357 struct page *newpage, **spages;
3363 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3364 newpage = alloc_page(GFP_KERNEL);
3366 if (newpage == NULL)
3368 memcpy(page_address(newpage), buf, len);
3373 } while (buflen != 0);
3379 __free_page(spages[rc-1]);
3383 struct nfs4_cached_acl {
3389 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3391 struct nfs_inode *nfsi = NFS_I(inode);
3393 spin_lock(&inode->i_lock);
3394 kfree(nfsi->nfs4_acl);
3395 nfsi->nfs4_acl = acl;
3396 spin_unlock(&inode->i_lock);
3399 static void nfs4_zap_acl_attr(struct inode *inode)
3401 nfs4_set_cached_acl(inode, NULL);
3404 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3406 struct nfs_inode *nfsi = NFS_I(inode);
3407 struct nfs4_cached_acl *acl;
3410 spin_lock(&inode->i_lock);
3411 acl = nfsi->nfs4_acl;
3414 if (buf == NULL) /* user is just asking for length */
3416 if (acl->cached == 0)
3418 ret = -ERANGE; /* see getxattr(2) man page */
3419 if (acl->len > buflen)
3421 memcpy(buf, acl->data, acl->len);
3425 spin_unlock(&inode->i_lock);
3429 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3431 struct nfs4_cached_acl *acl;
3433 if (buf && acl_len <= PAGE_SIZE) {
3434 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3438 memcpy(acl->data, buf, acl_len);
3440 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3447 nfs4_set_cached_acl(inode, acl);
3450 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3452 struct page *pages[NFS4ACL_MAXPAGES];
3453 struct nfs_getaclargs args = {
3454 .fh = NFS_FH(inode),
3458 struct nfs_getaclres res = {
3462 struct rpc_message msg = {
3463 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3467 struct page *localpage = NULL;
3470 if (buflen < PAGE_SIZE) {
3471 /* As long as we're doing a round trip to the server anyway,
3472 * let's be prepared for a page of acl data. */
3473 localpage = alloc_page(GFP_KERNEL);
3474 resp_buf = page_address(localpage);
3475 if (localpage == NULL)
3477 args.acl_pages[0] = localpage;
3478 args.acl_pgbase = 0;
3479 args.acl_len = PAGE_SIZE;
3482 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3484 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3487 if (res.acl_len > args.acl_len)
3488 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3490 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3493 if (res.acl_len > buflen)
3496 memcpy(buf, resp_buf, res.acl_len);
3501 __free_page(localpage);
3505 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3507 struct nfs4_exception exception = { };
3510 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3513 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3514 } while (exception.retry);
3518 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3520 struct nfs_server *server = NFS_SERVER(inode);
3523 if (!nfs4_server_supports_acls(server))
3525 ret = nfs_revalidate_inode(server, inode);
3528 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3529 nfs_zap_acl_cache(inode);
3530 ret = nfs4_read_cached_acl(inode, buf, buflen);
3533 return nfs4_get_acl_uncached(inode, buf, buflen);
3536 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3538 struct nfs_server *server = NFS_SERVER(inode);
3539 struct page *pages[NFS4ACL_MAXPAGES];
3540 struct nfs_setaclargs arg = {
3541 .fh = NFS_FH(inode),
3545 struct nfs_setaclres res;
3546 struct rpc_message msg = {
3547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3553 if (!nfs4_server_supports_acls(server))
3555 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3558 nfs_inode_return_delegation(inode);
3559 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3562 * Free each page after tx, so the only ref left is
3563 * held by the network stack
3566 put_page(pages[i-1]);
3569 * Acl update can result in inode attribute update.
3570 * so mark the attribute cache invalid.
3572 spin_lock(&inode->i_lock);
3573 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3574 spin_unlock(&inode->i_lock);
3575 nfs_access_zap_cache(inode);
3576 nfs_zap_acl_cache(inode);
3580 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3582 struct nfs4_exception exception = { };
3585 err = nfs4_handle_exception(NFS_SERVER(inode),
3586 __nfs4_proc_set_acl(inode, buf, buflen),
3588 } while (exception.retry);
3593 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3595 struct nfs_client *clp = server->nfs_client;
3597 if (task->tk_status >= 0)
3599 switch(task->tk_status) {
3600 case -NFS4ERR_ADMIN_REVOKED:
3601 case -NFS4ERR_BAD_STATEID:
3602 case -NFS4ERR_OPENMODE:
3605 nfs4_schedule_stateid_recovery(server, state);
3606 goto wait_on_recovery;
3607 case -NFS4ERR_STALE_STATEID:
3608 case -NFS4ERR_STALE_CLIENTID:
3609 case -NFS4ERR_EXPIRED:
3610 nfs4_schedule_lease_recovery(clp);
3611 goto wait_on_recovery;
3612 #if defined(CONFIG_NFS_V4_1)
3613 case -NFS4ERR_BADSESSION:
3614 case -NFS4ERR_BADSLOT:
3615 case -NFS4ERR_BAD_HIGH_SLOT:
3616 case -NFS4ERR_DEADSESSION:
3617 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3618 case -NFS4ERR_SEQ_FALSE_RETRY:
3619 case -NFS4ERR_SEQ_MISORDERED:
3620 dprintk("%s ERROR %d, Reset session\n", __func__,
3622 nfs4_schedule_session_recovery(clp->cl_session);
3623 task->tk_status = 0;
3625 #endif /* CONFIG_NFS_V4_1 */
3626 case -NFS4ERR_DELAY:
3627 nfs_inc_server_stats(server, NFSIOS_DELAY);
3628 case -NFS4ERR_GRACE:
3630 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3631 task->tk_status = 0;
3633 case -NFS4ERR_OLD_STATEID:
3634 task->tk_status = 0;
3637 task->tk_status = nfs4_map_errors(task->tk_status);
3640 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3641 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3642 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3643 task->tk_status = 0;
3647 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3648 unsigned short port, struct rpc_cred *cred,
3649 struct nfs4_setclientid_res *res)
3651 nfs4_verifier sc_verifier;
3652 struct nfs4_setclientid setclientid = {
3653 .sc_verifier = &sc_verifier,
3655 .sc_cb_ident = clp->cl_cb_ident,
3657 struct rpc_message msg = {
3658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3659 .rpc_argp = &setclientid,
3667 p = (__be32*)sc_verifier.data;
3668 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3669 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3672 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3673 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3675 rpc_peeraddr2str(clp->cl_rpcclient,
3677 rpc_peeraddr2str(clp->cl_rpcclient,
3679 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3680 clp->cl_id_uniquifier);
3681 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3682 sizeof(setclientid.sc_netid),
3683 rpc_peeraddr2str(clp->cl_rpcclient,
3684 RPC_DISPLAY_NETID));
3685 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3686 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3687 clp->cl_ipaddr, port >> 8, port & 255);
3689 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3690 if (status != -NFS4ERR_CLID_INUSE)
3695 ssleep(clp->cl_lease_time / HZ + 1);
3697 if (++clp->cl_id_uniquifier == 0)
3703 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3704 struct nfs4_setclientid_res *arg,
3705 struct rpc_cred *cred)
3707 struct nfs_fsinfo fsinfo;
3708 struct rpc_message msg = {
3709 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3711 .rpc_resp = &fsinfo,
3718 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3720 spin_lock(&clp->cl_lock);
3721 clp->cl_lease_time = fsinfo.lease_time * HZ;
3722 clp->cl_last_renewal = now;
3723 spin_unlock(&clp->cl_lock);
3728 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3729 struct nfs4_setclientid_res *arg,
3730 struct rpc_cred *cred)
3735 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
3739 case -NFS4ERR_RESOURCE:
3740 /* The IBM lawyers misread another document! */
3741 case -NFS4ERR_DELAY:
3742 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3748 struct nfs4_delegreturndata {
3749 struct nfs4_delegreturnargs args;
3750 struct nfs4_delegreturnres res;
3752 nfs4_stateid stateid;
3753 unsigned long timestamp;
3754 struct nfs_fattr fattr;
3758 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3760 struct nfs4_delegreturndata *data = calldata;
3762 if (!nfs4_sequence_done(task, &data->res.seq_res))
3765 switch (task->tk_status) {
3766 case -NFS4ERR_STALE_STATEID:
3767 case -NFS4ERR_EXPIRED:
3769 renew_lease(data->res.server, data->timestamp);
3772 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3774 nfs_restart_rpc(task, data->res.server->nfs_client);
3778 data->rpc_status = task->tk_status;
3781 static void nfs4_delegreturn_release(void *calldata)
3786 #if defined(CONFIG_NFS_V4_1)
3787 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3789 struct nfs4_delegreturndata *d_data;
3791 d_data = (struct nfs4_delegreturndata *)data;
3793 if (nfs4_setup_sequence(d_data->res.server,
3794 &d_data->args.seq_args,
3795 &d_data->res.seq_res, 1, task))
3797 rpc_call_start(task);
3799 #endif /* CONFIG_NFS_V4_1 */
3801 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3802 #if defined(CONFIG_NFS_V4_1)
3803 .rpc_call_prepare = nfs4_delegreturn_prepare,
3804 #endif /* CONFIG_NFS_V4_1 */
3805 .rpc_call_done = nfs4_delegreturn_done,
3806 .rpc_release = nfs4_delegreturn_release,
3809 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3811 struct nfs4_delegreturndata *data;
3812 struct nfs_server *server = NFS_SERVER(inode);
3813 struct rpc_task *task;
3814 struct rpc_message msg = {
3815 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3818 struct rpc_task_setup task_setup_data = {
3819 .rpc_client = server->client,
3820 .rpc_message = &msg,
3821 .callback_ops = &nfs4_delegreturn_ops,
3822 .flags = RPC_TASK_ASYNC,
3826 data = kzalloc(sizeof(*data), GFP_NOFS);
3829 data->args.fhandle = &data->fh;
3830 data->args.stateid = &data->stateid;
3831 data->args.bitmask = server->attr_bitmask;
3832 nfs_copy_fh(&data->fh, NFS_FH(inode));
3833 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3834 data->res.fattr = &data->fattr;
3835 data->res.server = server;
3836 nfs_fattr_init(data->res.fattr);
3837 data->timestamp = jiffies;
3838 data->rpc_status = 0;
3840 task_setup_data.callback_data = data;
3841 msg.rpc_argp = &data->args;
3842 msg.rpc_resp = &data->res;
3843 task = rpc_run_task(&task_setup_data);
3845 return PTR_ERR(task);
3848 status = nfs4_wait_for_completion_rpc_task(task);
3851 status = data->rpc_status;
3854 nfs_refresh_inode(inode, &data->fattr);
3860 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3862 struct nfs_server *server = NFS_SERVER(inode);
3863 struct nfs4_exception exception = { };
3866 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3868 case -NFS4ERR_STALE_STATEID:
3869 case -NFS4ERR_EXPIRED:
3873 err = nfs4_handle_exception(server, err, &exception);
3874 } while (exception.retry);
3878 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3879 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3882 * sleep, with exponential backoff, and retry the LOCK operation.
3884 static unsigned long
3885 nfs4_set_lock_task_retry(unsigned long timeout)
3887 schedule_timeout_killable(timeout);
3889 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3890 return NFS4_LOCK_MAXTIMEOUT;
3894 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3896 struct inode *inode = state->inode;
3897 struct nfs_server *server = NFS_SERVER(inode);
3898 struct nfs_client *clp = server->nfs_client;
3899 struct nfs_lockt_args arg = {
3900 .fh = NFS_FH(inode),
3903 struct nfs_lockt_res res = {
3906 struct rpc_message msg = {
3907 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3910 .rpc_cred = state->owner->so_cred,
3912 struct nfs4_lock_state *lsp;
3915 arg.lock_owner.clientid = clp->cl_clientid;
3916 status = nfs4_set_lock_state(state, request);
3919 lsp = request->fl_u.nfs4_fl.owner;
3920 arg.lock_owner.id = lsp->ls_id.id;
3921 arg.lock_owner.s_dev = server->s_dev;
3922 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3925 request->fl_type = F_UNLCK;
3927 case -NFS4ERR_DENIED:
3930 request->fl_ops->fl_release_private(request);
3935 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3937 struct nfs4_exception exception = { };
3941 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3942 _nfs4_proc_getlk(state, cmd, request),
3944 } while (exception.retry);
3948 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3951 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3953 res = posix_lock_file_wait(file, fl);
3956 res = flock_lock_file_wait(file, fl);
3964 struct nfs4_unlockdata {
3965 struct nfs_locku_args arg;
3966 struct nfs_locku_res res;
3967 struct nfs4_lock_state *lsp;
3968 struct nfs_open_context *ctx;
3969 struct file_lock fl;
3970 const struct nfs_server *server;
3971 unsigned long timestamp;
3974 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3975 struct nfs_open_context *ctx,
3976 struct nfs4_lock_state *lsp,
3977 struct nfs_seqid *seqid)
3979 struct nfs4_unlockdata *p;
3980 struct inode *inode = lsp->ls_state->inode;
3982 p = kzalloc(sizeof(*p), GFP_NOFS);
3985 p->arg.fh = NFS_FH(inode);
3987 p->arg.seqid = seqid;
3988 p->res.seqid = seqid;
3989 p->arg.stateid = &lsp->ls_stateid;
3991 atomic_inc(&lsp->ls_count);
3992 /* Ensure we don't close file until we're done freeing locks! */
3993 p->ctx = get_nfs_open_context(ctx);
3994 memcpy(&p->fl, fl, sizeof(p->fl));
3995 p->server = NFS_SERVER(inode);
3999 static void nfs4_locku_release_calldata(void *data)
4001 struct nfs4_unlockdata *calldata = data;
4002 nfs_free_seqid(calldata->arg.seqid);
4003 nfs4_put_lock_state(calldata->lsp);
4004 put_nfs_open_context(calldata->ctx);
4008 static void nfs4_locku_done(struct rpc_task *task, void *data)
4010 struct nfs4_unlockdata *calldata = data;
4012 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4014 switch (task->tk_status) {
4016 memcpy(calldata->lsp->ls_stateid.data,
4017 calldata->res.stateid.data,
4018 sizeof(calldata->lsp->ls_stateid.data));
4019 renew_lease(calldata->server, calldata->timestamp);
4021 case -NFS4ERR_BAD_STATEID:
4022 case -NFS4ERR_OLD_STATEID:
4023 case -NFS4ERR_STALE_STATEID:
4024 case -NFS4ERR_EXPIRED:
4027 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4028 nfs_restart_rpc(task,
4029 calldata->server->nfs_client);
4033 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4035 struct nfs4_unlockdata *calldata = data;
4037 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4039 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
4040 /* Note: exit _without_ running nfs4_locku_done */
4041 task->tk_action = NULL;
4044 calldata->timestamp = jiffies;
4045 if (nfs4_setup_sequence(calldata->server,
4046 &calldata->arg.seq_args,
4047 &calldata->res.seq_res, 1, task))
4049 rpc_call_start(task);
4052 static const struct rpc_call_ops nfs4_locku_ops = {
4053 .rpc_call_prepare = nfs4_locku_prepare,
4054 .rpc_call_done = nfs4_locku_done,
4055 .rpc_release = nfs4_locku_release_calldata,
4058 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4059 struct nfs_open_context *ctx,
4060 struct nfs4_lock_state *lsp,
4061 struct nfs_seqid *seqid)
4063 struct nfs4_unlockdata *data;
4064 struct rpc_message msg = {
4065 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4066 .rpc_cred = ctx->cred,
4068 struct rpc_task_setup task_setup_data = {
4069 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4070 .rpc_message = &msg,
4071 .callback_ops = &nfs4_locku_ops,
4072 .workqueue = nfsiod_workqueue,
4073 .flags = RPC_TASK_ASYNC,
4076 /* Ensure this is an unlock - when canceling a lock, the
4077 * canceled lock is passed in, and it won't be an unlock.
4079 fl->fl_type = F_UNLCK;
4081 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4083 nfs_free_seqid(seqid);
4084 return ERR_PTR(-ENOMEM);
4087 msg.rpc_argp = &data->arg;
4088 msg.rpc_resp = &data->res;
4089 task_setup_data.callback_data = data;
4090 return rpc_run_task(&task_setup_data);
4093 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4095 struct nfs_inode *nfsi = NFS_I(state->inode);
4096 struct nfs_seqid *seqid;
4097 struct nfs4_lock_state *lsp;
4098 struct rpc_task *task;
4100 unsigned char fl_flags = request->fl_flags;
4102 status = nfs4_set_lock_state(state, request);
4103 /* Unlock _before_ we do the RPC call */
4104 request->fl_flags |= FL_EXISTS;
4105 down_read(&nfsi->rwsem);
4106 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4107 up_read(&nfsi->rwsem);
4110 up_read(&nfsi->rwsem);
4113 /* Is this a delegated lock? */
4114 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4116 lsp = request->fl_u.nfs4_fl.owner;
4117 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4121 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4122 status = PTR_ERR(task);
4125 status = nfs4_wait_for_completion_rpc_task(task);
4128 request->fl_flags = fl_flags;
4132 struct nfs4_lockdata {
4133 struct nfs_lock_args arg;
4134 struct nfs_lock_res res;
4135 struct nfs4_lock_state *lsp;
4136 struct nfs_open_context *ctx;
4137 struct file_lock fl;
4138 unsigned long timestamp;
4141 struct nfs_server *server;
4144 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4145 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4148 struct nfs4_lockdata *p;
4149 struct inode *inode = lsp->ls_state->inode;
4150 struct nfs_server *server = NFS_SERVER(inode);
4152 p = kzalloc(sizeof(*p), gfp_mask);
4156 p->arg.fh = NFS_FH(inode);
4158 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4159 if (p->arg.open_seqid == NULL)
4161 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4162 if (p->arg.lock_seqid == NULL)
4163 goto out_free_seqid;
4164 p->arg.lock_stateid = &lsp->ls_stateid;
4165 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4166 p->arg.lock_owner.id = lsp->ls_id.id;
4167 p->arg.lock_owner.s_dev = server->s_dev;
4168 p->res.lock_seqid = p->arg.lock_seqid;
4171 atomic_inc(&lsp->ls_count);
4172 p->ctx = get_nfs_open_context(ctx);
4173 memcpy(&p->fl, fl, sizeof(p->fl));
4176 nfs_free_seqid(p->arg.open_seqid);
4182 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4184 struct nfs4_lockdata *data = calldata;
4185 struct nfs4_state *state = data->lsp->ls_state;
4187 dprintk("%s: begin!\n", __func__);
4188 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4190 /* Do we need to do an open_to_lock_owner? */
4191 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4192 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4194 data->arg.open_stateid = &state->stateid;
4195 data->arg.new_lock_owner = 1;
4196 data->res.open_seqid = data->arg.open_seqid;
4198 data->arg.new_lock_owner = 0;
4199 data->timestamp = jiffies;
4200 if (nfs4_setup_sequence(data->server,
4201 &data->arg.seq_args,
4202 &data->res.seq_res, 1, task))
4204 rpc_call_start(task);
4205 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4208 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4210 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4211 nfs4_lock_prepare(task, calldata);
4214 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4216 struct nfs4_lockdata *data = calldata;
4218 dprintk("%s: begin!\n", __func__);
4220 if (!nfs4_sequence_done(task, &data->res.seq_res))
4223 data->rpc_status = task->tk_status;
4224 if (data->arg.new_lock_owner != 0) {
4225 if (data->rpc_status == 0)
4226 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4230 if (data->rpc_status == 0) {
4231 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4232 sizeof(data->lsp->ls_stateid.data));
4233 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4234 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
4237 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4240 static void nfs4_lock_release(void *calldata)
4242 struct nfs4_lockdata *data = calldata;
4244 dprintk("%s: begin!\n", __func__);
4245 nfs_free_seqid(data->arg.open_seqid);
4246 if (data->cancelled != 0) {
4247 struct rpc_task *task;
4248 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4249 data->arg.lock_seqid);
4251 rpc_put_task_async(task);
4252 dprintk("%s: cancelling lock!\n", __func__);
4254 nfs_free_seqid(data->arg.lock_seqid);
4255 nfs4_put_lock_state(data->lsp);
4256 put_nfs_open_context(data->ctx);
4258 dprintk("%s: done!\n", __func__);
4261 static const struct rpc_call_ops nfs4_lock_ops = {
4262 .rpc_call_prepare = nfs4_lock_prepare,
4263 .rpc_call_done = nfs4_lock_done,
4264 .rpc_release = nfs4_lock_release,
4267 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4268 .rpc_call_prepare = nfs4_recover_lock_prepare,
4269 .rpc_call_done = nfs4_lock_done,
4270 .rpc_release = nfs4_lock_release,
4273 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4276 case -NFS4ERR_ADMIN_REVOKED:
4277 case -NFS4ERR_BAD_STATEID:
4278 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4279 if (new_lock_owner != 0 ||
4280 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4281 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4283 case -NFS4ERR_STALE_STATEID:
4284 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4285 case -NFS4ERR_EXPIRED:
4286 nfs4_schedule_lease_recovery(server->nfs_client);
4290 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4292 struct nfs4_lockdata *data;
4293 struct rpc_task *task;
4294 struct rpc_message msg = {
4295 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4296 .rpc_cred = state->owner->so_cred,
4298 struct rpc_task_setup task_setup_data = {
4299 .rpc_client = NFS_CLIENT(state->inode),
4300 .rpc_message = &msg,
4301 .callback_ops = &nfs4_lock_ops,
4302 .workqueue = nfsiod_workqueue,
4303 .flags = RPC_TASK_ASYNC,
4307 dprintk("%s: begin!\n", __func__);
4308 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4309 fl->fl_u.nfs4_fl.owner,
4310 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4314 data->arg.block = 1;
4315 if (recovery_type > NFS_LOCK_NEW) {
4316 if (recovery_type == NFS_LOCK_RECLAIM)
4317 data->arg.reclaim = NFS_LOCK_RECLAIM;
4318 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4320 msg.rpc_argp = &data->arg;
4321 msg.rpc_resp = &data->res;
4322 task_setup_data.callback_data = data;
4323 task = rpc_run_task(&task_setup_data);
4325 return PTR_ERR(task);
4326 ret = nfs4_wait_for_completion_rpc_task(task);
4328 ret = data->rpc_status;
4330 nfs4_handle_setlk_error(data->server, data->lsp,
4331 data->arg.new_lock_owner, ret);
4333 data->cancelled = 1;
4335 dprintk("%s: done, ret = %d!\n", __func__, ret);
4339 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4341 struct nfs_server *server = NFS_SERVER(state->inode);
4342 struct nfs4_exception exception = { };
4346 /* Cache the lock if possible... */
4347 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4349 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4350 if (err != -NFS4ERR_DELAY)
4352 nfs4_handle_exception(server, err, &exception);
4353 } while (exception.retry);
4357 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4359 struct nfs_server *server = NFS_SERVER(state->inode);
4360 struct nfs4_exception exception = { };
4363 err = nfs4_set_lock_state(state, request);
4367 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4369 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4373 case -NFS4ERR_GRACE:
4374 case -NFS4ERR_DELAY:
4375 nfs4_handle_exception(server, err, &exception);
4378 } while (exception.retry);
4383 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4385 struct nfs_inode *nfsi = NFS_I(state->inode);
4386 unsigned char fl_flags = request->fl_flags;
4387 int status = -ENOLCK;
4389 if ((fl_flags & FL_POSIX) &&
4390 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4392 /* Is this a delegated open? */
4393 status = nfs4_set_lock_state(state, request);
4396 request->fl_flags |= FL_ACCESS;
4397 status = do_vfs_lock(request->fl_file, request);
4400 down_read(&nfsi->rwsem);
4401 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4402 /* Yes: cache locks! */
4403 /* ...but avoid races with delegation recall... */
4404 request->fl_flags = fl_flags & ~FL_SLEEP;
4405 status = do_vfs_lock(request->fl_file, request);
4408 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4411 /* Note: we always want to sleep here! */
4412 request->fl_flags = fl_flags | FL_SLEEP;
4413 if (do_vfs_lock(request->fl_file, request) < 0)
4414 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4416 up_read(&nfsi->rwsem);
4418 request->fl_flags = fl_flags;
4422 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4424 struct nfs4_exception exception = { };
4428 err = _nfs4_proc_setlk(state, cmd, request);
4429 if (err == -NFS4ERR_DENIED)
4431 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4433 } while (exception.retry);
4438 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4440 struct nfs_open_context *ctx;
4441 struct nfs4_state *state;
4442 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4445 /* verify open state */
4446 ctx = nfs_file_open_context(filp);
4449 if (request->fl_start < 0 || request->fl_end < 0)
4452 if (IS_GETLK(cmd)) {
4454 return nfs4_proc_getlk(state, F_GETLK, request);
4458 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4461 if (request->fl_type == F_UNLCK) {
4463 return nfs4_proc_unlck(state, cmd, request);
4470 status = nfs4_proc_setlk(state, cmd, request);
4471 if ((status != -EAGAIN) || IS_SETLK(cmd))
4473 timeout = nfs4_set_lock_task_retry(timeout);
4474 status = -ERESTARTSYS;
4477 } while(status < 0);
4481 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4483 struct nfs_server *server = NFS_SERVER(state->inode);
4484 struct nfs4_exception exception = { };
4487 err = nfs4_set_lock_state(state, fl);
4491 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4494 printk(KERN_ERR "%s: unhandled error %d.\n",
4499 case -NFS4ERR_EXPIRED:
4500 case -NFS4ERR_STALE_CLIENTID:
4501 case -NFS4ERR_STALE_STATEID:
4502 nfs4_schedule_lease_recovery(server->nfs_client);
4504 case -NFS4ERR_BADSESSION:
4505 case -NFS4ERR_BADSLOT:
4506 case -NFS4ERR_BAD_HIGH_SLOT:
4507 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4508 case -NFS4ERR_DEADSESSION:
4509 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
4513 * The show must go on: exit, but mark the
4514 * stateid as needing recovery.
4516 case -NFS4ERR_ADMIN_REVOKED:
4517 case -NFS4ERR_BAD_STATEID:
4518 case -NFS4ERR_OPENMODE:
4519 nfs4_schedule_stateid_recovery(server, state);
4524 * User RPCSEC_GSS context has expired.
4525 * We cannot recover this stateid now, so
4526 * skip it and allow recovery thread to
4532 case -NFS4ERR_DENIED:
4533 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4536 case -NFS4ERR_DELAY:
4539 err = nfs4_handle_exception(server, err, &exception);
4540 } while (exception.retry);
4545 static void nfs4_release_lockowner_release(void *calldata)
4550 const struct rpc_call_ops nfs4_release_lockowner_ops = {
4551 .rpc_release = nfs4_release_lockowner_release,
4554 void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4556 struct nfs_server *server = lsp->ls_state->owner->so_server;
4557 struct nfs_release_lockowner_args *args;
4558 struct rpc_message msg = {
4559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4562 if (server->nfs_client->cl_mvops->minor_version != 0)
4564 args = kmalloc(sizeof(*args), GFP_NOFS);
4567 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4568 args->lock_owner.id = lsp->ls_id.id;
4569 args->lock_owner.s_dev = server->s_dev;
4570 msg.rpc_argp = args;
4571 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4574 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4576 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4577 const void *buf, size_t buflen,
4578 int flags, int type)
4580 if (strcmp(key, "") != 0)
4583 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
4586 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4587 void *buf, size_t buflen, int type)
4589 if (strcmp(key, "") != 0)
4592 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
4595 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4596 size_t list_len, const char *name,
4597 size_t name_len, int type)
4599 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
4601 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4604 if (list && len <= list_len)
4605 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
4609 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4611 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4612 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4613 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4616 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4617 NFS_ATTR_FATTR_NLINK;
4618 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4622 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4623 struct nfs4_fs_locations *fs_locations, struct page *page)
4625 struct nfs_server *server = NFS_SERVER(dir);
4627 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4628 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
4630 struct nfs4_fs_locations_arg args = {
4631 .dir_fh = NFS_FH(dir),
4636 struct nfs4_fs_locations_res res = {
4637 .fs_locations = fs_locations,
4639 struct rpc_message msg = {
4640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4646 dprintk("%s: start\n", __func__);
4647 nfs_fattr_init(&fs_locations->fattr);
4648 fs_locations->server = server;
4649 fs_locations->nlocations = 0;
4650 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4651 nfs_fixup_referral_attributes(&fs_locations->fattr);
4652 dprintk("%s: returned status = %d\n", __func__, status);
4656 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4659 struct nfs4_secinfo_arg args = {
4660 .dir_fh = NFS_FH(dir),
4663 struct nfs4_secinfo_res res = {
4666 struct rpc_message msg = {
4667 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4672 dprintk("NFS call secinfo %s\n", name->name);
4673 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4674 dprintk("NFS reply secinfo: %d\n", status);
4678 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4680 struct nfs4_exception exception = { };
4683 err = nfs4_handle_exception(NFS_SERVER(dir),
4684 _nfs4_proc_secinfo(dir, name, flavors),
4686 } while (exception.retry);
4690 #ifdef CONFIG_NFS_V4_1
4692 * Check the exchange flags returned by the server for invalid flags, having
4693 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4696 static int nfs4_check_cl_exchange_flags(u32 flags)
4698 if (flags & ~EXCHGID4_FLAG_MASK_R)
4700 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4701 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4703 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4707 return -NFS4ERR_INVAL;
4711 * nfs4_proc_exchange_id()
4713 * Since the clientid has expired, all compounds using sessions
4714 * associated with the stale clientid will be returning
4715 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4716 * be in some phase of session reset.
4718 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4720 nfs4_verifier verifier;
4721 struct nfs41_exchange_id_args args = {
4723 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
4725 struct nfs41_exchange_id_res res = {
4729 struct rpc_message msg = {
4730 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4737 dprintk("--> %s\n", __func__);
4738 BUG_ON(clp == NULL);
4740 p = (u32 *)verifier.data;
4741 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4742 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4743 args.verifier = &verifier;
4745 args.id_len = scnprintf(args.id, sizeof(args.id),
4748 init_utsname()->nodename,
4749 init_utsname()->domainname,
4750 clp->cl_rpcclient->cl_auth->au_flavor);
4752 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4754 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
4755 dprintk("<-- %s status= %d\n", __func__, status);
4759 struct nfs4_get_lease_time_data {
4760 struct nfs4_get_lease_time_args *args;
4761 struct nfs4_get_lease_time_res *res;
4762 struct nfs_client *clp;
4765 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4769 struct nfs4_get_lease_time_data *data =
4770 (struct nfs4_get_lease_time_data *)calldata;
4772 dprintk("--> %s\n", __func__);
4773 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4774 /* just setup sequence, do not trigger session recovery
4775 since we're invoked within one */
4776 ret = nfs41_setup_sequence(data->clp->cl_session,
4777 &data->args->la_seq_args,
4778 &data->res->lr_seq_res, 0, task);
4780 BUG_ON(ret == -EAGAIN);
4781 rpc_call_start(task);
4782 dprintk("<-- %s\n", __func__);
4786 * Called from nfs4_state_manager thread for session setup, so don't recover
4787 * from sequence operation or clientid errors.
4789 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4791 struct nfs4_get_lease_time_data *data =
4792 (struct nfs4_get_lease_time_data *)calldata;
4794 dprintk("--> %s\n", __func__);
4795 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4797 switch (task->tk_status) {
4798 case -NFS4ERR_DELAY:
4799 case -NFS4ERR_GRACE:
4800 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4801 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4802 task->tk_status = 0;
4803 nfs_restart_rpc(task, data->clp);
4806 dprintk("<-- %s\n", __func__);
4809 struct rpc_call_ops nfs4_get_lease_time_ops = {
4810 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4811 .rpc_call_done = nfs4_get_lease_time_done,
4814 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4816 struct rpc_task *task;
4817 struct nfs4_get_lease_time_args args;
4818 struct nfs4_get_lease_time_res res = {
4819 .lr_fsinfo = fsinfo,
4821 struct nfs4_get_lease_time_data data = {
4826 struct rpc_message msg = {
4827 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4831 struct rpc_task_setup task_setup = {
4832 .rpc_client = clp->cl_rpcclient,
4833 .rpc_message = &msg,
4834 .callback_ops = &nfs4_get_lease_time_ops,
4835 .callback_data = &data
4839 dprintk("--> %s\n", __func__);
4840 task = rpc_run_task(&task_setup);
4843 status = PTR_ERR(task);
4845 status = task->tk_status;
4848 dprintk("<-- %s return %d\n", __func__, status);
4854 * Reset a slot table
4856 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4859 struct nfs4_slot *new = NULL;
4863 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4864 max_reqs, tbl->max_slots);
4866 /* Does the newly negotiated max_reqs match the existing slot table? */
4867 if (max_reqs != tbl->max_slots) {
4869 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
4876 spin_lock(&tbl->slot_tbl_lock);
4879 tbl->max_slots = max_reqs;
4881 for (i = 0; i < tbl->max_slots; ++i)
4882 tbl->slots[i].seq_nr = ivalue;
4883 spin_unlock(&tbl->slot_tbl_lock);
4884 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4885 tbl, tbl->slots, tbl->max_slots);
4887 dprintk("<-- %s: return %d\n", __func__, ret);
4892 * Reset the forechannel and backchannel slot tables
4894 static int nfs4_reset_slot_tables(struct nfs4_session *session)
4898 status = nfs4_reset_slot_table(&session->fc_slot_table,
4899 session->fc_attrs.max_reqs, 1);
4903 status = nfs4_reset_slot_table(&session->bc_slot_table,
4904 session->bc_attrs.max_reqs, 0);
4908 /* Destroy the slot table */
4909 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4911 if (session->fc_slot_table.slots != NULL) {
4912 kfree(session->fc_slot_table.slots);
4913 session->fc_slot_table.slots = NULL;
4915 if (session->bc_slot_table.slots != NULL) {
4916 kfree(session->bc_slot_table.slots);
4917 session->bc_slot_table.slots = NULL;
4923 * Initialize slot table
4925 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4926 int max_slots, int ivalue)
4928 struct nfs4_slot *slot;
4931 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4933 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
4935 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
4940 spin_lock(&tbl->slot_tbl_lock);
4941 tbl->max_slots = max_slots;
4943 tbl->highest_used_slotid = -1; /* no slot is currently used */
4944 spin_unlock(&tbl->slot_tbl_lock);
4945 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4946 tbl, tbl->slots, tbl->max_slots);
4948 dprintk("<-- %s: return %d\n", __func__, ret);
4953 * Initialize the forechannel and backchannel tables
4955 static int nfs4_init_slot_tables(struct nfs4_session *session)
4957 struct nfs4_slot_table *tbl;
4960 tbl = &session->fc_slot_table;
4961 if (tbl->slots == NULL) {
4962 status = nfs4_init_slot_table(tbl,
4963 session->fc_attrs.max_reqs, 1);
4968 tbl = &session->bc_slot_table;
4969 if (tbl->slots == NULL) {
4970 status = nfs4_init_slot_table(tbl,
4971 session->bc_attrs.max_reqs, 0);
4973 nfs4_destroy_slot_tables(session);
4979 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4981 struct nfs4_session *session;
4982 struct nfs4_slot_table *tbl;
4984 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
4988 tbl = &session->fc_slot_table;
4989 tbl->highest_used_slotid = -1;
4990 spin_lock_init(&tbl->slot_tbl_lock);
4991 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4992 init_completion(&tbl->complete);
4994 tbl = &session->bc_slot_table;
4995 tbl->highest_used_slotid = -1;
4996 spin_lock_init(&tbl->slot_tbl_lock);
4997 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4998 init_completion(&tbl->complete);
5000 session->session_state = 1<<NFS4_SESSION_INITING;
5006 void nfs4_destroy_session(struct nfs4_session *session)
5008 nfs4_proc_destroy_session(session);
5009 dprintk("%s Destroy backchannel for xprt %p\n",
5010 __func__, session->clp->cl_rpcclient->cl_xprt);
5011 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5012 NFS41_BC_MIN_CALLBACKS);
5013 nfs4_destroy_slot_tables(session);
5018 * Initialize the values to be used by the client in CREATE_SESSION
5019 * If nfs4_init_session set the fore channel request and response sizes,
5022 * Set the back channel max_resp_sz_cached to zero to force the client to
5023 * always set csa_cachethis to FALSE because the current implementation
5024 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5026 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5028 struct nfs4_session *session = args->client->cl_session;
5029 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5030 mxresp_sz = session->fc_attrs.max_resp_sz;
5033 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5035 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5036 /* Fore channel attributes */
5037 args->fc_attrs.headerpadsz = 0;
5038 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5039 args->fc_attrs.max_resp_sz = mxresp_sz;
5040 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5041 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5043 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5044 "max_ops=%u max_reqs=%u\n",
5046 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5047 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5049 /* Back channel attributes */
5050 args->bc_attrs.headerpadsz = 0;
5051 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5052 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5053 args->bc_attrs.max_resp_sz_cached = 0;
5054 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5055 args->bc_attrs.max_reqs = 1;
5057 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5058 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5060 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5061 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5062 args->bc_attrs.max_reqs);
5065 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5067 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5068 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5070 if (rcvd->headerpadsz > sent->headerpadsz)
5072 if (rcvd->max_resp_sz > sent->max_resp_sz)
5075 * Our requested max_ops is the minimum we need; we're not
5076 * prepared to break up compounds into smaller pieces than that.
5077 * So, no point even trying to continue if the server won't
5080 if (rcvd->max_ops < sent->max_ops)
5082 if (rcvd->max_reqs == 0)
5087 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5089 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5090 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5092 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5094 if (rcvd->max_resp_sz < sent->max_resp_sz)
5096 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5098 /* These would render the backchannel useless: */
5099 if (rcvd->max_ops == 0)
5101 if (rcvd->max_reqs == 0)
5106 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5107 struct nfs4_session *session)
5111 ret = nfs4_verify_fore_channel_attrs(args, session);
5114 return nfs4_verify_back_channel_attrs(args, session);
5117 static int _nfs4_proc_create_session(struct nfs_client *clp)
5119 struct nfs4_session *session = clp->cl_session;
5120 struct nfs41_create_session_args args = {
5122 .cb_program = NFS4_CALLBACK,
5124 struct nfs41_create_session_res res = {
5127 struct rpc_message msg = {
5128 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5134 nfs4_init_channel_attrs(&args);
5135 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5137 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5140 /* Verify the session's negotiated channel_attrs values */
5141 status = nfs4_verify_channel_attrs(&args, session);
5143 /* Increment the clientid slot sequence id */
5151 * Issues a CREATE_SESSION operation to the server.
5152 * It is the responsibility of the caller to verify the session is
5153 * expired before calling this routine.
5155 int nfs4_proc_create_session(struct nfs_client *clp)
5159 struct nfs4_session *session = clp->cl_session;
5163 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5166 status = _nfs4_proc_create_session(clp);
5167 if (status == -NFS4ERR_DELAY) {
5168 err = nfs4_delay(clp->cl_rpcclient, &timeout);
5172 } while (status == -NFS4ERR_DELAY);
5177 /* Init and reset the fore channel */
5178 status = nfs4_init_slot_tables(session);
5179 dprintk("slot table initialization returned %d\n", status);
5182 status = nfs4_reset_slot_tables(session);
5183 dprintk("slot table reset returned %d\n", status);
5187 ptr = (unsigned *)&session->sess_id.data[0];
5188 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5189 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5191 dprintk("<-- %s\n", __func__);
5196 * Issue the over-the-wire RPC DESTROY_SESSION.
5197 * The caller must serialize access to this routine.
5199 int nfs4_proc_destroy_session(struct nfs4_session *session)
5202 struct rpc_message msg;
5204 dprintk("--> nfs4_proc_destroy_session\n");
5206 /* session is still being setup */
5207 if (session->clp->cl_cons_state != NFS_CS_READY)
5210 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5211 msg.rpc_argp = session;
5212 msg.rpc_resp = NULL;
5213 msg.rpc_cred = NULL;
5214 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5218 "Got error %d from the server on DESTROY_SESSION. "
5219 "Session has been destroyed regardless...\n", status);
5221 dprintk("<-- nfs4_proc_destroy_session\n");
5225 int nfs4_init_session(struct nfs_server *server)
5227 struct nfs_client *clp = server->nfs_client;
5228 struct nfs4_session *session;
5229 unsigned int rsize, wsize;
5232 if (!nfs4_has_session(clp))
5235 session = clp->cl_session;
5236 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5239 rsize = server->rsize;
5241 rsize = NFS_MAX_FILE_IO_SIZE;
5242 wsize = server->wsize;
5244 wsize = NFS_MAX_FILE_IO_SIZE;
5246 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5247 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5249 ret = nfs4_recover_expired_lease(server);
5251 ret = nfs4_check_client_ready(clp);
5255 int nfs4_init_ds_session(struct nfs_client *clp)
5257 struct nfs4_session *session = clp->cl_session;
5260 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5263 ret = nfs4_client_recover_expired_lease(clp);
5265 /* Test for the DS role */
5266 if (!is_ds_client(clp))
5269 ret = nfs4_check_client_ready(clp);
5273 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5277 * Renew the cl_session lease.
5279 struct nfs4_sequence_data {
5280 struct nfs_client *clp;
5281 struct nfs4_sequence_args args;
5282 struct nfs4_sequence_res res;
5285 static void nfs41_sequence_release(void *data)
5287 struct nfs4_sequence_data *calldata = data;
5288 struct nfs_client *clp = calldata->clp;
5290 if (atomic_read(&clp->cl_count) > 1)
5291 nfs4_schedule_state_renewal(clp);
5292 nfs_put_client(clp);
5296 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5298 switch(task->tk_status) {
5299 case -NFS4ERR_DELAY:
5300 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5303 nfs4_schedule_lease_recovery(clp);
5308 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5310 struct nfs4_sequence_data *calldata = data;
5311 struct nfs_client *clp = calldata->clp;
5313 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5316 if (task->tk_status < 0) {
5317 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5318 if (atomic_read(&clp->cl_count) == 1)
5321 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5322 rpc_restart_call_prepare(task);
5326 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5328 dprintk("<-- %s\n", __func__);
5331 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5333 struct nfs4_sequence_data *calldata = data;
5334 struct nfs_client *clp = calldata->clp;
5335 struct nfs4_sequence_args *args;
5336 struct nfs4_sequence_res *res;
5338 args = task->tk_msg.rpc_argp;
5339 res = task->tk_msg.rpc_resp;
5341 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
5343 rpc_call_start(task);
5346 static const struct rpc_call_ops nfs41_sequence_ops = {
5347 .rpc_call_done = nfs41_sequence_call_done,
5348 .rpc_call_prepare = nfs41_sequence_prepare,
5349 .rpc_release = nfs41_sequence_release,
5352 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5354 struct nfs4_sequence_data *calldata;
5355 struct rpc_message msg = {
5356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5359 struct rpc_task_setup task_setup_data = {
5360 .rpc_client = clp->cl_rpcclient,
5361 .rpc_message = &msg,
5362 .callback_ops = &nfs41_sequence_ops,
5363 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5366 if (!atomic_inc_not_zero(&clp->cl_count))
5367 return ERR_PTR(-EIO);
5368 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5369 if (calldata == NULL) {
5370 nfs_put_client(clp);
5371 return ERR_PTR(-ENOMEM);
5373 msg.rpc_argp = &calldata->args;
5374 msg.rpc_resp = &calldata->res;
5375 calldata->clp = clp;
5376 task_setup_data.callback_data = calldata;
5378 return rpc_run_task(&task_setup_data);
5381 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5383 struct rpc_task *task;
5386 task = _nfs41_proc_sequence(clp, cred);
5388 ret = PTR_ERR(task);
5390 rpc_put_task_async(task);
5391 dprintk("<-- %s status=%d\n", __func__, ret);
5395 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5397 struct rpc_task *task;
5400 task = _nfs41_proc_sequence(clp, cred);
5402 ret = PTR_ERR(task);
5405 ret = rpc_wait_for_completion_task(task);
5407 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5409 if (task->tk_status == 0)
5410 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5411 ret = task->tk_status;
5415 dprintk("<-- %s status=%d\n", __func__, ret);
5419 struct nfs4_reclaim_complete_data {
5420 struct nfs_client *clp;
5421 struct nfs41_reclaim_complete_args arg;
5422 struct nfs41_reclaim_complete_res res;
5425 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5427 struct nfs4_reclaim_complete_data *calldata = data;
5429 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5430 if (nfs41_setup_sequence(calldata->clp->cl_session,
5431 &calldata->arg.seq_args,
5432 &calldata->res.seq_res, 0, task))
5435 rpc_call_start(task);
5438 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5440 switch(task->tk_status) {
5442 case -NFS4ERR_COMPLETE_ALREADY:
5443 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5445 case -NFS4ERR_DELAY:
5446 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5449 nfs4_schedule_lease_recovery(clp);
5454 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5456 struct nfs4_reclaim_complete_data *calldata = data;
5457 struct nfs_client *clp = calldata->clp;
5458 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5460 dprintk("--> %s\n", __func__);
5461 if (!nfs41_sequence_done(task, res))
5464 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5465 rpc_restart_call_prepare(task);
5468 dprintk("<-- %s\n", __func__);
5471 static void nfs4_free_reclaim_complete_data(void *data)
5473 struct nfs4_reclaim_complete_data *calldata = data;
5478 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5479 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5480 .rpc_call_done = nfs4_reclaim_complete_done,
5481 .rpc_release = nfs4_free_reclaim_complete_data,
5485 * Issue a global reclaim complete.
5487 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5489 struct nfs4_reclaim_complete_data *calldata;
5490 struct rpc_task *task;
5491 struct rpc_message msg = {
5492 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5494 struct rpc_task_setup task_setup_data = {
5495 .rpc_client = clp->cl_rpcclient,
5496 .rpc_message = &msg,
5497 .callback_ops = &nfs4_reclaim_complete_call_ops,
5498 .flags = RPC_TASK_ASYNC,
5500 int status = -ENOMEM;
5502 dprintk("--> %s\n", __func__);
5503 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5504 if (calldata == NULL)
5506 calldata->clp = clp;
5507 calldata->arg.one_fs = 0;
5509 msg.rpc_argp = &calldata->arg;
5510 msg.rpc_resp = &calldata->res;
5511 task_setup_data.callback_data = calldata;
5512 task = rpc_run_task(&task_setup_data);
5514 status = PTR_ERR(task);
5517 status = nfs4_wait_for_completion_rpc_task(task);
5519 status = task->tk_status;
5523 dprintk("<-- %s status=%d\n", __func__, status);
5528 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5530 struct nfs4_layoutget *lgp = calldata;
5531 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5533 dprintk("--> %s\n", __func__);
5534 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5535 * right now covering the LAYOUTGET we are about to send.
5536 * However, that is not so catastrophic, and there seems
5537 * to be no way to prevent it completely.
5539 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5540 &lgp->res.seq_res, 0, task))
5542 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5543 NFS_I(lgp->args.inode)->layout,
5544 lgp->args.ctx->state)) {
5545 rpc_exit(task, NFS4_OK);
5548 rpc_call_start(task);
5551 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5553 struct nfs4_layoutget *lgp = calldata;
5554 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5556 dprintk("--> %s\n", __func__);
5558 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5561 switch (task->tk_status) {
5564 case -NFS4ERR_LAYOUTTRYLATER:
5565 case -NFS4ERR_RECALLCONFLICT:
5566 task->tk_status = -NFS4ERR_DELAY;
5569 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5570 rpc_restart_call_prepare(task);
5574 dprintk("<-- %s\n", __func__);
5577 static void nfs4_layoutget_release(void *calldata)
5579 struct nfs4_layoutget *lgp = calldata;
5581 dprintk("--> %s\n", __func__);
5582 if (lgp->res.layout.buf != NULL)
5583 free_page((unsigned long) lgp->res.layout.buf);
5584 put_nfs_open_context(lgp->args.ctx);
5586 dprintk("<-- %s\n", __func__);
5589 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5590 .rpc_call_prepare = nfs4_layoutget_prepare,
5591 .rpc_call_done = nfs4_layoutget_done,
5592 .rpc_release = nfs4_layoutget_release,
5595 int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5597 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5598 struct rpc_task *task;
5599 struct rpc_message msg = {
5600 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5601 .rpc_argp = &lgp->args,
5602 .rpc_resp = &lgp->res,
5604 struct rpc_task_setup task_setup_data = {
5605 .rpc_client = server->client,
5606 .rpc_message = &msg,
5607 .callback_ops = &nfs4_layoutget_call_ops,
5608 .callback_data = lgp,
5609 .flags = RPC_TASK_ASYNC,
5613 dprintk("--> %s\n", __func__);
5615 lgp->res.layout.buf = (void *)__get_free_page(GFP_NOFS);
5616 if (lgp->res.layout.buf == NULL) {
5617 nfs4_layoutget_release(lgp);
5621 lgp->res.seq_res.sr_slot = NULL;
5622 task = rpc_run_task(&task_setup_data);
5624 return PTR_ERR(task);
5625 status = nfs4_wait_for_completion_rpc_task(task);
5627 status = task->tk_status;
5629 status = pnfs_layout_process(lgp);
5631 dprintk("<-- %s status=%d\n", __func__, status);
5636 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5638 struct nfs4_getdeviceinfo_args args = {
5641 struct nfs4_getdeviceinfo_res res = {
5644 struct rpc_message msg = {
5645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5651 dprintk("--> %s\n", __func__);
5652 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5653 dprintk("<-- %s status=%d\n", __func__, status);
5658 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5660 struct nfs4_exception exception = { };
5664 err = nfs4_handle_exception(server,
5665 _nfs4_proc_getdeviceinfo(server, pdev),
5667 } while (exception.retry);
5670 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5672 #endif /* CONFIG_NFS_V4_1 */
5674 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
5675 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5676 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5677 .recover_open = nfs4_open_reclaim,
5678 .recover_lock = nfs4_lock_reclaim,
5679 .establish_clid = nfs4_init_clientid,
5680 .get_clid_cred = nfs4_get_setclientid_cred,
5683 #if defined(CONFIG_NFS_V4_1)
5684 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5685 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5686 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5687 .recover_open = nfs4_open_reclaim,
5688 .recover_lock = nfs4_lock_reclaim,
5689 .establish_clid = nfs41_init_clientid,
5690 .get_clid_cred = nfs4_get_exchange_id_cred,
5691 .reclaim_complete = nfs41_proc_reclaim_complete,
5693 #endif /* CONFIG_NFS_V4_1 */
5695 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
5696 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5697 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5698 .recover_open = nfs4_open_expired,
5699 .recover_lock = nfs4_lock_expired,
5700 .establish_clid = nfs4_init_clientid,
5701 .get_clid_cred = nfs4_get_setclientid_cred,
5704 #if defined(CONFIG_NFS_V4_1)
5705 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5706 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5707 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5708 .recover_open = nfs4_open_expired,
5709 .recover_lock = nfs4_lock_expired,
5710 .establish_clid = nfs41_init_clientid,
5711 .get_clid_cred = nfs4_get_exchange_id_cred,
5713 #endif /* CONFIG_NFS_V4_1 */
5715 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5716 .sched_state_renewal = nfs4_proc_async_renew,
5717 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
5718 .renew_lease = nfs4_proc_renew,
5721 #if defined(CONFIG_NFS_V4_1)
5722 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5723 .sched_state_renewal = nfs41_proc_async_sequence,
5724 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
5725 .renew_lease = nfs4_proc_sequence,
5729 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5731 .call_sync = _nfs4_call_sync,
5732 .validate_stateid = nfs4_validate_delegation_stateid,
5733 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5734 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5735 .state_renewal_ops = &nfs40_state_renewal_ops,
5738 #if defined(CONFIG_NFS_V4_1)
5739 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5741 .call_sync = _nfs4_call_sync_session,
5742 .validate_stateid = nfs41_validate_delegation_stateid,
5743 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5744 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5745 .state_renewal_ops = &nfs41_state_renewal_ops,
5749 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5750 [0] = &nfs_v4_0_minor_ops,
5751 #if defined(CONFIG_NFS_V4_1)
5752 [1] = &nfs_v4_1_minor_ops,
5756 static const struct inode_operations nfs4_file_inode_operations = {
5757 .permission = nfs_permission,
5758 .getattr = nfs_getattr,
5759 .setattr = nfs_setattr,
5760 .getxattr = generic_getxattr,
5761 .setxattr = generic_setxattr,
5762 .listxattr = generic_listxattr,
5763 .removexattr = generic_removexattr,
5766 const struct nfs_rpc_ops nfs_v4_clientops = {
5767 .version = 4, /* protocol version */
5768 .dentry_ops = &nfs4_dentry_operations,
5769 .dir_inode_ops = &nfs4_dir_inode_operations,
5770 .file_inode_ops = &nfs4_file_inode_operations,
5771 .getroot = nfs4_proc_get_root,
5772 .getattr = nfs4_proc_getattr,
5773 .setattr = nfs4_proc_setattr,
5774 .lookupfh = nfs4_proc_lookupfh,
5775 .lookup = nfs4_proc_lookup,
5776 .access = nfs4_proc_access,
5777 .readlink = nfs4_proc_readlink,
5778 .create = nfs4_proc_create,
5779 .remove = nfs4_proc_remove,
5780 .unlink_setup = nfs4_proc_unlink_setup,
5781 .unlink_done = nfs4_proc_unlink_done,
5782 .rename = nfs4_proc_rename,
5783 .rename_setup = nfs4_proc_rename_setup,
5784 .rename_done = nfs4_proc_rename_done,
5785 .link = nfs4_proc_link,
5786 .symlink = nfs4_proc_symlink,
5787 .mkdir = nfs4_proc_mkdir,
5788 .rmdir = nfs4_proc_remove,
5789 .readdir = nfs4_proc_readdir,
5790 .mknod = nfs4_proc_mknod,
5791 .statfs = nfs4_proc_statfs,
5792 .fsinfo = nfs4_proc_fsinfo,
5793 .pathconf = nfs4_proc_pathconf,
5794 .set_capabilities = nfs4_server_capabilities,
5795 .decode_dirent = nfs4_decode_dirent,
5796 .read_setup = nfs4_proc_read_setup,
5797 .read_done = nfs4_read_done,
5798 .write_setup = nfs4_proc_write_setup,
5799 .write_done = nfs4_write_done,
5800 .commit_setup = nfs4_proc_commit_setup,
5801 .commit_done = nfs4_commit_done,
5802 .lock = nfs4_proc_lock,
5803 .clear_acl_cache = nfs4_zap_acl_attr,
5804 .close_context = nfs4_close_context,
5805 .open_context = nfs4_atomic_open,
5806 .init_client = nfs4_init_client,
5807 .secinfo = nfs4_proc_secinfo,
5810 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
5811 .prefix = XATTR_NAME_NFSV4_ACL,
5812 .list = nfs4_xattr_list_nfs4_acl,
5813 .get = nfs4_xattr_get_nfs4_acl,
5814 .set = nfs4_xattr_set_nfs4_acl,
5817 const struct xattr_handler *nfs4_xattr_handlers[] = {
5818 &nfs4_xattr_nfs4_acl_handler,