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/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/sunrpc/gss_api.h>
47 #include <linux/nfs.h>
48 #include <linux/nfs4.h>
49 #include <linux/nfs_fs.h>
50 #include <linux/nfs_page.h>
51 #include <linux/nfs_mount.h>
52 #include <linux/namei.h>
53 #include <linux/mount.h>
54 #include <linux/module.h>
55 #include <linux/nfs_idmap.h>
56 #include <linux/sunrpc/bc_xprt.h>
57 #include <linux/xattr.h>
58 #include <linux/utsname.h>
59 #include <linux/freezer.h>
62 #include "delegation.h"
68 #define NFSDBG_FACILITY NFSDBG_PROC
70 #define NFS4_POLL_RETRY_MIN (HZ/10)
71 #define NFS4_POLL_RETRY_MAX (15*HZ)
73 #define NFS4_MAX_LOOP_ON_RECOVER (10)
75 static unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
78 static int _nfs4_proc_open(struct nfs4_opendata *data);
79 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
80 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
81 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
82 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
83 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
84 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
86 struct nfs4_state *state);
87 #ifdef CONFIG_NFS_V4_1
88 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
89 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
91 /* Prevent leaks of NFSv4 errors into userland */
92 static int nfs4_map_errors(int err)
97 case -NFS4ERR_RESOURCE:
99 case -NFS4ERR_WRONGSEC:
101 case -NFS4ERR_BADOWNER:
102 case -NFS4ERR_BADNAME:
105 dprintk("%s could not handle NFSv4 error %d\n",
113 * This is our standard bitmap for GETATTR requests.
115 const u32 nfs4_fattr_bitmap[2] = {
117 | FATTR4_WORD0_CHANGE
120 | FATTR4_WORD0_FILEID,
122 | FATTR4_WORD1_NUMLINKS
124 | FATTR4_WORD1_OWNER_GROUP
125 | FATTR4_WORD1_RAWDEV
126 | FATTR4_WORD1_SPACE_USED
127 | FATTR4_WORD1_TIME_ACCESS
128 | FATTR4_WORD1_TIME_METADATA
129 | FATTR4_WORD1_TIME_MODIFY
132 const u32 nfs4_statfs_bitmap[2] = {
133 FATTR4_WORD0_FILES_AVAIL
134 | FATTR4_WORD0_FILES_FREE
135 | FATTR4_WORD0_FILES_TOTAL,
136 FATTR4_WORD1_SPACE_AVAIL
137 | FATTR4_WORD1_SPACE_FREE
138 | FATTR4_WORD1_SPACE_TOTAL
141 const u32 nfs4_pathconf_bitmap[2] = {
143 | FATTR4_WORD0_MAXNAME,
147 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
148 | FATTR4_WORD0_MAXREAD
149 | FATTR4_WORD0_MAXWRITE
150 | FATTR4_WORD0_LEASE_TIME,
151 FATTR4_WORD1_TIME_DELTA
152 | FATTR4_WORD1_FS_LAYOUT_TYPES,
153 FATTR4_WORD2_LAYOUT_BLKSIZE
156 const u32 nfs4_fs_locations_bitmap[2] = {
158 | FATTR4_WORD0_CHANGE
161 | FATTR4_WORD0_FILEID
162 | FATTR4_WORD0_FS_LOCATIONS,
164 | FATTR4_WORD1_NUMLINKS
166 | FATTR4_WORD1_OWNER_GROUP
167 | FATTR4_WORD1_RAWDEV
168 | FATTR4_WORD1_SPACE_USED
169 | FATTR4_WORD1_TIME_ACCESS
170 | FATTR4_WORD1_TIME_METADATA
171 | FATTR4_WORD1_TIME_MODIFY
172 | FATTR4_WORD1_MOUNTED_ON_FILEID
175 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
176 struct nfs4_readdir_arg *readdir)
180 BUG_ON(readdir->count < 80);
182 readdir->cookie = cookie;
183 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
188 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
193 * NFSv4 servers do not return entries for '.' and '..'
194 * Therefore, we fake these entries here. We let '.'
195 * have cookie 0 and '..' have cookie 1. Note that
196 * when talking to the server, we always send cookie 0
199 start = p = kmap_atomic(*readdir->pages, KM_USER0);
202 *p++ = xdr_one; /* next */
203 *p++ = xdr_zero; /* cookie, first word */
204 *p++ = xdr_one; /* cookie, second word */
205 *p++ = xdr_one; /* entry len */
206 memcpy(p, ".\0\0\0", 4); /* entry */
208 *p++ = xdr_one; /* bitmap length */
209 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
210 *p++ = htonl(8); /* attribute buffer length */
211 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
214 *p++ = xdr_one; /* next */
215 *p++ = xdr_zero; /* cookie, first word */
216 *p++ = xdr_two; /* cookie, second word */
217 *p++ = xdr_two; /* entry len */
218 memcpy(p, "..\0\0", 4); /* entry */
220 *p++ = xdr_one; /* bitmap length */
221 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
222 *p++ = htonl(8); /* attribute buffer length */
223 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
225 readdir->pgbase = (char *)p - (char *)start;
226 readdir->count -= readdir->pgbase;
227 kunmap_atomic(start, KM_USER0);
230 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
236 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
237 nfs_wait_bit_killable, TASK_KILLABLE);
241 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
248 *timeout = NFS4_POLL_RETRY_MIN;
249 if (*timeout > NFS4_POLL_RETRY_MAX)
250 *timeout = NFS4_POLL_RETRY_MAX;
251 freezable_schedule_timeout_killable(*timeout);
252 if (fatal_signal_pending(current))
258 /* This is the error handling routine for processes that are allowed
261 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
263 struct nfs_client *clp = server->nfs_client;
264 struct nfs4_state *state = exception->state;
267 exception->retry = 0;
271 case -NFS4ERR_ADMIN_REVOKED:
272 case -NFS4ERR_BAD_STATEID:
273 case -NFS4ERR_OPENMODE:
276 nfs4_schedule_stateid_recovery(server, state);
277 goto wait_on_recovery;
278 case -NFS4ERR_EXPIRED:
280 nfs4_schedule_stateid_recovery(server, state);
281 case -NFS4ERR_STALE_STATEID:
282 case -NFS4ERR_STALE_CLIENTID:
283 nfs4_schedule_lease_recovery(clp);
284 goto wait_on_recovery;
285 #if defined(CONFIG_NFS_V4_1)
286 case -NFS4ERR_BADSESSION:
287 case -NFS4ERR_BADSLOT:
288 case -NFS4ERR_BAD_HIGH_SLOT:
289 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
290 case -NFS4ERR_DEADSESSION:
291 case -NFS4ERR_SEQ_FALSE_RETRY:
292 case -NFS4ERR_SEQ_MISORDERED:
293 dprintk("%s ERROR: %d Reset session\n", __func__,
295 nfs4_schedule_session_recovery(clp->cl_session);
296 exception->retry = 1;
298 #endif /* defined(CONFIG_NFS_V4_1) */
299 case -NFS4ERR_FILE_OPEN:
300 if (exception->timeout > HZ) {
301 /* We have retried a decent amount, time to
310 ret = nfs4_delay(server->client, &exception->timeout);
313 case -NFS4ERR_RETRY_UNCACHED_REP:
314 case -NFS4ERR_OLD_STATEID:
315 exception->retry = 1;
317 case -NFS4ERR_BADOWNER:
318 /* The following works around a Linux server bug! */
319 case -NFS4ERR_BADNAME:
320 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
321 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
322 exception->retry = 1;
323 printk(KERN_WARNING "NFS: v4 server %s "
324 "does not accept raw "
326 "Reenabling the idmapper.\n",
327 server->nfs_client->cl_hostname);
330 /* We failed to handle the error */
331 return nfs4_map_errors(ret);
333 ret = nfs4_wait_clnt_recover(clp);
335 exception->retry = 1;
340 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
342 spin_lock(&clp->cl_lock);
343 if (time_before(clp->cl_last_renewal,timestamp))
344 clp->cl_last_renewal = timestamp;
345 spin_unlock(&clp->cl_lock);
348 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
350 do_renew_lease(server->nfs_client, timestamp);
353 #if defined(CONFIG_NFS_V4_1)
356 * nfs4_free_slot - free a slot and efficiently update slot table.
358 * freeing a slot is trivially done by clearing its respective bit
360 * If the freed slotid equals highest_used_slotid we want to update it
361 * so that the server would be able to size down the slot table if needed,
362 * otherwise we know that the highest_used_slotid is still in use.
363 * When updating highest_used_slotid there may be "holes" in the bitmap
364 * so we need to scan down from highest_used_slotid to 0 looking for the now
365 * highest slotid in use.
366 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
368 * Must be called while holding tbl->slot_tbl_lock
371 nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
373 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
374 /* clear used bit in bitmap */
375 __clear_bit(slotid, tbl->used_slots);
377 /* update highest_used_slotid when it is freed */
378 if (slotid == tbl->highest_used_slotid) {
379 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
380 if (slotid < tbl->max_slots)
381 tbl->highest_used_slotid = slotid;
383 tbl->highest_used_slotid = NFS4_NO_SLOT;
385 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
386 slotid, tbl->highest_used_slotid);
389 bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
391 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
396 * Signal state manager thread if session fore channel is drained
398 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
400 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
401 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
402 nfs4_set_task_privileged, NULL);
406 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
409 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
410 complete(&ses->fc_slot_table.complete);
414 * Signal state manager thread if session back channel is drained
416 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
418 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
419 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
421 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
422 complete(&ses->bc_slot_table.complete);
425 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
427 struct nfs4_slot_table *tbl;
429 tbl = &res->sr_session->fc_slot_table;
431 /* just wake up the next guy waiting since
432 * we may have not consumed a slot after all */
433 dprintk("%s: No slot\n", __func__);
437 spin_lock(&tbl->slot_tbl_lock);
438 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
439 nfs4_check_drain_fc_complete(res->sr_session);
440 spin_unlock(&tbl->slot_tbl_lock);
444 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
446 unsigned long timestamp;
447 struct nfs_client *clp;
450 * sr_status remains 1 if an RPC level error occurred. The server
451 * may or may not have processed the sequence operation..
452 * Proceed as if the server received and processed the sequence
455 if (res->sr_status == 1)
456 res->sr_status = NFS_OK;
458 /* don't increment the sequence number if the task wasn't sent */
459 if (!RPC_WAS_SENT(task))
462 /* Check the SEQUENCE operation status */
463 switch (res->sr_status) {
465 /* Update the slot's sequence and clientid lease timer */
466 ++res->sr_slot->seq_nr;
467 timestamp = res->sr_renewal_time;
468 clp = res->sr_session->clp;
469 do_renew_lease(clp, timestamp);
470 /* Check sequence flags */
471 if (res->sr_status_flags != 0)
472 nfs4_schedule_lease_recovery(clp);
475 /* The server detected a resend of the RPC call and
476 * returned NFS4ERR_DELAY as per Section 2.10.6.2
479 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
481 res->sr_slot - res->sr_session->fc_slot_table.slots,
482 res->sr_slot->seq_nr);
485 /* Just update the slot sequence no. */
486 ++res->sr_slot->seq_nr;
489 /* The session may be reset by one of the error handlers. */
490 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
491 nfs41_sequence_free_slot(res);
494 if (!rpc_restart_call(task))
496 rpc_delay(task, NFS4_POLL_RETRY_MAX);
500 static int nfs4_sequence_done(struct rpc_task *task,
501 struct nfs4_sequence_res *res)
503 if (res->sr_session == NULL)
505 return nfs41_sequence_done(task, res);
509 * nfs4_find_slot - efficiently look for a free slot
511 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
512 * If found, we mark the slot as used, update the highest_used_slotid,
513 * and respectively set up the sequence operation args.
514 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
516 * Note: must be called with under the slot_tbl_lock.
519 nfs4_find_slot(struct nfs4_slot_table *tbl)
522 u32 ret_id = NFS4_NO_SLOT;
524 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
525 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
527 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
528 if (slotid >= tbl->max_slots)
530 __set_bit(slotid, tbl->used_slots);
531 if (slotid > tbl->highest_used_slotid ||
532 tbl->highest_used_slotid == NFS4_NO_SLOT)
533 tbl->highest_used_slotid = slotid;
536 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
537 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
541 static void nfs41_init_sequence(struct nfs4_sequence_args *args,
542 struct nfs4_sequence_res *res, int cache_reply)
544 args->sa_session = NULL;
545 args->sa_cache_this = 0;
547 args->sa_cache_this = 1;
548 res->sr_session = NULL;
552 int nfs41_setup_sequence(struct nfs4_session *session,
553 struct nfs4_sequence_args *args,
554 struct nfs4_sequence_res *res,
555 struct rpc_task *task)
557 struct nfs4_slot *slot;
558 struct nfs4_slot_table *tbl;
561 dprintk("--> %s\n", __func__);
562 /* slot already allocated? */
563 if (res->sr_slot != NULL)
566 tbl = &session->fc_slot_table;
568 spin_lock(&tbl->slot_tbl_lock);
569 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
570 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
571 /* The state manager will wait until the slot table is empty */
572 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
573 spin_unlock(&tbl->slot_tbl_lock);
574 dprintk("%s session is draining\n", __func__);
578 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
579 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
580 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
581 spin_unlock(&tbl->slot_tbl_lock);
582 dprintk("%s enforce FIFO order\n", __func__);
586 slotid = nfs4_find_slot(tbl);
587 if (slotid == NFS4_NO_SLOT) {
588 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
589 spin_unlock(&tbl->slot_tbl_lock);
590 dprintk("<-- %s: no free slots\n", __func__);
593 spin_unlock(&tbl->slot_tbl_lock);
595 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
596 slot = tbl->slots + slotid;
597 args->sa_session = session;
598 args->sa_slotid = slotid;
600 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
602 res->sr_session = session;
604 res->sr_renewal_time = jiffies;
605 res->sr_status_flags = 0;
607 * sr_status is only set in decode_sequence, and so will remain
608 * set to 1 if an rpc level failure occurs.
613 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
615 int nfs4_setup_sequence(const struct nfs_server *server,
616 struct nfs4_sequence_args *args,
617 struct nfs4_sequence_res *res,
618 struct rpc_task *task)
620 struct nfs4_session *session = nfs4_get_session(server);
626 dprintk("--> %s clp %p session %p sr_slot %td\n",
627 __func__, session->clp, session, res->sr_slot ?
628 res->sr_slot - session->fc_slot_table.slots : -1);
630 ret = nfs41_setup_sequence(session, args, res, task);
632 dprintk("<-- %s status=%d\n", __func__, ret);
636 struct nfs41_call_sync_data {
637 const struct nfs_server *seq_server;
638 struct nfs4_sequence_args *seq_args;
639 struct nfs4_sequence_res *seq_res;
642 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
644 struct nfs41_call_sync_data *data = calldata;
646 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
648 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
649 data->seq_res, task))
651 rpc_call_start(task);
654 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
656 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
657 nfs41_call_sync_prepare(task, calldata);
660 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
662 struct nfs41_call_sync_data *data = calldata;
664 nfs41_sequence_done(task, data->seq_res);
667 struct rpc_call_ops nfs41_call_sync_ops = {
668 .rpc_call_prepare = nfs41_call_sync_prepare,
669 .rpc_call_done = nfs41_call_sync_done,
672 struct rpc_call_ops nfs41_call_priv_sync_ops = {
673 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
674 .rpc_call_done = nfs41_call_sync_done,
677 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
678 struct nfs_server *server,
679 struct rpc_message *msg,
680 struct nfs4_sequence_args *args,
681 struct nfs4_sequence_res *res,
685 struct rpc_task *task;
686 struct nfs41_call_sync_data data = {
687 .seq_server = server,
691 struct rpc_task_setup task_setup = {
694 .callback_ops = &nfs41_call_sync_ops,
695 .callback_data = &data
699 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
700 task = rpc_run_task(&task_setup);
704 ret = task->tk_status;
710 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
711 struct nfs_server *server,
712 struct rpc_message *msg,
713 struct nfs4_sequence_args *args,
714 struct nfs4_sequence_res *res,
717 nfs41_init_sequence(args, res, cache_reply);
718 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
723 void nfs41_init_sequence(struct nfs4_sequence_args *args,
724 struct nfs4_sequence_res *res, int cache_reply)
728 static int nfs4_sequence_done(struct rpc_task *task,
729 struct nfs4_sequence_res *res)
733 #endif /* CONFIG_NFS_V4_1 */
735 int _nfs4_call_sync(struct rpc_clnt *clnt,
736 struct nfs_server *server,
737 struct rpc_message *msg,
738 struct nfs4_sequence_args *args,
739 struct nfs4_sequence_res *res,
742 nfs41_init_sequence(args, res, cache_reply);
743 return rpc_call_sync(clnt, msg, 0);
747 int nfs4_call_sync(struct rpc_clnt *clnt,
748 struct nfs_server *server,
749 struct rpc_message *msg,
750 struct nfs4_sequence_args *args,
751 struct nfs4_sequence_res *res,
754 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
755 args, res, cache_reply);
758 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
760 struct nfs_inode *nfsi = NFS_I(dir);
762 spin_lock(&dir->i_lock);
763 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
764 if (!cinfo->atomic || cinfo->before != dir->i_version)
765 nfs_force_lookup_revalidate(dir);
766 dir->i_version = cinfo->after;
767 spin_unlock(&dir->i_lock);
770 struct nfs4_opendata {
772 struct nfs_openargs o_arg;
773 struct nfs_openres o_res;
774 struct nfs_open_confirmargs c_arg;
775 struct nfs_open_confirmres c_res;
776 struct nfs4_string owner_name;
777 struct nfs4_string group_name;
778 struct nfs_fattr f_attr;
779 struct nfs_fattr dir_attr;
781 struct dentry *dentry;
782 struct nfs4_state_owner *owner;
783 struct nfs4_state *state;
785 unsigned long timestamp;
786 unsigned int rpc_done : 1;
792 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
794 p->o_res.f_attr = &p->f_attr;
795 p->o_res.dir_attr = &p->dir_attr;
796 p->o_res.seqid = p->o_arg.seqid;
797 p->c_res.seqid = p->c_arg.seqid;
798 p->o_res.server = p->o_arg.server;
799 nfs_fattr_init(&p->f_attr);
800 nfs_fattr_init(&p->dir_attr);
801 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
804 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
805 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
806 const struct iattr *attrs,
809 struct dentry *parent = dget_parent(dentry);
810 struct inode *dir = parent->d_inode;
811 struct nfs_server *server = NFS_SERVER(dir);
812 struct nfs4_opendata *p;
814 p = kzalloc(sizeof(*p), gfp_mask);
817 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
818 if (p->o_arg.seqid == NULL)
820 nfs_sb_active(dentry->d_sb);
821 p->dentry = dget(dentry);
824 atomic_inc(&sp->so_count);
825 p->o_arg.fh = NFS_FH(dir);
826 p->o_arg.open_flags = flags;
827 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
828 p->o_arg.clientid = server->nfs_client->cl_clientid;
829 p->o_arg.id = sp->so_seqid.owner_id;
830 p->o_arg.name = &dentry->d_name;
831 p->o_arg.server = server;
832 p->o_arg.bitmask = server->attr_bitmask;
833 p->o_arg.dir_bitmask = server->cache_consistency_bitmask;
834 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
835 if (attrs != NULL && attrs->ia_valid != 0) {
838 p->o_arg.u.attrs = &p->attrs;
839 memcpy(&p->attrs, attrs, sizeof(p->attrs));
840 s = (u32 *) p->o_arg.u.verifier.data;
844 p->c_arg.fh = &p->o_res.fh;
845 p->c_arg.stateid = &p->o_res.stateid;
846 p->c_arg.seqid = p->o_arg.seqid;
847 nfs4_init_opendata_res(p);
857 static void nfs4_opendata_free(struct kref *kref)
859 struct nfs4_opendata *p = container_of(kref,
860 struct nfs4_opendata, kref);
861 struct super_block *sb = p->dentry->d_sb;
863 nfs_free_seqid(p->o_arg.seqid);
864 if (p->state != NULL)
865 nfs4_put_open_state(p->state);
866 nfs4_put_state_owner(p->owner);
870 nfs_fattr_free_names(&p->f_attr);
874 static void nfs4_opendata_put(struct nfs4_opendata *p)
877 kref_put(&p->kref, nfs4_opendata_free);
880 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
884 ret = rpc_wait_for_completion_task(task);
888 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
892 if (open_mode & (O_EXCL|O_TRUNC))
894 switch (mode & (FMODE_READ|FMODE_WRITE)) {
896 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
897 && state->n_rdonly != 0;
900 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
901 && state->n_wronly != 0;
903 case FMODE_READ|FMODE_WRITE:
904 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
905 && state->n_rdwr != 0;
911 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
913 if (delegation == NULL)
915 if ((delegation->type & fmode) != fmode)
917 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
919 nfs_mark_delegation_referenced(delegation);
923 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
932 case FMODE_READ|FMODE_WRITE:
935 nfs4_state_set_mode_locked(state, state->state | fmode);
938 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
940 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
941 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
942 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
945 set_bit(NFS_O_RDONLY_STATE, &state->flags);
948 set_bit(NFS_O_WRONLY_STATE, &state->flags);
950 case FMODE_READ|FMODE_WRITE:
951 set_bit(NFS_O_RDWR_STATE, &state->flags);
955 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
957 write_seqlock(&state->seqlock);
958 nfs_set_open_stateid_locked(state, stateid, fmode);
959 write_sequnlock(&state->seqlock);
962 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
965 * Protect the call to nfs4_state_set_mode_locked and
966 * serialise the stateid update
968 write_seqlock(&state->seqlock);
969 if (deleg_stateid != NULL) {
970 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
971 set_bit(NFS_DELEGATED_STATE, &state->flags);
973 if (open_stateid != NULL)
974 nfs_set_open_stateid_locked(state, open_stateid, fmode);
975 write_sequnlock(&state->seqlock);
976 spin_lock(&state->owner->so_lock);
977 update_open_stateflags(state, fmode);
978 spin_unlock(&state->owner->so_lock);
981 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
983 struct nfs_inode *nfsi = NFS_I(state->inode);
984 struct nfs_delegation *deleg_cur;
987 fmode &= (FMODE_READ|FMODE_WRITE);
990 deleg_cur = rcu_dereference(nfsi->delegation);
991 if (deleg_cur == NULL)
994 spin_lock(&deleg_cur->lock);
995 if (nfsi->delegation != deleg_cur ||
996 (deleg_cur->type & fmode) != fmode)
997 goto no_delegation_unlock;
999 if (delegation == NULL)
1000 delegation = &deleg_cur->stateid;
1001 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
1002 goto no_delegation_unlock;
1004 nfs_mark_delegation_referenced(deleg_cur);
1005 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1007 no_delegation_unlock:
1008 spin_unlock(&deleg_cur->lock);
1012 if (!ret && open_stateid != NULL) {
1013 __update_open_stateid(state, open_stateid, NULL, fmode);
1021 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1023 struct nfs_delegation *delegation;
1026 delegation = rcu_dereference(NFS_I(inode)->delegation);
1027 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1032 nfs_inode_return_delegation(inode);
1035 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1037 struct nfs4_state *state = opendata->state;
1038 struct nfs_inode *nfsi = NFS_I(state->inode);
1039 struct nfs_delegation *delegation;
1040 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
1041 fmode_t fmode = opendata->o_arg.fmode;
1042 nfs4_stateid stateid;
1046 if (can_open_cached(state, fmode, open_mode)) {
1047 spin_lock(&state->owner->so_lock);
1048 if (can_open_cached(state, fmode, open_mode)) {
1049 update_open_stateflags(state, fmode);
1050 spin_unlock(&state->owner->so_lock);
1051 goto out_return_state;
1053 spin_unlock(&state->owner->so_lock);
1056 delegation = rcu_dereference(nfsi->delegation);
1057 if (!can_open_delegated(delegation, fmode)) {
1061 /* Save the delegation */
1062 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1064 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1069 /* Try to update the stateid using the delegation */
1070 if (update_open_stateid(state, NULL, &stateid, fmode))
1071 goto out_return_state;
1074 return ERR_PTR(ret);
1076 atomic_inc(&state->count);
1080 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1082 struct inode *inode;
1083 struct nfs4_state *state = NULL;
1084 struct nfs_delegation *delegation;
1087 if (!data->rpc_done) {
1088 state = nfs4_try_open_cached(data);
1093 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1095 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1096 ret = PTR_ERR(inode);
1100 state = nfs4_get_open_state(inode, data->owner);
1103 if (data->o_res.delegation_type != 0) {
1104 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1105 int delegation_flags = 0;
1108 delegation = rcu_dereference(NFS_I(inode)->delegation);
1110 delegation_flags = delegation->flags;
1112 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1113 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1114 "returning a delegation for "
1115 "OPEN(CLAIM_DELEGATE_CUR)\n",
1117 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1118 nfs_inode_set_delegation(state->inode,
1119 data->owner->so_cred,
1122 nfs_inode_reclaim_delegation(state->inode,
1123 data->owner->so_cred,
1127 update_open_stateid(state, &data->o_res.stateid, NULL,
1135 return ERR_PTR(ret);
1138 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1140 struct nfs_inode *nfsi = NFS_I(state->inode);
1141 struct nfs_open_context *ctx;
1143 spin_lock(&state->inode->i_lock);
1144 list_for_each_entry(ctx, &nfsi->open_files, list) {
1145 if (ctx->state != state)
1147 get_nfs_open_context(ctx);
1148 spin_unlock(&state->inode->i_lock);
1151 spin_unlock(&state->inode->i_lock);
1152 return ERR_PTR(-ENOENT);
1155 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1157 struct nfs4_opendata *opendata;
1159 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1160 if (opendata == NULL)
1161 return ERR_PTR(-ENOMEM);
1162 opendata->state = state;
1163 atomic_inc(&state->count);
1167 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1169 struct nfs4_state *newstate;
1172 opendata->o_arg.open_flags = 0;
1173 opendata->o_arg.fmode = fmode;
1174 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1175 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1176 nfs4_init_opendata_res(opendata);
1177 ret = _nfs4_recover_proc_open(opendata);
1180 newstate = nfs4_opendata_to_nfs4_state(opendata);
1181 if (IS_ERR(newstate))
1182 return PTR_ERR(newstate);
1183 nfs4_close_state(newstate, fmode);
1188 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1190 struct nfs4_state *newstate;
1193 /* memory barrier prior to reading state->n_* */
1194 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1196 if (state->n_rdwr != 0) {
1197 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1198 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1201 if (newstate != state)
1204 if (state->n_wronly != 0) {
1205 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1206 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1209 if (newstate != state)
1212 if (state->n_rdonly != 0) {
1213 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1214 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1217 if (newstate != state)
1221 * We may have performed cached opens for all three recoveries.
1222 * Check if we need to update the current stateid.
1224 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1225 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
1226 write_seqlock(&state->seqlock);
1227 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1228 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
1229 write_sequnlock(&state->seqlock);
1236 * reclaim state on the server after a reboot.
1238 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1240 struct nfs_delegation *delegation;
1241 struct nfs4_opendata *opendata;
1242 fmode_t delegation_type = 0;
1245 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1246 if (IS_ERR(opendata))
1247 return PTR_ERR(opendata);
1248 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1249 opendata->o_arg.fh = NFS_FH(state->inode);
1251 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1252 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1253 delegation_type = delegation->type;
1255 opendata->o_arg.u.delegation_type = delegation_type;
1256 status = nfs4_open_recover(opendata, state);
1257 nfs4_opendata_put(opendata);
1261 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1263 struct nfs_server *server = NFS_SERVER(state->inode);
1264 struct nfs4_exception exception = { };
1267 err = _nfs4_do_open_reclaim(ctx, state);
1268 if (err != -NFS4ERR_DELAY)
1270 nfs4_handle_exception(server, err, &exception);
1271 } while (exception.retry);
1275 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1277 struct nfs_open_context *ctx;
1280 ctx = nfs4_state_find_open_context(state);
1282 return PTR_ERR(ctx);
1283 ret = nfs4_do_open_reclaim(ctx, state);
1284 put_nfs_open_context(ctx);
1288 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1290 struct nfs4_opendata *opendata;
1293 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1294 if (IS_ERR(opendata))
1295 return PTR_ERR(opendata);
1296 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1297 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
1298 sizeof(opendata->o_arg.u.delegation.data));
1299 ret = nfs4_open_recover(opendata, state);
1300 nfs4_opendata_put(opendata);
1304 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1306 struct nfs4_exception exception = { };
1307 struct nfs_server *server = NFS_SERVER(state->inode);
1310 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1316 case -NFS4ERR_BADSESSION:
1317 case -NFS4ERR_BADSLOT:
1318 case -NFS4ERR_BAD_HIGH_SLOT:
1319 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1320 case -NFS4ERR_DEADSESSION:
1321 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
1323 case -NFS4ERR_STALE_CLIENTID:
1324 case -NFS4ERR_STALE_STATEID:
1325 case -NFS4ERR_EXPIRED:
1326 /* Don't recall a delegation if it was lost */
1327 nfs4_schedule_lease_recovery(server->nfs_client);
1331 * The show must go on: exit, but mark the
1332 * stateid as needing recovery.
1334 case -NFS4ERR_ADMIN_REVOKED:
1335 case -NFS4ERR_BAD_STATEID:
1336 nfs4_schedule_stateid_recovery(server, state);
1339 * User RPCSEC_GSS context has expired.
1340 * We cannot recover this stateid now, so
1341 * skip it and allow recovery thread to
1348 err = nfs4_handle_exception(server, err, &exception);
1349 } while (exception.retry);
1354 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1356 struct nfs4_opendata *data = calldata;
1358 data->rpc_status = task->tk_status;
1359 if (data->rpc_status == 0) {
1360 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1361 sizeof(data->o_res.stateid.data));
1362 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1363 renew_lease(data->o_res.server, data->timestamp);
1368 static void nfs4_open_confirm_release(void *calldata)
1370 struct nfs4_opendata *data = calldata;
1371 struct nfs4_state *state = NULL;
1373 /* If this request hasn't been cancelled, do nothing */
1374 if (data->cancelled == 0)
1376 /* In case of error, no cleanup! */
1377 if (!data->rpc_done)
1379 state = nfs4_opendata_to_nfs4_state(data);
1381 nfs4_close_state(state, data->o_arg.fmode);
1383 nfs4_opendata_put(data);
1386 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1387 .rpc_call_done = nfs4_open_confirm_done,
1388 .rpc_release = nfs4_open_confirm_release,
1392 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1394 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1396 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1397 struct rpc_task *task;
1398 struct rpc_message msg = {
1399 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1400 .rpc_argp = &data->c_arg,
1401 .rpc_resp = &data->c_res,
1402 .rpc_cred = data->owner->so_cred,
1404 struct rpc_task_setup task_setup_data = {
1405 .rpc_client = server->client,
1406 .rpc_message = &msg,
1407 .callback_ops = &nfs4_open_confirm_ops,
1408 .callback_data = data,
1409 .workqueue = nfsiod_workqueue,
1410 .flags = RPC_TASK_ASYNC,
1414 kref_get(&data->kref);
1416 data->rpc_status = 0;
1417 data->timestamp = jiffies;
1418 task = rpc_run_task(&task_setup_data);
1420 return PTR_ERR(task);
1421 status = nfs4_wait_for_completion_rpc_task(task);
1423 data->cancelled = 1;
1426 status = data->rpc_status;
1431 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1433 struct nfs4_opendata *data = calldata;
1434 struct nfs4_state_owner *sp = data->owner;
1436 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1439 * Check if we still need to send an OPEN call, or if we can use
1440 * a delegation instead.
1442 if (data->state != NULL) {
1443 struct nfs_delegation *delegation;
1445 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1448 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1449 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1450 can_open_delegated(delegation, data->o_arg.fmode))
1451 goto unlock_no_action;
1454 /* Update sequence id. */
1455 data->o_arg.id = sp->so_seqid.owner_id;
1456 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1457 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1458 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1459 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1461 data->timestamp = jiffies;
1462 if (nfs4_setup_sequence(data->o_arg.server,
1463 &data->o_arg.seq_args,
1464 &data->o_res.seq_res, task))
1466 rpc_call_start(task);
1471 task->tk_action = NULL;
1475 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1477 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1478 nfs4_open_prepare(task, calldata);
1481 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1483 struct nfs4_opendata *data = calldata;
1485 data->rpc_status = task->tk_status;
1487 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1490 if (task->tk_status == 0) {
1491 switch (data->o_res.f_attr->mode & S_IFMT) {
1495 data->rpc_status = -ELOOP;
1498 data->rpc_status = -EISDIR;
1501 data->rpc_status = -ENOTDIR;
1503 renew_lease(data->o_res.server, data->timestamp);
1504 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1505 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1510 static void nfs4_open_release(void *calldata)
1512 struct nfs4_opendata *data = calldata;
1513 struct nfs4_state *state = NULL;
1515 /* If this request hasn't been cancelled, do nothing */
1516 if (data->cancelled == 0)
1518 /* In case of error, no cleanup! */
1519 if (data->rpc_status != 0 || !data->rpc_done)
1521 /* In case we need an open_confirm, no cleanup! */
1522 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1524 state = nfs4_opendata_to_nfs4_state(data);
1526 nfs4_close_state(state, data->o_arg.fmode);
1528 nfs4_opendata_put(data);
1531 static const struct rpc_call_ops nfs4_open_ops = {
1532 .rpc_call_prepare = nfs4_open_prepare,
1533 .rpc_call_done = nfs4_open_done,
1534 .rpc_release = nfs4_open_release,
1537 static const struct rpc_call_ops nfs4_recover_open_ops = {
1538 .rpc_call_prepare = nfs4_recover_open_prepare,
1539 .rpc_call_done = nfs4_open_done,
1540 .rpc_release = nfs4_open_release,
1543 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1545 struct inode *dir = data->dir->d_inode;
1546 struct nfs_server *server = NFS_SERVER(dir);
1547 struct nfs_openargs *o_arg = &data->o_arg;
1548 struct nfs_openres *o_res = &data->o_res;
1549 struct rpc_task *task;
1550 struct rpc_message msg = {
1551 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1554 .rpc_cred = data->owner->so_cred,
1556 struct rpc_task_setup task_setup_data = {
1557 .rpc_client = server->client,
1558 .rpc_message = &msg,
1559 .callback_ops = &nfs4_open_ops,
1560 .callback_data = data,
1561 .workqueue = nfsiod_workqueue,
1562 .flags = RPC_TASK_ASYNC,
1566 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1567 kref_get(&data->kref);
1569 data->rpc_status = 0;
1570 data->cancelled = 0;
1572 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1573 task = rpc_run_task(&task_setup_data);
1575 return PTR_ERR(task);
1576 status = nfs4_wait_for_completion_rpc_task(task);
1578 data->cancelled = 1;
1581 status = data->rpc_status;
1587 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1589 struct inode *dir = data->dir->d_inode;
1590 struct nfs_openres *o_res = &data->o_res;
1593 status = nfs4_run_open_task(data, 1);
1594 if (status != 0 || !data->rpc_done)
1597 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1599 nfs_refresh_inode(dir, o_res->dir_attr);
1601 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1602 status = _nfs4_proc_open_confirm(data);
1611 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1613 static int _nfs4_proc_open(struct nfs4_opendata *data)
1615 struct inode *dir = data->dir->d_inode;
1616 struct nfs_server *server = NFS_SERVER(dir);
1617 struct nfs_openargs *o_arg = &data->o_arg;
1618 struct nfs_openres *o_res = &data->o_res;
1621 status = nfs4_run_open_task(data, 0);
1622 if (!data->rpc_done)
1625 if (status == -NFS4ERR_BADNAME &&
1626 !(o_arg->open_flags & O_CREAT))
1631 nfs_fattr_map_and_free_names(server, &data->f_attr);
1633 if (o_arg->open_flags & O_CREAT) {
1634 update_changeattr(dir, &o_res->cinfo);
1635 nfs_post_op_update_inode(dir, o_res->dir_attr);
1637 nfs_refresh_inode(dir, o_res->dir_attr);
1638 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1639 server->caps &= ~NFS_CAP_POSIX_LOCK;
1640 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1641 status = _nfs4_proc_open_confirm(data);
1645 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1646 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1650 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1655 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1656 ret = nfs4_wait_clnt_recover(clp);
1659 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1660 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1662 nfs4_schedule_state_manager(clp);
1668 static int nfs4_recover_expired_lease(struct nfs_server *server)
1670 return nfs4_client_recover_expired_lease(server->nfs_client);
1675 * reclaim state on the server after a network partition.
1676 * Assumes caller holds the appropriate lock
1678 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1680 struct nfs4_opendata *opendata;
1683 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1684 if (IS_ERR(opendata))
1685 return PTR_ERR(opendata);
1686 ret = nfs4_open_recover(opendata, state);
1688 d_drop(ctx->dentry);
1689 nfs4_opendata_put(opendata);
1693 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1695 struct nfs_server *server = NFS_SERVER(state->inode);
1696 struct nfs4_exception exception = { };
1700 err = _nfs4_open_expired(ctx, state);
1704 case -NFS4ERR_GRACE:
1705 case -NFS4ERR_DELAY:
1706 nfs4_handle_exception(server, err, &exception);
1709 } while (exception.retry);
1714 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1716 struct nfs_open_context *ctx;
1719 ctx = nfs4_state_find_open_context(state);
1721 return PTR_ERR(ctx);
1722 ret = nfs4_do_open_expired(ctx, state);
1723 put_nfs_open_context(ctx);
1727 #if defined(CONFIG_NFS_V4_1)
1728 static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
1730 int status = NFS_OK;
1731 struct nfs_server *server = NFS_SERVER(state->inode);
1733 if (state->flags & flags) {
1734 status = nfs41_test_stateid(server, stateid);
1735 if (status != NFS_OK) {
1736 nfs41_free_stateid(server, stateid);
1737 state->flags &= ~flags;
1743 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1745 int deleg_status, open_status;
1746 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1747 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1749 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1750 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1752 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1754 return nfs4_open_expired(sp, state);
1759 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1760 * fields corresponding to attributes that were used to store the verifier.
1761 * Make sure we clobber those fields in the later setattr call
1763 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1765 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1766 !(sattr->ia_valid & ATTR_ATIME_SET))
1767 sattr->ia_valid |= ATTR_ATIME;
1769 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1770 !(sattr->ia_valid & ATTR_MTIME_SET))
1771 sattr->ia_valid |= ATTR_MTIME;
1775 * Returns a referenced nfs4_state
1777 static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
1779 struct nfs4_state_owner *sp;
1780 struct nfs4_state *state = NULL;
1781 struct nfs_server *server = NFS_SERVER(dir);
1782 struct nfs4_opendata *opendata;
1785 /* Protect against reboot recovery conflicts */
1787 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1789 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1792 status = nfs4_recover_expired_lease(server);
1794 goto err_put_state_owner;
1795 if (dentry->d_inode != NULL)
1796 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1798 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1799 if (opendata == NULL)
1800 goto err_put_state_owner;
1802 if (dentry->d_inode != NULL)
1803 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1805 status = _nfs4_proc_open(opendata);
1807 goto err_opendata_put;
1809 state = nfs4_opendata_to_nfs4_state(opendata);
1810 status = PTR_ERR(state);
1812 goto err_opendata_put;
1813 if (server->caps & NFS_CAP_POSIX_LOCK)
1814 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1816 if (opendata->o_arg.open_flags & O_EXCL) {
1817 nfs4_exclusive_attrset(opendata, sattr);
1819 nfs_fattr_init(opendata->o_res.f_attr);
1820 status = nfs4_do_setattr(state->inode, cred,
1821 opendata->o_res.f_attr, sattr,
1824 nfs_setattr_update_inode(state->inode, sattr);
1825 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1827 nfs4_opendata_put(opendata);
1828 nfs4_put_state_owner(sp);
1832 nfs4_opendata_put(opendata);
1833 err_put_state_owner:
1834 nfs4_put_state_owner(sp);
1841 static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
1843 struct nfs4_exception exception = { };
1844 struct nfs4_state *res;
1848 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
1851 /* NOTE: BAD_SEQID means the server and client disagree about the
1852 * book-keeping w.r.t. state-changing operations
1853 * (OPEN/CLOSE/LOCK/LOCKU...)
1854 * It is actually a sign of a bug on the client or on the server.
1856 * If we receive a BAD_SEQID error in the particular case of
1857 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1858 * have unhashed the old state_owner for us, and that we can
1859 * therefore safely retry using a new one. We should still warn
1860 * the user though...
1862 if (status == -NFS4ERR_BAD_SEQID) {
1863 printk(KERN_WARNING "NFS: v4 server %s "
1864 " returned a bad sequence-id error!\n",
1865 NFS_SERVER(dir)->nfs_client->cl_hostname);
1866 exception.retry = 1;
1870 * BAD_STATEID on OPEN means that the server cancelled our
1871 * state before it received the OPEN_CONFIRM.
1872 * Recover by retrying the request as per the discussion
1873 * on Page 181 of RFC3530.
1875 if (status == -NFS4ERR_BAD_STATEID) {
1876 exception.retry = 1;
1879 if (status == -EAGAIN) {
1880 /* We must have found a delegation */
1881 exception.retry = 1;
1884 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1885 status, &exception));
1886 } while (exception.retry);
1890 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1891 struct nfs_fattr *fattr, struct iattr *sattr,
1892 struct nfs4_state *state)
1894 struct nfs_server *server = NFS_SERVER(inode);
1895 struct nfs_setattrargs arg = {
1896 .fh = NFS_FH(inode),
1899 .bitmask = server->attr_bitmask,
1901 struct nfs_setattrres res = {
1905 struct rpc_message msg = {
1906 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1911 unsigned long timestamp = jiffies;
1914 nfs_fattr_init(fattr);
1916 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1917 /* Use that stateid */
1918 } else if (state != NULL) {
1919 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
1921 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1923 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1924 if (status == 0 && state != NULL)
1925 renew_lease(server, timestamp);
1929 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1930 struct nfs_fattr *fattr, struct iattr *sattr,
1931 struct nfs4_state *state)
1933 struct nfs_server *server = NFS_SERVER(inode);
1934 struct nfs4_exception exception = { };
1937 err = nfs4_handle_exception(server,
1938 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1940 } while (exception.retry);
1944 struct nfs4_closedata {
1945 struct inode *inode;
1946 struct nfs4_state *state;
1947 struct nfs_closeargs arg;
1948 struct nfs_closeres res;
1949 struct nfs_fattr fattr;
1950 unsigned long timestamp;
1955 static void nfs4_free_closedata(void *data)
1957 struct nfs4_closedata *calldata = data;
1958 struct nfs4_state_owner *sp = calldata->state->owner;
1959 struct super_block *sb = calldata->state->inode->i_sb;
1962 pnfs_roc_release(calldata->state->inode);
1963 nfs4_put_open_state(calldata->state);
1964 nfs_free_seqid(calldata->arg.seqid);
1965 nfs4_put_state_owner(sp);
1966 nfs_sb_deactive(sb);
1970 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1973 spin_lock(&state->owner->so_lock);
1974 if (!(fmode & FMODE_READ))
1975 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1976 if (!(fmode & FMODE_WRITE))
1977 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1978 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1979 spin_unlock(&state->owner->so_lock);
1982 static void nfs4_close_done(struct rpc_task *task, void *data)
1984 struct nfs4_closedata *calldata = data;
1985 struct nfs4_state *state = calldata->state;
1986 struct nfs_server *server = NFS_SERVER(calldata->inode);
1988 dprintk("%s: begin!\n", __func__);
1989 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1991 /* hmm. we are done with the inode, and in the process of freeing
1992 * the state_owner. we keep this around to process errors
1994 switch (task->tk_status) {
1997 pnfs_roc_set_barrier(state->inode,
1998 calldata->roc_barrier);
1999 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2000 renew_lease(server, calldata->timestamp);
2001 nfs4_close_clear_stateid_flags(state,
2002 calldata->arg.fmode);
2004 case -NFS4ERR_STALE_STATEID:
2005 case -NFS4ERR_OLD_STATEID:
2006 case -NFS4ERR_BAD_STATEID:
2007 case -NFS4ERR_EXPIRED:
2008 if (calldata->arg.fmode == 0)
2011 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2012 rpc_restart_call_prepare(task);
2014 nfs_release_seqid(calldata->arg.seqid);
2015 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2016 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2019 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2021 struct nfs4_closedata *calldata = data;
2022 struct nfs4_state *state = calldata->state;
2025 dprintk("%s: begin!\n", __func__);
2026 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2029 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2030 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2031 spin_lock(&state->owner->so_lock);
2032 /* Calculate the change in open mode */
2033 if (state->n_rdwr == 0) {
2034 if (state->n_rdonly == 0) {
2035 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2036 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2037 calldata->arg.fmode &= ~FMODE_READ;
2039 if (state->n_wronly == 0) {
2040 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2041 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2042 calldata->arg.fmode &= ~FMODE_WRITE;
2045 spin_unlock(&state->owner->so_lock);
2048 /* Note: exit _without_ calling nfs4_close_done */
2049 task->tk_action = NULL;
2053 if (calldata->arg.fmode == 0) {
2054 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2055 if (calldata->roc &&
2056 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2057 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2063 nfs_fattr_init(calldata->res.fattr);
2064 calldata->timestamp = jiffies;
2065 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
2066 &calldata->arg.seq_args,
2067 &calldata->res.seq_res,
2070 rpc_call_start(task);
2072 dprintk("%s: done!\n", __func__);
2075 static const struct rpc_call_ops nfs4_close_ops = {
2076 .rpc_call_prepare = nfs4_close_prepare,
2077 .rpc_call_done = nfs4_close_done,
2078 .rpc_release = nfs4_free_closedata,
2082 * It is possible for data to be read/written from a mem-mapped file
2083 * after the sys_close call (which hits the vfs layer as a flush).
2084 * This means that we can't safely call nfsv4 close on a file until
2085 * the inode is cleared. This in turn means that we are not good
2086 * NFSv4 citizens - we do not indicate to the server to update the file's
2087 * share state even when we are done with one of the three share
2088 * stateid's in the inode.
2090 * NOTE: Caller must be holding the sp->so_owner semaphore!
2092 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
2094 struct nfs_server *server = NFS_SERVER(state->inode);
2095 struct nfs4_closedata *calldata;
2096 struct nfs4_state_owner *sp = state->owner;
2097 struct rpc_task *task;
2098 struct rpc_message msg = {
2099 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2100 .rpc_cred = state->owner->so_cred,
2102 struct rpc_task_setup task_setup_data = {
2103 .rpc_client = server->client,
2104 .rpc_message = &msg,
2105 .callback_ops = &nfs4_close_ops,
2106 .workqueue = nfsiod_workqueue,
2107 .flags = RPC_TASK_ASYNC,
2109 int status = -ENOMEM;
2111 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2112 if (calldata == NULL)
2114 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2115 calldata->inode = state->inode;
2116 calldata->state = state;
2117 calldata->arg.fh = NFS_FH(state->inode);
2118 calldata->arg.stateid = &state->open_stateid;
2119 /* Serialization for the sequence id */
2120 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2121 if (calldata->arg.seqid == NULL)
2122 goto out_free_calldata;
2123 calldata->arg.fmode = 0;
2124 calldata->arg.bitmask = server->cache_consistency_bitmask;
2125 calldata->res.fattr = &calldata->fattr;
2126 calldata->res.seqid = calldata->arg.seqid;
2127 calldata->res.server = server;
2128 calldata->roc = roc;
2129 nfs_sb_active(calldata->inode->i_sb);
2131 msg.rpc_argp = &calldata->arg;
2132 msg.rpc_resp = &calldata->res;
2133 task_setup_data.callback_data = calldata;
2134 task = rpc_run_task(&task_setup_data);
2136 return PTR_ERR(task);
2139 status = rpc_wait_for_completion_task(task);
2146 pnfs_roc_release(state->inode);
2147 nfs4_put_open_state(state);
2148 nfs4_put_state_owner(sp);
2152 static struct inode *
2153 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2155 struct nfs4_state *state;
2157 /* Protect against concurrent sillydeletes */
2158 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
2160 return ERR_CAST(state);
2162 return igrab(state->inode);
2165 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2167 if (ctx->state == NULL)
2170 nfs4_close_sync(ctx->state, ctx->mode);
2172 nfs4_close_state(ctx->state, ctx->mode);
2175 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2177 struct nfs4_server_caps_arg args = {
2180 struct nfs4_server_caps_res res = {};
2181 struct rpc_message msg = {
2182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2188 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2190 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2191 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2192 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2193 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2194 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2195 NFS_CAP_CTIME|NFS_CAP_MTIME);
2196 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2197 server->caps |= NFS_CAP_ACLS;
2198 if (res.has_links != 0)
2199 server->caps |= NFS_CAP_HARDLINKS;
2200 if (res.has_symlinks != 0)
2201 server->caps |= NFS_CAP_SYMLINKS;
2202 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2203 server->caps |= NFS_CAP_FILEID;
2204 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2205 server->caps |= NFS_CAP_MODE;
2206 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2207 server->caps |= NFS_CAP_NLINK;
2208 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2209 server->caps |= NFS_CAP_OWNER;
2210 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2211 server->caps |= NFS_CAP_OWNER_GROUP;
2212 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2213 server->caps |= NFS_CAP_ATIME;
2214 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2215 server->caps |= NFS_CAP_CTIME;
2216 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2217 server->caps |= NFS_CAP_MTIME;
2219 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2220 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2221 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2222 server->acl_bitmask = res.acl_bitmask;
2223 server->fh_expire_type = res.fh_expire_type;
2229 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2231 struct nfs4_exception exception = { };
2234 err = nfs4_handle_exception(server,
2235 _nfs4_server_capabilities(server, fhandle),
2237 } while (exception.retry);
2241 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2242 struct nfs_fsinfo *info)
2244 struct nfs4_lookup_root_arg args = {
2245 .bitmask = nfs4_fattr_bitmap,
2247 struct nfs4_lookup_res res = {
2249 .fattr = info->fattr,
2252 struct rpc_message msg = {
2253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2258 nfs_fattr_init(info->fattr);
2259 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2262 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2263 struct nfs_fsinfo *info)
2265 struct nfs4_exception exception = { };
2268 err = _nfs4_lookup_root(server, fhandle, info);
2271 case -NFS4ERR_WRONGSEC:
2274 err = nfs4_handle_exception(server, err, &exception);
2276 } while (exception.retry);
2280 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2281 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2283 struct rpc_auth *auth;
2286 auth = rpcauth_create(flavor, server->client);
2291 ret = nfs4_lookup_root(server, fhandle, info);
2296 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2297 struct nfs_fsinfo *info)
2299 int i, len, status = 0;
2300 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2302 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2303 flav_array[len] = RPC_AUTH_NULL;
2306 for (i = 0; i < len; i++) {
2307 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2308 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2313 * -EACCESS could mean that the user doesn't have correct permissions
2314 * to access the mount. It could also mean that we tried to mount
2315 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2316 * existing mount programs don't handle -EACCES very well so it should
2317 * be mapped to -EPERM instead.
2319 if (status == -EACCES)
2325 * get the file handle for the "/" directory on the server
2327 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2328 struct nfs_fsinfo *info)
2330 int minor_version = server->nfs_client->cl_minorversion;
2331 int status = nfs4_lookup_root(server, fhandle, info);
2332 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2334 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2335 * by nfs4_map_errors() as this function exits.
2337 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2339 status = nfs4_server_capabilities(server, fhandle);
2341 status = nfs4_do_fsinfo(server, fhandle, info);
2342 return nfs4_map_errors(status);
2346 * Get locations and (maybe) other attributes of a referral.
2347 * Note that we'll actually follow the referral later when
2348 * we detect fsid mismatch in inode revalidation
2350 static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2351 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2353 int status = -ENOMEM;
2354 struct page *page = NULL;
2355 struct nfs4_fs_locations *locations = NULL;
2357 page = alloc_page(GFP_KERNEL);
2360 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2361 if (locations == NULL)
2364 status = nfs4_proc_fs_locations(dir, name, locations, page);
2367 /* Make sure server returned a different fsid for the referral */
2368 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2369 dprintk("%s: server did not return a different fsid for"
2370 " a referral at %s\n", __func__, name->name);
2374 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2375 nfs_fixup_referral_attributes(&locations->fattr);
2377 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2378 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2379 memset(fhandle, 0, sizeof(struct nfs_fh));
2387 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2389 struct nfs4_getattr_arg args = {
2391 .bitmask = server->attr_bitmask,
2393 struct nfs4_getattr_res res = {
2397 struct rpc_message msg = {
2398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2403 nfs_fattr_init(fattr);
2404 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2407 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2409 struct nfs4_exception exception = { };
2412 err = nfs4_handle_exception(server,
2413 _nfs4_proc_getattr(server, fhandle, fattr),
2415 } while (exception.retry);
2420 * The file is not closed if it is opened due to the a request to change
2421 * the size of the file. The open call will not be needed once the
2422 * VFS layer lookup-intents are implemented.
2424 * Close is called when the inode is destroyed.
2425 * If we haven't opened the file for O_WRONLY, we
2426 * need to in the size_change case to obtain a stateid.
2429 * Because OPEN is always done by name in nfsv4, it is
2430 * possible that we opened a different file by the same
2431 * name. We can recognize this race condition, but we
2432 * can't do anything about it besides returning an error.
2434 * This will be fixed with VFS changes (lookup-intent).
2437 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2438 struct iattr *sattr)
2440 struct inode *inode = dentry->d_inode;
2441 struct rpc_cred *cred = NULL;
2442 struct nfs4_state *state = NULL;
2445 if (pnfs_ld_layoutret_on_setattr(inode))
2446 pnfs_return_layout(inode);
2448 nfs_fattr_init(fattr);
2450 /* Search for an existing open(O_WRITE) file */
2451 if (sattr->ia_valid & ATTR_FILE) {
2452 struct nfs_open_context *ctx;
2454 ctx = nfs_file_open_context(sattr->ia_file);
2461 /* Deal with open(O_TRUNC) */
2462 if (sattr->ia_valid & ATTR_OPEN)
2463 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2465 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2467 nfs_setattr_update_inode(inode, sattr);
2471 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2472 const struct qstr *name, struct nfs_fh *fhandle,
2473 struct nfs_fattr *fattr)
2475 struct nfs_server *server = NFS_SERVER(dir);
2477 struct nfs4_lookup_arg args = {
2478 .bitmask = server->attr_bitmask,
2479 .dir_fh = NFS_FH(dir),
2482 struct nfs4_lookup_res res = {
2487 struct rpc_message msg = {
2488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2493 nfs_fattr_init(fattr);
2495 dprintk("NFS call lookup %s\n", name->name);
2496 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2497 dprintk("NFS reply lookup: %d\n", status);
2501 void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2503 memset(fh, 0, sizeof(struct nfs_fh));
2504 fattr->fsid.major = 1;
2505 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2506 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2507 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2511 static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2512 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2514 struct nfs4_exception exception = { };
2519 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2521 case -NFS4ERR_BADNAME:
2523 case -NFS4ERR_MOVED:
2524 return nfs4_get_referral(dir, name, fattr, fhandle);
2525 case -NFS4ERR_WRONGSEC:
2526 nfs_fixup_secinfo_attributes(fattr, fhandle);
2528 err = nfs4_handle_exception(NFS_SERVER(dir),
2529 status, &exception);
2530 } while (exception.retry);
2534 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2536 struct nfs_server *server = NFS_SERVER(inode);
2537 struct nfs4_accessargs args = {
2538 .fh = NFS_FH(inode),
2539 .bitmask = server->cache_consistency_bitmask,
2541 struct nfs4_accessres res = {
2544 struct rpc_message msg = {
2545 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2548 .rpc_cred = entry->cred,
2550 int mode = entry->mask;
2554 * Determine which access bits we want to ask for...
2556 if (mode & MAY_READ)
2557 args.access |= NFS4_ACCESS_READ;
2558 if (S_ISDIR(inode->i_mode)) {
2559 if (mode & MAY_WRITE)
2560 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2561 if (mode & MAY_EXEC)
2562 args.access |= NFS4_ACCESS_LOOKUP;
2564 if (mode & MAY_WRITE)
2565 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2566 if (mode & MAY_EXEC)
2567 args.access |= NFS4_ACCESS_EXECUTE;
2570 res.fattr = nfs_alloc_fattr();
2571 if (res.fattr == NULL)
2574 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2577 if (res.access & NFS4_ACCESS_READ)
2578 entry->mask |= MAY_READ;
2579 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2580 entry->mask |= MAY_WRITE;
2581 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2582 entry->mask |= MAY_EXEC;
2583 nfs_refresh_inode(inode, res.fattr);
2585 nfs_free_fattr(res.fattr);
2589 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2591 struct nfs4_exception exception = { };
2594 err = nfs4_handle_exception(NFS_SERVER(inode),
2595 _nfs4_proc_access(inode, entry),
2597 } while (exception.retry);
2602 * TODO: For the time being, we don't try to get any attributes
2603 * along with any of the zero-copy operations READ, READDIR,
2606 * In the case of the first three, we want to put the GETATTR
2607 * after the read-type operation -- this is because it is hard
2608 * to predict the length of a GETATTR response in v4, and thus
2609 * align the READ data correctly. This means that the GETATTR
2610 * may end up partially falling into the page cache, and we should
2611 * shift it into the 'tail' of the xdr_buf before processing.
2612 * To do this efficiently, we need to know the total length
2613 * of data received, which doesn't seem to be available outside
2616 * In the case of WRITE, we also want to put the GETATTR after
2617 * the operation -- in this case because we want to make sure
2618 * we get the post-operation mtime and size. This means that
2619 * we can't use xdr_encode_pages() as written: we need a variant
2620 * of it which would leave room in the 'tail' iovec.
2622 * Both of these changes to the XDR layer would in fact be quite
2623 * minor, but I decided to leave them for a subsequent patch.
2625 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2626 unsigned int pgbase, unsigned int pglen)
2628 struct nfs4_readlink args = {
2629 .fh = NFS_FH(inode),
2634 struct nfs4_readlink_res res;
2635 struct rpc_message msg = {
2636 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2641 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2644 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2645 unsigned int pgbase, unsigned int pglen)
2647 struct nfs4_exception exception = { };
2650 err = nfs4_handle_exception(NFS_SERVER(inode),
2651 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2653 } while (exception.retry);
2659 * We will need to arrange for the VFS layer to provide an atomic open.
2660 * Until then, this create/open method is prone to inefficiency and race
2661 * conditions due to the lookup, create, and open VFS calls from sys_open()
2662 * placed on the wire.
2664 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2665 * The file will be opened again in the subsequent VFS open call
2666 * (nfs4_proc_file_open).
2668 * The open for read will just hang around to be used by any process that
2669 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2673 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2674 int flags, struct nfs_open_context *ctx)
2676 struct dentry *de = dentry;
2677 struct nfs4_state *state;
2678 struct rpc_cred *cred = NULL;
2687 sattr->ia_mode &= ~current_umask();
2688 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
2690 if (IS_ERR(state)) {
2691 status = PTR_ERR(state);
2694 d_add(dentry, igrab(state->inode));
2695 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2699 nfs4_close_sync(state, fmode);
2704 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2706 struct nfs_server *server = NFS_SERVER(dir);
2707 struct nfs_removeargs args = {
2709 .name.len = name->len,
2710 .name.name = name->name,
2711 .bitmask = server->attr_bitmask,
2713 struct nfs_removeres res = {
2716 struct rpc_message msg = {
2717 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2721 int status = -ENOMEM;
2723 res.dir_attr = nfs_alloc_fattr();
2724 if (res.dir_attr == NULL)
2727 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2729 update_changeattr(dir, &res.cinfo);
2730 nfs_post_op_update_inode(dir, res.dir_attr);
2732 nfs_free_fattr(res.dir_attr);
2737 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2739 struct nfs4_exception exception = { };
2742 err = nfs4_handle_exception(NFS_SERVER(dir),
2743 _nfs4_proc_remove(dir, name),
2745 } while (exception.retry);
2749 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2751 struct nfs_server *server = NFS_SERVER(dir);
2752 struct nfs_removeargs *args = msg->rpc_argp;
2753 struct nfs_removeres *res = msg->rpc_resp;
2755 args->bitmask = server->cache_consistency_bitmask;
2756 res->server = server;
2757 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2758 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
2761 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2763 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2765 if (!nfs4_sequence_done(task, &res->seq_res))
2767 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2769 update_changeattr(dir, &res->cinfo);
2770 nfs_post_op_update_inode(dir, res->dir_attr);
2774 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2776 struct nfs_server *server = NFS_SERVER(dir);
2777 struct nfs_renameargs *arg = msg->rpc_argp;
2778 struct nfs_renameres *res = msg->rpc_resp;
2780 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2781 arg->bitmask = server->attr_bitmask;
2782 res->server = server;
2783 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
2786 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2787 struct inode *new_dir)
2789 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2791 if (!nfs4_sequence_done(task, &res->seq_res))
2793 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2796 update_changeattr(old_dir, &res->old_cinfo);
2797 nfs_post_op_update_inode(old_dir, res->old_fattr);
2798 update_changeattr(new_dir, &res->new_cinfo);
2799 nfs_post_op_update_inode(new_dir, res->new_fattr);
2803 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2804 struct inode *new_dir, struct qstr *new_name)
2806 struct nfs_server *server = NFS_SERVER(old_dir);
2807 struct nfs_renameargs arg = {
2808 .old_dir = NFS_FH(old_dir),
2809 .new_dir = NFS_FH(new_dir),
2810 .old_name = old_name,
2811 .new_name = new_name,
2812 .bitmask = server->attr_bitmask,
2814 struct nfs_renameres res = {
2817 struct rpc_message msg = {
2818 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2822 int status = -ENOMEM;
2824 res.old_fattr = nfs_alloc_fattr();
2825 res.new_fattr = nfs_alloc_fattr();
2826 if (res.old_fattr == NULL || res.new_fattr == NULL)
2829 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2831 update_changeattr(old_dir, &res.old_cinfo);
2832 nfs_post_op_update_inode(old_dir, res.old_fattr);
2833 update_changeattr(new_dir, &res.new_cinfo);
2834 nfs_post_op_update_inode(new_dir, res.new_fattr);
2837 nfs_free_fattr(res.new_fattr);
2838 nfs_free_fattr(res.old_fattr);
2842 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2843 struct inode *new_dir, struct qstr *new_name)
2845 struct nfs4_exception exception = { };
2848 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2849 _nfs4_proc_rename(old_dir, old_name,
2852 } while (exception.retry);
2856 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2858 struct nfs_server *server = NFS_SERVER(inode);
2859 struct nfs4_link_arg arg = {
2860 .fh = NFS_FH(inode),
2861 .dir_fh = NFS_FH(dir),
2863 .bitmask = server->attr_bitmask,
2865 struct nfs4_link_res res = {
2868 struct rpc_message msg = {
2869 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2873 int status = -ENOMEM;
2875 res.fattr = nfs_alloc_fattr();
2876 res.dir_attr = nfs_alloc_fattr();
2877 if (res.fattr == NULL || res.dir_attr == NULL)
2880 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2882 update_changeattr(dir, &res.cinfo);
2883 nfs_post_op_update_inode(dir, res.dir_attr);
2884 nfs_post_op_update_inode(inode, res.fattr);
2887 nfs_free_fattr(res.dir_attr);
2888 nfs_free_fattr(res.fattr);
2892 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2894 struct nfs4_exception exception = { };
2897 err = nfs4_handle_exception(NFS_SERVER(inode),
2898 _nfs4_proc_link(inode, dir, name),
2900 } while (exception.retry);
2904 struct nfs4_createdata {
2905 struct rpc_message msg;
2906 struct nfs4_create_arg arg;
2907 struct nfs4_create_res res;
2909 struct nfs_fattr fattr;
2910 struct nfs_fattr dir_fattr;
2913 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2914 struct qstr *name, struct iattr *sattr, u32 ftype)
2916 struct nfs4_createdata *data;
2918 data = kzalloc(sizeof(*data), GFP_KERNEL);
2920 struct nfs_server *server = NFS_SERVER(dir);
2922 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2923 data->msg.rpc_argp = &data->arg;
2924 data->msg.rpc_resp = &data->res;
2925 data->arg.dir_fh = NFS_FH(dir);
2926 data->arg.server = server;
2927 data->arg.name = name;
2928 data->arg.attrs = sattr;
2929 data->arg.ftype = ftype;
2930 data->arg.bitmask = server->attr_bitmask;
2931 data->res.server = server;
2932 data->res.fh = &data->fh;
2933 data->res.fattr = &data->fattr;
2934 data->res.dir_fattr = &data->dir_fattr;
2935 nfs_fattr_init(data->res.fattr);
2936 nfs_fattr_init(data->res.dir_fattr);
2941 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2943 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
2944 &data->arg.seq_args, &data->res.seq_res, 1);
2946 update_changeattr(dir, &data->res.dir_cinfo);
2947 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2948 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2953 static void nfs4_free_createdata(struct nfs4_createdata *data)
2958 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2959 struct page *page, unsigned int len, struct iattr *sattr)
2961 struct nfs4_createdata *data;
2962 int status = -ENAMETOOLONG;
2964 if (len > NFS4_MAXPATHLEN)
2968 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2972 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2973 data->arg.u.symlink.pages = &page;
2974 data->arg.u.symlink.len = len;
2976 status = nfs4_do_create(dir, dentry, data);
2978 nfs4_free_createdata(data);
2983 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2984 struct page *page, unsigned int len, struct iattr *sattr)
2986 struct nfs4_exception exception = { };
2989 err = nfs4_handle_exception(NFS_SERVER(dir),
2990 _nfs4_proc_symlink(dir, dentry, page,
2993 } while (exception.retry);
2997 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2998 struct iattr *sattr)
3000 struct nfs4_createdata *data;
3001 int status = -ENOMEM;
3003 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3007 status = nfs4_do_create(dir, dentry, data);
3009 nfs4_free_createdata(data);
3014 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3015 struct iattr *sattr)
3017 struct nfs4_exception exception = { };
3020 sattr->ia_mode &= ~current_umask();
3022 err = nfs4_handle_exception(NFS_SERVER(dir),
3023 _nfs4_proc_mkdir(dir, dentry, sattr),
3025 } while (exception.retry);
3029 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3030 u64 cookie, struct page **pages, unsigned int count, int plus)
3032 struct inode *dir = dentry->d_inode;
3033 struct nfs4_readdir_arg args = {
3038 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3041 struct nfs4_readdir_res res;
3042 struct rpc_message msg = {
3043 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3050 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3051 dentry->d_parent->d_name.name,
3052 dentry->d_name.name,
3053 (unsigned long long)cookie);
3054 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3055 res.pgbase = args.pgbase;
3056 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3058 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
3059 status += args.pgbase;
3062 nfs_invalidate_atime(dir);
3064 dprintk("%s: returns %d\n", __func__, status);
3068 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3069 u64 cookie, struct page **pages, unsigned int count, int plus)
3071 struct nfs4_exception exception = { };
3074 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3075 _nfs4_proc_readdir(dentry, cred, cookie,
3076 pages, count, plus),
3078 } while (exception.retry);
3082 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3083 struct iattr *sattr, dev_t rdev)
3085 struct nfs4_createdata *data;
3086 int mode = sattr->ia_mode;
3087 int status = -ENOMEM;
3089 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3090 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3092 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3097 data->arg.ftype = NF4FIFO;
3098 else if (S_ISBLK(mode)) {
3099 data->arg.ftype = NF4BLK;
3100 data->arg.u.device.specdata1 = MAJOR(rdev);
3101 data->arg.u.device.specdata2 = MINOR(rdev);
3103 else if (S_ISCHR(mode)) {
3104 data->arg.ftype = NF4CHR;
3105 data->arg.u.device.specdata1 = MAJOR(rdev);
3106 data->arg.u.device.specdata2 = MINOR(rdev);
3109 status = nfs4_do_create(dir, dentry, data);
3111 nfs4_free_createdata(data);
3116 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3117 struct iattr *sattr, dev_t rdev)
3119 struct nfs4_exception exception = { };
3122 sattr->ia_mode &= ~current_umask();
3124 err = nfs4_handle_exception(NFS_SERVER(dir),
3125 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3127 } while (exception.retry);
3131 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3132 struct nfs_fsstat *fsstat)
3134 struct nfs4_statfs_arg args = {
3136 .bitmask = server->attr_bitmask,
3138 struct nfs4_statfs_res res = {
3141 struct rpc_message msg = {
3142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3147 nfs_fattr_init(fsstat->fattr);
3148 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3151 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3153 struct nfs4_exception exception = { };
3156 err = nfs4_handle_exception(server,
3157 _nfs4_proc_statfs(server, fhandle, fsstat),
3159 } while (exception.retry);
3163 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3164 struct nfs_fsinfo *fsinfo)
3166 struct nfs4_fsinfo_arg args = {
3168 .bitmask = server->attr_bitmask,
3170 struct nfs4_fsinfo_res res = {
3173 struct rpc_message msg = {
3174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3179 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3182 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3184 struct nfs4_exception exception = { };
3188 err = nfs4_handle_exception(server,
3189 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3191 } while (exception.retry);
3195 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3197 nfs_fattr_init(fsinfo->fattr);
3198 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3201 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3202 struct nfs_pathconf *pathconf)
3204 struct nfs4_pathconf_arg args = {
3206 .bitmask = server->attr_bitmask,
3208 struct nfs4_pathconf_res res = {
3209 .pathconf = pathconf,
3211 struct rpc_message msg = {
3212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3217 /* None of the pathconf attributes are mandatory to implement */
3218 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3219 memset(pathconf, 0, sizeof(*pathconf));
3223 nfs_fattr_init(pathconf->fattr);
3224 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3227 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3228 struct nfs_pathconf *pathconf)
3230 struct nfs4_exception exception = { };
3234 err = nfs4_handle_exception(server,
3235 _nfs4_proc_pathconf(server, fhandle, pathconf),
3237 } while (exception.retry);
3241 void __nfs4_read_done_cb(struct nfs_read_data *data)
3243 nfs_invalidate_atime(data->inode);
3246 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3248 struct nfs_server *server = NFS_SERVER(data->inode);
3250 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3251 rpc_restart_call_prepare(task);
3255 __nfs4_read_done_cb(data);
3256 if (task->tk_status > 0)
3257 renew_lease(server, data->timestamp);
3261 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3264 dprintk("--> %s\n", __func__);
3266 if (!nfs4_sequence_done(task, &data->res.seq_res))
3269 return data->read_done_cb ? data->read_done_cb(task, data) :
3270 nfs4_read_done_cb(task, data);
3273 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3275 data->timestamp = jiffies;
3276 data->read_done_cb = nfs4_read_done_cb;
3277 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3278 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3281 /* Reset the the nfs_read_data to send the read to the MDS. */
3282 void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3284 dprintk("%s Reset task for i/o through\n", __func__);
3285 put_lseg(data->lseg);
3287 /* offsets will differ in the dense stripe case */
3288 data->args.offset = data->mds_offset;
3289 data->ds_clp = NULL;
3290 data->args.fh = NFS_FH(data->inode);
3291 data->read_done_cb = nfs4_read_done_cb;
3292 task->tk_ops = data->mds_ops;
3293 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3295 EXPORT_SYMBOL_GPL(nfs4_reset_read);
3297 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3299 struct inode *inode = data->inode;
3301 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3302 rpc_restart_call_prepare(task);
3305 if (task->tk_status >= 0) {
3306 renew_lease(NFS_SERVER(inode), data->timestamp);
3307 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3312 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3314 if (!nfs4_sequence_done(task, &data->res.seq_res))
3316 return data->write_done_cb ? data->write_done_cb(task, data) :
3317 nfs4_write_done_cb(task, data);
3320 /* Reset the the nfs_write_data to send the write to the MDS. */
3321 void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3323 dprintk("%s Reset task for i/o through\n", __func__);
3324 put_lseg(data->lseg);
3326 data->ds_clp = NULL;
3327 data->write_done_cb = nfs4_write_done_cb;
3328 data->args.fh = NFS_FH(data->inode);
3329 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3330 data->args.offset = data->mds_offset;
3331 data->res.fattr = &data->fattr;
3332 task->tk_ops = data->mds_ops;
3333 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3335 EXPORT_SYMBOL_GPL(nfs4_reset_write);
3337 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3339 struct nfs_server *server = NFS_SERVER(data->inode);
3342 data->args.bitmask = NULL;
3343 data->res.fattr = NULL;
3345 data->args.bitmask = server->cache_consistency_bitmask;
3346 if (!data->write_done_cb)
3347 data->write_done_cb = nfs4_write_done_cb;
3348 data->res.server = server;
3349 data->timestamp = jiffies;
3351 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3352 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3355 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3357 struct inode *inode = data->inode;
3359 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3360 rpc_restart_call_prepare(task);
3363 nfs_refresh_inode(inode, data->res.fattr);
3367 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3369 if (!nfs4_sequence_done(task, &data->res.seq_res))
3371 return data->write_done_cb(task, data);
3374 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3376 struct nfs_server *server = NFS_SERVER(data->inode);
3379 data->args.bitmask = NULL;
3380 data->res.fattr = NULL;
3382 data->args.bitmask = server->cache_consistency_bitmask;
3383 if (!data->write_done_cb)
3384 data->write_done_cb = nfs4_commit_done_cb;
3385 data->res.server = server;
3386 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3387 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3390 struct nfs4_renewdata {
3391 struct nfs_client *client;
3392 unsigned long timestamp;
3396 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3397 * standalone procedure for queueing an asynchronous RENEW.
3399 static void nfs4_renew_release(void *calldata)
3401 struct nfs4_renewdata *data = calldata;
3402 struct nfs_client *clp = data->client;
3404 if (atomic_read(&clp->cl_count) > 1)
3405 nfs4_schedule_state_renewal(clp);
3406 nfs_put_client(clp);
3410 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3412 struct nfs4_renewdata *data = calldata;
3413 struct nfs_client *clp = data->client;
3414 unsigned long timestamp = data->timestamp;
3416 if (task->tk_status < 0) {
3417 /* Unless we're shutting down, schedule state recovery! */
3418 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3420 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3421 nfs4_schedule_lease_recovery(clp);
3424 nfs4_schedule_path_down_recovery(clp);
3426 do_renew_lease(clp, timestamp);
3429 static const struct rpc_call_ops nfs4_renew_ops = {
3430 .rpc_call_done = nfs4_renew_done,
3431 .rpc_release = nfs4_renew_release,
3434 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3436 struct rpc_message msg = {
3437 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3441 struct nfs4_renewdata *data;
3443 if (renew_flags == 0)
3445 if (!atomic_inc_not_zero(&clp->cl_count))
3447 data = kmalloc(sizeof(*data), GFP_NOFS);
3451 data->timestamp = jiffies;
3452 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3453 &nfs4_renew_ops, data);
3456 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3458 struct rpc_message msg = {
3459 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3463 unsigned long now = jiffies;
3466 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3469 do_renew_lease(clp, now);
3473 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3475 return (server->caps & NFS_CAP_ACLS)
3476 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3477 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3480 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3481 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3484 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3486 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3487 struct page **pages, unsigned int *pgbase)
3489 struct page *newpage, **spages;
3495 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3496 newpage = alloc_page(GFP_KERNEL);
3498 if (newpage == NULL)
3500 memcpy(page_address(newpage), buf, len);
3505 } while (buflen != 0);
3511 __free_page(spages[rc-1]);
3515 struct nfs4_cached_acl {
3521 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3523 struct nfs_inode *nfsi = NFS_I(inode);
3525 spin_lock(&inode->i_lock);
3526 kfree(nfsi->nfs4_acl);
3527 nfsi->nfs4_acl = acl;
3528 spin_unlock(&inode->i_lock);
3531 static void nfs4_zap_acl_attr(struct inode *inode)
3533 nfs4_set_cached_acl(inode, NULL);
3536 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3538 struct nfs_inode *nfsi = NFS_I(inode);
3539 struct nfs4_cached_acl *acl;
3542 spin_lock(&inode->i_lock);
3543 acl = nfsi->nfs4_acl;
3546 if (buf == NULL) /* user is just asking for length */
3548 if (acl->cached == 0)
3550 ret = -ERANGE; /* see getxattr(2) man page */
3551 if (acl->len > buflen)
3553 memcpy(buf, acl->data, acl->len);
3557 spin_unlock(&inode->i_lock);
3561 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3563 struct nfs4_cached_acl *acl;
3565 if (buf && acl_len <= PAGE_SIZE) {
3566 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3570 memcpy(acl->data, buf, acl_len);
3572 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3579 nfs4_set_cached_acl(inode, acl);
3583 * The getxattr API returns the required buffer length when called with a
3584 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3585 * the required buf. On a NULL buf, we send a page of data to the server
3586 * guessing that the ACL request can be serviced by a page. If so, we cache
3587 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3588 * the cache. If not so, we throw away the page, and cache the required
3589 * length. The next getxattr call will then produce another round trip to
3590 * the server, this time with the input buf of the required size.
3592 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3594 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3595 struct nfs_getaclargs args = {
3596 .fh = NFS_FH(inode),
3600 struct nfs_getaclres res = {
3604 struct rpc_message msg = {
3605 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3609 int ret = -ENOMEM, npages, i, acl_len = 0;
3611 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3612 /* As long as we're doing a round trip to the server anyway,
3613 * let's be prepared for a page of acl data. */
3617 for (i = 0; i < npages; i++) {
3618 pages[i] = alloc_page(GFP_KERNEL);
3623 /* for decoding across pages */
3624 args.acl_scratch = alloc_page(GFP_KERNEL);
3625 if (!args.acl_scratch)
3628 args.acl_len = npages * PAGE_SIZE;
3629 args.acl_pgbase = 0;
3630 /* Let decode_getfacl know not to fail if the ACL data is larger than
3631 * the page we send as a guess */
3633 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3634 resp_buf = page_address(pages[0]);
3636 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3637 __func__, buf, buflen, npages, args.acl_len);
3638 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3639 &msg, &args.seq_args, &res.seq_res, 0);
3643 acl_len = res.acl_len - res.acl_data_offset;
3644 if (acl_len > args.acl_len)
3645 nfs4_write_cached_acl(inode, NULL, acl_len);
3647 nfs4_write_cached_acl(inode, resp_buf + res.acl_data_offset,
3651 if (acl_len > buflen)
3653 _copy_from_pages(buf, pages, res.acl_data_offset,
3658 for (i = 0; i < npages; i++)
3660 __free_page(pages[i]);
3661 if (args.acl_scratch)
3662 __free_page(args.acl_scratch);
3666 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3668 struct nfs4_exception exception = { };
3671 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3674 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3675 } while (exception.retry);
3679 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3681 struct nfs_server *server = NFS_SERVER(inode);
3684 if (!nfs4_server_supports_acls(server))
3686 ret = nfs_revalidate_inode(server, inode);
3689 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3690 nfs_zap_acl_cache(inode);
3691 ret = nfs4_read_cached_acl(inode, buf, buflen);
3693 /* -ENOENT is returned if there is no ACL or if there is an ACL
3694 * but no cached acl data, just the acl length */
3696 return nfs4_get_acl_uncached(inode, buf, buflen);
3699 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3701 struct nfs_server *server = NFS_SERVER(inode);
3702 struct page *pages[NFS4ACL_MAXPAGES];
3703 struct nfs_setaclargs arg = {
3704 .fh = NFS_FH(inode),
3708 struct nfs_setaclres res;
3709 struct rpc_message msg = {
3710 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3716 if (!nfs4_server_supports_acls(server))
3718 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3721 nfs_inode_return_delegation(inode);
3722 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3725 * Free each page after tx, so the only ref left is
3726 * held by the network stack
3729 put_page(pages[i-1]);
3732 * Acl update can result in inode attribute update.
3733 * so mark the attribute cache invalid.
3735 spin_lock(&inode->i_lock);
3736 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3737 spin_unlock(&inode->i_lock);
3738 nfs_access_zap_cache(inode);
3739 nfs_zap_acl_cache(inode);
3743 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3745 struct nfs4_exception exception = { };
3748 err = nfs4_handle_exception(NFS_SERVER(inode),
3749 __nfs4_proc_set_acl(inode, buf, buflen),
3751 } while (exception.retry);
3756 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3758 struct nfs_client *clp = server->nfs_client;
3760 if (task->tk_status >= 0)
3762 switch(task->tk_status) {
3763 case -NFS4ERR_ADMIN_REVOKED:
3764 case -NFS4ERR_BAD_STATEID:
3765 case -NFS4ERR_OPENMODE:
3768 nfs4_schedule_stateid_recovery(server, state);
3769 goto wait_on_recovery;
3770 case -NFS4ERR_EXPIRED:
3772 nfs4_schedule_stateid_recovery(server, state);
3773 case -NFS4ERR_STALE_STATEID:
3774 case -NFS4ERR_STALE_CLIENTID:
3775 nfs4_schedule_lease_recovery(clp);
3776 goto wait_on_recovery;
3777 #if defined(CONFIG_NFS_V4_1)
3778 case -NFS4ERR_BADSESSION:
3779 case -NFS4ERR_BADSLOT:
3780 case -NFS4ERR_BAD_HIGH_SLOT:
3781 case -NFS4ERR_DEADSESSION:
3782 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3783 case -NFS4ERR_SEQ_FALSE_RETRY:
3784 case -NFS4ERR_SEQ_MISORDERED:
3785 dprintk("%s ERROR %d, Reset session\n", __func__,
3787 nfs4_schedule_session_recovery(clp->cl_session);
3788 task->tk_status = 0;
3790 #endif /* CONFIG_NFS_V4_1 */
3791 case -NFS4ERR_DELAY:
3792 nfs_inc_server_stats(server, NFSIOS_DELAY);
3793 case -NFS4ERR_GRACE:
3795 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3796 task->tk_status = 0;
3798 case -NFS4ERR_RETRY_UNCACHED_REP:
3799 case -NFS4ERR_OLD_STATEID:
3800 task->tk_status = 0;
3803 task->tk_status = nfs4_map_errors(task->tk_status);
3806 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3807 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3808 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3809 task->tk_status = 0;
3813 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3814 unsigned short port, struct rpc_cred *cred,
3815 struct nfs4_setclientid_res *res)
3817 nfs4_verifier sc_verifier;
3818 struct nfs4_setclientid setclientid = {
3819 .sc_verifier = &sc_verifier,
3821 .sc_cb_ident = clp->cl_cb_ident,
3823 struct rpc_message msg = {
3824 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3825 .rpc_argp = &setclientid,
3833 p = (__be32*)sc_verifier.data;
3834 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3835 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3839 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3840 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3842 rpc_peeraddr2str(clp->cl_rpcclient,
3844 rpc_peeraddr2str(clp->cl_rpcclient,
3846 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3847 clp->cl_id_uniquifier);
3848 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3849 sizeof(setclientid.sc_netid),
3850 rpc_peeraddr2str(clp->cl_rpcclient,
3851 RPC_DISPLAY_NETID));
3852 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3853 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3854 clp->cl_ipaddr, port >> 8, port & 255);
3857 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3858 if (status != -NFS4ERR_CLID_INUSE)
3861 ++clp->cl_id_uniquifier;
3865 ssleep(clp->cl_lease_time / HZ + 1);
3870 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3871 struct nfs4_setclientid_res *arg,
3872 struct rpc_cred *cred)
3874 struct nfs_fsinfo fsinfo;
3875 struct rpc_message msg = {
3876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3878 .rpc_resp = &fsinfo,
3885 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3887 spin_lock(&clp->cl_lock);
3888 clp->cl_lease_time = fsinfo.lease_time * HZ;
3889 clp->cl_last_renewal = now;
3890 spin_unlock(&clp->cl_lock);
3895 struct nfs4_delegreturndata {
3896 struct nfs4_delegreturnargs args;
3897 struct nfs4_delegreturnres res;
3899 nfs4_stateid stateid;
3900 unsigned long timestamp;
3901 struct nfs_fattr fattr;
3905 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3907 struct nfs4_delegreturndata *data = calldata;
3909 if (!nfs4_sequence_done(task, &data->res.seq_res))
3912 switch (task->tk_status) {
3913 case -NFS4ERR_STALE_STATEID:
3914 case -NFS4ERR_EXPIRED:
3916 renew_lease(data->res.server, data->timestamp);
3919 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3921 rpc_restart_call_prepare(task);
3925 data->rpc_status = task->tk_status;
3928 static void nfs4_delegreturn_release(void *calldata)
3933 #if defined(CONFIG_NFS_V4_1)
3934 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3936 struct nfs4_delegreturndata *d_data;
3938 d_data = (struct nfs4_delegreturndata *)data;
3940 if (nfs4_setup_sequence(d_data->res.server,
3941 &d_data->args.seq_args,
3942 &d_data->res.seq_res, task))
3944 rpc_call_start(task);
3946 #endif /* CONFIG_NFS_V4_1 */
3948 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3949 #if defined(CONFIG_NFS_V4_1)
3950 .rpc_call_prepare = nfs4_delegreturn_prepare,
3951 #endif /* CONFIG_NFS_V4_1 */
3952 .rpc_call_done = nfs4_delegreturn_done,
3953 .rpc_release = nfs4_delegreturn_release,
3956 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3958 struct nfs4_delegreturndata *data;
3959 struct nfs_server *server = NFS_SERVER(inode);
3960 struct rpc_task *task;
3961 struct rpc_message msg = {
3962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3965 struct rpc_task_setup task_setup_data = {
3966 .rpc_client = server->client,
3967 .rpc_message = &msg,
3968 .callback_ops = &nfs4_delegreturn_ops,
3969 .flags = RPC_TASK_ASYNC,
3973 data = kzalloc(sizeof(*data), GFP_NOFS);
3976 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3977 data->args.fhandle = &data->fh;
3978 data->args.stateid = &data->stateid;
3979 data->args.bitmask = server->attr_bitmask;
3980 nfs_copy_fh(&data->fh, NFS_FH(inode));
3981 memcpy(&data->stateid, stateid, sizeof(data->stateid));
3982 data->res.fattr = &data->fattr;
3983 data->res.server = server;
3984 nfs_fattr_init(data->res.fattr);
3985 data->timestamp = jiffies;
3986 data->rpc_status = 0;
3988 task_setup_data.callback_data = data;
3989 msg.rpc_argp = &data->args;
3990 msg.rpc_resp = &data->res;
3991 task = rpc_run_task(&task_setup_data);
3993 return PTR_ERR(task);
3996 status = nfs4_wait_for_completion_rpc_task(task);
3999 status = data->rpc_status;
4002 nfs_refresh_inode(inode, &data->fattr);
4008 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4010 struct nfs_server *server = NFS_SERVER(inode);
4011 struct nfs4_exception exception = { };
4014 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4016 case -NFS4ERR_STALE_STATEID:
4017 case -NFS4ERR_EXPIRED:
4021 err = nfs4_handle_exception(server, err, &exception);
4022 } while (exception.retry);
4026 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4027 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4030 * sleep, with exponential backoff, and retry the LOCK operation.
4032 static unsigned long
4033 nfs4_set_lock_task_retry(unsigned long timeout)
4035 freezable_schedule_timeout_killable(timeout);
4037 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4038 return NFS4_LOCK_MAXTIMEOUT;
4042 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4044 struct inode *inode = state->inode;
4045 struct nfs_server *server = NFS_SERVER(inode);
4046 struct nfs_client *clp = server->nfs_client;
4047 struct nfs_lockt_args arg = {
4048 .fh = NFS_FH(inode),
4051 struct nfs_lockt_res res = {
4054 struct rpc_message msg = {
4055 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4058 .rpc_cred = state->owner->so_cred,
4060 struct nfs4_lock_state *lsp;
4063 arg.lock_owner.clientid = clp->cl_clientid;
4064 status = nfs4_set_lock_state(state, request);
4067 lsp = request->fl_u.nfs4_fl.owner;
4068 arg.lock_owner.id = lsp->ls_seqid.owner_id;
4069 arg.lock_owner.s_dev = server->s_dev;
4070 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4073 request->fl_type = F_UNLCK;
4075 case -NFS4ERR_DENIED:
4078 request->fl_ops->fl_release_private(request);
4083 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4085 struct nfs4_exception exception = { };
4089 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4090 _nfs4_proc_getlk(state, cmd, request),
4092 } while (exception.retry);
4096 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4099 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4101 res = posix_lock_file_wait(file, fl);
4104 res = flock_lock_file_wait(file, fl);
4112 struct nfs4_unlockdata {
4113 struct nfs_locku_args arg;
4114 struct nfs_locku_res res;
4115 struct nfs4_lock_state *lsp;
4116 struct nfs_open_context *ctx;
4117 struct file_lock fl;
4118 const struct nfs_server *server;
4119 unsigned long timestamp;
4122 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4123 struct nfs_open_context *ctx,
4124 struct nfs4_lock_state *lsp,
4125 struct nfs_seqid *seqid)
4127 struct nfs4_unlockdata *p;
4128 struct inode *inode = lsp->ls_state->inode;
4130 p = kzalloc(sizeof(*p), GFP_NOFS);
4133 p->arg.fh = NFS_FH(inode);
4135 p->arg.seqid = seqid;
4136 p->res.seqid = seqid;
4137 p->arg.stateid = &lsp->ls_stateid;
4139 atomic_inc(&lsp->ls_count);
4140 /* Ensure we don't close file until we're done freeing locks! */
4141 p->ctx = get_nfs_open_context(ctx);
4142 memcpy(&p->fl, fl, sizeof(p->fl));
4143 p->server = NFS_SERVER(inode);
4147 static void nfs4_locku_release_calldata(void *data)
4149 struct nfs4_unlockdata *calldata = data;
4150 nfs_free_seqid(calldata->arg.seqid);
4151 nfs4_put_lock_state(calldata->lsp);
4152 put_nfs_open_context(calldata->ctx);
4156 static void nfs4_locku_done(struct rpc_task *task, void *data)
4158 struct nfs4_unlockdata *calldata = data;
4160 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4162 switch (task->tk_status) {
4164 memcpy(calldata->lsp->ls_stateid.data,
4165 calldata->res.stateid.data,
4166 sizeof(calldata->lsp->ls_stateid.data));
4167 renew_lease(calldata->server, calldata->timestamp);
4169 case -NFS4ERR_BAD_STATEID:
4170 case -NFS4ERR_OLD_STATEID:
4171 case -NFS4ERR_STALE_STATEID:
4172 case -NFS4ERR_EXPIRED:
4175 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4176 rpc_restart_call_prepare(task);
4180 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4182 struct nfs4_unlockdata *calldata = data;
4184 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4186 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
4187 /* Note: exit _without_ running nfs4_locku_done */
4188 task->tk_action = NULL;
4191 calldata->timestamp = jiffies;
4192 if (nfs4_setup_sequence(calldata->server,
4193 &calldata->arg.seq_args,
4194 &calldata->res.seq_res, task))
4196 rpc_call_start(task);
4199 static const struct rpc_call_ops nfs4_locku_ops = {
4200 .rpc_call_prepare = nfs4_locku_prepare,
4201 .rpc_call_done = nfs4_locku_done,
4202 .rpc_release = nfs4_locku_release_calldata,
4205 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4206 struct nfs_open_context *ctx,
4207 struct nfs4_lock_state *lsp,
4208 struct nfs_seqid *seqid)
4210 struct nfs4_unlockdata *data;
4211 struct rpc_message msg = {
4212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4213 .rpc_cred = ctx->cred,
4215 struct rpc_task_setup task_setup_data = {
4216 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4217 .rpc_message = &msg,
4218 .callback_ops = &nfs4_locku_ops,
4219 .workqueue = nfsiod_workqueue,
4220 .flags = RPC_TASK_ASYNC,
4223 /* Ensure this is an unlock - when canceling a lock, the
4224 * canceled lock is passed in, and it won't be an unlock.
4226 fl->fl_type = F_UNLCK;
4228 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4230 nfs_free_seqid(seqid);
4231 return ERR_PTR(-ENOMEM);
4234 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4235 msg.rpc_argp = &data->arg;
4236 msg.rpc_resp = &data->res;
4237 task_setup_data.callback_data = data;
4238 return rpc_run_task(&task_setup_data);
4241 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4243 struct nfs_inode *nfsi = NFS_I(state->inode);
4244 struct nfs_seqid *seqid;
4245 struct nfs4_lock_state *lsp;
4246 struct rpc_task *task;
4248 unsigned char fl_flags = request->fl_flags;
4250 status = nfs4_set_lock_state(state, request);
4251 /* Unlock _before_ we do the RPC call */
4252 request->fl_flags |= FL_EXISTS;
4253 down_read(&nfsi->rwsem);
4254 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4255 up_read(&nfsi->rwsem);
4258 up_read(&nfsi->rwsem);
4261 /* Is this a delegated lock? */
4262 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4264 lsp = request->fl_u.nfs4_fl.owner;
4265 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4269 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4270 status = PTR_ERR(task);
4273 status = nfs4_wait_for_completion_rpc_task(task);
4276 request->fl_flags = fl_flags;
4280 struct nfs4_lockdata {
4281 struct nfs_lock_args arg;
4282 struct nfs_lock_res res;
4283 struct nfs4_lock_state *lsp;
4284 struct nfs_open_context *ctx;
4285 struct file_lock fl;
4286 unsigned long timestamp;
4289 struct nfs_server *server;
4292 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4293 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4296 struct nfs4_lockdata *p;
4297 struct inode *inode = lsp->ls_state->inode;
4298 struct nfs_server *server = NFS_SERVER(inode);
4300 p = kzalloc(sizeof(*p), gfp_mask);
4304 p->arg.fh = NFS_FH(inode);
4306 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4307 if (p->arg.open_seqid == NULL)
4309 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4310 if (p->arg.lock_seqid == NULL)
4311 goto out_free_seqid;
4312 p->arg.lock_stateid = &lsp->ls_stateid;
4313 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4314 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4315 p->arg.lock_owner.s_dev = server->s_dev;
4316 p->res.lock_seqid = p->arg.lock_seqid;
4319 atomic_inc(&lsp->ls_count);
4320 p->ctx = get_nfs_open_context(ctx);
4321 memcpy(&p->fl, fl, sizeof(p->fl));
4324 nfs_free_seqid(p->arg.open_seqid);
4330 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4332 struct nfs4_lockdata *data = calldata;
4333 struct nfs4_state *state = data->lsp->ls_state;
4335 dprintk("%s: begin!\n", __func__);
4336 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4338 /* Do we need to do an open_to_lock_owner? */
4339 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4340 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4342 data->arg.open_stateid = &state->stateid;
4343 data->arg.new_lock_owner = 1;
4344 data->res.open_seqid = data->arg.open_seqid;
4346 data->arg.new_lock_owner = 0;
4347 data->timestamp = jiffies;
4348 if (nfs4_setup_sequence(data->server,
4349 &data->arg.seq_args,
4350 &data->res.seq_res, task))
4352 rpc_call_start(task);
4353 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4356 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4358 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4359 nfs4_lock_prepare(task, calldata);
4362 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4364 struct nfs4_lockdata *data = calldata;
4366 dprintk("%s: begin!\n", __func__);
4368 if (!nfs4_sequence_done(task, &data->res.seq_res))
4371 data->rpc_status = task->tk_status;
4372 if (data->arg.new_lock_owner != 0) {
4373 if (data->rpc_status == 0)
4374 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4378 if (data->rpc_status == 0) {
4379 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4380 sizeof(data->lsp->ls_stateid.data));
4381 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4382 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4385 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4388 static void nfs4_lock_release(void *calldata)
4390 struct nfs4_lockdata *data = calldata;
4392 dprintk("%s: begin!\n", __func__);
4393 nfs_free_seqid(data->arg.open_seqid);
4394 if (data->cancelled != 0) {
4395 struct rpc_task *task;
4396 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4397 data->arg.lock_seqid);
4399 rpc_put_task_async(task);
4400 dprintk("%s: cancelling lock!\n", __func__);
4402 nfs_free_seqid(data->arg.lock_seqid);
4403 nfs4_put_lock_state(data->lsp);
4404 put_nfs_open_context(data->ctx);
4406 dprintk("%s: done!\n", __func__);
4409 static const struct rpc_call_ops nfs4_lock_ops = {
4410 .rpc_call_prepare = nfs4_lock_prepare,
4411 .rpc_call_done = nfs4_lock_done,
4412 .rpc_release = nfs4_lock_release,
4415 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4416 .rpc_call_prepare = nfs4_recover_lock_prepare,
4417 .rpc_call_done = nfs4_lock_done,
4418 .rpc_release = nfs4_lock_release,
4421 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4424 case -NFS4ERR_ADMIN_REVOKED:
4425 case -NFS4ERR_BAD_STATEID:
4426 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4427 if (new_lock_owner != 0 ||
4428 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4429 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4431 case -NFS4ERR_STALE_STATEID:
4432 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4433 case -NFS4ERR_EXPIRED:
4434 nfs4_schedule_lease_recovery(server->nfs_client);
4438 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4440 struct nfs4_lockdata *data;
4441 struct rpc_task *task;
4442 struct rpc_message msg = {
4443 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4444 .rpc_cred = state->owner->so_cred,
4446 struct rpc_task_setup task_setup_data = {
4447 .rpc_client = NFS_CLIENT(state->inode),
4448 .rpc_message = &msg,
4449 .callback_ops = &nfs4_lock_ops,
4450 .workqueue = nfsiod_workqueue,
4451 .flags = RPC_TASK_ASYNC,
4455 dprintk("%s: begin!\n", __func__);
4456 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4457 fl->fl_u.nfs4_fl.owner,
4458 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4462 data->arg.block = 1;
4463 if (recovery_type > NFS_LOCK_NEW) {
4464 if (recovery_type == NFS_LOCK_RECLAIM)
4465 data->arg.reclaim = NFS_LOCK_RECLAIM;
4466 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4468 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4469 msg.rpc_argp = &data->arg;
4470 msg.rpc_resp = &data->res;
4471 task_setup_data.callback_data = data;
4472 task = rpc_run_task(&task_setup_data);
4474 return PTR_ERR(task);
4475 ret = nfs4_wait_for_completion_rpc_task(task);
4477 ret = data->rpc_status;
4479 nfs4_handle_setlk_error(data->server, data->lsp,
4480 data->arg.new_lock_owner, ret);
4482 data->cancelled = 1;
4484 dprintk("%s: done, ret = %d!\n", __func__, ret);
4488 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4490 struct nfs_server *server = NFS_SERVER(state->inode);
4491 struct nfs4_exception exception = { };
4495 /* Cache the lock if possible... */
4496 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4498 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4499 if (err != -NFS4ERR_DELAY)
4501 nfs4_handle_exception(server, err, &exception);
4502 } while (exception.retry);
4506 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4508 struct nfs_server *server = NFS_SERVER(state->inode);
4509 struct nfs4_exception exception = { };
4512 err = nfs4_set_lock_state(state, request);
4516 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4518 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4522 case -NFS4ERR_GRACE:
4523 case -NFS4ERR_DELAY:
4524 nfs4_handle_exception(server, err, &exception);
4527 } while (exception.retry);
4532 #if defined(CONFIG_NFS_V4_1)
4533 static int nfs41_check_expired_locks(struct nfs4_state *state)
4535 int status, ret = NFS_OK;
4536 struct nfs4_lock_state *lsp;
4537 struct nfs_server *server = NFS_SERVER(state->inode);
4539 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4540 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4541 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4542 if (status != NFS_OK) {
4543 nfs41_free_stateid(server, &lsp->ls_stateid);
4544 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4553 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4555 int status = NFS_OK;
4557 if (test_bit(LK_STATE_IN_USE, &state->flags))
4558 status = nfs41_check_expired_locks(state);
4559 if (status == NFS_OK)
4561 return nfs4_lock_expired(state, request);
4565 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4567 struct nfs_inode *nfsi = NFS_I(state->inode);
4568 unsigned char fl_flags = request->fl_flags;
4569 int status = -ENOLCK;
4571 if ((fl_flags & FL_POSIX) &&
4572 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4574 /* Is this a delegated open? */
4575 status = nfs4_set_lock_state(state, request);
4578 request->fl_flags |= FL_ACCESS;
4579 status = do_vfs_lock(request->fl_file, request);
4582 down_read(&nfsi->rwsem);
4583 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4584 /* Yes: cache locks! */
4585 /* ...but avoid races with delegation recall... */
4586 request->fl_flags = fl_flags & ~FL_SLEEP;
4587 status = do_vfs_lock(request->fl_file, request);
4590 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4593 /* Note: we always want to sleep here! */
4594 request->fl_flags = fl_flags | FL_SLEEP;
4595 if (do_vfs_lock(request->fl_file, request) < 0)
4596 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4597 "manager!\n", __func__);
4599 up_read(&nfsi->rwsem);
4601 request->fl_flags = fl_flags;
4605 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4607 struct nfs4_exception exception = { };
4611 err = _nfs4_proc_setlk(state, cmd, request);
4612 if (err == -NFS4ERR_DENIED)
4614 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4616 } while (exception.retry);
4621 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4623 struct nfs_open_context *ctx;
4624 struct nfs4_state *state;
4625 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4628 /* verify open state */
4629 ctx = nfs_file_open_context(filp);
4632 if (request->fl_start < 0 || request->fl_end < 0)
4635 if (IS_GETLK(cmd)) {
4637 return nfs4_proc_getlk(state, F_GETLK, request);
4641 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4644 if (request->fl_type == F_UNLCK) {
4646 return nfs4_proc_unlck(state, cmd, request);
4653 status = nfs4_proc_setlk(state, cmd, request);
4654 if ((status != -EAGAIN) || IS_SETLK(cmd))
4656 timeout = nfs4_set_lock_task_retry(timeout);
4657 status = -ERESTARTSYS;
4660 } while(status < 0);
4664 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4666 struct nfs_server *server = NFS_SERVER(state->inode);
4667 struct nfs4_exception exception = { };
4670 err = nfs4_set_lock_state(state, fl);
4674 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4677 printk(KERN_ERR "NFS: %s: unhandled error "
4678 "%d.\n", __func__, err);
4682 case -NFS4ERR_EXPIRED:
4683 nfs4_schedule_stateid_recovery(server, state);
4684 case -NFS4ERR_STALE_CLIENTID:
4685 case -NFS4ERR_STALE_STATEID:
4686 nfs4_schedule_lease_recovery(server->nfs_client);
4688 case -NFS4ERR_BADSESSION:
4689 case -NFS4ERR_BADSLOT:
4690 case -NFS4ERR_BAD_HIGH_SLOT:
4691 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4692 case -NFS4ERR_DEADSESSION:
4693 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
4697 * The show must go on: exit, but mark the
4698 * stateid as needing recovery.
4700 case -NFS4ERR_ADMIN_REVOKED:
4701 case -NFS4ERR_BAD_STATEID:
4702 case -NFS4ERR_OPENMODE:
4703 nfs4_schedule_stateid_recovery(server, state);
4708 * User RPCSEC_GSS context has expired.
4709 * We cannot recover this stateid now, so
4710 * skip it and allow recovery thread to
4716 case -NFS4ERR_DENIED:
4717 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4720 case -NFS4ERR_DELAY:
4723 err = nfs4_handle_exception(server, err, &exception);
4724 } while (exception.retry);
4729 static void nfs4_release_lockowner_release(void *calldata)
4734 const struct rpc_call_ops nfs4_release_lockowner_ops = {
4735 .rpc_release = nfs4_release_lockowner_release,
4738 void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4740 struct nfs_server *server = lsp->ls_state->owner->so_server;
4741 struct nfs_release_lockowner_args *args;
4742 struct rpc_message msg = {
4743 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4746 if (server->nfs_client->cl_mvops->minor_version != 0)
4748 args = kmalloc(sizeof(*args), GFP_NOFS);
4751 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4752 args->lock_owner.id = lsp->ls_seqid.owner_id;
4753 args->lock_owner.s_dev = server->s_dev;
4754 msg.rpc_argp = args;
4755 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4758 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4760 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4761 const void *buf, size_t buflen,
4762 int flags, int type)
4764 if (strcmp(key, "") != 0)
4767 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
4770 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4771 void *buf, size_t buflen, int type)
4773 if (strcmp(key, "") != 0)
4776 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
4779 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4780 size_t list_len, const char *name,
4781 size_t name_len, int type)
4783 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
4785 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4788 if (list && len <= list_len)
4789 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
4794 * nfs_fhget will use either the mounted_on_fileid or the fileid
4796 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4798 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4799 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4800 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4801 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
4804 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4805 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
4806 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4810 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4811 struct nfs4_fs_locations *fs_locations, struct page *page)
4813 struct nfs_server *server = NFS_SERVER(dir);
4815 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4817 struct nfs4_fs_locations_arg args = {
4818 .dir_fh = NFS_FH(dir),
4823 struct nfs4_fs_locations_res res = {
4824 .fs_locations = fs_locations,
4826 struct rpc_message msg = {
4827 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4833 dprintk("%s: start\n", __func__);
4835 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4836 * is not supported */
4837 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4838 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4840 bitmask[0] |= FATTR4_WORD0_FILEID;
4842 nfs_fattr_init(&fs_locations->fattr);
4843 fs_locations->server = server;
4844 fs_locations->nlocations = 0;
4845 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4846 dprintk("%s: returned status = %d\n", __func__, status);
4850 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4853 struct nfs4_secinfo_arg args = {
4854 .dir_fh = NFS_FH(dir),
4857 struct nfs4_secinfo_res res = {
4860 struct rpc_message msg = {
4861 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4866 dprintk("NFS call secinfo %s\n", name->name);
4867 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4868 dprintk("NFS reply secinfo: %d\n", status);
4872 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4874 struct nfs4_exception exception = { };
4877 err = nfs4_handle_exception(NFS_SERVER(dir),
4878 _nfs4_proc_secinfo(dir, name, flavors),
4880 } while (exception.retry);
4884 #ifdef CONFIG_NFS_V4_1
4886 * Check the exchange flags returned by the server for invalid flags, having
4887 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4890 static int nfs4_check_cl_exchange_flags(u32 flags)
4892 if (flags & ~EXCHGID4_FLAG_MASK_R)
4894 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4895 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4897 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4901 return -NFS4ERR_INVAL;
4905 nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4907 if (a->server_scope_sz == b->server_scope_sz &&
4908 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4915 * nfs4_proc_exchange_id()
4917 * Since the clientid has expired, all compounds using sessions
4918 * associated with the stale clientid will be returning
4919 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4920 * be in some phase of session reset.
4922 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4924 nfs4_verifier verifier;
4925 struct nfs41_exchange_id_args args = {
4927 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
4929 struct nfs41_exchange_id_res res = {
4933 struct rpc_message msg = {
4934 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4941 dprintk("--> %s\n", __func__);
4942 BUG_ON(clp == NULL);
4944 p = (u32 *)verifier.data;
4945 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4946 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4947 args.verifier = &verifier;
4949 args.id_len = scnprintf(args.id, sizeof(args.id),
4952 init_utsname()->nodename,
4953 init_utsname()->domainname,
4954 clp->cl_rpcclient->cl_auth->au_flavor);
4956 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
4957 if (unlikely(!res.server_scope)) {
4962 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
4963 if (unlikely(!res.impl_id)) {
4965 goto out_server_scope;
4968 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4970 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
4973 /* use the most recent implementation id */
4974 kfree(clp->impl_id);
4975 clp->impl_id = res.impl_id;
4980 if (clp->server_scope &&
4981 !nfs41_same_server_scope(clp->server_scope,
4982 res.server_scope)) {
4983 dprintk("%s: server_scope mismatch detected\n",
4985 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4986 kfree(clp->server_scope);
4987 clp->server_scope = NULL;
4990 if (!clp->server_scope) {
4991 clp->server_scope = res.server_scope;
4997 kfree(res.server_scope);
5000 dprintk("%s: Server Implementation ID: "
5001 "domain: %s, name: %s, date: %llu,%u\n",
5002 __func__, clp->impl_id->domain, clp->impl_id->name,
5003 clp->impl_id->date.seconds,
5004 clp->impl_id->date.nseconds);
5005 dprintk("<-- %s status= %d\n", __func__, status);
5009 struct nfs4_get_lease_time_data {
5010 struct nfs4_get_lease_time_args *args;
5011 struct nfs4_get_lease_time_res *res;
5012 struct nfs_client *clp;
5015 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5019 struct nfs4_get_lease_time_data *data =
5020 (struct nfs4_get_lease_time_data *)calldata;
5022 dprintk("--> %s\n", __func__);
5023 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5024 /* just setup sequence, do not trigger session recovery
5025 since we're invoked within one */
5026 ret = nfs41_setup_sequence(data->clp->cl_session,
5027 &data->args->la_seq_args,
5028 &data->res->lr_seq_res, task);
5030 BUG_ON(ret == -EAGAIN);
5031 rpc_call_start(task);
5032 dprintk("<-- %s\n", __func__);
5036 * Called from nfs4_state_manager thread for session setup, so don't recover
5037 * from sequence operation or clientid errors.
5039 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5041 struct nfs4_get_lease_time_data *data =
5042 (struct nfs4_get_lease_time_data *)calldata;
5044 dprintk("--> %s\n", __func__);
5045 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5047 switch (task->tk_status) {
5048 case -NFS4ERR_DELAY:
5049 case -NFS4ERR_GRACE:
5050 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5051 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5052 task->tk_status = 0;
5054 case -NFS4ERR_RETRY_UNCACHED_REP:
5055 rpc_restart_call_prepare(task);
5058 dprintk("<-- %s\n", __func__);
5061 struct rpc_call_ops nfs4_get_lease_time_ops = {
5062 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5063 .rpc_call_done = nfs4_get_lease_time_done,
5066 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5068 struct rpc_task *task;
5069 struct nfs4_get_lease_time_args args;
5070 struct nfs4_get_lease_time_res res = {
5071 .lr_fsinfo = fsinfo,
5073 struct nfs4_get_lease_time_data data = {
5078 struct rpc_message msg = {
5079 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5083 struct rpc_task_setup task_setup = {
5084 .rpc_client = clp->cl_rpcclient,
5085 .rpc_message = &msg,
5086 .callback_ops = &nfs4_get_lease_time_ops,
5087 .callback_data = &data,
5088 .flags = RPC_TASK_TIMEOUT,
5092 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5093 dprintk("--> %s\n", __func__);
5094 task = rpc_run_task(&task_setup);
5097 status = PTR_ERR(task);
5099 status = task->tk_status;
5102 dprintk("<-- %s return %d\n", __func__, status);
5108 * Reset a slot table
5110 static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5113 struct nfs4_slot *new = NULL;
5117 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5118 max_reqs, tbl->max_slots);
5120 /* Does the newly negotiated max_reqs match the existing slot table? */
5121 if (max_reqs != tbl->max_slots) {
5123 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
5130 spin_lock(&tbl->slot_tbl_lock);
5133 tbl->max_slots = max_reqs;
5135 for (i = 0; i < tbl->max_slots; ++i)
5136 tbl->slots[i].seq_nr = ivalue;
5137 spin_unlock(&tbl->slot_tbl_lock);
5138 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5139 tbl, tbl->slots, tbl->max_slots);
5141 dprintk("<-- %s: return %d\n", __func__, ret);
5145 /* Destroy the slot table */
5146 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5148 if (session->fc_slot_table.slots != NULL) {
5149 kfree(session->fc_slot_table.slots);
5150 session->fc_slot_table.slots = NULL;
5152 if (session->bc_slot_table.slots != NULL) {
5153 kfree(session->bc_slot_table.slots);
5154 session->bc_slot_table.slots = NULL;
5160 * Initialize slot table
5162 static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5163 int max_slots, int ivalue)
5165 struct nfs4_slot *slot;
5168 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5170 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
5172 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
5177 spin_lock(&tbl->slot_tbl_lock);
5178 tbl->max_slots = max_slots;
5180 tbl->highest_used_slotid = NFS4_NO_SLOT; /* no slot is currently used */
5181 spin_unlock(&tbl->slot_tbl_lock);
5182 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5183 tbl, tbl->slots, tbl->max_slots);
5185 dprintk("<-- %s: return %d\n", __func__, ret);
5190 * Initialize or reset the forechannel and backchannel tables
5192 static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
5194 struct nfs4_slot_table *tbl;
5197 dprintk("--> %s\n", __func__);
5199 tbl = &ses->fc_slot_table;
5200 if (tbl->slots == NULL) {
5201 status = nfs4_init_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5202 if (status) /* -ENOMEM */
5205 status = nfs4_reset_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5210 tbl = &ses->bc_slot_table;
5211 if (tbl->slots == NULL) {
5212 status = nfs4_init_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5214 /* Fore and back channel share a connection so get
5215 * both slot tables or neither */
5216 nfs4_destroy_slot_tables(ses);
5218 status = nfs4_reset_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5222 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5224 struct nfs4_session *session;
5225 struct nfs4_slot_table *tbl;
5227 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5231 tbl = &session->fc_slot_table;
5232 tbl->highest_used_slotid = NFS4_NO_SLOT;
5233 spin_lock_init(&tbl->slot_tbl_lock);
5234 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5235 init_completion(&tbl->complete);
5237 tbl = &session->bc_slot_table;
5238 tbl->highest_used_slotid = NFS4_NO_SLOT;
5239 spin_lock_init(&tbl->slot_tbl_lock);
5240 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5241 init_completion(&tbl->complete);
5243 session->session_state = 1<<NFS4_SESSION_INITING;
5249 void nfs4_destroy_session(struct nfs4_session *session)
5251 struct rpc_xprt *xprt;
5253 nfs4_proc_destroy_session(session);
5256 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5258 dprintk("%s Destroy backchannel for xprt %p\n",
5260 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
5261 nfs4_destroy_slot_tables(session);
5266 * Initialize the values to be used by the client in CREATE_SESSION
5267 * If nfs4_init_session set the fore channel request and response sizes,
5270 * Set the back channel max_resp_sz_cached to zero to force the client to
5271 * always set csa_cachethis to FALSE because the current implementation
5272 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5274 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5276 struct nfs4_session *session = args->client->cl_session;
5277 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5278 mxresp_sz = session->fc_attrs.max_resp_sz;
5281 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5283 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5284 /* Fore channel attributes */
5285 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5286 args->fc_attrs.max_resp_sz = mxresp_sz;
5287 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5288 args->fc_attrs.max_reqs = max_session_slots;
5290 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5291 "max_ops=%u max_reqs=%u\n",
5293 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5294 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5296 /* Back channel attributes */
5297 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5298 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5299 args->bc_attrs.max_resp_sz_cached = 0;
5300 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5301 args->bc_attrs.max_reqs = 1;
5303 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5304 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5306 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5307 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5308 args->bc_attrs.max_reqs);
5311 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5313 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5314 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5316 if (rcvd->max_resp_sz > sent->max_resp_sz)
5319 * Our requested max_ops is the minimum we need; we're not
5320 * prepared to break up compounds into smaller pieces than that.
5321 * So, no point even trying to continue if the server won't
5324 if (rcvd->max_ops < sent->max_ops)
5326 if (rcvd->max_reqs == 0)
5328 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5329 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5333 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5335 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5336 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5338 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5340 if (rcvd->max_resp_sz < sent->max_resp_sz)
5342 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5344 /* These would render the backchannel useless: */
5345 if (rcvd->max_ops != sent->max_ops)
5347 if (rcvd->max_reqs != sent->max_reqs)
5352 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5353 struct nfs4_session *session)
5357 ret = nfs4_verify_fore_channel_attrs(args, session);
5360 return nfs4_verify_back_channel_attrs(args, session);
5363 static int _nfs4_proc_create_session(struct nfs_client *clp)
5365 struct nfs4_session *session = clp->cl_session;
5366 struct nfs41_create_session_args args = {
5368 .cb_program = NFS4_CALLBACK,
5370 struct nfs41_create_session_res res = {
5373 struct rpc_message msg = {
5374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5380 nfs4_init_channel_attrs(&args);
5381 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5383 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5386 /* Verify the session's negotiated channel_attrs values */
5387 status = nfs4_verify_channel_attrs(&args, session);
5389 /* Increment the clientid slot sequence id */
5397 * Issues a CREATE_SESSION operation to the server.
5398 * It is the responsibility of the caller to verify the session is
5399 * expired before calling this routine.
5401 int nfs4_proc_create_session(struct nfs_client *clp)
5405 struct nfs4_session *session = clp->cl_session;
5407 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5409 status = _nfs4_proc_create_session(clp);
5413 /* Init or reset the session slot tables */
5414 status = nfs4_setup_session_slot_tables(session);
5415 dprintk("slot table setup returned %d\n", status);
5419 ptr = (unsigned *)&session->sess_id.data[0];
5420 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5421 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5423 dprintk("<-- %s\n", __func__);
5428 * Issue the over-the-wire RPC DESTROY_SESSION.
5429 * The caller must serialize access to this routine.
5431 int nfs4_proc_destroy_session(struct nfs4_session *session)
5434 struct rpc_message msg;
5436 dprintk("--> nfs4_proc_destroy_session\n");
5438 /* session is still being setup */
5439 if (session->clp->cl_cons_state != NFS_CS_READY)
5442 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5443 msg.rpc_argp = session;
5444 msg.rpc_resp = NULL;
5445 msg.rpc_cred = NULL;
5446 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5450 "NFS: Got error %d from the server on DESTROY_SESSION. "
5451 "Session has been destroyed regardless...\n", status);
5453 dprintk("<-- nfs4_proc_destroy_session\n");
5457 int nfs4_init_session(struct nfs_server *server)
5459 struct nfs_client *clp = server->nfs_client;
5460 struct nfs4_session *session;
5461 unsigned int rsize, wsize;
5464 if (!nfs4_has_session(clp))
5467 session = clp->cl_session;
5468 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5471 rsize = server->rsize;
5473 rsize = NFS_MAX_FILE_IO_SIZE;
5474 wsize = server->wsize;
5476 wsize = NFS_MAX_FILE_IO_SIZE;
5478 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5479 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5481 ret = nfs4_recover_expired_lease(server);
5483 ret = nfs4_check_client_ready(clp);
5487 int nfs4_init_ds_session(struct nfs_client *clp)
5489 struct nfs4_session *session = clp->cl_session;
5492 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5495 ret = nfs4_client_recover_expired_lease(clp);
5497 /* Test for the DS role */
5498 if (!is_ds_client(clp))
5501 ret = nfs4_check_client_ready(clp);
5505 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5509 * Renew the cl_session lease.
5511 struct nfs4_sequence_data {
5512 struct nfs_client *clp;
5513 struct nfs4_sequence_args args;
5514 struct nfs4_sequence_res res;
5517 static void nfs41_sequence_release(void *data)
5519 struct nfs4_sequence_data *calldata = data;
5520 struct nfs_client *clp = calldata->clp;
5522 if (atomic_read(&clp->cl_count) > 1)
5523 nfs4_schedule_state_renewal(clp);
5524 nfs_put_client(clp);
5528 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5530 switch(task->tk_status) {
5531 case -NFS4ERR_DELAY:
5532 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5535 nfs4_schedule_lease_recovery(clp);
5540 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5542 struct nfs4_sequence_data *calldata = data;
5543 struct nfs_client *clp = calldata->clp;
5545 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5548 if (task->tk_status < 0) {
5549 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5550 if (atomic_read(&clp->cl_count) == 1)
5553 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5554 rpc_restart_call_prepare(task);
5558 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5560 dprintk("<-- %s\n", __func__);
5563 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5565 struct nfs4_sequence_data *calldata = data;
5566 struct nfs_client *clp = calldata->clp;
5567 struct nfs4_sequence_args *args;
5568 struct nfs4_sequence_res *res;
5570 args = task->tk_msg.rpc_argp;
5571 res = task->tk_msg.rpc_resp;
5573 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
5575 rpc_call_start(task);
5578 static const struct rpc_call_ops nfs41_sequence_ops = {
5579 .rpc_call_done = nfs41_sequence_call_done,
5580 .rpc_call_prepare = nfs41_sequence_prepare,
5581 .rpc_release = nfs41_sequence_release,
5584 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5586 struct nfs4_sequence_data *calldata;
5587 struct rpc_message msg = {
5588 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5591 struct rpc_task_setup task_setup_data = {
5592 .rpc_client = clp->cl_rpcclient,
5593 .rpc_message = &msg,
5594 .callback_ops = &nfs41_sequence_ops,
5595 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5598 if (!atomic_inc_not_zero(&clp->cl_count))
5599 return ERR_PTR(-EIO);
5600 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5601 if (calldata == NULL) {
5602 nfs_put_client(clp);
5603 return ERR_PTR(-ENOMEM);
5605 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
5606 msg.rpc_argp = &calldata->args;
5607 msg.rpc_resp = &calldata->res;
5608 calldata->clp = clp;
5609 task_setup_data.callback_data = calldata;
5611 return rpc_run_task(&task_setup_data);
5614 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5616 struct rpc_task *task;
5619 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5621 task = _nfs41_proc_sequence(clp, cred);
5623 ret = PTR_ERR(task);
5625 rpc_put_task_async(task);
5626 dprintk("<-- %s status=%d\n", __func__, ret);
5630 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5632 struct rpc_task *task;
5635 task = _nfs41_proc_sequence(clp, cred);
5637 ret = PTR_ERR(task);
5640 ret = rpc_wait_for_completion_task(task);
5642 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5644 if (task->tk_status == 0)
5645 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5646 ret = task->tk_status;
5650 dprintk("<-- %s status=%d\n", __func__, ret);
5654 struct nfs4_reclaim_complete_data {
5655 struct nfs_client *clp;
5656 struct nfs41_reclaim_complete_args arg;
5657 struct nfs41_reclaim_complete_res res;
5660 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5662 struct nfs4_reclaim_complete_data *calldata = data;
5664 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5665 if (nfs41_setup_sequence(calldata->clp->cl_session,
5666 &calldata->arg.seq_args,
5667 &calldata->res.seq_res, task))
5670 rpc_call_start(task);
5673 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5675 switch(task->tk_status) {
5677 case -NFS4ERR_COMPLETE_ALREADY:
5678 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5680 case -NFS4ERR_DELAY:
5681 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5683 case -NFS4ERR_RETRY_UNCACHED_REP:
5686 nfs4_schedule_lease_recovery(clp);
5691 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5693 struct nfs4_reclaim_complete_data *calldata = data;
5694 struct nfs_client *clp = calldata->clp;
5695 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5697 dprintk("--> %s\n", __func__);
5698 if (!nfs41_sequence_done(task, res))
5701 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5702 rpc_restart_call_prepare(task);
5705 dprintk("<-- %s\n", __func__);
5708 static void nfs4_free_reclaim_complete_data(void *data)
5710 struct nfs4_reclaim_complete_data *calldata = data;
5715 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5716 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5717 .rpc_call_done = nfs4_reclaim_complete_done,
5718 .rpc_release = nfs4_free_reclaim_complete_data,
5722 * Issue a global reclaim complete.
5724 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5726 struct nfs4_reclaim_complete_data *calldata;
5727 struct rpc_task *task;
5728 struct rpc_message msg = {
5729 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5731 struct rpc_task_setup task_setup_data = {
5732 .rpc_client = clp->cl_rpcclient,
5733 .rpc_message = &msg,
5734 .callback_ops = &nfs4_reclaim_complete_call_ops,
5735 .flags = RPC_TASK_ASYNC,
5737 int status = -ENOMEM;
5739 dprintk("--> %s\n", __func__);
5740 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5741 if (calldata == NULL)
5743 calldata->clp = clp;
5744 calldata->arg.one_fs = 0;
5746 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
5747 msg.rpc_argp = &calldata->arg;
5748 msg.rpc_resp = &calldata->res;
5749 task_setup_data.callback_data = calldata;
5750 task = rpc_run_task(&task_setup_data);
5752 status = PTR_ERR(task);
5755 status = nfs4_wait_for_completion_rpc_task(task);
5757 status = task->tk_status;
5761 dprintk("<-- %s status=%d\n", __func__, status);
5766 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5768 struct nfs4_layoutget *lgp = calldata;
5769 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5771 dprintk("--> %s\n", __func__);
5772 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5773 * right now covering the LAYOUTGET we are about to send.
5774 * However, that is not so catastrophic, and there seems
5775 * to be no way to prevent it completely.
5777 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5778 &lgp->res.seq_res, task))
5780 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5781 NFS_I(lgp->args.inode)->layout,
5782 lgp->args.ctx->state)) {
5783 rpc_exit(task, NFS4_OK);
5786 rpc_call_start(task);
5789 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5791 struct nfs4_layoutget *lgp = calldata;
5792 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5794 dprintk("--> %s\n", __func__);
5796 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5799 switch (task->tk_status) {
5802 case -NFS4ERR_LAYOUTTRYLATER:
5803 case -NFS4ERR_RECALLCONFLICT:
5804 task->tk_status = -NFS4ERR_DELAY;
5807 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5808 rpc_restart_call_prepare(task);
5812 dprintk("<-- %s\n", __func__);
5815 static void nfs4_layoutget_release(void *calldata)
5817 struct nfs4_layoutget *lgp = calldata;
5819 dprintk("--> %s\n", __func__);
5820 put_nfs_open_context(lgp->args.ctx);
5822 dprintk("<-- %s\n", __func__);
5825 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5826 .rpc_call_prepare = nfs4_layoutget_prepare,
5827 .rpc_call_done = nfs4_layoutget_done,
5828 .rpc_release = nfs4_layoutget_release,
5831 int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5833 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5834 struct rpc_task *task;
5835 struct rpc_message msg = {
5836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5837 .rpc_argp = &lgp->args,
5838 .rpc_resp = &lgp->res,
5840 struct rpc_task_setup task_setup_data = {
5841 .rpc_client = server->client,
5842 .rpc_message = &msg,
5843 .callback_ops = &nfs4_layoutget_call_ops,
5844 .callback_data = lgp,
5845 .flags = RPC_TASK_ASYNC,
5849 dprintk("--> %s\n", __func__);
5851 lgp->res.layoutp = &lgp->args.layout;
5852 lgp->res.seq_res.sr_slot = NULL;
5853 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
5854 task = rpc_run_task(&task_setup_data);
5856 return PTR_ERR(task);
5857 status = nfs4_wait_for_completion_rpc_task(task);
5859 status = task->tk_status;
5861 status = pnfs_layout_process(lgp);
5863 dprintk("<-- %s status=%d\n", __func__, status);
5868 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5870 struct nfs4_layoutreturn *lrp = calldata;
5872 dprintk("--> %s\n", __func__);
5873 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5874 &lrp->res.seq_res, task))
5876 rpc_call_start(task);
5879 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5881 struct nfs4_layoutreturn *lrp = calldata;
5882 struct nfs_server *server;
5883 struct pnfs_layout_hdr *lo = lrp->args.layout;
5885 dprintk("--> %s\n", __func__);
5887 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5890 server = NFS_SERVER(lrp->args.inode);
5891 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5892 rpc_restart_call_prepare(task);
5895 spin_lock(&lo->plh_inode->i_lock);
5896 if (task->tk_status == 0) {
5897 if (lrp->res.lrs_present) {
5898 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
5900 BUG_ON(!list_empty(&lo->plh_segs));
5902 lo->plh_block_lgets--;
5903 spin_unlock(&lo->plh_inode->i_lock);
5904 dprintk("<-- %s\n", __func__);
5907 static void nfs4_layoutreturn_release(void *calldata)
5909 struct nfs4_layoutreturn *lrp = calldata;
5911 dprintk("--> %s\n", __func__);
5912 put_layout_hdr(lrp->args.layout);
5914 dprintk("<-- %s\n", __func__);
5917 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5918 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5919 .rpc_call_done = nfs4_layoutreturn_done,
5920 .rpc_release = nfs4_layoutreturn_release,
5923 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5925 struct rpc_task *task;
5926 struct rpc_message msg = {
5927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5928 .rpc_argp = &lrp->args,
5929 .rpc_resp = &lrp->res,
5931 struct rpc_task_setup task_setup_data = {
5932 .rpc_client = lrp->clp->cl_rpcclient,
5933 .rpc_message = &msg,
5934 .callback_ops = &nfs4_layoutreturn_call_ops,
5935 .callback_data = lrp,
5939 dprintk("--> %s\n", __func__);
5940 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
5941 task = rpc_run_task(&task_setup_data);
5943 return PTR_ERR(task);
5944 status = task->tk_status;
5945 dprintk("<-- %s status=%d\n", __func__, status);
5951 * Retrieve the list of Data Server devices from the MDS.
5953 static int _nfs4_getdevicelist(struct nfs_server *server,
5954 const struct nfs_fh *fh,
5955 struct pnfs_devicelist *devlist)
5957 struct nfs4_getdevicelist_args args = {
5959 .layoutclass = server->pnfs_curr_ld->id,
5961 struct nfs4_getdevicelist_res res = {
5964 struct rpc_message msg = {
5965 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5971 dprintk("--> %s\n", __func__);
5972 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5974 dprintk("<-- %s status=%d\n", __func__, status);
5978 int nfs4_proc_getdevicelist(struct nfs_server *server,
5979 const struct nfs_fh *fh,
5980 struct pnfs_devicelist *devlist)
5982 struct nfs4_exception exception = { };
5986 err = nfs4_handle_exception(server,
5987 _nfs4_getdevicelist(server, fh, devlist),
5989 } while (exception.retry);
5991 dprintk("%s: err=%d, num_devs=%u\n", __func__,
5992 err, devlist->num_devs);
5996 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5999 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6001 struct nfs4_getdeviceinfo_args args = {
6004 struct nfs4_getdeviceinfo_res res = {
6007 struct rpc_message msg = {
6008 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6014 dprintk("--> %s\n", __func__);
6015 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6016 dprintk("<-- %s status=%d\n", __func__, status);
6021 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6023 struct nfs4_exception exception = { };
6027 err = nfs4_handle_exception(server,
6028 _nfs4_proc_getdeviceinfo(server, pdev),
6030 } while (exception.retry);
6033 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6035 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6037 struct nfs4_layoutcommit_data *data = calldata;
6038 struct nfs_server *server = NFS_SERVER(data->args.inode);
6040 if (nfs4_setup_sequence(server, &data->args.seq_args,
6041 &data->res.seq_res, task))
6043 rpc_call_start(task);
6047 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6049 struct nfs4_layoutcommit_data *data = calldata;
6050 struct nfs_server *server = NFS_SERVER(data->args.inode);
6052 if (!nfs4_sequence_done(task, &data->res.seq_res))
6055 switch (task->tk_status) { /* Just ignore these failures */
6056 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6057 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6058 case NFS4ERR_BADLAYOUT: /* no layout */
6059 case NFS4ERR_GRACE: /* loca_recalim always false */
6060 task->tk_status = 0;
6063 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6064 rpc_restart_call_prepare(task);
6068 if (task->tk_status == 0)
6069 nfs_post_op_update_inode_force_wcc(data->args.inode,
6073 static void nfs4_layoutcommit_release(void *calldata)
6075 struct nfs4_layoutcommit_data *data = calldata;
6076 struct pnfs_layout_segment *lseg, *tmp;
6077 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
6079 pnfs_cleanup_layoutcommit(data);
6080 /* Matched by references in pnfs_set_layoutcommit */
6081 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6082 list_del_init(&lseg->pls_lc_list);
6083 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6088 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6089 smp_mb__after_clear_bit();
6090 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6092 put_rpccred(data->cred);
6096 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6097 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6098 .rpc_call_done = nfs4_layoutcommit_done,
6099 .rpc_release = nfs4_layoutcommit_release,
6103 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6105 struct rpc_message msg = {
6106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6107 .rpc_argp = &data->args,
6108 .rpc_resp = &data->res,
6109 .rpc_cred = data->cred,
6111 struct rpc_task_setup task_setup_data = {
6112 .task = &data->task,
6113 .rpc_client = NFS_CLIENT(data->args.inode),
6114 .rpc_message = &msg,
6115 .callback_ops = &nfs4_layoutcommit_ops,
6116 .callback_data = data,
6117 .flags = RPC_TASK_ASYNC,
6119 struct rpc_task *task;
6122 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6123 "lbw: %llu inode %lu\n",
6124 data->task.tk_pid, sync,
6125 data->args.lastbytewritten,
6126 data->args.inode->i_ino);
6128 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6129 task = rpc_run_task(&task_setup_data);
6131 return PTR_ERR(task);
6134 status = nfs4_wait_for_completion_rpc_task(task);
6137 status = task->tk_status;
6139 dprintk("%s: status %d\n", __func__, status);
6145 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6146 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6148 struct nfs41_secinfo_no_name_args args = {
6149 .style = SECINFO_STYLE_CURRENT_FH,
6151 struct nfs4_secinfo_res res = {
6154 struct rpc_message msg = {
6155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6159 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6163 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6164 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6166 struct nfs4_exception exception = { };
6169 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6172 case -NFS4ERR_WRONGSEC:
6173 case -NFS4ERR_NOTSUPP:
6176 err = nfs4_handle_exception(server, err, &exception);
6178 } while (exception.retry);
6183 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6184 struct nfs_fsinfo *info)
6188 rpc_authflavor_t flavor;
6189 struct nfs4_secinfo_flavors *flavors;
6191 page = alloc_page(GFP_KERNEL);
6197 flavors = page_address(page);
6198 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6201 * Fall back on "guess and check" method if
6202 * the server doesn't support SECINFO_NO_NAME
6204 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6205 err = nfs4_find_root_sec(server, fhandle, info);
6211 flavor = nfs_find_best_sec(flavors);
6213 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6223 static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6226 struct nfs41_test_stateid_args args = {
6229 struct nfs41_test_stateid_res res;
6230 struct rpc_message msg = {
6231 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6236 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6237 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6239 if (status == NFS_OK)
6244 static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6246 struct nfs4_exception exception = { };
6249 err = nfs4_handle_exception(server,
6250 _nfs41_test_stateid(server, stateid),
6252 } while (exception.retry);
6256 static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6258 struct nfs41_free_stateid_args args = {
6261 struct nfs41_free_stateid_res res;
6262 struct rpc_message msg = {
6263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6268 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6269 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6272 static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6274 struct nfs4_exception exception = { };
6277 err = nfs4_handle_exception(server,
6278 _nfs4_free_stateid(server, stateid),
6280 } while (exception.retry);
6283 #endif /* CONFIG_NFS_V4_1 */
6285 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6286 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6287 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6288 .recover_open = nfs4_open_reclaim,
6289 .recover_lock = nfs4_lock_reclaim,
6290 .establish_clid = nfs4_init_clientid,
6291 .get_clid_cred = nfs4_get_setclientid_cred,
6294 #if defined(CONFIG_NFS_V4_1)
6295 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6296 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6297 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6298 .recover_open = nfs4_open_reclaim,
6299 .recover_lock = nfs4_lock_reclaim,
6300 .establish_clid = nfs41_init_clientid,
6301 .get_clid_cred = nfs4_get_exchange_id_cred,
6302 .reclaim_complete = nfs41_proc_reclaim_complete,
6304 #endif /* CONFIG_NFS_V4_1 */
6306 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6307 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6308 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6309 .recover_open = nfs4_open_expired,
6310 .recover_lock = nfs4_lock_expired,
6311 .establish_clid = nfs4_init_clientid,
6312 .get_clid_cred = nfs4_get_setclientid_cred,
6315 #if defined(CONFIG_NFS_V4_1)
6316 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6317 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6318 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6319 .recover_open = nfs41_open_expired,
6320 .recover_lock = nfs41_lock_expired,
6321 .establish_clid = nfs41_init_clientid,
6322 .get_clid_cred = nfs4_get_exchange_id_cred,
6324 #endif /* CONFIG_NFS_V4_1 */
6326 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6327 .sched_state_renewal = nfs4_proc_async_renew,
6328 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6329 .renew_lease = nfs4_proc_renew,
6332 #if defined(CONFIG_NFS_V4_1)
6333 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6334 .sched_state_renewal = nfs41_proc_async_sequence,
6335 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
6336 .renew_lease = nfs4_proc_sequence,
6340 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6342 .call_sync = _nfs4_call_sync,
6343 .validate_stateid = nfs4_validate_delegation_stateid,
6344 .find_root_sec = nfs4_find_root_sec,
6345 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6346 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6347 .state_renewal_ops = &nfs40_state_renewal_ops,
6350 #if defined(CONFIG_NFS_V4_1)
6351 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6353 .call_sync = _nfs4_call_sync_session,
6354 .validate_stateid = nfs41_validate_delegation_stateid,
6355 .find_root_sec = nfs41_find_root_sec,
6356 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6357 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6358 .state_renewal_ops = &nfs41_state_renewal_ops,
6362 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6363 [0] = &nfs_v4_0_minor_ops,
6364 #if defined(CONFIG_NFS_V4_1)
6365 [1] = &nfs_v4_1_minor_ops,
6369 static const struct inode_operations nfs4_file_inode_operations = {
6370 .permission = nfs_permission,
6371 .getattr = nfs_getattr,
6372 .setattr = nfs_setattr,
6373 .getxattr = generic_getxattr,
6374 .setxattr = generic_setxattr,
6375 .listxattr = generic_listxattr,
6376 .removexattr = generic_removexattr,
6379 const struct nfs_rpc_ops nfs_v4_clientops = {
6380 .version = 4, /* protocol version */
6381 .dentry_ops = &nfs4_dentry_operations,
6382 .dir_inode_ops = &nfs4_dir_inode_operations,
6383 .file_inode_ops = &nfs4_file_inode_operations,
6384 .file_ops = &nfs4_file_operations,
6385 .getroot = nfs4_proc_get_root,
6386 .getattr = nfs4_proc_getattr,
6387 .setattr = nfs4_proc_setattr,
6388 .lookup = nfs4_proc_lookup,
6389 .access = nfs4_proc_access,
6390 .readlink = nfs4_proc_readlink,
6391 .create = nfs4_proc_create,
6392 .remove = nfs4_proc_remove,
6393 .unlink_setup = nfs4_proc_unlink_setup,
6394 .unlink_done = nfs4_proc_unlink_done,
6395 .rename = nfs4_proc_rename,
6396 .rename_setup = nfs4_proc_rename_setup,
6397 .rename_done = nfs4_proc_rename_done,
6398 .link = nfs4_proc_link,
6399 .symlink = nfs4_proc_symlink,
6400 .mkdir = nfs4_proc_mkdir,
6401 .rmdir = nfs4_proc_remove,
6402 .readdir = nfs4_proc_readdir,
6403 .mknod = nfs4_proc_mknod,
6404 .statfs = nfs4_proc_statfs,
6405 .fsinfo = nfs4_proc_fsinfo,
6406 .pathconf = nfs4_proc_pathconf,
6407 .set_capabilities = nfs4_server_capabilities,
6408 .decode_dirent = nfs4_decode_dirent,
6409 .read_setup = nfs4_proc_read_setup,
6410 .read_done = nfs4_read_done,
6411 .write_setup = nfs4_proc_write_setup,
6412 .write_done = nfs4_write_done,
6413 .commit_setup = nfs4_proc_commit_setup,
6414 .commit_done = nfs4_commit_done,
6415 .lock = nfs4_proc_lock,
6416 .clear_acl_cache = nfs4_zap_acl_attr,
6417 .close_context = nfs4_close_context,
6418 .open_context = nfs4_atomic_open,
6419 .init_client = nfs4_init_client,
6420 .secinfo = nfs4_proc_secinfo,
6423 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6424 .prefix = XATTR_NAME_NFSV4_ACL,
6425 .list = nfs4_xattr_list_nfs4_acl,
6426 .get = nfs4_xattr_get_nfs4_acl,
6427 .set = nfs4_xattr_set_nfs4_acl,
6430 const struct xattr_handler *nfs4_xattr_handlers[] = {
6431 &nfs4_xattr_nfs4_acl_handler,
6435 module_param(max_session_slots, ushort, 0644);
6436 MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6437 "requests the client will negotiate");