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 inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
75 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
76 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
77 struct nfs_fattr *fattr, struct iattr *sattr,
78 struct nfs4_state *state);
80 /* Prevent leaks of NFSv4 errors into userland */
81 static int nfs4_map_errors(int err)
86 case -NFS4ERR_RESOURCE:
89 dprintk("%s could not handle NFSv4 error %d\n",
97 * This is our standard bitmap for GETATTR requests.
99 const u32 nfs4_fattr_bitmap[2] = {
101 | FATTR4_WORD0_CHANGE
104 | FATTR4_WORD0_FILEID,
106 | FATTR4_WORD1_NUMLINKS
108 | FATTR4_WORD1_OWNER_GROUP
109 | FATTR4_WORD1_RAWDEV
110 | FATTR4_WORD1_SPACE_USED
111 | FATTR4_WORD1_TIME_ACCESS
112 | FATTR4_WORD1_TIME_METADATA
113 | FATTR4_WORD1_TIME_MODIFY
116 const u32 nfs4_statfs_bitmap[2] = {
117 FATTR4_WORD0_FILES_AVAIL
118 | FATTR4_WORD0_FILES_FREE
119 | FATTR4_WORD0_FILES_TOTAL,
120 FATTR4_WORD1_SPACE_AVAIL
121 | FATTR4_WORD1_SPACE_FREE
122 | FATTR4_WORD1_SPACE_TOTAL
125 const u32 nfs4_pathconf_bitmap[2] = {
127 | FATTR4_WORD0_MAXNAME,
131 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
132 | FATTR4_WORD0_MAXREAD
133 | FATTR4_WORD0_MAXWRITE
134 | FATTR4_WORD0_LEASE_TIME,
135 FATTR4_WORD1_TIME_DELTA
136 | FATTR4_WORD1_FS_LAYOUT_TYPES
139 const u32 nfs4_fs_locations_bitmap[2] = {
141 | FATTR4_WORD0_CHANGE
144 | FATTR4_WORD0_FILEID
145 | FATTR4_WORD0_FS_LOCATIONS,
147 | FATTR4_WORD1_NUMLINKS
149 | FATTR4_WORD1_OWNER_GROUP
150 | FATTR4_WORD1_RAWDEV
151 | FATTR4_WORD1_SPACE_USED
152 | FATTR4_WORD1_TIME_ACCESS
153 | FATTR4_WORD1_TIME_METADATA
154 | FATTR4_WORD1_TIME_MODIFY
155 | FATTR4_WORD1_MOUNTED_ON_FILEID
158 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
159 struct nfs4_readdir_arg *readdir)
163 BUG_ON(readdir->count < 80);
165 readdir->cookie = cookie;
166 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
171 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
176 * NFSv4 servers do not return entries for '.' and '..'
177 * Therefore, we fake these entries here. We let '.'
178 * have cookie 0 and '..' have cookie 1. Note that
179 * when talking to the server, we always send cookie 0
182 start = p = kmap_atomic(*readdir->pages, KM_USER0);
185 *p++ = xdr_one; /* next */
186 *p++ = xdr_zero; /* cookie, first word */
187 *p++ = xdr_one; /* cookie, second word */
188 *p++ = xdr_one; /* entry len */
189 memcpy(p, ".\0\0\0", 4); /* entry */
191 *p++ = xdr_one; /* bitmap length */
192 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
193 *p++ = htonl(8); /* attribute buffer length */
194 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
197 *p++ = xdr_one; /* next */
198 *p++ = xdr_zero; /* cookie, first word */
199 *p++ = xdr_two; /* cookie, second word */
200 *p++ = xdr_two; /* entry len */
201 memcpy(p, "..\0\0", 4); /* entry */
203 *p++ = xdr_one; /* bitmap length */
204 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
205 *p++ = htonl(8); /* attribute buffer length */
206 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
208 readdir->pgbase = (char *)p - (char *)start;
209 readdir->count -= readdir->pgbase;
210 kunmap_atomic(start, KM_USER0);
213 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
219 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
220 nfs_wait_bit_killable, TASK_KILLABLE);
224 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
231 *timeout = NFS4_POLL_RETRY_MIN;
232 if (*timeout > NFS4_POLL_RETRY_MAX)
233 *timeout = NFS4_POLL_RETRY_MAX;
234 schedule_timeout_killable(*timeout);
235 if (fatal_signal_pending(current))
241 /* This is the error handling routine for processes that are allowed
244 static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
246 struct nfs_client *clp = server->nfs_client;
247 struct nfs4_state *state = exception->state;
250 exception->retry = 0;
254 case -NFS4ERR_ADMIN_REVOKED:
255 case -NFS4ERR_BAD_STATEID:
256 case -NFS4ERR_OPENMODE:
259 nfs4_schedule_stateid_recovery(server, state);
260 goto wait_on_recovery;
261 case -NFS4ERR_STALE_STATEID:
262 case -NFS4ERR_STALE_CLIENTID:
263 case -NFS4ERR_EXPIRED:
264 nfs4_schedule_lease_recovery(clp);
265 goto wait_on_recovery;
266 #if defined(CONFIG_NFS_V4_1)
267 case -NFS4ERR_BADSESSION:
268 case -NFS4ERR_BADSLOT:
269 case -NFS4ERR_BAD_HIGH_SLOT:
270 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
271 case -NFS4ERR_DEADSESSION:
272 case -NFS4ERR_SEQ_FALSE_RETRY:
273 case -NFS4ERR_SEQ_MISORDERED:
274 dprintk("%s ERROR: %d Reset session\n", __func__,
276 nfs4_schedule_session_recovery(clp->cl_session);
277 exception->retry = 1;
279 #endif /* defined(CONFIG_NFS_V4_1) */
280 case -NFS4ERR_FILE_OPEN:
281 if (exception->timeout > HZ) {
282 /* We have retried a decent amount, time to
291 ret = nfs4_delay(server->client, &exception->timeout);
294 case -NFS4ERR_OLD_STATEID:
295 exception->retry = 1;
297 /* We failed to handle the error */
298 return nfs4_map_errors(ret);
300 ret = nfs4_wait_clnt_recover(clp);
302 exception->retry = 1;
307 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
309 spin_lock(&clp->cl_lock);
310 if (time_before(clp->cl_last_renewal,timestamp))
311 clp->cl_last_renewal = timestamp;
312 spin_unlock(&clp->cl_lock);
315 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
317 do_renew_lease(server->nfs_client, timestamp);
320 #if defined(CONFIG_NFS_V4_1)
323 * nfs4_free_slot - free a slot and efficiently update slot table.
325 * freeing a slot is trivially done by clearing its respective bit
327 * If the freed slotid equals highest_used_slotid we want to update it
328 * so that the server would be able to size down the slot table if needed,
329 * otherwise we know that the highest_used_slotid is still in use.
330 * When updating highest_used_slotid there may be "holes" in the bitmap
331 * so we need to scan down from highest_used_slotid to 0 looking for the now
332 * highest slotid in use.
333 * If none found, highest_used_slotid is set to -1.
335 * Must be called while holding tbl->slot_tbl_lock
338 nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
340 int free_slotid = free_slot - tbl->slots;
341 int slotid = free_slotid;
343 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
344 /* clear used bit in bitmap */
345 __clear_bit(slotid, tbl->used_slots);
347 /* update highest_used_slotid when it is freed */
348 if (slotid == tbl->highest_used_slotid) {
349 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
350 if (slotid < tbl->max_slots)
351 tbl->highest_used_slotid = slotid;
353 tbl->highest_used_slotid = -1;
355 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
356 free_slotid, tbl->highest_used_slotid);
360 * Signal state manager thread if session fore channel is drained
362 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
364 struct rpc_task *task;
366 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
367 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
369 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
373 if (ses->fc_slot_table.highest_used_slotid != -1)
376 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
377 complete(&ses->fc_slot_table.complete);
381 * Signal state manager thread if session back channel is drained
383 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
385 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
386 ses->bc_slot_table.highest_used_slotid != -1)
388 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
389 complete(&ses->bc_slot_table.complete);
392 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
394 struct nfs4_slot_table *tbl;
396 tbl = &res->sr_session->fc_slot_table;
398 /* just wake up the next guy waiting since
399 * we may have not consumed a slot after all */
400 dprintk("%s: No slot\n", __func__);
404 spin_lock(&tbl->slot_tbl_lock);
405 nfs4_free_slot(tbl, res->sr_slot);
406 nfs4_check_drain_fc_complete(res->sr_session);
407 spin_unlock(&tbl->slot_tbl_lock);
411 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
413 unsigned long timestamp;
414 struct nfs_client *clp;
417 * sr_status remains 1 if an RPC level error occurred. The server
418 * may or may not have processed the sequence operation..
419 * Proceed as if the server received and processed the sequence
422 if (res->sr_status == 1)
423 res->sr_status = NFS_OK;
425 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
429 /* Check the SEQUENCE operation status */
430 switch (res->sr_status) {
432 /* Update the slot's sequence and clientid lease timer */
433 ++res->sr_slot->seq_nr;
434 timestamp = res->sr_renewal_time;
435 clp = res->sr_session->clp;
436 do_renew_lease(clp, timestamp);
437 /* Check sequence flags */
438 if (res->sr_status_flags != 0)
439 nfs4_schedule_lease_recovery(clp);
442 /* The server detected a resend of the RPC call and
443 * returned NFS4ERR_DELAY as per Section 2.10.6.2
446 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
448 res->sr_slot - res->sr_session->fc_slot_table.slots,
449 res->sr_slot->seq_nr);
452 /* Just update the slot sequence no. */
453 ++res->sr_slot->seq_nr;
456 /* The session may be reset by one of the error handlers. */
457 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
458 nfs41_sequence_free_slot(res);
461 if (!rpc_restart_call(task))
463 rpc_delay(task, NFS4_POLL_RETRY_MAX);
467 static int nfs4_sequence_done(struct rpc_task *task,
468 struct nfs4_sequence_res *res)
470 if (res->sr_session == NULL)
472 return nfs41_sequence_done(task, res);
476 * nfs4_find_slot - efficiently look for a free slot
478 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
479 * If found, we mark the slot as used, update the highest_used_slotid,
480 * and respectively set up the sequence operation args.
481 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
483 * Note: must be called with under the slot_tbl_lock.
486 nfs4_find_slot(struct nfs4_slot_table *tbl)
489 u8 ret_id = NFS4_MAX_SLOT_TABLE;
490 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
492 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
493 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
495 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
496 if (slotid >= tbl->max_slots)
498 __set_bit(slotid, tbl->used_slots);
499 if (slotid > tbl->highest_used_slotid)
500 tbl->highest_used_slotid = slotid;
503 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
504 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
508 static int nfs41_setup_sequence(struct nfs4_session *session,
509 struct nfs4_sequence_args *args,
510 struct nfs4_sequence_res *res,
512 struct rpc_task *task)
514 struct nfs4_slot *slot;
515 struct nfs4_slot_table *tbl;
518 dprintk("--> %s\n", __func__);
519 /* slot already allocated? */
520 if (res->sr_slot != NULL)
523 tbl = &session->fc_slot_table;
525 spin_lock(&tbl->slot_tbl_lock);
526 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
527 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
529 * The state manager will wait until the slot table is empty.
530 * Schedule the reset thread
532 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
533 spin_unlock(&tbl->slot_tbl_lock);
534 dprintk("%s Schedule Session Reset\n", __func__);
538 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
539 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
540 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
541 spin_unlock(&tbl->slot_tbl_lock);
542 dprintk("%s enforce FIFO order\n", __func__);
546 slotid = nfs4_find_slot(tbl);
547 if (slotid == NFS4_MAX_SLOT_TABLE) {
548 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
549 spin_unlock(&tbl->slot_tbl_lock);
550 dprintk("<-- %s: no free slots\n", __func__);
553 spin_unlock(&tbl->slot_tbl_lock);
555 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
556 slot = tbl->slots + slotid;
557 args->sa_session = session;
558 args->sa_slotid = slotid;
559 args->sa_cache_this = cache_reply;
561 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
563 res->sr_session = session;
565 res->sr_renewal_time = jiffies;
566 res->sr_status_flags = 0;
568 * sr_status is only set in decode_sequence, and so will remain
569 * set to 1 if an rpc level failure occurs.
575 int nfs4_setup_sequence(const struct nfs_server *server,
576 struct nfs4_sequence_args *args,
577 struct nfs4_sequence_res *res,
579 struct rpc_task *task)
581 struct nfs4_session *session = nfs4_get_session(server);
584 if (session == NULL) {
585 args->sa_session = NULL;
586 res->sr_session = NULL;
590 dprintk("--> %s clp %p session %p sr_slot %td\n",
591 __func__, session->clp, session, res->sr_slot ?
592 res->sr_slot - session->fc_slot_table.slots : -1);
594 ret = nfs41_setup_sequence(session, args, res, cache_reply,
597 dprintk("<-- %s status=%d\n", __func__, ret);
601 struct nfs41_call_sync_data {
602 const struct nfs_server *seq_server;
603 struct nfs4_sequence_args *seq_args;
604 struct nfs4_sequence_res *seq_res;
608 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
610 struct nfs41_call_sync_data *data = calldata;
612 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
614 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
615 data->seq_res, data->cache_reply, task))
617 rpc_call_start(task);
620 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
622 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
623 nfs41_call_sync_prepare(task, calldata);
626 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
628 struct nfs41_call_sync_data *data = calldata;
630 nfs41_sequence_done(task, data->seq_res);
633 struct rpc_call_ops nfs41_call_sync_ops = {
634 .rpc_call_prepare = nfs41_call_sync_prepare,
635 .rpc_call_done = nfs41_call_sync_done,
638 struct rpc_call_ops nfs41_call_priv_sync_ops = {
639 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
640 .rpc_call_done = nfs41_call_sync_done,
643 static int nfs4_call_sync_sequence(struct nfs_server *server,
644 struct rpc_message *msg,
645 struct nfs4_sequence_args *args,
646 struct nfs4_sequence_res *res,
651 struct rpc_task *task;
652 struct nfs41_call_sync_data data = {
653 .seq_server = server,
656 .cache_reply = cache_reply,
658 struct rpc_task_setup task_setup = {
659 .rpc_client = server->client,
661 .callback_ops = &nfs41_call_sync_ops,
662 .callback_data = &data
667 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
668 task = rpc_run_task(&task_setup);
672 ret = task->tk_status;
678 int _nfs4_call_sync_session(struct nfs_server *server,
679 struct rpc_message *msg,
680 struct nfs4_sequence_args *args,
681 struct nfs4_sequence_res *res,
684 return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
688 static int nfs4_sequence_done(struct rpc_task *task,
689 struct nfs4_sequence_res *res)
693 #endif /* CONFIG_NFS_V4_1 */
695 int _nfs4_call_sync(struct nfs_server *server,
696 struct rpc_message *msg,
697 struct nfs4_sequence_args *args,
698 struct nfs4_sequence_res *res,
701 args->sa_session = res->sr_session = NULL;
702 return rpc_call_sync(server->client, msg, 0);
705 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
706 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
707 &(res)->seq_res, (cache_reply))
709 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
711 struct nfs_inode *nfsi = NFS_I(dir);
713 spin_lock(&dir->i_lock);
714 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
715 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
716 nfs_force_lookup_revalidate(dir);
717 nfsi->change_attr = cinfo->after;
718 spin_unlock(&dir->i_lock);
721 struct nfs4_opendata {
723 struct nfs_openargs o_arg;
724 struct nfs_openres o_res;
725 struct nfs_open_confirmargs c_arg;
726 struct nfs_open_confirmres c_res;
727 struct nfs_fattr f_attr;
728 struct nfs_fattr dir_attr;
731 struct nfs4_state_owner *owner;
732 struct nfs4_state *state;
734 unsigned long timestamp;
735 unsigned int rpc_done : 1;
741 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
743 p->o_res.f_attr = &p->f_attr;
744 p->o_res.dir_attr = &p->dir_attr;
745 p->o_res.seqid = p->o_arg.seqid;
746 p->c_res.seqid = p->c_arg.seqid;
747 p->o_res.server = p->o_arg.server;
748 nfs_fattr_init(&p->f_attr);
749 nfs_fattr_init(&p->dir_attr);
752 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
753 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
754 const struct iattr *attrs,
757 struct dentry *parent = dget_parent(path->dentry);
758 struct inode *dir = parent->d_inode;
759 struct nfs_server *server = NFS_SERVER(dir);
760 struct nfs4_opendata *p;
762 p = kzalloc(sizeof(*p), gfp_mask);
765 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
766 if (p->o_arg.seqid == NULL)
772 atomic_inc(&sp->so_count);
773 p->o_arg.fh = NFS_FH(dir);
774 p->o_arg.open_flags = flags;
775 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
776 p->o_arg.clientid = server->nfs_client->cl_clientid;
777 p->o_arg.id = sp->so_owner_id.id;
778 p->o_arg.name = &p->path.dentry->d_name;
779 p->o_arg.server = server;
780 p->o_arg.bitmask = server->attr_bitmask;
781 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
782 if (flags & O_CREAT) {
785 p->o_arg.u.attrs = &p->attrs;
786 memcpy(&p->attrs, attrs, sizeof(p->attrs));
787 s = (u32 *) p->o_arg.u.verifier.data;
791 p->c_arg.fh = &p->o_res.fh;
792 p->c_arg.stateid = &p->o_res.stateid;
793 p->c_arg.seqid = p->o_arg.seqid;
794 nfs4_init_opendata_res(p);
804 static void nfs4_opendata_free(struct kref *kref)
806 struct nfs4_opendata *p = container_of(kref,
807 struct nfs4_opendata, kref);
809 nfs_free_seqid(p->o_arg.seqid);
810 if (p->state != NULL)
811 nfs4_put_open_state(p->state);
812 nfs4_put_state_owner(p->owner);
818 static void nfs4_opendata_put(struct nfs4_opendata *p)
821 kref_put(&p->kref, nfs4_opendata_free);
824 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
828 ret = rpc_wait_for_completion_task(task);
832 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
836 if (open_mode & O_EXCL)
838 switch (mode & (FMODE_READ|FMODE_WRITE)) {
840 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
841 && state->n_rdonly != 0;
844 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
845 && state->n_wronly != 0;
847 case FMODE_READ|FMODE_WRITE:
848 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
849 && state->n_rdwr != 0;
855 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
857 if ((delegation->type & fmode) != fmode)
859 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
861 nfs_mark_delegation_referenced(delegation);
865 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
874 case FMODE_READ|FMODE_WRITE:
877 nfs4_state_set_mode_locked(state, state->state | fmode);
880 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
882 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
883 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
884 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
887 set_bit(NFS_O_RDONLY_STATE, &state->flags);
890 set_bit(NFS_O_WRONLY_STATE, &state->flags);
892 case FMODE_READ|FMODE_WRITE:
893 set_bit(NFS_O_RDWR_STATE, &state->flags);
897 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
899 write_seqlock(&state->seqlock);
900 nfs_set_open_stateid_locked(state, stateid, fmode);
901 write_sequnlock(&state->seqlock);
904 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
907 * Protect the call to nfs4_state_set_mode_locked and
908 * serialise the stateid update
910 write_seqlock(&state->seqlock);
911 if (deleg_stateid != NULL) {
912 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
913 set_bit(NFS_DELEGATED_STATE, &state->flags);
915 if (open_stateid != NULL)
916 nfs_set_open_stateid_locked(state, open_stateid, fmode);
917 write_sequnlock(&state->seqlock);
918 spin_lock(&state->owner->so_lock);
919 update_open_stateflags(state, fmode);
920 spin_unlock(&state->owner->so_lock);
923 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
925 struct nfs_inode *nfsi = NFS_I(state->inode);
926 struct nfs_delegation *deleg_cur;
929 fmode &= (FMODE_READ|FMODE_WRITE);
932 deleg_cur = rcu_dereference(nfsi->delegation);
933 if (deleg_cur == NULL)
936 spin_lock(&deleg_cur->lock);
937 if (nfsi->delegation != deleg_cur ||
938 (deleg_cur->type & fmode) != fmode)
939 goto no_delegation_unlock;
941 if (delegation == NULL)
942 delegation = &deleg_cur->stateid;
943 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
944 goto no_delegation_unlock;
946 nfs_mark_delegation_referenced(deleg_cur);
947 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
949 no_delegation_unlock:
950 spin_unlock(&deleg_cur->lock);
954 if (!ret && open_stateid != NULL) {
955 __update_open_stateid(state, open_stateid, NULL, fmode);
963 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
965 struct nfs_delegation *delegation;
968 delegation = rcu_dereference(NFS_I(inode)->delegation);
969 if (delegation == NULL || (delegation->type & fmode) == fmode) {
974 nfs_inode_return_delegation(inode);
977 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
979 struct nfs4_state *state = opendata->state;
980 struct nfs_inode *nfsi = NFS_I(state->inode);
981 struct nfs_delegation *delegation;
982 int open_mode = opendata->o_arg.open_flags & O_EXCL;
983 fmode_t fmode = opendata->o_arg.fmode;
984 nfs4_stateid stateid;
988 if (can_open_cached(state, fmode, open_mode)) {
989 spin_lock(&state->owner->so_lock);
990 if (can_open_cached(state, fmode, open_mode)) {
991 update_open_stateflags(state, fmode);
992 spin_unlock(&state->owner->so_lock);
993 goto out_return_state;
995 spin_unlock(&state->owner->so_lock);
998 delegation = rcu_dereference(nfsi->delegation);
999 if (delegation == NULL ||
1000 !can_open_delegated(delegation, fmode)) {
1004 /* Save the delegation */
1005 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1007 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1012 /* Try to update the stateid using the delegation */
1013 if (update_open_stateid(state, NULL, &stateid, fmode))
1014 goto out_return_state;
1017 return ERR_PTR(ret);
1019 atomic_inc(&state->count);
1023 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1025 struct inode *inode;
1026 struct nfs4_state *state = NULL;
1027 struct nfs_delegation *delegation;
1030 if (!data->rpc_done) {
1031 state = nfs4_try_open_cached(data);
1036 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1038 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1039 ret = PTR_ERR(inode);
1043 state = nfs4_get_open_state(inode, data->owner);
1046 if (data->o_res.delegation_type != 0) {
1047 int delegation_flags = 0;
1050 delegation = rcu_dereference(NFS_I(inode)->delegation);
1052 delegation_flags = delegation->flags;
1054 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1055 nfs_inode_set_delegation(state->inode,
1056 data->owner->so_cred,
1059 nfs_inode_reclaim_delegation(state->inode,
1060 data->owner->so_cred,
1064 update_open_stateid(state, &data->o_res.stateid, NULL,
1072 return ERR_PTR(ret);
1075 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1077 struct nfs_inode *nfsi = NFS_I(state->inode);
1078 struct nfs_open_context *ctx;
1080 spin_lock(&state->inode->i_lock);
1081 list_for_each_entry(ctx, &nfsi->open_files, list) {
1082 if (ctx->state != state)
1084 get_nfs_open_context(ctx);
1085 spin_unlock(&state->inode->i_lock);
1088 spin_unlock(&state->inode->i_lock);
1089 return ERR_PTR(-ENOENT);
1092 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1094 struct nfs4_opendata *opendata;
1096 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
1097 if (opendata == NULL)
1098 return ERR_PTR(-ENOMEM);
1099 opendata->state = state;
1100 atomic_inc(&state->count);
1104 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1106 struct nfs4_state *newstate;
1109 opendata->o_arg.open_flags = 0;
1110 opendata->o_arg.fmode = fmode;
1111 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1112 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1113 nfs4_init_opendata_res(opendata);
1114 ret = _nfs4_recover_proc_open(opendata);
1117 newstate = nfs4_opendata_to_nfs4_state(opendata);
1118 if (IS_ERR(newstate))
1119 return PTR_ERR(newstate);
1120 nfs4_close_state(&opendata->path, newstate, fmode);
1125 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1127 struct nfs4_state *newstate;
1130 /* memory barrier prior to reading state->n_* */
1131 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1133 if (state->n_rdwr != 0) {
1134 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1135 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1138 if (newstate != state)
1141 if (state->n_wronly != 0) {
1142 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1143 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1146 if (newstate != state)
1149 if (state->n_rdonly != 0) {
1150 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1151 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1154 if (newstate != state)
1158 * We may have performed cached opens for all three recoveries.
1159 * Check if we need to update the current stateid.
1161 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1162 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1163 write_seqlock(&state->seqlock);
1164 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1165 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1166 write_sequnlock(&state->seqlock);
1173 * reclaim state on the server after a reboot.
1175 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1177 struct nfs_delegation *delegation;
1178 struct nfs4_opendata *opendata;
1179 fmode_t delegation_type = 0;
1182 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1183 if (IS_ERR(opendata))
1184 return PTR_ERR(opendata);
1185 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1186 opendata->o_arg.fh = NFS_FH(state->inode);
1188 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1189 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1190 delegation_type = delegation->type;
1192 opendata->o_arg.u.delegation_type = delegation_type;
1193 status = nfs4_open_recover(opendata, state);
1194 nfs4_opendata_put(opendata);
1198 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1200 struct nfs_server *server = NFS_SERVER(state->inode);
1201 struct nfs4_exception exception = { };
1204 err = _nfs4_do_open_reclaim(ctx, state);
1205 if (err != -NFS4ERR_DELAY)
1207 nfs4_handle_exception(server, err, &exception);
1208 } while (exception.retry);
1212 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1214 struct nfs_open_context *ctx;
1217 ctx = nfs4_state_find_open_context(state);
1219 return PTR_ERR(ctx);
1220 ret = nfs4_do_open_reclaim(ctx, state);
1221 put_nfs_open_context(ctx);
1225 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1227 struct nfs4_opendata *opendata;
1230 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1231 if (IS_ERR(opendata))
1232 return PTR_ERR(opendata);
1233 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1234 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1235 sizeof(opendata->o_arg.u.delegation.data));
1236 ret = nfs4_open_recover(opendata, state);
1237 nfs4_opendata_put(opendata);
1241 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1243 struct nfs4_exception exception = { };
1244 struct nfs_server *server = NFS_SERVER(state->inode);
1247 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1253 case -NFS4ERR_BADSESSION:
1254 case -NFS4ERR_BADSLOT:
1255 case -NFS4ERR_BAD_HIGH_SLOT:
1256 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1257 case -NFS4ERR_DEADSESSION:
1258 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
1260 case -NFS4ERR_STALE_CLIENTID:
1261 case -NFS4ERR_STALE_STATEID:
1262 case -NFS4ERR_EXPIRED:
1263 /* Don't recall a delegation if it was lost */
1264 nfs4_schedule_lease_recovery(server->nfs_client);
1268 * The show must go on: exit, but mark the
1269 * stateid as needing recovery.
1271 case -NFS4ERR_ADMIN_REVOKED:
1272 case -NFS4ERR_BAD_STATEID:
1273 nfs4_schedule_stateid_recovery(server, state);
1276 * User RPCSEC_GSS context has expired.
1277 * We cannot recover this stateid now, so
1278 * skip it and allow recovery thread to
1285 err = nfs4_handle_exception(server, err, &exception);
1286 } while (exception.retry);
1291 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1293 struct nfs4_opendata *data = calldata;
1295 data->rpc_status = task->tk_status;
1296 if (data->rpc_status == 0) {
1297 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1298 sizeof(data->o_res.stateid.data));
1299 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1300 renew_lease(data->o_res.server, data->timestamp);
1305 static void nfs4_open_confirm_release(void *calldata)
1307 struct nfs4_opendata *data = calldata;
1308 struct nfs4_state *state = NULL;
1310 /* If this request hasn't been cancelled, do nothing */
1311 if (data->cancelled == 0)
1313 /* In case of error, no cleanup! */
1314 if (!data->rpc_done)
1316 state = nfs4_opendata_to_nfs4_state(data);
1318 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1320 nfs4_opendata_put(data);
1323 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1324 .rpc_call_done = nfs4_open_confirm_done,
1325 .rpc_release = nfs4_open_confirm_release,
1329 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1331 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1333 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1334 struct rpc_task *task;
1335 struct rpc_message msg = {
1336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1337 .rpc_argp = &data->c_arg,
1338 .rpc_resp = &data->c_res,
1339 .rpc_cred = data->owner->so_cred,
1341 struct rpc_task_setup task_setup_data = {
1342 .rpc_client = server->client,
1343 .rpc_message = &msg,
1344 .callback_ops = &nfs4_open_confirm_ops,
1345 .callback_data = data,
1346 .workqueue = nfsiod_workqueue,
1347 .flags = RPC_TASK_ASYNC,
1351 kref_get(&data->kref);
1353 data->rpc_status = 0;
1354 data->timestamp = jiffies;
1355 task = rpc_run_task(&task_setup_data);
1357 return PTR_ERR(task);
1358 status = nfs4_wait_for_completion_rpc_task(task);
1360 data->cancelled = 1;
1363 status = data->rpc_status;
1368 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1370 struct nfs4_opendata *data = calldata;
1371 struct nfs4_state_owner *sp = data->owner;
1373 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1376 * Check if we still need to send an OPEN call, or if we can use
1377 * a delegation instead.
1379 if (data->state != NULL) {
1380 struct nfs_delegation *delegation;
1382 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1385 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1386 if (delegation != NULL &&
1387 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1393 /* Update sequence id. */
1394 data->o_arg.id = sp->so_owner_id.id;
1395 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1396 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1397 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1398 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1400 data->timestamp = jiffies;
1401 if (nfs4_setup_sequence(data->o_arg.server,
1402 &data->o_arg.seq_args,
1403 &data->o_res.seq_res, 1, task))
1405 rpc_call_start(task);
1408 task->tk_action = NULL;
1412 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1414 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1415 nfs4_open_prepare(task, calldata);
1418 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1420 struct nfs4_opendata *data = calldata;
1422 data->rpc_status = task->tk_status;
1424 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1427 if (task->tk_status == 0) {
1428 switch (data->o_res.f_attr->mode & S_IFMT) {
1432 data->rpc_status = -ELOOP;
1435 data->rpc_status = -EISDIR;
1438 data->rpc_status = -ENOTDIR;
1440 renew_lease(data->o_res.server, data->timestamp);
1441 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1442 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1447 static void nfs4_open_release(void *calldata)
1449 struct nfs4_opendata *data = calldata;
1450 struct nfs4_state *state = NULL;
1452 /* If this request hasn't been cancelled, do nothing */
1453 if (data->cancelled == 0)
1455 /* In case of error, no cleanup! */
1456 if (data->rpc_status != 0 || !data->rpc_done)
1458 /* In case we need an open_confirm, no cleanup! */
1459 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1461 state = nfs4_opendata_to_nfs4_state(data);
1463 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1465 nfs4_opendata_put(data);
1468 static const struct rpc_call_ops nfs4_open_ops = {
1469 .rpc_call_prepare = nfs4_open_prepare,
1470 .rpc_call_done = nfs4_open_done,
1471 .rpc_release = nfs4_open_release,
1474 static const struct rpc_call_ops nfs4_recover_open_ops = {
1475 .rpc_call_prepare = nfs4_recover_open_prepare,
1476 .rpc_call_done = nfs4_open_done,
1477 .rpc_release = nfs4_open_release,
1480 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1482 struct inode *dir = data->dir->d_inode;
1483 struct nfs_server *server = NFS_SERVER(dir);
1484 struct nfs_openargs *o_arg = &data->o_arg;
1485 struct nfs_openres *o_res = &data->o_res;
1486 struct rpc_task *task;
1487 struct rpc_message msg = {
1488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1491 .rpc_cred = data->owner->so_cred,
1493 struct rpc_task_setup task_setup_data = {
1494 .rpc_client = server->client,
1495 .rpc_message = &msg,
1496 .callback_ops = &nfs4_open_ops,
1497 .callback_data = data,
1498 .workqueue = nfsiod_workqueue,
1499 .flags = RPC_TASK_ASYNC,
1503 kref_get(&data->kref);
1505 data->rpc_status = 0;
1506 data->cancelled = 0;
1508 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1509 task = rpc_run_task(&task_setup_data);
1511 return PTR_ERR(task);
1512 status = nfs4_wait_for_completion_rpc_task(task);
1514 data->cancelled = 1;
1517 status = data->rpc_status;
1523 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1525 struct inode *dir = data->dir->d_inode;
1526 struct nfs_openres *o_res = &data->o_res;
1529 status = nfs4_run_open_task(data, 1);
1530 if (status != 0 || !data->rpc_done)
1533 nfs_refresh_inode(dir, o_res->dir_attr);
1535 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1536 status = _nfs4_proc_open_confirm(data);
1545 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1547 static int _nfs4_proc_open(struct nfs4_opendata *data)
1549 struct inode *dir = data->dir->d_inode;
1550 struct nfs_server *server = NFS_SERVER(dir);
1551 struct nfs_openargs *o_arg = &data->o_arg;
1552 struct nfs_openres *o_res = &data->o_res;
1555 status = nfs4_run_open_task(data, 0);
1556 if (status != 0 || !data->rpc_done)
1559 if (o_arg->open_flags & O_CREAT) {
1560 update_changeattr(dir, &o_res->cinfo);
1561 nfs_post_op_update_inode(dir, o_res->dir_attr);
1563 nfs_refresh_inode(dir, o_res->dir_attr);
1564 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1565 server->caps &= ~NFS_CAP_POSIX_LOCK;
1566 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1567 status = _nfs4_proc_open_confirm(data);
1571 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1572 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1576 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1581 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1582 ret = nfs4_wait_clnt_recover(clp);
1585 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1586 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1588 nfs4_schedule_state_manager(clp);
1594 static int nfs4_recover_expired_lease(struct nfs_server *server)
1596 return nfs4_client_recover_expired_lease(server->nfs_client);
1601 * reclaim state on the server after a network partition.
1602 * Assumes caller holds the appropriate lock
1604 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1606 struct nfs4_opendata *opendata;
1609 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1610 if (IS_ERR(opendata))
1611 return PTR_ERR(opendata);
1612 ret = nfs4_open_recover(opendata, state);
1614 d_drop(ctx->path.dentry);
1615 nfs4_opendata_put(opendata);
1619 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1621 struct nfs_server *server = NFS_SERVER(state->inode);
1622 struct nfs4_exception exception = { };
1626 err = _nfs4_open_expired(ctx, state);
1630 case -NFS4ERR_GRACE:
1631 case -NFS4ERR_DELAY:
1632 nfs4_handle_exception(server, err, &exception);
1635 } while (exception.retry);
1640 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1642 struct nfs_open_context *ctx;
1645 ctx = nfs4_state_find_open_context(state);
1647 return PTR_ERR(ctx);
1648 ret = nfs4_do_open_expired(ctx, state);
1649 put_nfs_open_context(ctx);
1654 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1655 * fields corresponding to attributes that were used to store the verifier.
1656 * Make sure we clobber those fields in the later setattr call
1658 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1660 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1661 !(sattr->ia_valid & ATTR_ATIME_SET))
1662 sattr->ia_valid |= ATTR_ATIME;
1664 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1665 !(sattr->ia_valid & ATTR_MTIME_SET))
1666 sattr->ia_valid |= ATTR_MTIME;
1670 * Returns a referenced nfs4_state
1672 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)
1674 struct nfs4_state_owner *sp;
1675 struct nfs4_state *state = NULL;
1676 struct nfs_server *server = NFS_SERVER(dir);
1677 struct nfs4_opendata *opendata;
1680 /* Protect against reboot recovery conflicts */
1682 if (!(sp = nfs4_get_state_owner(server, cred))) {
1683 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1686 status = nfs4_recover_expired_lease(server);
1688 goto err_put_state_owner;
1689 if (path->dentry->d_inode != NULL)
1690 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1692 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
1693 if (opendata == NULL)
1694 goto err_put_state_owner;
1696 if (path->dentry->d_inode != NULL)
1697 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1699 status = _nfs4_proc_open(opendata);
1701 goto err_opendata_put;
1703 state = nfs4_opendata_to_nfs4_state(opendata);
1704 status = PTR_ERR(state);
1706 goto err_opendata_put;
1707 if (server->caps & NFS_CAP_POSIX_LOCK)
1708 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1710 if (opendata->o_arg.open_flags & O_EXCL) {
1711 nfs4_exclusive_attrset(opendata, sattr);
1713 nfs_fattr_init(opendata->o_res.f_attr);
1714 status = nfs4_do_setattr(state->inode, cred,
1715 opendata->o_res.f_attr, sattr,
1718 nfs_setattr_update_inode(state->inode, sattr);
1719 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1721 nfs4_opendata_put(opendata);
1722 nfs4_put_state_owner(sp);
1726 nfs4_opendata_put(opendata);
1727 err_put_state_owner:
1728 nfs4_put_state_owner(sp);
1735 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)
1737 struct nfs4_exception exception = { };
1738 struct nfs4_state *res;
1742 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1745 /* NOTE: BAD_SEQID means the server and client disagree about the
1746 * book-keeping w.r.t. state-changing operations
1747 * (OPEN/CLOSE/LOCK/LOCKU...)
1748 * It is actually a sign of a bug on the client or on the server.
1750 * If we receive a BAD_SEQID error in the particular case of
1751 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1752 * have unhashed the old state_owner for us, and that we can
1753 * therefore safely retry using a new one. We should still warn
1754 * the user though...
1756 if (status == -NFS4ERR_BAD_SEQID) {
1757 printk(KERN_WARNING "NFS: v4 server %s "
1758 " returned a bad sequence-id error!\n",
1759 NFS_SERVER(dir)->nfs_client->cl_hostname);
1760 exception.retry = 1;
1764 * BAD_STATEID on OPEN means that the server cancelled our
1765 * state before it received the OPEN_CONFIRM.
1766 * Recover by retrying the request as per the discussion
1767 * on Page 181 of RFC3530.
1769 if (status == -NFS4ERR_BAD_STATEID) {
1770 exception.retry = 1;
1773 if (status == -EAGAIN) {
1774 /* We must have found a delegation */
1775 exception.retry = 1;
1778 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1779 status, &exception));
1780 } while (exception.retry);
1784 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1785 struct nfs_fattr *fattr, struct iattr *sattr,
1786 struct nfs4_state *state)
1788 struct nfs_server *server = NFS_SERVER(inode);
1789 struct nfs_setattrargs arg = {
1790 .fh = NFS_FH(inode),
1793 .bitmask = server->attr_bitmask,
1795 struct nfs_setattrres res = {
1799 struct rpc_message msg = {
1800 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1805 unsigned long timestamp = jiffies;
1808 nfs_fattr_init(fattr);
1810 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1811 /* Use that stateid */
1812 } else if (state != NULL) {
1813 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
1815 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1817 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1818 if (status == 0 && state != NULL)
1819 renew_lease(server, timestamp);
1823 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1824 struct nfs_fattr *fattr, struct iattr *sattr,
1825 struct nfs4_state *state)
1827 struct nfs_server *server = NFS_SERVER(inode);
1828 struct nfs4_exception exception = { };
1831 err = nfs4_handle_exception(server,
1832 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1834 } while (exception.retry);
1838 struct nfs4_closedata {
1840 struct inode *inode;
1841 struct nfs4_state *state;
1842 struct nfs_closeargs arg;
1843 struct nfs_closeres res;
1844 struct nfs_fattr fattr;
1845 unsigned long timestamp;
1850 static void nfs4_free_closedata(void *data)
1852 struct nfs4_closedata *calldata = data;
1853 struct nfs4_state_owner *sp = calldata->state->owner;
1856 pnfs_roc_release(calldata->state->inode);
1857 nfs4_put_open_state(calldata->state);
1858 nfs_free_seqid(calldata->arg.seqid);
1859 nfs4_put_state_owner(sp);
1860 path_put(&calldata->path);
1864 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1867 spin_lock(&state->owner->so_lock);
1868 if (!(fmode & FMODE_READ))
1869 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1870 if (!(fmode & FMODE_WRITE))
1871 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1872 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1873 spin_unlock(&state->owner->so_lock);
1876 static void nfs4_close_done(struct rpc_task *task, void *data)
1878 struct nfs4_closedata *calldata = data;
1879 struct nfs4_state *state = calldata->state;
1880 struct nfs_server *server = NFS_SERVER(calldata->inode);
1882 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1884 /* hmm. we are done with the inode, and in the process of freeing
1885 * the state_owner. we keep this around to process errors
1887 switch (task->tk_status) {
1890 pnfs_roc_set_barrier(state->inode,
1891 calldata->roc_barrier);
1892 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1893 renew_lease(server, calldata->timestamp);
1894 nfs4_close_clear_stateid_flags(state,
1895 calldata->arg.fmode);
1897 case -NFS4ERR_STALE_STATEID:
1898 case -NFS4ERR_OLD_STATEID:
1899 case -NFS4ERR_BAD_STATEID:
1900 case -NFS4ERR_EXPIRED:
1901 if (calldata->arg.fmode == 0)
1904 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1905 rpc_restart_call_prepare(task);
1907 nfs_release_seqid(calldata->arg.seqid);
1908 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1911 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1913 struct nfs4_closedata *calldata = data;
1914 struct nfs4_state *state = calldata->state;
1917 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1920 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1921 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
1922 spin_lock(&state->owner->so_lock);
1923 /* Calculate the change in open mode */
1924 if (state->n_rdwr == 0) {
1925 if (state->n_rdonly == 0) {
1926 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1927 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1928 calldata->arg.fmode &= ~FMODE_READ;
1930 if (state->n_wronly == 0) {
1931 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1932 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1933 calldata->arg.fmode &= ~FMODE_WRITE;
1936 spin_unlock(&state->owner->so_lock);
1939 /* Note: exit _without_ calling nfs4_close_done */
1940 task->tk_action = NULL;
1944 if (calldata->arg.fmode == 0) {
1945 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1946 if (calldata->roc &&
1947 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
1948 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
1954 nfs_fattr_init(calldata->res.fattr);
1955 calldata->timestamp = jiffies;
1956 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
1957 &calldata->arg.seq_args, &calldata->res.seq_res,
1960 rpc_call_start(task);
1963 static const struct rpc_call_ops nfs4_close_ops = {
1964 .rpc_call_prepare = nfs4_close_prepare,
1965 .rpc_call_done = nfs4_close_done,
1966 .rpc_release = nfs4_free_closedata,
1970 * It is possible for data to be read/written from a mem-mapped file
1971 * after the sys_close call (which hits the vfs layer as a flush).
1972 * This means that we can't safely call nfsv4 close on a file until
1973 * the inode is cleared. This in turn means that we are not good
1974 * NFSv4 citizens - we do not indicate to the server to update the file's
1975 * share state even when we are done with one of the three share
1976 * stateid's in the inode.
1978 * NOTE: Caller must be holding the sp->so_owner semaphore!
1980 int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
1982 struct nfs_server *server = NFS_SERVER(state->inode);
1983 struct nfs4_closedata *calldata;
1984 struct nfs4_state_owner *sp = state->owner;
1985 struct rpc_task *task;
1986 struct rpc_message msg = {
1987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1988 .rpc_cred = state->owner->so_cred,
1990 struct rpc_task_setup task_setup_data = {
1991 .rpc_client = server->client,
1992 .rpc_message = &msg,
1993 .callback_ops = &nfs4_close_ops,
1994 .workqueue = nfsiod_workqueue,
1995 .flags = RPC_TASK_ASYNC,
1997 int status = -ENOMEM;
1999 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2000 if (calldata == NULL)
2002 calldata->inode = state->inode;
2003 calldata->state = state;
2004 calldata->arg.fh = NFS_FH(state->inode);
2005 calldata->arg.stateid = &state->open_stateid;
2006 /* Serialization for the sequence id */
2007 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2008 if (calldata->arg.seqid == NULL)
2009 goto out_free_calldata;
2010 calldata->arg.fmode = 0;
2011 calldata->arg.bitmask = server->cache_consistency_bitmask;
2012 calldata->res.fattr = &calldata->fattr;
2013 calldata->res.seqid = calldata->arg.seqid;
2014 calldata->res.server = server;
2015 calldata->roc = roc;
2017 calldata->path = *path;
2019 msg.rpc_argp = &calldata->arg;
2020 msg.rpc_resp = &calldata->res;
2021 task_setup_data.callback_data = calldata;
2022 task = rpc_run_task(&task_setup_data);
2024 return PTR_ERR(task);
2027 status = rpc_wait_for_completion_task(task);
2034 pnfs_roc_release(state->inode);
2035 nfs4_put_open_state(state);
2036 nfs4_put_state_owner(sp);
2040 static struct inode *
2041 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2043 struct nfs4_state *state;
2045 /* Protect against concurrent sillydeletes */
2046 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
2048 return ERR_CAST(state);
2050 return igrab(state->inode);
2053 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2055 if (ctx->state == NULL)
2058 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2060 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2063 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2065 struct nfs4_server_caps_arg args = {
2068 struct nfs4_server_caps_res res = {};
2069 struct rpc_message msg = {
2070 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2076 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2078 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2079 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2080 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2081 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2082 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2083 NFS_CAP_CTIME|NFS_CAP_MTIME);
2084 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2085 server->caps |= NFS_CAP_ACLS;
2086 if (res.has_links != 0)
2087 server->caps |= NFS_CAP_HARDLINKS;
2088 if (res.has_symlinks != 0)
2089 server->caps |= NFS_CAP_SYMLINKS;
2090 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2091 server->caps |= NFS_CAP_FILEID;
2092 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2093 server->caps |= NFS_CAP_MODE;
2094 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2095 server->caps |= NFS_CAP_NLINK;
2096 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2097 server->caps |= NFS_CAP_OWNER;
2098 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2099 server->caps |= NFS_CAP_OWNER_GROUP;
2100 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2101 server->caps |= NFS_CAP_ATIME;
2102 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2103 server->caps |= NFS_CAP_CTIME;
2104 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2105 server->caps |= NFS_CAP_MTIME;
2107 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2108 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2109 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2110 server->acl_bitmask = res.acl_bitmask;
2116 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2118 struct nfs4_exception exception = { };
2121 err = nfs4_handle_exception(server,
2122 _nfs4_server_capabilities(server, fhandle),
2124 } while (exception.retry);
2128 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2129 struct nfs_fsinfo *info)
2131 struct nfs4_lookup_root_arg args = {
2132 .bitmask = nfs4_fattr_bitmap,
2134 struct nfs4_lookup_res res = {
2136 .fattr = info->fattr,
2139 struct rpc_message msg = {
2140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2145 nfs_fattr_init(info->fattr);
2146 return nfs4_call_sync(server, &msg, &args, &res, 0);
2149 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2150 struct nfs_fsinfo *info)
2152 struct nfs4_exception exception = { };
2155 err = nfs4_handle_exception(server,
2156 _nfs4_lookup_root(server, fhandle, info),
2158 } while (exception.retry);
2163 * get the file handle for the "/" directory on the server
2165 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2166 struct nfs_fsinfo *info)
2170 status = nfs4_lookup_root(server, fhandle, info);
2172 status = nfs4_server_capabilities(server, fhandle);
2174 status = nfs4_do_fsinfo(server, fhandle, info);
2175 return nfs4_map_errors(status);
2179 * Get locations and (maybe) other attributes of a referral.
2180 * Note that we'll actually follow the referral later when
2181 * we detect fsid mismatch in inode revalidation
2183 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2185 int status = -ENOMEM;
2186 struct page *page = NULL;
2187 struct nfs4_fs_locations *locations = NULL;
2189 page = alloc_page(GFP_KERNEL);
2192 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2193 if (locations == NULL)
2196 status = nfs4_proc_fs_locations(dir, name, locations, page);
2199 /* Make sure server returned a different fsid for the referral */
2200 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2201 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
2206 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2207 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2209 fattr->mode = S_IFDIR;
2210 memset(fhandle, 0, sizeof(struct nfs_fh));
2218 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2220 struct nfs4_getattr_arg args = {
2222 .bitmask = server->attr_bitmask,
2224 struct nfs4_getattr_res res = {
2228 struct rpc_message msg = {
2229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2234 nfs_fattr_init(fattr);
2235 return nfs4_call_sync(server, &msg, &args, &res, 0);
2238 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2240 struct nfs4_exception exception = { };
2243 err = nfs4_handle_exception(server,
2244 _nfs4_proc_getattr(server, fhandle, fattr),
2246 } while (exception.retry);
2251 * The file is not closed if it is opened due to the a request to change
2252 * the size of the file. The open call will not be needed once the
2253 * VFS layer lookup-intents are implemented.
2255 * Close is called when the inode is destroyed.
2256 * If we haven't opened the file for O_WRONLY, we
2257 * need to in the size_change case to obtain a stateid.
2260 * Because OPEN is always done by name in nfsv4, it is
2261 * possible that we opened a different file by the same
2262 * name. We can recognize this race condition, but we
2263 * can't do anything about it besides returning an error.
2265 * This will be fixed with VFS changes (lookup-intent).
2268 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2269 struct iattr *sattr)
2271 struct inode *inode = dentry->d_inode;
2272 struct rpc_cred *cred = NULL;
2273 struct nfs4_state *state = NULL;
2276 nfs_fattr_init(fattr);
2278 /* Search for an existing open(O_WRITE) file */
2279 if (sattr->ia_valid & ATTR_FILE) {
2280 struct nfs_open_context *ctx;
2282 ctx = nfs_file_open_context(sattr->ia_file);
2289 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2291 nfs_setattr_update_inode(inode, sattr);
2295 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2296 const struct qstr *name, struct nfs_fh *fhandle,
2297 struct nfs_fattr *fattr)
2300 struct nfs4_lookup_arg args = {
2301 .bitmask = server->attr_bitmask,
2305 struct nfs4_lookup_res res = {
2310 struct rpc_message msg = {
2311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2316 nfs_fattr_init(fattr);
2318 dprintk("NFS call lookupfh %s\n", name->name);
2319 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2320 dprintk("NFS reply lookupfh: %d\n", status);
2324 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2325 struct qstr *name, struct nfs_fh *fhandle,
2326 struct nfs_fattr *fattr)
2328 struct nfs4_exception exception = { };
2331 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2333 if (err == -NFS4ERR_MOVED) {
2337 err = nfs4_handle_exception(server, err, &exception);
2338 } while (exception.retry);
2342 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2343 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2347 dprintk("NFS call lookup %s\n", name->name);
2348 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2349 if (status == -NFS4ERR_MOVED)
2350 status = nfs4_get_referral(dir, name, fattr, fhandle);
2351 dprintk("NFS reply lookup: %d\n", status);
2355 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2357 struct nfs4_exception exception = { };
2360 err = nfs4_handle_exception(NFS_SERVER(dir),
2361 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2363 } while (exception.retry);
2367 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2369 struct nfs_server *server = NFS_SERVER(inode);
2370 struct nfs4_accessargs args = {
2371 .fh = NFS_FH(inode),
2372 .bitmask = server->attr_bitmask,
2374 struct nfs4_accessres res = {
2377 struct rpc_message msg = {
2378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2381 .rpc_cred = entry->cred,
2383 int mode = entry->mask;
2387 * Determine which access bits we want to ask for...
2389 if (mode & MAY_READ)
2390 args.access |= NFS4_ACCESS_READ;
2391 if (S_ISDIR(inode->i_mode)) {
2392 if (mode & MAY_WRITE)
2393 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2394 if (mode & MAY_EXEC)
2395 args.access |= NFS4_ACCESS_LOOKUP;
2397 if (mode & MAY_WRITE)
2398 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2399 if (mode & MAY_EXEC)
2400 args.access |= NFS4_ACCESS_EXECUTE;
2403 res.fattr = nfs_alloc_fattr();
2404 if (res.fattr == NULL)
2407 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2410 if (res.access & NFS4_ACCESS_READ)
2411 entry->mask |= MAY_READ;
2412 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2413 entry->mask |= MAY_WRITE;
2414 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2415 entry->mask |= MAY_EXEC;
2416 nfs_refresh_inode(inode, res.fattr);
2418 nfs_free_fattr(res.fattr);
2422 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2424 struct nfs4_exception exception = { };
2427 err = nfs4_handle_exception(NFS_SERVER(inode),
2428 _nfs4_proc_access(inode, entry),
2430 } while (exception.retry);
2435 * TODO: For the time being, we don't try to get any attributes
2436 * along with any of the zero-copy operations READ, READDIR,
2439 * In the case of the first three, we want to put the GETATTR
2440 * after the read-type operation -- this is because it is hard
2441 * to predict the length of a GETATTR response in v4, and thus
2442 * align the READ data correctly. This means that the GETATTR
2443 * may end up partially falling into the page cache, and we should
2444 * shift it into the 'tail' of the xdr_buf before processing.
2445 * To do this efficiently, we need to know the total length
2446 * of data received, which doesn't seem to be available outside
2449 * In the case of WRITE, we also want to put the GETATTR after
2450 * the operation -- in this case because we want to make sure
2451 * we get the post-operation mtime and size. This means that
2452 * we can't use xdr_encode_pages() as written: we need a variant
2453 * of it which would leave room in the 'tail' iovec.
2455 * Both of these changes to the XDR layer would in fact be quite
2456 * minor, but I decided to leave them for a subsequent patch.
2458 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2459 unsigned int pgbase, unsigned int pglen)
2461 struct nfs4_readlink args = {
2462 .fh = NFS_FH(inode),
2467 struct nfs4_readlink_res res;
2468 struct rpc_message msg = {
2469 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2474 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2477 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2478 unsigned int pgbase, unsigned int pglen)
2480 struct nfs4_exception exception = { };
2483 err = nfs4_handle_exception(NFS_SERVER(inode),
2484 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2486 } while (exception.retry);
2492 * We will need to arrange for the VFS layer to provide an atomic open.
2493 * Until then, this create/open method is prone to inefficiency and race
2494 * conditions due to the lookup, create, and open VFS calls from sys_open()
2495 * placed on the wire.
2497 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2498 * The file will be opened again in the subsequent VFS open call
2499 * (nfs4_proc_file_open).
2501 * The open for read will just hang around to be used by any process that
2502 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2506 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2507 int flags, struct nfs_open_context *ctx)
2509 struct path my_path = {
2512 struct path *path = &my_path;
2513 struct nfs4_state *state;
2514 struct rpc_cred *cred = NULL;
2523 sattr->ia_mode &= ~current_umask();
2524 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
2526 if (IS_ERR(state)) {
2527 status = PTR_ERR(state);
2530 d_add(dentry, igrab(state->inode));
2531 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2535 nfs4_close_sync(path, state, fmode);
2540 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2542 struct nfs_server *server = NFS_SERVER(dir);
2543 struct nfs_removeargs args = {
2545 .name.len = name->len,
2546 .name.name = name->name,
2547 .bitmask = server->attr_bitmask,
2549 struct nfs_removeres res = {
2552 struct rpc_message msg = {
2553 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2557 int status = -ENOMEM;
2559 res.dir_attr = nfs_alloc_fattr();
2560 if (res.dir_attr == NULL)
2563 status = nfs4_call_sync(server, &msg, &args, &res, 1);
2565 update_changeattr(dir, &res.cinfo);
2566 nfs_post_op_update_inode(dir, res.dir_attr);
2568 nfs_free_fattr(res.dir_attr);
2573 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2575 struct nfs4_exception exception = { };
2578 err = nfs4_handle_exception(NFS_SERVER(dir),
2579 _nfs4_proc_remove(dir, name),
2581 } while (exception.retry);
2585 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2587 struct nfs_server *server = NFS_SERVER(dir);
2588 struct nfs_removeargs *args = msg->rpc_argp;
2589 struct nfs_removeres *res = msg->rpc_resp;
2591 args->bitmask = server->cache_consistency_bitmask;
2592 res->server = server;
2593 res->seq_res.sr_slot = NULL;
2594 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2597 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2599 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2601 if (!nfs4_sequence_done(task, &res->seq_res))
2603 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2605 update_changeattr(dir, &res->cinfo);
2606 nfs_post_op_update_inode(dir, res->dir_attr);
2610 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2612 struct nfs_server *server = NFS_SERVER(dir);
2613 struct nfs_renameargs *arg = msg->rpc_argp;
2614 struct nfs_renameres *res = msg->rpc_resp;
2616 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2617 arg->bitmask = server->attr_bitmask;
2618 res->server = server;
2621 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2622 struct inode *new_dir)
2624 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2626 if (!nfs4_sequence_done(task, &res->seq_res))
2628 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2631 update_changeattr(old_dir, &res->old_cinfo);
2632 nfs_post_op_update_inode(old_dir, res->old_fattr);
2633 update_changeattr(new_dir, &res->new_cinfo);
2634 nfs_post_op_update_inode(new_dir, res->new_fattr);
2638 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2639 struct inode *new_dir, struct qstr *new_name)
2641 struct nfs_server *server = NFS_SERVER(old_dir);
2642 struct nfs_renameargs arg = {
2643 .old_dir = NFS_FH(old_dir),
2644 .new_dir = NFS_FH(new_dir),
2645 .old_name = old_name,
2646 .new_name = new_name,
2647 .bitmask = server->attr_bitmask,
2649 struct nfs_renameres res = {
2652 struct rpc_message msg = {
2653 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2657 int status = -ENOMEM;
2659 res.old_fattr = nfs_alloc_fattr();
2660 res.new_fattr = nfs_alloc_fattr();
2661 if (res.old_fattr == NULL || res.new_fattr == NULL)
2664 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2666 update_changeattr(old_dir, &res.old_cinfo);
2667 nfs_post_op_update_inode(old_dir, res.old_fattr);
2668 update_changeattr(new_dir, &res.new_cinfo);
2669 nfs_post_op_update_inode(new_dir, res.new_fattr);
2672 nfs_free_fattr(res.new_fattr);
2673 nfs_free_fattr(res.old_fattr);
2677 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2678 struct inode *new_dir, struct qstr *new_name)
2680 struct nfs4_exception exception = { };
2683 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2684 _nfs4_proc_rename(old_dir, old_name,
2687 } while (exception.retry);
2691 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2693 struct nfs_server *server = NFS_SERVER(inode);
2694 struct nfs4_link_arg arg = {
2695 .fh = NFS_FH(inode),
2696 .dir_fh = NFS_FH(dir),
2698 .bitmask = server->attr_bitmask,
2700 struct nfs4_link_res res = {
2703 struct rpc_message msg = {
2704 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2708 int status = -ENOMEM;
2710 res.fattr = nfs_alloc_fattr();
2711 res.dir_attr = nfs_alloc_fattr();
2712 if (res.fattr == NULL || res.dir_attr == NULL)
2715 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2717 update_changeattr(dir, &res.cinfo);
2718 nfs_post_op_update_inode(dir, res.dir_attr);
2719 nfs_post_op_update_inode(inode, res.fattr);
2722 nfs_free_fattr(res.dir_attr);
2723 nfs_free_fattr(res.fattr);
2727 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2729 struct nfs4_exception exception = { };
2732 err = nfs4_handle_exception(NFS_SERVER(inode),
2733 _nfs4_proc_link(inode, dir, name),
2735 } while (exception.retry);
2739 struct nfs4_createdata {
2740 struct rpc_message msg;
2741 struct nfs4_create_arg arg;
2742 struct nfs4_create_res res;
2744 struct nfs_fattr fattr;
2745 struct nfs_fattr dir_fattr;
2748 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2749 struct qstr *name, struct iattr *sattr, u32 ftype)
2751 struct nfs4_createdata *data;
2753 data = kzalloc(sizeof(*data), GFP_KERNEL);
2755 struct nfs_server *server = NFS_SERVER(dir);
2757 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2758 data->msg.rpc_argp = &data->arg;
2759 data->msg.rpc_resp = &data->res;
2760 data->arg.dir_fh = NFS_FH(dir);
2761 data->arg.server = server;
2762 data->arg.name = name;
2763 data->arg.attrs = sattr;
2764 data->arg.ftype = ftype;
2765 data->arg.bitmask = server->attr_bitmask;
2766 data->res.server = server;
2767 data->res.fh = &data->fh;
2768 data->res.fattr = &data->fattr;
2769 data->res.dir_fattr = &data->dir_fattr;
2770 nfs_fattr_init(data->res.fattr);
2771 nfs_fattr_init(data->res.dir_fattr);
2776 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2778 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2779 &data->arg, &data->res, 1);
2781 update_changeattr(dir, &data->res.dir_cinfo);
2782 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2783 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2788 static void nfs4_free_createdata(struct nfs4_createdata *data)
2793 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2794 struct page *page, unsigned int len, struct iattr *sattr)
2796 struct nfs4_createdata *data;
2797 int status = -ENAMETOOLONG;
2799 if (len > NFS4_MAXPATHLEN)
2803 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2807 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2808 data->arg.u.symlink.pages = &page;
2809 data->arg.u.symlink.len = len;
2811 status = nfs4_do_create(dir, dentry, data);
2813 nfs4_free_createdata(data);
2818 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2819 struct page *page, unsigned int len, struct iattr *sattr)
2821 struct nfs4_exception exception = { };
2824 err = nfs4_handle_exception(NFS_SERVER(dir),
2825 _nfs4_proc_symlink(dir, dentry, page,
2828 } while (exception.retry);
2832 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2833 struct iattr *sattr)
2835 struct nfs4_createdata *data;
2836 int status = -ENOMEM;
2838 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2842 status = nfs4_do_create(dir, dentry, data);
2844 nfs4_free_createdata(data);
2849 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2850 struct iattr *sattr)
2852 struct nfs4_exception exception = { };
2855 sattr->ia_mode &= ~current_umask();
2857 err = nfs4_handle_exception(NFS_SERVER(dir),
2858 _nfs4_proc_mkdir(dir, dentry, sattr),
2860 } while (exception.retry);
2864 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2865 u64 cookie, struct page **pages, unsigned int count, int plus)
2867 struct inode *dir = dentry->d_inode;
2868 struct nfs4_readdir_arg args = {
2873 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2876 struct nfs4_readdir_res res;
2877 struct rpc_message msg = {
2878 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2885 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2886 dentry->d_parent->d_name.name,
2887 dentry->d_name.name,
2888 (unsigned long long)cookie);
2889 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2890 res.pgbase = args.pgbase;
2891 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2893 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2894 status += args.pgbase;
2897 nfs_invalidate_atime(dir);
2899 dprintk("%s: returns %d\n", __func__, status);
2903 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2904 u64 cookie, struct page **pages, unsigned int count, int plus)
2906 struct nfs4_exception exception = { };
2909 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2910 _nfs4_proc_readdir(dentry, cred, cookie,
2911 pages, count, plus),
2913 } while (exception.retry);
2917 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2918 struct iattr *sattr, dev_t rdev)
2920 struct nfs4_createdata *data;
2921 int mode = sattr->ia_mode;
2922 int status = -ENOMEM;
2924 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2925 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2927 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2932 data->arg.ftype = NF4FIFO;
2933 else if (S_ISBLK(mode)) {
2934 data->arg.ftype = NF4BLK;
2935 data->arg.u.device.specdata1 = MAJOR(rdev);
2936 data->arg.u.device.specdata2 = MINOR(rdev);
2938 else if (S_ISCHR(mode)) {
2939 data->arg.ftype = NF4CHR;
2940 data->arg.u.device.specdata1 = MAJOR(rdev);
2941 data->arg.u.device.specdata2 = MINOR(rdev);
2944 status = nfs4_do_create(dir, dentry, data);
2946 nfs4_free_createdata(data);
2951 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2952 struct iattr *sattr, dev_t rdev)
2954 struct nfs4_exception exception = { };
2957 sattr->ia_mode &= ~current_umask();
2959 err = nfs4_handle_exception(NFS_SERVER(dir),
2960 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2962 } while (exception.retry);
2966 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2967 struct nfs_fsstat *fsstat)
2969 struct nfs4_statfs_arg args = {
2971 .bitmask = server->attr_bitmask,
2973 struct nfs4_statfs_res res = {
2976 struct rpc_message msg = {
2977 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2982 nfs_fattr_init(fsstat->fattr);
2983 return nfs4_call_sync(server, &msg, &args, &res, 0);
2986 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2988 struct nfs4_exception exception = { };
2991 err = nfs4_handle_exception(server,
2992 _nfs4_proc_statfs(server, fhandle, fsstat),
2994 } while (exception.retry);
2998 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2999 struct nfs_fsinfo *fsinfo)
3001 struct nfs4_fsinfo_arg args = {
3003 .bitmask = server->attr_bitmask,
3005 struct nfs4_fsinfo_res res = {
3008 struct rpc_message msg = {
3009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3014 return nfs4_call_sync(server, &msg, &args, &res, 0);
3017 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3019 struct nfs4_exception exception = { };
3023 err = nfs4_handle_exception(server,
3024 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3026 } while (exception.retry);
3030 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3032 nfs_fattr_init(fsinfo->fattr);
3033 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3036 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3037 struct nfs_pathconf *pathconf)
3039 struct nfs4_pathconf_arg args = {
3041 .bitmask = server->attr_bitmask,
3043 struct nfs4_pathconf_res res = {
3044 .pathconf = pathconf,
3046 struct rpc_message msg = {
3047 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3052 /* None of the pathconf attributes are mandatory to implement */
3053 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3054 memset(pathconf, 0, sizeof(*pathconf));
3058 nfs_fattr_init(pathconf->fattr);
3059 return nfs4_call_sync(server, &msg, &args, &res, 0);
3062 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3063 struct nfs_pathconf *pathconf)
3065 struct nfs4_exception exception = { };
3069 err = nfs4_handle_exception(server,
3070 _nfs4_proc_pathconf(server, fhandle, pathconf),
3072 } while (exception.retry);
3076 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3078 struct nfs_server *server = NFS_SERVER(data->inode);
3080 dprintk("--> %s\n", __func__);
3082 if (!nfs4_sequence_done(task, &data->res.seq_res))
3085 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3086 nfs_restart_rpc(task, server->nfs_client);
3090 nfs_invalidate_atime(data->inode);
3091 if (task->tk_status > 0)
3092 renew_lease(server, data->timestamp);
3096 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3098 data->timestamp = jiffies;
3099 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3102 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3104 struct inode *inode = data->inode;
3106 if (!nfs4_sequence_done(task, &data->res.seq_res))
3109 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3110 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3113 if (task->tk_status >= 0) {
3114 renew_lease(NFS_SERVER(inode), data->timestamp);
3115 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3120 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3122 struct nfs_server *server = NFS_SERVER(data->inode);
3124 data->args.bitmask = server->cache_consistency_bitmask;
3125 data->res.server = server;
3126 data->timestamp = jiffies;
3128 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3131 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3133 struct inode *inode = data->inode;
3135 if (!nfs4_sequence_done(task, &data->res.seq_res))
3138 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3139 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3142 nfs_refresh_inode(inode, data->res.fattr);
3146 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3148 struct nfs_server *server = NFS_SERVER(data->inode);
3150 data->args.bitmask = server->cache_consistency_bitmask;
3151 data->res.server = server;
3152 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3155 struct nfs4_renewdata {
3156 struct nfs_client *client;
3157 unsigned long timestamp;
3161 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3162 * standalone procedure for queueing an asynchronous RENEW.
3164 static void nfs4_renew_release(void *calldata)
3166 struct nfs4_renewdata *data = calldata;
3167 struct nfs_client *clp = data->client;
3169 if (atomic_read(&clp->cl_count) > 1)
3170 nfs4_schedule_state_renewal(clp);
3171 nfs_put_client(clp);
3175 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3177 struct nfs4_renewdata *data = calldata;
3178 struct nfs_client *clp = data->client;
3179 unsigned long timestamp = data->timestamp;
3181 if (task->tk_status < 0) {
3182 /* Unless we're shutting down, schedule state recovery! */
3183 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3184 nfs4_schedule_lease_recovery(clp);
3187 do_renew_lease(clp, timestamp);
3190 static const struct rpc_call_ops nfs4_renew_ops = {
3191 .rpc_call_done = nfs4_renew_done,
3192 .rpc_release = nfs4_renew_release,
3195 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3197 struct rpc_message msg = {
3198 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3202 struct nfs4_renewdata *data;
3204 if (!atomic_inc_not_zero(&clp->cl_count))
3206 data = kmalloc(sizeof(*data), GFP_KERNEL);
3210 data->timestamp = jiffies;
3211 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3212 &nfs4_renew_ops, data);
3215 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3217 struct rpc_message msg = {
3218 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3222 unsigned long now = jiffies;
3225 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3228 do_renew_lease(clp, now);
3232 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3234 return (server->caps & NFS_CAP_ACLS)
3235 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3236 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3239 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3240 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3243 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3245 static void buf_to_pages(const void *buf, size_t buflen,
3246 struct page **pages, unsigned int *pgbase)
3248 const void *p = buf;
3250 *pgbase = offset_in_page(buf);
3252 while (p < buf + buflen) {
3253 *(pages++) = virt_to_page(p);
3254 p += PAGE_CACHE_SIZE;
3258 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3259 struct page **pages, unsigned int *pgbase)
3261 struct page *newpage, **spages;
3267 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3268 newpage = alloc_page(GFP_KERNEL);
3270 if (newpage == NULL)
3272 memcpy(page_address(newpage), buf, len);
3277 } while (buflen != 0);
3283 __free_page(spages[rc-1]);
3287 struct nfs4_cached_acl {
3293 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3295 struct nfs_inode *nfsi = NFS_I(inode);
3297 spin_lock(&inode->i_lock);
3298 kfree(nfsi->nfs4_acl);
3299 nfsi->nfs4_acl = acl;
3300 spin_unlock(&inode->i_lock);
3303 static void nfs4_zap_acl_attr(struct inode *inode)
3305 nfs4_set_cached_acl(inode, NULL);
3308 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3310 struct nfs_inode *nfsi = NFS_I(inode);
3311 struct nfs4_cached_acl *acl;
3314 spin_lock(&inode->i_lock);
3315 acl = nfsi->nfs4_acl;
3318 if (buf == NULL) /* user is just asking for length */
3320 if (acl->cached == 0)
3322 ret = -ERANGE; /* see getxattr(2) man page */
3323 if (acl->len > buflen)
3325 memcpy(buf, acl->data, acl->len);
3329 spin_unlock(&inode->i_lock);
3333 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3335 struct nfs4_cached_acl *acl;
3337 if (buf && acl_len <= PAGE_SIZE) {
3338 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3342 memcpy(acl->data, buf, acl_len);
3344 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3351 nfs4_set_cached_acl(inode, acl);
3354 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3356 struct page *pages[NFS4ACL_MAXPAGES];
3357 struct nfs_getaclargs args = {
3358 .fh = NFS_FH(inode),
3362 struct nfs_getaclres res = {
3366 struct rpc_message msg = {
3367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3371 struct page *localpage = NULL;
3374 if (buflen < PAGE_SIZE) {
3375 /* As long as we're doing a round trip to the server anyway,
3376 * let's be prepared for a page of acl data. */
3377 localpage = alloc_page(GFP_KERNEL);
3378 resp_buf = page_address(localpage);
3379 if (localpage == NULL)
3381 args.acl_pages[0] = localpage;
3382 args.acl_pgbase = 0;
3383 args.acl_len = PAGE_SIZE;
3386 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3388 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
3391 if (res.acl_len > args.acl_len)
3392 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3394 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3397 if (res.acl_len > buflen)
3400 memcpy(buf, resp_buf, res.acl_len);
3405 __free_page(localpage);
3409 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3411 struct nfs4_exception exception = { };
3414 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3417 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3418 } while (exception.retry);
3422 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3424 struct nfs_server *server = NFS_SERVER(inode);
3427 if (!nfs4_server_supports_acls(server))
3429 ret = nfs_revalidate_inode(server, inode);
3432 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3433 nfs_zap_acl_cache(inode);
3434 ret = nfs4_read_cached_acl(inode, buf, buflen);
3437 return nfs4_get_acl_uncached(inode, buf, buflen);
3440 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3442 struct nfs_server *server = NFS_SERVER(inode);
3443 struct page *pages[NFS4ACL_MAXPAGES];
3444 struct nfs_setaclargs arg = {
3445 .fh = NFS_FH(inode),
3449 struct nfs_setaclres res;
3450 struct rpc_message msg = {
3451 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3457 if (!nfs4_server_supports_acls(server))
3459 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3462 nfs_inode_return_delegation(inode);
3463 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3466 * Free each page after tx, so the only ref left is
3467 * held by the network stack
3470 put_page(pages[i-1]);
3473 * Acl update can result in inode attribute update.
3474 * so mark the attribute cache invalid.
3476 spin_lock(&inode->i_lock);
3477 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3478 spin_unlock(&inode->i_lock);
3479 nfs_access_zap_cache(inode);
3480 nfs_zap_acl_cache(inode);
3484 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3486 struct nfs4_exception exception = { };
3489 err = nfs4_handle_exception(NFS_SERVER(inode),
3490 __nfs4_proc_set_acl(inode, buf, buflen),
3492 } while (exception.retry);
3497 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3499 struct nfs_client *clp = server->nfs_client;
3501 if (task->tk_status >= 0)
3503 switch(task->tk_status) {
3504 case -NFS4ERR_ADMIN_REVOKED:
3505 case -NFS4ERR_BAD_STATEID:
3506 case -NFS4ERR_OPENMODE:
3509 nfs4_schedule_stateid_recovery(server, state);
3510 goto wait_on_recovery;
3511 case -NFS4ERR_STALE_STATEID:
3512 case -NFS4ERR_STALE_CLIENTID:
3513 case -NFS4ERR_EXPIRED:
3514 nfs4_schedule_lease_recovery(clp);
3515 goto wait_on_recovery;
3516 #if defined(CONFIG_NFS_V4_1)
3517 case -NFS4ERR_BADSESSION:
3518 case -NFS4ERR_BADSLOT:
3519 case -NFS4ERR_BAD_HIGH_SLOT:
3520 case -NFS4ERR_DEADSESSION:
3521 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3522 case -NFS4ERR_SEQ_FALSE_RETRY:
3523 case -NFS4ERR_SEQ_MISORDERED:
3524 dprintk("%s ERROR %d, Reset session\n", __func__,
3526 nfs4_schedule_session_recovery(clp->cl_session);
3527 task->tk_status = 0;
3529 #endif /* CONFIG_NFS_V4_1 */
3530 case -NFS4ERR_DELAY:
3531 nfs_inc_server_stats(server, NFSIOS_DELAY);
3532 case -NFS4ERR_GRACE:
3534 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3535 task->tk_status = 0;
3537 case -NFS4ERR_OLD_STATEID:
3538 task->tk_status = 0;
3541 task->tk_status = nfs4_map_errors(task->tk_status);
3544 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3545 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3546 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3547 task->tk_status = 0;
3551 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3552 unsigned short port, struct rpc_cred *cred,
3553 struct nfs4_setclientid_res *res)
3555 nfs4_verifier sc_verifier;
3556 struct nfs4_setclientid setclientid = {
3557 .sc_verifier = &sc_verifier,
3559 .sc_cb_ident = clp->cl_cb_ident,
3561 struct rpc_message msg = {
3562 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3563 .rpc_argp = &setclientid,
3571 p = (__be32*)sc_verifier.data;
3572 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3573 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3576 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3577 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3579 rpc_peeraddr2str(clp->cl_rpcclient,
3581 rpc_peeraddr2str(clp->cl_rpcclient,
3583 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3584 clp->cl_id_uniquifier);
3585 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3586 sizeof(setclientid.sc_netid),
3587 rpc_peeraddr2str(clp->cl_rpcclient,
3588 RPC_DISPLAY_NETID));
3589 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3590 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3591 clp->cl_ipaddr, port >> 8, port & 255);
3593 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3594 if (status != -NFS4ERR_CLID_INUSE)
3599 ssleep(clp->cl_lease_time / HZ + 1);
3601 if (++clp->cl_id_uniquifier == 0)
3607 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3608 struct nfs4_setclientid_res *arg,
3609 struct rpc_cred *cred)
3611 struct nfs_fsinfo fsinfo;
3612 struct rpc_message msg = {
3613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3615 .rpc_resp = &fsinfo,
3622 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3624 spin_lock(&clp->cl_lock);
3625 clp->cl_lease_time = fsinfo.lease_time * HZ;
3626 clp->cl_last_renewal = now;
3627 spin_unlock(&clp->cl_lock);
3632 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3633 struct nfs4_setclientid_res *arg,
3634 struct rpc_cred *cred)
3639 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
3643 case -NFS4ERR_RESOURCE:
3644 /* The IBM lawyers misread another document! */
3645 case -NFS4ERR_DELAY:
3646 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3652 struct nfs4_delegreturndata {
3653 struct nfs4_delegreturnargs args;
3654 struct nfs4_delegreturnres res;
3656 nfs4_stateid stateid;
3657 unsigned long timestamp;
3658 struct nfs_fattr fattr;
3662 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3664 struct nfs4_delegreturndata *data = calldata;
3666 if (!nfs4_sequence_done(task, &data->res.seq_res))
3669 switch (task->tk_status) {
3670 case -NFS4ERR_STALE_STATEID:
3671 case -NFS4ERR_EXPIRED:
3673 renew_lease(data->res.server, data->timestamp);
3676 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3678 nfs_restart_rpc(task, data->res.server->nfs_client);
3682 data->rpc_status = task->tk_status;
3685 static void nfs4_delegreturn_release(void *calldata)
3690 #if defined(CONFIG_NFS_V4_1)
3691 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3693 struct nfs4_delegreturndata *d_data;
3695 d_data = (struct nfs4_delegreturndata *)data;
3697 if (nfs4_setup_sequence(d_data->res.server,
3698 &d_data->args.seq_args,
3699 &d_data->res.seq_res, 1, task))
3701 rpc_call_start(task);
3703 #endif /* CONFIG_NFS_V4_1 */
3705 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3706 #if defined(CONFIG_NFS_V4_1)
3707 .rpc_call_prepare = nfs4_delegreturn_prepare,
3708 #endif /* CONFIG_NFS_V4_1 */
3709 .rpc_call_done = nfs4_delegreturn_done,
3710 .rpc_release = nfs4_delegreturn_release,
3713 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3715 struct nfs4_delegreturndata *data;
3716 struct nfs_server *server = NFS_SERVER(inode);
3717 struct rpc_task *task;
3718 struct rpc_message msg = {
3719 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3722 struct rpc_task_setup task_setup_data = {
3723 .rpc_client = server->client,
3724 .rpc_message = &msg,
3725 .callback_ops = &nfs4_delegreturn_ops,
3726 .flags = RPC_TASK_ASYNC,
3730 data = kzalloc(sizeof(*data), GFP_NOFS);
3733 data->args.fhandle = &data->fh;
3734 data->args.stateid = &data->stateid;
3735 data->args.bitmask = server->attr_bitmask;
3736 nfs_copy_fh(&data->fh, NFS_FH(inode));
3737 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3738 data->res.fattr = &data->fattr;
3739 data->res.server = server;
3740 nfs_fattr_init(data->res.fattr);
3741 data->timestamp = jiffies;
3742 data->rpc_status = 0;
3744 task_setup_data.callback_data = data;
3745 msg.rpc_argp = &data->args;
3746 msg.rpc_resp = &data->res;
3747 task = rpc_run_task(&task_setup_data);
3749 return PTR_ERR(task);
3752 status = nfs4_wait_for_completion_rpc_task(task);
3755 status = data->rpc_status;
3758 nfs_refresh_inode(inode, &data->fattr);
3764 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3766 struct nfs_server *server = NFS_SERVER(inode);
3767 struct nfs4_exception exception = { };
3770 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3772 case -NFS4ERR_STALE_STATEID:
3773 case -NFS4ERR_EXPIRED:
3777 err = nfs4_handle_exception(server, err, &exception);
3778 } while (exception.retry);
3782 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3783 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3786 * sleep, with exponential backoff, and retry the LOCK operation.
3788 static unsigned long
3789 nfs4_set_lock_task_retry(unsigned long timeout)
3791 schedule_timeout_killable(timeout);
3793 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3794 return NFS4_LOCK_MAXTIMEOUT;
3798 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3800 struct inode *inode = state->inode;
3801 struct nfs_server *server = NFS_SERVER(inode);
3802 struct nfs_client *clp = server->nfs_client;
3803 struct nfs_lockt_args arg = {
3804 .fh = NFS_FH(inode),
3807 struct nfs_lockt_res res = {
3810 struct rpc_message msg = {
3811 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3814 .rpc_cred = state->owner->so_cred,
3816 struct nfs4_lock_state *lsp;
3819 arg.lock_owner.clientid = clp->cl_clientid;
3820 status = nfs4_set_lock_state(state, request);
3823 lsp = request->fl_u.nfs4_fl.owner;
3824 arg.lock_owner.id = lsp->ls_id.id;
3825 arg.lock_owner.s_dev = server->s_dev;
3826 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3829 request->fl_type = F_UNLCK;
3831 case -NFS4ERR_DENIED:
3834 request->fl_ops->fl_release_private(request);
3839 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3841 struct nfs4_exception exception = { };
3845 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3846 _nfs4_proc_getlk(state, cmd, request),
3848 } while (exception.retry);
3852 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3855 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3857 res = posix_lock_file_wait(file, fl);
3860 res = flock_lock_file_wait(file, fl);
3868 struct nfs4_unlockdata {
3869 struct nfs_locku_args arg;
3870 struct nfs_locku_res res;
3871 struct nfs4_lock_state *lsp;
3872 struct nfs_open_context *ctx;
3873 struct file_lock fl;
3874 const struct nfs_server *server;
3875 unsigned long timestamp;
3878 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3879 struct nfs_open_context *ctx,
3880 struct nfs4_lock_state *lsp,
3881 struct nfs_seqid *seqid)
3883 struct nfs4_unlockdata *p;
3884 struct inode *inode = lsp->ls_state->inode;
3886 p = kzalloc(sizeof(*p), GFP_NOFS);
3889 p->arg.fh = NFS_FH(inode);
3891 p->arg.seqid = seqid;
3892 p->res.seqid = seqid;
3893 p->arg.stateid = &lsp->ls_stateid;
3895 atomic_inc(&lsp->ls_count);
3896 /* Ensure we don't close file until we're done freeing locks! */
3897 p->ctx = get_nfs_open_context(ctx);
3898 memcpy(&p->fl, fl, sizeof(p->fl));
3899 p->server = NFS_SERVER(inode);
3903 static void nfs4_locku_release_calldata(void *data)
3905 struct nfs4_unlockdata *calldata = data;
3906 nfs_free_seqid(calldata->arg.seqid);
3907 nfs4_put_lock_state(calldata->lsp);
3908 put_nfs_open_context(calldata->ctx);
3912 static void nfs4_locku_done(struct rpc_task *task, void *data)
3914 struct nfs4_unlockdata *calldata = data;
3916 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3918 switch (task->tk_status) {
3920 memcpy(calldata->lsp->ls_stateid.data,
3921 calldata->res.stateid.data,
3922 sizeof(calldata->lsp->ls_stateid.data));
3923 renew_lease(calldata->server, calldata->timestamp);
3925 case -NFS4ERR_BAD_STATEID:
3926 case -NFS4ERR_OLD_STATEID:
3927 case -NFS4ERR_STALE_STATEID:
3928 case -NFS4ERR_EXPIRED:
3931 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
3932 nfs_restart_rpc(task,
3933 calldata->server->nfs_client);
3937 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3939 struct nfs4_unlockdata *calldata = data;
3941 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3943 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3944 /* Note: exit _without_ running nfs4_locku_done */
3945 task->tk_action = NULL;
3948 calldata->timestamp = jiffies;
3949 if (nfs4_setup_sequence(calldata->server,
3950 &calldata->arg.seq_args,
3951 &calldata->res.seq_res, 1, task))
3953 rpc_call_start(task);
3956 static const struct rpc_call_ops nfs4_locku_ops = {
3957 .rpc_call_prepare = nfs4_locku_prepare,
3958 .rpc_call_done = nfs4_locku_done,
3959 .rpc_release = nfs4_locku_release_calldata,
3962 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3963 struct nfs_open_context *ctx,
3964 struct nfs4_lock_state *lsp,
3965 struct nfs_seqid *seqid)
3967 struct nfs4_unlockdata *data;
3968 struct rpc_message msg = {
3969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3970 .rpc_cred = ctx->cred,
3972 struct rpc_task_setup task_setup_data = {
3973 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3974 .rpc_message = &msg,
3975 .callback_ops = &nfs4_locku_ops,
3976 .workqueue = nfsiod_workqueue,
3977 .flags = RPC_TASK_ASYNC,
3980 /* Ensure this is an unlock - when canceling a lock, the
3981 * canceled lock is passed in, and it won't be an unlock.
3983 fl->fl_type = F_UNLCK;
3985 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3987 nfs_free_seqid(seqid);
3988 return ERR_PTR(-ENOMEM);
3991 msg.rpc_argp = &data->arg;
3992 msg.rpc_resp = &data->res;
3993 task_setup_data.callback_data = data;
3994 return rpc_run_task(&task_setup_data);
3997 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3999 struct nfs_inode *nfsi = NFS_I(state->inode);
4000 struct nfs_seqid *seqid;
4001 struct nfs4_lock_state *lsp;
4002 struct rpc_task *task;
4004 unsigned char fl_flags = request->fl_flags;
4006 status = nfs4_set_lock_state(state, request);
4007 /* Unlock _before_ we do the RPC call */
4008 request->fl_flags |= FL_EXISTS;
4009 down_read(&nfsi->rwsem);
4010 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4011 up_read(&nfsi->rwsem);
4014 up_read(&nfsi->rwsem);
4017 /* Is this a delegated lock? */
4018 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4020 lsp = request->fl_u.nfs4_fl.owner;
4021 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4025 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4026 status = PTR_ERR(task);
4029 status = nfs4_wait_for_completion_rpc_task(task);
4032 request->fl_flags = fl_flags;
4036 struct nfs4_lockdata {
4037 struct nfs_lock_args arg;
4038 struct nfs_lock_res res;
4039 struct nfs4_lock_state *lsp;
4040 struct nfs_open_context *ctx;
4041 struct file_lock fl;
4042 unsigned long timestamp;
4045 struct nfs_server *server;
4048 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4049 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4052 struct nfs4_lockdata *p;
4053 struct inode *inode = lsp->ls_state->inode;
4054 struct nfs_server *server = NFS_SERVER(inode);
4056 p = kzalloc(sizeof(*p), gfp_mask);
4060 p->arg.fh = NFS_FH(inode);
4062 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4063 if (p->arg.open_seqid == NULL)
4065 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4066 if (p->arg.lock_seqid == NULL)
4067 goto out_free_seqid;
4068 p->arg.lock_stateid = &lsp->ls_stateid;
4069 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4070 p->arg.lock_owner.id = lsp->ls_id.id;
4071 p->arg.lock_owner.s_dev = server->s_dev;
4072 p->res.lock_seqid = p->arg.lock_seqid;
4075 atomic_inc(&lsp->ls_count);
4076 p->ctx = get_nfs_open_context(ctx);
4077 memcpy(&p->fl, fl, sizeof(p->fl));
4080 nfs_free_seqid(p->arg.open_seqid);
4086 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4088 struct nfs4_lockdata *data = calldata;
4089 struct nfs4_state *state = data->lsp->ls_state;
4091 dprintk("%s: begin!\n", __func__);
4092 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4094 /* Do we need to do an open_to_lock_owner? */
4095 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4096 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4098 data->arg.open_stateid = &state->stateid;
4099 data->arg.new_lock_owner = 1;
4100 data->res.open_seqid = data->arg.open_seqid;
4102 data->arg.new_lock_owner = 0;
4103 data->timestamp = jiffies;
4104 if (nfs4_setup_sequence(data->server,
4105 &data->arg.seq_args,
4106 &data->res.seq_res, 1, task))
4108 rpc_call_start(task);
4109 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4112 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4114 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4115 nfs4_lock_prepare(task, calldata);
4118 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4120 struct nfs4_lockdata *data = calldata;
4122 dprintk("%s: begin!\n", __func__);
4124 if (!nfs4_sequence_done(task, &data->res.seq_res))
4127 data->rpc_status = task->tk_status;
4128 if (data->arg.new_lock_owner != 0) {
4129 if (data->rpc_status == 0)
4130 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4134 if (data->rpc_status == 0) {
4135 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4136 sizeof(data->lsp->ls_stateid.data));
4137 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4138 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
4141 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4144 static void nfs4_lock_release(void *calldata)
4146 struct nfs4_lockdata *data = calldata;
4148 dprintk("%s: begin!\n", __func__);
4149 nfs_free_seqid(data->arg.open_seqid);
4150 if (data->cancelled != 0) {
4151 struct rpc_task *task;
4152 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4153 data->arg.lock_seqid);
4155 rpc_put_task_async(task);
4156 dprintk("%s: cancelling lock!\n", __func__);
4158 nfs_free_seqid(data->arg.lock_seqid);
4159 nfs4_put_lock_state(data->lsp);
4160 put_nfs_open_context(data->ctx);
4162 dprintk("%s: done!\n", __func__);
4165 static const struct rpc_call_ops nfs4_lock_ops = {
4166 .rpc_call_prepare = nfs4_lock_prepare,
4167 .rpc_call_done = nfs4_lock_done,
4168 .rpc_release = nfs4_lock_release,
4171 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4172 .rpc_call_prepare = nfs4_recover_lock_prepare,
4173 .rpc_call_done = nfs4_lock_done,
4174 .rpc_release = nfs4_lock_release,
4177 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4180 case -NFS4ERR_ADMIN_REVOKED:
4181 case -NFS4ERR_BAD_STATEID:
4182 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4183 if (new_lock_owner != 0 ||
4184 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4185 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4187 case -NFS4ERR_STALE_STATEID:
4188 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4189 case -NFS4ERR_EXPIRED:
4190 nfs4_schedule_lease_recovery(server->nfs_client);
4194 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4196 struct nfs4_lockdata *data;
4197 struct rpc_task *task;
4198 struct rpc_message msg = {
4199 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4200 .rpc_cred = state->owner->so_cred,
4202 struct rpc_task_setup task_setup_data = {
4203 .rpc_client = NFS_CLIENT(state->inode),
4204 .rpc_message = &msg,
4205 .callback_ops = &nfs4_lock_ops,
4206 .workqueue = nfsiod_workqueue,
4207 .flags = RPC_TASK_ASYNC,
4211 dprintk("%s: begin!\n", __func__);
4212 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4213 fl->fl_u.nfs4_fl.owner,
4214 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4218 data->arg.block = 1;
4219 if (recovery_type > NFS_LOCK_NEW) {
4220 if (recovery_type == NFS_LOCK_RECLAIM)
4221 data->arg.reclaim = NFS_LOCK_RECLAIM;
4222 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4224 msg.rpc_argp = &data->arg;
4225 msg.rpc_resp = &data->res;
4226 task_setup_data.callback_data = data;
4227 task = rpc_run_task(&task_setup_data);
4229 return PTR_ERR(task);
4230 ret = nfs4_wait_for_completion_rpc_task(task);
4232 ret = data->rpc_status;
4234 nfs4_handle_setlk_error(data->server, data->lsp,
4235 data->arg.new_lock_owner, ret);
4237 data->cancelled = 1;
4239 dprintk("%s: done, ret = %d!\n", __func__, ret);
4243 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4245 struct nfs_server *server = NFS_SERVER(state->inode);
4246 struct nfs4_exception exception = { };
4250 /* Cache the lock if possible... */
4251 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4253 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4254 if (err != -NFS4ERR_DELAY)
4256 nfs4_handle_exception(server, err, &exception);
4257 } while (exception.retry);
4261 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4263 struct nfs_server *server = NFS_SERVER(state->inode);
4264 struct nfs4_exception exception = { };
4267 err = nfs4_set_lock_state(state, request);
4271 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4273 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4277 case -NFS4ERR_GRACE:
4278 case -NFS4ERR_DELAY:
4279 nfs4_handle_exception(server, err, &exception);
4282 } while (exception.retry);
4287 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4289 struct nfs_inode *nfsi = NFS_I(state->inode);
4290 unsigned char fl_flags = request->fl_flags;
4291 int status = -ENOLCK;
4293 if ((fl_flags & FL_POSIX) &&
4294 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4296 /* Is this a delegated open? */
4297 status = nfs4_set_lock_state(state, request);
4300 request->fl_flags |= FL_ACCESS;
4301 status = do_vfs_lock(request->fl_file, request);
4304 down_read(&nfsi->rwsem);
4305 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4306 /* Yes: cache locks! */
4307 /* ...but avoid races with delegation recall... */
4308 request->fl_flags = fl_flags & ~FL_SLEEP;
4309 status = do_vfs_lock(request->fl_file, request);
4312 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4315 /* Note: we always want to sleep here! */
4316 request->fl_flags = fl_flags | FL_SLEEP;
4317 if (do_vfs_lock(request->fl_file, request) < 0)
4318 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4320 up_read(&nfsi->rwsem);
4322 request->fl_flags = fl_flags;
4326 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4328 struct nfs4_exception exception = { };
4332 err = _nfs4_proc_setlk(state, cmd, request);
4333 if (err == -NFS4ERR_DENIED)
4335 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4337 } while (exception.retry);
4342 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4344 struct nfs_open_context *ctx;
4345 struct nfs4_state *state;
4346 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4349 /* verify open state */
4350 ctx = nfs_file_open_context(filp);
4353 if (request->fl_start < 0 || request->fl_end < 0)
4356 if (IS_GETLK(cmd)) {
4358 return nfs4_proc_getlk(state, F_GETLK, request);
4362 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4365 if (request->fl_type == F_UNLCK) {
4367 return nfs4_proc_unlck(state, cmd, request);
4374 status = nfs4_proc_setlk(state, cmd, request);
4375 if ((status != -EAGAIN) || IS_SETLK(cmd))
4377 timeout = nfs4_set_lock_task_retry(timeout);
4378 status = -ERESTARTSYS;
4381 } while(status < 0);
4385 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4387 struct nfs_server *server = NFS_SERVER(state->inode);
4388 struct nfs4_exception exception = { };
4391 err = nfs4_set_lock_state(state, fl);
4395 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4398 printk(KERN_ERR "%s: unhandled error %d.\n",
4403 case -NFS4ERR_EXPIRED:
4404 case -NFS4ERR_STALE_CLIENTID:
4405 case -NFS4ERR_STALE_STATEID:
4406 nfs4_schedule_lease_recovery(server->nfs_client);
4408 case -NFS4ERR_BADSESSION:
4409 case -NFS4ERR_BADSLOT:
4410 case -NFS4ERR_BAD_HIGH_SLOT:
4411 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4412 case -NFS4ERR_DEADSESSION:
4413 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
4417 * The show must go on: exit, but mark the
4418 * stateid as needing recovery.
4420 case -NFS4ERR_ADMIN_REVOKED:
4421 case -NFS4ERR_BAD_STATEID:
4422 case -NFS4ERR_OPENMODE:
4423 nfs4_schedule_stateid_recovery(server, state);
4428 * User RPCSEC_GSS context has expired.
4429 * We cannot recover this stateid now, so
4430 * skip it and allow recovery thread to
4436 case -NFS4ERR_DENIED:
4437 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4440 case -NFS4ERR_DELAY:
4443 err = nfs4_handle_exception(server, err, &exception);
4444 } while (exception.retry);
4449 static void nfs4_release_lockowner_release(void *calldata)
4454 const struct rpc_call_ops nfs4_release_lockowner_ops = {
4455 .rpc_release = nfs4_release_lockowner_release,
4458 void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4460 struct nfs_server *server = lsp->ls_state->owner->so_server;
4461 struct nfs_release_lockowner_args *args;
4462 struct rpc_message msg = {
4463 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4466 if (server->nfs_client->cl_mvops->minor_version != 0)
4468 args = kmalloc(sizeof(*args), GFP_NOFS);
4471 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4472 args->lock_owner.id = lsp->ls_id.id;
4473 args->lock_owner.s_dev = server->s_dev;
4474 msg.rpc_argp = args;
4475 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4478 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4480 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4481 const void *buf, size_t buflen,
4482 int flags, int type)
4484 if (strcmp(key, "") != 0)
4487 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
4490 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4491 void *buf, size_t buflen, int type)
4493 if (strcmp(key, "") != 0)
4496 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
4499 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4500 size_t list_len, const char *name,
4501 size_t name_len, int type)
4503 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
4505 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4508 if (list && len <= list_len)
4509 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
4513 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4515 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4516 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4517 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4520 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4521 NFS_ATTR_FATTR_NLINK;
4522 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4526 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4527 struct nfs4_fs_locations *fs_locations, struct page *page)
4529 struct nfs_server *server = NFS_SERVER(dir);
4531 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4532 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
4534 struct nfs4_fs_locations_arg args = {
4535 .dir_fh = NFS_FH(dir),
4540 struct nfs4_fs_locations_res res = {
4541 .fs_locations = fs_locations,
4543 struct rpc_message msg = {
4544 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4550 dprintk("%s: start\n", __func__);
4551 nfs_fattr_init(&fs_locations->fattr);
4552 fs_locations->server = server;
4553 fs_locations->nlocations = 0;
4554 status = nfs4_call_sync(server, &msg, &args, &res, 0);
4555 nfs_fixup_referral_attributes(&fs_locations->fattr);
4556 dprintk("%s: returned status = %d\n", __func__, status);
4560 #ifdef CONFIG_NFS_V4_1
4562 * Check the exchange flags returned by the server for invalid flags, having
4563 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4566 static int nfs4_check_cl_exchange_flags(u32 flags)
4568 if (flags & ~EXCHGID4_FLAG_MASK_R)
4570 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4571 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4573 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4577 return -NFS4ERR_INVAL;
4581 * nfs4_proc_exchange_id()
4583 * Since the clientid has expired, all compounds using sessions
4584 * associated with the stale clientid will be returning
4585 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4586 * be in some phase of session reset.
4588 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4590 nfs4_verifier verifier;
4591 struct nfs41_exchange_id_args args = {
4593 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
4595 struct nfs41_exchange_id_res res = {
4599 struct rpc_message msg = {
4600 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4607 dprintk("--> %s\n", __func__);
4608 BUG_ON(clp == NULL);
4610 p = (u32 *)verifier.data;
4611 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4612 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4613 args.verifier = &verifier;
4615 args.id_len = scnprintf(args.id, sizeof(args.id),
4618 init_utsname()->nodename,
4619 init_utsname()->domainname,
4620 clp->cl_rpcclient->cl_auth->au_flavor);
4622 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4624 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
4625 dprintk("<-- %s status= %d\n", __func__, status);
4629 struct nfs4_get_lease_time_data {
4630 struct nfs4_get_lease_time_args *args;
4631 struct nfs4_get_lease_time_res *res;
4632 struct nfs_client *clp;
4635 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4639 struct nfs4_get_lease_time_data *data =
4640 (struct nfs4_get_lease_time_data *)calldata;
4642 dprintk("--> %s\n", __func__);
4643 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4644 /* just setup sequence, do not trigger session recovery
4645 since we're invoked within one */
4646 ret = nfs41_setup_sequence(data->clp->cl_session,
4647 &data->args->la_seq_args,
4648 &data->res->lr_seq_res, 0, task);
4650 BUG_ON(ret == -EAGAIN);
4651 rpc_call_start(task);
4652 dprintk("<-- %s\n", __func__);
4656 * Called from nfs4_state_manager thread for session setup, so don't recover
4657 * from sequence operation or clientid errors.
4659 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4661 struct nfs4_get_lease_time_data *data =
4662 (struct nfs4_get_lease_time_data *)calldata;
4664 dprintk("--> %s\n", __func__);
4665 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4667 switch (task->tk_status) {
4668 case -NFS4ERR_DELAY:
4669 case -NFS4ERR_GRACE:
4670 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4671 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4672 task->tk_status = 0;
4673 nfs_restart_rpc(task, data->clp);
4676 dprintk("<-- %s\n", __func__);
4679 struct rpc_call_ops nfs4_get_lease_time_ops = {
4680 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4681 .rpc_call_done = nfs4_get_lease_time_done,
4684 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4686 struct rpc_task *task;
4687 struct nfs4_get_lease_time_args args;
4688 struct nfs4_get_lease_time_res res = {
4689 .lr_fsinfo = fsinfo,
4691 struct nfs4_get_lease_time_data data = {
4696 struct rpc_message msg = {
4697 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4701 struct rpc_task_setup task_setup = {
4702 .rpc_client = clp->cl_rpcclient,
4703 .rpc_message = &msg,
4704 .callback_ops = &nfs4_get_lease_time_ops,
4705 .callback_data = &data
4709 dprintk("--> %s\n", __func__);
4710 task = rpc_run_task(&task_setup);
4713 status = PTR_ERR(task);
4715 status = task->tk_status;
4718 dprintk("<-- %s return %d\n", __func__, status);
4724 * Reset a slot table
4726 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4729 struct nfs4_slot *new = NULL;
4733 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4734 max_reqs, tbl->max_slots);
4736 /* Does the newly negotiated max_reqs match the existing slot table? */
4737 if (max_reqs != tbl->max_slots) {
4739 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
4746 spin_lock(&tbl->slot_tbl_lock);
4749 tbl->max_slots = max_reqs;
4751 for (i = 0; i < tbl->max_slots; ++i)
4752 tbl->slots[i].seq_nr = ivalue;
4753 spin_unlock(&tbl->slot_tbl_lock);
4754 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4755 tbl, tbl->slots, tbl->max_slots);
4757 dprintk("<-- %s: return %d\n", __func__, ret);
4762 * Reset the forechannel and backchannel slot tables
4764 static int nfs4_reset_slot_tables(struct nfs4_session *session)
4768 status = nfs4_reset_slot_table(&session->fc_slot_table,
4769 session->fc_attrs.max_reqs, 1);
4773 status = nfs4_reset_slot_table(&session->bc_slot_table,
4774 session->bc_attrs.max_reqs, 0);
4778 /* Destroy the slot table */
4779 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4781 if (session->fc_slot_table.slots != NULL) {
4782 kfree(session->fc_slot_table.slots);
4783 session->fc_slot_table.slots = NULL;
4785 if (session->bc_slot_table.slots != NULL) {
4786 kfree(session->bc_slot_table.slots);
4787 session->bc_slot_table.slots = NULL;
4793 * Initialize slot table
4795 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4796 int max_slots, int ivalue)
4798 struct nfs4_slot *slot;
4801 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4803 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
4805 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
4810 spin_lock(&tbl->slot_tbl_lock);
4811 tbl->max_slots = max_slots;
4813 tbl->highest_used_slotid = -1; /* no slot is currently used */
4814 spin_unlock(&tbl->slot_tbl_lock);
4815 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4816 tbl, tbl->slots, tbl->max_slots);
4818 dprintk("<-- %s: return %d\n", __func__, ret);
4823 * Initialize the forechannel and backchannel tables
4825 static int nfs4_init_slot_tables(struct nfs4_session *session)
4827 struct nfs4_slot_table *tbl;
4830 tbl = &session->fc_slot_table;
4831 if (tbl->slots == NULL) {
4832 status = nfs4_init_slot_table(tbl,
4833 session->fc_attrs.max_reqs, 1);
4838 tbl = &session->bc_slot_table;
4839 if (tbl->slots == NULL) {
4840 status = nfs4_init_slot_table(tbl,
4841 session->bc_attrs.max_reqs, 0);
4843 nfs4_destroy_slot_tables(session);
4849 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4851 struct nfs4_session *session;
4852 struct nfs4_slot_table *tbl;
4854 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
4858 tbl = &session->fc_slot_table;
4859 tbl->highest_used_slotid = -1;
4860 spin_lock_init(&tbl->slot_tbl_lock);
4861 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4862 init_completion(&tbl->complete);
4864 tbl = &session->bc_slot_table;
4865 tbl->highest_used_slotid = -1;
4866 spin_lock_init(&tbl->slot_tbl_lock);
4867 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4868 init_completion(&tbl->complete);
4870 session->session_state = 1<<NFS4_SESSION_INITING;
4876 void nfs4_destroy_session(struct nfs4_session *session)
4878 nfs4_proc_destroy_session(session);
4879 dprintk("%s Destroy backchannel for xprt %p\n",
4880 __func__, session->clp->cl_rpcclient->cl_xprt);
4881 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4882 NFS41_BC_MIN_CALLBACKS);
4883 nfs4_destroy_slot_tables(session);
4888 * Initialize the values to be used by the client in CREATE_SESSION
4889 * If nfs4_init_session set the fore channel request and response sizes,
4892 * Set the back channel max_resp_sz_cached to zero to force the client to
4893 * always set csa_cachethis to FALSE because the current implementation
4894 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4896 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4898 struct nfs4_session *session = args->client->cl_session;
4899 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4900 mxresp_sz = session->fc_attrs.max_resp_sz;
4903 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4905 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4906 /* Fore channel attributes */
4907 args->fc_attrs.headerpadsz = 0;
4908 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4909 args->fc_attrs.max_resp_sz = mxresp_sz;
4910 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4911 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4913 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4914 "max_ops=%u max_reqs=%u\n",
4916 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4917 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
4919 /* Back channel attributes */
4920 args->bc_attrs.headerpadsz = 0;
4921 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4922 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4923 args->bc_attrs.max_resp_sz_cached = 0;
4924 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4925 args->bc_attrs.max_reqs = 1;
4927 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4928 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4930 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4931 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4932 args->bc_attrs.max_reqs);
4935 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
4937 struct nfs4_channel_attrs *sent = &args->fc_attrs;
4938 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
4940 if (rcvd->headerpadsz > sent->headerpadsz)
4942 if (rcvd->max_resp_sz > sent->max_resp_sz)
4945 * Our requested max_ops is the minimum we need; we're not
4946 * prepared to break up compounds into smaller pieces than that.
4947 * So, no point even trying to continue if the server won't
4950 if (rcvd->max_ops < sent->max_ops)
4952 if (rcvd->max_reqs == 0)
4957 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
4959 struct nfs4_channel_attrs *sent = &args->bc_attrs;
4960 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
4962 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
4964 if (rcvd->max_resp_sz < sent->max_resp_sz)
4966 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
4968 /* These would render the backchannel useless: */
4969 if (rcvd->max_ops == 0)
4971 if (rcvd->max_reqs == 0)
4976 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4977 struct nfs4_session *session)
4981 ret = nfs4_verify_fore_channel_attrs(args, session);
4984 return nfs4_verify_back_channel_attrs(args, session);
4987 static int _nfs4_proc_create_session(struct nfs_client *clp)
4989 struct nfs4_session *session = clp->cl_session;
4990 struct nfs41_create_session_args args = {
4992 .cb_program = NFS4_CALLBACK,
4994 struct nfs41_create_session_res res = {
4997 struct rpc_message msg = {
4998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5004 nfs4_init_channel_attrs(&args);
5005 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5007 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5010 /* Verify the session's negotiated channel_attrs values */
5011 status = nfs4_verify_channel_attrs(&args, session);
5013 /* Increment the clientid slot sequence id */
5021 * Issues a CREATE_SESSION operation to the server.
5022 * It is the responsibility of the caller to verify the session is
5023 * expired before calling this routine.
5025 int nfs4_proc_create_session(struct nfs_client *clp)
5029 struct nfs4_session *session = clp->cl_session;
5033 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5036 status = _nfs4_proc_create_session(clp);
5037 if (status == -NFS4ERR_DELAY) {
5038 err = nfs4_delay(clp->cl_rpcclient, &timeout);
5042 } while (status == -NFS4ERR_DELAY);
5047 /* Init and reset the fore channel */
5048 status = nfs4_init_slot_tables(session);
5049 dprintk("slot table initialization returned %d\n", status);
5052 status = nfs4_reset_slot_tables(session);
5053 dprintk("slot table reset returned %d\n", status);
5057 ptr = (unsigned *)&session->sess_id.data[0];
5058 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5059 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5061 dprintk("<-- %s\n", __func__);
5066 * Issue the over-the-wire RPC DESTROY_SESSION.
5067 * The caller must serialize access to this routine.
5069 int nfs4_proc_destroy_session(struct nfs4_session *session)
5072 struct rpc_message msg;
5074 dprintk("--> nfs4_proc_destroy_session\n");
5076 /* session is still being setup */
5077 if (session->clp->cl_cons_state != NFS_CS_READY)
5080 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5081 msg.rpc_argp = session;
5082 msg.rpc_resp = NULL;
5083 msg.rpc_cred = NULL;
5084 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5088 "Got error %d from the server on DESTROY_SESSION. "
5089 "Session has been destroyed regardless...\n", status);
5091 dprintk("<-- nfs4_proc_destroy_session\n");
5095 int nfs4_init_session(struct nfs_server *server)
5097 struct nfs_client *clp = server->nfs_client;
5098 struct nfs4_session *session;
5099 unsigned int rsize, wsize;
5102 if (!nfs4_has_session(clp))
5105 session = clp->cl_session;
5106 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5109 rsize = server->rsize;
5111 rsize = NFS_MAX_FILE_IO_SIZE;
5112 wsize = server->wsize;
5114 wsize = NFS_MAX_FILE_IO_SIZE;
5116 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5117 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5119 ret = nfs4_recover_expired_lease(server);
5121 ret = nfs4_check_client_ready(clp);
5125 int nfs4_init_ds_session(struct nfs_client *clp)
5127 struct nfs4_session *session = clp->cl_session;
5130 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5133 ret = nfs4_client_recover_expired_lease(clp);
5135 /* Test for the DS role */
5136 if (!is_ds_client(clp))
5139 ret = nfs4_check_client_ready(clp);
5143 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5147 * Renew the cl_session lease.
5149 struct nfs4_sequence_data {
5150 struct nfs_client *clp;
5151 struct nfs4_sequence_args args;
5152 struct nfs4_sequence_res res;
5155 static void nfs41_sequence_release(void *data)
5157 struct nfs4_sequence_data *calldata = data;
5158 struct nfs_client *clp = calldata->clp;
5160 if (atomic_read(&clp->cl_count) > 1)
5161 nfs4_schedule_state_renewal(clp);
5162 nfs_put_client(clp);
5166 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5168 switch(task->tk_status) {
5169 case -NFS4ERR_DELAY:
5170 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5173 nfs4_schedule_lease_recovery(clp);
5178 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5180 struct nfs4_sequence_data *calldata = data;
5181 struct nfs_client *clp = calldata->clp;
5183 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5186 if (task->tk_status < 0) {
5187 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5188 if (atomic_read(&clp->cl_count) == 1)
5191 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5192 rpc_restart_call_prepare(task);
5196 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5198 dprintk("<-- %s\n", __func__);
5201 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5203 struct nfs4_sequence_data *calldata = data;
5204 struct nfs_client *clp = calldata->clp;
5205 struct nfs4_sequence_args *args;
5206 struct nfs4_sequence_res *res;
5208 args = task->tk_msg.rpc_argp;
5209 res = task->tk_msg.rpc_resp;
5211 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
5213 rpc_call_start(task);
5216 static const struct rpc_call_ops nfs41_sequence_ops = {
5217 .rpc_call_done = nfs41_sequence_call_done,
5218 .rpc_call_prepare = nfs41_sequence_prepare,
5219 .rpc_release = nfs41_sequence_release,
5222 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5224 struct nfs4_sequence_data *calldata;
5225 struct rpc_message msg = {
5226 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5229 struct rpc_task_setup task_setup_data = {
5230 .rpc_client = clp->cl_rpcclient,
5231 .rpc_message = &msg,
5232 .callback_ops = &nfs41_sequence_ops,
5233 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5236 if (!atomic_inc_not_zero(&clp->cl_count))
5237 return ERR_PTR(-EIO);
5238 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5239 if (calldata == NULL) {
5240 nfs_put_client(clp);
5241 return ERR_PTR(-ENOMEM);
5243 msg.rpc_argp = &calldata->args;
5244 msg.rpc_resp = &calldata->res;
5245 calldata->clp = clp;
5246 task_setup_data.callback_data = calldata;
5248 return rpc_run_task(&task_setup_data);
5251 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5253 struct rpc_task *task;
5256 task = _nfs41_proc_sequence(clp, cred);
5258 ret = PTR_ERR(task);
5260 rpc_put_task_async(task);
5261 dprintk("<-- %s status=%d\n", __func__, ret);
5265 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5267 struct rpc_task *task;
5270 task = _nfs41_proc_sequence(clp, cred);
5272 ret = PTR_ERR(task);
5275 ret = rpc_wait_for_completion_task(task);
5277 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5279 if (task->tk_status == 0)
5280 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5281 ret = task->tk_status;
5285 dprintk("<-- %s status=%d\n", __func__, ret);
5289 struct nfs4_reclaim_complete_data {
5290 struct nfs_client *clp;
5291 struct nfs41_reclaim_complete_args arg;
5292 struct nfs41_reclaim_complete_res res;
5295 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5297 struct nfs4_reclaim_complete_data *calldata = data;
5299 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5300 if (nfs41_setup_sequence(calldata->clp->cl_session,
5301 &calldata->arg.seq_args,
5302 &calldata->res.seq_res, 0, task))
5305 rpc_call_start(task);
5308 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5310 switch(task->tk_status) {
5312 case -NFS4ERR_COMPLETE_ALREADY:
5313 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5315 case -NFS4ERR_DELAY:
5316 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5319 nfs4_schedule_lease_recovery(clp);
5324 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5326 struct nfs4_reclaim_complete_data *calldata = data;
5327 struct nfs_client *clp = calldata->clp;
5328 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5330 dprintk("--> %s\n", __func__);
5331 if (!nfs41_sequence_done(task, res))
5334 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5335 rpc_restart_call_prepare(task);
5338 dprintk("<-- %s\n", __func__);
5341 static void nfs4_free_reclaim_complete_data(void *data)
5343 struct nfs4_reclaim_complete_data *calldata = data;
5348 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5349 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5350 .rpc_call_done = nfs4_reclaim_complete_done,
5351 .rpc_release = nfs4_free_reclaim_complete_data,
5355 * Issue a global reclaim complete.
5357 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5359 struct nfs4_reclaim_complete_data *calldata;
5360 struct rpc_task *task;
5361 struct rpc_message msg = {
5362 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5364 struct rpc_task_setup task_setup_data = {
5365 .rpc_client = clp->cl_rpcclient,
5366 .rpc_message = &msg,
5367 .callback_ops = &nfs4_reclaim_complete_call_ops,
5368 .flags = RPC_TASK_ASYNC,
5370 int status = -ENOMEM;
5372 dprintk("--> %s\n", __func__);
5373 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5374 if (calldata == NULL)
5376 calldata->clp = clp;
5377 calldata->arg.one_fs = 0;
5379 msg.rpc_argp = &calldata->arg;
5380 msg.rpc_resp = &calldata->res;
5381 task_setup_data.callback_data = calldata;
5382 task = rpc_run_task(&task_setup_data);
5384 status = PTR_ERR(task);
5387 status = nfs4_wait_for_completion_rpc_task(task);
5389 status = task->tk_status;
5393 dprintk("<-- %s status=%d\n", __func__, status);
5398 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5400 struct nfs4_layoutget *lgp = calldata;
5401 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5403 dprintk("--> %s\n", __func__);
5404 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5405 * right now covering the LAYOUTGET we are about to send.
5406 * However, that is not so catastrophic, and there seems
5407 * to be no way to prevent it completely.
5409 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5410 &lgp->res.seq_res, 0, task))
5412 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5413 NFS_I(lgp->args.inode)->layout,
5414 lgp->args.ctx->state)) {
5415 rpc_exit(task, NFS4_OK);
5418 rpc_call_start(task);
5421 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5423 struct nfs4_layoutget *lgp = calldata;
5424 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5426 dprintk("--> %s\n", __func__);
5428 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5431 switch (task->tk_status) {
5434 case -NFS4ERR_LAYOUTTRYLATER:
5435 case -NFS4ERR_RECALLCONFLICT:
5436 task->tk_status = -NFS4ERR_DELAY;
5439 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5440 rpc_restart_call_prepare(task);
5444 dprintk("<-- %s\n", __func__);
5447 static void nfs4_layoutget_release(void *calldata)
5449 struct nfs4_layoutget *lgp = calldata;
5451 dprintk("--> %s\n", __func__);
5452 if (lgp->res.layout.buf != NULL)
5453 free_page((unsigned long) lgp->res.layout.buf);
5454 put_nfs_open_context(lgp->args.ctx);
5456 dprintk("<-- %s\n", __func__);
5459 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5460 .rpc_call_prepare = nfs4_layoutget_prepare,
5461 .rpc_call_done = nfs4_layoutget_done,
5462 .rpc_release = nfs4_layoutget_release,
5465 int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5467 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5468 struct rpc_task *task;
5469 struct rpc_message msg = {
5470 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5471 .rpc_argp = &lgp->args,
5472 .rpc_resp = &lgp->res,
5474 struct rpc_task_setup task_setup_data = {
5475 .rpc_client = server->client,
5476 .rpc_message = &msg,
5477 .callback_ops = &nfs4_layoutget_call_ops,
5478 .callback_data = lgp,
5479 .flags = RPC_TASK_ASYNC,
5483 dprintk("--> %s\n", __func__);
5485 lgp->res.layout.buf = (void *)__get_free_page(GFP_NOFS);
5486 if (lgp->res.layout.buf == NULL) {
5487 nfs4_layoutget_release(lgp);
5491 lgp->res.seq_res.sr_slot = NULL;
5492 task = rpc_run_task(&task_setup_data);
5494 return PTR_ERR(task);
5495 status = nfs4_wait_for_completion_rpc_task(task);
5497 status = task->tk_status;
5499 status = pnfs_layout_process(lgp);
5501 dprintk("<-- %s status=%d\n", __func__, status);
5506 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5508 struct nfs4_getdeviceinfo_args args = {
5511 struct nfs4_getdeviceinfo_res res = {
5514 struct rpc_message msg = {
5515 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5521 dprintk("--> %s\n", __func__);
5522 status = nfs4_call_sync(server, &msg, &args, &res, 0);
5523 dprintk("<-- %s status=%d\n", __func__, status);
5528 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5530 struct nfs4_exception exception = { };
5534 err = nfs4_handle_exception(server,
5535 _nfs4_proc_getdeviceinfo(server, pdev),
5537 } while (exception.retry);
5540 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5542 #endif /* CONFIG_NFS_V4_1 */
5544 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
5545 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5546 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5547 .recover_open = nfs4_open_reclaim,
5548 .recover_lock = nfs4_lock_reclaim,
5549 .establish_clid = nfs4_init_clientid,
5550 .get_clid_cred = nfs4_get_setclientid_cred,
5553 #if defined(CONFIG_NFS_V4_1)
5554 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5555 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5556 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5557 .recover_open = nfs4_open_reclaim,
5558 .recover_lock = nfs4_lock_reclaim,
5559 .establish_clid = nfs41_init_clientid,
5560 .get_clid_cred = nfs4_get_exchange_id_cred,
5561 .reclaim_complete = nfs41_proc_reclaim_complete,
5563 #endif /* CONFIG_NFS_V4_1 */
5565 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
5566 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5567 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5568 .recover_open = nfs4_open_expired,
5569 .recover_lock = nfs4_lock_expired,
5570 .establish_clid = nfs4_init_clientid,
5571 .get_clid_cred = nfs4_get_setclientid_cred,
5574 #if defined(CONFIG_NFS_V4_1)
5575 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5576 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5577 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5578 .recover_open = nfs4_open_expired,
5579 .recover_lock = nfs4_lock_expired,
5580 .establish_clid = nfs41_init_clientid,
5581 .get_clid_cred = nfs4_get_exchange_id_cred,
5583 #endif /* CONFIG_NFS_V4_1 */
5585 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5586 .sched_state_renewal = nfs4_proc_async_renew,
5587 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
5588 .renew_lease = nfs4_proc_renew,
5591 #if defined(CONFIG_NFS_V4_1)
5592 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5593 .sched_state_renewal = nfs41_proc_async_sequence,
5594 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
5595 .renew_lease = nfs4_proc_sequence,
5599 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5601 .call_sync = _nfs4_call_sync,
5602 .validate_stateid = nfs4_validate_delegation_stateid,
5603 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5604 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5605 .state_renewal_ops = &nfs40_state_renewal_ops,
5608 #if defined(CONFIG_NFS_V4_1)
5609 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5611 .call_sync = _nfs4_call_sync_session,
5612 .validate_stateid = nfs41_validate_delegation_stateid,
5613 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5614 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5615 .state_renewal_ops = &nfs41_state_renewal_ops,
5619 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5620 [0] = &nfs_v4_0_minor_ops,
5621 #if defined(CONFIG_NFS_V4_1)
5622 [1] = &nfs_v4_1_minor_ops,
5626 static const struct inode_operations nfs4_file_inode_operations = {
5627 .permission = nfs_permission,
5628 .getattr = nfs_getattr,
5629 .setattr = nfs_setattr,
5630 .getxattr = generic_getxattr,
5631 .setxattr = generic_setxattr,
5632 .listxattr = generic_listxattr,
5633 .removexattr = generic_removexattr,
5636 const struct nfs_rpc_ops nfs_v4_clientops = {
5637 .version = 4, /* protocol version */
5638 .dentry_ops = &nfs4_dentry_operations,
5639 .dir_inode_ops = &nfs4_dir_inode_operations,
5640 .file_inode_ops = &nfs4_file_inode_operations,
5641 .getroot = nfs4_proc_get_root,
5642 .getattr = nfs4_proc_getattr,
5643 .setattr = nfs4_proc_setattr,
5644 .lookupfh = nfs4_proc_lookupfh,
5645 .lookup = nfs4_proc_lookup,
5646 .access = nfs4_proc_access,
5647 .readlink = nfs4_proc_readlink,
5648 .create = nfs4_proc_create,
5649 .remove = nfs4_proc_remove,
5650 .unlink_setup = nfs4_proc_unlink_setup,
5651 .unlink_done = nfs4_proc_unlink_done,
5652 .rename = nfs4_proc_rename,
5653 .rename_setup = nfs4_proc_rename_setup,
5654 .rename_done = nfs4_proc_rename_done,
5655 .link = nfs4_proc_link,
5656 .symlink = nfs4_proc_symlink,
5657 .mkdir = nfs4_proc_mkdir,
5658 .rmdir = nfs4_proc_remove,
5659 .readdir = nfs4_proc_readdir,
5660 .mknod = nfs4_proc_mknod,
5661 .statfs = nfs4_proc_statfs,
5662 .fsinfo = nfs4_proc_fsinfo,
5663 .pathconf = nfs4_proc_pathconf,
5664 .set_capabilities = nfs4_server_capabilities,
5665 .decode_dirent = nfs4_decode_dirent,
5666 .read_setup = nfs4_proc_read_setup,
5667 .read_done = nfs4_read_done,
5668 .write_setup = nfs4_proc_write_setup,
5669 .write_done = nfs4_write_done,
5670 .commit_setup = nfs4_proc_commit_setup,
5671 .commit_done = nfs4_commit_done,
5672 .lock = nfs4_proc_lock,
5673 .clear_acl_cache = nfs4_zap_acl_attr,
5674 .close_context = nfs4_close_context,
5675 .open_context = nfs4_atomic_open,
5676 .init_client = nfs4_init_client,
5679 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
5680 .prefix = XATTR_NAME_NFSV4_ACL,
5681 .list = nfs4_xattr_list_nfs4_acl,
5682 .get = nfs4_xattr_get_nfs4_acl,
5683 .set = nfs4_xattr_set_nfs4_acl,
5686 const struct xattr_handler *nfs4_xattr_handlers[] = {
5687 &nfs4_xattr_nfs4_acl_handler,