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/sunrpc/gss_api.h>
45 #include <linux/nfs.h>
46 #include <linux/nfs4.h>
47 #include <linux/nfs_fs.h>
48 #include <linux/nfs_page.h>
49 #include <linux/nfs_mount.h>
50 #include <linux/namei.h>
51 #include <linux/mount.h>
52 #include <linux/module.h>
53 #include <linux/sunrpc/bc_xprt.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
58 #include "delegation.h"
64 #define NFSDBG_FACILITY NFSDBG_PROC
66 #define NFS4_POLL_RETRY_MIN (HZ/10)
67 #define NFS4_POLL_RETRY_MAX (15*HZ)
69 #define NFS4_MAX_LOOP_ON_RECOVER (10)
72 static int _nfs4_proc_open(struct nfs4_opendata *data);
73 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
74 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
75 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
76 static int _nfs4_proc_lookup(struct rpc_clnt *client, struct inode *dir,
77 const struct qstr *name, struct nfs_fh *fhandle,
78 struct nfs_fattr *fattr);
79 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
80 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
81 struct nfs_fattr *fattr, struct iattr *sattr,
82 struct nfs4_state *state);
84 /* Prevent leaks of NFSv4 errors into userland */
85 static int nfs4_map_errors(int err)
90 case -NFS4ERR_RESOURCE:
92 case -NFS4ERR_WRONGSEC:
94 case -NFS4ERR_BADOWNER:
95 case -NFS4ERR_BADNAME:
98 dprintk("%s could not handle NFSv4 error %d\n",
106 * This is our standard bitmap for GETATTR requests.
108 const u32 nfs4_fattr_bitmap[2] = {
110 | FATTR4_WORD0_CHANGE
113 | FATTR4_WORD0_FILEID,
115 | FATTR4_WORD1_NUMLINKS
117 | FATTR4_WORD1_OWNER_GROUP
118 | FATTR4_WORD1_RAWDEV
119 | FATTR4_WORD1_SPACE_USED
120 | FATTR4_WORD1_TIME_ACCESS
121 | FATTR4_WORD1_TIME_METADATA
122 | FATTR4_WORD1_TIME_MODIFY
125 const u32 nfs4_statfs_bitmap[2] = {
126 FATTR4_WORD0_FILES_AVAIL
127 | FATTR4_WORD0_FILES_FREE
128 | FATTR4_WORD0_FILES_TOTAL,
129 FATTR4_WORD1_SPACE_AVAIL
130 | FATTR4_WORD1_SPACE_FREE
131 | FATTR4_WORD1_SPACE_TOTAL
134 const u32 nfs4_pathconf_bitmap[2] = {
136 | FATTR4_WORD0_MAXNAME,
140 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
141 | FATTR4_WORD0_MAXREAD
142 | FATTR4_WORD0_MAXWRITE
143 | FATTR4_WORD0_LEASE_TIME,
144 FATTR4_WORD1_TIME_DELTA
145 | FATTR4_WORD1_FS_LAYOUT_TYPES
148 const u32 nfs4_fs_locations_bitmap[2] = {
150 | FATTR4_WORD0_CHANGE
153 | FATTR4_WORD0_FILEID
154 | FATTR4_WORD0_FS_LOCATIONS,
156 | FATTR4_WORD1_NUMLINKS
158 | FATTR4_WORD1_OWNER_GROUP
159 | FATTR4_WORD1_RAWDEV
160 | FATTR4_WORD1_SPACE_USED
161 | FATTR4_WORD1_TIME_ACCESS
162 | FATTR4_WORD1_TIME_METADATA
163 | FATTR4_WORD1_TIME_MODIFY
164 | FATTR4_WORD1_MOUNTED_ON_FILEID
167 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
168 struct nfs4_readdir_arg *readdir)
172 BUG_ON(readdir->count < 80);
174 readdir->cookie = cookie;
175 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
180 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
185 * NFSv4 servers do not return entries for '.' and '..'
186 * Therefore, we fake these entries here. We let '.'
187 * have cookie 0 and '..' have cookie 1. Note that
188 * when talking to the server, we always send cookie 0
191 start = p = kmap_atomic(*readdir->pages, KM_USER0);
194 *p++ = xdr_one; /* next */
195 *p++ = xdr_zero; /* cookie, first word */
196 *p++ = xdr_one; /* cookie, second word */
197 *p++ = xdr_one; /* entry len */
198 memcpy(p, ".\0\0\0", 4); /* entry */
200 *p++ = xdr_one; /* bitmap length */
201 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
202 *p++ = htonl(8); /* attribute buffer length */
203 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
206 *p++ = xdr_one; /* next */
207 *p++ = xdr_zero; /* cookie, first word */
208 *p++ = xdr_two; /* cookie, second word */
209 *p++ = xdr_two; /* entry len */
210 memcpy(p, "..\0\0", 4); /* entry */
212 *p++ = xdr_one; /* bitmap length */
213 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
214 *p++ = htonl(8); /* attribute buffer length */
215 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
217 readdir->pgbase = (char *)p - (char *)start;
218 readdir->count -= readdir->pgbase;
219 kunmap_atomic(start, KM_USER0);
222 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
228 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
229 nfs_wait_bit_killable, TASK_KILLABLE);
233 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
240 *timeout = NFS4_POLL_RETRY_MIN;
241 if (*timeout > NFS4_POLL_RETRY_MAX)
242 *timeout = NFS4_POLL_RETRY_MAX;
243 schedule_timeout_killable(*timeout);
244 if (fatal_signal_pending(current))
250 /* This is the error handling routine for processes that are allowed
253 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
255 struct nfs_client *clp = server->nfs_client;
256 struct nfs4_state *state = exception->state;
259 exception->retry = 0;
263 case -NFS4ERR_ADMIN_REVOKED:
264 case -NFS4ERR_BAD_STATEID:
265 case -NFS4ERR_OPENMODE:
268 nfs4_schedule_stateid_recovery(server, state);
269 goto wait_on_recovery;
270 case -NFS4ERR_EXPIRED:
272 nfs4_schedule_stateid_recovery(server, state);
273 case -NFS4ERR_STALE_STATEID:
274 case -NFS4ERR_STALE_CLIENTID:
275 nfs4_schedule_lease_recovery(clp);
276 goto wait_on_recovery;
277 #if defined(CONFIG_NFS_V4_1)
278 case -NFS4ERR_BADSESSION:
279 case -NFS4ERR_BADSLOT:
280 case -NFS4ERR_BAD_HIGH_SLOT:
281 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
282 case -NFS4ERR_DEADSESSION:
283 case -NFS4ERR_SEQ_FALSE_RETRY:
284 case -NFS4ERR_SEQ_MISORDERED:
285 dprintk("%s ERROR: %d Reset session\n", __func__,
287 nfs4_schedule_session_recovery(clp->cl_session);
288 exception->retry = 1;
290 #endif /* defined(CONFIG_NFS_V4_1) */
291 case -NFS4ERR_FILE_OPEN:
292 if (exception->timeout > HZ) {
293 /* We have retried a decent amount, time to
302 ret = nfs4_delay(server->client, &exception->timeout);
305 case -NFS4ERR_RETRY_UNCACHED_REP:
306 case -NFS4ERR_OLD_STATEID:
307 exception->retry = 1;
309 case -NFS4ERR_BADOWNER:
310 /* The following works around a Linux server bug! */
311 case -NFS4ERR_BADNAME:
312 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
313 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
314 exception->retry = 1;
315 printk(KERN_WARNING "NFS: v4 server %s "
316 "does not accept raw "
318 "Reenabling the idmapper.\n",
319 server->nfs_client->cl_hostname);
322 /* We failed to handle the error */
323 return nfs4_map_errors(ret);
325 ret = nfs4_wait_clnt_recover(clp);
327 exception->retry = 1;
332 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
334 spin_lock(&clp->cl_lock);
335 if (time_before(clp->cl_last_renewal,timestamp))
336 clp->cl_last_renewal = timestamp;
337 spin_unlock(&clp->cl_lock);
340 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
342 do_renew_lease(server->nfs_client, timestamp);
345 #if defined(CONFIG_NFS_V4_1)
348 * nfs4_free_slot - free a slot and efficiently update slot table.
350 * freeing a slot is trivially done by clearing its respective bit
352 * If the freed slotid equals highest_used_slotid we want to update it
353 * so that the server would be able to size down the slot table if needed,
354 * otherwise we know that the highest_used_slotid is still in use.
355 * When updating highest_used_slotid there may be "holes" in the bitmap
356 * so we need to scan down from highest_used_slotid to 0 looking for the now
357 * highest slotid in use.
358 * If none found, highest_used_slotid is set to -1.
360 * Must be called while holding tbl->slot_tbl_lock
363 nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
365 int free_slotid = free_slot - tbl->slots;
366 int slotid = free_slotid;
368 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
369 /* clear used bit in bitmap */
370 __clear_bit(slotid, tbl->used_slots);
372 /* update highest_used_slotid when it is freed */
373 if (slotid == tbl->highest_used_slotid) {
374 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
375 if (slotid < tbl->max_slots)
376 tbl->highest_used_slotid = slotid;
378 tbl->highest_used_slotid = -1;
380 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
381 free_slotid, tbl->highest_used_slotid);
385 * Signal state manager thread if session fore channel is drained
387 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
389 struct rpc_task *task;
391 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
392 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
394 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
398 if (ses->fc_slot_table.highest_used_slotid != -1)
401 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
402 complete(&ses->fc_slot_table.complete);
406 * Signal state manager thread if session back channel is drained
408 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
410 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
411 ses->bc_slot_table.highest_used_slotid != -1)
413 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
414 complete(&ses->bc_slot_table.complete);
417 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
419 struct nfs4_slot_table *tbl;
421 tbl = &res->sr_session->fc_slot_table;
423 /* just wake up the next guy waiting since
424 * we may have not consumed a slot after all */
425 dprintk("%s: No slot\n", __func__);
429 spin_lock(&tbl->slot_tbl_lock);
430 nfs4_free_slot(tbl, res->sr_slot);
431 nfs4_check_drain_fc_complete(res->sr_session);
432 spin_unlock(&tbl->slot_tbl_lock);
436 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
438 unsigned long timestamp;
439 struct nfs_client *clp;
442 * sr_status remains 1 if an RPC level error occurred. The server
443 * may or may not have processed the sequence operation..
444 * Proceed as if the server received and processed the sequence
447 if (res->sr_status == 1)
448 res->sr_status = NFS_OK;
450 /* don't increment the sequence number if the task wasn't sent */
451 if (!RPC_WAS_SENT(task))
454 /* Check the SEQUENCE operation status */
455 switch (res->sr_status) {
457 /* Update the slot's sequence and clientid lease timer */
458 ++res->sr_slot->seq_nr;
459 timestamp = res->sr_renewal_time;
460 clp = res->sr_session->clp;
461 do_renew_lease(clp, timestamp);
462 /* Check sequence flags */
463 if (res->sr_status_flags != 0)
464 nfs4_schedule_lease_recovery(clp);
467 /* The server detected a resend of the RPC call and
468 * returned NFS4ERR_DELAY as per Section 2.10.6.2
471 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
473 res->sr_slot - res->sr_session->fc_slot_table.slots,
474 res->sr_slot->seq_nr);
477 /* Just update the slot sequence no. */
478 ++res->sr_slot->seq_nr;
481 /* The session may be reset by one of the error handlers. */
482 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
483 nfs41_sequence_free_slot(res);
486 if (!rpc_restart_call(task))
488 rpc_delay(task, NFS4_POLL_RETRY_MAX);
492 static int nfs4_sequence_done(struct rpc_task *task,
493 struct nfs4_sequence_res *res)
495 if (res->sr_session == NULL)
497 return nfs41_sequence_done(task, res);
501 * nfs4_find_slot - efficiently look for a free slot
503 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
504 * If found, we mark the slot as used, update the highest_used_slotid,
505 * and respectively set up the sequence operation args.
506 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
508 * Note: must be called with under the slot_tbl_lock.
511 nfs4_find_slot(struct nfs4_slot_table *tbl)
514 u8 ret_id = NFS4_MAX_SLOT_TABLE;
515 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
517 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
518 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
520 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
521 if (slotid >= tbl->max_slots)
523 __set_bit(slotid, tbl->used_slots);
524 if (slotid > tbl->highest_used_slotid)
525 tbl->highest_used_slotid = slotid;
528 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
529 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
533 int nfs41_setup_sequence(struct nfs4_session *session,
534 struct nfs4_sequence_args *args,
535 struct nfs4_sequence_res *res,
537 struct rpc_task *task)
539 struct nfs4_slot *slot;
540 struct nfs4_slot_table *tbl;
543 dprintk("--> %s\n", __func__);
544 /* slot already allocated? */
545 if (res->sr_slot != NULL)
548 tbl = &session->fc_slot_table;
550 spin_lock(&tbl->slot_tbl_lock);
551 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
552 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
554 * The state manager will wait until the slot table is empty.
555 * Schedule the reset thread
557 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
558 spin_unlock(&tbl->slot_tbl_lock);
559 dprintk("%s Schedule Session Reset\n", __func__);
563 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
564 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
565 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
566 spin_unlock(&tbl->slot_tbl_lock);
567 dprintk("%s enforce FIFO order\n", __func__);
571 slotid = nfs4_find_slot(tbl);
572 if (slotid == NFS4_MAX_SLOT_TABLE) {
573 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
574 spin_unlock(&tbl->slot_tbl_lock);
575 dprintk("<-- %s: no free slots\n", __func__);
578 spin_unlock(&tbl->slot_tbl_lock);
580 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
581 slot = tbl->slots + slotid;
582 args->sa_session = session;
583 args->sa_slotid = slotid;
584 args->sa_cache_this = cache_reply;
586 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
588 res->sr_session = session;
590 res->sr_renewal_time = jiffies;
591 res->sr_status_flags = 0;
593 * sr_status is only set in decode_sequence, and so will remain
594 * set to 1 if an rpc level failure occurs.
599 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
601 int nfs4_setup_sequence(const struct nfs_server *server,
602 struct nfs4_sequence_args *args,
603 struct nfs4_sequence_res *res,
605 struct rpc_task *task)
607 struct nfs4_session *session = nfs4_get_session(server);
610 if (session == NULL) {
611 args->sa_session = NULL;
612 res->sr_session = NULL;
616 dprintk("--> %s clp %p session %p sr_slot %td\n",
617 __func__, session->clp, session, res->sr_slot ?
618 res->sr_slot - session->fc_slot_table.slots : -1);
620 ret = nfs41_setup_sequence(session, args, res, cache_reply,
623 dprintk("<-- %s status=%d\n", __func__, ret);
627 struct nfs41_call_sync_data {
628 const struct nfs_server *seq_server;
629 struct nfs4_sequence_args *seq_args;
630 struct nfs4_sequence_res *seq_res;
634 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
636 struct nfs41_call_sync_data *data = calldata;
638 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
640 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
641 data->seq_res, data->cache_reply, task))
643 rpc_call_start(task);
646 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
648 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
649 nfs41_call_sync_prepare(task, calldata);
652 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
654 struct nfs41_call_sync_data *data = calldata;
656 nfs41_sequence_done(task, data->seq_res);
659 struct rpc_call_ops nfs41_call_sync_ops = {
660 .rpc_call_prepare = nfs41_call_sync_prepare,
661 .rpc_call_done = nfs41_call_sync_done,
664 struct rpc_call_ops nfs41_call_priv_sync_ops = {
665 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
666 .rpc_call_done = nfs41_call_sync_done,
669 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
670 struct nfs_server *server,
671 struct rpc_message *msg,
672 struct nfs4_sequence_args *args,
673 struct nfs4_sequence_res *res,
678 struct rpc_task *task;
679 struct nfs41_call_sync_data data = {
680 .seq_server = server,
683 .cache_reply = cache_reply,
685 struct rpc_task_setup task_setup = {
688 .callback_ops = &nfs41_call_sync_ops,
689 .callback_data = &data
694 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
695 task = rpc_run_task(&task_setup);
699 ret = task->tk_status;
705 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
706 struct nfs_server *server,
707 struct rpc_message *msg,
708 struct nfs4_sequence_args *args,
709 struct nfs4_sequence_res *res,
712 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
716 static int nfs4_sequence_done(struct rpc_task *task,
717 struct nfs4_sequence_res *res)
721 #endif /* CONFIG_NFS_V4_1 */
723 int _nfs4_call_sync(struct rpc_clnt *clnt,
724 struct nfs_server *server,
725 struct rpc_message *msg,
726 struct nfs4_sequence_args *args,
727 struct nfs4_sequence_res *res,
730 args->sa_session = res->sr_session = NULL;
731 return rpc_call_sync(clnt, msg, 0);
735 int nfs4_call_sync(struct rpc_clnt *clnt,
736 struct nfs_server *server,
737 struct rpc_message *msg,
738 struct nfs4_sequence_args *args,
739 struct nfs4_sequence_res *res,
742 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
743 args, res, cache_reply);
746 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
748 struct nfs_inode *nfsi = NFS_I(dir);
750 spin_lock(&dir->i_lock);
751 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
752 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
753 nfs_force_lookup_revalidate(dir);
754 nfsi->change_attr = cinfo->after;
755 spin_unlock(&dir->i_lock);
758 struct nfs4_opendata {
760 struct nfs_openargs o_arg;
761 struct nfs_openres o_res;
762 struct nfs_open_confirmargs c_arg;
763 struct nfs_open_confirmres c_res;
764 struct nfs_fattr f_attr;
765 struct nfs_fattr dir_attr;
768 struct nfs4_state_owner *owner;
769 struct nfs4_state *state;
771 unsigned long timestamp;
772 unsigned int rpc_done : 1;
778 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
780 p->o_res.f_attr = &p->f_attr;
781 p->o_res.dir_attr = &p->dir_attr;
782 p->o_res.seqid = p->o_arg.seqid;
783 p->c_res.seqid = p->c_arg.seqid;
784 p->o_res.server = p->o_arg.server;
785 nfs_fattr_init(&p->f_attr);
786 nfs_fattr_init(&p->dir_attr);
789 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
790 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
791 const struct iattr *attrs,
794 struct dentry *parent = dget_parent(path->dentry);
795 struct inode *dir = parent->d_inode;
796 struct nfs_server *server = NFS_SERVER(dir);
797 struct nfs4_opendata *p;
799 p = kzalloc(sizeof(*p), gfp_mask);
802 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
803 if (p->o_arg.seqid == NULL)
809 atomic_inc(&sp->so_count);
810 p->o_arg.fh = NFS_FH(dir);
811 p->o_arg.open_flags = flags;
812 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
813 p->o_arg.clientid = server->nfs_client->cl_clientid;
814 p->o_arg.id = sp->so_owner_id.id;
815 p->o_arg.name = &p->path.dentry->d_name;
816 p->o_arg.server = server;
817 p->o_arg.bitmask = server->attr_bitmask;
818 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
819 if (flags & O_CREAT) {
822 p->o_arg.u.attrs = &p->attrs;
823 memcpy(&p->attrs, attrs, sizeof(p->attrs));
824 s = (u32 *) p->o_arg.u.verifier.data;
828 p->c_arg.fh = &p->o_res.fh;
829 p->c_arg.stateid = &p->o_res.stateid;
830 p->c_arg.seqid = p->o_arg.seqid;
831 nfs4_init_opendata_res(p);
841 static void nfs4_opendata_free(struct kref *kref)
843 struct nfs4_opendata *p = container_of(kref,
844 struct nfs4_opendata, kref);
846 nfs_free_seqid(p->o_arg.seqid);
847 if (p->state != NULL)
848 nfs4_put_open_state(p->state);
849 nfs4_put_state_owner(p->owner);
855 static void nfs4_opendata_put(struct nfs4_opendata *p)
858 kref_put(&p->kref, nfs4_opendata_free);
861 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
865 ret = rpc_wait_for_completion_task(task);
869 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
873 if (open_mode & O_EXCL)
875 switch (mode & (FMODE_READ|FMODE_WRITE)) {
877 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
878 && state->n_rdonly != 0;
881 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
882 && state->n_wronly != 0;
884 case FMODE_READ|FMODE_WRITE:
885 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
886 && state->n_rdwr != 0;
892 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
894 if ((delegation->type & fmode) != fmode)
896 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
898 nfs_mark_delegation_referenced(delegation);
902 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
911 case FMODE_READ|FMODE_WRITE:
914 nfs4_state_set_mode_locked(state, state->state | fmode);
917 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
919 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
920 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
921 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
924 set_bit(NFS_O_RDONLY_STATE, &state->flags);
927 set_bit(NFS_O_WRONLY_STATE, &state->flags);
929 case FMODE_READ|FMODE_WRITE:
930 set_bit(NFS_O_RDWR_STATE, &state->flags);
934 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
936 write_seqlock(&state->seqlock);
937 nfs_set_open_stateid_locked(state, stateid, fmode);
938 write_sequnlock(&state->seqlock);
941 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
944 * Protect the call to nfs4_state_set_mode_locked and
945 * serialise the stateid update
947 write_seqlock(&state->seqlock);
948 if (deleg_stateid != NULL) {
949 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
950 set_bit(NFS_DELEGATED_STATE, &state->flags);
952 if (open_stateid != NULL)
953 nfs_set_open_stateid_locked(state, open_stateid, fmode);
954 write_sequnlock(&state->seqlock);
955 spin_lock(&state->owner->so_lock);
956 update_open_stateflags(state, fmode);
957 spin_unlock(&state->owner->so_lock);
960 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
962 struct nfs_inode *nfsi = NFS_I(state->inode);
963 struct nfs_delegation *deleg_cur;
966 fmode &= (FMODE_READ|FMODE_WRITE);
969 deleg_cur = rcu_dereference(nfsi->delegation);
970 if (deleg_cur == NULL)
973 spin_lock(&deleg_cur->lock);
974 if (nfsi->delegation != deleg_cur ||
975 (deleg_cur->type & fmode) != fmode)
976 goto no_delegation_unlock;
978 if (delegation == NULL)
979 delegation = &deleg_cur->stateid;
980 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
981 goto no_delegation_unlock;
983 nfs_mark_delegation_referenced(deleg_cur);
984 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
986 no_delegation_unlock:
987 spin_unlock(&deleg_cur->lock);
991 if (!ret && open_stateid != NULL) {
992 __update_open_stateid(state, open_stateid, NULL, fmode);
1000 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1002 struct nfs_delegation *delegation;
1005 delegation = rcu_dereference(NFS_I(inode)->delegation);
1006 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1011 nfs_inode_return_delegation(inode);
1014 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1016 struct nfs4_state *state = opendata->state;
1017 struct nfs_inode *nfsi = NFS_I(state->inode);
1018 struct nfs_delegation *delegation;
1019 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1020 fmode_t fmode = opendata->o_arg.fmode;
1021 nfs4_stateid stateid;
1025 if (can_open_cached(state, fmode, open_mode)) {
1026 spin_lock(&state->owner->so_lock);
1027 if (can_open_cached(state, fmode, open_mode)) {
1028 update_open_stateflags(state, fmode);
1029 spin_unlock(&state->owner->so_lock);
1030 goto out_return_state;
1032 spin_unlock(&state->owner->so_lock);
1035 delegation = rcu_dereference(nfsi->delegation);
1036 if (delegation == NULL ||
1037 !can_open_delegated(delegation, fmode)) {
1041 /* Save the delegation */
1042 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1044 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1049 /* Try to update the stateid using the delegation */
1050 if (update_open_stateid(state, NULL, &stateid, fmode))
1051 goto out_return_state;
1054 return ERR_PTR(ret);
1056 atomic_inc(&state->count);
1060 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1062 struct inode *inode;
1063 struct nfs4_state *state = NULL;
1064 struct nfs_delegation *delegation;
1067 if (!data->rpc_done) {
1068 state = nfs4_try_open_cached(data);
1073 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1075 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1076 ret = PTR_ERR(inode);
1080 state = nfs4_get_open_state(inode, data->owner);
1083 if (data->o_res.delegation_type != 0) {
1084 int delegation_flags = 0;
1087 delegation = rcu_dereference(NFS_I(inode)->delegation);
1089 delegation_flags = delegation->flags;
1091 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1092 nfs_inode_set_delegation(state->inode,
1093 data->owner->so_cred,
1096 nfs_inode_reclaim_delegation(state->inode,
1097 data->owner->so_cred,
1101 update_open_stateid(state, &data->o_res.stateid, NULL,
1109 return ERR_PTR(ret);
1112 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1114 struct nfs_inode *nfsi = NFS_I(state->inode);
1115 struct nfs_open_context *ctx;
1117 spin_lock(&state->inode->i_lock);
1118 list_for_each_entry(ctx, &nfsi->open_files, list) {
1119 if (ctx->state != state)
1121 get_nfs_open_context(ctx);
1122 spin_unlock(&state->inode->i_lock);
1125 spin_unlock(&state->inode->i_lock);
1126 return ERR_PTR(-ENOENT);
1129 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1131 struct nfs4_opendata *opendata;
1133 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
1134 if (opendata == NULL)
1135 return ERR_PTR(-ENOMEM);
1136 opendata->state = state;
1137 atomic_inc(&state->count);
1141 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1143 struct nfs4_state *newstate;
1146 opendata->o_arg.open_flags = 0;
1147 opendata->o_arg.fmode = fmode;
1148 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1149 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1150 nfs4_init_opendata_res(opendata);
1151 ret = _nfs4_recover_proc_open(opendata);
1154 newstate = nfs4_opendata_to_nfs4_state(opendata);
1155 if (IS_ERR(newstate))
1156 return PTR_ERR(newstate);
1157 nfs4_close_state(newstate, fmode);
1162 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1164 struct nfs4_state *newstate;
1167 /* memory barrier prior to reading state->n_* */
1168 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1170 if (state->n_rdwr != 0) {
1171 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1172 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1175 if (newstate != state)
1178 if (state->n_wronly != 0) {
1179 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1180 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1183 if (newstate != state)
1186 if (state->n_rdonly != 0) {
1187 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1188 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1191 if (newstate != state)
1195 * We may have performed cached opens for all three recoveries.
1196 * Check if we need to update the current stateid.
1198 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1199 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1200 write_seqlock(&state->seqlock);
1201 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1202 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1203 write_sequnlock(&state->seqlock);
1210 * reclaim state on the server after a reboot.
1212 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1214 struct nfs_delegation *delegation;
1215 struct nfs4_opendata *opendata;
1216 fmode_t delegation_type = 0;
1219 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1220 if (IS_ERR(opendata))
1221 return PTR_ERR(opendata);
1222 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1223 opendata->o_arg.fh = NFS_FH(state->inode);
1225 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1226 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1227 delegation_type = delegation->type;
1229 opendata->o_arg.u.delegation_type = delegation_type;
1230 status = nfs4_open_recover(opendata, state);
1231 nfs4_opendata_put(opendata);
1235 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1237 struct nfs_server *server = NFS_SERVER(state->inode);
1238 struct nfs4_exception exception = { };
1241 err = _nfs4_do_open_reclaim(ctx, state);
1242 if (err != -NFS4ERR_DELAY)
1244 nfs4_handle_exception(server, err, &exception);
1245 } while (exception.retry);
1249 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1251 struct nfs_open_context *ctx;
1254 ctx = nfs4_state_find_open_context(state);
1256 return PTR_ERR(ctx);
1257 ret = nfs4_do_open_reclaim(ctx, state);
1258 put_nfs_open_context(ctx);
1262 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1264 struct nfs4_opendata *opendata;
1267 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1268 if (IS_ERR(opendata))
1269 return PTR_ERR(opendata);
1270 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1271 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1272 sizeof(opendata->o_arg.u.delegation.data));
1273 ret = nfs4_open_recover(opendata, state);
1274 nfs4_opendata_put(opendata);
1278 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1280 struct nfs4_exception exception = { };
1281 struct nfs_server *server = NFS_SERVER(state->inode);
1284 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1290 case -NFS4ERR_BADSESSION:
1291 case -NFS4ERR_BADSLOT:
1292 case -NFS4ERR_BAD_HIGH_SLOT:
1293 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1294 case -NFS4ERR_DEADSESSION:
1295 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
1297 case -NFS4ERR_STALE_CLIENTID:
1298 case -NFS4ERR_STALE_STATEID:
1299 case -NFS4ERR_EXPIRED:
1300 /* Don't recall a delegation if it was lost */
1301 nfs4_schedule_lease_recovery(server->nfs_client);
1305 * The show must go on: exit, but mark the
1306 * stateid as needing recovery.
1308 case -NFS4ERR_ADMIN_REVOKED:
1309 case -NFS4ERR_BAD_STATEID:
1310 nfs4_schedule_stateid_recovery(server, state);
1313 * User RPCSEC_GSS context has expired.
1314 * We cannot recover this stateid now, so
1315 * skip it and allow recovery thread to
1322 err = nfs4_handle_exception(server, err, &exception);
1323 } while (exception.retry);
1328 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1330 struct nfs4_opendata *data = calldata;
1332 data->rpc_status = task->tk_status;
1333 if (data->rpc_status == 0) {
1334 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1335 sizeof(data->o_res.stateid.data));
1336 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1337 renew_lease(data->o_res.server, data->timestamp);
1342 static void nfs4_open_confirm_release(void *calldata)
1344 struct nfs4_opendata *data = calldata;
1345 struct nfs4_state *state = NULL;
1347 /* If this request hasn't been cancelled, do nothing */
1348 if (data->cancelled == 0)
1350 /* In case of error, no cleanup! */
1351 if (!data->rpc_done)
1353 state = nfs4_opendata_to_nfs4_state(data);
1355 nfs4_close_state(state, data->o_arg.fmode);
1357 nfs4_opendata_put(data);
1360 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1361 .rpc_call_done = nfs4_open_confirm_done,
1362 .rpc_release = nfs4_open_confirm_release,
1366 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1368 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1370 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1371 struct rpc_task *task;
1372 struct rpc_message msg = {
1373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1374 .rpc_argp = &data->c_arg,
1375 .rpc_resp = &data->c_res,
1376 .rpc_cred = data->owner->so_cred,
1378 struct rpc_task_setup task_setup_data = {
1379 .rpc_client = server->client,
1380 .rpc_message = &msg,
1381 .callback_ops = &nfs4_open_confirm_ops,
1382 .callback_data = data,
1383 .workqueue = nfsiod_workqueue,
1384 .flags = RPC_TASK_ASYNC,
1388 kref_get(&data->kref);
1390 data->rpc_status = 0;
1391 data->timestamp = jiffies;
1392 task = rpc_run_task(&task_setup_data);
1394 return PTR_ERR(task);
1395 status = nfs4_wait_for_completion_rpc_task(task);
1397 data->cancelled = 1;
1400 status = data->rpc_status;
1405 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1407 struct nfs4_opendata *data = calldata;
1408 struct nfs4_state_owner *sp = data->owner;
1410 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1413 * Check if we still need to send an OPEN call, or if we can use
1414 * a delegation instead.
1416 if (data->state != NULL) {
1417 struct nfs_delegation *delegation;
1419 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1422 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1423 if (delegation != NULL &&
1424 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1430 /* Update sequence id. */
1431 data->o_arg.id = sp->so_owner_id.id;
1432 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1433 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1434 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1435 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1437 data->timestamp = jiffies;
1438 if (nfs4_setup_sequence(data->o_arg.server,
1439 &data->o_arg.seq_args,
1440 &data->o_res.seq_res, 1, task))
1442 rpc_call_start(task);
1445 task->tk_action = NULL;
1449 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1451 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1452 nfs4_open_prepare(task, calldata);
1455 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1457 struct nfs4_opendata *data = calldata;
1459 data->rpc_status = task->tk_status;
1461 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1464 if (task->tk_status == 0) {
1465 switch (data->o_res.f_attr->mode & S_IFMT) {
1469 data->rpc_status = -ELOOP;
1472 data->rpc_status = -EISDIR;
1475 data->rpc_status = -ENOTDIR;
1477 renew_lease(data->o_res.server, data->timestamp);
1478 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1479 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1484 static void nfs4_open_release(void *calldata)
1486 struct nfs4_opendata *data = calldata;
1487 struct nfs4_state *state = NULL;
1489 /* If this request hasn't been cancelled, do nothing */
1490 if (data->cancelled == 0)
1492 /* In case of error, no cleanup! */
1493 if (data->rpc_status != 0 || !data->rpc_done)
1495 /* In case we need an open_confirm, no cleanup! */
1496 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1498 state = nfs4_opendata_to_nfs4_state(data);
1500 nfs4_close_state(state, data->o_arg.fmode);
1502 nfs4_opendata_put(data);
1505 static const struct rpc_call_ops nfs4_open_ops = {
1506 .rpc_call_prepare = nfs4_open_prepare,
1507 .rpc_call_done = nfs4_open_done,
1508 .rpc_release = nfs4_open_release,
1511 static const struct rpc_call_ops nfs4_recover_open_ops = {
1512 .rpc_call_prepare = nfs4_recover_open_prepare,
1513 .rpc_call_done = nfs4_open_done,
1514 .rpc_release = nfs4_open_release,
1517 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1519 struct inode *dir = data->dir->d_inode;
1520 struct nfs_server *server = NFS_SERVER(dir);
1521 struct nfs_openargs *o_arg = &data->o_arg;
1522 struct nfs_openres *o_res = &data->o_res;
1523 struct rpc_task *task;
1524 struct rpc_message msg = {
1525 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1528 .rpc_cred = data->owner->so_cred,
1530 struct rpc_task_setup task_setup_data = {
1531 .rpc_client = server->client,
1532 .rpc_message = &msg,
1533 .callback_ops = &nfs4_open_ops,
1534 .callback_data = data,
1535 .workqueue = nfsiod_workqueue,
1536 .flags = RPC_TASK_ASYNC,
1540 kref_get(&data->kref);
1542 data->rpc_status = 0;
1543 data->cancelled = 0;
1545 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1546 task = rpc_run_task(&task_setup_data);
1548 return PTR_ERR(task);
1549 status = nfs4_wait_for_completion_rpc_task(task);
1551 data->cancelled = 1;
1554 status = data->rpc_status;
1560 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1562 struct inode *dir = data->dir->d_inode;
1563 struct nfs_openres *o_res = &data->o_res;
1566 status = nfs4_run_open_task(data, 1);
1567 if (status != 0 || !data->rpc_done)
1570 nfs_refresh_inode(dir, o_res->dir_attr);
1572 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1573 status = _nfs4_proc_open_confirm(data);
1582 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1584 static int _nfs4_proc_open(struct nfs4_opendata *data)
1586 struct inode *dir = data->dir->d_inode;
1587 struct nfs_server *server = NFS_SERVER(dir);
1588 struct nfs_openargs *o_arg = &data->o_arg;
1589 struct nfs_openres *o_res = &data->o_res;
1592 status = nfs4_run_open_task(data, 0);
1593 if (status != 0 || !data->rpc_done)
1596 if (o_arg->open_flags & O_CREAT) {
1597 update_changeattr(dir, &o_res->cinfo);
1598 nfs_post_op_update_inode(dir, o_res->dir_attr);
1600 nfs_refresh_inode(dir, o_res->dir_attr);
1601 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1602 server->caps &= ~NFS_CAP_POSIX_LOCK;
1603 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1604 status = _nfs4_proc_open_confirm(data);
1608 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1609 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1613 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1618 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1619 ret = nfs4_wait_clnt_recover(clp);
1622 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1623 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1625 nfs4_schedule_state_manager(clp);
1631 static int nfs4_recover_expired_lease(struct nfs_server *server)
1633 return nfs4_client_recover_expired_lease(server->nfs_client);
1638 * reclaim state on the server after a network partition.
1639 * Assumes caller holds the appropriate lock
1641 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1643 struct nfs4_opendata *opendata;
1646 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1647 if (IS_ERR(opendata))
1648 return PTR_ERR(opendata);
1649 ret = nfs4_open_recover(opendata, state);
1651 d_drop(ctx->path.dentry);
1652 nfs4_opendata_put(opendata);
1656 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1658 struct nfs_server *server = NFS_SERVER(state->inode);
1659 struct nfs4_exception exception = { };
1663 err = _nfs4_open_expired(ctx, state);
1667 case -NFS4ERR_GRACE:
1668 case -NFS4ERR_DELAY:
1669 nfs4_handle_exception(server, err, &exception);
1672 } while (exception.retry);
1677 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1679 struct nfs_open_context *ctx;
1682 ctx = nfs4_state_find_open_context(state);
1684 return PTR_ERR(ctx);
1685 ret = nfs4_do_open_expired(ctx, state);
1686 put_nfs_open_context(ctx);
1691 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1692 * fields corresponding to attributes that were used to store the verifier.
1693 * Make sure we clobber those fields in the later setattr call
1695 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1697 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1698 !(sattr->ia_valid & ATTR_ATIME_SET))
1699 sattr->ia_valid |= ATTR_ATIME;
1701 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1702 !(sattr->ia_valid & ATTR_MTIME_SET))
1703 sattr->ia_valid |= ATTR_MTIME;
1707 * Returns a referenced nfs4_state
1709 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)
1711 struct nfs4_state_owner *sp;
1712 struct nfs4_state *state = NULL;
1713 struct nfs_server *server = NFS_SERVER(dir);
1714 struct nfs4_opendata *opendata;
1717 /* Protect against reboot recovery conflicts */
1719 if (!(sp = nfs4_get_state_owner(server, cred))) {
1720 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1723 status = nfs4_recover_expired_lease(server);
1725 goto err_put_state_owner;
1726 if (path->dentry->d_inode != NULL)
1727 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1729 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
1730 if (opendata == NULL)
1731 goto err_put_state_owner;
1733 if (path->dentry->d_inode != NULL)
1734 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1736 status = _nfs4_proc_open(opendata);
1738 goto err_opendata_put;
1740 state = nfs4_opendata_to_nfs4_state(opendata);
1741 status = PTR_ERR(state);
1743 goto err_opendata_put;
1744 if (server->caps & NFS_CAP_POSIX_LOCK)
1745 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1747 if (opendata->o_arg.open_flags & O_EXCL) {
1748 nfs4_exclusive_attrset(opendata, sattr);
1750 nfs_fattr_init(opendata->o_res.f_attr);
1751 status = nfs4_do_setattr(state->inode, cred,
1752 opendata->o_res.f_attr, sattr,
1755 nfs_setattr_update_inode(state->inode, sattr);
1756 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1758 nfs4_opendata_put(opendata);
1759 nfs4_put_state_owner(sp);
1763 nfs4_opendata_put(opendata);
1764 err_put_state_owner:
1765 nfs4_put_state_owner(sp);
1772 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)
1774 struct nfs4_exception exception = { };
1775 struct nfs4_state *res;
1779 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1782 /* NOTE: BAD_SEQID means the server and client disagree about the
1783 * book-keeping w.r.t. state-changing operations
1784 * (OPEN/CLOSE/LOCK/LOCKU...)
1785 * It is actually a sign of a bug on the client or on the server.
1787 * If we receive a BAD_SEQID error in the particular case of
1788 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1789 * have unhashed the old state_owner for us, and that we can
1790 * therefore safely retry using a new one. We should still warn
1791 * the user though...
1793 if (status == -NFS4ERR_BAD_SEQID) {
1794 printk(KERN_WARNING "NFS: v4 server %s "
1795 " returned a bad sequence-id error!\n",
1796 NFS_SERVER(dir)->nfs_client->cl_hostname);
1797 exception.retry = 1;
1801 * BAD_STATEID on OPEN means that the server cancelled our
1802 * state before it received the OPEN_CONFIRM.
1803 * Recover by retrying the request as per the discussion
1804 * on Page 181 of RFC3530.
1806 if (status == -NFS4ERR_BAD_STATEID) {
1807 exception.retry = 1;
1810 if (status == -EAGAIN) {
1811 /* We must have found a delegation */
1812 exception.retry = 1;
1815 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1816 status, &exception));
1817 } while (exception.retry);
1821 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1822 struct nfs_fattr *fattr, struct iattr *sattr,
1823 struct nfs4_state *state)
1825 struct nfs_server *server = NFS_SERVER(inode);
1826 struct nfs_setattrargs arg = {
1827 .fh = NFS_FH(inode),
1830 .bitmask = server->attr_bitmask,
1832 struct nfs_setattrres res = {
1836 struct rpc_message msg = {
1837 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1842 unsigned long timestamp = jiffies;
1845 nfs_fattr_init(fattr);
1847 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1848 /* Use that stateid */
1849 } else if (state != NULL) {
1850 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
1852 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1854 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1855 if (status == 0 && state != NULL)
1856 renew_lease(server, timestamp);
1860 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1861 struct nfs_fattr *fattr, struct iattr *sattr,
1862 struct nfs4_state *state)
1864 struct nfs_server *server = NFS_SERVER(inode);
1865 struct nfs4_exception exception = { };
1868 err = nfs4_handle_exception(server,
1869 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1871 } while (exception.retry);
1875 struct nfs4_closedata {
1876 struct inode *inode;
1877 struct nfs4_state *state;
1878 struct nfs_closeargs arg;
1879 struct nfs_closeres res;
1880 struct nfs_fattr fattr;
1881 unsigned long timestamp;
1886 static void nfs4_free_closedata(void *data)
1888 struct nfs4_closedata *calldata = data;
1889 struct nfs4_state_owner *sp = calldata->state->owner;
1890 struct super_block *sb = calldata->state->inode->i_sb;
1893 pnfs_roc_release(calldata->state->inode);
1894 nfs4_put_open_state(calldata->state);
1895 nfs_free_seqid(calldata->arg.seqid);
1896 nfs4_put_state_owner(sp);
1897 nfs_sb_deactive(sb);
1901 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1904 spin_lock(&state->owner->so_lock);
1905 if (!(fmode & FMODE_READ))
1906 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1907 if (!(fmode & FMODE_WRITE))
1908 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1909 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1910 spin_unlock(&state->owner->so_lock);
1913 static void nfs4_close_done(struct rpc_task *task, void *data)
1915 struct nfs4_closedata *calldata = data;
1916 struct nfs4_state *state = calldata->state;
1917 struct nfs_server *server = NFS_SERVER(calldata->inode);
1919 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1921 /* hmm. we are done with the inode, and in the process of freeing
1922 * the state_owner. we keep this around to process errors
1924 switch (task->tk_status) {
1927 pnfs_roc_set_barrier(state->inode,
1928 calldata->roc_barrier);
1929 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1930 renew_lease(server, calldata->timestamp);
1931 nfs4_close_clear_stateid_flags(state,
1932 calldata->arg.fmode);
1934 case -NFS4ERR_STALE_STATEID:
1935 case -NFS4ERR_OLD_STATEID:
1936 case -NFS4ERR_BAD_STATEID:
1937 case -NFS4ERR_EXPIRED:
1938 if (calldata->arg.fmode == 0)
1941 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1942 rpc_restart_call_prepare(task);
1944 nfs_release_seqid(calldata->arg.seqid);
1945 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1948 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1950 struct nfs4_closedata *calldata = data;
1951 struct nfs4_state *state = calldata->state;
1954 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1957 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1958 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
1959 spin_lock(&state->owner->so_lock);
1960 /* Calculate the change in open mode */
1961 if (state->n_rdwr == 0) {
1962 if (state->n_rdonly == 0) {
1963 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1964 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1965 calldata->arg.fmode &= ~FMODE_READ;
1967 if (state->n_wronly == 0) {
1968 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1969 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1970 calldata->arg.fmode &= ~FMODE_WRITE;
1973 spin_unlock(&state->owner->so_lock);
1976 /* Note: exit _without_ calling nfs4_close_done */
1977 task->tk_action = NULL;
1981 if (calldata->arg.fmode == 0) {
1982 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1983 if (calldata->roc &&
1984 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
1985 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
1991 nfs_fattr_init(calldata->res.fattr);
1992 calldata->timestamp = jiffies;
1993 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
1994 &calldata->arg.seq_args, &calldata->res.seq_res,
1997 rpc_call_start(task);
2000 static const struct rpc_call_ops nfs4_close_ops = {
2001 .rpc_call_prepare = nfs4_close_prepare,
2002 .rpc_call_done = nfs4_close_done,
2003 .rpc_release = nfs4_free_closedata,
2007 * It is possible for data to be read/written from a mem-mapped file
2008 * after the sys_close call (which hits the vfs layer as a flush).
2009 * This means that we can't safely call nfsv4 close on a file until
2010 * the inode is cleared. This in turn means that we are not good
2011 * NFSv4 citizens - we do not indicate to the server to update the file's
2012 * share state even when we are done with one of the three share
2013 * stateid's in the inode.
2015 * NOTE: Caller must be holding the sp->so_owner semaphore!
2017 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
2019 struct nfs_server *server = NFS_SERVER(state->inode);
2020 struct nfs4_closedata *calldata;
2021 struct nfs4_state_owner *sp = state->owner;
2022 struct rpc_task *task;
2023 struct rpc_message msg = {
2024 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2025 .rpc_cred = state->owner->so_cred,
2027 struct rpc_task_setup task_setup_data = {
2028 .rpc_client = server->client,
2029 .rpc_message = &msg,
2030 .callback_ops = &nfs4_close_ops,
2031 .workqueue = nfsiod_workqueue,
2032 .flags = RPC_TASK_ASYNC,
2034 int status = -ENOMEM;
2036 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2037 if (calldata == NULL)
2039 calldata->inode = state->inode;
2040 calldata->state = state;
2041 calldata->arg.fh = NFS_FH(state->inode);
2042 calldata->arg.stateid = &state->open_stateid;
2043 /* Serialization for the sequence id */
2044 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2045 if (calldata->arg.seqid == NULL)
2046 goto out_free_calldata;
2047 calldata->arg.fmode = 0;
2048 calldata->arg.bitmask = server->cache_consistency_bitmask;
2049 calldata->res.fattr = &calldata->fattr;
2050 calldata->res.seqid = calldata->arg.seqid;
2051 calldata->res.server = server;
2052 calldata->roc = roc;
2053 nfs_sb_active(calldata->inode->i_sb);
2055 msg.rpc_argp = &calldata->arg;
2056 msg.rpc_resp = &calldata->res;
2057 task_setup_data.callback_data = calldata;
2058 task = rpc_run_task(&task_setup_data);
2060 return PTR_ERR(task);
2063 status = rpc_wait_for_completion_task(task);
2070 pnfs_roc_release(state->inode);
2071 nfs4_put_open_state(state);
2072 nfs4_put_state_owner(sp);
2076 static struct inode *
2077 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2079 struct nfs4_state *state;
2081 /* Protect against concurrent sillydeletes */
2082 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
2084 return ERR_CAST(state);
2086 return igrab(state->inode);
2089 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2091 if (ctx->state == NULL)
2094 nfs4_close_sync(ctx->state, ctx->mode);
2096 nfs4_close_state(ctx->state, ctx->mode);
2099 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2101 struct nfs4_server_caps_arg args = {
2104 struct nfs4_server_caps_res res = {};
2105 struct rpc_message msg = {
2106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2112 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2114 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2115 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2116 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2117 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2118 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2119 NFS_CAP_CTIME|NFS_CAP_MTIME);
2120 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2121 server->caps |= NFS_CAP_ACLS;
2122 if (res.has_links != 0)
2123 server->caps |= NFS_CAP_HARDLINKS;
2124 if (res.has_symlinks != 0)
2125 server->caps |= NFS_CAP_SYMLINKS;
2126 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2127 server->caps |= NFS_CAP_FILEID;
2128 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2129 server->caps |= NFS_CAP_MODE;
2130 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2131 server->caps |= NFS_CAP_NLINK;
2132 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2133 server->caps |= NFS_CAP_OWNER;
2134 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2135 server->caps |= NFS_CAP_OWNER_GROUP;
2136 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2137 server->caps |= NFS_CAP_ATIME;
2138 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2139 server->caps |= NFS_CAP_CTIME;
2140 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2141 server->caps |= NFS_CAP_MTIME;
2143 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2144 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2145 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2146 server->acl_bitmask = res.acl_bitmask;
2152 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2154 struct nfs4_exception exception = { };
2157 err = nfs4_handle_exception(server,
2158 _nfs4_server_capabilities(server, fhandle),
2160 } while (exception.retry);
2164 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2165 struct nfs_fsinfo *info)
2167 struct nfs4_lookup_root_arg args = {
2168 .bitmask = nfs4_fattr_bitmap,
2170 struct nfs4_lookup_res res = {
2172 .fattr = info->fattr,
2175 struct rpc_message msg = {
2176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2181 nfs_fattr_init(info->fattr);
2182 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2185 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2186 struct nfs_fsinfo *info)
2188 struct nfs4_exception exception = { };
2191 err = _nfs4_lookup_root(server, fhandle, info);
2194 case -NFS4ERR_WRONGSEC:
2197 err = nfs4_handle_exception(server, err, &exception);
2199 } while (exception.retry);
2203 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2204 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2206 struct rpc_auth *auth;
2209 auth = rpcauth_create(flavor, server->client);
2214 ret = nfs4_lookup_root(server, fhandle, info);
2219 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2220 struct nfs_fsinfo *info)
2222 int i, len, status = 0;
2223 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2225 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2226 flav_array[len] = RPC_AUTH_NULL;
2229 for (i = 0; i < len; i++) {
2230 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2231 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2236 * -EACCESS could mean that the user doesn't have correct permissions
2237 * to access the mount. It could also mean that we tried to mount
2238 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2239 * existing mount programs don't handle -EACCES very well so it should
2240 * be mapped to -EPERM instead.
2242 if (status == -EACCES)
2248 * get the file handle for the "/" directory on the server
2250 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2251 struct nfs_fsinfo *info)
2253 int status = nfs4_lookup_root(server, fhandle, info);
2254 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2256 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2257 * by nfs4_map_errors() as this function exits.
2259 status = nfs4_find_root_sec(server, fhandle, info);
2261 status = nfs4_server_capabilities(server, fhandle);
2263 status = nfs4_do_fsinfo(server, fhandle, info);
2264 return nfs4_map_errors(status);
2267 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
2269 * Get locations and (maybe) other attributes of a referral.
2270 * Note that we'll actually follow the referral later when
2271 * we detect fsid mismatch in inode revalidation
2273 static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2274 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2276 int status = -ENOMEM;
2277 struct page *page = NULL;
2278 struct nfs4_fs_locations *locations = NULL;
2280 page = alloc_page(GFP_KERNEL);
2283 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2284 if (locations == NULL)
2287 status = nfs4_proc_fs_locations(dir, name, locations, page);
2290 /* Make sure server returned a different fsid for the referral */
2291 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2292 dprintk("%s: server did not return a different fsid for"
2293 " a referral at %s\n", __func__, name->name);
2297 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2298 nfs_fixup_referral_attributes(&locations->fattr);
2300 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2301 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2302 memset(fhandle, 0, sizeof(struct nfs_fh));
2310 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2312 struct nfs4_getattr_arg args = {
2314 .bitmask = server->attr_bitmask,
2316 struct nfs4_getattr_res res = {
2320 struct rpc_message msg = {
2321 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2326 nfs_fattr_init(fattr);
2327 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2330 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2332 struct nfs4_exception exception = { };
2335 err = nfs4_handle_exception(server,
2336 _nfs4_proc_getattr(server, fhandle, fattr),
2338 } while (exception.retry);
2343 * The file is not closed if it is opened due to the a request to change
2344 * the size of the file. The open call will not be needed once the
2345 * VFS layer lookup-intents are implemented.
2347 * Close is called when the inode is destroyed.
2348 * If we haven't opened the file for O_WRONLY, we
2349 * need to in the size_change case to obtain a stateid.
2352 * Because OPEN is always done by name in nfsv4, it is
2353 * possible that we opened a different file by the same
2354 * name. We can recognize this race condition, but we
2355 * can't do anything about it besides returning an error.
2357 * This will be fixed with VFS changes (lookup-intent).
2360 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2361 struct iattr *sattr)
2363 struct inode *inode = dentry->d_inode;
2364 struct rpc_cred *cred = NULL;
2365 struct nfs4_state *state = NULL;
2368 if (pnfs_ld_layoutret_on_setattr(inode))
2369 pnfs_return_layout(inode);
2371 nfs_fattr_init(fattr);
2373 /* Search for an existing open(O_WRITE) file */
2374 if (sattr->ia_valid & ATTR_FILE) {
2375 struct nfs_open_context *ctx;
2377 ctx = nfs_file_open_context(sattr->ia_file);
2384 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2386 nfs_setattr_update_inode(inode, sattr);
2390 static int _nfs4_proc_lookupfh(struct rpc_clnt *clnt, struct nfs_server *server,
2391 const struct nfs_fh *dirfh, const struct qstr *name,
2392 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2395 struct nfs4_lookup_arg args = {
2396 .bitmask = server->attr_bitmask,
2400 struct nfs4_lookup_res res = {
2405 struct rpc_message msg = {
2406 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2411 nfs_fattr_init(fattr);
2413 dprintk("NFS call lookupfh %s\n", name->name);
2414 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2415 dprintk("NFS reply lookupfh: %d\n", status);
2419 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2420 struct qstr *name, struct nfs_fh *fhandle,
2421 struct nfs_fattr *fattr)
2423 struct nfs4_exception exception = { };
2426 err = _nfs4_proc_lookupfh(server->client, server, dirfh, name, fhandle, fattr);
2428 if (err == -NFS4ERR_MOVED) {
2432 err = nfs4_handle_exception(server, err, &exception);
2433 } while (exception.retry);
2437 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2438 const struct qstr *name, struct nfs_fh *fhandle,
2439 struct nfs_fattr *fattr)
2443 dprintk("NFS call lookup %s\n", name->name);
2444 status = _nfs4_proc_lookupfh(clnt, NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2445 if (status == -NFS4ERR_MOVED)
2446 status = nfs4_get_referral(dir, name, fattr, fhandle);
2447 dprintk("NFS reply lookup: %d\n", status);
2451 void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2453 memset(fh, 0, sizeof(struct nfs_fh));
2454 fattr->fsid.major = 1;
2455 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2456 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2457 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2461 static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2462 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2464 struct nfs4_exception exception = { };
2467 err = nfs4_handle_exception(NFS_SERVER(dir),
2468 _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr),
2471 nfs_fixup_secinfo_attributes(fattr, fhandle);
2472 } while (exception.retry);
2476 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2478 struct nfs_server *server = NFS_SERVER(inode);
2479 struct nfs4_accessargs args = {
2480 .fh = NFS_FH(inode),
2481 .bitmask = server->attr_bitmask,
2483 struct nfs4_accessres res = {
2486 struct rpc_message msg = {
2487 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2490 .rpc_cred = entry->cred,
2492 int mode = entry->mask;
2496 * Determine which access bits we want to ask for...
2498 if (mode & MAY_READ)
2499 args.access |= NFS4_ACCESS_READ;
2500 if (S_ISDIR(inode->i_mode)) {
2501 if (mode & MAY_WRITE)
2502 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2503 if (mode & MAY_EXEC)
2504 args.access |= NFS4_ACCESS_LOOKUP;
2506 if (mode & MAY_WRITE)
2507 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2508 if (mode & MAY_EXEC)
2509 args.access |= NFS4_ACCESS_EXECUTE;
2512 res.fattr = nfs_alloc_fattr();
2513 if (res.fattr == NULL)
2516 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2519 if (res.access & NFS4_ACCESS_READ)
2520 entry->mask |= MAY_READ;
2521 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2522 entry->mask |= MAY_WRITE;
2523 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2524 entry->mask |= MAY_EXEC;
2525 nfs_refresh_inode(inode, res.fattr);
2527 nfs_free_fattr(res.fattr);
2531 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2533 struct nfs4_exception exception = { };
2536 err = nfs4_handle_exception(NFS_SERVER(inode),
2537 _nfs4_proc_access(inode, entry),
2539 } while (exception.retry);
2544 * TODO: For the time being, we don't try to get any attributes
2545 * along with any of the zero-copy operations READ, READDIR,
2548 * In the case of the first three, we want to put the GETATTR
2549 * after the read-type operation -- this is because it is hard
2550 * to predict the length of a GETATTR response in v4, and thus
2551 * align the READ data correctly. This means that the GETATTR
2552 * may end up partially falling into the page cache, and we should
2553 * shift it into the 'tail' of the xdr_buf before processing.
2554 * To do this efficiently, we need to know the total length
2555 * of data received, which doesn't seem to be available outside
2558 * In the case of WRITE, we also want to put the GETATTR after
2559 * the operation -- in this case because we want to make sure
2560 * we get the post-operation mtime and size. This means that
2561 * we can't use xdr_encode_pages() as written: we need a variant
2562 * of it which would leave room in the 'tail' iovec.
2564 * Both of these changes to the XDR layer would in fact be quite
2565 * minor, but I decided to leave them for a subsequent patch.
2567 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2568 unsigned int pgbase, unsigned int pglen)
2570 struct nfs4_readlink args = {
2571 .fh = NFS_FH(inode),
2576 struct nfs4_readlink_res res;
2577 struct rpc_message msg = {
2578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2583 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2586 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2587 unsigned int pgbase, unsigned int pglen)
2589 struct nfs4_exception exception = { };
2592 err = nfs4_handle_exception(NFS_SERVER(inode),
2593 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2595 } while (exception.retry);
2601 * We will need to arrange for the VFS layer to provide an atomic open.
2602 * Until then, this create/open method is prone to inefficiency and race
2603 * conditions due to the lookup, create, and open VFS calls from sys_open()
2604 * placed on the wire.
2606 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2607 * The file will be opened again in the subsequent VFS open call
2608 * (nfs4_proc_file_open).
2610 * The open for read will just hang around to be used by any process that
2611 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2615 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2616 int flags, struct nfs_open_context *ctx)
2618 struct path my_path = {
2621 struct path *path = &my_path;
2622 struct nfs4_state *state;
2623 struct rpc_cred *cred = NULL;
2632 sattr->ia_mode &= ~current_umask();
2633 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
2635 if (IS_ERR(state)) {
2636 status = PTR_ERR(state);
2639 d_add(dentry, igrab(state->inode));
2640 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2644 nfs4_close_sync(state, fmode);
2649 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2651 struct nfs_server *server = NFS_SERVER(dir);
2652 struct nfs_removeargs args = {
2654 .name.len = name->len,
2655 .name.name = name->name,
2656 .bitmask = server->attr_bitmask,
2658 struct nfs_removeres res = {
2661 struct rpc_message msg = {
2662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2666 int status = -ENOMEM;
2668 res.dir_attr = nfs_alloc_fattr();
2669 if (res.dir_attr == NULL)
2672 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2674 update_changeattr(dir, &res.cinfo);
2675 nfs_post_op_update_inode(dir, res.dir_attr);
2677 nfs_free_fattr(res.dir_attr);
2682 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2684 struct nfs4_exception exception = { };
2687 err = nfs4_handle_exception(NFS_SERVER(dir),
2688 _nfs4_proc_remove(dir, name),
2690 } while (exception.retry);
2694 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2696 struct nfs_server *server = NFS_SERVER(dir);
2697 struct nfs_removeargs *args = msg->rpc_argp;
2698 struct nfs_removeres *res = msg->rpc_resp;
2700 args->bitmask = server->cache_consistency_bitmask;
2701 res->server = server;
2702 res->seq_res.sr_slot = NULL;
2703 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2706 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2708 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2710 if (!nfs4_sequence_done(task, &res->seq_res))
2712 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2714 update_changeattr(dir, &res->cinfo);
2715 nfs_post_op_update_inode(dir, res->dir_attr);
2719 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2721 struct nfs_server *server = NFS_SERVER(dir);
2722 struct nfs_renameargs *arg = msg->rpc_argp;
2723 struct nfs_renameres *res = msg->rpc_resp;
2725 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2726 arg->bitmask = server->attr_bitmask;
2727 res->server = server;
2730 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2731 struct inode *new_dir)
2733 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2735 if (!nfs4_sequence_done(task, &res->seq_res))
2737 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2740 update_changeattr(old_dir, &res->old_cinfo);
2741 nfs_post_op_update_inode(old_dir, res->old_fattr);
2742 update_changeattr(new_dir, &res->new_cinfo);
2743 nfs_post_op_update_inode(new_dir, res->new_fattr);
2747 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2748 struct inode *new_dir, struct qstr *new_name)
2750 struct nfs_server *server = NFS_SERVER(old_dir);
2751 struct nfs_renameargs arg = {
2752 .old_dir = NFS_FH(old_dir),
2753 .new_dir = NFS_FH(new_dir),
2754 .old_name = old_name,
2755 .new_name = new_name,
2756 .bitmask = server->attr_bitmask,
2758 struct nfs_renameres res = {
2761 struct rpc_message msg = {
2762 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2766 int status = -ENOMEM;
2768 res.old_fattr = nfs_alloc_fattr();
2769 res.new_fattr = nfs_alloc_fattr();
2770 if (res.old_fattr == NULL || res.new_fattr == NULL)
2773 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2775 update_changeattr(old_dir, &res.old_cinfo);
2776 nfs_post_op_update_inode(old_dir, res.old_fattr);
2777 update_changeattr(new_dir, &res.new_cinfo);
2778 nfs_post_op_update_inode(new_dir, res.new_fattr);
2781 nfs_free_fattr(res.new_fattr);
2782 nfs_free_fattr(res.old_fattr);
2786 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2787 struct inode *new_dir, struct qstr *new_name)
2789 struct nfs4_exception exception = { };
2792 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2793 _nfs4_proc_rename(old_dir, old_name,
2796 } while (exception.retry);
2800 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2802 struct nfs_server *server = NFS_SERVER(inode);
2803 struct nfs4_link_arg arg = {
2804 .fh = NFS_FH(inode),
2805 .dir_fh = NFS_FH(dir),
2807 .bitmask = server->attr_bitmask,
2809 struct nfs4_link_res res = {
2812 struct rpc_message msg = {
2813 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2817 int status = -ENOMEM;
2819 res.fattr = nfs_alloc_fattr();
2820 res.dir_attr = nfs_alloc_fattr();
2821 if (res.fattr == NULL || res.dir_attr == NULL)
2824 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2826 update_changeattr(dir, &res.cinfo);
2827 nfs_post_op_update_inode(dir, res.dir_attr);
2828 nfs_post_op_update_inode(inode, res.fattr);
2831 nfs_free_fattr(res.dir_attr);
2832 nfs_free_fattr(res.fattr);
2836 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2838 struct nfs4_exception exception = { };
2841 err = nfs4_handle_exception(NFS_SERVER(inode),
2842 _nfs4_proc_link(inode, dir, name),
2844 } while (exception.retry);
2848 struct nfs4_createdata {
2849 struct rpc_message msg;
2850 struct nfs4_create_arg arg;
2851 struct nfs4_create_res res;
2853 struct nfs_fattr fattr;
2854 struct nfs_fattr dir_fattr;
2857 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2858 struct qstr *name, struct iattr *sattr, u32 ftype)
2860 struct nfs4_createdata *data;
2862 data = kzalloc(sizeof(*data), GFP_KERNEL);
2864 struct nfs_server *server = NFS_SERVER(dir);
2866 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2867 data->msg.rpc_argp = &data->arg;
2868 data->msg.rpc_resp = &data->res;
2869 data->arg.dir_fh = NFS_FH(dir);
2870 data->arg.server = server;
2871 data->arg.name = name;
2872 data->arg.attrs = sattr;
2873 data->arg.ftype = ftype;
2874 data->arg.bitmask = server->attr_bitmask;
2875 data->res.server = server;
2876 data->res.fh = &data->fh;
2877 data->res.fattr = &data->fattr;
2878 data->res.dir_fattr = &data->dir_fattr;
2879 nfs_fattr_init(data->res.fattr);
2880 nfs_fattr_init(data->res.dir_fattr);
2885 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2887 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
2888 &data->arg.seq_args, &data->res.seq_res, 1);
2890 update_changeattr(dir, &data->res.dir_cinfo);
2891 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2892 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2897 static void nfs4_free_createdata(struct nfs4_createdata *data)
2902 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2903 struct page *page, unsigned int len, struct iattr *sattr)
2905 struct nfs4_createdata *data;
2906 int status = -ENAMETOOLONG;
2908 if (len > NFS4_MAXPATHLEN)
2912 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2916 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2917 data->arg.u.symlink.pages = &page;
2918 data->arg.u.symlink.len = len;
2920 status = nfs4_do_create(dir, dentry, data);
2922 nfs4_free_createdata(data);
2927 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2928 struct page *page, unsigned int len, struct iattr *sattr)
2930 struct nfs4_exception exception = { };
2933 err = nfs4_handle_exception(NFS_SERVER(dir),
2934 _nfs4_proc_symlink(dir, dentry, page,
2937 } while (exception.retry);
2941 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2942 struct iattr *sattr)
2944 struct nfs4_createdata *data;
2945 int status = -ENOMEM;
2947 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2951 status = nfs4_do_create(dir, dentry, data);
2953 nfs4_free_createdata(data);
2958 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2959 struct iattr *sattr)
2961 struct nfs4_exception exception = { };
2964 sattr->ia_mode &= ~current_umask();
2966 err = nfs4_handle_exception(NFS_SERVER(dir),
2967 _nfs4_proc_mkdir(dir, dentry, sattr),
2969 } while (exception.retry);
2973 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2974 u64 cookie, struct page **pages, unsigned int count, int plus)
2976 struct inode *dir = dentry->d_inode;
2977 struct nfs4_readdir_arg args = {
2982 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2985 struct nfs4_readdir_res res;
2986 struct rpc_message msg = {
2987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2994 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2995 dentry->d_parent->d_name.name,
2996 dentry->d_name.name,
2997 (unsigned long long)cookie);
2998 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2999 res.pgbase = args.pgbase;
3000 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3002 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
3003 status += args.pgbase;
3006 nfs_invalidate_atime(dir);
3008 dprintk("%s: returns %d\n", __func__, status);
3012 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3013 u64 cookie, struct page **pages, unsigned int count, int plus)
3015 struct nfs4_exception exception = { };
3018 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3019 _nfs4_proc_readdir(dentry, cred, cookie,
3020 pages, count, plus),
3022 } while (exception.retry);
3026 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3027 struct iattr *sattr, dev_t rdev)
3029 struct nfs4_createdata *data;
3030 int mode = sattr->ia_mode;
3031 int status = -ENOMEM;
3033 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3034 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3036 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3041 data->arg.ftype = NF4FIFO;
3042 else if (S_ISBLK(mode)) {
3043 data->arg.ftype = NF4BLK;
3044 data->arg.u.device.specdata1 = MAJOR(rdev);
3045 data->arg.u.device.specdata2 = MINOR(rdev);
3047 else if (S_ISCHR(mode)) {
3048 data->arg.ftype = NF4CHR;
3049 data->arg.u.device.specdata1 = MAJOR(rdev);
3050 data->arg.u.device.specdata2 = MINOR(rdev);
3053 status = nfs4_do_create(dir, dentry, data);
3055 nfs4_free_createdata(data);
3060 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3061 struct iattr *sattr, dev_t rdev)
3063 struct nfs4_exception exception = { };
3066 sattr->ia_mode &= ~current_umask();
3068 err = nfs4_handle_exception(NFS_SERVER(dir),
3069 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3071 } while (exception.retry);
3075 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3076 struct nfs_fsstat *fsstat)
3078 struct nfs4_statfs_arg args = {
3080 .bitmask = server->attr_bitmask,
3082 struct nfs4_statfs_res res = {
3085 struct rpc_message msg = {
3086 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3091 nfs_fattr_init(fsstat->fattr);
3092 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3095 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3097 struct nfs4_exception exception = { };
3100 err = nfs4_handle_exception(server,
3101 _nfs4_proc_statfs(server, fhandle, fsstat),
3103 } while (exception.retry);
3107 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3108 struct nfs_fsinfo *fsinfo)
3110 struct nfs4_fsinfo_arg args = {
3112 .bitmask = server->attr_bitmask,
3114 struct nfs4_fsinfo_res res = {
3117 struct rpc_message msg = {
3118 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3123 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3126 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3128 struct nfs4_exception exception = { };
3132 err = nfs4_handle_exception(server,
3133 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3135 } while (exception.retry);
3139 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3141 nfs_fattr_init(fsinfo->fattr);
3142 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3145 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3146 struct nfs_pathconf *pathconf)
3148 struct nfs4_pathconf_arg args = {
3150 .bitmask = server->attr_bitmask,
3152 struct nfs4_pathconf_res res = {
3153 .pathconf = pathconf,
3155 struct rpc_message msg = {
3156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3161 /* None of the pathconf attributes are mandatory to implement */
3162 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3163 memset(pathconf, 0, sizeof(*pathconf));
3167 nfs_fattr_init(pathconf->fattr);
3168 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3171 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3172 struct nfs_pathconf *pathconf)
3174 struct nfs4_exception exception = { };
3178 err = nfs4_handle_exception(server,
3179 _nfs4_proc_pathconf(server, fhandle, pathconf),
3181 } while (exception.retry);
3185 void __nfs4_read_done_cb(struct nfs_read_data *data)
3187 nfs_invalidate_atime(data->inode);
3190 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3192 struct nfs_server *server = NFS_SERVER(data->inode);
3194 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3195 nfs_restart_rpc(task, server->nfs_client);
3199 __nfs4_read_done_cb(data);
3200 if (task->tk_status > 0)
3201 renew_lease(server, data->timestamp);
3205 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3208 dprintk("--> %s\n", __func__);
3210 if (!nfs4_sequence_done(task, &data->res.seq_res))
3213 return data->read_done_cb ? data->read_done_cb(task, data) :
3214 nfs4_read_done_cb(task, data);
3217 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3219 data->timestamp = jiffies;
3220 data->read_done_cb = nfs4_read_done_cb;
3221 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3224 /* Reset the the nfs_read_data to send the read to the MDS. */
3225 void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3227 dprintk("%s Reset task for i/o through\n", __func__);
3228 put_lseg(data->lseg);
3230 /* offsets will differ in the dense stripe case */
3231 data->args.offset = data->mds_offset;
3232 data->ds_clp = NULL;
3233 data->args.fh = NFS_FH(data->inode);
3234 data->read_done_cb = nfs4_read_done_cb;
3235 task->tk_ops = data->mds_ops;
3236 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3238 EXPORT_SYMBOL_GPL(nfs4_reset_read);
3240 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3242 struct inode *inode = data->inode;
3244 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3245 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3248 if (task->tk_status >= 0) {
3249 renew_lease(NFS_SERVER(inode), data->timestamp);
3250 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3255 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3257 if (!nfs4_sequence_done(task, &data->res.seq_res))
3259 return data->write_done_cb ? data->write_done_cb(task, data) :
3260 nfs4_write_done_cb(task, data);
3263 /* Reset the the nfs_write_data to send the write to the MDS. */
3264 void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3266 dprintk("%s Reset task for i/o through\n", __func__);
3267 put_lseg(data->lseg);
3269 data->ds_clp = NULL;
3270 data->write_done_cb = nfs4_write_done_cb;
3271 data->args.fh = NFS_FH(data->inode);
3272 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3273 data->args.offset = data->mds_offset;
3274 data->res.fattr = &data->fattr;
3275 task->tk_ops = data->mds_ops;
3276 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3278 EXPORT_SYMBOL_GPL(nfs4_reset_write);
3280 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3282 struct nfs_server *server = NFS_SERVER(data->inode);
3285 data->args.bitmask = NULL;
3286 data->res.fattr = NULL;
3288 data->args.bitmask = server->cache_consistency_bitmask;
3289 if (!data->write_done_cb)
3290 data->write_done_cb = nfs4_write_done_cb;
3291 data->res.server = server;
3292 data->timestamp = jiffies;
3294 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3297 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3299 struct inode *inode = data->inode;
3301 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3302 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3305 nfs_refresh_inode(inode, data->res.fattr);
3309 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3311 if (!nfs4_sequence_done(task, &data->res.seq_res))
3313 return data->write_done_cb(task, data);
3316 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3318 struct nfs_server *server = NFS_SERVER(data->inode);
3321 data->args.bitmask = NULL;
3322 data->res.fattr = NULL;
3324 data->args.bitmask = server->cache_consistency_bitmask;
3325 if (!data->write_done_cb)
3326 data->write_done_cb = nfs4_commit_done_cb;
3327 data->res.server = server;
3328 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3331 struct nfs4_renewdata {
3332 struct nfs_client *client;
3333 unsigned long timestamp;
3337 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3338 * standalone procedure for queueing an asynchronous RENEW.
3340 static void nfs4_renew_release(void *calldata)
3342 struct nfs4_renewdata *data = calldata;
3343 struct nfs_client *clp = data->client;
3345 if (atomic_read(&clp->cl_count) > 1)
3346 nfs4_schedule_state_renewal(clp);
3347 nfs_put_client(clp);
3351 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3353 struct nfs4_renewdata *data = calldata;
3354 struct nfs_client *clp = data->client;
3355 unsigned long timestamp = data->timestamp;
3357 if (task->tk_status < 0) {
3358 /* Unless we're shutting down, schedule state recovery! */
3359 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3360 nfs4_schedule_lease_recovery(clp);
3363 do_renew_lease(clp, timestamp);
3366 static const struct rpc_call_ops nfs4_renew_ops = {
3367 .rpc_call_done = nfs4_renew_done,
3368 .rpc_release = nfs4_renew_release,
3371 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3373 struct rpc_message msg = {
3374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3378 struct nfs4_renewdata *data;
3380 if (!atomic_inc_not_zero(&clp->cl_count))
3382 data = kmalloc(sizeof(*data), GFP_KERNEL);
3386 data->timestamp = jiffies;
3387 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3388 &nfs4_renew_ops, data);
3391 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3393 struct rpc_message msg = {
3394 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3398 unsigned long now = jiffies;
3401 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3404 do_renew_lease(clp, now);
3408 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3410 return (server->caps & NFS_CAP_ACLS)
3411 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3412 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3415 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3416 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3419 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3421 static void buf_to_pages(const void *buf, size_t buflen,
3422 struct page **pages, unsigned int *pgbase)
3424 const void *p = buf;
3426 *pgbase = offset_in_page(buf);
3428 while (p < buf + buflen) {
3429 *(pages++) = virt_to_page(p);
3430 p += PAGE_CACHE_SIZE;
3434 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3435 struct page **pages, unsigned int *pgbase)
3437 struct page *newpage, **spages;
3443 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3444 newpage = alloc_page(GFP_KERNEL);
3446 if (newpage == NULL)
3448 memcpy(page_address(newpage), buf, len);
3453 } while (buflen != 0);
3459 __free_page(spages[rc-1]);
3463 struct nfs4_cached_acl {
3469 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3471 struct nfs_inode *nfsi = NFS_I(inode);
3473 spin_lock(&inode->i_lock);
3474 kfree(nfsi->nfs4_acl);
3475 nfsi->nfs4_acl = acl;
3476 spin_unlock(&inode->i_lock);
3479 static void nfs4_zap_acl_attr(struct inode *inode)
3481 nfs4_set_cached_acl(inode, NULL);
3484 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3486 struct nfs_inode *nfsi = NFS_I(inode);
3487 struct nfs4_cached_acl *acl;
3490 spin_lock(&inode->i_lock);
3491 acl = nfsi->nfs4_acl;
3494 if (buf == NULL) /* user is just asking for length */
3496 if (acl->cached == 0)
3498 ret = -ERANGE; /* see getxattr(2) man page */
3499 if (acl->len > buflen)
3501 memcpy(buf, acl->data, acl->len);
3505 spin_unlock(&inode->i_lock);
3509 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3511 struct nfs4_cached_acl *acl;
3513 if (buf && acl_len <= PAGE_SIZE) {
3514 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3518 memcpy(acl->data, buf, acl_len);
3520 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3527 nfs4_set_cached_acl(inode, acl);
3530 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3532 struct page *pages[NFS4ACL_MAXPAGES];
3533 struct nfs_getaclargs args = {
3534 .fh = NFS_FH(inode),
3538 struct nfs_getaclres res = {
3542 struct rpc_message msg = {
3543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3547 struct page *localpage = NULL;
3550 if (buflen < PAGE_SIZE) {
3551 /* As long as we're doing a round trip to the server anyway,
3552 * let's be prepared for a page of acl data. */
3553 localpage = alloc_page(GFP_KERNEL);
3554 resp_buf = page_address(localpage);
3555 if (localpage == NULL)
3557 args.acl_pages[0] = localpage;
3558 args.acl_pgbase = 0;
3559 args.acl_len = PAGE_SIZE;
3562 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3564 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3567 if (res.acl_len > args.acl_len)
3568 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3570 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3573 if (res.acl_len > buflen)
3576 memcpy(buf, resp_buf, res.acl_len);
3581 __free_page(localpage);
3585 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3587 struct nfs4_exception exception = { };
3590 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3593 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3594 } while (exception.retry);
3598 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3600 struct nfs_server *server = NFS_SERVER(inode);
3603 if (!nfs4_server_supports_acls(server))
3605 ret = nfs_revalidate_inode(server, inode);
3608 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3609 nfs_zap_acl_cache(inode);
3610 ret = nfs4_read_cached_acl(inode, buf, buflen);
3613 return nfs4_get_acl_uncached(inode, buf, buflen);
3616 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3618 struct nfs_server *server = NFS_SERVER(inode);
3619 struct page *pages[NFS4ACL_MAXPAGES];
3620 struct nfs_setaclargs arg = {
3621 .fh = NFS_FH(inode),
3625 struct nfs_setaclres res;
3626 struct rpc_message msg = {
3627 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3633 if (!nfs4_server_supports_acls(server))
3635 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3638 nfs_inode_return_delegation(inode);
3639 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3642 * Free each page after tx, so the only ref left is
3643 * held by the network stack
3646 put_page(pages[i-1]);
3649 * Acl update can result in inode attribute update.
3650 * so mark the attribute cache invalid.
3652 spin_lock(&inode->i_lock);
3653 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3654 spin_unlock(&inode->i_lock);
3655 nfs_access_zap_cache(inode);
3656 nfs_zap_acl_cache(inode);
3660 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3662 struct nfs4_exception exception = { };
3665 err = nfs4_handle_exception(NFS_SERVER(inode),
3666 __nfs4_proc_set_acl(inode, buf, buflen),
3668 } while (exception.retry);
3673 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3675 struct nfs_client *clp = server->nfs_client;
3677 if (task->tk_status >= 0)
3679 switch(task->tk_status) {
3680 case -NFS4ERR_ADMIN_REVOKED:
3681 case -NFS4ERR_BAD_STATEID:
3682 case -NFS4ERR_OPENMODE:
3685 nfs4_schedule_stateid_recovery(server, state);
3686 goto wait_on_recovery;
3687 case -NFS4ERR_EXPIRED:
3689 nfs4_schedule_stateid_recovery(server, state);
3690 case -NFS4ERR_STALE_STATEID:
3691 case -NFS4ERR_STALE_CLIENTID:
3692 nfs4_schedule_lease_recovery(clp);
3693 goto wait_on_recovery;
3694 #if defined(CONFIG_NFS_V4_1)
3695 case -NFS4ERR_BADSESSION:
3696 case -NFS4ERR_BADSLOT:
3697 case -NFS4ERR_BAD_HIGH_SLOT:
3698 case -NFS4ERR_DEADSESSION:
3699 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3700 case -NFS4ERR_SEQ_FALSE_RETRY:
3701 case -NFS4ERR_SEQ_MISORDERED:
3702 dprintk("%s ERROR %d, Reset session\n", __func__,
3704 nfs4_schedule_session_recovery(clp->cl_session);
3705 task->tk_status = 0;
3707 #endif /* CONFIG_NFS_V4_1 */
3708 case -NFS4ERR_DELAY:
3709 nfs_inc_server_stats(server, NFSIOS_DELAY);
3710 case -NFS4ERR_GRACE:
3712 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3713 task->tk_status = 0;
3715 case -NFS4ERR_RETRY_UNCACHED_REP:
3716 case -NFS4ERR_OLD_STATEID:
3717 task->tk_status = 0;
3720 task->tk_status = nfs4_map_errors(task->tk_status);
3723 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3724 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3725 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3726 task->tk_status = 0;
3730 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3731 unsigned short port, struct rpc_cred *cred,
3732 struct nfs4_setclientid_res *res)
3734 nfs4_verifier sc_verifier;
3735 struct nfs4_setclientid setclientid = {
3736 .sc_verifier = &sc_verifier,
3738 .sc_cb_ident = clp->cl_cb_ident,
3740 struct rpc_message msg = {
3741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3742 .rpc_argp = &setclientid,
3750 p = (__be32*)sc_verifier.data;
3751 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3752 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3755 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3756 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3758 rpc_peeraddr2str(clp->cl_rpcclient,
3760 rpc_peeraddr2str(clp->cl_rpcclient,
3762 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3763 clp->cl_id_uniquifier);
3764 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3765 sizeof(setclientid.sc_netid),
3766 rpc_peeraddr2str(clp->cl_rpcclient,
3767 RPC_DISPLAY_NETID));
3768 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3769 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3770 clp->cl_ipaddr, port >> 8, port & 255);
3772 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3773 if (status != -NFS4ERR_CLID_INUSE)
3776 ++clp->cl_id_uniquifier;
3780 ssleep(clp->cl_lease_time / HZ + 1);
3785 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3786 struct nfs4_setclientid_res *arg,
3787 struct rpc_cred *cred)
3789 struct nfs_fsinfo fsinfo;
3790 struct rpc_message msg = {
3791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3793 .rpc_resp = &fsinfo,
3800 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3802 spin_lock(&clp->cl_lock);
3803 clp->cl_lease_time = fsinfo.lease_time * HZ;
3804 clp->cl_last_renewal = now;
3805 spin_unlock(&clp->cl_lock);
3810 struct nfs4_delegreturndata {
3811 struct nfs4_delegreturnargs args;
3812 struct nfs4_delegreturnres res;
3814 nfs4_stateid stateid;
3815 unsigned long timestamp;
3816 struct nfs_fattr fattr;
3820 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3822 struct nfs4_delegreturndata *data = calldata;
3824 if (!nfs4_sequence_done(task, &data->res.seq_res))
3827 switch (task->tk_status) {
3828 case -NFS4ERR_STALE_STATEID:
3829 case -NFS4ERR_EXPIRED:
3831 renew_lease(data->res.server, data->timestamp);
3834 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3836 nfs_restart_rpc(task, data->res.server->nfs_client);
3840 data->rpc_status = task->tk_status;
3843 static void nfs4_delegreturn_release(void *calldata)
3848 #if defined(CONFIG_NFS_V4_1)
3849 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3851 struct nfs4_delegreturndata *d_data;
3853 d_data = (struct nfs4_delegreturndata *)data;
3855 if (nfs4_setup_sequence(d_data->res.server,
3856 &d_data->args.seq_args,
3857 &d_data->res.seq_res, 1, task))
3859 rpc_call_start(task);
3861 #endif /* CONFIG_NFS_V4_1 */
3863 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3864 #if defined(CONFIG_NFS_V4_1)
3865 .rpc_call_prepare = nfs4_delegreturn_prepare,
3866 #endif /* CONFIG_NFS_V4_1 */
3867 .rpc_call_done = nfs4_delegreturn_done,
3868 .rpc_release = nfs4_delegreturn_release,
3871 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3873 struct nfs4_delegreturndata *data;
3874 struct nfs_server *server = NFS_SERVER(inode);
3875 struct rpc_task *task;
3876 struct rpc_message msg = {
3877 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3880 struct rpc_task_setup task_setup_data = {
3881 .rpc_client = server->client,
3882 .rpc_message = &msg,
3883 .callback_ops = &nfs4_delegreturn_ops,
3884 .flags = RPC_TASK_ASYNC,
3888 data = kzalloc(sizeof(*data), GFP_NOFS);
3891 data->args.fhandle = &data->fh;
3892 data->args.stateid = &data->stateid;
3893 data->args.bitmask = server->attr_bitmask;
3894 nfs_copy_fh(&data->fh, NFS_FH(inode));
3895 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3896 data->res.fattr = &data->fattr;
3897 data->res.server = server;
3898 nfs_fattr_init(data->res.fattr);
3899 data->timestamp = jiffies;
3900 data->rpc_status = 0;
3902 task_setup_data.callback_data = data;
3903 msg.rpc_argp = &data->args;
3904 msg.rpc_resp = &data->res;
3905 task = rpc_run_task(&task_setup_data);
3907 return PTR_ERR(task);
3910 status = nfs4_wait_for_completion_rpc_task(task);
3913 status = data->rpc_status;
3916 nfs_refresh_inode(inode, &data->fattr);
3922 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3924 struct nfs_server *server = NFS_SERVER(inode);
3925 struct nfs4_exception exception = { };
3928 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3930 case -NFS4ERR_STALE_STATEID:
3931 case -NFS4ERR_EXPIRED:
3935 err = nfs4_handle_exception(server, err, &exception);
3936 } while (exception.retry);
3940 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3941 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3944 * sleep, with exponential backoff, and retry the LOCK operation.
3946 static unsigned long
3947 nfs4_set_lock_task_retry(unsigned long timeout)
3949 schedule_timeout_killable(timeout);
3951 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3952 return NFS4_LOCK_MAXTIMEOUT;
3956 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3958 struct inode *inode = state->inode;
3959 struct nfs_server *server = NFS_SERVER(inode);
3960 struct nfs_client *clp = server->nfs_client;
3961 struct nfs_lockt_args arg = {
3962 .fh = NFS_FH(inode),
3965 struct nfs_lockt_res res = {
3968 struct rpc_message msg = {
3969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3972 .rpc_cred = state->owner->so_cred,
3974 struct nfs4_lock_state *lsp;
3977 arg.lock_owner.clientid = clp->cl_clientid;
3978 status = nfs4_set_lock_state(state, request);
3981 lsp = request->fl_u.nfs4_fl.owner;
3982 arg.lock_owner.id = lsp->ls_id.id;
3983 arg.lock_owner.s_dev = server->s_dev;
3984 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3987 request->fl_type = F_UNLCK;
3989 case -NFS4ERR_DENIED:
3992 request->fl_ops->fl_release_private(request);
3997 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3999 struct nfs4_exception exception = { };
4003 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4004 _nfs4_proc_getlk(state, cmd, request),
4006 } while (exception.retry);
4010 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4013 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4015 res = posix_lock_file_wait(file, fl);
4018 res = flock_lock_file_wait(file, fl);
4026 struct nfs4_unlockdata {
4027 struct nfs_locku_args arg;
4028 struct nfs_locku_res res;
4029 struct nfs4_lock_state *lsp;
4030 struct nfs_open_context *ctx;
4031 struct file_lock fl;
4032 const struct nfs_server *server;
4033 unsigned long timestamp;
4036 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4037 struct nfs_open_context *ctx,
4038 struct nfs4_lock_state *lsp,
4039 struct nfs_seqid *seqid)
4041 struct nfs4_unlockdata *p;
4042 struct inode *inode = lsp->ls_state->inode;
4044 p = kzalloc(sizeof(*p), GFP_NOFS);
4047 p->arg.fh = NFS_FH(inode);
4049 p->arg.seqid = seqid;
4050 p->res.seqid = seqid;
4051 p->arg.stateid = &lsp->ls_stateid;
4053 atomic_inc(&lsp->ls_count);
4054 /* Ensure we don't close file until we're done freeing locks! */
4055 p->ctx = get_nfs_open_context(ctx);
4056 memcpy(&p->fl, fl, sizeof(p->fl));
4057 p->server = NFS_SERVER(inode);
4061 static void nfs4_locku_release_calldata(void *data)
4063 struct nfs4_unlockdata *calldata = data;
4064 nfs_free_seqid(calldata->arg.seqid);
4065 nfs4_put_lock_state(calldata->lsp);
4066 put_nfs_open_context(calldata->ctx);
4070 static void nfs4_locku_done(struct rpc_task *task, void *data)
4072 struct nfs4_unlockdata *calldata = data;
4074 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4076 switch (task->tk_status) {
4078 memcpy(calldata->lsp->ls_stateid.data,
4079 calldata->res.stateid.data,
4080 sizeof(calldata->lsp->ls_stateid.data));
4081 renew_lease(calldata->server, calldata->timestamp);
4083 case -NFS4ERR_BAD_STATEID:
4084 case -NFS4ERR_OLD_STATEID:
4085 case -NFS4ERR_STALE_STATEID:
4086 case -NFS4ERR_EXPIRED:
4089 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4090 nfs_restart_rpc(task,
4091 calldata->server->nfs_client);
4095 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4097 struct nfs4_unlockdata *calldata = data;
4099 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4101 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
4102 /* Note: exit _without_ running nfs4_locku_done */
4103 task->tk_action = NULL;
4106 calldata->timestamp = jiffies;
4107 if (nfs4_setup_sequence(calldata->server,
4108 &calldata->arg.seq_args,
4109 &calldata->res.seq_res, 1, task))
4111 rpc_call_start(task);
4114 static const struct rpc_call_ops nfs4_locku_ops = {
4115 .rpc_call_prepare = nfs4_locku_prepare,
4116 .rpc_call_done = nfs4_locku_done,
4117 .rpc_release = nfs4_locku_release_calldata,
4120 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4121 struct nfs_open_context *ctx,
4122 struct nfs4_lock_state *lsp,
4123 struct nfs_seqid *seqid)
4125 struct nfs4_unlockdata *data;
4126 struct rpc_message msg = {
4127 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4128 .rpc_cred = ctx->cred,
4130 struct rpc_task_setup task_setup_data = {
4131 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4132 .rpc_message = &msg,
4133 .callback_ops = &nfs4_locku_ops,
4134 .workqueue = nfsiod_workqueue,
4135 .flags = RPC_TASK_ASYNC,
4138 /* Ensure this is an unlock - when canceling a lock, the
4139 * canceled lock is passed in, and it won't be an unlock.
4141 fl->fl_type = F_UNLCK;
4143 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4145 nfs_free_seqid(seqid);
4146 return ERR_PTR(-ENOMEM);
4149 msg.rpc_argp = &data->arg;
4150 msg.rpc_resp = &data->res;
4151 task_setup_data.callback_data = data;
4152 return rpc_run_task(&task_setup_data);
4155 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4157 struct nfs_inode *nfsi = NFS_I(state->inode);
4158 struct nfs_seqid *seqid;
4159 struct nfs4_lock_state *lsp;
4160 struct rpc_task *task;
4162 unsigned char fl_flags = request->fl_flags;
4164 status = nfs4_set_lock_state(state, request);
4165 /* Unlock _before_ we do the RPC call */
4166 request->fl_flags |= FL_EXISTS;
4167 down_read(&nfsi->rwsem);
4168 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4169 up_read(&nfsi->rwsem);
4172 up_read(&nfsi->rwsem);
4175 /* Is this a delegated lock? */
4176 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4178 lsp = request->fl_u.nfs4_fl.owner;
4179 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4183 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4184 status = PTR_ERR(task);
4187 status = nfs4_wait_for_completion_rpc_task(task);
4190 request->fl_flags = fl_flags;
4194 struct nfs4_lockdata {
4195 struct nfs_lock_args arg;
4196 struct nfs_lock_res res;
4197 struct nfs4_lock_state *lsp;
4198 struct nfs_open_context *ctx;
4199 struct file_lock fl;
4200 unsigned long timestamp;
4203 struct nfs_server *server;
4206 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4207 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4210 struct nfs4_lockdata *p;
4211 struct inode *inode = lsp->ls_state->inode;
4212 struct nfs_server *server = NFS_SERVER(inode);
4214 p = kzalloc(sizeof(*p), gfp_mask);
4218 p->arg.fh = NFS_FH(inode);
4220 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4221 if (p->arg.open_seqid == NULL)
4223 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4224 if (p->arg.lock_seqid == NULL)
4225 goto out_free_seqid;
4226 p->arg.lock_stateid = &lsp->ls_stateid;
4227 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4228 p->arg.lock_owner.id = lsp->ls_id.id;
4229 p->arg.lock_owner.s_dev = server->s_dev;
4230 p->res.lock_seqid = p->arg.lock_seqid;
4233 atomic_inc(&lsp->ls_count);
4234 p->ctx = get_nfs_open_context(ctx);
4235 memcpy(&p->fl, fl, sizeof(p->fl));
4238 nfs_free_seqid(p->arg.open_seqid);
4244 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4246 struct nfs4_lockdata *data = calldata;
4247 struct nfs4_state *state = data->lsp->ls_state;
4249 dprintk("%s: begin!\n", __func__);
4250 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4252 /* Do we need to do an open_to_lock_owner? */
4253 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4254 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4256 data->arg.open_stateid = &state->stateid;
4257 data->arg.new_lock_owner = 1;
4258 data->res.open_seqid = data->arg.open_seqid;
4260 data->arg.new_lock_owner = 0;
4261 data->timestamp = jiffies;
4262 if (nfs4_setup_sequence(data->server,
4263 &data->arg.seq_args,
4264 &data->res.seq_res, 1, task))
4266 rpc_call_start(task);
4267 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4270 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4272 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4273 nfs4_lock_prepare(task, calldata);
4276 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4278 struct nfs4_lockdata *data = calldata;
4280 dprintk("%s: begin!\n", __func__);
4282 if (!nfs4_sequence_done(task, &data->res.seq_res))
4285 data->rpc_status = task->tk_status;
4286 if (data->arg.new_lock_owner != 0) {
4287 if (data->rpc_status == 0)
4288 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4292 if (data->rpc_status == 0) {
4293 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4294 sizeof(data->lsp->ls_stateid.data));
4295 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4296 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
4299 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4302 static void nfs4_lock_release(void *calldata)
4304 struct nfs4_lockdata *data = calldata;
4306 dprintk("%s: begin!\n", __func__);
4307 nfs_free_seqid(data->arg.open_seqid);
4308 if (data->cancelled != 0) {
4309 struct rpc_task *task;
4310 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4311 data->arg.lock_seqid);
4313 rpc_put_task_async(task);
4314 dprintk("%s: cancelling lock!\n", __func__);
4316 nfs_free_seqid(data->arg.lock_seqid);
4317 nfs4_put_lock_state(data->lsp);
4318 put_nfs_open_context(data->ctx);
4320 dprintk("%s: done!\n", __func__);
4323 static const struct rpc_call_ops nfs4_lock_ops = {
4324 .rpc_call_prepare = nfs4_lock_prepare,
4325 .rpc_call_done = nfs4_lock_done,
4326 .rpc_release = nfs4_lock_release,
4329 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4330 .rpc_call_prepare = nfs4_recover_lock_prepare,
4331 .rpc_call_done = nfs4_lock_done,
4332 .rpc_release = nfs4_lock_release,
4335 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4338 case -NFS4ERR_ADMIN_REVOKED:
4339 case -NFS4ERR_BAD_STATEID:
4340 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4341 if (new_lock_owner != 0 ||
4342 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4343 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4345 case -NFS4ERR_STALE_STATEID:
4346 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4347 case -NFS4ERR_EXPIRED:
4348 nfs4_schedule_lease_recovery(server->nfs_client);
4352 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4354 struct nfs4_lockdata *data;
4355 struct rpc_task *task;
4356 struct rpc_message msg = {
4357 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4358 .rpc_cred = state->owner->so_cred,
4360 struct rpc_task_setup task_setup_data = {
4361 .rpc_client = NFS_CLIENT(state->inode),
4362 .rpc_message = &msg,
4363 .callback_ops = &nfs4_lock_ops,
4364 .workqueue = nfsiod_workqueue,
4365 .flags = RPC_TASK_ASYNC,
4369 dprintk("%s: begin!\n", __func__);
4370 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4371 fl->fl_u.nfs4_fl.owner,
4372 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4376 data->arg.block = 1;
4377 if (recovery_type > NFS_LOCK_NEW) {
4378 if (recovery_type == NFS_LOCK_RECLAIM)
4379 data->arg.reclaim = NFS_LOCK_RECLAIM;
4380 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4382 msg.rpc_argp = &data->arg;
4383 msg.rpc_resp = &data->res;
4384 task_setup_data.callback_data = data;
4385 task = rpc_run_task(&task_setup_data);
4387 return PTR_ERR(task);
4388 ret = nfs4_wait_for_completion_rpc_task(task);
4390 ret = data->rpc_status;
4392 nfs4_handle_setlk_error(data->server, data->lsp,
4393 data->arg.new_lock_owner, ret);
4395 data->cancelled = 1;
4397 dprintk("%s: done, ret = %d!\n", __func__, ret);
4401 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4403 struct nfs_server *server = NFS_SERVER(state->inode);
4404 struct nfs4_exception exception = { };
4408 /* Cache the lock if possible... */
4409 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4411 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4412 if (err != -NFS4ERR_DELAY)
4414 nfs4_handle_exception(server, err, &exception);
4415 } while (exception.retry);
4419 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4421 struct nfs_server *server = NFS_SERVER(state->inode);
4422 struct nfs4_exception exception = { };
4425 err = nfs4_set_lock_state(state, request);
4429 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4431 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4435 case -NFS4ERR_GRACE:
4436 case -NFS4ERR_DELAY:
4437 nfs4_handle_exception(server, err, &exception);
4440 } while (exception.retry);
4445 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4447 struct nfs_inode *nfsi = NFS_I(state->inode);
4448 unsigned char fl_flags = request->fl_flags;
4449 int status = -ENOLCK;
4451 if ((fl_flags & FL_POSIX) &&
4452 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4454 /* Is this a delegated open? */
4455 status = nfs4_set_lock_state(state, request);
4458 request->fl_flags |= FL_ACCESS;
4459 status = do_vfs_lock(request->fl_file, request);
4462 down_read(&nfsi->rwsem);
4463 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4464 /* Yes: cache locks! */
4465 /* ...but avoid races with delegation recall... */
4466 request->fl_flags = fl_flags & ~FL_SLEEP;
4467 status = do_vfs_lock(request->fl_file, request);
4470 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4473 /* Note: we always want to sleep here! */
4474 request->fl_flags = fl_flags | FL_SLEEP;
4475 if (do_vfs_lock(request->fl_file, request) < 0)
4476 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4478 up_read(&nfsi->rwsem);
4480 request->fl_flags = fl_flags;
4484 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4486 struct nfs4_exception exception = { };
4490 err = _nfs4_proc_setlk(state, cmd, request);
4491 if (err == -NFS4ERR_DENIED)
4493 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4495 } while (exception.retry);
4500 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4502 struct nfs_open_context *ctx;
4503 struct nfs4_state *state;
4504 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4507 /* verify open state */
4508 ctx = nfs_file_open_context(filp);
4511 if (request->fl_start < 0 || request->fl_end < 0)
4514 if (IS_GETLK(cmd)) {
4516 return nfs4_proc_getlk(state, F_GETLK, request);
4520 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4523 if (request->fl_type == F_UNLCK) {
4525 return nfs4_proc_unlck(state, cmd, request);
4532 status = nfs4_proc_setlk(state, cmd, request);
4533 if ((status != -EAGAIN) || IS_SETLK(cmd))
4535 timeout = nfs4_set_lock_task_retry(timeout);
4536 status = -ERESTARTSYS;
4539 } while(status < 0);
4543 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4545 struct nfs_server *server = NFS_SERVER(state->inode);
4546 struct nfs4_exception exception = { };
4549 err = nfs4_set_lock_state(state, fl);
4553 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4556 printk(KERN_ERR "%s: unhandled error %d.\n",
4561 case -NFS4ERR_EXPIRED:
4562 nfs4_schedule_stateid_recovery(server, state);
4563 case -NFS4ERR_STALE_CLIENTID:
4564 case -NFS4ERR_STALE_STATEID:
4565 nfs4_schedule_lease_recovery(server->nfs_client);
4567 case -NFS4ERR_BADSESSION:
4568 case -NFS4ERR_BADSLOT:
4569 case -NFS4ERR_BAD_HIGH_SLOT:
4570 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4571 case -NFS4ERR_DEADSESSION:
4572 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
4576 * The show must go on: exit, but mark the
4577 * stateid as needing recovery.
4579 case -NFS4ERR_ADMIN_REVOKED:
4580 case -NFS4ERR_BAD_STATEID:
4581 case -NFS4ERR_OPENMODE:
4582 nfs4_schedule_stateid_recovery(server, state);
4587 * User RPCSEC_GSS context has expired.
4588 * We cannot recover this stateid now, so
4589 * skip it and allow recovery thread to
4595 case -NFS4ERR_DENIED:
4596 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4599 case -NFS4ERR_DELAY:
4602 err = nfs4_handle_exception(server, err, &exception);
4603 } while (exception.retry);
4608 static void nfs4_release_lockowner_release(void *calldata)
4613 const struct rpc_call_ops nfs4_release_lockowner_ops = {
4614 .rpc_release = nfs4_release_lockowner_release,
4617 void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4619 struct nfs_server *server = lsp->ls_state->owner->so_server;
4620 struct nfs_release_lockowner_args *args;
4621 struct rpc_message msg = {
4622 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4625 if (server->nfs_client->cl_mvops->minor_version != 0)
4627 args = kmalloc(sizeof(*args), GFP_NOFS);
4630 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4631 args->lock_owner.id = lsp->ls_id.id;
4632 args->lock_owner.s_dev = server->s_dev;
4633 msg.rpc_argp = args;
4634 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4637 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4639 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4640 const void *buf, size_t buflen,
4641 int flags, int type)
4643 if (strcmp(key, "") != 0)
4646 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
4649 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4650 void *buf, size_t buflen, int type)
4652 if (strcmp(key, "") != 0)
4655 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
4658 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4659 size_t list_len, const char *name,
4660 size_t name_len, int type)
4662 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
4664 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4667 if (list && len <= list_len)
4668 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
4673 * nfs_fhget will use either the mounted_on_fileid or the fileid
4675 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4677 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4678 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4679 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4680 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4683 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4684 NFS_ATTR_FATTR_NLINK;
4685 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4689 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4690 struct nfs4_fs_locations *fs_locations, struct page *page)
4692 struct nfs_server *server = NFS_SERVER(dir);
4694 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4696 struct nfs4_fs_locations_arg args = {
4697 .dir_fh = NFS_FH(dir),
4702 struct nfs4_fs_locations_res res = {
4703 .fs_locations = fs_locations,
4705 struct rpc_message msg = {
4706 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4712 dprintk("%s: start\n", __func__);
4714 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4715 * is not supported */
4716 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4717 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4719 bitmask[0] |= FATTR4_WORD0_FILEID;
4721 nfs_fattr_init(&fs_locations->fattr);
4722 fs_locations->server = server;
4723 fs_locations->nlocations = 0;
4724 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4725 dprintk("%s: returned status = %d\n", __func__, status);
4729 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4732 struct nfs4_secinfo_arg args = {
4733 .dir_fh = NFS_FH(dir),
4736 struct nfs4_secinfo_res res = {
4739 struct rpc_message msg = {
4740 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4745 dprintk("NFS call secinfo %s\n", name->name);
4746 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4747 dprintk("NFS reply secinfo: %d\n", status);
4751 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4753 struct nfs4_exception exception = { };
4756 err = nfs4_handle_exception(NFS_SERVER(dir),
4757 _nfs4_proc_secinfo(dir, name, flavors),
4759 } while (exception.retry);
4763 #ifdef CONFIG_NFS_V4_1
4765 * Check the exchange flags returned by the server for invalid flags, having
4766 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4769 static int nfs4_check_cl_exchange_flags(u32 flags)
4771 if (flags & ~EXCHGID4_FLAG_MASK_R)
4773 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4774 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4776 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4780 return -NFS4ERR_INVAL;
4784 * nfs4_proc_exchange_id()
4786 * Since the clientid has expired, all compounds using sessions
4787 * associated with the stale clientid will be returning
4788 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4789 * be in some phase of session reset.
4791 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4793 nfs4_verifier verifier;
4794 struct nfs41_exchange_id_args args = {
4796 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
4798 struct nfs41_exchange_id_res res = {
4802 struct rpc_message msg = {
4803 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4810 dprintk("--> %s\n", __func__);
4811 BUG_ON(clp == NULL);
4813 p = (u32 *)verifier.data;
4814 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4815 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4816 args.verifier = &verifier;
4818 args.id_len = scnprintf(args.id, sizeof(args.id),
4821 init_utsname()->nodename,
4822 init_utsname()->domainname,
4823 clp->cl_rpcclient->cl_auth->au_flavor);
4825 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4827 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
4828 dprintk("<-- %s status= %d\n", __func__, status);
4832 struct nfs4_get_lease_time_data {
4833 struct nfs4_get_lease_time_args *args;
4834 struct nfs4_get_lease_time_res *res;
4835 struct nfs_client *clp;
4838 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4842 struct nfs4_get_lease_time_data *data =
4843 (struct nfs4_get_lease_time_data *)calldata;
4845 dprintk("--> %s\n", __func__);
4846 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4847 /* just setup sequence, do not trigger session recovery
4848 since we're invoked within one */
4849 ret = nfs41_setup_sequence(data->clp->cl_session,
4850 &data->args->la_seq_args,
4851 &data->res->lr_seq_res, 0, task);
4853 BUG_ON(ret == -EAGAIN);
4854 rpc_call_start(task);
4855 dprintk("<-- %s\n", __func__);
4859 * Called from nfs4_state_manager thread for session setup, so don't recover
4860 * from sequence operation or clientid errors.
4862 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4864 struct nfs4_get_lease_time_data *data =
4865 (struct nfs4_get_lease_time_data *)calldata;
4867 dprintk("--> %s\n", __func__);
4868 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4870 switch (task->tk_status) {
4871 case -NFS4ERR_DELAY:
4872 case -NFS4ERR_GRACE:
4873 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4874 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4875 task->tk_status = 0;
4877 case -NFS4ERR_RETRY_UNCACHED_REP:
4878 nfs_restart_rpc(task, data->clp);
4881 dprintk("<-- %s\n", __func__);
4884 struct rpc_call_ops nfs4_get_lease_time_ops = {
4885 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4886 .rpc_call_done = nfs4_get_lease_time_done,
4889 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4891 struct rpc_task *task;
4892 struct nfs4_get_lease_time_args args;
4893 struct nfs4_get_lease_time_res res = {
4894 .lr_fsinfo = fsinfo,
4896 struct nfs4_get_lease_time_data data = {
4901 struct rpc_message msg = {
4902 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4906 struct rpc_task_setup task_setup = {
4907 .rpc_client = clp->cl_rpcclient,
4908 .rpc_message = &msg,
4909 .callback_ops = &nfs4_get_lease_time_ops,
4910 .callback_data = &data,
4911 .flags = RPC_TASK_TIMEOUT,
4915 dprintk("--> %s\n", __func__);
4916 task = rpc_run_task(&task_setup);
4919 status = PTR_ERR(task);
4921 status = task->tk_status;
4924 dprintk("<-- %s return %d\n", __func__, status);
4930 * Reset a slot table
4932 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4935 struct nfs4_slot *new = NULL;
4939 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4940 max_reqs, tbl->max_slots);
4942 /* Does the newly negotiated max_reqs match the existing slot table? */
4943 if (max_reqs != tbl->max_slots) {
4945 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
4952 spin_lock(&tbl->slot_tbl_lock);
4955 tbl->max_slots = max_reqs;
4957 for (i = 0; i < tbl->max_slots; ++i)
4958 tbl->slots[i].seq_nr = ivalue;
4959 spin_unlock(&tbl->slot_tbl_lock);
4960 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4961 tbl, tbl->slots, tbl->max_slots);
4963 dprintk("<-- %s: return %d\n", __func__, ret);
4968 * Reset the forechannel and backchannel slot tables
4970 static int nfs4_reset_slot_tables(struct nfs4_session *session)
4974 status = nfs4_reset_slot_table(&session->fc_slot_table,
4975 session->fc_attrs.max_reqs, 1);
4979 status = nfs4_reset_slot_table(&session->bc_slot_table,
4980 session->bc_attrs.max_reqs, 0);
4984 /* Destroy the slot table */
4985 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4987 if (session->fc_slot_table.slots != NULL) {
4988 kfree(session->fc_slot_table.slots);
4989 session->fc_slot_table.slots = NULL;
4991 if (session->bc_slot_table.slots != NULL) {
4992 kfree(session->bc_slot_table.slots);
4993 session->bc_slot_table.slots = NULL;
4999 * Initialize slot table
5001 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5002 int max_slots, int ivalue)
5004 struct nfs4_slot *slot;
5007 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5009 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
5011 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
5016 spin_lock(&tbl->slot_tbl_lock);
5017 tbl->max_slots = max_slots;
5019 tbl->highest_used_slotid = -1; /* no slot is currently used */
5020 spin_unlock(&tbl->slot_tbl_lock);
5021 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5022 tbl, tbl->slots, tbl->max_slots);
5024 dprintk("<-- %s: return %d\n", __func__, ret);
5029 * Initialize the forechannel and backchannel tables
5031 static int nfs4_init_slot_tables(struct nfs4_session *session)
5033 struct nfs4_slot_table *tbl;
5036 tbl = &session->fc_slot_table;
5037 if (tbl->slots == NULL) {
5038 status = nfs4_init_slot_table(tbl,
5039 session->fc_attrs.max_reqs, 1);
5044 tbl = &session->bc_slot_table;
5045 if (tbl->slots == NULL) {
5046 status = nfs4_init_slot_table(tbl,
5047 session->bc_attrs.max_reqs, 0);
5049 nfs4_destroy_slot_tables(session);
5055 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5057 struct nfs4_session *session;
5058 struct nfs4_slot_table *tbl;
5060 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5064 tbl = &session->fc_slot_table;
5065 tbl->highest_used_slotid = -1;
5066 spin_lock_init(&tbl->slot_tbl_lock);
5067 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5068 init_completion(&tbl->complete);
5070 tbl = &session->bc_slot_table;
5071 tbl->highest_used_slotid = -1;
5072 spin_lock_init(&tbl->slot_tbl_lock);
5073 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5074 init_completion(&tbl->complete);
5076 session->session_state = 1<<NFS4_SESSION_INITING;
5082 void nfs4_destroy_session(struct nfs4_session *session)
5084 nfs4_proc_destroy_session(session);
5085 dprintk("%s Destroy backchannel for xprt %p\n",
5086 __func__, session->clp->cl_rpcclient->cl_xprt);
5087 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5088 NFS41_BC_MIN_CALLBACKS);
5089 nfs4_destroy_slot_tables(session);
5094 * Initialize the values to be used by the client in CREATE_SESSION
5095 * If nfs4_init_session set the fore channel request and response sizes,
5098 * Set the back channel max_resp_sz_cached to zero to force the client to
5099 * always set csa_cachethis to FALSE because the current implementation
5100 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5102 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5104 struct nfs4_session *session = args->client->cl_session;
5105 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5106 mxresp_sz = session->fc_attrs.max_resp_sz;
5109 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5111 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5112 /* Fore channel attributes */
5113 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5114 args->fc_attrs.max_resp_sz = mxresp_sz;
5115 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5116 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5118 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5119 "max_ops=%u max_reqs=%u\n",
5121 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5122 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5124 /* Back channel attributes */
5125 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5126 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5127 args->bc_attrs.max_resp_sz_cached = 0;
5128 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5129 args->bc_attrs.max_reqs = 1;
5131 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5132 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5134 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5135 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5136 args->bc_attrs.max_reqs);
5139 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5141 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5142 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5144 if (rcvd->max_resp_sz > sent->max_resp_sz)
5147 * Our requested max_ops is the minimum we need; we're not
5148 * prepared to break up compounds into smaller pieces than that.
5149 * So, no point even trying to continue if the server won't
5152 if (rcvd->max_ops < sent->max_ops)
5154 if (rcvd->max_reqs == 0)
5159 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5161 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5162 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5164 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5166 if (rcvd->max_resp_sz < sent->max_resp_sz)
5168 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5170 /* These would render the backchannel useless: */
5171 if (rcvd->max_ops == 0)
5173 if (rcvd->max_reqs == 0)
5178 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5179 struct nfs4_session *session)
5183 ret = nfs4_verify_fore_channel_attrs(args, session);
5186 return nfs4_verify_back_channel_attrs(args, session);
5189 static int _nfs4_proc_create_session(struct nfs_client *clp)
5191 struct nfs4_session *session = clp->cl_session;
5192 struct nfs41_create_session_args args = {
5194 .cb_program = NFS4_CALLBACK,
5196 struct nfs41_create_session_res res = {
5199 struct rpc_message msg = {
5200 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5206 nfs4_init_channel_attrs(&args);
5207 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5209 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5212 /* Verify the session's negotiated channel_attrs values */
5213 status = nfs4_verify_channel_attrs(&args, session);
5215 /* Increment the clientid slot sequence id */
5223 * Issues a CREATE_SESSION operation to the server.
5224 * It is the responsibility of the caller to verify the session is
5225 * expired before calling this routine.
5227 int nfs4_proc_create_session(struct nfs_client *clp)
5231 struct nfs4_session *session = clp->cl_session;
5233 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5235 status = _nfs4_proc_create_session(clp);
5239 /* Init and reset the fore channel */
5240 status = nfs4_init_slot_tables(session);
5241 dprintk("slot table initialization returned %d\n", status);
5244 status = nfs4_reset_slot_tables(session);
5245 dprintk("slot table reset returned %d\n", status);
5249 ptr = (unsigned *)&session->sess_id.data[0];
5250 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5251 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5253 dprintk("<-- %s\n", __func__);
5258 * Issue the over-the-wire RPC DESTROY_SESSION.
5259 * The caller must serialize access to this routine.
5261 int nfs4_proc_destroy_session(struct nfs4_session *session)
5264 struct rpc_message msg;
5266 dprintk("--> nfs4_proc_destroy_session\n");
5268 /* session is still being setup */
5269 if (session->clp->cl_cons_state != NFS_CS_READY)
5272 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5273 msg.rpc_argp = session;
5274 msg.rpc_resp = NULL;
5275 msg.rpc_cred = NULL;
5276 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5280 "Got error %d from the server on DESTROY_SESSION. "
5281 "Session has been destroyed regardless...\n", status);
5283 dprintk("<-- nfs4_proc_destroy_session\n");
5287 int nfs4_init_session(struct nfs_server *server)
5289 struct nfs_client *clp = server->nfs_client;
5290 struct nfs4_session *session;
5291 unsigned int rsize, wsize;
5294 if (!nfs4_has_session(clp))
5297 session = clp->cl_session;
5298 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5301 rsize = server->rsize;
5303 rsize = NFS_MAX_FILE_IO_SIZE;
5304 wsize = server->wsize;
5306 wsize = NFS_MAX_FILE_IO_SIZE;
5308 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5309 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5311 ret = nfs4_recover_expired_lease(server);
5313 ret = nfs4_check_client_ready(clp);
5317 int nfs4_init_ds_session(struct nfs_client *clp)
5319 struct nfs4_session *session = clp->cl_session;
5322 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5325 ret = nfs4_client_recover_expired_lease(clp);
5327 /* Test for the DS role */
5328 if (!is_ds_client(clp))
5331 ret = nfs4_check_client_ready(clp);
5335 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5339 * Renew the cl_session lease.
5341 struct nfs4_sequence_data {
5342 struct nfs_client *clp;
5343 struct nfs4_sequence_args args;
5344 struct nfs4_sequence_res res;
5347 static void nfs41_sequence_release(void *data)
5349 struct nfs4_sequence_data *calldata = data;
5350 struct nfs_client *clp = calldata->clp;
5352 if (atomic_read(&clp->cl_count) > 1)
5353 nfs4_schedule_state_renewal(clp);
5354 nfs_put_client(clp);
5358 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5360 switch(task->tk_status) {
5361 case -NFS4ERR_DELAY:
5362 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5365 nfs4_schedule_lease_recovery(clp);
5370 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5372 struct nfs4_sequence_data *calldata = data;
5373 struct nfs_client *clp = calldata->clp;
5375 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5378 if (task->tk_status < 0) {
5379 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5380 if (atomic_read(&clp->cl_count) == 1)
5383 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5384 rpc_restart_call_prepare(task);
5388 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5390 dprintk("<-- %s\n", __func__);
5393 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5395 struct nfs4_sequence_data *calldata = data;
5396 struct nfs_client *clp = calldata->clp;
5397 struct nfs4_sequence_args *args;
5398 struct nfs4_sequence_res *res;
5400 args = task->tk_msg.rpc_argp;
5401 res = task->tk_msg.rpc_resp;
5403 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
5405 rpc_call_start(task);
5408 static const struct rpc_call_ops nfs41_sequence_ops = {
5409 .rpc_call_done = nfs41_sequence_call_done,
5410 .rpc_call_prepare = nfs41_sequence_prepare,
5411 .rpc_release = nfs41_sequence_release,
5414 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5416 struct nfs4_sequence_data *calldata;
5417 struct rpc_message msg = {
5418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5421 struct rpc_task_setup task_setup_data = {
5422 .rpc_client = clp->cl_rpcclient,
5423 .rpc_message = &msg,
5424 .callback_ops = &nfs41_sequence_ops,
5425 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5428 if (!atomic_inc_not_zero(&clp->cl_count))
5429 return ERR_PTR(-EIO);
5430 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5431 if (calldata == NULL) {
5432 nfs_put_client(clp);
5433 return ERR_PTR(-ENOMEM);
5435 msg.rpc_argp = &calldata->args;
5436 msg.rpc_resp = &calldata->res;
5437 calldata->clp = clp;
5438 task_setup_data.callback_data = calldata;
5440 return rpc_run_task(&task_setup_data);
5443 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5445 struct rpc_task *task;
5448 task = _nfs41_proc_sequence(clp, cred);
5450 ret = PTR_ERR(task);
5452 rpc_put_task_async(task);
5453 dprintk("<-- %s status=%d\n", __func__, ret);
5457 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5459 struct rpc_task *task;
5462 task = _nfs41_proc_sequence(clp, cred);
5464 ret = PTR_ERR(task);
5467 ret = rpc_wait_for_completion_task(task);
5469 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5471 if (task->tk_status == 0)
5472 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5473 ret = task->tk_status;
5477 dprintk("<-- %s status=%d\n", __func__, ret);
5481 struct nfs4_reclaim_complete_data {
5482 struct nfs_client *clp;
5483 struct nfs41_reclaim_complete_args arg;
5484 struct nfs41_reclaim_complete_res res;
5487 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5489 struct nfs4_reclaim_complete_data *calldata = data;
5491 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5492 if (nfs41_setup_sequence(calldata->clp->cl_session,
5493 &calldata->arg.seq_args,
5494 &calldata->res.seq_res, 0, task))
5497 rpc_call_start(task);
5500 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5502 switch(task->tk_status) {
5504 case -NFS4ERR_COMPLETE_ALREADY:
5505 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5507 case -NFS4ERR_DELAY:
5508 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5510 case -NFS4ERR_RETRY_UNCACHED_REP:
5513 nfs4_schedule_lease_recovery(clp);
5518 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5520 struct nfs4_reclaim_complete_data *calldata = data;
5521 struct nfs_client *clp = calldata->clp;
5522 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5524 dprintk("--> %s\n", __func__);
5525 if (!nfs41_sequence_done(task, res))
5528 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5529 rpc_restart_call_prepare(task);
5532 dprintk("<-- %s\n", __func__);
5535 static void nfs4_free_reclaim_complete_data(void *data)
5537 struct nfs4_reclaim_complete_data *calldata = data;
5542 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5543 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5544 .rpc_call_done = nfs4_reclaim_complete_done,
5545 .rpc_release = nfs4_free_reclaim_complete_data,
5549 * Issue a global reclaim complete.
5551 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5553 struct nfs4_reclaim_complete_data *calldata;
5554 struct rpc_task *task;
5555 struct rpc_message msg = {
5556 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5558 struct rpc_task_setup task_setup_data = {
5559 .rpc_client = clp->cl_rpcclient,
5560 .rpc_message = &msg,
5561 .callback_ops = &nfs4_reclaim_complete_call_ops,
5562 .flags = RPC_TASK_ASYNC,
5564 int status = -ENOMEM;
5566 dprintk("--> %s\n", __func__);
5567 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5568 if (calldata == NULL)
5570 calldata->clp = clp;
5571 calldata->arg.one_fs = 0;
5573 msg.rpc_argp = &calldata->arg;
5574 msg.rpc_resp = &calldata->res;
5575 task_setup_data.callback_data = calldata;
5576 task = rpc_run_task(&task_setup_data);
5578 status = PTR_ERR(task);
5581 status = nfs4_wait_for_completion_rpc_task(task);
5583 status = task->tk_status;
5587 dprintk("<-- %s status=%d\n", __func__, status);
5592 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5594 struct nfs4_layoutget *lgp = calldata;
5595 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5597 dprintk("--> %s\n", __func__);
5598 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5599 * right now covering the LAYOUTGET we are about to send.
5600 * However, that is not so catastrophic, and there seems
5601 * to be no way to prevent it completely.
5603 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5604 &lgp->res.seq_res, 0, task))
5606 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5607 NFS_I(lgp->args.inode)->layout,
5608 lgp->args.ctx->state)) {
5609 rpc_exit(task, NFS4_OK);
5612 rpc_call_start(task);
5615 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5617 struct nfs4_layoutget *lgp = calldata;
5618 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5620 dprintk("--> %s\n", __func__);
5622 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5625 switch (task->tk_status) {
5628 case -NFS4ERR_LAYOUTTRYLATER:
5629 case -NFS4ERR_RECALLCONFLICT:
5630 task->tk_status = -NFS4ERR_DELAY;
5633 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5634 rpc_restart_call_prepare(task);
5638 dprintk("<-- %s\n", __func__);
5641 static void nfs4_layoutget_release(void *calldata)
5643 struct nfs4_layoutget *lgp = calldata;
5645 dprintk("--> %s\n", __func__);
5646 put_nfs_open_context(lgp->args.ctx);
5648 dprintk("<-- %s\n", __func__);
5651 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5652 .rpc_call_prepare = nfs4_layoutget_prepare,
5653 .rpc_call_done = nfs4_layoutget_done,
5654 .rpc_release = nfs4_layoutget_release,
5657 int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5659 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5660 struct rpc_task *task;
5661 struct rpc_message msg = {
5662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5663 .rpc_argp = &lgp->args,
5664 .rpc_resp = &lgp->res,
5666 struct rpc_task_setup task_setup_data = {
5667 .rpc_client = server->client,
5668 .rpc_message = &msg,
5669 .callback_ops = &nfs4_layoutget_call_ops,
5670 .callback_data = lgp,
5671 .flags = RPC_TASK_ASYNC,
5675 dprintk("--> %s\n", __func__);
5677 lgp->res.layoutp = &lgp->args.layout;
5678 lgp->res.seq_res.sr_slot = NULL;
5679 task = rpc_run_task(&task_setup_data);
5681 return PTR_ERR(task);
5682 status = nfs4_wait_for_completion_rpc_task(task);
5684 status = task->tk_status;
5686 status = pnfs_layout_process(lgp);
5688 dprintk("<-- %s status=%d\n", __func__, status);
5693 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5695 struct nfs4_layoutreturn *lrp = calldata;
5697 dprintk("--> %s\n", __func__);
5698 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5699 &lrp->res.seq_res, 0, task))
5701 rpc_call_start(task);
5704 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5706 struct nfs4_layoutreturn *lrp = calldata;
5707 struct nfs_server *server;
5708 struct pnfs_layout_hdr *lo = NFS_I(lrp->args.inode)->layout;
5710 dprintk("--> %s\n", __func__);
5712 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5715 server = NFS_SERVER(lrp->args.inode);
5716 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5717 nfs_restart_rpc(task, lrp->clp);
5720 spin_lock(&lo->plh_inode->i_lock);
5721 if (task->tk_status == 0) {
5722 if (lrp->res.lrs_present) {
5723 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
5725 BUG_ON(!list_empty(&lo->plh_segs));
5727 lo->plh_block_lgets--;
5728 spin_unlock(&lo->plh_inode->i_lock);
5729 dprintk("<-- %s\n", __func__);
5732 static void nfs4_layoutreturn_release(void *calldata)
5734 struct nfs4_layoutreturn *lrp = calldata;
5736 dprintk("--> %s\n", __func__);
5737 put_layout_hdr(NFS_I(lrp->args.inode)->layout);
5739 dprintk("<-- %s\n", __func__);
5742 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5743 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5744 .rpc_call_done = nfs4_layoutreturn_done,
5745 .rpc_release = nfs4_layoutreturn_release,
5748 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5750 struct rpc_task *task;
5751 struct rpc_message msg = {
5752 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5753 .rpc_argp = &lrp->args,
5754 .rpc_resp = &lrp->res,
5756 struct rpc_task_setup task_setup_data = {
5757 .rpc_client = lrp->clp->cl_rpcclient,
5758 .rpc_message = &msg,
5759 .callback_ops = &nfs4_layoutreturn_call_ops,
5760 .callback_data = lrp,
5764 dprintk("--> %s\n", __func__);
5765 task = rpc_run_task(&task_setup_data);
5767 return PTR_ERR(task);
5768 status = task->tk_status;
5769 dprintk("<-- %s status=%d\n", __func__, status);
5775 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5777 struct nfs4_getdeviceinfo_args args = {
5780 struct nfs4_getdeviceinfo_res res = {
5783 struct rpc_message msg = {
5784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5790 dprintk("--> %s\n", __func__);
5791 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5792 dprintk("<-- %s status=%d\n", __func__, status);
5797 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5799 struct nfs4_exception exception = { };
5803 err = nfs4_handle_exception(server,
5804 _nfs4_proc_getdeviceinfo(server, pdev),
5806 } while (exception.retry);
5809 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5811 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5813 struct nfs4_layoutcommit_data *data = calldata;
5814 struct nfs_server *server = NFS_SERVER(data->args.inode);
5816 if (nfs4_setup_sequence(server, &data->args.seq_args,
5817 &data->res.seq_res, 1, task))
5819 rpc_call_start(task);
5823 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5825 struct nfs4_layoutcommit_data *data = calldata;
5826 struct nfs_server *server = NFS_SERVER(data->args.inode);
5828 if (!nfs4_sequence_done(task, &data->res.seq_res))
5831 switch (task->tk_status) { /* Just ignore these failures */
5832 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5833 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5834 case NFS4ERR_BADLAYOUT: /* no layout */
5835 case NFS4ERR_GRACE: /* loca_recalim always false */
5836 task->tk_status = 0;
5839 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5840 nfs_restart_rpc(task, server->nfs_client);
5844 if (task->tk_status == 0)
5845 nfs_post_op_update_inode_force_wcc(data->args.inode,
5849 static void nfs4_layoutcommit_release(void *calldata)
5851 struct nfs4_layoutcommit_data *data = calldata;
5853 /* Matched by references in pnfs_set_layoutcommit */
5854 put_lseg(data->lseg);
5855 put_rpccred(data->cred);
5859 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
5860 .rpc_call_prepare = nfs4_layoutcommit_prepare,
5861 .rpc_call_done = nfs4_layoutcommit_done,
5862 .rpc_release = nfs4_layoutcommit_release,
5866 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
5868 struct rpc_message msg = {
5869 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
5870 .rpc_argp = &data->args,
5871 .rpc_resp = &data->res,
5872 .rpc_cred = data->cred,
5874 struct rpc_task_setup task_setup_data = {
5875 .task = &data->task,
5876 .rpc_client = NFS_CLIENT(data->args.inode),
5877 .rpc_message = &msg,
5878 .callback_ops = &nfs4_layoutcommit_ops,
5879 .callback_data = data,
5880 .flags = RPC_TASK_ASYNC,
5882 struct rpc_task *task;
5885 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
5886 "lbw: %llu inode %lu\n",
5887 data->task.tk_pid, sync,
5888 data->args.lastbytewritten,
5889 data->args.inode->i_ino);
5891 task = rpc_run_task(&task_setup_data);
5893 return PTR_ERR(task);
5896 status = nfs4_wait_for_completion_rpc_task(task);
5899 status = task->tk_status;
5901 dprintk("%s: status %d\n", __func__, status);
5905 #endif /* CONFIG_NFS_V4_1 */
5907 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
5908 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5909 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5910 .recover_open = nfs4_open_reclaim,
5911 .recover_lock = nfs4_lock_reclaim,
5912 .establish_clid = nfs4_init_clientid,
5913 .get_clid_cred = nfs4_get_setclientid_cred,
5916 #if defined(CONFIG_NFS_V4_1)
5917 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5918 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5919 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5920 .recover_open = nfs4_open_reclaim,
5921 .recover_lock = nfs4_lock_reclaim,
5922 .establish_clid = nfs41_init_clientid,
5923 .get_clid_cred = nfs4_get_exchange_id_cred,
5924 .reclaim_complete = nfs41_proc_reclaim_complete,
5926 #endif /* CONFIG_NFS_V4_1 */
5928 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
5929 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5930 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5931 .recover_open = nfs4_open_expired,
5932 .recover_lock = nfs4_lock_expired,
5933 .establish_clid = nfs4_init_clientid,
5934 .get_clid_cred = nfs4_get_setclientid_cred,
5937 #if defined(CONFIG_NFS_V4_1)
5938 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5939 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5940 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5941 .recover_open = nfs4_open_expired,
5942 .recover_lock = nfs4_lock_expired,
5943 .establish_clid = nfs41_init_clientid,
5944 .get_clid_cred = nfs4_get_exchange_id_cred,
5946 #endif /* CONFIG_NFS_V4_1 */
5948 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5949 .sched_state_renewal = nfs4_proc_async_renew,
5950 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
5951 .renew_lease = nfs4_proc_renew,
5954 #if defined(CONFIG_NFS_V4_1)
5955 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5956 .sched_state_renewal = nfs41_proc_async_sequence,
5957 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
5958 .renew_lease = nfs4_proc_sequence,
5962 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5964 .call_sync = _nfs4_call_sync,
5965 .validate_stateid = nfs4_validate_delegation_stateid,
5966 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5967 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5968 .state_renewal_ops = &nfs40_state_renewal_ops,
5971 #if defined(CONFIG_NFS_V4_1)
5972 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5974 .call_sync = _nfs4_call_sync_session,
5975 .validate_stateid = nfs41_validate_delegation_stateid,
5976 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5977 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5978 .state_renewal_ops = &nfs41_state_renewal_ops,
5982 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5983 [0] = &nfs_v4_0_minor_ops,
5984 #if defined(CONFIG_NFS_V4_1)
5985 [1] = &nfs_v4_1_minor_ops,
5989 static const struct inode_operations nfs4_file_inode_operations = {
5990 .permission = nfs_permission,
5991 .getattr = nfs_getattr,
5992 .setattr = nfs_setattr,
5993 .getxattr = generic_getxattr,
5994 .setxattr = generic_setxattr,
5995 .listxattr = generic_listxattr,
5996 .removexattr = generic_removexattr,
5999 const struct nfs_rpc_ops nfs_v4_clientops = {
6000 .version = 4, /* protocol version */
6001 .dentry_ops = &nfs4_dentry_operations,
6002 .dir_inode_ops = &nfs4_dir_inode_operations,
6003 .file_inode_ops = &nfs4_file_inode_operations,
6004 .getroot = nfs4_proc_get_root,
6005 .getattr = nfs4_proc_getattr,
6006 .setattr = nfs4_proc_setattr,
6007 .lookupfh = nfs4_proc_lookupfh,
6008 .lookup = nfs4_proc_lookup,
6009 .access = nfs4_proc_access,
6010 .readlink = nfs4_proc_readlink,
6011 .create = nfs4_proc_create,
6012 .remove = nfs4_proc_remove,
6013 .unlink_setup = nfs4_proc_unlink_setup,
6014 .unlink_done = nfs4_proc_unlink_done,
6015 .rename = nfs4_proc_rename,
6016 .rename_setup = nfs4_proc_rename_setup,
6017 .rename_done = nfs4_proc_rename_done,
6018 .link = nfs4_proc_link,
6019 .symlink = nfs4_proc_symlink,
6020 .mkdir = nfs4_proc_mkdir,
6021 .rmdir = nfs4_proc_remove,
6022 .readdir = nfs4_proc_readdir,
6023 .mknod = nfs4_proc_mknod,
6024 .statfs = nfs4_proc_statfs,
6025 .fsinfo = nfs4_proc_fsinfo,
6026 .pathconf = nfs4_proc_pathconf,
6027 .set_capabilities = nfs4_server_capabilities,
6028 .decode_dirent = nfs4_decode_dirent,
6029 .read_setup = nfs4_proc_read_setup,
6030 .read_done = nfs4_read_done,
6031 .write_setup = nfs4_proc_write_setup,
6032 .write_done = nfs4_write_done,
6033 .commit_setup = nfs4_proc_commit_setup,
6034 .commit_done = nfs4_commit_done,
6035 .lock = nfs4_proc_lock,
6036 .clear_acl_cache = nfs4_zap_acl_attr,
6037 .close_context = nfs4_close_context,
6038 .open_context = nfs4_atomic_open,
6039 .init_client = nfs4_init_client,
6040 .secinfo = nfs4_proc_secinfo,
6043 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6044 .prefix = XATTR_NAME_NFSV4_ACL,
6045 .list = nfs4_xattr_list_nfs4_acl,
6046 .get = nfs4_xattr_get_nfs4_acl,
6047 .set = nfs4_xattr_set_nfs4_acl,
6050 const struct xattr_handler *nfs4_xattr_handlers[] = {
6051 &nfs4_xattr_nfs4_acl_handler,