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(&opendata->path, 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(&data->path, 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(&data->path, 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 {
1877 struct inode *inode;
1878 struct nfs4_state *state;
1879 struct nfs_closeargs arg;
1880 struct nfs_closeres res;
1881 struct nfs_fattr fattr;
1882 unsigned long timestamp;
1887 static void nfs4_free_closedata(void *data)
1889 struct nfs4_closedata *calldata = data;
1890 struct nfs4_state_owner *sp = calldata->state->owner;
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 path_put(&calldata->path);
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 path *path, 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;
2054 calldata->path = *path;
2056 msg.rpc_argp = &calldata->arg;
2057 msg.rpc_resp = &calldata->res;
2058 task_setup_data.callback_data = calldata;
2059 task = rpc_run_task(&task_setup_data);
2061 return PTR_ERR(task);
2064 status = rpc_wait_for_completion_task(task);
2071 pnfs_roc_release(state->inode);
2072 nfs4_put_open_state(state);
2073 nfs4_put_state_owner(sp);
2077 static struct inode *
2078 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2080 struct nfs4_state *state;
2082 /* Protect against concurrent sillydeletes */
2083 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
2085 return ERR_CAST(state);
2087 return igrab(state->inode);
2090 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2092 if (ctx->state == NULL)
2095 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2097 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2100 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2102 struct nfs4_server_caps_arg args = {
2105 struct nfs4_server_caps_res res = {};
2106 struct rpc_message msg = {
2107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2113 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2115 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2116 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2117 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2118 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2119 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2120 NFS_CAP_CTIME|NFS_CAP_MTIME);
2121 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2122 server->caps |= NFS_CAP_ACLS;
2123 if (res.has_links != 0)
2124 server->caps |= NFS_CAP_HARDLINKS;
2125 if (res.has_symlinks != 0)
2126 server->caps |= NFS_CAP_SYMLINKS;
2127 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2128 server->caps |= NFS_CAP_FILEID;
2129 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2130 server->caps |= NFS_CAP_MODE;
2131 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2132 server->caps |= NFS_CAP_NLINK;
2133 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2134 server->caps |= NFS_CAP_OWNER;
2135 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2136 server->caps |= NFS_CAP_OWNER_GROUP;
2137 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2138 server->caps |= NFS_CAP_ATIME;
2139 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2140 server->caps |= NFS_CAP_CTIME;
2141 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2142 server->caps |= NFS_CAP_MTIME;
2144 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2145 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2146 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2147 server->acl_bitmask = res.acl_bitmask;
2153 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2155 struct nfs4_exception exception = { };
2158 err = nfs4_handle_exception(server,
2159 _nfs4_server_capabilities(server, fhandle),
2161 } while (exception.retry);
2165 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2166 struct nfs_fsinfo *info)
2168 struct nfs4_lookup_root_arg args = {
2169 .bitmask = nfs4_fattr_bitmap,
2171 struct nfs4_lookup_res res = {
2173 .fattr = info->fattr,
2176 struct rpc_message msg = {
2177 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2182 nfs_fattr_init(info->fattr);
2183 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2186 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2187 struct nfs_fsinfo *info)
2189 struct nfs4_exception exception = { };
2192 err = _nfs4_lookup_root(server, fhandle, info);
2195 case -NFS4ERR_WRONGSEC:
2198 err = nfs4_handle_exception(server, err, &exception);
2200 } while (exception.retry);
2204 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2205 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2207 struct rpc_auth *auth;
2210 auth = rpcauth_create(flavor, server->client);
2215 ret = nfs4_lookup_root(server, fhandle, info);
2220 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2221 struct nfs_fsinfo *info)
2223 int i, len, status = 0;
2224 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2226 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2227 flav_array[len] = RPC_AUTH_NULL;
2230 for (i = 0; i < len; i++) {
2231 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2232 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2237 * -EACCESS could mean that the user doesn't have correct permissions
2238 * to access the mount. It could also mean that we tried to mount
2239 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2240 * existing mount programs don't handle -EACCES very well so it should
2241 * be mapped to -EPERM instead.
2243 if (status == -EACCES)
2249 * get the file handle for the "/" directory on the server
2251 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2252 struct nfs_fsinfo *info)
2254 int status = nfs4_lookup_root(server, fhandle, info);
2255 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2257 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2258 * by nfs4_map_errors() as this function exits.
2260 status = nfs4_find_root_sec(server, fhandle, info);
2262 status = nfs4_server_capabilities(server, fhandle);
2264 status = nfs4_do_fsinfo(server, fhandle, info);
2265 return nfs4_map_errors(status);
2268 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
2270 * Get locations and (maybe) other attributes of a referral.
2271 * Note that we'll actually follow the referral later when
2272 * we detect fsid mismatch in inode revalidation
2274 static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2275 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2277 int status = -ENOMEM;
2278 struct page *page = NULL;
2279 struct nfs4_fs_locations *locations = NULL;
2281 page = alloc_page(GFP_KERNEL);
2284 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2285 if (locations == NULL)
2288 status = nfs4_proc_fs_locations(dir, name, locations, page);
2291 /* Make sure server returned a different fsid for the referral */
2292 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2293 dprintk("%s: server did not return a different fsid for"
2294 " a referral at %s\n", __func__, name->name);
2298 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2299 nfs_fixup_referral_attributes(&locations->fattr);
2301 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2302 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2303 memset(fhandle, 0, sizeof(struct nfs_fh));
2311 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2313 struct nfs4_getattr_arg args = {
2315 .bitmask = server->attr_bitmask,
2317 struct nfs4_getattr_res res = {
2321 struct rpc_message msg = {
2322 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2327 nfs_fattr_init(fattr);
2328 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2331 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2333 struct nfs4_exception exception = { };
2336 err = nfs4_handle_exception(server,
2337 _nfs4_proc_getattr(server, fhandle, fattr),
2339 } while (exception.retry);
2344 * The file is not closed if it is opened due to the a request to change
2345 * the size of the file. The open call will not be needed once the
2346 * VFS layer lookup-intents are implemented.
2348 * Close is called when the inode is destroyed.
2349 * If we haven't opened the file for O_WRONLY, we
2350 * need to in the size_change case to obtain a stateid.
2353 * Because OPEN is always done by name in nfsv4, it is
2354 * possible that we opened a different file by the same
2355 * name. We can recognize this race condition, but we
2356 * can't do anything about it besides returning an error.
2358 * This will be fixed with VFS changes (lookup-intent).
2361 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2362 struct iattr *sattr)
2364 struct inode *inode = dentry->d_inode;
2365 struct rpc_cred *cred = NULL;
2366 struct nfs4_state *state = NULL;
2369 if (pnfs_ld_layoutret_on_setattr(inode))
2370 pnfs_return_layout(inode);
2372 nfs_fattr_init(fattr);
2374 /* Search for an existing open(O_WRITE) file */
2375 if (sattr->ia_valid & ATTR_FILE) {
2376 struct nfs_open_context *ctx;
2378 ctx = nfs_file_open_context(sattr->ia_file);
2385 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2387 nfs_setattr_update_inode(inode, sattr);
2391 static int _nfs4_proc_lookupfh(struct rpc_clnt *clnt, struct nfs_server *server,
2392 const struct nfs_fh *dirfh, const struct qstr *name,
2393 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2396 struct nfs4_lookup_arg args = {
2397 .bitmask = server->attr_bitmask,
2401 struct nfs4_lookup_res res = {
2406 struct rpc_message msg = {
2407 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2412 nfs_fattr_init(fattr);
2414 dprintk("NFS call lookupfh %s\n", name->name);
2415 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2416 dprintk("NFS reply lookupfh: %d\n", status);
2420 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2421 struct qstr *name, struct nfs_fh *fhandle,
2422 struct nfs_fattr *fattr)
2424 struct nfs4_exception exception = { };
2427 err = _nfs4_proc_lookupfh(server->client, server, dirfh, name, fhandle, fattr);
2429 if (err == -NFS4ERR_MOVED) {
2433 err = nfs4_handle_exception(server, err, &exception);
2434 } while (exception.retry);
2438 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2439 const struct qstr *name, struct nfs_fh *fhandle,
2440 struct nfs_fattr *fattr)
2444 dprintk("NFS call lookup %s\n", name->name);
2445 status = _nfs4_proc_lookupfh(clnt, NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2446 if (status == -NFS4ERR_MOVED)
2447 status = nfs4_get_referral(dir, name, fattr, fhandle);
2448 dprintk("NFS reply lookup: %d\n", status);
2452 void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2454 memset(fh, 0, sizeof(struct nfs_fh));
2455 fattr->fsid.major = 1;
2456 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2457 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2458 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2462 static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2463 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2465 struct nfs4_exception exception = { };
2468 err = nfs4_handle_exception(NFS_SERVER(dir),
2469 _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr),
2472 nfs_fixup_secinfo_attributes(fattr, fhandle);
2473 } while (exception.retry);
2477 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2479 struct nfs_server *server = NFS_SERVER(inode);
2480 struct nfs4_accessargs args = {
2481 .fh = NFS_FH(inode),
2482 .bitmask = server->attr_bitmask,
2484 struct nfs4_accessres res = {
2487 struct rpc_message msg = {
2488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2491 .rpc_cred = entry->cred,
2493 int mode = entry->mask;
2497 * Determine which access bits we want to ask for...
2499 if (mode & MAY_READ)
2500 args.access |= NFS4_ACCESS_READ;
2501 if (S_ISDIR(inode->i_mode)) {
2502 if (mode & MAY_WRITE)
2503 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2504 if (mode & MAY_EXEC)
2505 args.access |= NFS4_ACCESS_LOOKUP;
2507 if (mode & MAY_WRITE)
2508 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2509 if (mode & MAY_EXEC)
2510 args.access |= NFS4_ACCESS_EXECUTE;
2513 res.fattr = nfs_alloc_fattr();
2514 if (res.fattr == NULL)
2517 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2520 if (res.access & NFS4_ACCESS_READ)
2521 entry->mask |= MAY_READ;
2522 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2523 entry->mask |= MAY_WRITE;
2524 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2525 entry->mask |= MAY_EXEC;
2526 nfs_refresh_inode(inode, res.fattr);
2528 nfs_free_fattr(res.fattr);
2532 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2534 struct nfs4_exception exception = { };
2537 err = nfs4_handle_exception(NFS_SERVER(inode),
2538 _nfs4_proc_access(inode, entry),
2540 } while (exception.retry);
2545 * TODO: For the time being, we don't try to get any attributes
2546 * along with any of the zero-copy operations READ, READDIR,
2549 * In the case of the first three, we want to put the GETATTR
2550 * after the read-type operation -- this is because it is hard
2551 * to predict the length of a GETATTR response in v4, and thus
2552 * align the READ data correctly. This means that the GETATTR
2553 * may end up partially falling into the page cache, and we should
2554 * shift it into the 'tail' of the xdr_buf before processing.
2555 * To do this efficiently, we need to know the total length
2556 * of data received, which doesn't seem to be available outside
2559 * In the case of WRITE, we also want to put the GETATTR after
2560 * the operation -- in this case because we want to make sure
2561 * we get the post-operation mtime and size. This means that
2562 * we can't use xdr_encode_pages() as written: we need a variant
2563 * of it which would leave room in the 'tail' iovec.
2565 * Both of these changes to the XDR layer would in fact be quite
2566 * minor, but I decided to leave them for a subsequent patch.
2568 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2569 unsigned int pgbase, unsigned int pglen)
2571 struct nfs4_readlink args = {
2572 .fh = NFS_FH(inode),
2577 struct nfs4_readlink_res res;
2578 struct rpc_message msg = {
2579 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2584 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2587 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2588 unsigned int pgbase, unsigned int pglen)
2590 struct nfs4_exception exception = { };
2593 err = nfs4_handle_exception(NFS_SERVER(inode),
2594 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2596 } while (exception.retry);
2602 * We will need to arrange for the VFS layer to provide an atomic open.
2603 * Until then, this create/open method is prone to inefficiency and race
2604 * conditions due to the lookup, create, and open VFS calls from sys_open()
2605 * placed on the wire.
2607 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2608 * The file will be opened again in the subsequent VFS open call
2609 * (nfs4_proc_file_open).
2611 * The open for read will just hang around to be used by any process that
2612 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2616 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2617 int flags, struct nfs_open_context *ctx)
2619 struct path my_path = {
2622 struct path *path = &my_path;
2623 struct nfs4_state *state;
2624 struct rpc_cred *cred = NULL;
2633 sattr->ia_mode &= ~current_umask();
2634 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
2636 if (IS_ERR(state)) {
2637 status = PTR_ERR(state);
2640 d_add(dentry, igrab(state->inode));
2641 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2645 nfs4_close_sync(path, state, fmode);
2650 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2652 struct nfs_server *server = NFS_SERVER(dir);
2653 struct nfs_removeargs args = {
2655 .name.len = name->len,
2656 .name.name = name->name,
2657 .bitmask = server->attr_bitmask,
2659 struct nfs_removeres res = {
2662 struct rpc_message msg = {
2663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2667 int status = -ENOMEM;
2669 res.dir_attr = nfs_alloc_fattr();
2670 if (res.dir_attr == NULL)
2673 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2675 update_changeattr(dir, &res.cinfo);
2676 nfs_post_op_update_inode(dir, res.dir_attr);
2678 nfs_free_fattr(res.dir_attr);
2683 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2685 struct nfs4_exception exception = { };
2688 err = nfs4_handle_exception(NFS_SERVER(dir),
2689 _nfs4_proc_remove(dir, name),
2691 } while (exception.retry);
2695 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2697 struct nfs_server *server = NFS_SERVER(dir);
2698 struct nfs_removeargs *args = msg->rpc_argp;
2699 struct nfs_removeres *res = msg->rpc_resp;
2701 args->bitmask = server->cache_consistency_bitmask;
2702 res->server = server;
2703 res->seq_res.sr_slot = NULL;
2704 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2707 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2709 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2711 if (!nfs4_sequence_done(task, &res->seq_res))
2713 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2715 update_changeattr(dir, &res->cinfo);
2716 nfs_post_op_update_inode(dir, res->dir_attr);
2720 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2722 struct nfs_server *server = NFS_SERVER(dir);
2723 struct nfs_renameargs *arg = msg->rpc_argp;
2724 struct nfs_renameres *res = msg->rpc_resp;
2726 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2727 arg->bitmask = server->attr_bitmask;
2728 res->server = server;
2731 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2732 struct inode *new_dir)
2734 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2736 if (!nfs4_sequence_done(task, &res->seq_res))
2738 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2741 update_changeattr(old_dir, &res->old_cinfo);
2742 nfs_post_op_update_inode(old_dir, res->old_fattr);
2743 update_changeattr(new_dir, &res->new_cinfo);
2744 nfs_post_op_update_inode(new_dir, res->new_fattr);
2748 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2749 struct inode *new_dir, struct qstr *new_name)
2751 struct nfs_server *server = NFS_SERVER(old_dir);
2752 struct nfs_renameargs arg = {
2753 .old_dir = NFS_FH(old_dir),
2754 .new_dir = NFS_FH(new_dir),
2755 .old_name = old_name,
2756 .new_name = new_name,
2757 .bitmask = server->attr_bitmask,
2759 struct nfs_renameres res = {
2762 struct rpc_message msg = {
2763 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2767 int status = -ENOMEM;
2769 res.old_fattr = nfs_alloc_fattr();
2770 res.new_fattr = nfs_alloc_fattr();
2771 if (res.old_fattr == NULL || res.new_fattr == NULL)
2774 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2776 update_changeattr(old_dir, &res.old_cinfo);
2777 nfs_post_op_update_inode(old_dir, res.old_fattr);
2778 update_changeattr(new_dir, &res.new_cinfo);
2779 nfs_post_op_update_inode(new_dir, res.new_fattr);
2782 nfs_free_fattr(res.new_fattr);
2783 nfs_free_fattr(res.old_fattr);
2787 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2788 struct inode *new_dir, struct qstr *new_name)
2790 struct nfs4_exception exception = { };
2793 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2794 _nfs4_proc_rename(old_dir, old_name,
2797 } while (exception.retry);
2801 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2803 struct nfs_server *server = NFS_SERVER(inode);
2804 struct nfs4_link_arg arg = {
2805 .fh = NFS_FH(inode),
2806 .dir_fh = NFS_FH(dir),
2808 .bitmask = server->attr_bitmask,
2810 struct nfs4_link_res res = {
2813 struct rpc_message msg = {
2814 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2818 int status = -ENOMEM;
2820 res.fattr = nfs_alloc_fattr();
2821 res.dir_attr = nfs_alloc_fattr();
2822 if (res.fattr == NULL || res.dir_attr == NULL)
2825 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2827 update_changeattr(dir, &res.cinfo);
2828 nfs_post_op_update_inode(dir, res.dir_attr);
2829 nfs_post_op_update_inode(inode, res.fattr);
2832 nfs_free_fattr(res.dir_attr);
2833 nfs_free_fattr(res.fattr);
2837 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2839 struct nfs4_exception exception = { };
2842 err = nfs4_handle_exception(NFS_SERVER(inode),
2843 _nfs4_proc_link(inode, dir, name),
2845 } while (exception.retry);
2849 struct nfs4_createdata {
2850 struct rpc_message msg;
2851 struct nfs4_create_arg arg;
2852 struct nfs4_create_res res;
2854 struct nfs_fattr fattr;
2855 struct nfs_fattr dir_fattr;
2858 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2859 struct qstr *name, struct iattr *sattr, u32 ftype)
2861 struct nfs4_createdata *data;
2863 data = kzalloc(sizeof(*data), GFP_KERNEL);
2865 struct nfs_server *server = NFS_SERVER(dir);
2867 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2868 data->msg.rpc_argp = &data->arg;
2869 data->msg.rpc_resp = &data->res;
2870 data->arg.dir_fh = NFS_FH(dir);
2871 data->arg.server = server;
2872 data->arg.name = name;
2873 data->arg.attrs = sattr;
2874 data->arg.ftype = ftype;
2875 data->arg.bitmask = server->attr_bitmask;
2876 data->res.server = server;
2877 data->res.fh = &data->fh;
2878 data->res.fattr = &data->fattr;
2879 data->res.dir_fattr = &data->dir_fattr;
2880 nfs_fattr_init(data->res.fattr);
2881 nfs_fattr_init(data->res.dir_fattr);
2886 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2888 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
2889 &data->arg.seq_args, &data->res.seq_res, 1);
2891 update_changeattr(dir, &data->res.dir_cinfo);
2892 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2893 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2898 static void nfs4_free_createdata(struct nfs4_createdata *data)
2903 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2904 struct page *page, unsigned int len, struct iattr *sattr)
2906 struct nfs4_createdata *data;
2907 int status = -ENAMETOOLONG;
2909 if (len > NFS4_MAXPATHLEN)
2913 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2917 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2918 data->arg.u.symlink.pages = &page;
2919 data->arg.u.symlink.len = len;
2921 status = nfs4_do_create(dir, dentry, data);
2923 nfs4_free_createdata(data);
2928 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2929 struct page *page, unsigned int len, struct iattr *sattr)
2931 struct nfs4_exception exception = { };
2934 err = nfs4_handle_exception(NFS_SERVER(dir),
2935 _nfs4_proc_symlink(dir, dentry, page,
2938 } while (exception.retry);
2942 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2943 struct iattr *sattr)
2945 struct nfs4_createdata *data;
2946 int status = -ENOMEM;
2948 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2952 status = nfs4_do_create(dir, dentry, data);
2954 nfs4_free_createdata(data);
2959 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2960 struct iattr *sattr)
2962 struct nfs4_exception exception = { };
2965 sattr->ia_mode &= ~current_umask();
2967 err = nfs4_handle_exception(NFS_SERVER(dir),
2968 _nfs4_proc_mkdir(dir, dentry, sattr),
2970 } while (exception.retry);
2974 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2975 u64 cookie, struct page **pages, unsigned int count, int plus)
2977 struct inode *dir = dentry->d_inode;
2978 struct nfs4_readdir_arg args = {
2983 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2986 struct nfs4_readdir_res res;
2987 struct rpc_message msg = {
2988 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2995 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2996 dentry->d_parent->d_name.name,
2997 dentry->d_name.name,
2998 (unsigned long long)cookie);
2999 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3000 res.pgbase = args.pgbase;
3001 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3003 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
3004 status += args.pgbase;
3007 nfs_invalidate_atime(dir);
3009 dprintk("%s: returns %d\n", __func__, status);
3013 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3014 u64 cookie, struct page **pages, unsigned int count, int plus)
3016 struct nfs4_exception exception = { };
3019 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3020 _nfs4_proc_readdir(dentry, cred, cookie,
3021 pages, count, plus),
3023 } while (exception.retry);
3027 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3028 struct iattr *sattr, dev_t rdev)
3030 struct nfs4_createdata *data;
3031 int mode = sattr->ia_mode;
3032 int status = -ENOMEM;
3034 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3035 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3037 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3042 data->arg.ftype = NF4FIFO;
3043 else if (S_ISBLK(mode)) {
3044 data->arg.ftype = NF4BLK;
3045 data->arg.u.device.specdata1 = MAJOR(rdev);
3046 data->arg.u.device.specdata2 = MINOR(rdev);
3048 else if (S_ISCHR(mode)) {
3049 data->arg.ftype = NF4CHR;
3050 data->arg.u.device.specdata1 = MAJOR(rdev);
3051 data->arg.u.device.specdata2 = MINOR(rdev);
3054 status = nfs4_do_create(dir, dentry, data);
3056 nfs4_free_createdata(data);
3061 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3062 struct iattr *sattr, dev_t rdev)
3064 struct nfs4_exception exception = { };
3067 sattr->ia_mode &= ~current_umask();
3069 err = nfs4_handle_exception(NFS_SERVER(dir),
3070 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3072 } while (exception.retry);
3076 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3077 struct nfs_fsstat *fsstat)
3079 struct nfs4_statfs_arg args = {
3081 .bitmask = server->attr_bitmask,
3083 struct nfs4_statfs_res res = {
3086 struct rpc_message msg = {
3087 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3092 nfs_fattr_init(fsstat->fattr);
3093 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3096 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3098 struct nfs4_exception exception = { };
3101 err = nfs4_handle_exception(server,
3102 _nfs4_proc_statfs(server, fhandle, fsstat),
3104 } while (exception.retry);
3108 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3109 struct nfs_fsinfo *fsinfo)
3111 struct nfs4_fsinfo_arg args = {
3113 .bitmask = server->attr_bitmask,
3115 struct nfs4_fsinfo_res res = {
3118 struct rpc_message msg = {
3119 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3124 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3127 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3129 struct nfs4_exception exception = { };
3133 err = nfs4_handle_exception(server,
3134 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3136 } while (exception.retry);
3140 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3142 nfs_fattr_init(fsinfo->fattr);
3143 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3146 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3147 struct nfs_pathconf *pathconf)
3149 struct nfs4_pathconf_arg args = {
3151 .bitmask = server->attr_bitmask,
3153 struct nfs4_pathconf_res res = {
3154 .pathconf = pathconf,
3156 struct rpc_message msg = {
3157 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3162 /* None of the pathconf attributes are mandatory to implement */
3163 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3164 memset(pathconf, 0, sizeof(*pathconf));
3168 nfs_fattr_init(pathconf->fattr);
3169 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3172 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3173 struct nfs_pathconf *pathconf)
3175 struct nfs4_exception exception = { };
3179 err = nfs4_handle_exception(server,
3180 _nfs4_proc_pathconf(server, fhandle, pathconf),
3182 } while (exception.retry);
3186 void __nfs4_read_done_cb(struct nfs_read_data *data)
3188 nfs_invalidate_atime(data->inode);
3191 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3193 struct nfs_server *server = NFS_SERVER(data->inode);
3195 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3196 nfs_restart_rpc(task, server->nfs_client);
3200 __nfs4_read_done_cb(data);
3201 if (task->tk_status > 0)
3202 renew_lease(server, data->timestamp);
3206 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3209 dprintk("--> %s\n", __func__);
3211 if (!nfs4_sequence_done(task, &data->res.seq_res))
3214 return data->read_done_cb ? data->read_done_cb(task, data) :
3215 nfs4_read_done_cb(task, data);
3218 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3220 data->timestamp = jiffies;
3221 data->read_done_cb = nfs4_read_done_cb;
3222 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3225 /* Reset the the nfs_read_data to send the read to the MDS. */
3226 void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3228 dprintk("%s Reset task for i/o through\n", __func__);
3229 put_lseg(data->lseg);
3231 /* offsets will differ in the dense stripe case */
3232 data->args.offset = data->mds_offset;
3233 data->ds_clp = NULL;
3234 data->args.fh = NFS_FH(data->inode);
3235 data->read_done_cb = nfs4_read_done_cb;
3236 task->tk_ops = data->mds_ops;
3237 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3239 EXPORT_SYMBOL_GPL(nfs4_reset_read);
3241 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3243 struct inode *inode = data->inode;
3245 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3246 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3249 if (task->tk_status >= 0) {
3250 renew_lease(NFS_SERVER(inode), data->timestamp);
3251 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3256 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3258 if (!nfs4_sequence_done(task, &data->res.seq_res))
3260 return data->write_done_cb ? data->write_done_cb(task, data) :
3261 nfs4_write_done_cb(task, data);
3264 /* Reset the the nfs_write_data to send the write to the MDS. */
3265 void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3267 dprintk("%s Reset task for i/o through\n", __func__);
3268 put_lseg(data->lseg);
3270 data->ds_clp = NULL;
3271 data->write_done_cb = nfs4_write_done_cb;
3272 data->args.fh = NFS_FH(data->inode);
3273 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3274 data->args.offset = data->mds_offset;
3275 data->res.fattr = &data->fattr;
3276 task->tk_ops = data->mds_ops;
3277 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3279 EXPORT_SYMBOL_GPL(nfs4_reset_write);
3281 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3283 struct nfs_server *server = NFS_SERVER(data->inode);
3286 data->args.bitmask = NULL;
3287 data->res.fattr = NULL;
3289 data->args.bitmask = server->cache_consistency_bitmask;
3290 if (!data->write_done_cb)
3291 data->write_done_cb = nfs4_write_done_cb;
3292 data->res.server = server;
3293 data->timestamp = jiffies;
3295 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3298 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3300 struct inode *inode = data->inode;
3302 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3303 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3306 nfs_refresh_inode(inode, data->res.fattr);
3310 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3312 if (!nfs4_sequence_done(task, &data->res.seq_res))
3314 return data->write_done_cb(task, data);
3317 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3319 struct nfs_server *server = NFS_SERVER(data->inode);
3322 data->args.bitmask = NULL;
3323 data->res.fattr = NULL;
3325 data->args.bitmask = server->cache_consistency_bitmask;
3326 if (!data->write_done_cb)
3327 data->write_done_cb = nfs4_commit_done_cb;
3328 data->res.server = server;
3329 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3332 struct nfs4_renewdata {
3333 struct nfs_client *client;
3334 unsigned long timestamp;
3338 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3339 * standalone procedure for queueing an asynchronous RENEW.
3341 static void nfs4_renew_release(void *calldata)
3343 struct nfs4_renewdata *data = calldata;
3344 struct nfs_client *clp = data->client;
3346 if (atomic_read(&clp->cl_count) > 1)
3347 nfs4_schedule_state_renewal(clp);
3348 nfs_put_client(clp);
3352 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3354 struct nfs4_renewdata *data = calldata;
3355 struct nfs_client *clp = data->client;
3356 unsigned long timestamp = data->timestamp;
3358 if (task->tk_status < 0) {
3359 /* Unless we're shutting down, schedule state recovery! */
3360 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3361 nfs4_schedule_lease_recovery(clp);
3364 do_renew_lease(clp, timestamp);
3367 static const struct rpc_call_ops nfs4_renew_ops = {
3368 .rpc_call_done = nfs4_renew_done,
3369 .rpc_release = nfs4_renew_release,
3372 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3374 struct rpc_message msg = {
3375 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3379 struct nfs4_renewdata *data;
3381 if (!atomic_inc_not_zero(&clp->cl_count))
3383 data = kmalloc(sizeof(*data), GFP_KERNEL);
3387 data->timestamp = jiffies;
3388 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3389 &nfs4_renew_ops, data);
3392 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3394 struct rpc_message msg = {
3395 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3399 unsigned long now = jiffies;
3402 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3405 do_renew_lease(clp, now);
3409 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3411 return (server->caps & NFS_CAP_ACLS)
3412 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3413 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3416 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3417 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3420 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3422 static void buf_to_pages(const void *buf, size_t buflen,
3423 struct page **pages, unsigned int *pgbase)
3425 const void *p = buf;
3427 *pgbase = offset_in_page(buf);
3429 while (p < buf + buflen) {
3430 *(pages++) = virt_to_page(p);
3431 p += PAGE_CACHE_SIZE;
3435 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3436 struct page **pages, unsigned int *pgbase)
3438 struct page *newpage, **spages;
3444 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3445 newpage = alloc_page(GFP_KERNEL);
3447 if (newpage == NULL)
3449 memcpy(page_address(newpage), buf, len);
3454 } while (buflen != 0);
3460 __free_page(spages[rc-1]);
3464 struct nfs4_cached_acl {
3470 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3472 struct nfs_inode *nfsi = NFS_I(inode);
3474 spin_lock(&inode->i_lock);
3475 kfree(nfsi->nfs4_acl);
3476 nfsi->nfs4_acl = acl;
3477 spin_unlock(&inode->i_lock);
3480 static void nfs4_zap_acl_attr(struct inode *inode)
3482 nfs4_set_cached_acl(inode, NULL);
3485 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3487 struct nfs_inode *nfsi = NFS_I(inode);
3488 struct nfs4_cached_acl *acl;
3491 spin_lock(&inode->i_lock);
3492 acl = nfsi->nfs4_acl;
3495 if (buf == NULL) /* user is just asking for length */
3497 if (acl->cached == 0)
3499 ret = -ERANGE; /* see getxattr(2) man page */
3500 if (acl->len > buflen)
3502 memcpy(buf, acl->data, acl->len);
3506 spin_unlock(&inode->i_lock);
3510 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3512 struct nfs4_cached_acl *acl;
3514 if (buf && acl_len <= PAGE_SIZE) {
3515 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3519 memcpy(acl->data, buf, acl_len);
3521 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3528 nfs4_set_cached_acl(inode, acl);
3531 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3533 struct page *pages[NFS4ACL_MAXPAGES];
3534 struct nfs_getaclargs args = {
3535 .fh = NFS_FH(inode),
3539 struct nfs_getaclres res = {
3543 struct rpc_message msg = {
3544 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3548 struct page *localpage = NULL;
3551 if (buflen < PAGE_SIZE) {
3552 /* As long as we're doing a round trip to the server anyway,
3553 * let's be prepared for a page of acl data. */
3554 localpage = alloc_page(GFP_KERNEL);
3555 resp_buf = page_address(localpage);
3556 if (localpage == NULL)
3558 args.acl_pages[0] = localpage;
3559 args.acl_pgbase = 0;
3560 args.acl_len = PAGE_SIZE;
3563 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3565 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3568 if (res.acl_len > args.acl_len)
3569 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3571 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3574 if (res.acl_len > buflen)
3577 memcpy(buf, resp_buf, res.acl_len);
3582 __free_page(localpage);
3586 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3588 struct nfs4_exception exception = { };
3591 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3594 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3595 } while (exception.retry);
3599 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3601 struct nfs_server *server = NFS_SERVER(inode);
3604 if (!nfs4_server_supports_acls(server))
3606 ret = nfs_revalidate_inode(server, inode);
3609 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3610 nfs_zap_acl_cache(inode);
3611 ret = nfs4_read_cached_acl(inode, buf, buflen);
3614 return nfs4_get_acl_uncached(inode, buf, buflen);
3617 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3619 struct nfs_server *server = NFS_SERVER(inode);
3620 struct page *pages[NFS4ACL_MAXPAGES];
3621 struct nfs_setaclargs arg = {
3622 .fh = NFS_FH(inode),
3626 struct nfs_setaclres res;
3627 struct rpc_message msg = {
3628 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3634 if (!nfs4_server_supports_acls(server))
3636 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3639 nfs_inode_return_delegation(inode);
3640 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3643 * Free each page after tx, so the only ref left is
3644 * held by the network stack
3647 put_page(pages[i-1]);
3650 * Acl update can result in inode attribute update.
3651 * so mark the attribute cache invalid.
3653 spin_lock(&inode->i_lock);
3654 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3655 spin_unlock(&inode->i_lock);
3656 nfs_access_zap_cache(inode);
3657 nfs_zap_acl_cache(inode);
3661 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3663 struct nfs4_exception exception = { };
3666 err = nfs4_handle_exception(NFS_SERVER(inode),
3667 __nfs4_proc_set_acl(inode, buf, buflen),
3669 } while (exception.retry);
3674 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3676 struct nfs_client *clp = server->nfs_client;
3678 if (task->tk_status >= 0)
3680 switch(task->tk_status) {
3681 case -NFS4ERR_ADMIN_REVOKED:
3682 case -NFS4ERR_BAD_STATEID:
3683 case -NFS4ERR_OPENMODE:
3686 nfs4_schedule_stateid_recovery(server, state);
3687 goto wait_on_recovery;
3688 case -NFS4ERR_EXPIRED:
3690 nfs4_schedule_stateid_recovery(server, state);
3691 case -NFS4ERR_STALE_STATEID:
3692 case -NFS4ERR_STALE_CLIENTID:
3693 nfs4_schedule_lease_recovery(clp);
3694 goto wait_on_recovery;
3695 #if defined(CONFIG_NFS_V4_1)
3696 case -NFS4ERR_BADSESSION:
3697 case -NFS4ERR_BADSLOT:
3698 case -NFS4ERR_BAD_HIGH_SLOT:
3699 case -NFS4ERR_DEADSESSION:
3700 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3701 case -NFS4ERR_SEQ_FALSE_RETRY:
3702 case -NFS4ERR_SEQ_MISORDERED:
3703 dprintk("%s ERROR %d, Reset session\n", __func__,
3705 nfs4_schedule_session_recovery(clp->cl_session);
3706 task->tk_status = 0;
3708 #endif /* CONFIG_NFS_V4_1 */
3709 case -NFS4ERR_DELAY:
3710 nfs_inc_server_stats(server, NFSIOS_DELAY);
3711 case -NFS4ERR_GRACE:
3713 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3714 task->tk_status = 0;
3716 case -NFS4ERR_RETRY_UNCACHED_REP:
3717 case -NFS4ERR_OLD_STATEID:
3718 task->tk_status = 0;
3721 task->tk_status = nfs4_map_errors(task->tk_status);
3724 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3725 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3726 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3727 task->tk_status = 0;
3731 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3732 unsigned short port, struct rpc_cred *cred,
3733 struct nfs4_setclientid_res *res)
3735 nfs4_verifier sc_verifier;
3736 struct nfs4_setclientid setclientid = {
3737 .sc_verifier = &sc_verifier,
3739 .sc_cb_ident = clp->cl_cb_ident,
3741 struct rpc_message msg = {
3742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3743 .rpc_argp = &setclientid,
3751 p = (__be32*)sc_verifier.data;
3752 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3753 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3756 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3757 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3759 rpc_peeraddr2str(clp->cl_rpcclient,
3761 rpc_peeraddr2str(clp->cl_rpcclient,
3763 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3764 clp->cl_id_uniquifier);
3765 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3766 sizeof(setclientid.sc_netid),
3767 rpc_peeraddr2str(clp->cl_rpcclient,
3768 RPC_DISPLAY_NETID));
3769 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3770 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3771 clp->cl_ipaddr, port >> 8, port & 255);
3773 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3774 if (status != -NFS4ERR_CLID_INUSE)
3777 ++clp->cl_id_uniquifier;
3781 ssleep(clp->cl_lease_time / HZ + 1);
3786 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3787 struct nfs4_setclientid_res *arg,
3788 struct rpc_cred *cred)
3790 struct nfs_fsinfo fsinfo;
3791 struct rpc_message msg = {
3792 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3794 .rpc_resp = &fsinfo,
3801 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3803 spin_lock(&clp->cl_lock);
3804 clp->cl_lease_time = fsinfo.lease_time * HZ;
3805 clp->cl_last_renewal = now;
3806 spin_unlock(&clp->cl_lock);
3811 struct nfs4_delegreturndata {
3812 struct nfs4_delegreturnargs args;
3813 struct nfs4_delegreturnres res;
3815 nfs4_stateid stateid;
3816 unsigned long timestamp;
3817 struct nfs_fattr fattr;
3821 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3823 struct nfs4_delegreturndata *data = calldata;
3825 if (!nfs4_sequence_done(task, &data->res.seq_res))
3828 switch (task->tk_status) {
3829 case -NFS4ERR_STALE_STATEID:
3830 case -NFS4ERR_EXPIRED:
3832 renew_lease(data->res.server, data->timestamp);
3835 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3837 nfs_restart_rpc(task, data->res.server->nfs_client);
3841 data->rpc_status = task->tk_status;
3844 static void nfs4_delegreturn_release(void *calldata)
3849 #if defined(CONFIG_NFS_V4_1)
3850 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3852 struct nfs4_delegreturndata *d_data;
3854 d_data = (struct nfs4_delegreturndata *)data;
3856 if (nfs4_setup_sequence(d_data->res.server,
3857 &d_data->args.seq_args,
3858 &d_data->res.seq_res, 1, task))
3860 rpc_call_start(task);
3862 #endif /* CONFIG_NFS_V4_1 */
3864 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3865 #if defined(CONFIG_NFS_V4_1)
3866 .rpc_call_prepare = nfs4_delegreturn_prepare,
3867 #endif /* CONFIG_NFS_V4_1 */
3868 .rpc_call_done = nfs4_delegreturn_done,
3869 .rpc_release = nfs4_delegreturn_release,
3872 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3874 struct nfs4_delegreturndata *data;
3875 struct nfs_server *server = NFS_SERVER(inode);
3876 struct rpc_task *task;
3877 struct rpc_message msg = {
3878 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3881 struct rpc_task_setup task_setup_data = {
3882 .rpc_client = server->client,
3883 .rpc_message = &msg,
3884 .callback_ops = &nfs4_delegreturn_ops,
3885 .flags = RPC_TASK_ASYNC,
3889 data = kzalloc(sizeof(*data), GFP_NOFS);
3892 data->args.fhandle = &data->fh;
3893 data->args.stateid = &data->stateid;
3894 data->args.bitmask = server->attr_bitmask;
3895 nfs_copy_fh(&data->fh, NFS_FH(inode));
3896 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3897 data->res.fattr = &data->fattr;
3898 data->res.server = server;
3899 nfs_fattr_init(data->res.fattr);
3900 data->timestamp = jiffies;
3901 data->rpc_status = 0;
3903 task_setup_data.callback_data = data;
3904 msg.rpc_argp = &data->args;
3905 msg.rpc_resp = &data->res;
3906 task = rpc_run_task(&task_setup_data);
3908 return PTR_ERR(task);
3911 status = nfs4_wait_for_completion_rpc_task(task);
3914 status = data->rpc_status;
3917 nfs_refresh_inode(inode, &data->fattr);
3923 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3925 struct nfs_server *server = NFS_SERVER(inode);
3926 struct nfs4_exception exception = { };
3929 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3931 case -NFS4ERR_STALE_STATEID:
3932 case -NFS4ERR_EXPIRED:
3936 err = nfs4_handle_exception(server, err, &exception);
3937 } while (exception.retry);
3941 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3942 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3945 * sleep, with exponential backoff, and retry the LOCK operation.
3947 static unsigned long
3948 nfs4_set_lock_task_retry(unsigned long timeout)
3950 schedule_timeout_killable(timeout);
3952 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3953 return NFS4_LOCK_MAXTIMEOUT;
3957 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3959 struct inode *inode = state->inode;
3960 struct nfs_server *server = NFS_SERVER(inode);
3961 struct nfs_client *clp = server->nfs_client;
3962 struct nfs_lockt_args arg = {
3963 .fh = NFS_FH(inode),
3966 struct nfs_lockt_res res = {
3969 struct rpc_message msg = {
3970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3973 .rpc_cred = state->owner->so_cred,
3975 struct nfs4_lock_state *lsp;
3978 arg.lock_owner.clientid = clp->cl_clientid;
3979 status = nfs4_set_lock_state(state, request);
3982 lsp = request->fl_u.nfs4_fl.owner;
3983 arg.lock_owner.id = lsp->ls_id.id;
3984 arg.lock_owner.s_dev = server->s_dev;
3985 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3988 request->fl_type = F_UNLCK;
3990 case -NFS4ERR_DENIED:
3993 request->fl_ops->fl_release_private(request);
3998 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4000 struct nfs4_exception exception = { };
4004 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4005 _nfs4_proc_getlk(state, cmd, request),
4007 } while (exception.retry);
4011 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4014 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4016 res = posix_lock_file_wait(file, fl);
4019 res = flock_lock_file_wait(file, fl);
4027 struct nfs4_unlockdata {
4028 struct nfs_locku_args arg;
4029 struct nfs_locku_res res;
4030 struct nfs4_lock_state *lsp;
4031 struct nfs_open_context *ctx;
4032 struct file_lock fl;
4033 const struct nfs_server *server;
4034 unsigned long timestamp;
4037 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4038 struct nfs_open_context *ctx,
4039 struct nfs4_lock_state *lsp,
4040 struct nfs_seqid *seqid)
4042 struct nfs4_unlockdata *p;
4043 struct inode *inode = lsp->ls_state->inode;
4045 p = kzalloc(sizeof(*p), GFP_NOFS);
4048 p->arg.fh = NFS_FH(inode);
4050 p->arg.seqid = seqid;
4051 p->res.seqid = seqid;
4052 p->arg.stateid = &lsp->ls_stateid;
4054 atomic_inc(&lsp->ls_count);
4055 /* Ensure we don't close file until we're done freeing locks! */
4056 p->ctx = get_nfs_open_context(ctx);
4057 memcpy(&p->fl, fl, sizeof(p->fl));
4058 p->server = NFS_SERVER(inode);
4062 static void nfs4_locku_release_calldata(void *data)
4064 struct nfs4_unlockdata *calldata = data;
4065 nfs_free_seqid(calldata->arg.seqid);
4066 nfs4_put_lock_state(calldata->lsp);
4067 put_nfs_open_context(calldata->ctx);
4071 static void nfs4_locku_done(struct rpc_task *task, void *data)
4073 struct nfs4_unlockdata *calldata = data;
4075 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4077 switch (task->tk_status) {
4079 memcpy(calldata->lsp->ls_stateid.data,
4080 calldata->res.stateid.data,
4081 sizeof(calldata->lsp->ls_stateid.data));
4082 renew_lease(calldata->server, calldata->timestamp);
4084 case -NFS4ERR_BAD_STATEID:
4085 case -NFS4ERR_OLD_STATEID:
4086 case -NFS4ERR_STALE_STATEID:
4087 case -NFS4ERR_EXPIRED:
4090 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4091 nfs_restart_rpc(task,
4092 calldata->server->nfs_client);
4096 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4098 struct nfs4_unlockdata *calldata = data;
4100 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4102 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
4103 /* Note: exit _without_ running nfs4_locku_done */
4104 task->tk_action = NULL;
4107 calldata->timestamp = jiffies;
4108 if (nfs4_setup_sequence(calldata->server,
4109 &calldata->arg.seq_args,
4110 &calldata->res.seq_res, 1, task))
4112 rpc_call_start(task);
4115 static const struct rpc_call_ops nfs4_locku_ops = {
4116 .rpc_call_prepare = nfs4_locku_prepare,
4117 .rpc_call_done = nfs4_locku_done,
4118 .rpc_release = nfs4_locku_release_calldata,
4121 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4122 struct nfs_open_context *ctx,
4123 struct nfs4_lock_state *lsp,
4124 struct nfs_seqid *seqid)
4126 struct nfs4_unlockdata *data;
4127 struct rpc_message msg = {
4128 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4129 .rpc_cred = ctx->cred,
4131 struct rpc_task_setup task_setup_data = {
4132 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4133 .rpc_message = &msg,
4134 .callback_ops = &nfs4_locku_ops,
4135 .workqueue = nfsiod_workqueue,
4136 .flags = RPC_TASK_ASYNC,
4139 /* Ensure this is an unlock - when canceling a lock, the
4140 * canceled lock is passed in, and it won't be an unlock.
4142 fl->fl_type = F_UNLCK;
4144 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4146 nfs_free_seqid(seqid);
4147 return ERR_PTR(-ENOMEM);
4150 msg.rpc_argp = &data->arg;
4151 msg.rpc_resp = &data->res;
4152 task_setup_data.callback_data = data;
4153 return rpc_run_task(&task_setup_data);
4156 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4158 struct nfs_inode *nfsi = NFS_I(state->inode);
4159 struct nfs_seqid *seqid;
4160 struct nfs4_lock_state *lsp;
4161 struct rpc_task *task;
4163 unsigned char fl_flags = request->fl_flags;
4165 status = nfs4_set_lock_state(state, request);
4166 /* Unlock _before_ we do the RPC call */
4167 request->fl_flags |= FL_EXISTS;
4168 down_read(&nfsi->rwsem);
4169 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4170 up_read(&nfsi->rwsem);
4173 up_read(&nfsi->rwsem);
4176 /* Is this a delegated lock? */
4177 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4179 lsp = request->fl_u.nfs4_fl.owner;
4180 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4184 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4185 status = PTR_ERR(task);
4188 status = nfs4_wait_for_completion_rpc_task(task);
4191 request->fl_flags = fl_flags;
4195 struct nfs4_lockdata {
4196 struct nfs_lock_args arg;
4197 struct nfs_lock_res res;
4198 struct nfs4_lock_state *lsp;
4199 struct nfs_open_context *ctx;
4200 struct file_lock fl;
4201 unsigned long timestamp;
4204 struct nfs_server *server;
4207 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4208 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4211 struct nfs4_lockdata *p;
4212 struct inode *inode = lsp->ls_state->inode;
4213 struct nfs_server *server = NFS_SERVER(inode);
4215 p = kzalloc(sizeof(*p), gfp_mask);
4219 p->arg.fh = NFS_FH(inode);
4221 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4222 if (p->arg.open_seqid == NULL)
4224 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4225 if (p->arg.lock_seqid == NULL)
4226 goto out_free_seqid;
4227 p->arg.lock_stateid = &lsp->ls_stateid;
4228 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4229 p->arg.lock_owner.id = lsp->ls_id.id;
4230 p->arg.lock_owner.s_dev = server->s_dev;
4231 p->res.lock_seqid = p->arg.lock_seqid;
4234 atomic_inc(&lsp->ls_count);
4235 p->ctx = get_nfs_open_context(ctx);
4236 memcpy(&p->fl, fl, sizeof(p->fl));
4239 nfs_free_seqid(p->arg.open_seqid);
4245 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4247 struct nfs4_lockdata *data = calldata;
4248 struct nfs4_state *state = data->lsp->ls_state;
4250 dprintk("%s: begin!\n", __func__);
4251 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4253 /* Do we need to do an open_to_lock_owner? */
4254 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4255 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4257 data->arg.open_stateid = &state->stateid;
4258 data->arg.new_lock_owner = 1;
4259 data->res.open_seqid = data->arg.open_seqid;
4261 data->arg.new_lock_owner = 0;
4262 data->timestamp = jiffies;
4263 if (nfs4_setup_sequence(data->server,
4264 &data->arg.seq_args,
4265 &data->res.seq_res, 1, task))
4267 rpc_call_start(task);
4268 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4271 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4273 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4274 nfs4_lock_prepare(task, calldata);
4277 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4279 struct nfs4_lockdata *data = calldata;
4281 dprintk("%s: begin!\n", __func__);
4283 if (!nfs4_sequence_done(task, &data->res.seq_res))
4286 data->rpc_status = task->tk_status;
4287 if (data->arg.new_lock_owner != 0) {
4288 if (data->rpc_status == 0)
4289 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4293 if (data->rpc_status == 0) {
4294 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4295 sizeof(data->lsp->ls_stateid.data));
4296 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4297 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
4300 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4303 static void nfs4_lock_release(void *calldata)
4305 struct nfs4_lockdata *data = calldata;
4307 dprintk("%s: begin!\n", __func__);
4308 nfs_free_seqid(data->arg.open_seqid);
4309 if (data->cancelled != 0) {
4310 struct rpc_task *task;
4311 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4312 data->arg.lock_seqid);
4314 rpc_put_task_async(task);
4315 dprintk("%s: cancelling lock!\n", __func__);
4317 nfs_free_seqid(data->arg.lock_seqid);
4318 nfs4_put_lock_state(data->lsp);
4319 put_nfs_open_context(data->ctx);
4321 dprintk("%s: done!\n", __func__);
4324 static const struct rpc_call_ops nfs4_lock_ops = {
4325 .rpc_call_prepare = nfs4_lock_prepare,
4326 .rpc_call_done = nfs4_lock_done,
4327 .rpc_release = nfs4_lock_release,
4330 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4331 .rpc_call_prepare = nfs4_recover_lock_prepare,
4332 .rpc_call_done = nfs4_lock_done,
4333 .rpc_release = nfs4_lock_release,
4336 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4339 case -NFS4ERR_ADMIN_REVOKED:
4340 case -NFS4ERR_BAD_STATEID:
4341 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4342 if (new_lock_owner != 0 ||
4343 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4344 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4346 case -NFS4ERR_STALE_STATEID:
4347 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4348 case -NFS4ERR_EXPIRED:
4349 nfs4_schedule_lease_recovery(server->nfs_client);
4353 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4355 struct nfs4_lockdata *data;
4356 struct rpc_task *task;
4357 struct rpc_message msg = {
4358 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4359 .rpc_cred = state->owner->so_cred,
4361 struct rpc_task_setup task_setup_data = {
4362 .rpc_client = NFS_CLIENT(state->inode),
4363 .rpc_message = &msg,
4364 .callback_ops = &nfs4_lock_ops,
4365 .workqueue = nfsiod_workqueue,
4366 .flags = RPC_TASK_ASYNC,
4370 dprintk("%s: begin!\n", __func__);
4371 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4372 fl->fl_u.nfs4_fl.owner,
4373 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4377 data->arg.block = 1;
4378 if (recovery_type > NFS_LOCK_NEW) {
4379 if (recovery_type == NFS_LOCK_RECLAIM)
4380 data->arg.reclaim = NFS_LOCK_RECLAIM;
4381 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4383 msg.rpc_argp = &data->arg;
4384 msg.rpc_resp = &data->res;
4385 task_setup_data.callback_data = data;
4386 task = rpc_run_task(&task_setup_data);
4388 return PTR_ERR(task);
4389 ret = nfs4_wait_for_completion_rpc_task(task);
4391 ret = data->rpc_status;
4393 nfs4_handle_setlk_error(data->server, data->lsp,
4394 data->arg.new_lock_owner, ret);
4396 data->cancelled = 1;
4398 dprintk("%s: done, ret = %d!\n", __func__, ret);
4402 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4404 struct nfs_server *server = NFS_SERVER(state->inode);
4405 struct nfs4_exception exception = { };
4409 /* Cache the lock if possible... */
4410 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4412 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4413 if (err != -NFS4ERR_DELAY)
4415 nfs4_handle_exception(server, err, &exception);
4416 } while (exception.retry);
4420 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4422 struct nfs_server *server = NFS_SERVER(state->inode);
4423 struct nfs4_exception exception = { };
4426 err = nfs4_set_lock_state(state, request);
4430 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4432 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4436 case -NFS4ERR_GRACE:
4437 case -NFS4ERR_DELAY:
4438 nfs4_handle_exception(server, err, &exception);
4441 } while (exception.retry);
4446 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4448 struct nfs_inode *nfsi = NFS_I(state->inode);
4449 unsigned char fl_flags = request->fl_flags;
4450 int status = -ENOLCK;
4452 if ((fl_flags & FL_POSIX) &&
4453 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4455 /* Is this a delegated open? */
4456 status = nfs4_set_lock_state(state, request);
4459 request->fl_flags |= FL_ACCESS;
4460 status = do_vfs_lock(request->fl_file, request);
4463 down_read(&nfsi->rwsem);
4464 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4465 /* Yes: cache locks! */
4466 /* ...but avoid races with delegation recall... */
4467 request->fl_flags = fl_flags & ~FL_SLEEP;
4468 status = do_vfs_lock(request->fl_file, request);
4471 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4474 /* Note: we always want to sleep here! */
4475 request->fl_flags = fl_flags | FL_SLEEP;
4476 if (do_vfs_lock(request->fl_file, request) < 0)
4477 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4479 up_read(&nfsi->rwsem);
4481 request->fl_flags = fl_flags;
4485 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4487 struct nfs4_exception exception = { };
4491 err = _nfs4_proc_setlk(state, cmd, request);
4492 if (err == -NFS4ERR_DENIED)
4494 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4496 } while (exception.retry);
4501 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4503 struct nfs_open_context *ctx;
4504 struct nfs4_state *state;
4505 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4508 /* verify open state */
4509 ctx = nfs_file_open_context(filp);
4512 if (request->fl_start < 0 || request->fl_end < 0)
4515 if (IS_GETLK(cmd)) {
4517 return nfs4_proc_getlk(state, F_GETLK, request);
4521 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4524 if (request->fl_type == F_UNLCK) {
4526 return nfs4_proc_unlck(state, cmd, request);
4533 status = nfs4_proc_setlk(state, cmd, request);
4534 if ((status != -EAGAIN) || IS_SETLK(cmd))
4536 timeout = nfs4_set_lock_task_retry(timeout);
4537 status = -ERESTARTSYS;
4540 } while(status < 0);
4544 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4546 struct nfs_server *server = NFS_SERVER(state->inode);
4547 struct nfs4_exception exception = { };
4550 err = nfs4_set_lock_state(state, fl);
4554 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4557 printk(KERN_ERR "%s: unhandled error %d.\n",
4562 case -NFS4ERR_EXPIRED:
4563 nfs4_schedule_stateid_recovery(server, state);
4564 case -NFS4ERR_STALE_CLIENTID:
4565 case -NFS4ERR_STALE_STATEID:
4566 nfs4_schedule_lease_recovery(server->nfs_client);
4568 case -NFS4ERR_BADSESSION:
4569 case -NFS4ERR_BADSLOT:
4570 case -NFS4ERR_BAD_HIGH_SLOT:
4571 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4572 case -NFS4ERR_DEADSESSION:
4573 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
4577 * The show must go on: exit, but mark the
4578 * stateid as needing recovery.
4580 case -NFS4ERR_ADMIN_REVOKED:
4581 case -NFS4ERR_BAD_STATEID:
4582 case -NFS4ERR_OPENMODE:
4583 nfs4_schedule_stateid_recovery(server, state);
4588 * User RPCSEC_GSS context has expired.
4589 * We cannot recover this stateid now, so
4590 * skip it and allow recovery thread to
4596 case -NFS4ERR_DENIED:
4597 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4600 case -NFS4ERR_DELAY:
4603 err = nfs4_handle_exception(server, err, &exception);
4604 } while (exception.retry);
4609 static void nfs4_release_lockowner_release(void *calldata)
4614 const struct rpc_call_ops nfs4_release_lockowner_ops = {
4615 .rpc_release = nfs4_release_lockowner_release,
4618 void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4620 struct nfs_server *server = lsp->ls_state->owner->so_server;
4621 struct nfs_release_lockowner_args *args;
4622 struct rpc_message msg = {
4623 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4626 if (server->nfs_client->cl_mvops->minor_version != 0)
4628 args = kmalloc(sizeof(*args), GFP_NOFS);
4631 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4632 args->lock_owner.id = lsp->ls_id.id;
4633 args->lock_owner.s_dev = server->s_dev;
4634 msg.rpc_argp = args;
4635 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4638 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4640 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4641 const void *buf, size_t buflen,
4642 int flags, int type)
4644 if (strcmp(key, "") != 0)
4647 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
4650 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4651 void *buf, size_t buflen, int type)
4653 if (strcmp(key, "") != 0)
4656 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
4659 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4660 size_t list_len, const char *name,
4661 size_t name_len, int type)
4663 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
4665 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4668 if (list && len <= list_len)
4669 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
4674 * nfs_fhget will use either the mounted_on_fileid or the fileid
4676 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4678 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4679 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4680 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4681 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4684 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4685 NFS_ATTR_FATTR_NLINK;
4686 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4690 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4691 struct nfs4_fs_locations *fs_locations, struct page *page)
4693 struct nfs_server *server = NFS_SERVER(dir);
4695 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4697 struct nfs4_fs_locations_arg args = {
4698 .dir_fh = NFS_FH(dir),
4703 struct nfs4_fs_locations_res res = {
4704 .fs_locations = fs_locations,
4706 struct rpc_message msg = {
4707 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4713 dprintk("%s: start\n", __func__);
4715 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4716 * is not supported */
4717 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4718 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4720 bitmask[0] |= FATTR4_WORD0_FILEID;
4722 nfs_fattr_init(&fs_locations->fattr);
4723 fs_locations->server = server;
4724 fs_locations->nlocations = 0;
4725 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4726 dprintk("%s: returned status = %d\n", __func__, status);
4730 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4733 struct nfs4_secinfo_arg args = {
4734 .dir_fh = NFS_FH(dir),
4737 struct nfs4_secinfo_res res = {
4740 struct rpc_message msg = {
4741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4746 dprintk("NFS call secinfo %s\n", name->name);
4747 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4748 dprintk("NFS reply secinfo: %d\n", status);
4752 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4754 struct nfs4_exception exception = { };
4757 err = nfs4_handle_exception(NFS_SERVER(dir),
4758 _nfs4_proc_secinfo(dir, name, flavors),
4760 } while (exception.retry);
4764 #ifdef CONFIG_NFS_V4_1
4766 * Check the exchange flags returned by the server for invalid flags, having
4767 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4770 static int nfs4_check_cl_exchange_flags(u32 flags)
4772 if (flags & ~EXCHGID4_FLAG_MASK_R)
4774 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4775 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4777 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4781 return -NFS4ERR_INVAL;
4785 * nfs4_proc_exchange_id()
4787 * Since the clientid has expired, all compounds using sessions
4788 * associated with the stale clientid will be returning
4789 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4790 * be in some phase of session reset.
4792 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4794 nfs4_verifier verifier;
4795 struct nfs41_exchange_id_args args = {
4797 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
4799 struct nfs41_exchange_id_res res = {
4803 struct rpc_message msg = {
4804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4811 dprintk("--> %s\n", __func__);
4812 BUG_ON(clp == NULL);
4814 p = (u32 *)verifier.data;
4815 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4816 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4817 args.verifier = &verifier;
4819 args.id_len = scnprintf(args.id, sizeof(args.id),
4822 init_utsname()->nodename,
4823 init_utsname()->domainname,
4824 clp->cl_rpcclient->cl_auth->au_flavor);
4826 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4828 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
4829 dprintk("<-- %s status= %d\n", __func__, status);
4833 struct nfs4_get_lease_time_data {
4834 struct nfs4_get_lease_time_args *args;
4835 struct nfs4_get_lease_time_res *res;
4836 struct nfs_client *clp;
4839 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4843 struct nfs4_get_lease_time_data *data =
4844 (struct nfs4_get_lease_time_data *)calldata;
4846 dprintk("--> %s\n", __func__);
4847 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4848 /* just setup sequence, do not trigger session recovery
4849 since we're invoked within one */
4850 ret = nfs41_setup_sequence(data->clp->cl_session,
4851 &data->args->la_seq_args,
4852 &data->res->lr_seq_res, 0, task);
4854 BUG_ON(ret == -EAGAIN);
4855 rpc_call_start(task);
4856 dprintk("<-- %s\n", __func__);
4860 * Called from nfs4_state_manager thread for session setup, so don't recover
4861 * from sequence operation or clientid errors.
4863 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4865 struct nfs4_get_lease_time_data *data =
4866 (struct nfs4_get_lease_time_data *)calldata;
4868 dprintk("--> %s\n", __func__);
4869 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4871 switch (task->tk_status) {
4872 case -NFS4ERR_DELAY:
4873 case -NFS4ERR_GRACE:
4874 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4875 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4876 task->tk_status = 0;
4878 case -NFS4ERR_RETRY_UNCACHED_REP:
4879 nfs_restart_rpc(task, data->clp);
4882 dprintk("<-- %s\n", __func__);
4885 struct rpc_call_ops nfs4_get_lease_time_ops = {
4886 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4887 .rpc_call_done = nfs4_get_lease_time_done,
4890 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4892 struct rpc_task *task;
4893 struct nfs4_get_lease_time_args args;
4894 struct nfs4_get_lease_time_res res = {
4895 .lr_fsinfo = fsinfo,
4897 struct nfs4_get_lease_time_data data = {
4902 struct rpc_message msg = {
4903 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4907 struct rpc_task_setup task_setup = {
4908 .rpc_client = clp->cl_rpcclient,
4909 .rpc_message = &msg,
4910 .callback_ops = &nfs4_get_lease_time_ops,
4911 .callback_data = &data,
4912 .flags = RPC_TASK_TIMEOUT,
4916 dprintk("--> %s\n", __func__);
4917 task = rpc_run_task(&task_setup);
4920 status = PTR_ERR(task);
4922 status = task->tk_status;
4925 dprintk("<-- %s return %d\n", __func__, status);
4931 * Reset a slot table
4933 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4936 struct nfs4_slot *new = NULL;
4940 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4941 max_reqs, tbl->max_slots);
4943 /* Does the newly negotiated max_reqs match the existing slot table? */
4944 if (max_reqs != tbl->max_slots) {
4946 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
4953 spin_lock(&tbl->slot_tbl_lock);
4956 tbl->max_slots = max_reqs;
4958 for (i = 0; i < tbl->max_slots; ++i)
4959 tbl->slots[i].seq_nr = ivalue;
4960 spin_unlock(&tbl->slot_tbl_lock);
4961 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4962 tbl, tbl->slots, tbl->max_slots);
4964 dprintk("<-- %s: return %d\n", __func__, ret);
4969 * Reset the forechannel and backchannel slot tables
4971 static int nfs4_reset_slot_tables(struct nfs4_session *session)
4975 status = nfs4_reset_slot_table(&session->fc_slot_table,
4976 session->fc_attrs.max_reqs, 1);
4980 status = nfs4_reset_slot_table(&session->bc_slot_table,
4981 session->bc_attrs.max_reqs, 0);
4985 /* Destroy the slot table */
4986 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4988 if (session->fc_slot_table.slots != NULL) {
4989 kfree(session->fc_slot_table.slots);
4990 session->fc_slot_table.slots = NULL;
4992 if (session->bc_slot_table.slots != NULL) {
4993 kfree(session->bc_slot_table.slots);
4994 session->bc_slot_table.slots = NULL;
5000 * Initialize slot table
5002 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5003 int max_slots, int ivalue)
5005 struct nfs4_slot *slot;
5008 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5010 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
5012 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
5017 spin_lock(&tbl->slot_tbl_lock);
5018 tbl->max_slots = max_slots;
5020 tbl->highest_used_slotid = -1; /* no slot is currently used */
5021 spin_unlock(&tbl->slot_tbl_lock);
5022 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5023 tbl, tbl->slots, tbl->max_slots);
5025 dprintk("<-- %s: return %d\n", __func__, ret);
5030 * Initialize the forechannel and backchannel tables
5032 static int nfs4_init_slot_tables(struct nfs4_session *session)
5034 struct nfs4_slot_table *tbl;
5037 tbl = &session->fc_slot_table;
5038 if (tbl->slots == NULL) {
5039 status = nfs4_init_slot_table(tbl,
5040 session->fc_attrs.max_reqs, 1);
5045 tbl = &session->bc_slot_table;
5046 if (tbl->slots == NULL) {
5047 status = nfs4_init_slot_table(tbl,
5048 session->bc_attrs.max_reqs, 0);
5050 nfs4_destroy_slot_tables(session);
5056 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5058 struct nfs4_session *session;
5059 struct nfs4_slot_table *tbl;
5061 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5065 tbl = &session->fc_slot_table;
5066 tbl->highest_used_slotid = -1;
5067 spin_lock_init(&tbl->slot_tbl_lock);
5068 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5069 init_completion(&tbl->complete);
5071 tbl = &session->bc_slot_table;
5072 tbl->highest_used_slotid = -1;
5073 spin_lock_init(&tbl->slot_tbl_lock);
5074 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5075 init_completion(&tbl->complete);
5077 session->session_state = 1<<NFS4_SESSION_INITING;
5083 void nfs4_destroy_session(struct nfs4_session *session)
5085 nfs4_proc_destroy_session(session);
5086 dprintk("%s Destroy backchannel for xprt %p\n",
5087 __func__, session->clp->cl_rpcclient->cl_xprt);
5088 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5089 NFS41_BC_MIN_CALLBACKS);
5090 nfs4_destroy_slot_tables(session);
5095 * Initialize the values to be used by the client in CREATE_SESSION
5096 * If nfs4_init_session set the fore channel request and response sizes,
5099 * Set the back channel max_resp_sz_cached to zero to force the client to
5100 * always set csa_cachethis to FALSE because the current implementation
5101 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5103 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5105 struct nfs4_session *session = args->client->cl_session;
5106 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5107 mxresp_sz = session->fc_attrs.max_resp_sz;
5110 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5112 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5113 /* Fore channel attributes */
5114 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5115 args->fc_attrs.max_resp_sz = mxresp_sz;
5116 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5117 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5119 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5120 "max_ops=%u max_reqs=%u\n",
5122 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5123 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5125 /* Back channel attributes */
5126 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5127 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5128 args->bc_attrs.max_resp_sz_cached = 0;
5129 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5130 args->bc_attrs.max_reqs = 1;
5132 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5133 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5135 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5136 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5137 args->bc_attrs.max_reqs);
5140 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5142 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5143 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5145 if (rcvd->max_resp_sz > sent->max_resp_sz)
5148 * Our requested max_ops is the minimum we need; we're not
5149 * prepared to break up compounds into smaller pieces than that.
5150 * So, no point even trying to continue if the server won't
5153 if (rcvd->max_ops < sent->max_ops)
5155 if (rcvd->max_reqs == 0)
5160 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5162 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5163 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5165 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5167 if (rcvd->max_resp_sz < sent->max_resp_sz)
5169 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5171 /* These would render the backchannel useless: */
5172 if (rcvd->max_ops == 0)
5174 if (rcvd->max_reqs == 0)
5179 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5180 struct nfs4_session *session)
5184 ret = nfs4_verify_fore_channel_attrs(args, session);
5187 return nfs4_verify_back_channel_attrs(args, session);
5190 static int _nfs4_proc_create_session(struct nfs_client *clp)
5192 struct nfs4_session *session = clp->cl_session;
5193 struct nfs41_create_session_args args = {
5195 .cb_program = NFS4_CALLBACK,
5197 struct nfs41_create_session_res res = {
5200 struct rpc_message msg = {
5201 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5207 nfs4_init_channel_attrs(&args);
5208 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5210 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5213 /* Verify the session's negotiated channel_attrs values */
5214 status = nfs4_verify_channel_attrs(&args, session);
5216 /* Increment the clientid slot sequence id */
5224 * Issues a CREATE_SESSION operation to the server.
5225 * It is the responsibility of the caller to verify the session is
5226 * expired before calling this routine.
5228 int nfs4_proc_create_session(struct nfs_client *clp)
5232 struct nfs4_session *session = clp->cl_session;
5234 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5236 status = _nfs4_proc_create_session(clp);
5240 /* Init and reset the fore channel */
5241 status = nfs4_init_slot_tables(session);
5242 dprintk("slot table initialization returned %d\n", status);
5245 status = nfs4_reset_slot_tables(session);
5246 dprintk("slot table reset returned %d\n", status);
5250 ptr = (unsigned *)&session->sess_id.data[0];
5251 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5252 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5254 dprintk("<-- %s\n", __func__);
5259 * Issue the over-the-wire RPC DESTROY_SESSION.
5260 * The caller must serialize access to this routine.
5262 int nfs4_proc_destroy_session(struct nfs4_session *session)
5265 struct rpc_message msg;
5267 dprintk("--> nfs4_proc_destroy_session\n");
5269 /* session is still being setup */
5270 if (session->clp->cl_cons_state != NFS_CS_READY)
5273 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5274 msg.rpc_argp = session;
5275 msg.rpc_resp = NULL;
5276 msg.rpc_cred = NULL;
5277 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5281 "Got error %d from the server on DESTROY_SESSION. "
5282 "Session has been destroyed regardless...\n", status);
5284 dprintk("<-- nfs4_proc_destroy_session\n");
5288 int nfs4_init_session(struct nfs_server *server)
5290 struct nfs_client *clp = server->nfs_client;
5291 struct nfs4_session *session;
5292 unsigned int rsize, wsize;
5295 if (!nfs4_has_session(clp))
5298 session = clp->cl_session;
5299 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5302 rsize = server->rsize;
5304 rsize = NFS_MAX_FILE_IO_SIZE;
5305 wsize = server->wsize;
5307 wsize = NFS_MAX_FILE_IO_SIZE;
5309 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5310 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5312 ret = nfs4_recover_expired_lease(server);
5314 ret = nfs4_check_client_ready(clp);
5318 int nfs4_init_ds_session(struct nfs_client *clp)
5320 struct nfs4_session *session = clp->cl_session;
5323 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5326 ret = nfs4_client_recover_expired_lease(clp);
5328 /* Test for the DS role */
5329 if (!is_ds_client(clp))
5332 ret = nfs4_check_client_ready(clp);
5336 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5340 * Renew the cl_session lease.
5342 struct nfs4_sequence_data {
5343 struct nfs_client *clp;
5344 struct nfs4_sequence_args args;
5345 struct nfs4_sequence_res res;
5348 static void nfs41_sequence_release(void *data)
5350 struct nfs4_sequence_data *calldata = data;
5351 struct nfs_client *clp = calldata->clp;
5353 if (atomic_read(&clp->cl_count) > 1)
5354 nfs4_schedule_state_renewal(clp);
5355 nfs_put_client(clp);
5359 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5361 switch(task->tk_status) {
5362 case -NFS4ERR_DELAY:
5363 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5366 nfs4_schedule_lease_recovery(clp);
5371 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5373 struct nfs4_sequence_data *calldata = data;
5374 struct nfs_client *clp = calldata->clp;
5376 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5379 if (task->tk_status < 0) {
5380 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5381 if (atomic_read(&clp->cl_count) == 1)
5384 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5385 rpc_restart_call_prepare(task);
5389 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5391 dprintk("<-- %s\n", __func__);
5394 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5396 struct nfs4_sequence_data *calldata = data;
5397 struct nfs_client *clp = calldata->clp;
5398 struct nfs4_sequence_args *args;
5399 struct nfs4_sequence_res *res;
5401 args = task->tk_msg.rpc_argp;
5402 res = task->tk_msg.rpc_resp;
5404 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
5406 rpc_call_start(task);
5409 static const struct rpc_call_ops nfs41_sequence_ops = {
5410 .rpc_call_done = nfs41_sequence_call_done,
5411 .rpc_call_prepare = nfs41_sequence_prepare,
5412 .rpc_release = nfs41_sequence_release,
5415 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5417 struct nfs4_sequence_data *calldata;
5418 struct rpc_message msg = {
5419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5422 struct rpc_task_setup task_setup_data = {
5423 .rpc_client = clp->cl_rpcclient,
5424 .rpc_message = &msg,
5425 .callback_ops = &nfs41_sequence_ops,
5426 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5429 if (!atomic_inc_not_zero(&clp->cl_count))
5430 return ERR_PTR(-EIO);
5431 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5432 if (calldata == NULL) {
5433 nfs_put_client(clp);
5434 return ERR_PTR(-ENOMEM);
5436 msg.rpc_argp = &calldata->args;
5437 msg.rpc_resp = &calldata->res;
5438 calldata->clp = clp;
5439 task_setup_data.callback_data = calldata;
5441 return rpc_run_task(&task_setup_data);
5444 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5446 struct rpc_task *task;
5449 task = _nfs41_proc_sequence(clp, cred);
5451 ret = PTR_ERR(task);
5453 rpc_put_task_async(task);
5454 dprintk("<-- %s status=%d\n", __func__, ret);
5458 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5460 struct rpc_task *task;
5463 task = _nfs41_proc_sequence(clp, cred);
5465 ret = PTR_ERR(task);
5468 ret = rpc_wait_for_completion_task(task);
5470 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5472 if (task->tk_status == 0)
5473 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5474 ret = task->tk_status;
5478 dprintk("<-- %s status=%d\n", __func__, ret);
5482 struct nfs4_reclaim_complete_data {
5483 struct nfs_client *clp;
5484 struct nfs41_reclaim_complete_args arg;
5485 struct nfs41_reclaim_complete_res res;
5488 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5490 struct nfs4_reclaim_complete_data *calldata = data;
5492 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5493 if (nfs41_setup_sequence(calldata->clp->cl_session,
5494 &calldata->arg.seq_args,
5495 &calldata->res.seq_res, 0, task))
5498 rpc_call_start(task);
5501 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5503 switch(task->tk_status) {
5505 case -NFS4ERR_COMPLETE_ALREADY:
5506 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5508 case -NFS4ERR_DELAY:
5509 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5511 case -NFS4ERR_RETRY_UNCACHED_REP:
5514 nfs4_schedule_lease_recovery(clp);
5519 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5521 struct nfs4_reclaim_complete_data *calldata = data;
5522 struct nfs_client *clp = calldata->clp;
5523 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5525 dprintk("--> %s\n", __func__);
5526 if (!nfs41_sequence_done(task, res))
5529 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5530 rpc_restart_call_prepare(task);
5533 dprintk("<-- %s\n", __func__);
5536 static void nfs4_free_reclaim_complete_data(void *data)
5538 struct nfs4_reclaim_complete_data *calldata = data;
5543 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5544 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5545 .rpc_call_done = nfs4_reclaim_complete_done,
5546 .rpc_release = nfs4_free_reclaim_complete_data,
5550 * Issue a global reclaim complete.
5552 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5554 struct nfs4_reclaim_complete_data *calldata;
5555 struct rpc_task *task;
5556 struct rpc_message msg = {
5557 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5559 struct rpc_task_setup task_setup_data = {
5560 .rpc_client = clp->cl_rpcclient,
5561 .rpc_message = &msg,
5562 .callback_ops = &nfs4_reclaim_complete_call_ops,
5563 .flags = RPC_TASK_ASYNC,
5565 int status = -ENOMEM;
5567 dprintk("--> %s\n", __func__);
5568 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5569 if (calldata == NULL)
5571 calldata->clp = clp;
5572 calldata->arg.one_fs = 0;
5574 msg.rpc_argp = &calldata->arg;
5575 msg.rpc_resp = &calldata->res;
5576 task_setup_data.callback_data = calldata;
5577 task = rpc_run_task(&task_setup_data);
5579 status = PTR_ERR(task);
5582 status = nfs4_wait_for_completion_rpc_task(task);
5584 status = task->tk_status;
5588 dprintk("<-- %s status=%d\n", __func__, status);
5593 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5595 struct nfs4_layoutget *lgp = calldata;
5596 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5598 dprintk("--> %s\n", __func__);
5599 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5600 * right now covering the LAYOUTGET we are about to send.
5601 * However, that is not so catastrophic, and there seems
5602 * to be no way to prevent it completely.
5604 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5605 &lgp->res.seq_res, 0, task))
5607 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5608 NFS_I(lgp->args.inode)->layout,
5609 lgp->args.ctx->state)) {
5610 rpc_exit(task, NFS4_OK);
5613 rpc_call_start(task);
5616 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5618 struct nfs4_layoutget *lgp = calldata;
5619 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5621 dprintk("--> %s\n", __func__);
5623 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5626 switch (task->tk_status) {
5629 case -NFS4ERR_LAYOUTTRYLATER:
5630 case -NFS4ERR_RECALLCONFLICT:
5631 task->tk_status = -NFS4ERR_DELAY;
5634 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5635 rpc_restart_call_prepare(task);
5639 dprintk("<-- %s\n", __func__);
5642 static void nfs4_layoutget_release(void *calldata)
5644 struct nfs4_layoutget *lgp = calldata;
5646 dprintk("--> %s\n", __func__);
5647 put_nfs_open_context(lgp->args.ctx);
5649 dprintk("<-- %s\n", __func__);
5652 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5653 .rpc_call_prepare = nfs4_layoutget_prepare,
5654 .rpc_call_done = nfs4_layoutget_done,
5655 .rpc_release = nfs4_layoutget_release,
5658 int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5660 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5661 struct rpc_task *task;
5662 struct rpc_message msg = {
5663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5664 .rpc_argp = &lgp->args,
5665 .rpc_resp = &lgp->res,
5667 struct rpc_task_setup task_setup_data = {
5668 .rpc_client = server->client,
5669 .rpc_message = &msg,
5670 .callback_ops = &nfs4_layoutget_call_ops,
5671 .callback_data = lgp,
5672 .flags = RPC_TASK_ASYNC,
5676 dprintk("--> %s\n", __func__);
5678 lgp->res.layoutp = &lgp->args.layout;
5679 lgp->res.seq_res.sr_slot = NULL;
5680 task = rpc_run_task(&task_setup_data);
5682 return PTR_ERR(task);
5683 status = nfs4_wait_for_completion_rpc_task(task);
5685 status = task->tk_status;
5687 status = pnfs_layout_process(lgp);
5689 dprintk("<-- %s status=%d\n", __func__, status);
5694 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5696 struct nfs4_layoutreturn *lrp = calldata;
5698 dprintk("--> %s\n", __func__);
5699 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5700 &lrp->res.seq_res, 0, task))
5702 rpc_call_start(task);
5705 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5707 struct nfs4_layoutreturn *lrp = calldata;
5708 struct nfs_server *server;
5709 struct pnfs_layout_hdr *lo = NFS_I(lrp->args.inode)->layout;
5711 dprintk("--> %s\n", __func__);
5713 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5716 server = NFS_SERVER(lrp->args.inode);
5717 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5718 nfs_restart_rpc(task, lrp->clp);
5721 spin_lock(&lo->plh_inode->i_lock);
5722 if (task->tk_status == 0) {
5723 if (lrp->res.lrs_present) {
5724 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
5726 BUG_ON(!list_empty(&lo->plh_segs));
5728 lo->plh_block_lgets--;
5729 spin_unlock(&lo->plh_inode->i_lock);
5730 dprintk("<-- %s\n", __func__);
5733 static void nfs4_layoutreturn_release(void *calldata)
5735 struct nfs4_layoutreturn *lrp = calldata;
5737 dprintk("--> %s\n", __func__);
5738 put_layout_hdr(NFS_I(lrp->args.inode)->layout);
5740 dprintk("<-- %s\n", __func__);
5743 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5744 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5745 .rpc_call_done = nfs4_layoutreturn_done,
5746 .rpc_release = nfs4_layoutreturn_release,
5749 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5751 struct rpc_task *task;
5752 struct rpc_message msg = {
5753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5754 .rpc_argp = &lrp->args,
5755 .rpc_resp = &lrp->res,
5757 struct rpc_task_setup task_setup_data = {
5758 .rpc_client = lrp->clp->cl_rpcclient,
5759 .rpc_message = &msg,
5760 .callback_ops = &nfs4_layoutreturn_call_ops,
5761 .callback_data = lrp,
5765 dprintk("--> %s\n", __func__);
5766 task = rpc_run_task(&task_setup_data);
5768 return PTR_ERR(task);
5769 status = task->tk_status;
5770 dprintk("<-- %s status=%d\n", __func__, status);
5776 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5778 struct nfs4_getdeviceinfo_args args = {
5781 struct nfs4_getdeviceinfo_res res = {
5784 struct rpc_message msg = {
5785 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5791 dprintk("--> %s\n", __func__);
5792 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5793 dprintk("<-- %s status=%d\n", __func__, status);
5798 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5800 struct nfs4_exception exception = { };
5804 err = nfs4_handle_exception(server,
5805 _nfs4_proc_getdeviceinfo(server, pdev),
5807 } while (exception.retry);
5810 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5812 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5814 struct nfs4_layoutcommit_data *data = calldata;
5815 struct nfs_server *server = NFS_SERVER(data->args.inode);
5817 if (nfs4_setup_sequence(server, &data->args.seq_args,
5818 &data->res.seq_res, 1, task))
5820 rpc_call_start(task);
5824 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5826 struct nfs4_layoutcommit_data *data = calldata;
5827 struct nfs_server *server = NFS_SERVER(data->args.inode);
5829 if (!nfs4_sequence_done(task, &data->res.seq_res))
5832 switch (task->tk_status) { /* Just ignore these failures */
5833 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5834 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5835 case NFS4ERR_BADLAYOUT: /* no layout */
5836 case NFS4ERR_GRACE: /* loca_recalim always false */
5837 task->tk_status = 0;
5840 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5841 nfs_restart_rpc(task, server->nfs_client);
5845 if (task->tk_status == 0)
5846 nfs_post_op_update_inode_force_wcc(data->args.inode,
5850 static void nfs4_layoutcommit_release(void *calldata)
5852 struct nfs4_layoutcommit_data *data = calldata;
5854 /* Matched by references in pnfs_set_layoutcommit */
5855 put_lseg(data->lseg);
5856 put_rpccred(data->cred);
5860 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
5861 .rpc_call_prepare = nfs4_layoutcommit_prepare,
5862 .rpc_call_done = nfs4_layoutcommit_done,
5863 .rpc_release = nfs4_layoutcommit_release,
5867 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
5869 struct rpc_message msg = {
5870 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
5871 .rpc_argp = &data->args,
5872 .rpc_resp = &data->res,
5873 .rpc_cred = data->cred,
5875 struct rpc_task_setup task_setup_data = {
5876 .task = &data->task,
5877 .rpc_client = NFS_CLIENT(data->args.inode),
5878 .rpc_message = &msg,
5879 .callback_ops = &nfs4_layoutcommit_ops,
5880 .callback_data = data,
5881 .flags = RPC_TASK_ASYNC,
5883 struct rpc_task *task;
5886 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
5887 "lbw: %llu inode %lu\n",
5888 data->task.tk_pid, sync,
5889 data->args.lastbytewritten,
5890 data->args.inode->i_ino);
5892 task = rpc_run_task(&task_setup_data);
5894 return PTR_ERR(task);
5897 status = nfs4_wait_for_completion_rpc_task(task);
5900 status = task->tk_status;
5902 dprintk("%s: status %d\n", __func__, status);
5906 #endif /* CONFIG_NFS_V4_1 */
5908 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
5909 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5910 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5911 .recover_open = nfs4_open_reclaim,
5912 .recover_lock = nfs4_lock_reclaim,
5913 .establish_clid = nfs4_init_clientid,
5914 .get_clid_cred = nfs4_get_setclientid_cred,
5917 #if defined(CONFIG_NFS_V4_1)
5918 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5919 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5920 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5921 .recover_open = nfs4_open_reclaim,
5922 .recover_lock = nfs4_lock_reclaim,
5923 .establish_clid = nfs41_init_clientid,
5924 .get_clid_cred = nfs4_get_exchange_id_cred,
5925 .reclaim_complete = nfs41_proc_reclaim_complete,
5927 #endif /* CONFIG_NFS_V4_1 */
5929 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
5930 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5931 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5932 .recover_open = nfs4_open_expired,
5933 .recover_lock = nfs4_lock_expired,
5934 .establish_clid = nfs4_init_clientid,
5935 .get_clid_cred = nfs4_get_setclientid_cred,
5938 #if defined(CONFIG_NFS_V4_1)
5939 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5940 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5941 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5942 .recover_open = nfs4_open_expired,
5943 .recover_lock = nfs4_lock_expired,
5944 .establish_clid = nfs41_init_clientid,
5945 .get_clid_cred = nfs4_get_exchange_id_cred,
5947 #endif /* CONFIG_NFS_V4_1 */
5949 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5950 .sched_state_renewal = nfs4_proc_async_renew,
5951 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
5952 .renew_lease = nfs4_proc_renew,
5955 #if defined(CONFIG_NFS_V4_1)
5956 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5957 .sched_state_renewal = nfs41_proc_async_sequence,
5958 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
5959 .renew_lease = nfs4_proc_sequence,
5963 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5965 .call_sync = _nfs4_call_sync,
5966 .validate_stateid = nfs4_validate_delegation_stateid,
5967 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5968 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5969 .state_renewal_ops = &nfs40_state_renewal_ops,
5972 #if defined(CONFIG_NFS_V4_1)
5973 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5975 .call_sync = _nfs4_call_sync_session,
5976 .validate_stateid = nfs41_validate_delegation_stateid,
5977 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5978 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5979 .state_renewal_ops = &nfs41_state_renewal_ops,
5983 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5984 [0] = &nfs_v4_0_minor_ops,
5985 #if defined(CONFIG_NFS_V4_1)
5986 [1] = &nfs_v4_1_minor_ops,
5990 static const struct inode_operations nfs4_file_inode_operations = {
5991 .permission = nfs_permission,
5992 .getattr = nfs_getattr,
5993 .setattr = nfs_setattr,
5994 .getxattr = generic_getxattr,
5995 .setxattr = generic_setxattr,
5996 .listxattr = generic_listxattr,
5997 .removexattr = generic_removexattr,
6000 const struct nfs_rpc_ops nfs_v4_clientops = {
6001 .version = 4, /* protocol version */
6002 .dentry_ops = &nfs4_dentry_operations,
6003 .dir_inode_ops = &nfs4_dir_inode_operations,
6004 .file_inode_ops = &nfs4_file_inode_operations,
6005 .getroot = nfs4_proc_get_root,
6006 .getattr = nfs4_proc_getattr,
6007 .setattr = nfs4_proc_setattr,
6008 .lookupfh = nfs4_proc_lookupfh,
6009 .lookup = nfs4_proc_lookup,
6010 .access = nfs4_proc_access,
6011 .readlink = nfs4_proc_readlink,
6012 .create = nfs4_proc_create,
6013 .remove = nfs4_proc_remove,
6014 .unlink_setup = nfs4_proc_unlink_setup,
6015 .unlink_done = nfs4_proc_unlink_done,
6016 .rename = nfs4_proc_rename,
6017 .rename_setup = nfs4_proc_rename_setup,
6018 .rename_done = nfs4_proc_rename_done,
6019 .link = nfs4_proc_link,
6020 .symlink = nfs4_proc_symlink,
6021 .mkdir = nfs4_proc_mkdir,
6022 .rmdir = nfs4_proc_remove,
6023 .readdir = nfs4_proc_readdir,
6024 .mknod = nfs4_proc_mknod,
6025 .statfs = nfs4_proc_statfs,
6026 .fsinfo = nfs4_proc_fsinfo,
6027 .pathconf = nfs4_proc_pathconf,
6028 .set_capabilities = nfs4_server_capabilities,
6029 .decode_dirent = nfs4_decode_dirent,
6030 .read_setup = nfs4_proc_read_setup,
6031 .read_done = nfs4_read_done,
6032 .write_setup = nfs4_proc_write_setup,
6033 .write_done = nfs4_write_done,
6034 .commit_setup = nfs4_proc_commit_setup,
6035 .commit_done = nfs4_commit_done,
6036 .lock = nfs4_proc_lock,
6037 .clear_acl_cache = nfs4_zap_acl_attr,
6038 .close_context = nfs4_close_context,
6039 .open_context = nfs4_atomic_open,
6040 .init_client = nfs4_init_client,
6041 .secinfo = nfs4_proc_secinfo,
6044 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6045 .prefix = XATTR_NAME_NFSV4_ACL,
6046 .list = nfs4_xattr_list_nfs4_acl,
6047 .get = nfs4_xattr_get_nfs4_acl,
6048 .set = nfs4_xattr_set_nfs4_acl,
6051 const struct xattr_handler *nfs4_xattr_handlers[] = {
6052 &nfs4_xattr_nfs4_acl_handler,