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 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.
574 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
576 int nfs4_setup_sequence(const struct nfs_server *server,
577 struct nfs4_sequence_args *args,
578 struct nfs4_sequence_res *res,
580 struct rpc_task *task)
582 struct nfs4_session *session = nfs4_get_session(server);
585 if (session == NULL) {
586 args->sa_session = NULL;
587 res->sr_session = NULL;
591 dprintk("--> %s clp %p session %p sr_slot %td\n",
592 __func__, session->clp, session, res->sr_slot ?
593 res->sr_slot - session->fc_slot_table.slots : -1);
595 ret = nfs41_setup_sequence(session, args, res, cache_reply,
598 dprintk("<-- %s status=%d\n", __func__, ret);
602 struct nfs41_call_sync_data {
603 const struct nfs_server *seq_server;
604 struct nfs4_sequence_args *seq_args;
605 struct nfs4_sequence_res *seq_res;
609 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
611 struct nfs41_call_sync_data *data = calldata;
613 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
615 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
616 data->seq_res, data->cache_reply, task))
618 rpc_call_start(task);
621 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
623 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
624 nfs41_call_sync_prepare(task, calldata);
627 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
629 struct nfs41_call_sync_data *data = calldata;
631 nfs41_sequence_done(task, data->seq_res);
634 struct rpc_call_ops nfs41_call_sync_ops = {
635 .rpc_call_prepare = nfs41_call_sync_prepare,
636 .rpc_call_done = nfs41_call_sync_done,
639 struct rpc_call_ops nfs41_call_priv_sync_ops = {
640 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
641 .rpc_call_done = nfs41_call_sync_done,
644 static int nfs4_call_sync_sequence(struct nfs_server *server,
645 struct rpc_message *msg,
646 struct nfs4_sequence_args *args,
647 struct nfs4_sequence_res *res,
652 struct rpc_task *task;
653 struct nfs41_call_sync_data data = {
654 .seq_server = server,
657 .cache_reply = cache_reply,
659 struct rpc_task_setup task_setup = {
660 .rpc_client = server->client,
662 .callback_ops = &nfs41_call_sync_ops,
663 .callback_data = &data
668 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
669 task = rpc_run_task(&task_setup);
673 ret = task->tk_status;
679 int _nfs4_call_sync_session(struct nfs_server *server,
680 struct rpc_message *msg,
681 struct nfs4_sequence_args *args,
682 struct nfs4_sequence_res *res,
685 return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
689 static int nfs4_sequence_done(struct rpc_task *task,
690 struct nfs4_sequence_res *res)
694 #endif /* CONFIG_NFS_V4_1 */
696 int _nfs4_call_sync(struct nfs_server *server,
697 struct rpc_message *msg,
698 struct nfs4_sequence_args *args,
699 struct nfs4_sequence_res *res,
702 args->sa_session = res->sr_session = NULL;
703 return rpc_call_sync(server->client, msg, 0);
706 #define nfs4_call_sync(server, msg, args, res, cache_reply) \
707 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
708 &(res)->seq_res, (cache_reply))
710 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
712 struct nfs_inode *nfsi = NFS_I(dir);
714 spin_lock(&dir->i_lock);
715 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
716 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
717 nfs_force_lookup_revalidate(dir);
718 nfsi->change_attr = cinfo->after;
719 spin_unlock(&dir->i_lock);
722 struct nfs4_opendata {
724 struct nfs_openargs o_arg;
725 struct nfs_openres o_res;
726 struct nfs_open_confirmargs c_arg;
727 struct nfs_open_confirmres c_res;
728 struct nfs_fattr f_attr;
729 struct nfs_fattr dir_attr;
732 struct nfs4_state_owner *owner;
733 struct nfs4_state *state;
735 unsigned long timestamp;
736 unsigned int rpc_done : 1;
742 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
744 p->o_res.f_attr = &p->f_attr;
745 p->o_res.dir_attr = &p->dir_attr;
746 p->o_res.seqid = p->o_arg.seqid;
747 p->c_res.seqid = p->c_arg.seqid;
748 p->o_res.server = p->o_arg.server;
749 nfs_fattr_init(&p->f_attr);
750 nfs_fattr_init(&p->dir_attr);
753 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
754 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
755 const struct iattr *attrs,
758 struct dentry *parent = dget_parent(path->dentry);
759 struct inode *dir = parent->d_inode;
760 struct nfs_server *server = NFS_SERVER(dir);
761 struct nfs4_opendata *p;
763 p = kzalloc(sizeof(*p), gfp_mask);
766 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
767 if (p->o_arg.seqid == NULL)
773 atomic_inc(&sp->so_count);
774 p->o_arg.fh = NFS_FH(dir);
775 p->o_arg.open_flags = flags;
776 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
777 p->o_arg.clientid = server->nfs_client->cl_clientid;
778 p->o_arg.id = sp->so_owner_id.id;
779 p->o_arg.name = &p->path.dentry->d_name;
780 p->o_arg.server = server;
781 p->o_arg.bitmask = server->attr_bitmask;
782 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
783 if (flags & O_CREAT) {
786 p->o_arg.u.attrs = &p->attrs;
787 memcpy(&p->attrs, attrs, sizeof(p->attrs));
788 s = (u32 *) p->o_arg.u.verifier.data;
792 p->c_arg.fh = &p->o_res.fh;
793 p->c_arg.stateid = &p->o_res.stateid;
794 p->c_arg.seqid = p->o_arg.seqid;
795 nfs4_init_opendata_res(p);
805 static void nfs4_opendata_free(struct kref *kref)
807 struct nfs4_opendata *p = container_of(kref,
808 struct nfs4_opendata, kref);
810 nfs_free_seqid(p->o_arg.seqid);
811 if (p->state != NULL)
812 nfs4_put_open_state(p->state);
813 nfs4_put_state_owner(p->owner);
819 static void nfs4_opendata_put(struct nfs4_opendata *p)
822 kref_put(&p->kref, nfs4_opendata_free);
825 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
829 ret = rpc_wait_for_completion_task(task);
833 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
837 if (open_mode & O_EXCL)
839 switch (mode & (FMODE_READ|FMODE_WRITE)) {
841 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
842 && state->n_rdonly != 0;
845 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
846 && state->n_wronly != 0;
848 case FMODE_READ|FMODE_WRITE:
849 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
850 && state->n_rdwr != 0;
856 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
858 if ((delegation->type & fmode) != fmode)
860 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
862 nfs_mark_delegation_referenced(delegation);
866 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
875 case FMODE_READ|FMODE_WRITE:
878 nfs4_state_set_mode_locked(state, state->state | fmode);
881 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
883 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
884 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
885 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
888 set_bit(NFS_O_RDONLY_STATE, &state->flags);
891 set_bit(NFS_O_WRONLY_STATE, &state->flags);
893 case FMODE_READ|FMODE_WRITE:
894 set_bit(NFS_O_RDWR_STATE, &state->flags);
898 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
900 write_seqlock(&state->seqlock);
901 nfs_set_open_stateid_locked(state, stateid, fmode);
902 write_sequnlock(&state->seqlock);
905 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
908 * Protect the call to nfs4_state_set_mode_locked and
909 * serialise the stateid update
911 write_seqlock(&state->seqlock);
912 if (deleg_stateid != NULL) {
913 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
914 set_bit(NFS_DELEGATED_STATE, &state->flags);
916 if (open_stateid != NULL)
917 nfs_set_open_stateid_locked(state, open_stateid, fmode);
918 write_sequnlock(&state->seqlock);
919 spin_lock(&state->owner->so_lock);
920 update_open_stateflags(state, fmode);
921 spin_unlock(&state->owner->so_lock);
924 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
926 struct nfs_inode *nfsi = NFS_I(state->inode);
927 struct nfs_delegation *deleg_cur;
930 fmode &= (FMODE_READ|FMODE_WRITE);
933 deleg_cur = rcu_dereference(nfsi->delegation);
934 if (deleg_cur == NULL)
937 spin_lock(&deleg_cur->lock);
938 if (nfsi->delegation != deleg_cur ||
939 (deleg_cur->type & fmode) != fmode)
940 goto no_delegation_unlock;
942 if (delegation == NULL)
943 delegation = &deleg_cur->stateid;
944 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
945 goto no_delegation_unlock;
947 nfs_mark_delegation_referenced(deleg_cur);
948 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
950 no_delegation_unlock:
951 spin_unlock(&deleg_cur->lock);
955 if (!ret && open_stateid != NULL) {
956 __update_open_stateid(state, open_stateid, NULL, fmode);
964 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
966 struct nfs_delegation *delegation;
969 delegation = rcu_dereference(NFS_I(inode)->delegation);
970 if (delegation == NULL || (delegation->type & fmode) == fmode) {
975 nfs_inode_return_delegation(inode);
978 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
980 struct nfs4_state *state = opendata->state;
981 struct nfs_inode *nfsi = NFS_I(state->inode);
982 struct nfs_delegation *delegation;
983 int open_mode = opendata->o_arg.open_flags & O_EXCL;
984 fmode_t fmode = opendata->o_arg.fmode;
985 nfs4_stateid stateid;
989 if (can_open_cached(state, fmode, open_mode)) {
990 spin_lock(&state->owner->so_lock);
991 if (can_open_cached(state, fmode, open_mode)) {
992 update_open_stateflags(state, fmode);
993 spin_unlock(&state->owner->so_lock);
994 goto out_return_state;
996 spin_unlock(&state->owner->so_lock);
999 delegation = rcu_dereference(nfsi->delegation);
1000 if (delegation == NULL ||
1001 !can_open_delegated(delegation, fmode)) {
1005 /* Save the delegation */
1006 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1008 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1013 /* Try to update the stateid using the delegation */
1014 if (update_open_stateid(state, NULL, &stateid, fmode))
1015 goto out_return_state;
1018 return ERR_PTR(ret);
1020 atomic_inc(&state->count);
1024 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1026 struct inode *inode;
1027 struct nfs4_state *state = NULL;
1028 struct nfs_delegation *delegation;
1031 if (!data->rpc_done) {
1032 state = nfs4_try_open_cached(data);
1037 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1039 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1040 ret = PTR_ERR(inode);
1044 state = nfs4_get_open_state(inode, data->owner);
1047 if (data->o_res.delegation_type != 0) {
1048 int delegation_flags = 0;
1051 delegation = rcu_dereference(NFS_I(inode)->delegation);
1053 delegation_flags = delegation->flags;
1055 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1056 nfs_inode_set_delegation(state->inode,
1057 data->owner->so_cred,
1060 nfs_inode_reclaim_delegation(state->inode,
1061 data->owner->so_cred,
1065 update_open_stateid(state, &data->o_res.stateid, NULL,
1073 return ERR_PTR(ret);
1076 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1078 struct nfs_inode *nfsi = NFS_I(state->inode);
1079 struct nfs_open_context *ctx;
1081 spin_lock(&state->inode->i_lock);
1082 list_for_each_entry(ctx, &nfsi->open_files, list) {
1083 if (ctx->state != state)
1085 get_nfs_open_context(ctx);
1086 spin_unlock(&state->inode->i_lock);
1089 spin_unlock(&state->inode->i_lock);
1090 return ERR_PTR(-ENOENT);
1093 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1095 struct nfs4_opendata *opendata;
1097 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
1098 if (opendata == NULL)
1099 return ERR_PTR(-ENOMEM);
1100 opendata->state = state;
1101 atomic_inc(&state->count);
1105 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1107 struct nfs4_state *newstate;
1110 opendata->o_arg.open_flags = 0;
1111 opendata->o_arg.fmode = fmode;
1112 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1113 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1114 nfs4_init_opendata_res(opendata);
1115 ret = _nfs4_recover_proc_open(opendata);
1118 newstate = nfs4_opendata_to_nfs4_state(opendata);
1119 if (IS_ERR(newstate))
1120 return PTR_ERR(newstate);
1121 nfs4_close_state(&opendata->path, newstate, fmode);
1126 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1128 struct nfs4_state *newstate;
1131 /* memory barrier prior to reading state->n_* */
1132 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1134 if (state->n_rdwr != 0) {
1135 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1136 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1139 if (newstate != state)
1142 if (state->n_wronly != 0) {
1143 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1144 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1147 if (newstate != state)
1150 if (state->n_rdonly != 0) {
1151 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1152 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1155 if (newstate != state)
1159 * We may have performed cached opens for all three recoveries.
1160 * Check if we need to update the current stateid.
1162 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1163 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1164 write_seqlock(&state->seqlock);
1165 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1166 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1167 write_sequnlock(&state->seqlock);
1174 * reclaim state on the server after a reboot.
1176 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1178 struct nfs_delegation *delegation;
1179 struct nfs4_opendata *opendata;
1180 fmode_t delegation_type = 0;
1183 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1184 if (IS_ERR(opendata))
1185 return PTR_ERR(opendata);
1186 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1187 opendata->o_arg.fh = NFS_FH(state->inode);
1189 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1190 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1191 delegation_type = delegation->type;
1193 opendata->o_arg.u.delegation_type = delegation_type;
1194 status = nfs4_open_recover(opendata, state);
1195 nfs4_opendata_put(opendata);
1199 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1201 struct nfs_server *server = NFS_SERVER(state->inode);
1202 struct nfs4_exception exception = { };
1205 err = _nfs4_do_open_reclaim(ctx, state);
1206 if (err != -NFS4ERR_DELAY)
1208 nfs4_handle_exception(server, err, &exception);
1209 } while (exception.retry);
1213 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1215 struct nfs_open_context *ctx;
1218 ctx = nfs4_state_find_open_context(state);
1220 return PTR_ERR(ctx);
1221 ret = nfs4_do_open_reclaim(ctx, state);
1222 put_nfs_open_context(ctx);
1226 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1228 struct nfs4_opendata *opendata;
1231 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1232 if (IS_ERR(opendata))
1233 return PTR_ERR(opendata);
1234 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1235 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1236 sizeof(opendata->o_arg.u.delegation.data));
1237 ret = nfs4_open_recover(opendata, state);
1238 nfs4_opendata_put(opendata);
1242 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1244 struct nfs4_exception exception = { };
1245 struct nfs_server *server = NFS_SERVER(state->inode);
1248 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1254 case -NFS4ERR_BADSESSION:
1255 case -NFS4ERR_BADSLOT:
1256 case -NFS4ERR_BAD_HIGH_SLOT:
1257 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1258 case -NFS4ERR_DEADSESSION:
1259 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
1261 case -NFS4ERR_STALE_CLIENTID:
1262 case -NFS4ERR_STALE_STATEID:
1263 case -NFS4ERR_EXPIRED:
1264 /* Don't recall a delegation if it was lost */
1265 nfs4_schedule_lease_recovery(server->nfs_client);
1269 * The show must go on: exit, but mark the
1270 * stateid as needing recovery.
1272 case -NFS4ERR_ADMIN_REVOKED:
1273 case -NFS4ERR_BAD_STATEID:
1274 nfs4_schedule_stateid_recovery(server, state);
1277 * User RPCSEC_GSS context has expired.
1278 * We cannot recover this stateid now, so
1279 * skip it and allow recovery thread to
1286 err = nfs4_handle_exception(server, err, &exception);
1287 } while (exception.retry);
1292 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1294 struct nfs4_opendata *data = calldata;
1296 data->rpc_status = task->tk_status;
1297 if (data->rpc_status == 0) {
1298 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1299 sizeof(data->o_res.stateid.data));
1300 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1301 renew_lease(data->o_res.server, data->timestamp);
1306 static void nfs4_open_confirm_release(void *calldata)
1308 struct nfs4_opendata *data = calldata;
1309 struct nfs4_state *state = NULL;
1311 /* If this request hasn't been cancelled, do nothing */
1312 if (data->cancelled == 0)
1314 /* In case of error, no cleanup! */
1315 if (!data->rpc_done)
1317 state = nfs4_opendata_to_nfs4_state(data);
1319 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1321 nfs4_opendata_put(data);
1324 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1325 .rpc_call_done = nfs4_open_confirm_done,
1326 .rpc_release = nfs4_open_confirm_release,
1330 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1332 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1334 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1335 struct rpc_task *task;
1336 struct rpc_message msg = {
1337 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1338 .rpc_argp = &data->c_arg,
1339 .rpc_resp = &data->c_res,
1340 .rpc_cred = data->owner->so_cred,
1342 struct rpc_task_setup task_setup_data = {
1343 .rpc_client = server->client,
1344 .rpc_message = &msg,
1345 .callback_ops = &nfs4_open_confirm_ops,
1346 .callback_data = data,
1347 .workqueue = nfsiod_workqueue,
1348 .flags = RPC_TASK_ASYNC,
1352 kref_get(&data->kref);
1354 data->rpc_status = 0;
1355 data->timestamp = jiffies;
1356 task = rpc_run_task(&task_setup_data);
1358 return PTR_ERR(task);
1359 status = nfs4_wait_for_completion_rpc_task(task);
1361 data->cancelled = 1;
1364 status = data->rpc_status;
1369 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1371 struct nfs4_opendata *data = calldata;
1372 struct nfs4_state_owner *sp = data->owner;
1374 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1377 * Check if we still need to send an OPEN call, or if we can use
1378 * a delegation instead.
1380 if (data->state != NULL) {
1381 struct nfs_delegation *delegation;
1383 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1386 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1387 if (delegation != NULL &&
1388 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1394 /* Update sequence id. */
1395 data->o_arg.id = sp->so_owner_id.id;
1396 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1397 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1398 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1399 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1401 data->timestamp = jiffies;
1402 if (nfs4_setup_sequence(data->o_arg.server,
1403 &data->o_arg.seq_args,
1404 &data->o_res.seq_res, 1, task))
1406 rpc_call_start(task);
1409 task->tk_action = NULL;
1413 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1415 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1416 nfs4_open_prepare(task, calldata);
1419 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1421 struct nfs4_opendata *data = calldata;
1423 data->rpc_status = task->tk_status;
1425 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1428 if (task->tk_status == 0) {
1429 switch (data->o_res.f_attr->mode & S_IFMT) {
1433 data->rpc_status = -ELOOP;
1436 data->rpc_status = -EISDIR;
1439 data->rpc_status = -ENOTDIR;
1441 renew_lease(data->o_res.server, data->timestamp);
1442 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1443 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1448 static void nfs4_open_release(void *calldata)
1450 struct nfs4_opendata *data = calldata;
1451 struct nfs4_state *state = NULL;
1453 /* If this request hasn't been cancelled, do nothing */
1454 if (data->cancelled == 0)
1456 /* In case of error, no cleanup! */
1457 if (data->rpc_status != 0 || !data->rpc_done)
1459 /* In case we need an open_confirm, no cleanup! */
1460 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1462 state = nfs4_opendata_to_nfs4_state(data);
1464 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1466 nfs4_opendata_put(data);
1469 static const struct rpc_call_ops nfs4_open_ops = {
1470 .rpc_call_prepare = nfs4_open_prepare,
1471 .rpc_call_done = nfs4_open_done,
1472 .rpc_release = nfs4_open_release,
1475 static const struct rpc_call_ops nfs4_recover_open_ops = {
1476 .rpc_call_prepare = nfs4_recover_open_prepare,
1477 .rpc_call_done = nfs4_open_done,
1478 .rpc_release = nfs4_open_release,
1481 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1483 struct inode *dir = data->dir->d_inode;
1484 struct nfs_server *server = NFS_SERVER(dir);
1485 struct nfs_openargs *o_arg = &data->o_arg;
1486 struct nfs_openres *o_res = &data->o_res;
1487 struct rpc_task *task;
1488 struct rpc_message msg = {
1489 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1492 .rpc_cred = data->owner->so_cred,
1494 struct rpc_task_setup task_setup_data = {
1495 .rpc_client = server->client,
1496 .rpc_message = &msg,
1497 .callback_ops = &nfs4_open_ops,
1498 .callback_data = data,
1499 .workqueue = nfsiod_workqueue,
1500 .flags = RPC_TASK_ASYNC,
1504 kref_get(&data->kref);
1506 data->rpc_status = 0;
1507 data->cancelled = 0;
1509 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1510 task = rpc_run_task(&task_setup_data);
1512 return PTR_ERR(task);
1513 status = nfs4_wait_for_completion_rpc_task(task);
1515 data->cancelled = 1;
1518 status = data->rpc_status;
1524 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1526 struct inode *dir = data->dir->d_inode;
1527 struct nfs_openres *o_res = &data->o_res;
1530 status = nfs4_run_open_task(data, 1);
1531 if (status != 0 || !data->rpc_done)
1534 nfs_refresh_inode(dir, o_res->dir_attr);
1536 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1537 status = _nfs4_proc_open_confirm(data);
1546 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1548 static int _nfs4_proc_open(struct nfs4_opendata *data)
1550 struct inode *dir = data->dir->d_inode;
1551 struct nfs_server *server = NFS_SERVER(dir);
1552 struct nfs_openargs *o_arg = &data->o_arg;
1553 struct nfs_openres *o_res = &data->o_res;
1556 status = nfs4_run_open_task(data, 0);
1557 if (status != 0 || !data->rpc_done)
1560 if (o_arg->open_flags & O_CREAT) {
1561 update_changeattr(dir, &o_res->cinfo);
1562 nfs_post_op_update_inode(dir, o_res->dir_attr);
1564 nfs_refresh_inode(dir, o_res->dir_attr);
1565 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1566 server->caps &= ~NFS_CAP_POSIX_LOCK;
1567 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1568 status = _nfs4_proc_open_confirm(data);
1572 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1573 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1577 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1582 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1583 ret = nfs4_wait_clnt_recover(clp);
1586 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1587 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1589 nfs4_schedule_state_manager(clp);
1595 static int nfs4_recover_expired_lease(struct nfs_server *server)
1597 return nfs4_client_recover_expired_lease(server->nfs_client);
1602 * reclaim state on the server after a network partition.
1603 * Assumes caller holds the appropriate lock
1605 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1607 struct nfs4_opendata *opendata;
1610 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1611 if (IS_ERR(opendata))
1612 return PTR_ERR(opendata);
1613 ret = nfs4_open_recover(opendata, state);
1615 d_drop(ctx->path.dentry);
1616 nfs4_opendata_put(opendata);
1620 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1622 struct nfs_server *server = NFS_SERVER(state->inode);
1623 struct nfs4_exception exception = { };
1627 err = _nfs4_open_expired(ctx, state);
1631 case -NFS4ERR_GRACE:
1632 case -NFS4ERR_DELAY:
1633 nfs4_handle_exception(server, err, &exception);
1636 } while (exception.retry);
1641 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1643 struct nfs_open_context *ctx;
1646 ctx = nfs4_state_find_open_context(state);
1648 return PTR_ERR(ctx);
1649 ret = nfs4_do_open_expired(ctx, state);
1650 put_nfs_open_context(ctx);
1655 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1656 * fields corresponding to attributes that were used to store the verifier.
1657 * Make sure we clobber those fields in the later setattr call
1659 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1661 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1662 !(sattr->ia_valid & ATTR_ATIME_SET))
1663 sattr->ia_valid |= ATTR_ATIME;
1665 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1666 !(sattr->ia_valid & ATTR_MTIME_SET))
1667 sattr->ia_valid |= ATTR_MTIME;
1671 * Returns a referenced nfs4_state
1673 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)
1675 struct nfs4_state_owner *sp;
1676 struct nfs4_state *state = NULL;
1677 struct nfs_server *server = NFS_SERVER(dir);
1678 struct nfs4_opendata *opendata;
1681 /* Protect against reboot recovery conflicts */
1683 if (!(sp = nfs4_get_state_owner(server, cred))) {
1684 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1687 status = nfs4_recover_expired_lease(server);
1689 goto err_put_state_owner;
1690 if (path->dentry->d_inode != NULL)
1691 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1693 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
1694 if (opendata == NULL)
1695 goto err_put_state_owner;
1697 if (path->dentry->d_inode != NULL)
1698 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1700 status = _nfs4_proc_open(opendata);
1702 goto err_opendata_put;
1704 state = nfs4_opendata_to_nfs4_state(opendata);
1705 status = PTR_ERR(state);
1707 goto err_opendata_put;
1708 if (server->caps & NFS_CAP_POSIX_LOCK)
1709 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1711 if (opendata->o_arg.open_flags & O_EXCL) {
1712 nfs4_exclusive_attrset(opendata, sattr);
1714 nfs_fattr_init(opendata->o_res.f_attr);
1715 status = nfs4_do_setattr(state->inode, cred,
1716 opendata->o_res.f_attr, sattr,
1719 nfs_setattr_update_inode(state->inode, sattr);
1720 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1722 nfs4_opendata_put(opendata);
1723 nfs4_put_state_owner(sp);
1727 nfs4_opendata_put(opendata);
1728 err_put_state_owner:
1729 nfs4_put_state_owner(sp);
1736 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)
1738 struct nfs4_exception exception = { };
1739 struct nfs4_state *res;
1743 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1746 /* NOTE: BAD_SEQID means the server and client disagree about the
1747 * book-keeping w.r.t. state-changing operations
1748 * (OPEN/CLOSE/LOCK/LOCKU...)
1749 * It is actually a sign of a bug on the client or on the server.
1751 * If we receive a BAD_SEQID error in the particular case of
1752 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1753 * have unhashed the old state_owner for us, and that we can
1754 * therefore safely retry using a new one. We should still warn
1755 * the user though...
1757 if (status == -NFS4ERR_BAD_SEQID) {
1758 printk(KERN_WARNING "NFS: v4 server %s "
1759 " returned a bad sequence-id error!\n",
1760 NFS_SERVER(dir)->nfs_client->cl_hostname);
1761 exception.retry = 1;
1765 * BAD_STATEID on OPEN means that the server cancelled our
1766 * state before it received the OPEN_CONFIRM.
1767 * Recover by retrying the request as per the discussion
1768 * on Page 181 of RFC3530.
1770 if (status == -NFS4ERR_BAD_STATEID) {
1771 exception.retry = 1;
1774 if (status == -EAGAIN) {
1775 /* We must have found a delegation */
1776 exception.retry = 1;
1779 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1780 status, &exception));
1781 } while (exception.retry);
1785 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1786 struct nfs_fattr *fattr, struct iattr *sattr,
1787 struct nfs4_state *state)
1789 struct nfs_server *server = NFS_SERVER(inode);
1790 struct nfs_setattrargs arg = {
1791 .fh = NFS_FH(inode),
1794 .bitmask = server->attr_bitmask,
1796 struct nfs_setattrres res = {
1800 struct rpc_message msg = {
1801 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1806 unsigned long timestamp = jiffies;
1809 nfs_fattr_init(fattr);
1811 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1812 /* Use that stateid */
1813 } else if (state != NULL) {
1814 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
1816 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1818 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
1819 if (status == 0 && state != NULL)
1820 renew_lease(server, timestamp);
1824 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1825 struct nfs_fattr *fattr, struct iattr *sattr,
1826 struct nfs4_state *state)
1828 struct nfs_server *server = NFS_SERVER(inode);
1829 struct nfs4_exception exception = { };
1832 err = nfs4_handle_exception(server,
1833 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1835 } while (exception.retry);
1839 struct nfs4_closedata {
1841 struct inode *inode;
1842 struct nfs4_state *state;
1843 struct nfs_closeargs arg;
1844 struct nfs_closeres res;
1845 struct nfs_fattr fattr;
1846 unsigned long timestamp;
1851 static void nfs4_free_closedata(void *data)
1853 struct nfs4_closedata *calldata = data;
1854 struct nfs4_state_owner *sp = calldata->state->owner;
1857 pnfs_roc_release(calldata->state->inode);
1858 nfs4_put_open_state(calldata->state);
1859 nfs_free_seqid(calldata->arg.seqid);
1860 nfs4_put_state_owner(sp);
1861 path_put(&calldata->path);
1865 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1868 spin_lock(&state->owner->so_lock);
1869 if (!(fmode & FMODE_READ))
1870 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1871 if (!(fmode & FMODE_WRITE))
1872 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1873 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1874 spin_unlock(&state->owner->so_lock);
1877 static void nfs4_close_done(struct rpc_task *task, void *data)
1879 struct nfs4_closedata *calldata = data;
1880 struct nfs4_state *state = calldata->state;
1881 struct nfs_server *server = NFS_SERVER(calldata->inode);
1883 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1885 /* hmm. we are done with the inode, and in the process of freeing
1886 * the state_owner. we keep this around to process errors
1888 switch (task->tk_status) {
1891 pnfs_roc_set_barrier(state->inode,
1892 calldata->roc_barrier);
1893 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1894 renew_lease(server, calldata->timestamp);
1895 nfs4_close_clear_stateid_flags(state,
1896 calldata->arg.fmode);
1898 case -NFS4ERR_STALE_STATEID:
1899 case -NFS4ERR_OLD_STATEID:
1900 case -NFS4ERR_BAD_STATEID:
1901 case -NFS4ERR_EXPIRED:
1902 if (calldata->arg.fmode == 0)
1905 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1906 rpc_restart_call_prepare(task);
1908 nfs_release_seqid(calldata->arg.seqid);
1909 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1912 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1914 struct nfs4_closedata *calldata = data;
1915 struct nfs4_state *state = calldata->state;
1918 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1921 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1922 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
1923 spin_lock(&state->owner->so_lock);
1924 /* Calculate the change in open mode */
1925 if (state->n_rdwr == 0) {
1926 if (state->n_rdonly == 0) {
1927 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1928 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1929 calldata->arg.fmode &= ~FMODE_READ;
1931 if (state->n_wronly == 0) {
1932 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1933 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1934 calldata->arg.fmode &= ~FMODE_WRITE;
1937 spin_unlock(&state->owner->so_lock);
1940 /* Note: exit _without_ calling nfs4_close_done */
1941 task->tk_action = NULL;
1945 if (calldata->arg.fmode == 0) {
1946 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1947 if (calldata->roc &&
1948 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
1949 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
1955 nfs_fattr_init(calldata->res.fattr);
1956 calldata->timestamp = jiffies;
1957 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
1958 &calldata->arg.seq_args, &calldata->res.seq_res,
1961 rpc_call_start(task);
1964 static const struct rpc_call_ops nfs4_close_ops = {
1965 .rpc_call_prepare = nfs4_close_prepare,
1966 .rpc_call_done = nfs4_close_done,
1967 .rpc_release = nfs4_free_closedata,
1971 * It is possible for data to be read/written from a mem-mapped file
1972 * after the sys_close call (which hits the vfs layer as a flush).
1973 * This means that we can't safely call nfsv4 close on a file until
1974 * the inode is cleared. This in turn means that we are not good
1975 * NFSv4 citizens - we do not indicate to the server to update the file's
1976 * share state even when we are done with one of the three share
1977 * stateid's in the inode.
1979 * NOTE: Caller must be holding the sp->so_owner semaphore!
1981 int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
1983 struct nfs_server *server = NFS_SERVER(state->inode);
1984 struct nfs4_closedata *calldata;
1985 struct nfs4_state_owner *sp = state->owner;
1986 struct rpc_task *task;
1987 struct rpc_message msg = {
1988 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1989 .rpc_cred = state->owner->so_cred,
1991 struct rpc_task_setup task_setup_data = {
1992 .rpc_client = server->client,
1993 .rpc_message = &msg,
1994 .callback_ops = &nfs4_close_ops,
1995 .workqueue = nfsiod_workqueue,
1996 .flags = RPC_TASK_ASYNC,
1998 int status = -ENOMEM;
2000 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2001 if (calldata == NULL)
2003 calldata->inode = state->inode;
2004 calldata->state = state;
2005 calldata->arg.fh = NFS_FH(state->inode);
2006 calldata->arg.stateid = &state->open_stateid;
2007 /* Serialization for the sequence id */
2008 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2009 if (calldata->arg.seqid == NULL)
2010 goto out_free_calldata;
2011 calldata->arg.fmode = 0;
2012 calldata->arg.bitmask = server->cache_consistency_bitmask;
2013 calldata->res.fattr = &calldata->fattr;
2014 calldata->res.seqid = calldata->arg.seqid;
2015 calldata->res.server = server;
2016 calldata->roc = roc;
2018 calldata->path = *path;
2020 msg.rpc_argp = &calldata->arg;
2021 msg.rpc_resp = &calldata->res;
2022 task_setup_data.callback_data = calldata;
2023 task = rpc_run_task(&task_setup_data);
2025 return PTR_ERR(task);
2028 status = rpc_wait_for_completion_task(task);
2035 pnfs_roc_release(state->inode);
2036 nfs4_put_open_state(state);
2037 nfs4_put_state_owner(sp);
2041 static struct inode *
2042 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2044 struct nfs4_state *state;
2046 /* Protect against concurrent sillydeletes */
2047 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
2049 return ERR_CAST(state);
2051 return igrab(state->inode);
2054 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2056 if (ctx->state == NULL)
2059 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2061 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2064 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2066 struct nfs4_server_caps_arg args = {
2069 struct nfs4_server_caps_res res = {};
2070 struct rpc_message msg = {
2071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2077 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2079 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2080 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2081 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2082 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2083 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2084 NFS_CAP_CTIME|NFS_CAP_MTIME);
2085 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2086 server->caps |= NFS_CAP_ACLS;
2087 if (res.has_links != 0)
2088 server->caps |= NFS_CAP_HARDLINKS;
2089 if (res.has_symlinks != 0)
2090 server->caps |= NFS_CAP_SYMLINKS;
2091 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2092 server->caps |= NFS_CAP_FILEID;
2093 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2094 server->caps |= NFS_CAP_MODE;
2095 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2096 server->caps |= NFS_CAP_NLINK;
2097 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2098 server->caps |= NFS_CAP_OWNER;
2099 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2100 server->caps |= NFS_CAP_OWNER_GROUP;
2101 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2102 server->caps |= NFS_CAP_ATIME;
2103 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2104 server->caps |= NFS_CAP_CTIME;
2105 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2106 server->caps |= NFS_CAP_MTIME;
2108 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2109 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2110 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2111 server->acl_bitmask = res.acl_bitmask;
2117 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2119 struct nfs4_exception exception = { };
2122 err = nfs4_handle_exception(server,
2123 _nfs4_server_capabilities(server, fhandle),
2125 } while (exception.retry);
2129 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2130 struct nfs_fsinfo *info)
2132 struct nfs4_lookup_root_arg args = {
2133 .bitmask = nfs4_fattr_bitmap,
2135 struct nfs4_lookup_res res = {
2137 .fattr = info->fattr,
2140 struct rpc_message msg = {
2141 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2146 nfs_fattr_init(info->fattr);
2147 return nfs4_call_sync(server, &msg, &args, &res, 0);
2150 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2151 struct nfs_fsinfo *info)
2153 struct nfs4_exception exception = { };
2156 err = nfs4_handle_exception(server,
2157 _nfs4_lookup_root(server, fhandle, info),
2159 } while (exception.retry);
2164 * get the file handle for the "/" directory on the server
2166 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2167 struct nfs_fsinfo *info)
2171 status = nfs4_lookup_root(server, fhandle, info);
2173 status = nfs4_server_capabilities(server, fhandle);
2175 status = nfs4_do_fsinfo(server, fhandle, info);
2176 return nfs4_map_errors(status);
2180 * Get locations and (maybe) other attributes of a referral.
2181 * Note that we'll actually follow the referral later when
2182 * we detect fsid mismatch in inode revalidation
2184 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2186 int status = -ENOMEM;
2187 struct page *page = NULL;
2188 struct nfs4_fs_locations *locations = NULL;
2190 page = alloc_page(GFP_KERNEL);
2193 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2194 if (locations == NULL)
2197 status = nfs4_proc_fs_locations(dir, name, locations, page);
2200 /* Make sure server returned a different fsid for the referral */
2201 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2202 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
2207 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2208 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2210 fattr->mode = S_IFDIR;
2211 memset(fhandle, 0, sizeof(struct nfs_fh));
2219 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2221 struct nfs4_getattr_arg args = {
2223 .bitmask = server->attr_bitmask,
2225 struct nfs4_getattr_res res = {
2229 struct rpc_message msg = {
2230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2235 nfs_fattr_init(fattr);
2236 return nfs4_call_sync(server, &msg, &args, &res, 0);
2239 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2241 struct nfs4_exception exception = { };
2244 err = nfs4_handle_exception(server,
2245 _nfs4_proc_getattr(server, fhandle, fattr),
2247 } while (exception.retry);
2252 * The file is not closed if it is opened due to the a request to change
2253 * the size of the file. The open call will not be needed once the
2254 * VFS layer lookup-intents are implemented.
2256 * Close is called when the inode is destroyed.
2257 * If we haven't opened the file for O_WRONLY, we
2258 * need to in the size_change case to obtain a stateid.
2261 * Because OPEN is always done by name in nfsv4, it is
2262 * possible that we opened a different file by the same
2263 * name. We can recognize this race condition, but we
2264 * can't do anything about it besides returning an error.
2266 * This will be fixed with VFS changes (lookup-intent).
2269 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2270 struct iattr *sattr)
2272 struct inode *inode = dentry->d_inode;
2273 struct rpc_cred *cred = NULL;
2274 struct nfs4_state *state = NULL;
2277 nfs_fattr_init(fattr);
2279 /* Search for an existing open(O_WRITE) file */
2280 if (sattr->ia_valid & ATTR_FILE) {
2281 struct nfs_open_context *ctx;
2283 ctx = nfs_file_open_context(sattr->ia_file);
2290 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2292 nfs_setattr_update_inode(inode, sattr);
2296 static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2297 const struct qstr *name, struct nfs_fh *fhandle,
2298 struct nfs_fattr *fattr)
2301 struct nfs4_lookup_arg args = {
2302 .bitmask = server->attr_bitmask,
2306 struct nfs4_lookup_res res = {
2311 struct rpc_message msg = {
2312 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2317 nfs_fattr_init(fattr);
2319 dprintk("NFS call lookupfh %s\n", name->name);
2320 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2321 dprintk("NFS reply lookupfh: %d\n", status);
2325 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2326 struct qstr *name, struct nfs_fh *fhandle,
2327 struct nfs_fattr *fattr)
2329 struct nfs4_exception exception = { };
2332 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2334 if (err == -NFS4ERR_MOVED) {
2338 err = nfs4_handle_exception(server, err, &exception);
2339 } while (exception.retry);
2343 static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
2344 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2348 dprintk("NFS call lookup %s\n", name->name);
2349 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2350 if (status == -NFS4ERR_MOVED)
2351 status = nfs4_get_referral(dir, name, fattr, fhandle);
2352 dprintk("NFS reply lookup: %d\n", status);
2356 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2358 struct nfs4_exception exception = { };
2361 err = nfs4_handle_exception(NFS_SERVER(dir),
2362 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2364 } while (exception.retry);
2368 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2370 struct nfs_server *server = NFS_SERVER(inode);
2371 struct nfs4_accessargs args = {
2372 .fh = NFS_FH(inode),
2373 .bitmask = server->attr_bitmask,
2375 struct nfs4_accessres res = {
2378 struct rpc_message msg = {
2379 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2382 .rpc_cred = entry->cred,
2384 int mode = entry->mask;
2388 * Determine which access bits we want to ask for...
2390 if (mode & MAY_READ)
2391 args.access |= NFS4_ACCESS_READ;
2392 if (S_ISDIR(inode->i_mode)) {
2393 if (mode & MAY_WRITE)
2394 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2395 if (mode & MAY_EXEC)
2396 args.access |= NFS4_ACCESS_LOOKUP;
2398 if (mode & MAY_WRITE)
2399 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2400 if (mode & MAY_EXEC)
2401 args.access |= NFS4_ACCESS_EXECUTE;
2404 res.fattr = nfs_alloc_fattr();
2405 if (res.fattr == NULL)
2408 status = nfs4_call_sync(server, &msg, &args, &res, 0);
2411 if (res.access & NFS4_ACCESS_READ)
2412 entry->mask |= MAY_READ;
2413 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2414 entry->mask |= MAY_WRITE;
2415 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2416 entry->mask |= MAY_EXEC;
2417 nfs_refresh_inode(inode, res.fattr);
2419 nfs_free_fattr(res.fattr);
2423 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2425 struct nfs4_exception exception = { };
2428 err = nfs4_handle_exception(NFS_SERVER(inode),
2429 _nfs4_proc_access(inode, entry),
2431 } while (exception.retry);
2436 * TODO: For the time being, we don't try to get any attributes
2437 * along with any of the zero-copy operations READ, READDIR,
2440 * In the case of the first three, we want to put the GETATTR
2441 * after the read-type operation -- this is because it is hard
2442 * to predict the length of a GETATTR response in v4, and thus
2443 * align the READ data correctly. This means that the GETATTR
2444 * may end up partially falling into the page cache, and we should
2445 * shift it into the 'tail' of the xdr_buf before processing.
2446 * To do this efficiently, we need to know the total length
2447 * of data received, which doesn't seem to be available outside
2450 * In the case of WRITE, we also want to put the GETATTR after
2451 * the operation -- in this case because we want to make sure
2452 * we get the post-operation mtime and size. This means that
2453 * we can't use xdr_encode_pages() as written: we need a variant
2454 * of it which would leave room in the 'tail' iovec.
2456 * Both of these changes to the XDR layer would in fact be quite
2457 * minor, but I decided to leave them for a subsequent patch.
2459 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2460 unsigned int pgbase, unsigned int pglen)
2462 struct nfs4_readlink args = {
2463 .fh = NFS_FH(inode),
2468 struct nfs4_readlink_res res;
2469 struct rpc_message msg = {
2470 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2475 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
2478 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2479 unsigned int pgbase, unsigned int pglen)
2481 struct nfs4_exception exception = { };
2484 err = nfs4_handle_exception(NFS_SERVER(inode),
2485 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2487 } while (exception.retry);
2493 * We will need to arrange for the VFS layer to provide an atomic open.
2494 * Until then, this create/open method is prone to inefficiency and race
2495 * conditions due to the lookup, create, and open VFS calls from sys_open()
2496 * placed on the wire.
2498 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2499 * The file will be opened again in the subsequent VFS open call
2500 * (nfs4_proc_file_open).
2502 * The open for read will just hang around to be used by any process that
2503 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2507 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2508 int flags, struct nfs_open_context *ctx)
2510 struct path my_path = {
2513 struct path *path = &my_path;
2514 struct nfs4_state *state;
2515 struct rpc_cred *cred = NULL;
2524 sattr->ia_mode &= ~current_umask();
2525 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
2527 if (IS_ERR(state)) {
2528 status = PTR_ERR(state);
2531 d_add(dentry, igrab(state->inode));
2532 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2536 nfs4_close_sync(path, state, fmode);
2541 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2543 struct nfs_server *server = NFS_SERVER(dir);
2544 struct nfs_removeargs args = {
2546 .name.len = name->len,
2547 .name.name = name->name,
2548 .bitmask = server->attr_bitmask,
2550 struct nfs_removeres res = {
2553 struct rpc_message msg = {
2554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2558 int status = -ENOMEM;
2560 res.dir_attr = nfs_alloc_fattr();
2561 if (res.dir_attr == NULL)
2564 status = nfs4_call_sync(server, &msg, &args, &res, 1);
2566 update_changeattr(dir, &res.cinfo);
2567 nfs_post_op_update_inode(dir, res.dir_attr);
2569 nfs_free_fattr(res.dir_attr);
2574 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2576 struct nfs4_exception exception = { };
2579 err = nfs4_handle_exception(NFS_SERVER(dir),
2580 _nfs4_proc_remove(dir, name),
2582 } while (exception.retry);
2586 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2588 struct nfs_server *server = NFS_SERVER(dir);
2589 struct nfs_removeargs *args = msg->rpc_argp;
2590 struct nfs_removeres *res = msg->rpc_resp;
2592 args->bitmask = server->cache_consistency_bitmask;
2593 res->server = server;
2594 res->seq_res.sr_slot = NULL;
2595 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2598 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2600 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2602 if (!nfs4_sequence_done(task, &res->seq_res))
2604 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2606 update_changeattr(dir, &res->cinfo);
2607 nfs_post_op_update_inode(dir, res->dir_attr);
2611 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2613 struct nfs_server *server = NFS_SERVER(dir);
2614 struct nfs_renameargs *arg = msg->rpc_argp;
2615 struct nfs_renameres *res = msg->rpc_resp;
2617 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2618 arg->bitmask = server->attr_bitmask;
2619 res->server = server;
2622 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2623 struct inode *new_dir)
2625 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2627 if (!nfs4_sequence_done(task, &res->seq_res))
2629 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2632 update_changeattr(old_dir, &res->old_cinfo);
2633 nfs_post_op_update_inode(old_dir, res->old_fattr);
2634 update_changeattr(new_dir, &res->new_cinfo);
2635 nfs_post_op_update_inode(new_dir, res->new_fattr);
2639 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2640 struct inode *new_dir, struct qstr *new_name)
2642 struct nfs_server *server = NFS_SERVER(old_dir);
2643 struct nfs_renameargs arg = {
2644 .old_dir = NFS_FH(old_dir),
2645 .new_dir = NFS_FH(new_dir),
2646 .old_name = old_name,
2647 .new_name = new_name,
2648 .bitmask = server->attr_bitmask,
2650 struct nfs_renameres res = {
2653 struct rpc_message msg = {
2654 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2658 int status = -ENOMEM;
2660 res.old_fattr = nfs_alloc_fattr();
2661 res.new_fattr = nfs_alloc_fattr();
2662 if (res.old_fattr == NULL || res.new_fattr == NULL)
2665 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2667 update_changeattr(old_dir, &res.old_cinfo);
2668 nfs_post_op_update_inode(old_dir, res.old_fattr);
2669 update_changeattr(new_dir, &res.new_cinfo);
2670 nfs_post_op_update_inode(new_dir, res.new_fattr);
2673 nfs_free_fattr(res.new_fattr);
2674 nfs_free_fattr(res.old_fattr);
2678 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2679 struct inode *new_dir, struct qstr *new_name)
2681 struct nfs4_exception exception = { };
2684 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2685 _nfs4_proc_rename(old_dir, old_name,
2688 } while (exception.retry);
2692 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2694 struct nfs_server *server = NFS_SERVER(inode);
2695 struct nfs4_link_arg arg = {
2696 .fh = NFS_FH(inode),
2697 .dir_fh = NFS_FH(dir),
2699 .bitmask = server->attr_bitmask,
2701 struct nfs4_link_res res = {
2704 struct rpc_message msg = {
2705 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2709 int status = -ENOMEM;
2711 res.fattr = nfs_alloc_fattr();
2712 res.dir_attr = nfs_alloc_fattr();
2713 if (res.fattr == NULL || res.dir_attr == NULL)
2716 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
2718 update_changeattr(dir, &res.cinfo);
2719 nfs_post_op_update_inode(dir, res.dir_attr);
2720 nfs_post_op_update_inode(inode, res.fattr);
2723 nfs_free_fattr(res.dir_attr);
2724 nfs_free_fattr(res.fattr);
2728 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2730 struct nfs4_exception exception = { };
2733 err = nfs4_handle_exception(NFS_SERVER(inode),
2734 _nfs4_proc_link(inode, dir, name),
2736 } while (exception.retry);
2740 struct nfs4_createdata {
2741 struct rpc_message msg;
2742 struct nfs4_create_arg arg;
2743 struct nfs4_create_res res;
2745 struct nfs_fattr fattr;
2746 struct nfs_fattr dir_fattr;
2749 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2750 struct qstr *name, struct iattr *sattr, u32 ftype)
2752 struct nfs4_createdata *data;
2754 data = kzalloc(sizeof(*data), GFP_KERNEL);
2756 struct nfs_server *server = NFS_SERVER(dir);
2758 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2759 data->msg.rpc_argp = &data->arg;
2760 data->msg.rpc_resp = &data->res;
2761 data->arg.dir_fh = NFS_FH(dir);
2762 data->arg.server = server;
2763 data->arg.name = name;
2764 data->arg.attrs = sattr;
2765 data->arg.ftype = ftype;
2766 data->arg.bitmask = server->attr_bitmask;
2767 data->res.server = server;
2768 data->res.fh = &data->fh;
2769 data->res.fattr = &data->fattr;
2770 data->res.dir_fattr = &data->dir_fattr;
2771 nfs_fattr_init(data->res.fattr);
2772 nfs_fattr_init(data->res.dir_fattr);
2777 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2779 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2780 &data->arg, &data->res, 1);
2782 update_changeattr(dir, &data->res.dir_cinfo);
2783 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2784 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2789 static void nfs4_free_createdata(struct nfs4_createdata *data)
2794 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2795 struct page *page, unsigned int len, struct iattr *sattr)
2797 struct nfs4_createdata *data;
2798 int status = -ENAMETOOLONG;
2800 if (len > NFS4_MAXPATHLEN)
2804 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2808 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2809 data->arg.u.symlink.pages = &page;
2810 data->arg.u.symlink.len = len;
2812 status = nfs4_do_create(dir, dentry, data);
2814 nfs4_free_createdata(data);
2819 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2820 struct page *page, unsigned int len, struct iattr *sattr)
2822 struct nfs4_exception exception = { };
2825 err = nfs4_handle_exception(NFS_SERVER(dir),
2826 _nfs4_proc_symlink(dir, dentry, page,
2829 } while (exception.retry);
2833 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2834 struct iattr *sattr)
2836 struct nfs4_createdata *data;
2837 int status = -ENOMEM;
2839 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2843 status = nfs4_do_create(dir, dentry, data);
2845 nfs4_free_createdata(data);
2850 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2851 struct iattr *sattr)
2853 struct nfs4_exception exception = { };
2856 sattr->ia_mode &= ~current_umask();
2858 err = nfs4_handle_exception(NFS_SERVER(dir),
2859 _nfs4_proc_mkdir(dir, dentry, sattr),
2861 } while (exception.retry);
2865 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2866 u64 cookie, struct page **pages, unsigned int count, int plus)
2868 struct inode *dir = dentry->d_inode;
2869 struct nfs4_readdir_arg args = {
2874 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2877 struct nfs4_readdir_res res;
2878 struct rpc_message msg = {
2879 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2886 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2887 dentry->d_parent->d_name.name,
2888 dentry->d_name.name,
2889 (unsigned long long)cookie);
2890 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2891 res.pgbase = args.pgbase;
2892 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
2894 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2895 status += args.pgbase;
2898 nfs_invalidate_atime(dir);
2900 dprintk("%s: returns %d\n", __func__, status);
2904 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2905 u64 cookie, struct page **pages, unsigned int count, int plus)
2907 struct nfs4_exception exception = { };
2910 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2911 _nfs4_proc_readdir(dentry, cred, cookie,
2912 pages, count, plus),
2914 } while (exception.retry);
2918 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2919 struct iattr *sattr, dev_t rdev)
2921 struct nfs4_createdata *data;
2922 int mode = sattr->ia_mode;
2923 int status = -ENOMEM;
2925 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2926 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
2928 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2933 data->arg.ftype = NF4FIFO;
2934 else if (S_ISBLK(mode)) {
2935 data->arg.ftype = NF4BLK;
2936 data->arg.u.device.specdata1 = MAJOR(rdev);
2937 data->arg.u.device.specdata2 = MINOR(rdev);
2939 else if (S_ISCHR(mode)) {
2940 data->arg.ftype = NF4CHR;
2941 data->arg.u.device.specdata1 = MAJOR(rdev);
2942 data->arg.u.device.specdata2 = MINOR(rdev);
2945 status = nfs4_do_create(dir, dentry, data);
2947 nfs4_free_createdata(data);
2952 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2953 struct iattr *sattr, dev_t rdev)
2955 struct nfs4_exception exception = { };
2958 sattr->ia_mode &= ~current_umask();
2960 err = nfs4_handle_exception(NFS_SERVER(dir),
2961 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2963 } while (exception.retry);
2967 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2968 struct nfs_fsstat *fsstat)
2970 struct nfs4_statfs_arg args = {
2972 .bitmask = server->attr_bitmask,
2974 struct nfs4_statfs_res res = {
2977 struct rpc_message msg = {
2978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2983 nfs_fattr_init(fsstat->fattr);
2984 return nfs4_call_sync(server, &msg, &args, &res, 0);
2987 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2989 struct nfs4_exception exception = { };
2992 err = nfs4_handle_exception(server,
2993 _nfs4_proc_statfs(server, fhandle, fsstat),
2995 } while (exception.retry);
2999 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3000 struct nfs_fsinfo *fsinfo)
3002 struct nfs4_fsinfo_arg args = {
3004 .bitmask = server->attr_bitmask,
3006 struct nfs4_fsinfo_res res = {
3009 struct rpc_message msg = {
3010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3015 return nfs4_call_sync(server, &msg, &args, &res, 0);
3018 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3020 struct nfs4_exception exception = { };
3024 err = nfs4_handle_exception(server,
3025 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3027 } while (exception.retry);
3031 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3033 nfs_fattr_init(fsinfo->fattr);
3034 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3037 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3038 struct nfs_pathconf *pathconf)
3040 struct nfs4_pathconf_arg args = {
3042 .bitmask = server->attr_bitmask,
3044 struct nfs4_pathconf_res res = {
3045 .pathconf = pathconf,
3047 struct rpc_message msg = {
3048 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3053 /* None of the pathconf attributes are mandatory to implement */
3054 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3055 memset(pathconf, 0, sizeof(*pathconf));
3059 nfs_fattr_init(pathconf->fattr);
3060 return nfs4_call_sync(server, &msg, &args, &res, 0);
3063 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3064 struct nfs_pathconf *pathconf)
3066 struct nfs4_exception exception = { };
3070 err = nfs4_handle_exception(server,
3071 _nfs4_proc_pathconf(server, fhandle, pathconf),
3073 } while (exception.retry);
3077 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3079 struct nfs_server *server = NFS_SERVER(data->inode);
3081 dprintk("--> %s\n", __func__);
3083 if (!nfs4_sequence_done(task, &data->res.seq_res))
3086 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3087 nfs_restart_rpc(task, server->nfs_client);
3091 nfs_invalidate_atime(data->inode);
3092 if (task->tk_status > 0)
3093 renew_lease(server, data->timestamp);
3097 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3099 data->timestamp = jiffies;
3100 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3103 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3105 struct inode *inode = data->inode;
3107 if (!nfs4_sequence_done(task, &data->res.seq_res))
3110 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3111 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3114 if (task->tk_status >= 0) {
3115 renew_lease(NFS_SERVER(inode), data->timestamp);
3116 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3121 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3123 struct nfs_server *server = NFS_SERVER(data->inode);
3125 data->args.bitmask = server->cache_consistency_bitmask;
3126 data->res.server = server;
3127 data->timestamp = jiffies;
3129 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3132 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3134 struct inode *inode = data->inode;
3136 if (!nfs4_sequence_done(task, &data->res.seq_res))
3139 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3140 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3143 nfs_refresh_inode(inode, data->res.fattr);
3147 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3149 struct nfs_server *server = NFS_SERVER(data->inode);
3151 data->args.bitmask = server->cache_consistency_bitmask;
3152 data->res.server = server;
3153 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3156 struct nfs4_renewdata {
3157 struct nfs_client *client;
3158 unsigned long timestamp;
3162 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3163 * standalone procedure for queueing an asynchronous RENEW.
3165 static void nfs4_renew_release(void *calldata)
3167 struct nfs4_renewdata *data = calldata;
3168 struct nfs_client *clp = data->client;
3170 if (atomic_read(&clp->cl_count) > 1)
3171 nfs4_schedule_state_renewal(clp);
3172 nfs_put_client(clp);
3176 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3178 struct nfs4_renewdata *data = calldata;
3179 struct nfs_client *clp = data->client;
3180 unsigned long timestamp = data->timestamp;
3182 if (task->tk_status < 0) {
3183 /* Unless we're shutting down, schedule state recovery! */
3184 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3185 nfs4_schedule_lease_recovery(clp);
3188 do_renew_lease(clp, timestamp);
3191 static const struct rpc_call_ops nfs4_renew_ops = {
3192 .rpc_call_done = nfs4_renew_done,
3193 .rpc_release = nfs4_renew_release,
3196 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3198 struct rpc_message msg = {
3199 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3203 struct nfs4_renewdata *data;
3205 if (!atomic_inc_not_zero(&clp->cl_count))
3207 data = kmalloc(sizeof(*data), GFP_KERNEL);
3211 data->timestamp = jiffies;
3212 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3213 &nfs4_renew_ops, data);
3216 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3218 struct rpc_message msg = {
3219 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3223 unsigned long now = jiffies;
3226 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3229 do_renew_lease(clp, now);
3233 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3235 return (server->caps & NFS_CAP_ACLS)
3236 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3237 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3240 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3241 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3244 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3246 static void buf_to_pages(const void *buf, size_t buflen,
3247 struct page **pages, unsigned int *pgbase)
3249 const void *p = buf;
3251 *pgbase = offset_in_page(buf);
3253 while (p < buf + buflen) {
3254 *(pages++) = virt_to_page(p);
3255 p += PAGE_CACHE_SIZE;
3259 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3260 struct page **pages, unsigned int *pgbase)
3262 struct page *newpage, **spages;
3268 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3269 newpage = alloc_page(GFP_KERNEL);
3271 if (newpage == NULL)
3273 memcpy(page_address(newpage), buf, len);
3278 } while (buflen != 0);
3284 __free_page(spages[rc-1]);
3288 struct nfs4_cached_acl {
3294 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3296 struct nfs_inode *nfsi = NFS_I(inode);
3298 spin_lock(&inode->i_lock);
3299 kfree(nfsi->nfs4_acl);
3300 nfsi->nfs4_acl = acl;
3301 spin_unlock(&inode->i_lock);
3304 static void nfs4_zap_acl_attr(struct inode *inode)
3306 nfs4_set_cached_acl(inode, NULL);
3309 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3311 struct nfs_inode *nfsi = NFS_I(inode);
3312 struct nfs4_cached_acl *acl;
3315 spin_lock(&inode->i_lock);
3316 acl = nfsi->nfs4_acl;
3319 if (buf == NULL) /* user is just asking for length */
3321 if (acl->cached == 0)
3323 ret = -ERANGE; /* see getxattr(2) man page */
3324 if (acl->len > buflen)
3326 memcpy(buf, acl->data, acl->len);
3330 spin_unlock(&inode->i_lock);
3334 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3336 struct nfs4_cached_acl *acl;
3338 if (buf && acl_len <= PAGE_SIZE) {
3339 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3343 memcpy(acl->data, buf, acl_len);
3345 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3352 nfs4_set_cached_acl(inode, acl);
3355 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3357 struct page *pages[NFS4ACL_MAXPAGES];
3358 struct nfs_getaclargs args = {
3359 .fh = NFS_FH(inode),
3363 struct nfs_getaclres res = {
3367 struct rpc_message msg = {
3368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3372 struct page *localpage = NULL;
3375 if (buflen < PAGE_SIZE) {
3376 /* As long as we're doing a round trip to the server anyway,
3377 * let's be prepared for a page of acl data. */
3378 localpage = alloc_page(GFP_KERNEL);
3379 resp_buf = page_address(localpage);
3380 if (localpage == NULL)
3382 args.acl_pages[0] = localpage;
3383 args.acl_pgbase = 0;
3384 args.acl_len = PAGE_SIZE;
3387 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3389 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
3392 if (res.acl_len > args.acl_len)
3393 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3395 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3398 if (res.acl_len > buflen)
3401 memcpy(buf, resp_buf, res.acl_len);
3406 __free_page(localpage);
3410 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3412 struct nfs4_exception exception = { };
3415 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3418 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3419 } while (exception.retry);
3423 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3425 struct nfs_server *server = NFS_SERVER(inode);
3428 if (!nfs4_server_supports_acls(server))
3430 ret = nfs_revalidate_inode(server, inode);
3433 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3434 nfs_zap_acl_cache(inode);
3435 ret = nfs4_read_cached_acl(inode, buf, buflen);
3438 return nfs4_get_acl_uncached(inode, buf, buflen);
3441 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3443 struct nfs_server *server = NFS_SERVER(inode);
3444 struct page *pages[NFS4ACL_MAXPAGES];
3445 struct nfs_setaclargs arg = {
3446 .fh = NFS_FH(inode),
3450 struct nfs_setaclres res;
3451 struct rpc_message msg = {
3452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3458 if (!nfs4_server_supports_acls(server))
3460 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3463 nfs_inode_return_delegation(inode);
3464 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
3467 * Free each page after tx, so the only ref left is
3468 * held by the network stack
3471 put_page(pages[i-1]);
3474 * Acl update can result in inode attribute update.
3475 * so mark the attribute cache invalid.
3477 spin_lock(&inode->i_lock);
3478 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3479 spin_unlock(&inode->i_lock);
3480 nfs_access_zap_cache(inode);
3481 nfs_zap_acl_cache(inode);
3485 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3487 struct nfs4_exception exception = { };
3490 err = nfs4_handle_exception(NFS_SERVER(inode),
3491 __nfs4_proc_set_acl(inode, buf, buflen),
3493 } while (exception.retry);
3498 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3500 struct nfs_client *clp = server->nfs_client;
3502 if (task->tk_status >= 0)
3504 switch(task->tk_status) {
3505 case -NFS4ERR_ADMIN_REVOKED:
3506 case -NFS4ERR_BAD_STATEID:
3507 case -NFS4ERR_OPENMODE:
3510 nfs4_schedule_stateid_recovery(server, state);
3511 goto wait_on_recovery;
3512 case -NFS4ERR_STALE_STATEID:
3513 case -NFS4ERR_STALE_CLIENTID:
3514 case -NFS4ERR_EXPIRED:
3515 nfs4_schedule_lease_recovery(clp);
3516 goto wait_on_recovery;
3517 #if defined(CONFIG_NFS_V4_1)
3518 case -NFS4ERR_BADSESSION:
3519 case -NFS4ERR_BADSLOT:
3520 case -NFS4ERR_BAD_HIGH_SLOT:
3521 case -NFS4ERR_DEADSESSION:
3522 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3523 case -NFS4ERR_SEQ_FALSE_RETRY:
3524 case -NFS4ERR_SEQ_MISORDERED:
3525 dprintk("%s ERROR %d, Reset session\n", __func__,
3527 nfs4_schedule_session_recovery(clp->cl_session);
3528 task->tk_status = 0;
3530 #endif /* CONFIG_NFS_V4_1 */
3531 case -NFS4ERR_DELAY:
3532 nfs_inc_server_stats(server, NFSIOS_DELAY);
3533 case -NFS4ERR_GRACE:
3535 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3536 task->tk_status = 0;
3538 case -NFS4ERR_OLD_STATEID:
3539 task->tk_status = 0;
3542 task->tk_status = nfs4_map_errors(task->tk_status);
3545 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3546 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3547 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3548 task->tk_status = 0;
3552 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3553 unsigned short port, struct rpc_cred *cred,
3554 struct nfs4_setclientid_res *res)
3556 nfs4_verifier sc_verifier;
3557 struct nfs4_setclientid setclientid = {
3558 .sc_verifier = &sc_verifier,
3560 .sc_cb_ident = clp->cl_cb_ident,
3562 struct rpc_message msg = {
3563 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3564 .rpc_argp = &setclientid,
3572 p = (__be32*)sc_verifier.data;
3573 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3574 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3577 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3578 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3580 rpc_peeraddr2str(clp->cl_rpcclient,
3582 rpc_peeraddr2str(clp->cl_rpcclient,
3584 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3585 clp->cl_id_uniquifier);
3586 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3587 sizeof(setclientid.sc_netid),
3588 rpc_peeraddr2str(clp->cl_rpcclient,
3589 RPC_DISPLAY_NETID));
3590 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3591 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3592 clp->cl_ipaddr, port >> 8, port & 255);
3594 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3595 if (status != -NFS4ERR_CLID_INUSE)
3600 ssleep(clp->cl_lease_time / HZ + 1);
3602 if (++clp->cl_id_uniquifier == 0)
3608 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3609 struct nfs4_setclientid_res *arg,
3610 struct rpc_cred *cred)
3612 struct nfs_fsinfo fsinfo;
3613 struct rpc_message msg = {
3614 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3616 .rpc_resp = &fsinfo,
3623 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3625 spin_lock(&clp->cl_lock);
3626 clp->cl_lease_time = fsinfo.lease_time * HZ;
3627 clp->cl_last_renewal = now;
3628 spin_unlock(&clp->cl_lock);
3633 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3634 struct nfs4_setclientid_res *arg,
3635 struct rpc_cred *cred)
3640 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
3644 case -NFS4ERR_RESOURCE:
3645 /* The IBM lawyers misread another document! */
3646 case -NFS4ERR_DELAY:
3647 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3653 struct nfs4_delegreturndata {
3654 struct nfs4_delegreturnargs args;
3655 struct nfs4_delegreturnres res;
3657 nfs4_stateid stateid;
3658 unsigned long timestamp;
3659 struct nfs_fattr fattr;
3663 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3665 struct nfs4_delegreturndata *data = calldata;
3667 if (!nfs4_sequence_done(task, &data->res.seq_res))
3670 switch (task->tk_status) {
3671 case -NFS4ERR_STALE_STATEID:
3672 case -NFS4ERR_EXPIRED:
3674 renew_lease(data->res.server, data->timestamp);
3677 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3679 nfs_restart_rpc(task, data->res.server->nfs_client);
3683 data->rpc_status = task->tk_status;
3686 static void nfs4_delegreturn_release(void *calldata)
3691 #if defined(CONFIG_NFS_V4_1)
3692 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3694 struct nfs4_delegreturndata *d_data;
3696 d_data = (struct nfs4_delegreturndata *)data;
3698 if (nfs4_setup_sequence(d_data->res.server,
3699 &d_data->args.seq_args,
3700 &d_data->res.seq_res, 1, task))
3702 rpc_call_start(task);
3704 #endif /* CONFIG_NFS_V4_1 */
3706 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3707 #if defined(CONFIG_NFS_V4_1)
3708 .rpc_call_prepare = nfs4_delegreturn_prepare,
3709 #endif /* CONFIG_NFS_V4_1 */
3710 .rpc_call_done = nfs4_delegreturn_done,
3711 .rpc_release = nfs4_delegreturn_release,
3714 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3716 struct nfs4_delegreturndata *data;
3717 struct nfs_server *server = NFS_SERVER(inode);
3718 struct rpc_task *task;
3719 struct rpc_message msg = {
3720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3723 struct rpc_task_setup task_setup_data = {
3724 .rpc_client = server->client,
3725 .rpc_message = &msg,
3726 .callback_ops = &nfs4_delegreturn_ops,
3727 .flags = RPC_TASK_ASYNC,
3731 data = kzalloc(sizeof(*data), GFP_NOFS);
3734 data->args.fhandle = &data->fh;
3735 data->args.stateid = &data->stateid;
3736 data->args.bitmask = server->attr_bitmask;
3737 nfs_copy_fh(&data->fh, NFS_FH(inode));
3738 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3739 data->res.fattr = &data->fattr;
3740 data->res.server = server;
3741 nfs_fattr_init(data->res.fattr);
3742 data->timestamp = jiffies;
3743 data->rpc_status = 0;
3745 task_setup_data.callback_data = data;
3746 msg.rpc_argp = &data->args;
3747 msg.rpc_resp = &data->res;
3748 task = rpc_run_task(&task_setup_data);
3750 return PTR_ERR(task);
3753 status = nfs4_wait_for_completion_rpc_task(task);
3756 status = data->rpc_status;
3759 nfs_refresh_inode(inode, &data->fattr);
3765 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3767 struct nfs_server *server = NFS_SERVER(inode);
3768 struct nfs4_exception exception = { };
3771 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3773 case -NFS4ERR_STALE_STATEID:
3774 case -NFS4ERR_EXPIRED:
3778 err = nfs4_handle_exception(server, err, &exception);
3779 } while (exception.retry);
3783 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3784 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3787 * sleep, with exponential backoff, and retry the LOCK operation.
3789 static unsigned long
3790 nfs4_set_lock_task_retry(unsigned long timeout)
3792 schedule_timeout_killable(timeout);
3794 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3795 return NFS4_LOCK_MAXTIMEOUT;
3799 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3801 struct inode *inode = state->inode;
3802 struct nfs_server *server = NFS_SERVER(inode);
3803 struct nfs_client *clp = server->nfs_client;
3804 struct nfs_lockt_args arg = {
3805 .fh = NFS_FH(inode),
3808 struct nfs_lockt_res res = {
3811 struct rpc_message msg = {
3812 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3815 .rpc_cred = state->owner->so_cred,
3817 struct nfs4_lock_state *lsp;
3820 arg.lock_owner.clientid = clp->cl_clientid;
3821 status = nfs4_set_lock_state(state, request);
3824 lsp = request->fl_u.nfs4_fl.owner;
3825 arg.lock_owner.id = lsp->ls_id.id;
3826 arg.lock_owner.s_dev = server->s_dev;
3827 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
3830 request->fl_type = F_UNLCK;
3832 case -NFS4ERR_DENIED:
3835 request->fl_ops->fl_release_private(request);
3840 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3842 struct nfs4_exception exception = { };
3846 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3847 _nfs4_proc_getlk(state, cmd, request),
3849 } while (exception.retry);
3853 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3856 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3858 res = posix_lock_file_wait(file, fl);
3861 res = flock_lock_file_wait(file, fl);
3869 struct nfs4_unlockdata {
3870 struct nfs_locku_args arg;
3871 struct nfs_locku_res res;
3872 struct nfs4_lock_state *lsp;
3873 struct nfs_open_context *ctx;
3874 struct file_lock fl;
3875 const struct nfs_server *server;
3876 unsigned long timestamp;
3879 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3880 struct nfs_open_context *ctx,
3881 struct nfs4_lock_state *lsp,
3882 struct nfs_seqid *seqid)
3884 struct nfs4_unlockdata *p;
3885 struct inode *inode = lsp->ls_state->inode;
3887 p = kzalloc(sizeof(*p), GFP_NOFS);
3890 p->arg.fh = NFS_FH(inode);
3892 p->arg.seqid = seqid;
3893 p->res.seqid = seqid;
3894 p->arg.stateid = &lsp->ls_stateid;
3896 atomic_inc(&lsp->ls_count);
3897 /* Ensure we don't close file until we're done freeing locks! */
3898 p->ctx = get_nfs_open_context(ctx);
3899 memcpy(&p->fl, fl, sizeof(p->fl));
3900 p->server = NFS_SERVER(inode);
3904 static void nfs4_locku_release_calldata(void *data)
3906 struct nfs4_unlockdata *calldata = data;
3907 nfs_free_seqid(calldata->arg.seqid);
3908 nfs4_put_lock_state(calldata->lsp);
3909 put_nfs_open_context(calldata->ctx);
3913 static void nfs4_locku_done(struct rpc_task *task, void *data)
3915 struct nfs4_unlockdata *calldata = data;
3917 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3919 switch (task->tk_status) {
3921 memcpy(calldata->lsp->ls_stateid.data,
3922 calldata->res.stateid.data,
3923 sizeof(calldata->lsp->ls_stateid.data));
3924 renew_lease(calldata->server, calldata->timestamp);
3926 case -NFS4ERR_BAD_STATEID:
3927 case -NFS4ERR_OLD_STATEID:
3928 case -NFS4ERR_STALE_STATEID:
3929 case -NFS4ERR_EXPIRED:
3932 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
3933 nfs_restart_rpc(task,
3934 calldata->server->nfs_client);
3938 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
3940 struct nfs4_unlockdata *calldata = data;
3942 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3944 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
3945 /* Note: exit _without_ running nfs4_locku_done */
3946 task->tk_action = NULL;
3949 calldata->timestamp = jiffies;
3950 if (nfs4_setup_sequence(calldata->server,
3951 &calldata->arg.seq_args,
3952 &calldata->res.seq_res, 1, task))
3954 rpc_call_start(task);
3957 static const struct rpc_call_ops nfs4_locku_ops = {
3958 .rpc_call_prepare = nfs4_locku_prepare,
3959 .rpc_call_done = nfs4_locku_done,
3960 .rpc_release = nfs4_locku_release_calldata,
3963 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3964 struct nfs_open_context *ctx,
3965 struct nfs4_lock_state *lsp,
3966 struct nfs_seqid *seqid)
3968 struct nfs4_unlockdata *data;
3969 struct rpc_message msg = {
3970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3971 .rpc_cred = ctx->cred,
3973 struct rpc_task_setup task_setup_data = {
3974 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
3975 .rpc_message = &msg,
3976 .callback_ops = &nfs4_locku_ops,
3977 .workqueue = nfsiod_workqueue,
3978 .flags = RPC_TASK_ASYNC,
3981 /* Ensure this is an unlock - when canceling a lock, the
3982 * canceled lock is passed in, and it won't be an unlock.
3984 fl->fl_type = F_UNLCK;
3986 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3988 nfs_free_seqid(seqid);
3989 return ERR_PTR(-ENOMEM);
3992 msg.rpc_argp = &data->arg;
3993 msg.rpc_resp = &data->res;
3994 task_setup_data.callback_data = data;
3995 return rpc_run_task(&task_setup_data);
3998 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4000 struct nfs_inode *nfsi = NFS_I(state->inode);
4001 struct nfs_seqid *seqid;
4002 struct nfs4_lock_state *lsp;
4003 struct rpc_task *task;
4005 unsigned char fl_flags = request->fl_flags;
4007 status = nfs4_set_lock_state(state, request);
4008 /* Unlock _before_ we do the RPC call */
4009 request->fl_flags |= FL_EXISTS;
4010 down_read(&nfsi->rwsem);
4011 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4012 up_read(&nfsi->rwsem);
4015 up_read(&nfsi->rwsem);
4018 /* Is this a delegated lock? */
4019 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4021 lsp = request->fl_u.nfs4_fl.owner;
4022 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4026 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4027 status = PTR_ERR(task);
4030 status = nfs4_wait_for_completion_rpc_task(task);
4033 request->fl_flags = fl_flags;
4037 struct nfs4_lockdata {
4038 struct nfs_lock_args arg;
4039 struct nfs_lock_res res;
4040 struct nfs4_lock_state *lsp;
4041 struct nfs_open_context *ctx;
4042 struct file_lock fl;
4043 unsigned long timestamp;
4046 struct nfs_server *server;
4049 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4050 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4053 struct nfs4_lockdata *p;
4054 struct inode *inode = lsp->ls_state->inode;
4055 struct nfs_server *server = NFS_SERVER(inode);
4057 p = kzalloc(sizeof(*p), gfp_mask);
4061 p->arg.fh = NFS_FH(inode);
4063 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4064 if (p->arg.open_seqid == NULL)
4066 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4067 if (p->arg.lock_seqid == NULL)
4068 goto out_free_seqid;
4069 p->arg.lock_stateid = &lsp->ls_stateid;
4070 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4071 p->arg.lock_owner.id = lsp->ls_id.id;
4072 p->arg.lock_owner.s_dev = server->s_dev;
4073 p->res.lock_seqid = p->arg.lock_seqid;
4076 atomic_inc(&lsp->ls_count);
4077 p->ctx = get_nfs_open_context(ctx);
4078 memcpy(&p->fl, fl, sizeof(p->fl));
4081 nfs_free_seqid(p->arg.open_seqid);
4087 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4089 struct nfs4_lockdata *data = calldata;
4090 struct nfs4_state *state = data->lsp->ls_state;
4092 dprintk("%s: begin!\n", __func__);
4093 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4095 /* Do we need to do an open_to_lock_owner? */
4096 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4097 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4099 data->arg.open_stateid = &state->stateid;
4100 data->arg.new_lock_owner = 1;
4101 data->res.open_seqid = data->arg.open_seqid;
4103 data->arg.new_lock_owner = 0;
4104 data->timestamp = jiffies;
4105 if (nfs4_setup_sequence(data->server,
4106 &data->arg.seq_args,
4107 &data->res.seq_res, 1, task))
4109 rpc_call_start(task);
4110 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4113 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4115 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4116 nfs4_lock_prepare(task, calldata);
4119 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4121 struct nfs4_lockdata *data = calldata;
4123 dprintk("%s: begin!\n", __func__);
4125 if (!nfs4_sequence_done(task, &data->res.seq_res))
4128 data->rpc_status = task->tk_status;
4129 if (data->arg.new_lock_owner != 0) {
4130 if (data->rpc_status == 0)
4131 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4135 if (data->rpc_status == 0) {
4136 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4137 sizeof(data->lsp->ls_stateid.data));
4138 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4139 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
4142 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4145 static void nfs4_lock_release(void *calldata)
4147 struct nfs4_lockdata *data = calldata;
4149 dprintk("%s: begin!\n", __func__);
4150 nfs_free_seqid(data->arg.open_seqid);
4151 if (data->cancelled != 0) {
4152 struct rpc_task *task;
4153 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4154 data->arg.lock_seqid);
4156 rpc_put_task_async(task);
4157 dprintk("%s: cancelling lock!\n", __func__);
4159 nfs_free_seqid(data->arg.lock_seqid);
4160 nfs4_put_lock_state(data->lsp);
4161 put_nfs_open_context(data->ctx);
4163 dprintk("%s: done!\n", __func__);
4166 static const struct rpc_call_ops nfs4_lock_ops = {
4167 .rpc_call_prepare = nfs4_lock_prepare,
4168 .rpc_call_done = nfs4_lock_done,
4169 .rpc_release = nfs4_lock_release,
4172 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4173 .rpc_call_prepare = nfs4_recover_lock_prepare,
4174 .rpc_call_done = nfs4_lock_done,
4175 .rpc_release = nfs4_lock_release,
4178 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4181 case -NFS4ERR_ADMIN_REVOKED:
4182 case -NFS4ERR_BAD_STATEID:
4183 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4184 if (new_lock_owner != 0 ||
4185 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4186 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4188 case -NFS4ERR_STALE_STATEID:
4189 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4190 case -NFS4ERR_EXPIRED:
4191 nfs4_schedule_lease_recovery(server->nfs_client);
4195 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4197 struct nfs4_lockdata *data;
4198 struct rpc_task *task;
4199 struct rpc_message msg = {
4200 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4201 .rpc_cred = state->owner->so_cred,
4203 struct rpc_task_setup task_setup_data = {
4204 .rpc_client = NFS_CLIENT(state->inode),
4205 .rpc_message = &msg,
4206 .callback_ops = &nfs4_lock_ops,
4207 .workqueue = nfsiod_workqueue,
4208 .flags = RPC_TASK_ASYNC,
4212 dprintk("%s: begin!\n", __func__);
4213 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4214 fl->fl_u.nfs4_fl.owner,
4215 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4219 data->arg.block = 1;
4220 if (recovery_type > NFS_LOCK_NEW) {
4221 if (recovery_type == NFS_LOCK_RECLAIM)
4222 data->arg.reclaim = NFS_LOCK_RECLAIM;
4223 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4225 msg.rpc_argp = &data->arg;
4226 msg.rpc_resp = &data->res;
4227 task_setup_data.callback_data = data;
4228 task = rpc_run_task(&task_setup_data);
4230 return PTR_ERR(task);
4231 ret = nfs4_wait_for_completion_rpc_task(task);
4233 ret = data->rpc_status;
4235 nfs4_handle_setlk_error(data->server, data->lsp,
4236 data->arg.new_lock_owner, ret);
4238 data->cancelled = 1;
4240 dprintk("%s: done, ret = %d!\n", __func__, ret);
4244 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4246 struct nfs_server *server = NFS_SERVER(state->inode);
4247 struct nfs4_exception exception = { };
4251 /* Cache the lock if possible... */
4252 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4254 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4255 if (err != -NFS4ERR_DELAY)
4257 nfs4_handle_exception(server, err, &exception);
4258 } while (exception.retry);
4262 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4264 struct nfs_server *server = NFS_SERVER(state->inode);
4265 struct nfs4_exception exception = { };
4268 err = nfs4_set_lock_state(state, request);
4272 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4274 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4278 case -NFS4ERR_GRACE:
4279 case -NFS4ERR_DELAY:
4280 nfs4_handle_exception(server, err, &exception);
4283 } while (exception.retry);
4288 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4290 struct nfs_inode *nfsi = NFS_I(state->inode);
4291 unsigned char fl_flags = request->fl_flags;
4292 int status = -ENOLCK;
4294 if ((fl_flags & FL_POSIX) &&
4295 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4297 /* Is this a delegated open? */
4298 status = nfs4_set_lock_state(state, request);
4301 request->fl_flags |= FL_ACCESS;
4302 status = do_vfs_lock(request->fl_file, request);
4305 down_read(&nfsi->rwsem);
4306 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4307 /* Yes: cache locks! */
4308 /* ...but avoid races with delegation recall... */
4309 request->fl_flags = fl_flags & ~FL_SLEEP;
4310 status = do_vfs_lock(request->fl_file, request);
4313 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4316 /* Note: we always want to sleep here! */
4317 request->fl_flags = fl_flags | FL_SLEEP;
4318 if (do_vfs_lock(request->fl_file, request) < 0)
4319 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4321 up_read(&nfsi->rwsem);
4323 request->fl_flags = fl_flags;
4327 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4329 struct nfs4_exception exception = { };
4333 err = _nfs4_proc_setlk(state, cmd, request);
4334 if (err == -NFS4ERR_DENIED)
4336 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4338 } while (exception.retry);
4343 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4345 struct nfs_open_context *ctx;
4346 struct nfs4_state *state;
4347 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4350 /* verify open state */
4351 ctx = nfs_file_open_context(filp);
4354 if (request->fl_start < 0 || request->fl_end < 0)
4357 if (IS_GETLK(cmd)) {
4359 return nfs4_proc_getlk(state, F_GETLK, request);
4363 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4366 if (request->fl_type == F_UNLCK) {
4368 return nfs4_proc_unlck(state, cmd, request);
4375 status = nfs4_proc_setlk(state, cmd, request);
4376 if ((status != -EAGAIN) || IS_SETLK(cmd))
4378 timeout = nfs4_set_lock_task_retry(timeout);
4379 status = -ERESTARTSYS;
4382 } while(status < 0);
4386 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4388 struct nfs_server *server = NFS_SERVER(state->inode);
4389 struct nfs4_exception exception = { };
4392 err = nfs4_set_lock_state(state, fl);
4396 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4399 printk(KERN_ERR "%s: unhandled error %d.\n",
4404 case -NFS4ERR_EXPIRED:
4405 case -NFS4ERR_STALE_CLIENTID:
4406 case -NFS4ERR_STALE_STATEID:
4407 nfs4_schedule_lease_recovery(server->nfs_client);
4409 case -NFS4ERR_BADSESSION:
4410 case -NFS4ERR_BADSLOT:
4411 case -NFS4ERR_BAD_HIGH_SLOT:
4412 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4413 case -NFS4ERR_DEADSESSION:
4414 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
4418 * The show must go on: exit, but mark the
4419 * stateid as needing recovery.
4421 case -NFS4ERR_ADMIN_REVOKED:
4422 case -NFS4ERR_BAD_STATEID:
4423 case -NFS4ERR_OPENMODE:
4424 nfs4_schedule_stateid_recovery(server, state);
4429 * User RPCSEC_GSS context has expired.
4430 * We cannot recover this stateid now, so
4431 * skip it and allow recovery thread to
4437 case -NFS4ERR_DENIED:
4438 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4441 case -NFS4ERR_DELAY:
4444 err = nfs4_handle_exception(server, err, &exception);
4445 } while (exception.retry);
4450 static void nfs4_release_lockowner_release(void *calldata)
4455 const struct rpc_call_ops nfs4_release_lockowner_ops = {
4456 .rpc_release = nfs4_release_lockowner_release,
4459 void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4461 struct nfs_server *server = lsp->ls_state->owner->so_server;
4462 struct nfs_release_lockowner_args *args;
4463 struct rpc_message msg = {
4464 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4467 if (server->nfs_client->cl_mvops->minor_version != 0)
4469 args = kmalloc(sizeof(*args), GFP_NOFS);
4472 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4473 args->lock_owner.id = lsp->ls_id.id;
4474 args->lock_owner.s_dev = server->s_dev;
4475 msg.rpc_argp = args;
4476 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4479 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4481 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4482 const void *buf, size_t buflen,
4483 int flags, int type)
4485 if (strcmp(key, "") != 0)
4488 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
4491 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4492 void *buf, size_t buflen, int type)
4494 if (strcmp(key, "") != 0)
4497 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
4500 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4501 size_t list_len, const char *name,
4502 size_t name_len, int type)
4504 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
4506 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4509 if (list && len <= list_len)
4510 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
4514 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4516 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4517 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4518 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4521 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4522 NFS_ATTR_FATTR_NLINK;
4523 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4527 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4528 struct nfs4_fs_locations *fs_locations, struct page *page)
4530 struct nfs_server *server = NFS_SERVER(dir);
4532 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4533 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
4535 struct nfs4_fs_locations_arg args = {
4536 .dir_fh = NFS_FH(dir),
4541 struct nfs4_fs_locations_res res = {
4542 .fs_locations = fs_locations,
4544 struct rpc_message msg = {
4545 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4551 dprintk("%s: start\n", __func__);
4552 nfs_fattr_init(&fs_locations->fattr);
4553 fs_locations->server = server;
4554 fs_locations->nlocations = 0;
4555 status = nfs4_call_sync(server, &msg, &args, &res, 0);
4556 nfs_fixup_referral_attributes(&fs_locations->fattr);
4557 dprintk("%s: returned status = %d\n", __func__, status);
4561 #ifdef CONFIG_NFS_V4_1
4563 * Check the exchange flags returned by the server for invalid flags, having
4564 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4567 static int nfs4_check_cl_exchange_flags(u32 flags)
4569 if (flags & ~EXCHGID4_FLAG_MASK_R)
4571 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4572 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4574 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4578 return -NFS4ERR_INVAL;
4582 * nfs4_proc_exchange_id()
4584 * Since the clientid has expired, all compounds using sessions
4585 * associated with the stale clientid will be returning
4586 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4587 * be in some phase of session reset.
4589 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4591 nfs4_verifier verifier;
4592 struct nfs41_exchange_id_args args = {
4594 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
4596 struct nfs41_exchange_id_res res = {
4600 struct rpc_message msg = {
4601 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4608 dprintk("--> %s\n", __func__);
4609 BUG_ON(clp == NULL);
4611 p = (u32 *)verifier.data;
4612 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4613 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4614 args.verifier = &verifier;
4616 args.id_len = scnprintf(args.id, sizeof(args.id),
4619 init_utsname()->nodename,
4620 init_utsname()->domainname,
4621 clp->cl_rpcclient->cl_auth->au_flavor);
4623 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4625 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
4626 dprintk("<-- %s status= %d\n", __func__, status);
4630 struct nfs4_get_lease_time_data {
4631 struct nfs4_get_lease_time_args *args;
4632 struct nfs4_get_lease_time_res *res;
4633 struct nfs_client *clp;
4636 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4640 struct nfs4_get_lease_time_data *data =
4641 (struct nfs4_get_lease_time_data *)calldata;
4643 dprintk("--> %s\n", __func__);
4644 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4645 /* just setup sequence, do not trigger session recovery
4646 since we're invoked within one */
4647 ret = nfs41_setup_sequence(data->clp->cl_session,
4648 &data->args->la_seq_args,
4649 &data->res->lr_seq_res, 0, task);
4651 BUG_ON(ret == -EAGAIN);
4652 rpc_call_start(task);
4653 dprintk("<-- %s\n", __func__);
4657 * Called from nfs4_state_manager thread for session setup, so don't recover
4658 * from sequence operation or clientid errors.
4660 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4662 struct nfs4_get_lease_time_data *data =
4663 (struct nfs4_get_lease_time_data *)calldata;
4665 dprintk("--> %s\n", __func__);
4666 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4668 switch (task->tk_status) {
4669 case -NFS4ERR_DELAY:
4670 case -NFS4ERR_GRACE:
4671 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4672 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4673 task->tk_status = 0;
4674 nfs_restart_rpc(task, data->clp);
4677 dprintk("<-- %s\n", __func__);
4680 struct rpc_call_ops nfs4_get_lease_time_ops = {
4681 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4682 .rpc_call_done = nfs4_get_lease_time_done,
4685 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4687 struct rpc_task *task;
4688 struct nfs4_get_lease_time_args args;
4689 struct nfs4_get_lease_time_res res = {
4690 .lr_fsinfo = fsinfo,
4692 struct nfs4_get_lease_time_data data = {
4697 struct rpc_message msg = {
4698 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4702 struct rpc_task_setup task_setup = {
4703 .rpc_client = clp->cl_rpcclient,
4704 .rpc_message = &msg,
4705 .callback_ops = &nfs4_get_lease_time_ops,
4706 .callback_data = &data
4710 dprintk("--> %s\n", __func__);
4711 task = rpc_run_task(&task_setup);
4714 status = PTR_ERR(task);
4716 status = task->tk_status;
4719 dprintk("<-- %s return %d\n", __func__, status);
4725 * Reset a slot table
4727 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4730 struct nfs4_slot *new = NULL;
4734 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4735 max_reqs, tbl->max_slots);
4737 /* Does the newly negotiated max_reqs match the existing slot table? */
4738 if (max_reqs != tbl->max_slots) {
4740 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
4747 spin_lock(&tbl->slot_tbl_lock);
4750 tbl->max_slots = max_reqs;
4752 for (i = 0; i < tbl->max_slots; ++i)
4753 tbl->slots[i].seq_nr = ivalue;
4754 spin_unlock(&tbl->slot_tbl_lock);
4755 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4756 tbl, tbl->slots, tbl->max_slots);
4758 dprintk("<-- %s: return %d\n", __func__, ret);
4763 * Reset the forechannel and backchannel slot tables
4765 static int nfs4_reset_slot_tables(struct nfs4_session *session)
4769 status = nfs4_reset_slot_table(&session->fc_slot_table,
4770 session->fc_attrs.max_reqs, 1);
4774 status = nfs4_reset_slot_table(&session->bc_slot_table,
4775 session->bc_attrs.max_reqs, 0);
4779 /* Destroy the slot table */
4780 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4782 if (session->fc_slot_table.slots != NULL) {
4783 kfree(session->fc_slot_table.slots);
4784 session->fc_slot_table.slots = NULL;
4786 if (session->bc_slot_table.slots != NULL) {
4787 kfree(session->bc_slot_table.slots);
4788 session->bc_slot_table.slots = NULL;
4794 * Initialize slot table
4796 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4797 int max_slots, int ivalue)
4799 struct nfs4_slot *slot;
4802 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4804 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
4806 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
4811 spin_lock(&tbl->slot_tbl_lock);
4812 tbl->max_slots = max_slots;
4814 tbl->highest_used_slotid = -1; /* no slot is currently used */
4815 spin_unlock(&tbl->slot_tbl_lock);
4816 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4817 tbl, tbl->slots, tbl->max_slots);
4819 dprintk("<-- %s: return %d\n", __func__, ret);
4824 * Initialize the forechannel and backchannel tables
4826 static int nfs4_init_slot_tables(struct nfs4_session *session)
4828 struct nfs4_slot_table *tbl;
4831 tbl = &session->fc_slot_table;
4832 if (tbl->slots == NULL) {
4833 status = nfs4_init_slot_table(tbl,
4834 session->fc_attrs.max_reqs, 1);
4839 tbl = &session->bc_slot_table;
4840 if (tbl->slots == NULL) {
4841 status = nfs4_init_slot_table(tbl,
4842 session->bc_attrs.max_reqs, 0);
4844 nfs4_destroy_slot_tables(session);
4850 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4852 struct nfs4_session *session;
4853 struct nfs4_slot_table *tbl;
4855 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
4859 tbl = &session->fc_slot_table;
4860 tbl->highest_used_slotid = -1;
4861 spin_lock_init(&tbl->slot_tbl_lock);
4862 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4863 init_completion(&tbl->complete);
4865 tbl = &session->bc_slot_table;
4866 tbl->highest_used_slotid = -1;
4867 spin_lock_init(&tbl->slot_tbl_lock);
4868 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4869 init_completion(&tbl->complete);
4871 session->session_state = 1<<NFS4_SESSION_INITING;
4877 void nfs4_destroy_session(struct nfs4_session *session)
4879 nfs4_proc_destroy_session(session);
4880 dprintk("%s Destroy backchannel for xprt %p\n",
4881 __func__, session->clp->cl_rpcclient->cl_xprt);
4882 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4883 NFS41_BC_MIN_CALLBACKS);
4884 nfs4_destroy_slot_tables(session);
4889 * Initialize the values to be used by the client in CREATE_SESSION
4890 * If nfs4_init_session set the fore channel request and response sizes,
4893 * Set the back channel max_resp_sz_cached to zero to force the client to
4894 * always set csa_cachethis to FALSE because the current implementation
4895 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4897 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4899 struct nfs4_session *session = args->client->cl_session;
4900 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4901 mxresp_sz = session->fc_attrs.max_resp_sz;
4904 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4906 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4907 /* Fore channel attributes */
4908 args->fc_attrs.headerpadsz = 0;
4909 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4910 args->fc_attrs.max_resp_sz = mxresp_sz;
4911 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4912 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4914 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4915 "max_ops=%u max_reqs=%u\n",
4917 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4918 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
4920 /* Back channel attributes */
4921 args->bc_attrs.headerpadsz = 0;
4922 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4923 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4924 args->bc_attrs.max_resp_sz_cached = 0;
4925 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4926 args->bc_attrs.max_reqs = 1;
4928 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4929 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4931 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4932 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4933 args->bc_attrs.max_reqs);
4936 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
4938 struct nfs4_channel_attrs *sent = &args->fc_attrs;
4939 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
4941 if (rcvd->headerpadsz > sent->headerpadsz)
4943 if (rcvd->max_resp_sz > sent->max_resp_sz)
4946 * Our requested max_ops is the minimum we need; we're not
4947 * prepared to break up compounds into smaller pieces than that.
4948 * So, no point even trying to continue if the server won't
4951 if (rcvd->max_ops < sent->max_ops)
4953 if (rcvd->max_reqs == 0)
4958 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
4960 struct nfs4_channel_attrs *sent = &args->bc_attrs;
4961 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
4963 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
4965 if (rcvd->max_resp_sz < sent->max_resp_sz)
4967 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
4969 /* These would render the backchannel useless: */
4970 if (rcvd->max_ops == 0)
4972 if (rcvd->max_reqs == 0)
4977 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4978 struct nfs4_session *session)
4982 ret = nfs4_verify_fore_channel_attrs(args, session);
4985 return nfs4_verify_back_channel_attrs(args, session);
4988 static int _nfs4_proc_create_session(struct nfs_client *clp)
4990 struct nfs4_session *session = clp->cl_session;
4991 struct nfs41_create_session_args args = {
4993 .cb_program = NFS4_CALLBACK,
4995 struct nfs41_create_session_res res = {
4998 struct rpc_message msg = {
4999 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5005 nfs4_init_channel_attrs(&args);
5006 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5008 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5011 /* Verify the session's negotiated channel_attrs values */
5012 status = nfs4_verify_channel_attrs(&args, session);
5014 /* Increment the clientid slot sequence id */
5022 * Issues a CREATE_SESSION operation to the server.
5023 * It is the responsibility of the caller to verify the session is
5024 * expired before calling this routine.
5026 int nfs4_proc_create_session(struct nfs_client *clp)
5030 struct nfs4_session *session = clp->cl_session;
5034 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5037 status = _nfs4_proc_create_session(clp);
5038 if (status == -NFS4ERR_DELAY) {
5039 err = nfs4_delay(clp->cl_rpcclient, &timeout);
5043 } while (status == -NFS4ERR_DELAY);
5048 /* Init and reset the fore channel */
5049 status = nfs4_init_slot_tables(session);
5050 dprintk("slot table initialization returned %d\n", status);
5053 status = nfs4_reset_slot_tables(session);
5054 dprintk("slot table reset returned %d\n", status);
5058 ptr = (unsigned *)&session->sess_id.data[0];
5059 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5060 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5062 dprintk("<-- %s\n", __func__);
5067 * Issue the over-the-wire RPC DESTROY_SESSION.
5068 * The caller must serialize access to this routine.
5070 int nfs4_proc_destroy_session(struct nfs4_session *session)
5073 struct rpc_message msg;
5075 dprintk("--> nfs4_proc_destroy_session\n");
5077 /* session is still being setup */
5078 if (session->clp->cl_cons_state != NFS_CS_READY)
5081 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5082 msg.rpc_argp = session;
5083 msg.rpc_resp = NULL;
5084 msg.rpc_cred = NULL;
5085 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5089 "Got error %d from the server on DESTROY_SESSION. "
5090 "Session has been destroyed regardless...\n", status);
5092 dprintk("<-- nfs4_proc_destroy_session\n");
5096 int nfs4_init_session(struct nfs_server *server)
5098 struct nfs_client *clp = server->nfs_client;
5099 struct nfs4_session *session;
5100 unsigned int rsize, wsize;
5103 if (!nfs4_has_session(clp))
5106 session = clp->cl_session;
5107 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5110 rsize = server->rsize;
5112 rsize = NFS_MAX_FILE_IO_SIZE;
5113 wsize = server->wsize;
5115 wsize = NFS_MAX_FILE_IO_SIZE;
5117 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5118 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5120 ret = nfs4_recover_expired_lease(server);
5122 ret = nfs4_check_client_ready(clp);
5126 int nfs4_init_ds_session(struct nfs_client *clp)
5128 struct nfs4_session *session = clp->cl_session;
5131 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5134 ret = nfs4_client_recover_expired_lease(clp);
5136 /* Test for the DS role */
5137 if (!is_ds_client(clp))
5140 ret = nfs4_check_client_ready(clp);
5144 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5148 * Renew the cl_session lease.
5150 struct nfs4_sequence_data {
5151 struct nfs_client *clp;
5152 struct nfs4_sequence_args args;
5153 struct nfs4_sequence_res res;
5156 static void nfs41_sequence_release(void *data)
5158 struct nfs4_sequence_data *calldata = data;
5159 struct nfs_client *clp = calldata->clp;
5161 if (atomic_read(&clp->cl_count) > 1)
5162 nfs4_schedule_state_renewal(clp);
5163 nfs_put_client(clp);
5167 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5169 switch(task->tk_status) {
5170 case -NFS4ERR_DELAY:
5171 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5174 nfs4_schedule_lease_recovery(clp);
5179 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5181 struct nfs4_sequence_data *calldata = data;
5182 struct nfs_client *clp = calldata->clp;
5184 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5187 if (task->tk_status < 0) {
5188 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5189 if (atomic_read(&clp->cl_count) == 1)
5192 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5193 rpc_restart_call_prepare(task);
5197 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5199 dprintk("<-- %s\n", __func__);
5202 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5204 struct nfs4_sequence_data *calldata = data;
5205 struct nfs_client *clp = calldata->clp;
5206 struct nfs4_sequence_args *args;
5207 struct nfs4_sequence_res *res;
5209 args = task->tk_msg.rpc_argp;
5210 res = task->tk_msg.rpc_resp;
5212 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
5214 rpc_call_start(task);
5217 static const struct rpc_call_ops nfs41_sequence_ops = {
5218 .rpc_call_done = nfs41_sequence_call_done,
5219 .rpc_call_prepare = nfs41_sequence_prepare,
5220 .rpc_release = nfs41_sequence_release,
5223 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5225 struct nfs4_sequence_data *calldata;
5226 struct rpc_message msg = {
5227 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5230 struct rpc_task_setup task_setup_data = {
5231 .rpc_client = clp->cl_rpcclient,
5232 .rpc_message = &msg,
5233 .callback_ops = &nfs41_sequence_ops,
5234 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5237 if (!atomic_inc_not_zero(&clp->cl_count))
5238 return ERR_PTR(-EIO);
5239 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5240 if (calldata == NULL) {
5241 nfs_put_client(clp);
5242 return ERR_PTR(-ENOMEM);
5244 msg.rpc_argp = &calldata->args;
5245 msg.rpc_resp = &calldata->res;
5246 calldata->clp = clp;
5247 task_setup_data.callback_data = calldata;
5249 return rpc_run_task(&task_setup_data);
5252 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5254 struct rpc_task *task;
5257 task = _nfs41_proc_sequence(clp, cred);
5259 ret = PTR_ERR(task);
5261 rpc_put_task_async(task);
5262 dprintk("<-- %s status=%d\n", __func__, ret);
5266 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5268 struct rpc_task *task;
5271 task = _nfs41_proc_sequence(clp, cred);
5273 ret = PTR_ERR(task);
5276 ret = rpc_wait_for_completion_task(task);
5278 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5280 if (task->tk_status == 0)
5281 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5282 ret = task->tk_status;
5286 dprintk("<-- %s status=%d\n", __func__, ret);
5290 struct nfs4_reclaim_complete_data {
5291 struct nfs_client *clp;
5292 struct nfs41_reclaim_complete_args arg;
5293 struct nfs41_reclaim_complete_res res;
5296 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5298 struct nfs4_reclaim_complete_data *calldata = data;
5300 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5301 if (nfs41_setup_sequence(calldata->clp->cl_session,
5302 &calldata->arg.seq_args,
5303 &calldata->res.seq_res, 0, task))
5306 rpc_call_start(task);
5309 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5311 switch(task->tk_status) {
5313 case -NFS4ERR_COMPLETE_ALREADY:
5314 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5316 case -NFS4ERR_DELAY:
5317 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5320 nfs4_schedule_lease_recovery(clp);
5325 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5327 struct nfs4_reclaim_complete_data *calldata = data;
5328 struct nfs_client *clp = calldata->clp;
5329 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5331 dprintk("--> %s\n", __func__);
5332 if (!nfs41_sequence_done(task, res))
5335 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5336 rpc_restart_call_prepare(task);
5339 dprintk("<-- %s\n", __func__);
5342 static void nfs4_free_reclaim_complete_data(void *data)
5344 struct nfs4_reclaim_complete_data *calldata = data;
5349 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5350 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5351 .rpc_call_done = nfs4_reclaim_complete_done,
5352 .rpc_release = nfs4_free_reclaim_complete_data,
5356 * Issue a global reclaim complete.
5358 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5360 struct nfs4_reclaim_complete_data *calldata;
5361 struct rpc_task *task;
5362 struct rpc_message msg = {
5363 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5365 struct rpc_task_setup task_setup_data = {
5366 .rpc_client = clp->cl_rpcclient,
5367 .rpc_message = &msg,
5368 .callback_ops = &nfs4_reclaim_complete_call_ops,
5369 .flags = RPC_TASK_ASYNC,
5371 int status = -ENOMEM;
5373 dprintk("--> %s\n", __func__);
5374 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5375 if (calldata == NULL)
5377 calldata->clp = clp;
5378 calldata->arg.one_fs = 0;
5380 msg.rpc_argp = &calldata->arg;
5381 msg.rpc_resp = &calldata->res;
5382 task_setup_data.callback_data = calldata;
5383 task = rpc_run_task(&task_setup_data);
5385 status = PTR_ERR(task);
5388 status = nfs4_wait_for_completion_rpc_task(task);
5390 status = task->tk_status;
5394 dprintk("<-- %s status=%d\n", __func__, status);
5399 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5401 struct nfs4_layoutget *lgp = calldata;
5402 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5404 dprintk("--> %s\n", __func__);
5405 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5406 * right now covering the LAYOUTGET we are about to send.
5407 * However, that is not so catastrophic, and there seems
5408 * to be no way to prevent it completely.
5410 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5411 &lgp->res.seq_res, 0, task))
5413 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5414 NFS_I(lgp->args.inode)->layout,
5415 lgp->args.ctx->state)) {
5416 rpc_exit(task, NFS4_OK);
5419 rpc_call_start(task);
5422 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5424 struct nfs4_layoutget *lgp = calldata;
5425 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5427 dprintk("--> %s\n", __func__);
5429 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5432 switch (task->tk_status) {
5435 case -NFS4ERR_LAYOUTTRYLATER:
5436 case -NFS4ERR_RECALLCONFLICT:
5437 task->tk_status = -NFS4ERR_DELAY;
5440 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5441 rpc_restart_call_prepare(task);
5445 dprintk("<-- %s\n", __func__);
5448 static void nfs4_layoutget_release(void *calldata)
5450 struct nfs4_layoutget *lgp = calldata;
5452 dprintk("--> %s\n", __func__);
5453 if (lgp->res.layout.buf != NULL)
5454 free_page((unsigned long) lgp->res.layout.buf);
5455 put_nfs_open_context(lgp->args.ctx);
5457 dprintk("<-- %s\n", __func__);
5460 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5461 .rpc_call_prepare = nfs4_layoutget_prepare,
5462 .rpc_call_done = nfs4_layoutget_done,
5463 .rpc_release = nfs4_layoutget_release,
5466 int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5468 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5469 struct rpc_task *task;
5470 struct rpc_message msg = {
5471 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5472 .rpc_argp = &lgp->args,
5473 .rpc_resp = &lgp->res,
5475 struct rpc_task_setup task_setup_data = {
5476 .rpc_client = server->client,
5477 .rpc_message = &msg,
5478 .callback_ops = &nfs4_layoutget_call_ops,
5479 .callback_data = lgp,
5480 .flags = RPC_TASK_ASYNC,
5484 dprintk("--> %s\n", __func__);
5486 lgp->res.layout.buf = (void *)__get_free_page(GFP_NOFS);
5487 if (lgp->res.layout.buf == NULL) {
5488 nfs4_layoutget_release(lgp);
5492 lgp->res.seq_res.sr_slot = NULL;
5493 task = rpc_run_task(&task_setup_data);
5495 return PTR_ERR(task);
5496 status = nfs4_wait_for_completion_rpc_task(task);
5498 status = task->tk_status;
5500 status = pnfs_layout_process(lgp);
5502 dprintk("<-- %s status=%d\n", __func__, status);
5507 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5509 struct nfs4_getdeviceinfo_args args = {
5512 struct nfs4_getdeviceinfo_res res = {
5515 struct rpc_message msg = {
5516 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5522 dprintk("--> %s\n", __func__);
5523 status = nfs4_call_sync(server, &msg, &args, &res, 0);
5524 dprintk("<-- %s status=%d\n", __func__, status);
5529 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5531 struct nfs4_exception exception = { };
5535 err = nfs4_handle_exception(server,
5536 _nfs4_proc_getdeviceinfo(server, pdev),
5538 } while (exception.retry);
5541 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5543 #endif /* CONFIG_NFS_V4_1 */
5545 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
5546 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5547 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5548 .recover_open = nfs4_open_reclaim,
5549 .recover_lock = nfs4_lock_reclaim,
5550 .establish_clid = nfs4_init_clientid,
5551 .get_clid_cred = nfs4_get_setclientid_cred,
5554 #if defined(CONFIG_NFS_V4_1)
5555 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5556 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5557 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5558 .recover_open = nfs4_open_reclaim,
5559 .recover_lock = nfs4_lock_reclaim,
5560 .establish_clid = nfs41_init_clientid,
5561 .get_clid_cred = nfs4_get_exchange_id_cred,
5562 .reclaim_complete = nfs41_proc_reclaim_complete,
5564 #endif /* CONFIG_NFS_V4_1 */
5566 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
5567 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5568 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5569 .recover_open = nfs4_open_expired,
5570 .recover_lock = nfs4_lock_expired,
5571 .establish_clid = nfs4_init_clientid,
5572 .get_clid_cred = nfs4_get_setclientid_cred,
5575 #if defined(CONFIG_NFS_V4_1)
5576 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5577 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5578 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5579 .recover_open = nfs4_open_expired,
5580 .recover_lock = nfs4_lock_expired,
5581 .establish_clid = nfs41_init_clientid,
5582 .get_clid_cred = nfs4_get_exchange_id_cred,
5584 #endif /* CONFIG_NFS_V4_1 */
5586 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5587 .sched_state_renewal = nfs4_proc_async_renew,
5588 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
5589 .renew_lease = nfs4_proc_renew,
5592 #if defined(CONFIG_NFS_V4_1)
5593 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5594 .sched_state_renewal = nfs41_proc_async_sequence,
5595 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
5596 .renew_lease = nfs4_proc_sequence,
5600 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5602 .call_sync = _nfs4_call_sync,
5603 .validate_stateid = nfs4_validate_delegation_stateid,
5604 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5605 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5606 .state_renewal_ops = &nfs40_state_renewal_ops,
5609 #if defined(CONFIG_NFS_V4_1)
5610 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5612 .call_sync = _nfs4_call_sync_session,
5613 .validate_stateid = nfs41_validate_delegation_stateid,
5614 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5615 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5616 .state_renewal_ops = &nfs41_state_renewal_ops,
5620 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5621 [0] = &nfs_v4_0_minor_ops,
5622 #if defined(CONFIG_NFS_V4_1)
5623 [1] = &nfs_v4_1_minor_ops,
5627 static const struct inode_operations nfs4_file_inode_operations = {
5628 .permission = nfs_permission,
5629 .getattr = nfs_getattr,
5630 .setattr = nfs_setattr,
5631 .getxattr = generic_getxattr,
5632 .setxattr = generic_setxattr,
5633 .listxattr = generic_listxattr,
5634 .removexattr = generic_removexattr,
5637 const struct nfs_rpc_ops nfs_v4_clientops = {
5638 .version = 4, /* protocol version */
5639 .dentry_ops = &nfs4_dentry_operations,
5640 .dir_inode_ops = &nfs4_dir_inode_operations,
5641 .file_inode_ops = &nfs4_file_inode_operations,
5642 .getroot = nfs4_proc_get_root,
5643 .getattr = nfs4_proc_getattr,
5644 .setattr = nfs4_proc_setattr,
5645 .lookupfh = nfs4_proc_lookupfh,
5646 .lookup = nfs4_proc_lookup,
5647 .access = nfs4_proc_access,
5648 .readlink = nfs4_proc_readlink,
5649 .create = nfs4_proc_create,
5650 .remove = nfs4_proc_remove,
5651 .unlink_setup = nfs4_proc_unlink_setup,
5652 .unlink_done = nfs4_proc_unlink_done,
5653 .rename = nfs4_proc_rename,
5654 .rename_setup = nfs4_proc_rename_setup,
5655 .rename_done = nfs4_proc_rename_done,
5656 .link = nfs4_proc_link,
5657 .symlink = nfs4_proc_symlink,
5658 .mkdir = nfs4_proc_mkdir,
5659 .rmdir = nfs4_proc_remove,
5660 .readdir = nfs4_proc_readdir,
5661 .mknod = nfs4_proc_mknod,
5662 .statfs = nfs4_proc_statfs,
5663 .fsinfo = nfs4_proc_fsinfo,
5664 .pathconf = nfs4_proc_pathconf,
5665 .set_capabilities = nfs4_server_capabilities,
5666 .decode_dirent = nfs4_decode_dirent,
5667 .read_setup = nfs4_proc_read_setup,
5668 .read_done = nfs4_read_done,
5669 .write_setup = nfs4_proc_write_setup,
5670 .write_done = nfs4_write_done,
5671 .commit_setup = nfs4_proc_commit_setup,
5672 .commit_done = nfs4_commit_done,
5673 .lock = nfs4_proc_lock,
5674 .clear_acl_cache = nfs4_zap_acl_attr,
5675 .close_context = nfs4_close_context,
5676 .open_context = nfs4_atomic_open,
5677 .init_client = nfs4_init_client,
5680 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
5681 .prefix = XATTR_NAME_NFSV4_ACL,
5682 .list = nfs4_xattr_list_nfs4_acl,
5683 .get = nfs4_xattr_get_nfs4_acl,
5684 .set = nfs4_xattr_set_nfs4_acl,
5687 const struct xattr_handler *nfs4_xattr_handlers[] = {
5688 &nfs4_xattr_nfs4_acl_handler,