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/namei.h>
50 #include <linux/mount.h>
51 #include <linux/module.h>
52 #include <linux/sunrpc/bc_xprt.h>
53 #include <linux/xattr.h>
54 #include <linux/utsname.h>
57 #include "delegation.h"
63 #define NFSDBG_FACILITY NFSDBG_PROC
65 #define NFS4_POLL_RETRY_MIN (HZ/10)
66 #define NFS4_POLL_RETRY_MAX (15*HZ)
68 #define NFS4_MAX_LOOP_ON_RECOVER (10)
71 static int _nfs4_proc_open(struct nfs4_opendata *data);
72 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
73 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
74 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
75 static int _nfs4_proc_lookup(struct rpc_clnt *client, struct inode *dir,
76 const struct qstr *name, struct nfs_fh *fhandle,
77 struct nfs_fattr *fattr);
78 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
79 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
80 struct nfs_fattr *fattr, struct iattr *sattr,
81 struct nfs4_state *state);
83 /* Prevent leaks of NFSv4 errors into userland */
84 static int nfs4_map_errors(int err)
89 case -NFS4ERR_RESOURCE:
91 case -NFS4ERR_WRONGSEC:
93 case -NFS4ERR_BADOWNER:
94 case -NFS4ERR_BADNAME:
97 dprintk("%s could not handle NFSv4 error %d\n",
105 * This is our standard bitmap for GETATTR requests.
107 const u32 nfs4_fattr_bitmap[2] = {
109 | FATTR4_WORD0_CHANGE
112 | FATTR4_WORD0_FILEID,
114 | FATTR4_WORD1_NUMLINKS
116 | FATTR4_WORD1_OWNER_GROUP
117 | FATTR4_WORD1_RAWDEV
118 | FATTR4_WORD1_SPACE_USED
119 | FATTR4_WORD1_TIME_ACCESS
120 | FATTR4_WORD1_TIME_METADATA
121 | FATTR4_WORD1_TIME_MODIFY
124 const u32 nfs4_statfs_bitmap[2] = {
125 FATTR4_WORD0_FILES_AVAIL
126 | FATTR4_WORD0_FILES_FREE
127 | FATTR4_WORD0_FILES_TOTAL,
128 FATTR4_WORD1_SPACE_AVAIL
129 | FATTR4_WORD1_SPACE_FREE
130 | FATTR4_WORD1_SPACE_TOTAL
133 const u32 nfs4_pathconf_bitmap[2] = {
135 | FATTR4_WORD0_MAXNAME,
139 const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
140 | FATTR4_WORD0_MAXREAD
141 | FATTR4_WORD0_MAXWRITE
142 | FATTR4_WORD0_LEASE_TIME,
143 FATTR4_WORD1_TIME_DELTA
144 | FATTR4_WORD1_FS_LAYOUT_TYPES
147 const u32 nfs4_fs_locations_bitmap[2] = {
149 | FATTR4_WORD0_CHANGE
152 | FATTR4_WORD0_FILEID
153 | FATTR4_WORD0_FS_LOCATIONS,
155 | FATTR4_WORD1_NUMLINKS
157 | FATTR4_WORD1_OWNER_GROUP
158 | FATTR4_WORD1_RAWDEV
159 | FATTR4_WORD1_SPACE_USED
160 | FATTR4_WORD1_TIME_ACCESS
161 | FATTR4_WORD1_TIME_METADATA
162 | FATTR4_WORD1_TIME_MODIFY
163 | FATTR4_WORD1_MOUNTED_ON_FILEID
166 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
167 struct nfs4_readdir_arg *readdir)
171 BUG_ON(readdir->count < 80);
173 readdir->cookie = cookie;
174 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
179 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
184 * NFSv4 servers do not return entries for '.' and '..'
185 * Therefore, we fake these entries here. We let '.'
186 * have cookie 0 and '..' have cookie 1. Note that
187 * when talking to the server, we always send cookie 0
190 start = p = kmap_atomic(*readdir->pages, KM_USER0);
193 *p++ = xdr_one; /* next */
194 *p++ = xdr_zero; /* cookie, first word */
195 *p++ = xdr_one; /* cookie, second word */
196 *p++ = xdr_one; /* entry len */
197 memcpy(p, ".\0\0\0", 4); /* entry */
199 *p++ = xdr_one; /* bitmap length */
200 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
201 *p++ = htonl(8); /* attribute buffer length */
202 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
205 *p++ = xdr_one; /* next */
206 *p++ = xdr_zero; /* cookie, first word */
207 *p++ = xdr_two; /* cookie, second word */
208 *p++ = xdr_two; /* entry len */
209 memcpy(p, "..\0\0", 4); /* entry */
211 *p++ = xdr_one; /* bitmap length */
212 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
213 *p++ = htonl(8); /* attribute buffer length */
214 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
216 readdir->pgbase = (char *)p - (char *)start;
217 readdir->count -= readdir->pgbase;
218 kunmap_atomic(start, KM_USER0);
221 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
227 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
228 nfs_wait_bit_killable, TASK_KILLABLE);
232 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
239 *timeout = NFS4_POLL_RETRY_MIN;
240 if (*timeout > NFS4_POLL_RETRY_MAX)
241 *timeout = NFS4_POLL_RETRY_MAX;
242 schedule_timeout_killable(*timeout);
243 if (fatal_signal_pending(current))
249 /* This is the error handling routine for processes that are allowed
252 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
254 struct nfs_client *clp = server->nfs_client;
255 struct nfs4_state *state = exception->state;
258 exception->retry = 0;
262 case -NFS4ERR_ADMIN_REVOKED:
263 case -NFS4ERR_BAD_STATEID:
264 case -NFS4ERR_OPENMODE:
267 nfs4_schedule_stateid_recovery(server, state);
268 goto wait_on_recovery;
269 case -NFS4ERR_STALE_STATEID:
270 case -NFS4ERR_STALE_CLIENTID:
271 case -NFS4ERR_EXPIRED:
272 nfs4_schedule_lease_recovery(clp);
273 goto wait_on_recovery;
274 #if defined(CONFIG_NFS_V4_1)
275 case -NFS4ERR_BADSESSION:
276 case -NFS4ERR_BADSLOT:
277 case -NFS4ERR_BAD_HIGH_SLOT:
278 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
279 case -NFS4ERR_DEADSESSION:
280 case -NFS4ERR_SEQ_FALSE_RETRY:
281 case -NFS4ERR_SEQ_MISORDERED:
282 dprintk("%s ERROR: %d Reset session\n", __func__,
284 nfs4_schedule_session_recovery(clp->cl_session);
285 exception->retry = 1;
287 #endif /* defined(CONFIG_NFS_V4_1) */
288 case -NFS4ERR_FILE_OPEN:
289 if (exception->timeout > HZ) {
290 /* We have retried a decent amount, time to
299 ret = nfs4_delay(server->client, &exception->timeout);
302 case -NFS4ERR_OLD_STATEID:
303 exception->retry = 1;
305 case -NFS4ERR_BADOWNER:
306 /* The following works around a Linux server bug! */
307 case -NFS4ERR_BADNAME:
308 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
309 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
310 exception->retry = 1;
311 printk(KERN_WARNING "NFS: v4 server %s "
312 "does not accept raw "
314 "Reenabling the idmapper.\n",
315 server->nfs_client->cl_hostname);
318 /* We failed to handle the error */
319 return nfs4_map_errors(ret);
321 ret = nfs4_wait_clnt_recover(clp);
323 exception->retry = 1;
328 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
330 spin_lock(&clp->cl_lock);
331 if (time_before(clp->cl_last_renewal,timestamp))
332 clp->cl_last_renewal = timestamp;
333 spin_unlock(&clp->cl_lock);
336 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
338 do_renew_lease(server->nfs_client, timestamp);
341 #if defined(CONFIG_NFS_V4_1)
344 * nfs4_free_slot - free a slot and efficiently update slot table.
346 * freeing a slot is trivially done by clearing its respective bit
348 * If the freed slotid equals highest_used_slotid we want to update it
349 * so that the server would be able to size down the slot table if needed,
350 * otherwise we know that the highest_used_slotid is still in use.
351 * When updating highest_used_slotid there may be "holes" in the bitmap
352 * so we need to scan down from highest_used_slotid to 0 looking for the now
353 * highest slotid in use.
354 * If none found, highest_used_slotid is set to -1.
356 * Must be called while holding tbl->slot_tbl_lock
359 nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
361 int free_slotid = free_slot - tbl->slots;
362 int slotid = free_slotid;
364 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
365 /* clear used bit in bitmap */
366 __clear_bit(slotid, tbl->used_slots);
368 /* update highest_used_slotid when it is freed */
369 if (slotid == tbl->highest_used_slotid) {
370 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
371 if (slotid < tbl->max_slots)
372 tbl->highest_used_slotid = slotid;
374 tbl->highest_used_slotid = -1;
376 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
377 free_slotid, tbl->highest_used_slotid);
381 * Signal state manager thread if session fore channel is drained
383 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
385 struct rpc_task *task;
387 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
388 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
390 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
394 if (ses->fc_slot_table.highest_used_slotid != -1)
397 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
398 complete(&ses->fc_slot_table.complete);
402 * Signal state manager thread if session back channel is drained
404 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
406 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
407 ses->bc_slot_table.highest_used_slotid != -1)
409 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
410 complete(&ses->bc_slot_table.complete);
413 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
415 struct nfs4_slot_table *tbl;
417 tbl = &res->sr_session->fc_slot_table;
419 /* just wake up the next guy waiting since
420 * we may have not consumed a slot after all */
421 dprintk("%s: No slot\n", __func__);
425 spin_lock(&tbl->slot_tbl_lock);
426 nfs4_free_slot(tbl, res->sr_slot);
427 nfs4_check_drain_fc_complete(res->sr_session);
428 spin_unlock(&tbl->slot_tbl_lock);
432 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
434 unsigned long timestamp;
435 struct nfs_client *clp;
438 * sr_status remains 1 if an RPC level error occurred. The server
439 * may or may not have processed the sequence operation..
440 * Proceed as if the server received and processed the sequence
443 if (res->sr_status == 1)
444 res->sr_status = NFS_OK;
446 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
450 /* Check the SEQUENCE operation status */
451 switch (res->sr_status) {
453 /* Update the slot's sequence and clientid lease timer */
454 ++res->sr_slot->seq_nr;
455 timestamp = res->sr_renewal_time;
456 clp = res->sr_session->clp;
457 do_renew_lease(clp, timestamp);
458 /* Check sequence flags */
459 if (res->sr_status_flags != 0)
460 nfs4_schedule_lease_recovery(clp);
463 /* The server detected a resend of the RPC call and
464 * returned NFS4ERR_DELAY as per Section 2.10.6.2
467 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
469 res->sr_slot - res->sr_session->fc_slot_table.slots,
470 res->sr_slot->seq_nr);
473 /* Just update the slot sequence no. */
474 ++res->sr_slot->seq_nr;
477 /* The session may be reset by one of the error handlers. */
478 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
479 nfs41_sequence_free_slot(res);
482 if (!rpc_restart_call(task))
484 rpc_delay(task, NFS4_POLL_RETRY_MAX);
488 static int nfs4_sequence_done(struct rpc_task *task,
489 struct nfs4_sequence_res *res)
491 if (res->sr_session == NULL)
493 return nfs41_sequence_done(task, res);
497 * nfs4_find_slot - efficiently look for a free slot
499 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
500 * If found, we mark the slot as used, update the highest_used_slotid,
501 * and respectively set up the sequence operation args.
502 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
504 * Note: must be called with under the slot_tbl_lock.
507 nfs4_find_slot(struct nfs4_slot_table *tbl)
510 u8 ret_id = NFS4_MAX_SLOT_TABLE;
511 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
513 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
514 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
516 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
517 if (slotid >= tbl->max_slots)
519 __set_bit(slotid, tbl->used_slots);
520 if (slotid > tbl->highest_used_slotid)
521 tbl->highest_used_slotid = slotid;
524 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
525 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
529 int nfs41_setup_sequence(struct nfs4_session *session,
530 struct nfs4_sequence_args *args,
531 struct nfs4_sequence_res *res,
533 struct rpc_task *task)
535 struct nfs4_slot *slot;
536 struct nfs4_slot_table *tbl;
539 dprintk("--> %s\n", __func__);
540 /* slot already allocated? */
541 if (res->sr_slot != NULL)
544 tbl = &session->fc_slot_table;
546 spin_lock(&tbl->slot_tbl_lock);
547 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
548 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
550 * The state manager will wait until the slot table is empty.
551 * Schedule the reset thread
553 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
554 spin_unlock(&tbl->slot_tbl_lock);
555 dprintk("%s Schedule Session Reset\n", __func__);
559 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
560 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
561 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
562 spin_unlock(&tbl->slot_tbl_lock);
563 dprintk("%s enforce FIFO order\n", __func__);
567 slotid = nfs4_find_slot(tbl);
568 if (slotid == NFS4_MAX_SLOT_TABLE) {
569 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
570 spin_unlock(&tbl->slot_tbl_lock);
571 dprintk("<-- %s: no free slots\n", __func__);
574 spin_unlock(&tbl->slot_tbl_lock);
576 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
577 slot = tbl->slots + slotid;
578 args->sa_session = session;
579 args->sa_slotid = slotid;
580 args->sa_cache_this = cache_reply;
582 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
584 res->sr_session = session;
586 res->sr_renewal_time = jiffies;
587 res->sr_status_flags = 0;
589 * sr_status is only set in decode_sequence, and so will remain
590 * set to 1 if an rpc level failure occurs.
595 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
597 int nfs4_setup_sequence(const struct nfs_server *server,
598 struct nfs4_sequence_args *args,
599 struct nfs4_sequence_res *res,
601 struct rpc_task *task)
603 struct nfs4_session *session = nfs4_get_session(server);
606 if (session == NULL) {
607 args->sa_session = NULL;
608 res->sr_session = NULL;
612 dprintk("--> %s clp %p session %p sr_slot %td\n",
613 __func__, session->clp, session, res->sr_slot ?
614 res->sr_slot - session->fc_slot_table.slots : -1);
616 ret = nfs41_setup_sequence(session, args, res, cache_reply,
619 dprintk("<-- %s status=%d\n", __func__, ret);
623 struct nfs41_call_sync_data {
624 const struct nfs_server *seq_server;
625 struct nfs4_sequence_args *seq_args;
626 struct nfs4_sequence_res *seq_res;
630 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
632 struct nfs41_call_sync_data *data = calldata;
634 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
636 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
637 data->seq_res, data->cache_reply, task))
639 rpc_call_start(task);
642 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
644 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
645 nfs41_call_sync_prepare(task, calldata);
648 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
650 struct nfs41_call_sync_data *data = calldata;
652 nfs41_sequence_done(task, data->seq_res);
655 struct rpc_call_ops nfs41_call_sync_ops = {
656 .rpc_call_prepare = nfs41_call_sync_prepare,
657 .rpc_call_done = nfs41_call_sync_done,
660 struct rpc_call_ops nfs41_call_priv_sync_ops = {
661 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
662 .rpc_call_done = nfs41_call_sync_done,
665 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
666 struct nfs_server *server,
667 struct rpc_message *msg,
668 struct nfs4_sequence_args *args,
669 struct nfs4_sequence_res *res,
674 struct rpc_task *task;
675 struct nfs41_call_sync_data data = {
676 .seq_server = server,
679 .cache_reply = cache_reply,
681 struct rpc_task_setup task_setup = {
684 .callback_ops = &nfs41_call_sync_ops,
685 .callback_data = &data
690 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
691 task = rpc_run_task(&task_setup);
695 ret = task->tk_status;
701 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
702 struct nfs_server *server,
703 struct rpc_message *msg,
704 struct nfs4_sequence_args *args,
705 struct nfs4_sequence_res *res,
708 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
712 static int nfs4_sequence_done(struct rpc_task *task,
713 struct nfs4_sequence_res *res)
717 #endif /* CONFIG_NFS_V4_1 */
719 int _nfs4_call_sync(struct rpc_clnt *clnt,
720 struct nfs_server *server,
721 struct rpc_message *msg,
722 struct nfs4_sequence_args *args,
723 struct nfs4_sequence_res *res,
726 args->sa_session = res->sr_session = NULL;
727 return rpc_call_sync(clnt, msg, 0);
731 int nfs4_call_sync(struct rpc_clnt *clnt,
732 struct nfs_server *server,
733 struct rpc_message *msg,
734 struct nfs4_sequence_args *args,
735 struct nfs4_sequence_res *res,
738 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
739 args, res, cache_reply);
742 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
744 struct nfs_inode *nfsi = NFS_I(dir);
746 spin_lock(&dir->i_lock);
747 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
748 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
749 nfs_force_lookup_revalidate(dir);
750 nfsi->change_attr = cinfo->after;
751 spin_unlock(&dir->i_lock);
754 struct nfs4_opendata {
756 struct nfs_openargs o_arg;
757 struct nfs_openres o_res;
758 struct nfs_open_confirmargs c_arg;
759 struct nfs_open_confirmres c_res;
760 struct nfs_fattr f_attr;
761 struct nfs_fattr dir_attr;
764 struct nfs4_state_owner *owner;
765 struct nfs4_state *state;
767 unsigned long timestamp;
768 unsigned int rpc_done : 1;
774 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
776 p->o_res.f_attr = &p->f_attr;
777 p->o_res.dir_attr = &p->dir_attr;
778 p->o_res.seqid = p->o_arg.seqid;
779 p->c_res.seqid = p->c_arg.seqid;
780 p->o_res.server = p->o_arg.server;
781 nfs_fattr_init(&p->f_attr);
782 nfs_fattr_init(&p->dir_attr);
785 static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
786 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
787 const struct iattr *attrs,
790 struct dentry *parent = dget_parent(path->dentry);
791 struct inode *dir = parent->d_inode;
792 struct nfs_server *server = NFS_SERVER(dir);
793 struct nfs4_opendata *p;
795 p = kzalloc(sizeof(*p), gfp_mask);
798 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
799 if (p->o_arg.seqid == NULL)
805 atomic_inc(&sp->so_count);
806 p->o_arg.fh = NFS_FH(dir);
807 p->o_arg.open_flags = flags;
808 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
809 p->o_arg.clientid = server->nfs_client->cl_clientid;
810 p->o_arg.id = sp->so_owner_id.id;
811 p->o_arg.name = &p->path.dentry->d_name;
812 p->o_arg.server = server;
813 p->o_arg.bitmask = server->attr_bitmask;
814 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
815 if (flags & O_CREAT) {
818 p->o_arg.u.attrs = &p->attrs;
819 memcpy(&p->attrs, attrs, sizeof(p->attrs));
820 s = (u32 *) p->o_arg.u.verifier.data;
824 p->c_arg.fh = &p->o_res.fh;
825 p->c_arg.stateid = &p->o_res.stateid;
826 p->c_arg.seqid = p->o_arg.seqid;
827 nfs4_init_opendata_res(p);
837 static void nfs4_opendata_free(struct kref *kref)
839 struct nfs4_opendata *p = container_of(kref,
840 struct nfs4_opendata, kref);
842 nfs_free_seqid(p->o_arg.seqid);
843 if (p->state != NULL)
844 nfs4_put_open_state(p->state);
845 nfs4_put_state_owner(p->owner);
851 static void nfs4_opendata_put(struct nfs4_opendata *p)
854 kref_put(&p->kref, nfs4_opendata_free);
857 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
861 ret = rpc_wait_for_completion_task(task);
865 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
869 if (open_mode & O_EXCL)
871 switch (mode & (FMODE_READ|FMODE_WRITE)) {
873 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
874 && state->n_rdonly != 0;
877 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
878 && state->n_wronly != 0;
880 case FMODE_READ|FMODE_WRITE:
881 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
882 && state->n_rdwr != 0;
888 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
890 if ((delegation->type & fmode) != fmode)
892 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
894 nfs_mark_delegation_referenced(delegation);
898 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
907 case FMODE_READ|FMODE_WRITE:
910 nfs4_state_set_mode_locked(state, state->state | fmode);
913 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
915 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
916 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
917 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
920 set_bit(NFS_O_RDONLY_STATE, &state->flags);
923 set_bit(NFS_O_WRONLY_STATE, &state->flags);
925 case FMODE_READ|FMODE_WRITE:
926 set_bit(NFS_O_RDWR_STATE, &state->flags);
930 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
932 write_seqlock(&state->seqlock);
933 nfs_set_open_stateid_locked(state, stateid, fmode);
934 write_sequnlock(&state->seqlock);
937 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
940 * Protect the call to nfs4_state_set_mode_locked and
941 * serialise the stateid update
943 write_seqlock(&state->seqlock);
944 if (deleg_stateid != NULL) {
945 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
946 set_bit(NFS_DELEGATED_STATE, &state->flags);
948 if (open_stateid != NULL)
949 nfs_set_open_stateid_locked(state, open_stateid, fmode);
950 write_sequnlock(&state->seqlock);
951 spin_lock(&state->owner->so_lock);
952 update_open_stateflags(state, fmode);
953 spin_unlock(&state->owner->so_lock);
956 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
958 struct nfs_inode *nfsi = NFS_I(state->inode);
959 struct nfs_delegation *deleg_cur;
962 fmode &= (FMODE_READ|FMODE_WRITE);
965 deleg_cur = rcu_dereference(nfsi->delegation);
966 if (deleg_cur == NULL)
969 spin_lock(&deleg_cur->lock);
970 if (nfsi->delegation != deleg_cur ||
971 (deleg_cur->type & fmode) != fmode)
972 goto no_delegation_unlock;
974 if (delegation == NULL)
975 delegation = &deleg_cur->stateid;
976 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
977 goto no_delegation_unlock;
979 nfs_mark_delegation_referenced(deleg_cur);
980 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
982 no_delegation_unlock:
983 spin_unlock(&deleg_cur->lock);
987 if (!ret && open_stateid != NULL) {
988 __update_open_stateid(state, open_stateid, NULL, fmode);
996 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
998 struct nfs_delegation *delegation;
1001 delegation = rcu_dereference(NFS_I(inode)->delegation);
1002 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1007 nfs_inode_return_delegation(inode);
1010 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1012 struct nfs4_state *state = opendata->state;
1013 struct nfs_inode *nfsi = NFS_I(state->inode);
1014 struct nfs_delegation *delegation;
1015 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1016 fmode_t fmode = opendata->o_arg.fmode;
1017 nfs4_stateid stateid;
1021 if (can_open_cached(state, fmode, open_mode)) {
1022 spin_lock(&state->owner->so_lock);
1023 if (can_open_cached(state, fmode, open_mode)) {
1024 update_open_stateflags(state, fmode);
1025 spin_unlock(&state->owner->so_lock);
1026 goto out_return_state;
1028 spin_unlock(&state->owner->so_lock);
1031 delegation = rcu_dereference(nfsi->delegation);
1032 if (delegation == NULL ||
1033 !can_open_delegated(delegation, fmode)) {
1037 /* Save the delegation */
1038 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1040 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1045 /* Try to update the stateid using the delegation */
1046 if (update_open_stateid(state, NULL, &stateid, fmode))
1047 goto out_return_state;
1050 return ERR_PTR(ret);
1052 atomic_inc(&state->count);
1056 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1058 struct inode *inode;
1059 struct nfs4_state *state = NULL;
1060 struct nfs_delegation *delegation;
1063 if (!data->rpc_done) {
1064 state = nfs4_try_open_cached(data);
1069 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1071 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1072 ret = PTR_ERR(inode);
1076 state = nfs4_get_open_state(inode, data->owner);
1079 if (data->o_res.delegation_type != 0) {
1080 int delegation_flags = 0;
1083 delegation = rcu_dereference(NFS_I(inode)->delegation);
1085 delegation_flags = delegation->flags;
1087 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1088 nfs_inode_set_delegation(state->inode,
1089 data->owner->so_cred,
1092 nfs_inode_reclaim_delegation(state->inode,
1093 data->owner->so_cred,
1097 update_open_stateid(state, &data->o_res.stateid, NULL,
1105 return ERR_PTR(ret);
1108 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1110 struct nfs_inode *nfsi = NFS_I(state->inode);
1111 struct nfs_open_context *ctx;
1113 spin_lock(&state->inode->i_lock);
1114 list_for_each_entry(ctx, &nfsi->open_files, list) {
1115 if (ctx->state != state)
1117 get_nfs_open_context(ctx);
1118 spin_unlock(&state->inode->i_lock);
1121 spin_unlock(&state->inode->i_lock);
1122 return ERR_PTR(-ENOENT);
1125 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1127 struct nfs4_opendata *opendata;
1129 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
1130 if (opendata == NULL)
1131 return ERR_PTR(-ENOMEM);
1132 opendata->state = state;
1133 atomic_inc(&state->count);
1137 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1139 struct nfs4_state *newstate;
1142 opendata->o_arg.open_flags = 0;
1143 opendata->o_arg.fmode = fmode;
1144 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1145 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1146 nfs4_init_opendata_res(opendata);
1147 ret = _nfs4_recover_proc_open(opendata);
1150 newstate = nfs4_opendata_to_nfs4_state(opendata);
1151 if (IS_ERR(newstate))
1152 return PTR_ERR(newstate);
1153 nfs4_close_state(&opendata->path, newstate, fmode);
1158 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1160 struct nfs4_state *newstate;
1163 /* memory barrier prior to reading state->n_* */
1164 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1166 if (state->n_rdwr != 0) {
1167 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1168 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1171 if (newstate != state)
1174 if (state->n_wronly != 0) {
1175 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1176 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1179 if (newstate != state)
1182 if (state->n_rdonly != 0) {
1183 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1184 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1187 if (newstate != state)
1191 * We may have performed cached opens for all three recoveries.
1192 * Check if we need to update the current stateid.
1194 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1195 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1196 write_seqlock(&state->seqlock);
1197 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1198 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1199 write_sequnlock(&state->seqlock);
1206 * reclaim state on the server after a reboot.
1208 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1210 struct nfs_delegation *delegation;
1211 struct nfs4_opendata *opendata;
1212 fmode_t delegation_type = 0;
1215 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1216 if (IS_ERR(opendata))
1217 return PTR_ERR(opendata);
1218 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1219 opendata->o_arg.fh = NFS_FH(state->inode);
1221 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1222 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1223 delegation_type = delegation->type;
1225 opendata->o_arg.u.delegation_type = delegation_type;
1226 status = nfs4_open_recover(opendata, state);
1227 nfs4_opendata_put(opendata);
1231 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1233 struct nfs_server *server = NFS_SERVER(state->inode);
1234 struct nfs4_exception exception = { };
1237 err = _nfs4_do_open_reclaim(ctx, state);
1238 if (err != -NFS4ERR_DELAY)
1240 nfs4_handle_exception(server, err, &exception);
1241 } while (exception.retry);
1245 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1247 struct nfs_open_context *ctx;
1250 ctx = nfs4_state_find_open_context(state);
1252 return PTR_ERR(ctx);
1253 ret = nfs4_do_open_reclaim(ctx, state);
1254 put_nfs_open_context(ctx);
1258 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1260 struct nfs4_opendata *opendata;
1263 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1264 if (IS_ERR(opendata))
1265 return PTR_ERR(opendata);
1266 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1267 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1268 sizeof(opendata->o_arg.u.delegation.data));
1269 ret = nfs4_open_recover(opendata, state);
1270 nfs4_opendata_put(opendata);
1274 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1276 struct nfs4_exception exception = { };
1277 struct nfs_server *server = NFS_SERVER(state->inode);
1280 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1286 case -NFS4ERR_BADSESSION:
1287 case -NFS4ERR_BADSLOT:
1288 case -NFS4ERR_BAD_HIGH_SLOT:
1289 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1290 case -NFS4ERR_DEADSESSION:
1291 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
1293 case -NFS4ERR_STALE_CLIENTID:
1294 case -NFS4ERR_STALE_STATEID:
1295 case -NFS4ERR_EXPIRED:
1296 /* Don't recall a delegation if it was lost */
1297 nfs4_schedule_lease_recovery(server->nfs_client);
1301 * The show must go on: exit, but mark the
1302 * stateid as needing recovery.
1304 case -NFS4ERR_ADMIN_REVOKED:
1305 case -NFS4ERR_BAD_STATEID:
1306 nfs4_schedule_stateid_recovery(server, state);
1309 * User RPCSEC_GSS context has expired.
1310 * We cannot recover this stateid now, so
1311 * skip it and allow recovery thread to
1318 err = nfs4_handle_exception(server, err, &exception);
1319 } while (exception.retry);
1324 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1326 struct nfs4_opendata *data = calldata;
1328 data->rpc_status = task->tk_status;
1329 if (data->rpc_status == 0) {
1330 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1331 sizeof(data->o_res.stateid.data));
1332 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1333 renew_lease(data->o_res.server, data->timestamp);
1338 static void nfs4_open_confirm_release(void *calldata)
1340 struct nfs4_opendata *data = calldata;
1341 struct nfs4_state *state = NULL;
1343 /* If this request hasn't been cancelled, do nothing */
1344 if (data->cancelled == 0)
1346 /* In case of error, no cleanup! */
1347 if (!data->rpc_done)
1349 state = nfs4_opendata_to_nfs4_state(data);
1351 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1353 nfs4_opendata_put(data);
1356 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1357 .rpc_call_done = nfs4_open_confirm_done,
1358 .rpc_release = nfs4_open_confirm_release,
1362 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1364 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1366 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1367 struct rpc_task *task;
1368 struct rpc_message msg = {
1369 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1370 .rpc_argp = &data->c_arg,
1371 .rpc_resp = &data->c_res,
1372 .rpc_cred = data->owner->so_cred,
1374 struct rpc_task_setup task_setup_data = {
1375 .rpc_client = server->client,
1376 .rpc_message = &msg,
1377 .callback_ops = &nfs4_open_confirm_ops,
1378 .callback_data = data,
1379 .workqueue = nfsiod_workqueue,
1380 .flags = RPC_TASK_ASYNC,
1384 kref_get(&data->kref);
1386 data->rpc_status = 0;
1387 data->timestamp = jiffies;
1388 task = rpc_run_task(&task_setup_data);
1390 return PTR_ERR(task);
1391 status = nfs4_wait_for_completion_rpc_task(task);
1393 data->cancelled = 1;
1396 status = data->rpc_status;
1401 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1403 struct nfs4_opendata *data = calldata;
1404 struct nfs4_state_owner *sp = data->owner;
1406 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1409 * Check if we still need to send an OPEN call, or if we can use
1410 * a delegation instead.
1412 if (data->state != NULL) {
1413 struct nfs_delegation *delegation;
1415 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1418 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1419 if (delegation != NULL &&
1420 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
1426 /* Update sequence id. */
1427 data->o_arg.id = sp->so_owner_id.id;
1428 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1429 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1430 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1431 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1433 data->timestamp = jiffies;
1434 if (nfs4_setup_sequence(data->o_arg.server,
1435 &data->o_arg.seq_args,
1436 &data->o_res.seq_res, 1, task))
1438 rpc_call_start(task);
1441 task->tk_action = NULL;
1445 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1447 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1448 nfs4_open_prepare(task, calldata);
1451 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1453 struct nfs4_opendata *data = calldata;
1455 data->rpc_status = task->tk_status;
1457 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1460 if (task->tk_status == 0) {
1461 switch (data->o_res.f_attr->mode & S_IFMT) {
1465 data->rpc_status = -ELOOP;
1468 data->rpc_status = -EISDIR;
1471 data->rpc_status = -ENOTDIR;
1473 renew_lease(data->o_res.server, data->timestamp);
1474 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1475 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1480 static void nfs4_open_release(void *calldata)
1482 struct nfs4_opendata *data = calldata;
1483 struct nfs4_state *state = NULL;
1485 /* If this request hasn't been cancelled, do nothing */
1486 if (data->cancelled == 0)
1488 /* In case of error, no cleanup! */
1489 if (data->rpc_status != 0 || !data->rpc_done)
1491 /* In case we need an open_confirm, no cleanup! */
1492 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1494 state = nfs4_opendata_to_nfs4_state(data);
1496 nfs4_close_state(&data->path, state, data->o_arg.fmode);
1498 nfs4_opendata_put(data);
1501 static const struct rpc_call_ops nfs4_open_ops = {
1502 .rpc_call_prepare = nfs4_open_prepare,
1503 .rpc_call_done = nfs4_open_done,
1504 .rpc_release = nfs4_open_release,
1507 static const struct rpc_call_ops nfs4_recover_open_ops = {
1508 .rpc_call_prepare = nfs4_recover_open_prepare,
1509 .rpc_call_done = nfs4_open_done,
1510 .rpc_release = nfs4_open_release,
1513 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1515 struct inode *dir = data->dir->d_inode;
1516 struct nfs_server *server = NFS_SERVER(dir);
1517 struct nfs_openargs *o_arg = &data->o_arg;
1518 struct nfs_openres *o_res = &data->o_res;
1519 struct rpc_task *task;
1520 struct rpc_message msg = {
1521 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1524 .rpc_cred = data->owner->so_cred,
1526 struct rpc_task_setup task_setup_data = {
1527 .rpc_client = server->client,
1528 .rpc_message = &msg,
1529 .callback_ops = &nfs4_open_ops,
1530 .callback_data = data,
1531 .workqueue = nfsiod_workqueue,
1532 .flags = RPC_TASK_ASYNC,
1536 kref_get(&data->kref);
1538 data->rpc_status = 0;
1539 data->cancelled = 0;
1541 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1542 task = rpc_run_task(&task_setup_data);
1544 return PTR_ERR(task);
1545 status = nfs4_wait_for_completion_rpc_task(task);
1547 data->cancelled = 1;
1550 status = data->rpc_status;
1556 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1558 struct inode *dir = data->dir->d_inode;
1559 struct nfs_openres *o_res = &data->o_res;
1562 status = nfs4_run_open_task(data, 1);
1563 if (status != 0 || !data->rpc_done)
1566 nfs_refresh_inode(dir, o_res->dir_attr);
1568 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1569 status = _nfs4_proc_open_confirm(data);
1578 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1580 static int _nfs4_proc_open(struct nfs4_opendata *data)
1582 struct inode *dir = data->dir->d_inode;
1583 struct nfs_server *server = NFS_SERVER(dir);
1584 struct nfs_openargs *o_arg = &data->o_arg;
1585 struct nfs_openres *o_res = &data->o_res;
1588 status = nfs4_run_open_task(data, 0);
1589 if (status != 0 || !data->rpc_done)
1592 if (o_arg->open_flags & O_CREAT) {
1593 update_changeattr(dir, &o_res->cinfo);
1594 nfs_post_op_update_inode(dir, o_res->dir_attr);
1596 nfs_refresh_inode(dir, o_res->dir_attr);
1597 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1598 server->caps &= ~NFS_CAP_POSIX_LOCK;
1599 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1600 status = _nfs4_proc_open_confirm(data);
1604 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1605 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1609 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1614 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1615 ret = nfs4_wait_clnt_recover(clp);
1618 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1619 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1621 nfs4_schedule_state_manager(clp);
1627 static int nfs4_recover_expired_lease(struct nfs_server *server)
1629 return nfs4_client_recover_expired_lease(server->nfs_client);
1634 * reclaim state on the server after a network partition.
1635 * Assumes caller holds the appropriate lock
1637 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1639 struct nfs4_opendata *opendata;
1642 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1643 if (IS_ERR(opendata))
1644 return PTR_ERR(opendata);
1645 ret = nfs4_open_recover(opendata, state);
1647 d_drop(ctx->path.dentry);
1648 nfs4_opendata_put(opendata);
1652 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1654 struct nfs_server *server = NFS_SERVER(state->inode);
1655 struct nfs4_exception exception = { };
1659 err = _nfs4_open_expired(ctx, state);
1663 case -NFS4ERR_GRACE:
1664 case -NFS4ERR_DELAY:
1665 nfs4_handle_exception(server, err, &exception);
1668 } while (exception.retry);
1673 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1675 struct nfs_open_context *ctx;
1678 ctx = nfs4_state_find_open_context(state);
1680 return PTR_ERR(ctx);
1681 ret = nfs4_do_open_expired(ctx, state);
1682 put_nfs_open_context(ctx);
1687 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1688 * fields corresponding to attributes that were used to store the verifier.
1689 * Make sure we clobber those fields in the later setattr call
1691 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1693 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1694 !(sattr->ia_valid & ATTR_ATIME_SET))
1695 sattr->ia_valid |= ATTR_ATIME;
1697 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1698 !(sattr->ia_valid & ATTR_MTIME_SET))
1699 sattr->ia_valid |= ATTR_MTIME;
1703 * Returns a referenced nfs4_state
1705 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)
1707 struct nfs4_state_owner *sp;
1708 struct nfs4_state *state = NULL;
1709 struct nfs_server *server = NFS_SERVER(dir);
1710 struct nfs4_opendata *opendata;
1713 /* Protect against reboot recovery conflicts */
1715 if (!(sp = nfs4_get_state_owner(server, cred))) {
1716 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1719 status = nfs4_recover_expired_lease(server);
1721 goto err_put_state_owner;
1722 if (path->dentry->d_inode != NULL)
1723 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
1725 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
1726 if (opendata == NULL)
1727 goto err_put_state_owner;
1729 if (path->dentry->d_inode != NULL)
1730 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1732 status = _nfs4_proc_open(opendata);
1734 goto err_opendata_put;
1736 state = nfs4_opendata_to_nfs4_state(opendata);
1737 status = PTR_ERR(state);
1739 goto err_opendata_put;
1740 if (server->caps & NFS_CAP_POSIX_LOCK)
1741 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1743 if (opendata->o_arg.open_flags & O_EXCL) {
1744 nfs4_exclusive_attrset(opendata, sattr);
1746 nfs_fattr_init(opendata->o_res.f_attr);
1747 status = nfs4_do_setattr(state->inode, cred,
1748 opendata->o_res.f_attr, sattr,
1751 nfs_setattr_update_inode(state->inode, sattr);
1752 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1754 nfs4_opendata_put(opendata);
1755 nfs4_put_state_owner(sp);
1759 nfs4_opendata_put(opendata);
1760 err_put_state_owner:
1761 nfs4_put_state_owner(sp);
1768 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)
1770 struct nfs4_exception exception = { };
1771 struct nfs4_state *res;
1775 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
1778 /* NOTE: BAD_SEQID means the server and client disagree about the
1779 * book-keeping w.r.t. state-changing operations
1780 * (OPEN/CLOSE/LOCK/LOCKU...)
1781 * It is actually a sign of a bug on the client or on the server.
1783 * If we receive a BAD_SEQID error in the particular case of
1784 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1785 * have unhashed the old state_owner for us, and that we can
1786 * therefore safely retry using a new one. We should still warn
1787 * the user though...
1789 if (status == -NFS4ERR_BAD_SEQID) {
1790 printk(KERN_WARNING "NFS: v4 server %s "
1791 " returned a bad sequence-id error!\n",
1792 NFS_SERVER(dir)->nfs_client->cl_hostname);
1793 exception.retry = 1;
1797 * BAD_STATEID on OPEN means that the server cancelled our
1798 * state before it received the OPEN_CONFIRM.
1799 * Recover by retrying the request as per the discussion
1800 * on Page 181 of RFC3530.
1802 if (status == -NFS4ERR_BAD_STATEID) {
1803 exception.retry = 1;
1806 if (status == -EAGAIN) {
1807 /* We must have found a delegation */
1808 exception.retry = 1;
1811 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1812 status, &exception));
1813 } while (exception.retry);
1817 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1818 struct nfs_fattr *fattr, struct iattr *sattr,
1819 struct nfs4_state *state)
1821 struct nfs_server *server = NFS_SERVER(inode);
1822 struct nfs_setattrargs arg = {
1823 .fh = NFS_FH(inode),
1826 .bitmask = server->attr_bitmask,
1828 struct nfs_setattrres res = {
1832 struct rpc_message msg = {
1833 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1838 unsigned long timestamp = jiffies;
1841 nfs_fattr_init(fattr);
1843 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1844 /* Use that stateid */
1845 } else if (state != NULL) {
1846 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
1848 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1850 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1851 if (status == 0 && state != NULL)
1852 renew_lease(server, timestamp);
1856 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1857 struct nfs_fattr *fattr, struct iattr *sattr,
1858 struct nfs4_state *state)
1860 struct nfs_server *server = NFS_SERVER(inode);
1861 struct nfs4_exception exception = { };
1864 err = nfs4_handle_exception(server,
1865 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1867 } while (exception.retry);
1871 struct nfs4_closedata {
1873 struct inode *inode;
1874 struct nfs4_state *state;
1875 struct nfs_closeargs arg;
1876 struct nfs_closeres res;
1877 struct nfs_fattr fattr;
1878 unsigned long timestamp;
1883 static void nfs4_free_closedata(void *data)
1885 struct nfs4_closedata *calldata = data;
1886 struct nfs4_state_owner *sp = calldata->state->owner;
1889 pnfs_roc_release(calldata->state->inode);
1890 nfs4_put_open_state(calldata->state);
1891 nfs_free_seqid(calldata->arg.seqid);
1892 nfs4_put_state_owner(sp);
1893 path_put(&calldata->path);
1897 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1900 spin_lock(&state->owner->so_lock);
1901 if (!(fmode & FMODE_READ))
1902 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1903 if (!(fmode & FMODE_WRITE))
1904 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1905 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1906 spin_unlock(&state->owner->so_lock);
1909 static void nfs4_close_done(struct rpc_task *task, void *data)
1911 struct nfs4_closedata *calldata = data;
1912 struct nfs4_state *state = calldata->state;
1913 struct nfs_server *server = NFS_SERVER(calldata->inode);
1915 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1917 /* hmm. we are done with the inode, and in the process of freeing
1918 * the state_owner. we keep this around to process errors
1920 switch (task->tk_status) {
1923 pnfs_roc_set_barrier(state->inode,
1924 calldata->roc_barrier);
1925 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
1926 renew_lease(server, calldata->timestamp);
1927 nfs4_close_clear_stateid_flags(state,
1928 calldata->arg.fmode);
1930 case -NFS4ERR_STALE_STATEID:
1931 case -NFS4ERR_OLD_STATEID:
1932 case -NFS4ERR_BAD_STATEID:
1933 case -NFS4ERR_EXPIRED:
1934 if (calldata->arg.fmode == 0)
1937 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1938 rpc_restart_call_prepare(task);
1940 nfs_release_seqid(calldata->arg.seqid);
1941 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
1944 static void nfs4_close_prepare(struct rpc_task *task, void *data)
1946 struct nfs4_closedata *calldata = data;
1947 struct nfs4_state *state = calldata->state;
1950 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
1953 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1954 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
1955 spin_lock(&state->owner->so_lock);
1956 /* Calculate the change in open mode */
1957 if (state->n_rdwr == 0) {
1958 if (state->n_rdonly == 0) {
1959 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1960 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1961 calldata->arg.fmode &= ~FMODE_READ;
1963 if (state->n_wronly == 0) {
1964 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1965 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1966 calldata->arg.fmode &= ~FMODE_WRITE;
1969 spin_unlock(&state->owner->so_lock);
1972 /* Note: exit _without_ calling nfs4_close_done */
1973 task->tk_action = NULL;
1977 if (calldata->arg.fmode == 0) {
1978 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1979 if (calldata->roc &&
1980 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
1981 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
1987 nfs_fattr_init(calldata->res.fattr);
1988 calldata->timestamp = jiffies;
1989 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
1990 &calldata->arg.seq_args, &calldata->res.seq_res,
1993 rpc_call_start(task);
1996 static const struct rpc_call_ops nfs4_close_ops = {
1997 .rpc_call_prepare = nfs4_close_prepare,
1998 .rpc_call_done = nfs4_close_done,
1999 .rpc_release = nfs4_free_closedata,
2003 * It is possible for data to be read/written from a mem-mapped file
2004 * after the sys_close call (which hits the vfs layer as a flush).
2005 * This means that we can't safely call nfsv4 close on a file until
2006 * the inode is cleared. This in turn means that we are not good
2007 * NFSv4 citizens - we do not indicate to the server to update the file's
2008 * share state even when we are done with one of the three share
2009 * stateid's in the inode.
2011 * NOTE: Caller must be holding the sp->so_owner semaphore!
2013 int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
2015 struct nfs_server *server = NFS_SERVER(state->inode);
2016 struct nfs4_closedata *calldata;
2017 struct nfs4_state_owner *sp = state->owner;
2018 struct rpc_task *task;
2019 struct rpc_message msg = {
2020 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2021 .rpc_cred = state->owner->so_cred,
2023 struct rpc_task_setup task_setup_data = {
2024 .rpc_client = server->client,
2025 .rpc_message = &msg,
2026 .callback_ops = &nfs4_close_ops,
2027 .workqueue = nfsiod_workqueue,
2028 .flags = RPC_TASK_ASYNC,
2030 int status = -ENOMEM;
2032 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2033 if (calldata == NULL)
2035 calldata->inode = state->inode;
2036 calldata->state = state;
2037 calldata->arg.fh = NFS_FH(state->inode);
2038 calldata->arg.stateid = &state->open_stateid;
2039 /* Serialization for the sequence id */
2040 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2041 if (calldata->arg.seqid == NULL)
2042 goto out_free_calldata;
2043 calldata->arg.fmode = 0;
2044 calldata->arg.bitmask = server->cache_consistency_bitmask;
2045 calldata->res.fattr = &calldata->fattr;
2046 calldata->res.seqid = calldata->arg.seqid;
2047 calldata->res.server = server;
2048 calldata->roc = roc;
2050 calldata->path = *path;
2052 msg.rpc_argp = &calldata->arg;
2053 msg.rpc_resp = &calldata->res;
2054 task_setup_data.callback_data = calldata;
2055 task = rpc_run_task(&task_setup_data);
2057 return PTR_ERR(task);
2060 status = rpc_wait_for_completion_task(task);
2067 pnfs_roc_release(state->inode);
2068 nfs4_put_open_state(state);
2069 nfs4_put_state_owner(sp);
2073 static struct inode *
2074 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2076 struct nfs4_state *state;
2078 /* Protect against concurrent sillydeletes */
2079 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
2081 return ERR_CAST(state);
2083 return igrab(state->inode);
2086 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2088 if (ctx->state == NULL)
2091 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2093 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2096 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2098 struct nfs4_server_caps_arg args = {
2101 struct nfs4_server_caps_res res = {};
2102 struct rpc_message msg = {
2103 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2109 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2111 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2112 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2113 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2114 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2115 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2116 NFS_CAP_CTIME|NFS_CAP_MTIME);
2117 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2118 server->caps |= NFS_CAP_ACLS;
2119 if (res.has_links != 0)
2120 server->caps |= NFS_CAP_HARDLINKS;
2121 if (res.has_symlinks != 0)
2122 server->caps |= NFS_CAP_SYMLINKS;
2123 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2124 server->caps |= NFS_CAP_FILEID;
2125 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2126 server->caps |= NFS_CAP_MODE;
2127 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2128 server->caps |= NFS_CAP_NLINK;
2129 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2130 server->caps |= NFS_CAP_OWNER;
2131 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2132 server->caps |= NFS_CAP_OWNER_GROUP;
2133 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2134 server->caps |= NFS_CAP_ATIME;
2135 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2136 server->caps |= NFS_CAP_CTIME;
2137 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2138 server->caps |= NFS_CAP_MTIME;
2140 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2141 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2142 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2143 server->acl_bitmask = res.acl_bitmask;
2149 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2151 struct nfs4_exception exception = { };
2154 err = nfs4_handle_exception(server,
2155 _nfs4_server_capabilities(server, fhandle),
2157 } while (exception.retry);
2161 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2162 struct nfs_fsinfo *info)
2164 struct nfs4_lookup_root_arg args = {
2165 .bitmask = nfs4_fattr_bitmap,
2167 struct nfs4_lookup_res res = {
2169 .fattr = info->fattr,
2172 struct rpc_message msg = {
2173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2178 nfs_fattr_init(info->fattr);
2179 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2182 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2183 struct nfs_fsinfo *info)
2185 struct nfs4_exception exception = { };
2188 err = nfs4_handle_exception(server,
2189 _nfs4_lookup_root(server, fhandle, info),
2191 } while (exception.retry);
2195 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2196 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2198 struct rpc_auth *auth;
2201 auth = rpcauth_create(flavor, server->client);
2206 ret = nfs4_lookup_root(server, fhandle, info);
2214 * get the file handle for the "/" directory on the server
2216 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2217 struct nfs_fsinfo *info)
2219 int i, len, status = 0;
2220 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS + 2];
2222 flav_array[0] = RPC_AUTH_UNIX;
2223 len = gss_mech_list_pseudoflavors(&flav_array[1]);
2224 flav_array[1+len] = RPC_AUTH_NULL;
2227 for (i = 0; i < len; i++) {
2228 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2233 status = nfs4_server_capabilities(server, fhandle);
2235 status = nfs4_do_fsinfo(server, fhandle, info);
2236 return nfs4_map_errors(status);
2240 * Get locations and (maybe) other attributes of a referral.
2241 * Note that we'll actually follow the referral later when
2242 * we detect fsid mismatch in inode revalidation
2244 static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2246 int status = -ENOMEM;
2247 struct page *page = NULL;
2248 struct nfs4_fs_locations *locations = NULL;
2250 page = alloc_page(GFP_KERNEL);
2253 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2254 if (locations == NULL)
2257 status = nfs4_proc_fs_locations(dir, name, locations, page);
2260 /* Make sure server returned a different fsid for the referral */
2261 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2262 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
2267 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2268 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2270 fattr->mode = S_IFDIR;
2271 memset(fhandle, 0, sizeof(struct nfs_fh));
2279 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2281 struct nfs4_getattr_arg args = {
2283 .bitmask = server->attr_bitmask,
2285 struct nfs4_getattr_res res = {
2289 struct rpc_message msg = {
2290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2295 nfs_fattr_init(fattr);
2296 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2299 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2301 struct nfs4_exception exception = { };
2304 err = nfs4_handle_exception(server,
2305 _nfs4_proc_getattr(server, fhandle, fattr),
2307 } while (exception.retry);
2312 * The file is not closed if it is opened due to the a request to change
2313 * the size of the file. The open call will not be needed once the
2314 * VFS layer lookup-intents are implemented.
2316 * Close is called when the inode is destroyed.
2317 * If we haven't opened the file for O_WRONLY, we
2318 * need to in the size_change case to obtain a stateid.
2321 * Because OPEN is always done by name in nfsv4, it is
2322 * possible that we opened a different file by the same
2323 * name. We can recognize this race condition, but we
2324 * can't do anything about it besides returning an error.
2326 * This will be fixed with VFS changes (lookup-intent).
2329 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2330 struct iattr *sattr)
2332 struct inode *inode = dentry->d_inode;
2333 struct rpc_cred *cred = NULL;
2334 struct nfs4_state *state = NULL;
2337 nfs_fattr_init(fattr);
2339 /* Search for an existing open(O_WRITE) file */
2340 if (sattr->ia_valid & ATTR_FILE) {
2341 struct nfs_open_context *ctx;
2343 ctx = nfs_file_open_context(sattr->ia_file);
2350 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2352 nfs_setattr_update_inode(inode, sattr);
2356 static int _nfs4_proc_lookupfh(struct rpc_clnt *clnt, struct nfs_server *server,
2357 const struct nfs_fh *dirfh, const struct qstr *name,
2358 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2361 struct nfs4_lookup_arg args = {
2362 .bitmask = server->attr_bitmask,
2366 struct nfs4_lookup_res res = {
2371 struct rpc_message msg = {
2372 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2377 nfs_fattr_init(fattr);
2379 dprintk("NFS call lookupfh %s\n", name->name);
2380 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2381 dprintk("NFS reply lookupfh: %d\n", status);
2385 static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2386 struct qstr *name, struct nfs_fh *fhandle,
2387 struct nfs_fattr *fattr)
2389 struct nfs4_exception exception = { };
2392 err = _nfs4_proc_lookupfh(server->client, server, dirfh, name, fhandle, fattr);
2394 if (err == -NFS4ERR_MOVED) {
2398 err = nfs4_handle_exception(server, err, &exception);
2399 } while (exception.retry);
2403 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2404 const struct qstr *name, struct nfs_fh *fhandle,
2405 struct nfs_fattr *fattr)
2409 dprintk("NFS call lookup %s\n", name->name);
2410 status = _nfs4_proc_lookupfh(clnt, NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
2411 if (status == -NFS4ERR_MOVED)
2412 status = nfs4_get_referral(dir, name, fattr, fhandle);
2413 dprintk("NFS reply lookup: %d\n", status);
2417 void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2419 memset(fh, 0, sizeof(struct nfs_fh));
2420 fattr->fsid.major = 1;
2421 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2422 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2423 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2427 static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2428 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2430 struct nfs4_exception exception = { };
2433 err = nfs4_handle_exception(NFS_SERVER(dir),
2434 _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr),
2437 nfs_fixup_secinfo_attributes(fattr, fhandle);
2438 } while (exception.retry);
2442 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2444 struct nfs_server *server = NFS_SERVER(inode);
2445 struct nfs4_accessargs args = {
2446 .fh = NFS_FH(inode),
2447 .bitmask = server->attr_bitmask,
2449 struct nfs4_accessres res = {
2452 struct rpc_message msg = {
2453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2456 .rpc_cred = entry->cred,
2458 int mode = entry->mask;
2462 * Determine which access bits we want to ask for...
2464 if (mode & MAY_READ)
2465 args.access |= NFS4_ACCESS_READ;
2466 if (S_ISDIR(inode->i_mode)) {
2467 if (mode & MAY_WRITE)
2468 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2469 if (mode & MAY_EXEC)
2470 args.access |= NFS4_ACCESS_LOOKUP;
2472 if (mode & MAY_WRITE)
2473 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2474 if (mode & MAY_EXEC)
2475 args.access |= NFS4_ACCESS_EXECUTE;
2478 res.fattr = nfs_alloc_fattr();
2479 if (res.fattr == NULL)
2482 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2485 if (res.access & NFS4_ACCESS_READ)
2486 entry->mask |= MAY_READ;
2487 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2488 entry->mask |= MAY_WRITE;
2489 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2490 entry->mask |= MAY_EXEC;
2491 nfs_refresh_inode(inode, res.fattr);
2493 nfs_free_fattr(res.fattr);
2497 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2499 struct nfs4_exception exception = { };
2502 err = nfs4_handle_exception(NFS_SERVER(inode),
2503 _nfs4_proc_access(inode, entry),
2505 } while (exception.retry);
2510 * TODO: For the time being, we don't try to get any attributes
2511 * along with any of the zero-copy operations READ, READDIR,
2514 * In the case of the first three, we want to put the GETATTR
2515 * after the read-type operation -- this is because it is hard
2516 * to predict the length of a GETATTR response in v4, and thus
2517 * align the READ data correctly. This means that the GETATTR
2518 * may end up partially falling into the page cache, and we should
2519 * shift it into the 'tail' of the xdr_buf before processing.
2520 * To do this efficiently, we need to know the total length
2521 * of data received, which doesn't seem to be available outside
2524 * In the case of WRITE, we also want to put the GETATTR after
2525 * the operation -- in this case because we want to make sure
2526 * we get the post-operation mtime and size. This means that
2527 * we can't use xdr_encode_pages() as written: we need a variant
2528 * of it which would leave room in the 'tail' iovec.
2530 * Both of these changes to the XDR layer would in fact be quite
2531 * minor, but I decided to leave them for a subsequent patch.
2533 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2534 unsigned int pgbase, unsigned int pglen)
2536 struct nfs4_readlink args = {
2537 .fh = NFS_FH(inode),
2542 struct nfs4_readlink_res res;
2543 struct rpc_message msg = {
2544 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2549 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2552 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2553 unsigned int pgbase, unsigned int pglen)
2555 struct nfs4_exception exception = { };
2558 err = nfs4_handle_exception(NFS_SERVER(inode),
2559 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2561 } while (exception.retry);
2567 * We will need to arrange for the VFS layer to provide an atomic open.
2568 * Until then, this create/open method is prone to inefficiency and race
2569 * conditions due to the lookup, create, and open VFS calls from sys_open()
2570 * placed on the wire.
2572 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2573 * The file will be opened again in the subsequent VFS open call
2574 * (nfs4_proc_file_open).
2576 * The open for read will just hang around to be used by any process that
2577 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2581 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2582 int flags, struct nfs_open_context *ctx)
2584 struct path my_path = {
2587 struct path *path = &my_path;
2588 struct nfs4_state *state;
2589 struct rpc_cred *cred = NULL;
2598 sattr->ia_mode &= ~current_umask();
2599 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
2601 if (IS_ERR(state)) {
2602 status = PTR_ERR(state);
2605 d_add(dentry, igrab(state->inode));
2606 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2610 nfs4_close_sync(path, state, fmode);
2615 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2617 struct nfs_server *server = NFS_SERVER(dir);
2618 struct nfs_removeargs args = {
2620 .name.len = name->len,
2621 .name.name = name->name,
2622 .bitmask = server->attr_bitmask,
2624 struct nfs_removeres res = {
2627 struct rpc_message msg = {
2628 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2632 int status = -ENOMEM;
2634 res.dir_attr = nfs_alloc_fattr();
2635 if (res.dir_attr == NULL)
2638 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2640 update_changeattr(dir, &res.cinfo);
2641 nfs_post_op_update_inode(dir, res.dir_attr);
2643 nfs_free_fattr(res.dir_attr);
2648 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2650 struct nfs4_exception exception = { };
2653 err = nfs4_handle_exception(NFS_SERVER(dir),
2654 _nfs4_proc_remove(dir, name),
2656 } while (exception.retry);
2660 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2662 struct nfs_server *server = NFS_SERVER(dir);
2663 struct nfs_removeargs *args = msg->rpc_argp;
2664 struct nfs_removeres *res = msg->rpc_resp;
2666 args->bitmask = server->cache_consistency_bitmask;
2667 res->server = server;
2668 res->seq_res.sr_slot = NULL;
2669 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2672 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2674 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2676 if (!nfs4_sequence_done(task, &res->seq_res))
2678 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2680 update_changeattr(dir, &res->cinfo);
2681 nfs_post_op_update_inode(dir, res->dir_attr);
2685 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2687 struct nfs_server *server = NFS_SERVER(dir);
2688 struct nfs_renameargs *arg = msg->rpc_argp;
2689 struct nfs_renameres *res = msg->rpc_resp;
2691 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2692 arg->bitmask = server->attr_bitmask;
2693 res->server = server;
2696 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2697 struct inode *new_dir)
2699 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2701 if (!nfs4_sequence_done(task, &res->seq_res))
2703 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2706 update_changeattr(old_dir, &res->old_cinfo);
2707 nfs_post_op_update_inode(old_dir, res->old_fattr);
2708 update_changeattr(new_dir, &res->new_cinfo);
2709 nfs_post_op_update_inode(new_dir, res->new_fattr);
2713 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2714 struct inode *new_dir, struct qstr *new_name)
2716 struct nfs_server *server = NFS_SERVER(old_dir);
2717 struct nfs_renameargs arg = {
2718 .old_dir = NFS_FH(old_dir),
2719 .new_dir = NFS_FH(new_dir),
2720 .old_name = old_name,
2721 .new_name = new_name,
2722 .bitmask = server->attr_bitmask,
2724 struct nfs_renameres res = {
2727 struct rpc_message msg = {
2728 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2732 int status = -ENOMEM;
2734 res.old_fattr = nfs_alloc_fattr();
2735 res.new_fattr = nfs_alloc_fattr();
2736 if (res.old_fattr == NULL || res.new_fattr == NULL)
2739 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
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);
2747 nfs_free_fattr(res.new_fattr);
2748 nfs_free_fattr(res.old_fattr);
2752 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2753 struct inode *new_dir, struct qstr *new_name)
2755 struct nfs4_exception exception = { };
2758 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2759 _nfs4_proc_rename(old_dir, old_name,
2762 } while (exception.retry);
2766 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2768 struct nfs_server *server = NFS_SERVER(inode);
2769 struct nfs4_link_arg arg = {
2770 .fh = NFS_FH(inode),
2771 .dir_fh = NFS_FH(dir),
2773 .bitmask = server->attr_bitmask,
2775 struct nfs4_link_res res = {
2778 struct rpc_message msg = {
2779 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2783 int status = -ENOMEM;
2785 res.fattr = nfs_alloc_fattr();
2786 res.dir_attr = nfs_alloc_fattr();
2787 if (res.fattr == NULL || res.dir_attr == NULL)
2790 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2792 update_changeattr(dir, &res.cinfo);
2793 nfs_post_op_update_inode(dir, res.dir_attr);
2794 nfs_post_op_update_inode(inode, res.fattr);
2797 nfs_free_fattr(res.dir_attr);
2798 nfs_free_fattr(res.fattr);
2802 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2804 struct nfs4_exception exception = { };
2807 err = nfs4_handle_exception(NFS_SERVER(inode),
2808 _nfs4_proc_link(inode, dir, name),
2810 } while (exception.retry);
2814 struct nfs4_createdata {
2815 struct rpc_message msg;
2816 struct nfs4_create_arg arg;
2817 struct nfs4_create_res res;
2819 struct nfs_fattr fattr;
2820 struct nfs_fattr dir_fattr;
2823 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2824 struct qstr *name, struct iattr *sattr, u32 ftype)
2826 struct nfs4_createdata *data;
2828 data = kzalloc(sizeof(*data), GFP_KERNEL);
2830 struct nfs_server *server = NFS_SERVER(dir);
2832 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2833 data->msg.rpc_argp = &data->arg;
2834 data->msg.rpc_resp = &data->res;
2835 data->arg.dir_fh = NFS_FH(dir);
2836 data->arg.server = server;
2837 data->arg.name = name;
2838 data->arg.attrs = sattr;
2839 data->arg.ftype = ftype;
2840 data->arg.bitmask = server->attr_bitmask;
2841 data->res.server = server;
2842 data->res.fh = &data->fh;
2843 data->res.fattr = &data->fattr;
2844 data->res.dir_fattr = &data->dir_fattr;
2845 nfs_fattr_init(data->res.fattr);
2846 nfs_fattr_init(data->res.dir_fattr);
2851 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2853 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
2854 &data->arg.seq_args, &data->res.seq_res, 1);
2856 update_changeattr(dir, &data->res.dir_cinfo);
2857 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2858 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2863 static void nfs4_free_createdata(struct nfs4_createdata *data)
2868 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2869 struct page *page, unsigned int len, struct iattr *sattr)
2871 struct nfs4_createdata *data;
2872 int status = -ENAMETOOLONG;
2874 if (len > NFS4_MAXPATHLEN)
2878 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2882 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2883 data->arg.u.symlink.pages = &page;
2884 data->arg.u.symlink.len = len;
2886 status = nfs4_do_create(dir, dentry, data);
2888 nfs4_free_createdata(data);
2893 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2894 struct page *page, unsigned int len, struct iattr *sattr)
2896 struct nfs4_exception exception = { };
2899 err = nfs4_handle_exception(NFS_SERVER(dir),
2900 _nfs4_proc_symlink(dir, dentry, page,
2903 } while (exception.retry);
2907 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2908 struct iattr *sattr)
2910 struct nfs4_createdata *data;
2911 int status = -ENOMEM;
2913 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2917 status = nfs4_do_create(dir, dentry, data);
2919 nfs4_free_createdata(data);
2924 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2925 struct iattr *sattr)
2927 struct nfs4_exception exception = { };
2930 sattr->ia_mode &= ~current_umask();
2932 err = nfs4_handle_exception(NFS_SERVER(dir),
2933 _nfs4_proc_mkdir(dir, dentry, sattr),
2935 } while (exception.retry);
2939 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2940 u64 cookie, struct page **pages, unsigned int count, int plus)
2942 struct inode *dir = dentry->d_inode;
2943 struct nfs4_readdir_arg args = {
2948 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
2951 struct nfs4_readdir_res res;
2952 struct rpc_message msg = {
2953 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2960 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
2961 dentry->d_parent->d_name.name,
2962 dentry->d_name.name,
2963 (unsigned long long)cookie);
2964 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2965 res.pgbase = args.pgbase;
2966 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
2968 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
2969 status += args.pgbase;
2972 nfs_invalidate_atime(dir);
2974 dprintk("%s: returns %d\n", __func__, status);
2978 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2979 u64 cookie, struct page **pages, unsigned int count, int plus)
2981 struct nfs4_exception exception = { };
2984 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2985 _nfs4_proc_readdir(dentry, cred, cookie,
2986 pages, count, plus),
2988 } while (exception.retry);
2992 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2993 struct iattr *sattr, dev_t rdev)
2995 struct nfs4_createdata *data;
2996 int mode = sattr->ia_mode;
2997 int status = -ENOMEM;
2999 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3000 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3002 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3007 data->arg.ftype = NF4FIFO;
3008 else if (S_ISBLK(mode)) {
3009 data->arg.ftype = NF4BLK;
3010 data->arg.u.device.specdata1 = MAJOR(rdev);
3011 data->arg.u.device.specdata2 = MINOR(rdev);
3013 else if (S_ISCHR(mode)) {
3014 data->arg.ftype = NF4CHR;
3015 data->arg.u.device.specdata1 = MAJOR(rdev);
3016 data->arg.u.device.specdata2 = MINOR(rdev);
3019 status = nfs4_do_create(dir, dentry, data);
3021 nfs4_free_createdata(data);
3026 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3027 struct iattr *sattr, dev_t rdev)
3029 struct nfs4_exception exception = { };
3032 sattr->ia_mode &= ~current_umask();
3034 err = nfs4_handle_exception(NFS_SERVER(dir),
3035 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3037 } while (exception.retry);
3041 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3042 struct nfs_fsstat *fsstat)
3044 struct nfs4_statfs_arg args = {
3046 .bitmask = server->attr_bitmask,
3048 struct nfs4_statfs_res res = {
3051 struct rpc_message msg = {
3052 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3057 nfs_fattr_init(fsstat->fattr);
3058 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3061 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3063 struct nfs4_exception exception = { };
3066 err = nfs4_handle_exception(server,
3067 _nfs4_proc_statfs(server, fhandle, fsstat),
3069 } while (exception.retry);
3073 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3074 struct nfs_fsinfo *fsinfo)
3076 struct nfs4_fsinfo_arg args = {
3078 .bitmask = server->attr_bitmask,
3080 struct nfs4_fsinfo_res res = {
3083 struct rpc_message msg = {
3084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3089 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3092 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3094 struct nfs4_exception exception = { };
3098 err = nfs4_handle_exception(server,
3099 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3101 } while (exception.retry);
3105 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3107 nfs_fattr_init(fsinfo->fattr);
3108 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3111 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3112 struct nfs_pathconf *pathconf)
3114 struct nfs4_pathconf_arg args = {
3116 .bitmask = server->attr_bitmask,
3118 struct nfs4_pathconf_res res = {
3119 .pathconf = pathconf,
3121 struct rpc_message msg = {
3122 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3127 /* None of the pathconf attributes are mandatory to implement */
3128 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3129 memset(pathconf, 0, sizeof(*pathconf));
3133 nfs_fattr_init(pathconf->fattr);
3134 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3137 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3138 struct nfs_pathconf *pathconf)
3140 struct nfs4_exception exception = { };
3144 err = nfs4_handle_exception(server,
3145 _nfs4_proc_pathconf(server, fhandle, pathconf),
3147 } while (exception.retry);
3151 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3153 struct nfs_server *server = NFS_SERVER(data->inode);
3155 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3156 nfs_restart_rpc(task, server->nfs_client);
3160 nfs_invalidate_atime(data->inode);
3161 if (task->tk_status > 0)
3162 renew_lease(server, data->timestamp);
3166 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3169 dprintk("--> %s\n", __func__);
3171 if (!nfs4_sequence_done(task, &data->res.seq_res))
3174 return data->read_done_cb(task, data);
3177 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3179 data->timestamp = jiffies;
3180 data->read_done_cb = nfs4_read_done_cb;
3181 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3184 /* Reset the the nfs_read_data to send the read to the MDS. */
3185 void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3187 dprintk("%s Reset task for i/o through\n", __func__);
3188 put_lseg(data->lseg);
3190 /* offsets will differ in the dense stripe case */
3191 data->args.offset = data->mds_offset;
3192 data->ds_clp = NULL;
3193 data->args.fh = NFS_FH(data->inode);
3194 data->read_done_cb = nfs4_read_done_cb;
3195 task->tk_ops = data->mds_ops;
3196 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3198 EXPORT_SYMBOL_GPL(nfs4_reset_read);
3200 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3202 struct inode *inode = data->inode;
3204 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3205 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3208 if (task->tk_status >= 0) {
3209 renew_lease(NFS_SERVER(inode), data->timestamp);
3210 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3215 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3217 if (!nfs4_sequence_done(task, &data->res.seq_res))
3219 return data->write_done_cb(task, data);
3222 /* Reset the the nfs_write_data to send the write to the MDS. */
3223 void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3225 dprintk("%s Reset task for i/o through\n", __func__);
3226 put_lseg(data->lseg);
3228 data->ds_clp = NULL;
3229 data->write_done_cb = nfs4_write_done_cb;
3230 data->args.fh = NFS_FH(data->inode);
3231 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3232 data->args.offset = data->mds_offset;
3233 data->res.fattr = &data->fattr;
3234 task->tk_ops = data->mds_ops;
3235 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3237 EXPORT_SYMBOL_GPL(nfs4_reset_write);
3239 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3241 struct nfs_server *server = NFS_SERVER(data->inode);
3244 data->args.bitmask = NULL;
3245 data->res.fattr = NULL;
3247 data->args.bitmask = server->cache_consistency_bitmask;
3248 if (!data->write_done_cb)
3249 data->write_done_cb = nfs4_write_done_cb;
3250 data->res.server = server;
3251 data->timestamp = jiffies;
3253 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3256 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3258 struct inode *inode = data->inode;
3260 if (!nfs4_sequence_done(task, &data->res.seq_res))
3263 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3264 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
3267 nfs_refresh_inode(inode, data->res.fattr);
3271 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3273 struct nfs_server *server = NFS_SERVER(data->inode);
3275 data->args.bitmask = server->cache_consistency_bitmask;
3276 data->res.server = server;
3277 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3280 struct nfs4_renewdata {
3281 struct nfs_client *client;
3282 unsigned long timestamp;
3286 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3287 * standalone procedure for queueing an asynchronous RENEW.
3289 static void nfs4_renew_release(void *calldata)
3291 struct nfs4_renewdata *data = calldata;
3292 struct nfs_client *clp = data->client;
3294 if (atomic_read(&clp->cl_count) > 1)
3295 nfs4_schedule_state_renewal(clp);
3296 nfs_put_client(clp);
3300 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3302 struct nfs4_renewdata *data = calldata;
3303 struct nfs_client *clp = data->client;
3304 unsigned long timestamp = data->timestamp;
3306 if (task->tk_status < 0) {
3307 /* Unless we're shutting down, schedule state recovery! */
3308 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3309 nfs4_schedule_lease_recovery(clp);
3312 do_renew_lease(clp, timestamp);
3315 static const struct rpc_call_ops nfs4_renew_ops = {
3316 .rpc_call_done = nfs4_renew_done,
3317 .rpc_release = nfs4_renew_release,
3320 int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
3322 struct rpc_message msg = {
3323 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3327 struct nfs4_renewdata *data;
3329 if (!atomic_inc_not_zero(&clp->cl_count))
3331 data = kmalloc(sizeof(*data), GFP_KERNEL);
3335 data->timestamp = jiffies;
3336 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3337 &nfs4_renew_ops, data);
3340 int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3342 struct rpc_message msg = {
3343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3347 unsigned long now = jiffies;
3350 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3353 do_renew_lease(clp, now);
3357 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3359 return (server->caps & NFS_CAP_ACLS)
3360 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3361 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3364 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3365 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3368 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3370 static void buf_to_pages(const void *buf, size_t buflen,
3371 struct page **pages, unsigned int *pgbase)
3373 const void *p = buf;
3375 *pgbase = offset_in_page(buf);
3377 while (p < buf + buflen) {
3378 *(pages++) = virt_to_page(p);
3379 p += PAGE_CACHE_SIZE;
3383 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3384 struct page **pages, unsigned int *pgbase)
3386 struct page *newpage, **spages;
3392 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3393 newpage = alloc_page(GFP_KERNEL);
3395 if (newpage == NULL)
3397 memcpy(page_address(newpage), buf, len);
3402 } while (buflen != 0);
3408 __free_page(spages[rc-1]);
3412 struct nfs4_cached_acl {
3418 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3420 struct nfs_inode *nfsi = NFS_I(inode);
3422 spin_lock(&inode->i_lock);
3423 kfree(nfsi->nfs4_acl);
3424 nfsi->nfs4_acl = acl;
3425 spin_unlock(&inode->i_lock);
3428 static void nfs4_zap_acl_attr(struct inode *inode)
3430 nfs4_set_cached_acl(inode, NULL);
3433 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3435 struct nfs_inode *nfsi = NFS_I(inode);
3436 struct nfs4_cached_acl *acl;
3439 spin_lock(&inode->i_lock);
3440 acl = nfsi->nfs4_acl;
3443 if (buf == NULL) /* user is just asking for length */
3445 if (acl->cached == 0)
3447 ret = -ERANGE; /* see getxattr(2) man page */
3448 if (acl->len > buflen)
3450 memcpy(buf, acl->data, acl->len);
3454 spin_unlock(&inode->i_lock);
3458 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3460 struct nfs4_cached_acl *acl;
3462 if (buf && acl_len <= PAGE_SIZE) {
3463 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3467 memcpy(acl->data, buf, acl_len);
3469 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3476 nfs4_set_cached_acl(inode, acl);
3479 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3481 struct page *pages[NFS4ACL_MAXPAGES];
3482 struct nfs_getaclargs args = {
3483 .fh = NFS_FH(inode),
3487 struct nfs_getaclres res = {
3491 struct rpc_message msg = {
3492 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3496 struct page *localpage = NULL;
3499 if (buflen < PAGE_SIZE) {
3500 /* As long as we're doing a round trip to the server anyway,
3501 * let's be prepared for a page of acl data. */
3502 localpage = alloc_page(GFP_KERNEL);
3503 resp_buf = page_address(localpage);
3504 if (localpage == NULL)
3506 args.acl_pages[0] = localpage;
3507 args.acl_pgbase = 0;
3508 args.acl_len = PAGE_SIZE;
3511 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3513 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3516 if (res.acl_len > args.acl_len)
3517 nfs4_write_cached_acl(inode, NULL, res.acl_len);
3519 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
3522 if (res.acl_len > buflen)
3525 memcpy(buf, resp_buf, res.acl_len);
3530 __free_page(localpage);
3534 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3536 struct nfs4_exception exception = { };
3539 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3542 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3543 } while (exception.retry);
3547 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3549 struct nfs_server *server = NFS_SERVER(inode);
3552 if (!nfs4_server_supports_acls(server))
3554 ret = nfs_revalidate_inode(server, inode);
3557 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3558 nfs_zap_acl_cache(inode);
3559 ret = nfs4_read_cached_acl(inode, buf, buflen);
3562 return nfs4_get_acl_uncached(inode, buf, buflen);
3565 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3567 struct nfs_server *server = NFS_SERVER(inode);
3568 struct page *pages[NFS4ACL_MAXPAGES];
3569 struct nfs_setaclargs arg = {
3570 .fh = NFS_FH(inode),
3574 struct nfs_setaclres res;
3575 struct rpc_message msg = {
3576 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3582 if (!nfs4_server_supports_acls(server))
3584 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3587 nfs_inode_return_delegation(inode);
3588 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3591 * Free each page after tx, so the only ref left is
3592 * held by the network stack
3595 put_page(pages[i-1]);
3598 * Acl update can result in inode attribute update.
3599 * so mark the attribute cache invalid.
3601 spin_lock(&inode->i_lock);
3602 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3603 spin_unlock(&inode->i_lock);
3604 nfs_access_zap_cache(inode);
3605 nfs_zap_acl_cache(inode);
3609 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3611 struct nfs4_exception exception = { };
3614 err = nfs4_handle_exception(NFS_SERVER(inode),
3615 __nfs4_proc_set_acl(inode, buf, buflen),
3617 } while (exception.retry);
3622 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3624 struct nfs_client *clp = server->nfs_client;
3626 if (task->tk_status >= 0)
3628 switch(task->tk_status) {
3629 case -NFS4ERR_ADMIN_REVOKED:
3630 case -NFS4ERR_BAD_STATEID:
3631 case -NFS4ERR_OPENMODE:
3634 nfs4_schedule_stateid_recovery(server, state);
3635 goto wait_on_recovery;
3636 case -NFS4ERR_STALE_STATEID:
3637 case -NFS4ERR_STALE_CLIENTID:
3638 case -NFS4ERR_EXPIRED:
3639 nfs4_schedule_lease_recovery(clp);
3640 goto wait_on_recovery;
3641 #if defined(CONFIG_NFS_V4_1)
3642 case -NFS4ERR_BADSESSION:
3643 case -NFS4ERR_BADSLOT:
3644 case -NFS4ERR_BAD_HIGH_SLOT:
3645 case -NFS4ERR_DEADSESSION:
3646 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3647 case -NFS4ERR_SEQ_FALSE_RETRY:
3648 case -NFS4ERR_SEQ_MISORDERED:
3649 dprintk("%s ERROR %d, Reset session\n", __func__,
3651 nfs4_schedule_session_recovery(clp->cl_session);
3652 task->tk_status = 0;
3654 #endif /* CONFIG_NFS_V4_1 */
3655 case -NFS4ERR_DELAY:
3656 nfs_inc_server_stats(server, NFSIOS_DELAY);
3657 case -NFS4ERR_GRACE:
3659 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3660 task->tk_status = 0;
3662 case -NFS4ERR_OLD_STATEID:
3663 task->tk_status = 0;
3666 task->tk_status = nfs4_map_errors(task->tk_status);
3669 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3670 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3671 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3672 task->tk_status = 0;
3676 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3677 unsigned short port, struct rpc_cred *cred,
3678 struct nfs4_setclientid_res *res)
3680 nfs4_verifier sc_verifier;
3681 struct nfs4_setclientid setclientid = {
3682 .sc_verifier = &sc_verifier,
3684 .sc_cb_ident = clp->cl_cb_ident,
3686 struct rpc_message msg = {
3687 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3688 .rpc_argp = &setclientid,
3696 p = (__be32*)sc_verifier.data;
3697 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3698 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3701 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3702 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3704 rpc_peeraddr2str(clp->cl_rpcclient,
3706 rpc_peeraddr2str(clp->cl_rpcclient,
3708 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3709 clp->cl_id_uniquifier);
3710 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3711 sizeof(setclientid.sc_netid),
3712 rpc_peeraddr2str(clp->cl_rpcclient,
3713 RPC_DISPLAY_NETID));
3714 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3715 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3716 clp->cl_ipaddr, port >> 8, port & 255);
3718 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3719 if (status != -NFS4ERR_CLID_INUSE)
3724 ssleep(clp->cl_lease_time / HZ + 1);
3726 if (++clp->cl_id_uniquifier == 0)
3732 static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3733 struct nfs4_setclientid_res *arg,
3734 struct rpc_cred *cred)
3736 struct nfs_fsinfo fsinfo;
3737 struct rpc_message msg = {
3738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3740 .rpc_resp = &fsinfo,
3747 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3749 spin_lock(&clp->cl_lock);
3750 clp->cl_lease_time = fsinfo.lease_time * HZ;
3751 clp->cl_last_renewal = now;
3752 spin_unlock(&clp->cl_lock);
3757 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3758 struct nfs4_setclientid_res *arg,
3759 struct rpc_cred *cred)
3764 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
3768 case -NFS4ERR_RESOURCE:
3769 /* The IBM lawyers misread another document! */
3770 case -NFS4ERR_DELAY:
3771 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3777 struct nfs4_delegreturndata {
3778 struct nfs4_delegreturnargs args;
3779 struct nfs4_delegreturnres res;
3781 nfs4_stateid stateid;
3782 unsigned long timestamp;
3783 struct nfs_fattr fattr;
3787 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3789 struct nfs4_delegreturndata *data = calldata;
3791 if (!nfs4_sequence_done(task, &data->res.seq_res))
3794 switch (task->tk_status) {
3795 case -NFS4ERR_STALE_STATEID:
3796 case -NFS4ERR_EXPIRED:
3798 renew_lease(data->res.server, data->timestamp);
3801 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3803 nfs_restart_rpc(task, data->res.server->nfs_client);
3807 data->rpc_status = task->tk_status;
3810 static void nfs4_delegreturn_release(void *calldata)
3815 #if defined(CONFIG_NFS_V4_1)
3816 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3818 struct nfs4_delegreturndata *d_data;
3820 d_data = (struct nfs4_delegreturndata *)data;
3822 if (nfs4_setup_sequence(d_data->res.server,
3823 &d_data->args.seq_args,
3824 &d_data->res.seq_res, 1, task))
3826 rpc_call_start(task);
3828 #endif /* CONFIG_NFS_V4_1 */
3830 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3831 #if defined(CONFIG_NFS_V4_1)
3832 .rpc_call_prepare = nfs4_delegreturn_prepare,
3833 #endif /* CONFIG_NFS_V4_1 */
3834 .rpc_call_done = nfs4_delegreturn_done,
3835 .rpc_release = nfs4_delegreturn_release,
3838 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3840 struct nfs4_delegreturndata *data;
3841 struct nfs_server *server = NFS_SERVER(inode);
3842 struct rpc_task *task;
3843 struct rpc_message msg = {
3844 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3847 struct rpc_task_setup task_setup_data = {
3848 .rpc_client = server->client,
3849 .rpc_message = &msg,
3850 .callback_ops = &nfs4_delegreturn_ops,
3851 .flags = RPC_TASK_ASYNC,
3855 data = kzalloc(sizeof(*data), GFP_NOFS);
3858 data->args.fhandle = &data->fh;
3859 data->args.stateid = &data->stateid;
3860 data->args.bitmask = server->attr_bitmask;
3861 nfs_copy_fh(&data->fh, NFS_FH(inode));
3862 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3863 data->res.fattr = &data->fattr;
3864 data->res.server = server;
3865 nfs_fattr_init(data->res.fattr);
3866 data->timestamp = jiffies;
3867 data->rpc_status = 0;
3869 task_setup_data.callback_data = data;
3870 msg.rpc_argp = &data->args;
3871 msg.rpc_resp = &data->res;
3872 task = rpc_run_task(&task_setup_data);
3874 return PTR_ERR(task);
3877 status = nfs4_wait_for_completion_rpc_task(task);
3880 status = data->rpc_status;
3883 nfs_refresh_inode(inode, &data->fattr);
3889 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3891 struct nfs_server *server = NFS_SERVER(inode);
3892 struct nfs4_exception exception = { };
3895 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
3897 case -NFS4ERR_STALE_STATEID:
3898 case -NFS4ERR_EXPIRED:
3902 err = nfs4_handle_exception(server, err, &exception);
3903 } while (exception.retry);
3907 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3908 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3911 * sleep, with exponential backoff, and retry the LOCK operation.
3913 static unsigned long
3914 nfs4_set_lock_task_retry(unsigned long timeout)
3916 schedule_timeout_killable(timeout);
3918 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3919 return NFS4_LOCK_MAXTIMEOUT;
3923 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3925 struct inode *inode = state->inode;
3926 struct nfs_server *server = NFS_SERVER(inode);
3927 struct nfs_client *clp = server->nfs_client;
3928 struct nfs_lockt_args arg = {
3929 .fh = NFS_FH(inode),
3932 struct nfs_lockt_res res = {
3935 struct rpc_message msg = {
3936 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3939 .rpc_cred = state->owner->so_cred,
3941 struct nfs4_lock_state *lsp;
3944 arg.lock_owner.clientid = clp->cl_clientid;
3945 status = nfs4_set_lock_state(state, request);
3948 lsp = request->fl_u.nfs4_fl.owner;
3949 arg.lock_owner.id = lsp->ls_id.id;
3950 arg.lock_owner.s_dev = server->s_dev;
3951 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3954 request->fl_type = F_UNLCK;
3956 case -NFS4ERR_DENIED:
3959 request->fl_ops->fl_release_private(request);
3964 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3966 struct nfs4_exception exception = { };
3970 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3971 _nfs4_proc_getlk(state, cmd, request),
3973 } while (exception.retry);
3977 static int do_vfs_lock(struct file *file, struct file_lock *fl)
3980 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3982 res = posix_lock_file_wait(file, fl);
3985 res = flock_lock_file_wait(file, fl);
3993 struct nfs4_unlockdata {
3994 struct nfs_locku_args arg;
3995 struct nfs_locku_res res;
3996 struct nfs4_lock_state *lsp;
3997 struct nfs_open_context *ctx;
3998 struct file_lock fl;
3999 const struct nfs_server *server;
4000 unsigned long timestamp;
4003 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4004 struct nfs_open_context *ctx,
4005 struct nfs4_lock_state *lsp,
4006 struct nfs_seqid *seqid)
4008 struct nfs4_unlockdata *p;
4009 struct inode *inode = lsp->ls_state->inode;
4011 p = kzalloc(sizeof(*p), GFP_NOFS);
4014 p->arg.fh = NFS_FH(inode);
4016 p->arg.seqid = seqid;
4017 p->res.seqid = seqid;
4018 p->arg.stateid = &lsp->ls_stateid;
4020 atomic_inc(&lsp->ls_count);
4021 /* Ensure we don't close file until we're done freeing locks! */
4022 p->ctx = get_nfs_open_context(ctx);
4023 memcpy(&p->fl, fl, sizeof(p->fl));
4024 p->server = NFS_SERVER(inode);
4028 static void nfs4_locku_release_calldata(void *data)
4030 struct nfs4_unlockdata *calldata = data;
4031 nfs_free_seqid(calldata->arg.seqid);
4032 nfs4_put_lock_state(calldata->lsp);
4033 put_nfs_open_context(calldata->ctx);
4037 static void nfs4_locku_done(struct rpc_task *task, void *data)
4039 struct nfs4_unlockdata *calldata = data;
4041 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4043 switch (task->tk_status) {
4045 memcpy(calldata->lsp->ls_stateid.data,
4046 calldata->res.stateid.data,
4047 sizeof(calldata->lsp->ls_stateid.data));
4048 renew_lease(calldata->server, calldata->timestamp);
4050 case -NFS4ERR_BAD_STATEID:
4051 case -NFS4ERR_OLD_STATEID:
4052 case -NFS4ERR_STALE_STATEID:
4053 case -NFS4ERR_EXPIRED:
4056 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4057 nfs_restart_rpc(task,
4058 calldata->server->nfs_client);
4062 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4064 struct nfs4_unlockdata *calldata = data;
4066 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4068 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
4069 /* Note: exit _without_ running nfs4_locku_done */
4070 task->tk_action = NULL;
4073 calldata->timestamp = jiffies;
4074 if (nfs4_setup_sequence(calldata->server,
4075 &calldata->arg.seq_args,
4076 &calldata->res.seq_res, 1, task))
4078 rpc_call_start(task);
4081 static const struct rpc_call_ops nfs4_locku_ops = {
4082 .rpc_call_prepare = nfs4_locku_prepare,
4083 .rpc_call_done = nfs4_locku_done,
4084 .rpc_release = nfs4_locku_release_calldata,
4087 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4088 struct nfs_open_context *ctx,
4089 struct nfs4_lock_state *lsp,
4090 struct nfs_seqid *seqid)
4092 struct nfs4_unlockdata *data;
4093 struct rpc_message msg = {
4094 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4095 .rpc_cred = ctx->cred,
4097 struct rpc_task_setup task_setup_data = {
4098 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4099 .rpc_message = &msg,
4100 .callback_ops = &nfs4_locku_ops,
4101 .workqueue = nfsiod_workqueue,
4102 .flags = RPC_TASK_ASYNC,
4105 /* Ensure this is an unlock - when canceling a lock, the
4106 * canceled lock is passed in, and it won't be an unlock.
4108 fl->fl_type = F_UNLCK;
4110 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4112 nfs_free_seqid(seqid);
4113 return ERR_PTR(-ENOMEM);
4116 msg.rpc_argp = &data->arg;
4117 msg.rpc_resp = &data->res;
4118 task_setup_data.callback_data = data;
4119 return rpc_run_task(&task_setup_data);
4122 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4124 struct nfs_inode *nfsi = NFS_I(state->inode);
4125 struct nfs_seqid *seqid;
4126 struct nfs4_lock_state *lsp;
4127 struct rpc_task *task;
4129 unsigned char fl_flags = request->fl_flags;
4131 status = nfs4_set_lock_state(state, request);
4132 /* Unlock _before_ we do the RPC call */
4133 request->fl_flags |= FL_EXISTS;
4134 down_read(&nfsi->rwsem);
4135 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4136 up_read(&nfsi->rwsem);
4139 up_read(&nfsi->rwsem);
4142 /* Is this a delegated lock? */
4143 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4145 lsp = request->fl_u.nfs4_fl.owner;
4146 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4150 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4151 status = PTR_ERR(task);
4154 status = nfs4_wait_for_completion_rpc_task(task);
4157 request->fl_flags = fl_flags;
4161 struct nfs4_lockdata {
4162 struct nfs_lock_args arg;
4163 struct nfs_lock_res res;
4164 struct nfs4_lock_state *lsp;
4165 struct nfs_open_context *ctx;
4166 struct file_lock fl;
4167 unsigned long timestamp;
4170 struct nfs_server *server;
4173 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4174 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4177 struct nfs4_lockdata *p;
4178 struct inode *inode = lsp->ls_state->inode;
4179 struct nfs_server *server = NFS_SERVER(inode);
4181 p = kzalloc(sizeof(*p), gfp_mask);
4185 p->arg.fh = NFS_FH(inode);
4187 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4188 if (p->arg.open_seqid == NULL)
4190 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4191 if (p->arg.lock_seqid == NULL)
4192 goto out_free_seqid;
4193 p->arg.lock_stateid = &lsp->ls_stateid;
4194 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4195 p->arg.lock_owner.id = lsp->ls_id.id;
4196 p->arg.lock_owner.s_dev = server->s_dev;
4197 p->res.lock_seqid = p->arg.lock_seqid;
4200 atomic_inc(&lsp->ls_count);
4201 p->ctx = get_nfs_open_context(ctx);
4202 memcpy(&p->fl, fl, sizeof(p->fl));
4205 nfs_free_seqid(p->arg.open_seqid);
4211 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4213 struct nfs4_lockdata *data = calldata;
4214 struct nfs4_state *state = data->lsp->ls_state;
4216 dprintk("%s: begin!\n", __func__);
4217 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4219 /* Do we need to do an open_to_lock_owner? */
4220 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4221 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4223 data->arg.open_stateid = &state->stateid;
4224 data->arg.new_lock_owner = 1;
4225 data->res.open_seqid = data->arg.open_seqid;
4227 data->arg.new_lock_owner = 0;
4228 data->timestamp = jiffies;
4229 if (nfs4_setup_sequence(data->server,
4230 &data->arg.seq_args,
4231 &data->res.seq_res, 1, task))
4233 rpc_call_start(task);
4234 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4237 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4239 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4240 nfs4_lock_prepare(task, calldata);
4243 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4245 struct nfs4_lockdata *data = calldata;
4247 dprintk("%s: begin!\n", __func__);
4249 if (!nfs4_sequence_done(task, &data->res.seq_res))
4252 data->rpc_status = task->tk_status;
4253 if (data->arg.new_lock_owner != 0) {
4254 if (data->rpc_status == 0)
4255 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4259 if (data->rpc_status == 0) {
4260 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4261 sizeof(data->lsp->ls_stateid.data));
4262 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4263 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
4266 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4269 static void nfs4_lock_release(void *calldata)
4271 struct nfs4_lockdata *data = calldata;
4273 dprintk("%s: begin!\n", __func__);
4274 nfs_free_seqid(data->arg.open_seqid);
4275 if (data->cancelled != 0) {
4276 struct rpc_task *task;
4277 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4278 data->arg.lock_seqid);
4280 rpc_put_task_async(task);
4281 dprintk("%s: cancelling lock!\n", __func__);
4283 nfs_free_seqid(data->arg.lock_seqid);
4284 nfs4_put_lock_state(data->lsp);
4285 put_nfs_open_context(data->ctx);
4287 dprintk("%s: done!\n", __func__);
4290 static const struct rpc_call_ops nfs4_lock_ops = {
4291 .rpc_call_prepare = nfs4_lock_prepare,
4292 .rpc_call_done = nfs4_lock_done,
4293 .rpc_release = nfs4_lock_release,
4296 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4297 .rpc_call_prepare = nfs4_recover_lock_prepare,
4298 .rpc_call_done = nfs4_lock_done,
4299 .rpc_release = nfs4_lock_release,
4302 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4305 case -NFS4ERR_ADMIN_REVOKED:
4306 case -NFS4ERR_BAD_STATEID:
4307 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4308 if (new_lock_owner != 0 ||
4309 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4310 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4312 case -NFS4ERR_STALE_STATEID:
4313 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4314 case -NFS4ERR_EXPIRED:
4315 nfs4_schedule_lease_recovery(server->nfs_client);
4319 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4321 struct nfs4_lockdata *data;
4322 struct rpc_task *task;
4323 struct rpc_message msg = {
4324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4325 .rpc_cred = state->owner->so_cred,
4327 struct rpc_task_setup task_setup_data = {
4328 .rpc_client = NFS_CLIENT(state->inode),
4329 .rpc_message = &msg,
4330 .callback_ops = &nfs4_lock_ops,
4331 .workqueue = nfsiod_workqueue,
4332 .flags = RPC_TASK_ASYNC,
4336 dprintk("%s: begin!\n", __func__);
4337 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4338 fl->fl_u.nfs4_fl.owner,
4339 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4343 data->arg.block = 1;
4344 if (recovery_type > NFS_LOCK_NEW) {
4345 if (recovery_type == NFS_LOCK_RECLAIM)
4346 data->arg.reclaim = NFS_LOCK_RECLAIM;
4347 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4349 msg.rpc_argp = &data->arg;
4350 msg.rpc_resp = &data->res;
4351 task_setup_data.callback_data = data;
4352 task = rpc_run_task(&task_setup_data);
4354 return PTR_ERR(task);
4355 ret = nfs4_wait_for_completion_rpc_task(task);
4357 ret = data->rpc_status;
4359 nfs4_handle_setlk_error(data->server, data->lsp,
4360 data->arg.new_lock_owner, ret);
4362 data->cancelled = 1;
4364 dprintk("%s: done, ret = %d!\n", __func__, ret);
4368 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4370 struct nfs_server *server = NFS_SERVER(state->inode);
4371 struct nfs4_exception exception = { };
4375 /* Cache the lock if possible... */
4376 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4378 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4379 if (err != -NFS4ERR_DELAY)
4381 nfs4_handle_exception(server, err, &exception);
4382 } while (exception.retry);
4386 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4388 struct nfs_server *server = NFS_SERVER(state->inode);
4389 struct nfs4_exception exception = { };
4392 err = nfs4_set_lock_state(state, request);
4396 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4398 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4402 case -NFS4ERR_GRACE:
4403 case -NFS4ERR_DELAY:
4404 nfs4_handle_exception(server, err, &exception);
4407 } while (exception.retry);
4412 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4414 struct nfs_inode *nfsi = NFS_I(state->inode);
4415 unsigned char fl_flags = request->fl_flags;
4416 int status = -ENOLCK;
4418 if ((fl_flags & FL_POSIX) &&
4419 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4421 /* Is this a delegated open? */
4422 status = nfs4_set_lock_state(state, request);
4425 request->fl_flags |= FL_ACCESS;
4426 status = do_vfs_lock(request->fl_file, request);
4429 down_read(&nfsi->rwsem);
4430 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4431 /* Yes: cache locks! */
4432 /* ...but avoid races with delegation recall... */
4433 request->fl_flags = fl_flags & ~FL_SLEEP;
4434 status = do_vfs_lock(request->fl_file, request);
4437 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4440 /* Note: we always want to sleep here! */
4441 request->fl_flags = fl_flags | FL_SLEEP;
4442 if (do_vfs_lock(request->fl_file, request) < 0)
4443 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
4445 up_read(&nfsi->rwsem);
4447 request->fl_flags = fl_flags;
4451 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4453 struct nfs4_exception exception = { };
4457 err = _nfs4_proc_setlk(state, cmd, request);
4458 if (err == -NFS4ERR_DENIED)
4460 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4462 } while (exception.retry);
4467 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4469 struct nfs_open_context *ctx;
4470 struct nfs4_state *state;
4471 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4474 /* verify open state */
4475 ctx = nfs_file_open_context(filp);
4478 if (request->fl_start < 0 || request->fl_end < 0)
4481 if (IS_GETLK(cmd)) {
4483 return nfs4_proc_getlk(state, F_GETLK, request);
4487 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4490 if (request->fl_type == F_UNLCK) {
4492 return nfs4_proc_unlck(state, cmd, request);
4499 status = nfs4_proc_setlk(state, cmd, request);
4500 if ((status != -EAGAIN) || IS_SETLK(cmd))
4502 timeout = nfs4_set_lock_task_retry(timeout);
4503 status = -ERESTARTSYS;
4506 } while(status < 0);
4510 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4512 struct nfs_server *server = NFS_SERVER(state->inode);
4513 struct nfs4_exception exception = { };
4516 err = nfs4_set_lock_state(state, fl);
4520 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4523 printk(KERN_ERR "%s: unhandled error %d.\n",
4528 case -NFS4ERR_EXPIRED:
4529 case -NFS4ERR_STALE_CLIENTID:
4530 case -NFS4ERR_STALE_STATEID:
4531 nfs4_schedule_lease_recovery(server->nfs_client);
4533 case -NFS4ERR_BADSESSION:
4534 case -NFS4ERR_BADSLOT:
4535 case -NFS4ERR_BAD_HIGH_SLOT:
4536 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4537 case -NFS4ERR_DEADSESSION:
4538 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
4542 * The show must go on: exit, but mark the
4543 * stateid as needing recovery.
4545 case -NFS4ERR_ADMIN_REVOKED:
4546 case -NFS4ERR_BAD_STATEID:
4547 case -NFS4ERR_OPENMODE:
4548 nfs4_schedule_stateid_recovery(server, state);
4553 * User RPCSEC_GSS context has expired.
4554 * We cannot recover this stateid now, so
4555 * skip it and allow recovery thread to
4561 case -NFS4ERR_DENIED:
4562 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4565 case -NFS4ERR_DELAY:
4568 err = nfs4_handle_exception(server, err, &exception);
4569 } while (exception.retry);
4574 static void nfs4_release_lockowner_release(void *calldata)
4579 const struct rpc_call_ops nfs4_release_lockowner_ops = {
4580 .rpc_release = nfs4_release_lockowner_release,
4583 void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4585 struct nfs_server *server = lsp->ls_state->owner->so_server;
4586 struct nfs_release_lockowner_args *args;
4587 struct rpc_message msg = {
4588 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4591 if (server->nfs_client->cl_mvops->minor_version != 0)
4593 args = kmalloc(sizeof(*args), GFP_NOFS);
4596 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4597 args->lock_owner.id = lsp->ls_id.id;
4598 args->lock_owner.s_dev = server->s_dev;
4599 msg.rpc_argp = args;
4600 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4603 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4605 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4606 const void *buf, size_t buflen,
4607 int flags, int type)
4609 if (strcmp(key, "") != 0)
4612 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
4615 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4616 void *buf, size_t buflen, int type)
4618 if (strcmp(key, "") != 0)
4621 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
4624 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4625 size_t list_len, const char *name,
4626 size_t name_len, int type)
4628 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
4630 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4633 if (list && len <= list_len)
4634 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
4638 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4640 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4641 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4642 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4645 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4646 NFS_ATTR_FATTR_NLINK;
4647 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4651 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4652 struct nfs4_fs_locations *fs_locations, struct page *page)
4654 struct nfs_server *server = NFS_SERVER(dir);
4656 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4657 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
4659 struct nfs4_fs_locations_arg args = {
4660 .dir_fh = NFS_FH(dir),
4665 struct nfs4_fs_locations_res res = {
4666 .fs_locations = fs_locations,
4668 struct rpc_message msg = {
4669 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4675 dprintk("%s: start\n", __func__);
4676 nfs_fattr_init(&fs_locations->fattr);
4677 fs_locations->server = server;
4678 fs_locations->nlocations = 0;
4679 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4680 nfs_fixup_referral_attributes(&fs_locations->fattr);
4681 dprintk("%s: returned status = %d\n", __func__, status);
4685 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4688 struct nfs4_secinfo_arg args = {
4689 .dir_fh = NFS_FH(dir),
4692 struct nfs4_secinfo_res res = {
4695 struct rpc_message msg = {
4696 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4701 dprintk("NFS call secinfo %s\n", name->name);
4702 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4703 dprintk("NFS reply secinfo: %d\n", status);
4707 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4709 struct nfs4_exception exception = { };
4712 err = nfs4_handle_exception(NFS_SERVER(dir),
4713 _nfs4_proc_secinfo(dir, name, flavors),
4715 } while (exception.retry);
4719 #ifdef CONFIG_NFS_V4_1
4721 * Check the exchange flags returned by the server for invalid flags, having
4722 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4725 static int nfs4_check_cl_exchange_flags(u32 flags)
4727 if (flags & ~EXCHGID4_FLAG_MASK_R)
4729 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4730 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4732 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4736 return -NFS4ERR_INVAL;
4740 * nfs4_proc_exchange_id()
4742 * Since the clientid has expired, all compounds using sessions
4743 * associated with the stale clientid will be returning
4744 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4745 * be in some phase of session reset.
4747 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4749 nfs4_verifier verifier;
4750 struct nfs41_exchange_id_args args = {
4752 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
4754 struct nfs41_exchange_id_res res = {
4758 struct rpc_message msg = {
4759 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4766 dprintk("--> %s\n", __func__);
4767 BUG_ON(clp == NULL);
4769 p = (u32 *)verifier.data;
4770 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4771 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4772 args.verifier = &verifier;
4774 args.id_len = scnprintf(args.id, sizeof(args.id),
4777 init_utsname()->nodename,
4778 init_utsname()->domainname,
4779 clp->cl_rpcclient->cl_auth->au_flavor);
4781 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4783 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
4784 dprintk("<-- %s status= %d\n", __func__, status);
4788 struct nfs4_get_lease_time_data {
4789 struct nfs4_get_lease_time_args *args;
4790 struct nfs4_get_lease_time_res *res;
4791 struct nfs_client *clp;
4794 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4798 struct nfs4_get_lease_time_data *data =
4799 (struct nfs4_get_lease_time_data *)calldata;
4801 dprintk("--> %s\n", __func__);
4802 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4803 /* just setup sequence, do not trigger session recovery
4804 since we're invoked within one */
4805 ret = nfs41_setup_sequence(data->clp->cl_session,
4806 &data->args->la_seq_args,
4807 &data->res->lr_seq_res, 0, task);
4809 BUG_ON(ret == -EAGAIN);
4810 rpc_call_start(task);
4811 dprintk("<-- %s\n", __func__);
4815 * Called from nfs4_state_manager thread for session setup, so don't recover
4816 * from sequence operation or clientid errors.
4818 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4820 struct nfs4_get_lease_time_data *data =
4821 (struct nfs4_get_lease_time_data *)calldata;
4823 dprintk("--> %s\n", __func__);
4824 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4826 switch (task->tk_status) {
4827 case -NFS4ERR_DELAY:
4828 case -NFS4ERR_GRACE:
4829 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4830 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4831 task->tk_status = 0;
4832 nfs_restart_rpc(task, data->clp);
4835 dprintk("<-- %s\n", __func__);
4838 struct rpc_call_ops nfs4_get_lease_time_ops = {
4839 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4840 .rpc_call_done = nfs4_get_lease_time_done,
4843 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4845 struct rpc_task *task;
4846 struct nfs4_get_lease_time_args args;
4847 struct nfs4_get_lease_time_res res = {
4848 .lr_fsinfo = fsinfo,
4850 struct nfs4_get_lease_time_data data = {
4855 struct rpc_message msg = {
4856 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4860 struct rpc_task_setup task_setup = {
4861 .rpc_client = clp->cl_rpcclient,
4862 .rpc_message = &msg,
4863 .callback_ops = &nfs4_get_lease_time_ops,
4864 .callback_data = &data
4868 dprintk("--> %s\n", __func__);
4869 task = rpc_run_task(&task_setup);
4872 status = PTR_ERR(task);
4874 status = task->tk_status;
4877 dprintk("<-- %s return %d\n", __func__, status);
4883 * Reset a slot table
4885 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4888 struct nfs4_slot *new = NULL;
4892 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4893 max_reqs, tbl->max_slots);
4895 /* Does the newly negotiated max_reqs match the existing slot table? */
4896 if (max_reqs != tbl->max_slots) {
4898 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
4905 spin_lock(&tbl->slot_tbl_lock);
4908 tbl->max_slots = max_reqs;
4910 for (i = 0; i < tbl->max_slots; ++i)
4911 tbl->slots[i].seq_nr = ivalue;
4912 spin_unlock(&tbl->slot_tbl_lock);
4913 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4914 tbl, tbl->slots, tbl->max_slots);
4916 dprintk("<-- %s: return %d\n", __func__, ret);
4921 * Reset the forechannel and backchannel slot tables
4923 static int nfs4_reset_slot_tables(struct nfs4_session *session)
4927 status = nfs4_reset_slot_table(&session->fc_slot_table,
4928 session->fc_attrs.max_reqs, 1);
4932 status = nfs4_reset_slot_table(&session->bc_slot_table,
4933 session->bc_attrs.max_reqs, 0);
4937 /* Destroy the slot table */
4938 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4940 if (session->fc_slot_table.slots != NULL) {
4941 kfree(session->fc_slot_table.slots);
4942 session->fc_slot_table.slots = NULL;
4944 if (session->bc_slot_table.slots != NULL) {
4945 kfree(session->bc_slot_table.slots);
4946 session->bc_slot_table.slots = NULL;
4952 * Initialize slot table
4954 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4955 int max_slots, int ivalue)
4957 struct nfs4_slot *slot;
4960 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4962 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
4964 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
4969 spin_lock(&tbl->slot_tbl_lock);
4970 tbl->max_slots = max_slots;
4972 tbl->highest_used_slotid = -1; /* no slot is currently used */
4973 spin_unlock(&tbl->slot_tbl_lock);
4974 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4975 tbl, tbl->slots, tbl->max_slots);
4977 dprintk("<-- %s: return %d\n", __func__, ret);
4982 * Initialize the forechannel and backchannel tables
4984 static int nfs4_init_slot_tables(struct nfs4_session *session)
4986 struct nfs4_slot_table *tbl;
4989 tbl = &session->fc_slot_table;
4990 if (tbl->slots == NULL) {
4991 status = nfs4_init_slot_table(tbl,
4992 session->fc_attrs.max_reqs, 1);
4997 tbl = &session->bc_slot_table;
4998 if (tbl->slots == NULL) {
4999 status = nfs4_init_slot_table(tbl,
5000 session->bc_attrs.max_reqs, 0);
5002 nfs4_destroy_slot_tables(session);
5008 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5010 struct nfs4_session *session;
5011 struct nfs4_slot_table *tbl;
5013 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5017 tbl = &session->fc_slot_table;
5018 tbl->highest_used_slotid = -1;
5019 spin_lock_init(&tbl->slot_tbl_lock);
5020 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5021 init_completion(&tbl->complete);
5023 tbl = &session->bc_slot_table;
5024 tbl->highest_used_slotid = -1;
5025 spin_lock_init(&tbl->slot_tbl_lock);
5026 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5027 init_completion(&tbl->complete);
5029 session->session_state = 1<<NFS4_SESSION_INITING;
5035 void nfs4_destroy_session(struct nfs4_session *session)
5037 nfs4_proc_destroy_session(session);
5038 dprintk("%s Destroy backchannel for xprt %p\n",
5039 __func__, session->clp->cl_rpcclient->cl_xprt);
5040 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5041 NFS41_BC_MIN_CALLBACKS);
5042 nfs4_destroy_slot_tables(session);
5047 * Initialize the values to be used by the client in CREATE_SESSION
5048 * If nfs4_init_session set the fore channel request and response sizes,
5051 * Set the back channel max_resp_sz_cached to zero to force the client to
5052 * always set csa_cachethis to FALSE because the current implementation
5053 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5055 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5057 struct nfs4_session *session = args->client->cl_session;
5058 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5059 mxresp_sz = session->fc_attrs.max_resp_sz;
5062 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5064 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5065 /* Fore channel attributes */
5066 args->fc_attrs.headerpadsz = 0;
5067 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5068 args->fc_attrs.max_resp_sz = mxresp_sz;
5069 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5070 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5072 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5073 "max_ops=%u max_reqs=%u\n",
5075 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5076 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5078 /* Back channel attributes */
5079 args->bc_attrs.headerpadsz = 0;
5080 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5081 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5082 args->bc_attrs.max_resp_sz_cached = 0;
5083 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5084 args->bc_attrs.max_reqs = 1;
5086 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5087 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5089 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5090 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5091 args->bc_attrs.max_reqs);
5094 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5096 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5097 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5099 if (rcvd->headerpadsz > sent->headerpadsz)
5101 if (rcvd->max_resp_sz > sent->max_resp_sz)
5104 * Our requested max_ops is the minimum we need; we're not
5105 * prepared to break up compounds into smaller pieces than that.
5106 * So, no point even trying to continue if the server won't
5109 if (rcvd->max_ops < sent->max_ops)
5111 if (rcvd->max_reqs == 0)
5116 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5118 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5119 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5121 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5123 if (rcvd->max_resp_sz < sent->max_resp_sz)
5125 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5127 /* These would render the backchannel useless: */
5128 if (rcvd->max_ops == 0)
5130 if (rcvd->max_reqs == 0)
5135 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5136 struct nfs4_session *session)
5140 ret = nfs4_verify_fore_channel_attrs(args, session);
5143 return nfs4_verify_back_channel_attrs(args, session);
5146 static int _nfs4_proc_create_session(struct nfs_client *clp)
5148 struct nfs4_session *session = clp->cl_session;
5149 struct nfs41_create_session_args args = {
5151 .cb_program = NFS4_CALLBACK,
5153 struct nfs41_create_session_res res = {
5156 struct rpc_message msg = {
5157 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5163 nfs4_init_channel_attrs(&args);
5164 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5166 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5169 /* Verify the session's negotiated channel_attrs values */
5170 status = nfs4_verify_channel_attrs(&args, session);
5172 /* Increment the clientid slot sequence id */
5180 * Issues a CREATE_SESSION operation to the server.
5181 * It is the responsibility of the caller to verify the session is
5182 * expired before calling this routine.
5184 int nfs4_proc_create_session(struct nfs_client *clp)
5188 struct nfs4_session *session = clp->cl_session;
5192 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5195 status = _nfs4_proc_create_session(clp);
5196 if (status == -NFS4ERR_DELAY) {
5197 err = nfs4_delay(clp->cl_rpcclient, &timeout);
5201 } while (status == -NFS4ERR_DELAY);
5206 /* Init and reset the fore channel */
5207 status = nfs4_init_slot_tables(session);
5208 dprintk("slot table initialization returned %d\n", status);
5211 status = nfs4_reset_slot_tables(session);
5212 dprintk("slot table reset returned %d\n", status);
5216 ptr = (unsigned *)&session->sess_id.data[0];
5217 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5218 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5220 dprintk("<-- %s\n", __func__);
5225 * Issue the over-the-wire RPC DESTROY_SESSION.
5226 * The caller must serialize access to this routine.
5228 int nfs4_proc_destroy_session(struct nfs4_session *session)
5231 struct rpc_message msg;
5233 dprintk("--> nfs4_proc_destroy_session\n");
5235 /* session is still being setup */
5236 if (session->clp->cl_cons_state != NFS_CS_READY)
5239 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5240 msg.rpc_argp = session;
5241 msg.rpc_resp = NULL;
5242 msg.rpc_cred = NULL;
5243 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5247 "Got error %d from the server on DESTROY_SESSION. "
5248 "Session has been destroyed regardless...\n", status);
5250 dprintk("<-- nfs4_proc_destroy_session\n");
5254 int nfs4_init_session(struct nfs_server *server)
5256 struct nfs_client *clp = server->nfs_client;
5257 struct nfs4_session *session;
5258 unsigned int rsize, wsize;
5261 if (!nfs4_has_session(clp))
5264 session = clp->cl_session;
5265 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5268 rsize = server->rsize;
5270 rsize = NFS_MAX_FILE_IO_SIZE;
5271 wsize = server->wsize;
5273 wsize = NFS_MAX_FILE_IO_SIZE;
5275 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5276 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5278 ret = nfs4_recover_expired_lease(server);
5280 ret = nfs4_check_client_ready(clp);
5284 int nfs4_init_ds_session(struct nfs_client *clp)
5286 struct nfs4_session *session = clp->cl_session;
5289 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5292 ret = nfs4_client_recover_expired_lease(clp);
5294 /* Test for the DS role */
5295 if (!is_ds_client(clp))
5298 ret = nfs4_check_client_ready(clp);
5302 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5306 * Renew the cl_session lease.
5308 struct nfs4_sequence_data {
5309 struct nfs_client *clp;
5310 struct nfs4_sequence_args args;
5311 struct nfs4_sequence_res res;
5314 static void nfs41_sequence_release(void *data)
5316 struct nfs4_sequence_data *calldata = data;
5317 struct nfs_client *clp = calldata->clp;
5319 if (atomic_read(&clp->cl_count) > 1)
5320 nfs4_schedule_state_renewal(clp);
5321 nfs_put_client(clp);
5325 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5327 switch(task->tk_status) {
5328 case -NFS4ERR_DELAY:
5329 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5332 nfs4_schedule_lease_recovery(clp);
5337 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5339 struct nfs4_sequence_data *calldata = data;
5340 struct nfs_client *clp = calldata->clp;
5342 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5345 if (task->tk_status < 0) {
5346 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5347 if (atomic_read(&clp->cl_count) == 1)
5350 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5351 rpc_restart_call_prepare(task);
5355 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5357 dprintk("<-- %s\n", __func__);
5360 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5362 struct nfs4_sequence_data *calldata = data;
5363 struct nfs_client *clp = calldata->clp;
5364 struct nfs4_sequence_args *args;
5365 struct nfs4_sequence_res *res;
5367 args = task->tk_msg.rpc_argp;
5368 res = task->tk_msg.rpc_resp;
5370 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
5372 rpc_call_start(task);
5375 static const struct rpc_call_ops nfs41_sequence_ops = {
5376 .rpc_call_done = nfs41_sequence_call_done,
5377 .rpc_call_prepare = nfs41_sequence_prepare,
5378 .rpc_release = nfs41_sequence_release,
5381 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5383 struct nfs4_sequence_data *calldata;
5384 struct rpc_message msg = {
5385 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5388 struct rpc_task_setup task_setup_data = {
5389 .rpc_client = clp->cl_rpcclient,
5390 .rpc_message = &msg,
5391 .callback_ops = &nfs41_sequence_ops,
5392 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5395 if (!atomic_inc_not_zero(&clp->cl_count))
5396 return ERR_PTR(-EIO);
5397 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5398 if (calldata == NULL) {
5399 nfs_put_client(clp);
5400 return ERR_PTR(-ENOMEM);
5402 msg.rpc_argp = &calldata->args;
5403 msg.rpc_resp = &calldata->res;
5404 calldata->clp = clp;
5405 task_setup_data.callback_data = calldata;
5407 return rpc_run_task(&task_setup_data);
5410 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5412 struct rpc_task *task;
5415 task = _nfs41_proc_sequence(clp, cred);
5417 ret = PTR_ERR(task);
5419 rpc_put_task_async(task);
5420 dprintk("<-- %s status=%d\n", __func__, ret);
5424 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5426 struct rpc_task *task;
5429 task = _nfs41_proc_sequence(clp, cred);
5431 ret = PTR_ERR(task);
5434 ret = rpc_wait_for_completion_task(task);
5436 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5438 if (task->tk_status == 0)
5439 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5440 ret = task->tk_status;
5444 dprintk("<-- %s status=%d\n", __func__, ret);
5448 struct nfs4_reclaim_complete_data {
5449 struct nfs_client *clp;
5450 struct nfs41_reclaim_complete_args arg;
5451 struct nfs41_reclaim_complete_res res;
5454 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5456 struct nfs4_reclaim_complete_data *calldata = data;
5458 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5459 if (nfs41_setup_sequence(calldata->clp->cl_session,
5460 &calldata->arg.seq_args,
5461 &calldata->res.seq_res, 0, task))
5464 rpc_call_start(task);
5467 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5469 switch(task->tk_status) {
5471 case -NFS4ERR_COMPLETE_ALREADY:
5472 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5474 case -NFS4ERR_DELAY:
5475 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5478 nfs4_schedule_lease_recovery(clp);
5483 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5485 struct nfs4_reclaim_complete_data *calldata = data;
5486 struct nfs_client *clp = calldata->clp;
5487 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5489 dprintk("--> %s\n", __func__);
5490 if (!nfs41_sequence_done(task, res))
5493 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5494 rpc_restart_call_prepare(task);
5497 dprintk("<-- %s\n", __func__);
5500 static void nfs4_free_reclaim_complete_data(void *data)
5502 struct nfs4_reclaim_complete_data *calldata = data;
5507 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5508 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5509 .rpc_call_done = nfs4_reclaim_complete_done,
5510 .rpc_release = nfs4_free_reclaim_complete_data,
5514 * Issue a global reclaim complete.
5516 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5518 struct nfs4_reclaim_complete_data *calldata;
5519 struct rpc_task *task;
5520 struct rpc_message msg = {
5521 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5523 struct rpc_task_setup task_setup_data = {
5524 .rpc_client = clp->cl_rpcclient,
5525 .rpc_message = &msg,
5526 .callback_ops = &nfs4_reclaim_complete_call_ops,
5527 .flags = RPC_TASK_ASYNC,
5529 int status = -ENOMEM;
5531 dprintk("--> %s\n", __func__);
5532 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5533 if (calldata == NULL)
5535 calldata->clp = clp;
5536 calldata->arg.one_fs = 0;
5538 msg.rpc_argp = &calldata->arg;
5539 msg.rpc_resp = &calldata->res;
5540 task_setup_data.callback_data = calldata;
5541 task = rpc_run_task(&task_setup_data);
5543 status = PTR_ERR(task);
5546 status = nfs4_wait_for_completion_rpc_task(task);
5548 status = task->tk_status;
5552 dprintk("<-- %s status=%d\n", __func__, status);
5557 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5559 struct nfs4_layoutget *lgp = calldata;
5560 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5562 dprintk("--> %s\n", __func__);
5563 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5564 * right now covering the LAYOUTGET we are about to send.
5565 * However, that is not so catastrophic, and there seems
5566 * to be no way to prevent it completely.
5568 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5569 &lgp->res.seq_res, 0, task))
5571 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5572 NFS_I(lgp->args.inode)->layout,
5573 lgp->args.ctx->state)) {
5574 rpc_exit(task, NFS4_OK);
5577 rpc_call_start(task);
5580 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5582 struct nfs4_layoutget *lgp = calldata;
5583 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5585 dprintk("--> %s\n", __func__);
5587 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5590 switch (task->tk_status) {
5593 case -NFS4ERR_LAYOUTTRYLATER:
5594 case -NFS4ERR_RECALLCONFLICT:
5595 task->tk_status = -NFS4ERR_DELAY;
5598 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5599 rpc_restart_call_prepare(task);
5603 dprintk("<-- %s\n", __func__);
5606 static void nfs4_layoutget_release(void *calldata)
5608 struct nfs4_layoutget *lgp = calldata;
5610 dprintk("--> %s\n", __func__);
5611 if (lgp->res.layout.buf != NULL)
5612 free_page((unsigned long) lgp->res.layout.buf);
5613 put_nfs_open_context(lgp->args.ctx);
5615 dprintk("<-- %s\n", __func__);
5618 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5619 .rpc_call_prepare = nfs4_layoutget_prepare,
5620 .rpc_call_done = nfs4_layoutget_done,
5621 .rpc_release = nfs4_layoutget_release,
5624 int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5626 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5627 struct rpc_task *task;
5628 struct rpc_message msg = {
5629 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5630 .rpc_argp = &lgp->args,
5631 .rpc_resp = &lgp->res,
5633 struct rpc_task_setup task_setup_data = {
5634 .rpc_client = server->client,
5635 .rpc_message = &msg,
5636 .callback_ops = &nfs4_layoutget_call_ops,
5637 .callback_data = lgp,
5638 .flags = RPC_TASK_ASYNC,
5642 dprintk("--> %s\n", __func__);
5644 lgp->res.layout.buf = (void *)__get_free_page(GFP_NOFS);
5645 if (lgp->res.layout.buf == NULL) {
5646 nfs4_layoutget_release(lgp);
5650 lgp->res.seq_res.sr_slot = NULL;
5651 task = rpc_run_task(&task_setup_data);
5653 return PTR_ERR(task);
5654 status = nfs4_wait_for_completion_rpc_task(task);
5656 status = task->tk_status;
5658 status = pnfs_layout_process(lgp);
5660 dprintk("<-- %s status=%d\n", __func__, status);
5665 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5667 struct nfs4_getdeviceinfo_args args = {
5670 struct nfs4_getdeviceinfo_res res = {
5673 struct rpc_message msg = {
5674 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5680 dprintk("--> %s\n", __func__);
5681 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5682 dprintk("<-- %s status=%d\n", __func__, status);
5687 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5689 struct nfs4_exception exception = { };
5693 err = nfs4_handle_exception(server,
5694 _nfs4_proc_getdeviceinfo(server, pdev),
5696 } while (exception.retry);
5699 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5701 #endif /* CONFIG_NFS_V4_1 */
5703 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
5704 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5705 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5706 .recover_open = nfs4_open_reclaim,
5707 .recover_lock = nfs4_lock_reclaim,
5708 .establish_clid = nfs4_init_clientid,
5709 .get_clid_cred = nfs4_get_setclientid_cred,
5712 #if defined(CONFIG_NFS_V4_1)
5713 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5714 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5715 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5716 .recover_open = nfs4_open_reclaim,
5717 .recover_lock = nfs4_lock_reclaim,
5718 .establish_clid = nfs41_init_clientid,
5719 .get_clid_cred = nfs4_get_exchange_id_cred,
5720 .reclaim_complete = nfs41_proc_reclaim_complete,
5722 #endif /* CONFIG_NFS_V4_1 */
5724 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
5725 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5726 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5727 .recover_open = nfs4_open_expired,
5728 .recover_lock = nfs4_lock_expired,
5729 .establish_clid = nfs4_init_clientid,
5730 .get_clid_cred = nfs4_get_setclientid_cred,
5733 #if defined(CONFIG_NFS_V4_1)
5734 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5735 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5736 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5737 .recover_open = nfs4_open_expired,
5738 .recover_lock = nfs4_lock_expired,
5739 .establish_clid = nfs41_init_clientid,
5740 .get_clid_cred = nfs4_get_exchange_id_cred,
5742 #endif /* CONFIG_NFS_V4_1 */
5744 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5745 .sched_state_renewal = nfs4_proc_async_renew,
5746 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
5747 .renew_lease = nfs4_proc_renew,
5750 #if defined(CONFIG_NFS_V4_1)
5751 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5752 .sched_state_renewal = nfs41_proc_async_sequence,
5753 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
5754 .renew_lease = nfs4_proc_sequence,
5758 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5760 .call_sync = _nfs4_call_sync,
5761 .validate_stateid = nfs4_validate_delegation_stateid,
5762 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5763 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5764 .state_renewal_ops = &nfs40_state_renewal_ops,
5767 #if defined(CONFIG_NFS_V4_1)
5768 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5770 .call_sync = _nfs4_call_sync_session,
5771 .validate_stateid = nfs41_validate_delegation_stateid,
5772 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5773 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5774 .state_renewal_ops = &nfs41_state_renewal_ops,
5778 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5779 [0] = &nfs_v4_0_minor_ops,
5780 #if defined(CONFIG_NFS_V4_1)
5781 [1] = &nfs_v4_1_minor_ops,
5785 static const struct inode_operations nfs4_file_inode_operations = {
5786 .permission = nfs_permission,
5787 .getattr = nfs_getattr,
5788 .setattr = nfs_setattr,
5789 .getxattr = generic_getxattr,
5790 .setxattr = generic_setxattr,
5791 .listxattr = generic_listxattr,
5792 .removexattr = generic_removexattr,
5795 const struct nfs_rpc_ops nfs_v4_clientops = {
5796 .version = 4, /* protocol version */
5797 .dentry_ops = &nfs4_dentry_operations,
5798 .dir_inode_ops = &nfs4_dir_inode_operations,
5799 .file_inode_ops = &nfs4_file_inode_operations,
5800 .getroot = nfs4_proc_get_root,
5801 .getattr = nfs4_proc_getattr,
5802 .setattr = nfs4_proc_setattr,
5803 .lookupfh = nfs4_proc_lookupfh,
5804 .lookup = nfs4_proc_lookup,
5805 .access = nfs4_proc_access,
5806 .readlink = nfs4_proc_readlink,
5807 .create = nfs4_proc_create,
5808 .remove = nfs4_proc_remove,
5809 .unlink_setup = nfs4_proc_unlink_setup,
5810 .unlink_done = nfs4_proc_unlink_done,
5811 .rename = nfs4_proc_rename,
5812 .rename_setup = nfs4_proc_rename_setup,
5813 .rename_done = nfs4_proc_rename_done,
5814 .link = nfs4_proc_link,
5815 .symlink = nfs4_proc_symlink,
5816 .mkdir = nfs4_proc_mkdir,
5817 .rmdir = nfs4_proc_remove,
5818 .readdir = nfs4_proc_readdir,
5819 .mknod = nfs4_proc_mknod,
5820 .statfs = nfs4_proc_statfs,
5821 .fsinfo = nfs4_proc_fsinfo,
5822 .pathconf = nfs4_proc_pathconf,
5823 .set_capabilities = nfs4_server_capabilities,
5824 .decode_dirent = nfs4_decode_dirent,
5825 .read_setup = nfs4_proc_read_setup,
5826 .read_done = nfs4_read_done,
5827 .write_setup = nfs4_proc_write_setup,
5828 .write_done = nfs4_write_done,
5829 .commit_setup = nfs4_proc_commit_setup,
5830 .commit_done = nfs4_commit_done,
5831 .lock = nfs4_proc_lock,
5832 .clear_acl_cache = nfs4_zap_acl_attr,
5833 .close_context = nfs4_close_context,
5834 .open_context = nfs4_atomic_open,
5835 .init_client = nfs4_init_client,
5836 .secinfo = nfs4_proc_secinfo,
5839 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
5840 .prefix = XATTR_NAME_NFSV4_ACL,
5841 .list = nfs4_xattr_list_nfs4_acl,
5842 .get = nfs4_xattr_get_nfs4_acl,
5843 .set = nfs4_xattr_set_nfs4_acl,
5846 const struct xattr_handler *nfs4_xattr_handlers[] = {
5847 &nfs4_xattr_nfs4_acl_handler,