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);
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);
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;
265 struct inode *inode = exception->inode;
268 exception->retry = 0;
272 case -NFS4ERR_OPENMODE:
273 if (inode && nfs_have_delegation(inode, FMODE_READ)) {
274 nfs_inode_return_delegation(inode);
275 exception->retry = 1;
280 nfs4_schedule_stateid_recovery(server, state);
281 goto wait_on_recovery;
282 case -NFS4ERR_DELEG_REVOKED:
283 case -NFS4ERR_ADMIN_REVOKED:
284 case -NFS4ERR_BAD_STATEID:
287 nfs_remove_bad_delegation(state->inode);
288 nfs4_schedule_stateid_recovery(server, state);
289 goto wait_on_recovery;
290 case -NFS4ERR_EXPIRED:
292 nfs4_schedule_stateid_recovery(server, state);
293 case -NFS4ERR_STALE_STATEID:
294 case -NFS4ERR_STALE_CLIENTID:
295 nfs4_schedule_lease_recovery(clp);
296 goto wait_on_recovery;
297 #if defined(CONFIG_NFS_V4_1)
298 case -NFS4ERR_BADSESSION:
299 case -NFS4ERR_BADSLOT:
300 case -NFS4ERR_BAD_HIGH_SLOT:
301 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
302 case -NFS4ERR_DEADSESSION:
303 case -NFS4ERR_SEQ_FALSE_RETRY:
304 case -NFS4ERR_SEQ_MISORDERED:
305 dprintk("%s ERROR: %d Reset session\n", __func__,
307 nfs4_schedule_session_recovery(clp->cl_session);
308 exception->retry = 1;
310 #endif /* defined(CONFIG_NFS_V4_1) */
311 case -NFS4ERR_FILE_OPEN:
312 if (exception->timeout > HZ) {
313 /* We have retried a decent amount, time to
322 ret = nfs4_delay(server->client, &exception->timeout);
325 case -NFS4ERR_RETRY_UNCACHED_REP:
326 case -NFS4ERR_OLD_STATEID:
327 exception->retry = 1;
329 case -NFS4ERR_BADOWNER:
330 /* The following works around a Linux server bug! */
331 case -NFS4ERR_BADNAME:
332 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
333 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
334 exception->retry = 1;
335 printk(KERN_WARNING "NFS: v4 server %s "
336 "does not accept raw "
338 "Reenabling the idmapper.\n",
339 server->nfs_client->cl_hostname);
342 /* We failed to handle the error */
343 return nfs4_map_errors(ret);
345 ret = nfs4_wait_clnt_recover(clp);
347 exception->retry = 1;
352 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
354 spin_lock(&clp->cl_lock);
355 if (time_before(clp->cl_last_renewal,timestamp))
356 clp->cl_last_renewal = timestamp;
357 spin_unlock(&clp->cl_lock);
360 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
362 do_renew_lease(server->nfs_client, timestamp);
365 #if defined(CONFIG_NFS_V4_1)
368 * nfs4_free_slot - free a slot and efficiently update slot table.
370 * freeing a slot is trivially done by clearing its respective bit
372 * If the freed slotid equals highest_used_slotid we want to update it
373 * so that the server would be able to size down the slot table if needed,
374 * otherwise we know that the highest_used_slotid is still in use.
375 * When updating highest_used_slotid there may be "holes" in the bitmap
376 * so we need to scan down from highest_used_slotid to 0 looking for the now
377 * highest slotid in use.
378 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
380 * Must be called while holding tbl->slot_tbl_lock
383 nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
385 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
386 /* clear used bit in bitmap */
387 __clear_bit(slotid, tbl->used_slots);
389 /* update highest_used_slotid when it is freed */
390 if (slotid == tbl->highest_used_slotid) {
391 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
392 if (slotid < tbl->max_slots)
393 tbl->highest_used_slotid = slotid;
395 tbl->highest_used_slotid = NFS4_NO_SLOT;
397 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
398 slotid, tbl->highest_used_slotid);
401 bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
403 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
408 * Signal state manager thread if session fore channel is drained
410 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
412 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
413 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
414 nfs4_set_task_privileged, NULL);
418 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
421 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
422 complete(&ses->fc_slot_table.complete);
426 * Signal state manager thread if session back channel is drained
428 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
430 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
431 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
433 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
434 complete(&ses->bc_slot_table.complete);
437 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
439 struct nfs4_slot_table *tbl;
441 tbl = &res->sr_session->fc_slot_table;
443 /* just wake up the next guy waiting since
444 * we may have not consumed a slot after all */
445 dprintk("%s: No slot\n", __func__);
449 spin_lock(&tbl->slot_tbl_lock);
450 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
451 nfs4_check_drain_fc_complete(res->sr_session);
452 spin_unlock(&tbl->slot_tbl_lock);
456 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
458 unsigned long timestamp;
459 struct nfs_client *clp;
462 * sr_status remains 1 if an RPC level error occurred. The server
463 * may or may not have processed the sequence operation..
464 * Proceed as if the server received and processed the sequence
467 if (res->sr_status == 1)
468 res->sr_status = NFS_OK;
470 /* don't increment the sequence number if the task wasn't sent */
471 if (!RPC_WAS_SENT(task))
474 /* Check the SEQUENCE operation status */
475 switch (res->sr_status) {
477 /* Update the slot's sequence and clientid lease timer */
478 ++res->sr_slot->seq_nr;
479 timestamp = res->sr_renewal_time;
480 clp = res->sr_session->clp;
481 do_renew_lease(clp, timestamp);
482 /* Check sequence flags */
483 if (res->sr_status_flags != 0)
484 nfs4_schedule_lease_recovery(clp);
487 /* The server detected a resend of the RPC call and
488 * returned NFS4ERR_DELAY as per Section 2.10.6.2
491 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
493 res->sr_slot - res->sr_session->fc_slot_table.slots,
494 res->sr_slot->seq_nr);
497 /* Just update the slot sequence no. */
498 ++res->sr_slot->seq_nr;
501 /* The session may be reset by one of the error handlers. */
502 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
503 nfs41_sequence_free_slot(res);
506 if (!rpc_restart_call(task))
508 rpc_delay(task, NFS4_POLL_RETRY_MAX);
512 static int nfs4_sequence_done(struct rpc_task *task,
513 struct nfs4_sequence_res *res)
515 if (res->sr_session == NULL)
517 return nfs41_sequence_done(task, res);
521 * nfs4_find_slot - efficiently look for a free slot
523 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
524 * If found, we mark the slot as used, update the highest_used_slotid,
525 * and respectively set up the sequence operation args.
526 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
528 * Note: must be called with under the slot_tbl_lock.
531 nfs4_find_slot(struct nfs4_slot_table *tbl)
534 u32 ret_id = NFS4_NO_SLOT;
536 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
537 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
539 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
540 if (slotid >= tbl->max_slots)
542 __set_bit(slotid, tbl->used_slots);
543 if (slotid > tbl->highest_used_slotid ||
544 tbl->highest_used_slotid == NFS4_NO_SLOT)
545 tbl->highest_used_slotid = slotid;
548 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
549 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
553 static void nfs41_init_sequence(struct nfs4_sequence_args *args,
554 struct nfs4_sequence_res *res, int cache_reply)
556 args->sa_session = NULL;
557 args->sa_cache_this = 0;
559 args->sa_cache_this = 1;
560 res->sr_session = NULL;
564 int nfs41_setup_sequence(struct nfs4_session *session,
565 struct nfs4_sequence_args *args,
566 struct nfs4_sequence_res *res,
567 struct rpc_task *task)
569 struct nfs4_slot *slot;
570 struct nfs4_slot_table *tbl;
573 dprintk("--> %s\n", __func__);
574 /* slot already allocated? */
575 if (res->sr_slot != NULL)
578 tbl = &session->fc_slot_table;
580 spin_lock(&tbl->slot_tbl_lock);
581 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
582 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
583 /* The state manager will wait until the slot table is empty */
584 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
585 spin_unlock(&tbl->slot_tbl_lock);
586 dprintk("%s session is draining\n", __func__);
590 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
591 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
592 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
593 spin_unlock(&tbl->slot_tbl_lock);
594 dprintk("%s enforce FIFO order\n", __func__);
598 slotid = nfs4_find_slot(tbl);
599 if (slotid == NFS4_NO_SLOT) {
600 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
601 spin_unlock(&tbl->slot_tbl_lock);
602 dprintk("<-- %s: no free slots\n", __func__);
605 spin_unlock(&tbl->slot_tbl_lock);
607 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
608 slot = tbl->slots + slotid;
609 args->sa_session = session;
610 args->sa_slotid = slotid;
612 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
614 res->sr_session = session;
616 res->sr_renewal_time = jiffies;
617 res->sr_status_flags = 0;
619 * sr_status is only set in decode_sequence, and so will remain
620 * set to 1 if an rpc level failure occurs.
625 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
627 int nfs4_setup_sequence(const struct nfs_server *server,
628 struct nfs4_sequence_args *args,
629 struct nfs4_sequence_res *res,
630 struct rpc_task *task)
632 struct nfs4_session *session = nfs4_get_session(server);
638 dprintk("--> %s clp %p session %p sr_slot %td\n",
639 __func__, session->clp, session, res->sr_slot ?
640 res->sr_slot - session->fc_slot_table.slots : -1);
642 ret = nfs41_setup_sequence(session, args, res, task);
644 dprintk("<-- %s status=%d\n", __func__, ret);
648 struct nfs41_call_sync_data {
649 const struct nfs_server *seq_server;
650 struct nfs4_sequence_args *seq_args;
651 struct nfs4_sequence_res *seq_res;
654 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
656 struct nfs41_call_sync_data *data = calldata;
658 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
660 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
661 data->seq_res, task))
663 rpc_call_start(task);
666 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
668 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
669 nfs41_call_sync_prepare(task, calldata);
672 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
674 struct nfs41_call_sync_data *data = calldata;
676 nfs41_sequence_done(task, data->seq_res);
679 static const struct rpc_call_ops nfs41_call_sync_ops = {
680 .rpc_call_prepare = nfs41_call_sync_prepare,
681 .rpc_call_done = nfs41_call_sync_done,
684 static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
685 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
686 .rpc_call_done = nfs41_call_sync_done,
689 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
690 struct nfs_server *server,
691 struct rpc_message *msg,
692 struct nfs4_sequence_args *args,
693 struct nfs4_sequence_res *res,
697 struct rpc_task *task;
698 struct nfs41_call_sync_data data = {
699 .seq_server = server,
703 struct rpc_task_setup task_setup = {
706 .callback_ops = &nfs41_call_sync_ops,
707 .callback_data = &data
711 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
712 task = rpc_run_task(&task_setup);
716 ret = task->tk_status;
722 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
723 struct nfs_server *server,
724 struct rpc_message *msg,
725 struct nfs4_sequence_args *args,
726 struct nfs4_sequence_res *res,
729 nfs41_init_sequence(args, res, cache_reply);
730 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
735 void nfs41_init_sequence(struct nfs4_sequence_args *args,
736 struct nfs4_sequence_res *res, int cache_reply)
740 static int nfs4_sequence_done(struct rpc_task *task,
741 struct nfs4_sequence_res *res)
745 #endif /* CONFIG_NFS_V4_1 */
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 nfs41_init_sequence(args, res, cache_reply);
755 return rpc_call_sync(clnt, msg, 0);
759 int nfs4_call_sync(struct rpc_clnt *clnt,
760 struct nfs_server *server,
761 struct rpc_message *msg,
762 struct nfs4_sequence_args *args,
763 struct nfs4_sequence_res *res,
766 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
767 args, res, cache_reply);
770 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
772 struct nfs_inode *nfsi = NFS_I(dir);
774 spin_lock(&dir->i_lock);
775 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
776 if (!cinfo->atomic || cinfo->before != dir->i_version)
777 nfs_force_lookup_revalidate(dir);
778 dir->i_version = cinfo->after;
779 spin_unlock(&dir->i_lock);
782 struct nfs4_opendata {
784 struct nfs_openargs o_arg;
785 struct nfs_openres o_res;
786 struct nfs_open_confirmargs c_arg;
787 struct nfs_open_confirmres c_res;
788 struct nfs4_string owner_name;
789 struct nfs4_string group_name;
790 struct nfs_fattr f_attr;
792 struct dentry *dentry;
793 struct nfs4_state_owner *owner;
794 struct nfs4_state *state;
796 unsigned long timestamp;
797 unsigned int rpc_done : 1;
803 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
805 p->o_res.f_attr = &p->f_attr;
806 p->o_res.seqid = p->o_arg.seqid;
807 p->c_res.seqid = p->c_arg.seqid;
808 p->o_res.server = p->o_arg.server;
809 nfs_fattr_init(&p->f_attr);
810 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
813 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
814 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
815 const struct iattr *attrs,
818 struct dentry *parent = dget_parent(dentry);
819 struct inode *dir = parent->d_inode;
820 struct nfs_server *server = NFS_SERVER(dir);
821 struct nfs4_opendata *p;
823 p = kzalloc(sizeof(*p), gfp_mask);
826 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
827 if (p->o_arg.seqid == NULL)
829 nfs_sb_active(dentry->d_sb);
830 p->dentry = dget(dentry);
833 atomic_inc(&sp->so_count);
834 p->o_arg.fh = NFS_FH(dir);
835 p->o_arg.open_flags = flags;
836 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
837 p->o_arg.clientid = server->nfs_client->cl_clientid;
838 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
839 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
840 p->o_arg.name = &dentry->d_name;
841 p->o_arg.server = server;
842 p->o_arg.bitmask = server->attr_bitmask;
843 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
844 if (attrs != NULL && attrs->ia_valid != 0) {
847 p->o_arg.u.attrs = &p->attrs;
848 memcpy(&p->attrs, attrs, sizeof(p->attrs));
851 verf[1] = current->pid;
852 memcpy(p->o_arg.u.verifier.data, verf,
853 sizeof(p->o_arg.u.verifier.data));
855 p->c_arg.fh = &p->o_res.fh;
856 p->c_arg.stateid = &p->o_res.stateid;
857 p->c_arg.seqid = p->o_arg.seqid;
858 nfs4_init_opendata_res(p);
868 static void nfs4_opendata_free(struct kref *kref)
870 struct nfs4_opendata *p = container_of(kref,
871 struct nfs4_opendata, kref);
872 struct super_block *sb = p->dentry->d_sb;
874 nfs_free_seqid(p->o_arg.seqid);
875 if (p->state != NULL)
876 nfs4_put_open_state(p->state);
877 nfs4_put_state_owner(p->owner);
881 nfs_fattr_free_names(&p->f_attr);
885 static void nfs4_opendata_put(struct nfs4_opendata *p)
888 kref_put(&p->kref, nfs4_opendata_free);
891 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
895 ret = rpc_wait_for_completion_task(task);
899 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
903 if (open_mode & (O_EXCL|O_TRUNC))
905 switch (mode & (FMODE_READ|FMODE_WRITE)) {
907 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
908 && state->n_rdonly != 0;
911 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
912 && state->n_wronly != 0;
914 case FMODE_READ|FMODE_WRITE:
915 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
916 && state->n_rdwr != 0;
922 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
924 if (delegation == NULL)
926 if ((delegation->type & fmode) != fmode)
928 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
930 nfs_mark_delegation_referenced(delegation);
934 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
943 case FMODE_READ|FMODE_WRITE:
946 nfs4_state_set_mode_locked(state, state->state | fmode);
949 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
951 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
952 nfs4_stateid_copy(&state->stateid, stateid);
953 nfs4_stateid_copy(&state->open_stateid, stateid);
956 set_bit(NFS_O_RDONLY_STATE, &state->flags);
959 set_bit(NFS_O_WRONLY_STATE, &state->flags);
961 case FMODE_READ|FMODE_WRITE:
962 set_bit(NFS_O_RDWR_STATE, &state->flags);
966 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
968 write_seqlock(&state->seqlock);
969 nfs_set_open_stateid_locked(state, stateid, fmode);
970 write_sequnlock(&state->seqlock);
973 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
976 * Protect the call to nfs4_state_set_mode_locked and
977 * serialise the stateid update
979 write_seqlock(&state->seqlock);
980 if (deleg_stateid != NULL) {
981 nfs4_stateid_copy(&state->stateid, deleg_stateid);
982 set_bit(NFS_DELEGATED_STATE, &state->flags);
984 if (open_stateid != NULL)
985 nfs_set_open_stateid_locked(state, open_stateid, fmode);
986 write_sequnlock(&state->seqlock);
987 spin_lock(&state->owner->so_lock);
988 update_open_stateflags(state, fmode);
989 spin_unlock(&state->owner->so_lock);
992 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
994 struct nfs_inode *nfsi = NFS_I(state->inode);
995 struct nfs_delegation *deleg_cur;
998 fmode &= (FMODE_READ|FMODE_WRITE);
1001 deleg_cur = rcu_dereference(nfsi->delegation);
1002 if (deleg_cur == NULL)
1005 spin_lock(&deleg_cur->lock);
1006 if (nfsi->delegation != deleg_cur ||
1007 (deleg_cur->type & fmode) != fmode)
1008 goto no_delegation_unlock;
1010 if (delegation == NULL)
1011 delegation = &deleg_cur->stateid;
1012 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1013 goto no_delegation_unlock;
1015 nfs_mark_delegation_referenced(deleg_cur);
1016 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1018 no_delegation_unlock:
1019 spin_unlock(&deleg_cur->lock);
1023 if (!ret && open_stateid != NULL) {
1024 __update_open_stateid(state, open_stateid, NULL, fmode);
1032 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1034 struct nfs_delegation *delegation;
1037 delegation = rcu_dereference(NFS_I(inode)->delegation);
1038 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1043 nfs_inode_return_delegation(inode);
1046 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1048 struct nfs4_state *state = opendata->state;
1049 struct nfs_inode *nfsi = NFS_I(state->inode);
1050 struct nfs_delegation *delegation;
1051 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
1052 fmode_t fmode = opendata->o_arg.fmode;
1053 nfs4_stateid stateid;
1057 if (can_open_cached(state, fmode, open_mode)) {
1058 spin_lock(&state->owner->so_lock);
1059 if (can_open_cached(state, fmode, open_mode)) {
1060 update_open_stateflags(state, fmode);
1061 spin_unlock(&state->owner->so_lock);
1062 goto out_return_state;
1064 spin_unlock(&state->owner->so_lock);
1067 delegation = rcu_dereference(nfsi->delegation);
1068 if (!can_open_delegated(delegation, fmode)) {
1072 /* Save the delegation */
1073 nfs4_stateid_copy(&stateid, &delegation->stateid);
1075 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1080 /* Try to update the stateid using the delegation */
1081 if (update_open_stateid(state, NULL, &stateid, fmode))
1082 goto out_return_state;
1085 return ERR_PTR(ret);
1087 atomic_inc(&state->count);
1091 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1093 struct inode *inode;
1094 struct nfs4_state *state = NULL;
1095 struct nfs_delegation *delegation;
1098 if (!data->rpc_done) {
1099 state = nfs4_try_open_cached(data);
1104 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1106 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1107 ret = PTR_ERR(inode);
1111 state = nfs4_get_open_state(inode, data->owner);
1114 if (data->o_res.delegation_type != 0) {
1115 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1116 int delegation_flags = 0;
1119 delegation = rcu_dereference(NFS_I(inode)->delegation);
1121 delegation_flags = delegation->flags;
1123 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1124 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1125 "returning a delegation for "
1126 "OPEN(CLAIM_DELEGATE_CUR)\n",
1128 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1129 nfs_inode_set_delegation(state->inode,
1130 data->owner->so_cred,
1133 nfs_inode_reclaim_delegation(state->inode,
1134 data->owner->so_cred,
1138 update_open_stateid(state, &data->o_res.stateid, NULL,
1146 return ERR_PTR(ret);
1149 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1151 struct nfs_inode *nfsi = NFS_I(state->inode);
1152 struct nfs_open_context *ctx;
1154 spin_lock(&state->inode->i_lock);
1155 list_for_each_entry(ctx, &nfsi->open_files, list) {
1156 if (ctx->state != state)
1158 get_nfs_open_context(ctx);
1159 spin_unlock(&state->inode->i_lock);
1162 spin_unlock(&state->inode->i_lock);
1163 return ERR_PTR(-ENOENT);
1166 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1168 struct nfs4_opendata *opendata;
1170 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1171 if (opendata == NULL)
1172 return ERR_PTR(-ENOMEM);
1173 opendata->state = state;
1174 atomic_inc(&state->count);
1178 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1180 struct nfs4_state *newstate;
1183 opendata->o_arg.open_flags = 0;
1184 opendata->o_arg.fmode = fmode;
1185 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1186 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1187 nfs4_init_opendata_res(opendata);
1188 ret = _nfs4_recover_proc_open(opendata);
1191 newstate = nfs4_opendata_to_nfs4_state(opendata);
1192 if (IS_ERR(newstate))
1193 return PTR_ERR(newstate);
1194 nfs4_close_state(newstate, fmode);
1199 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1201 struct nfs4_state *newstate;
1204 /* memory barrier prior to reading state->n_* */
1205 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1207 if (state->n_rdwr != 0) {
1208 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1209 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1212 if (newstate != state)
1215 if (state->n_wronly != 0) {
1216 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1217 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1220 if (newstate != state)
1223 if (state->n_rdonly != 0) {
1224 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1225 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1228 if (newstate != state)
1232 * We may have performed cached opens for all three recoveries.
1233 * Check if we need to update the current stateid.
1235 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1236 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1237 write_seqlock(&state->seqlock);
1238 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1239 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1240 write_sequnlock(&state->seqlock);
1247 * reclaim state on the server after a reboot.
1249 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1251 struct nfs_delegation *delegation;
1252 struct nfs4_opendata *opendata;
1253 fmode_t delegation_type = 0;
1256 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1257 if (IS_ERR(opendata))
1258 return PTR_ERR(opendata);
1259 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1260 opendata->o_arg.fh = NFS_FH(state->inode);
1262 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1263 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1264 delegation_type = delegation->type;
1266 opendata->o_arg.u.delegation_type = delegation_type;
1267 status = nfs4_open_recover(opendata, state);
1268 nfs4_opendata_put(opendata);
1272 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1274 struct nfs_server *server = NFS_SERVER(state->inode);
1275 struct nfs4_exception exception = { };
1278 err = _nfs4_do_open_reclaim(ctx, state);
1279 if (err != -NFS4ERR_DELAY)
1281 nfs4_handle_exception(server, err, &exception);
1282 } while (exception.retry);
1286 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1288 struct nfs_open_context *ctx;
1291 ctx = nfs4_state_find_open_context(state);
1293 return PTR_ERR(ctx);
1294 ret = nfs4_do_open_reclaim(ctx, state);
1295 put_nfs_open_context(ctx);
1299 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1301 struct nfs4_opendata *opendata;
1304 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1305 if (IS_ERR(opendata))
1306 return PTR_ERR(opendata);
1307 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1308 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1309 ret = nfs4_open_recover(opendata, state);
1310 nfs4_opendata_put(opendata);
1314 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1316 struct nfs4_exception exception = { };
1317 struct nfs_server *server = NFS_SERVER(state->inode);
1320 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1326 case -NFS4ERR_BADSESSION:
1327 case -NFS4ERR_BADSLOT:
1328 case -NFS4ERR_BAD_HIGH_SLOT:
1329 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1330 case -NFS4ERR_DEADSESSION:
1331 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
1333 case -NFS4ERR_STALE_CLIENTID:
1334 case -NFS4ERR_STALE_STATEID:
1335 case -NFS4ERR_EXPIRED:
1336 /* Don't recall a delegation if it was lost */
1337 nfs4_schedule_lease_recovery(server->nfs_client);
1341 * The show must go on: exit, but mark the
1342 * stateid as needing recovery.
1344 case -NFS4ERR_DELEG_REVOKED:
1345 case -NFS4ERR_ADMIN_REVOKED:
1346 case -NFS4ERR_BAD_STATEID:
1347 nfs_inode_find_state_and_recover(state->inode,
1349 nfs4_schedule_stateid_recovery(server, state);
1352 * User RPCSEC_GSS context has expired.
1353 * We cannot recover this stateid now, so
1354 * skip it and allow recovery thread to
1361 err = nfs4_handle_exception(server, err, &exception);
1362 } while (exception.retry);
1367 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1369 struct nfs4_opendata *data = calldata;
1371 data->rpc_status = task->tk_status;
1372 if (data->rpc_status == 0) {
1373 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1374 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1375 renew_lease(data->o_res.server, data->timestamp);
1380 static void nfs4_open_confirm_release(void *calldata)
1382 struct nfs4_opendata *data = calldata;
1383 struct nfs4_state *state = NULL;
1385 /* If this request hasn't been cancelled, do nothing */
1386 if (data->cancelled == 0)
1388 /* In case of error, no cleanup! */
1389 if (!data->rpc_done)
1391 state = nfs4_opendata_to_nfs4_state(data);
1393 nfs4_close_state(state, data->o_arg.fmode);
1395 nfs4_opendata_put(data);
1398 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1399 .rpc_call_done = nfs4_open_confirm_done,
1400 .rpc_release = nfs4_open_confirm_release,
1404 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1406 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1408 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1409 struct rpc_task *task;
1410 struct rpc_message msg = {
1411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1412 .rpc_argp = &data->c_arg,
1413 .rpc_resp = &data->c_res,
1414 .rpc_cred = data->owner->so_cred,
1416 struct rpc_task_setup task_setup_data = {
1417 .rpc_client = server->client,
1418 .rpc_message = &msg,
1419 .callback_ops = &nfs4_open_confirm_ops,
1420 .callback_data = data,
1421 .workqueue = nfsiod_workqueue,
1422 .flags = RPC_TASK_ASYNC,
1426 kref_get(&data->kref);
1428 data->rpc_status = 0;
1429 data->timestamp = jiffies;
1430 task = rpc_run_task(&task_setup_data);
1432 return PTR_ERR(task);
1433 status = nfs4_wait_for_completion_rpc_task(task);
1435 data->cancelled = 1;
1438 status = data->rpc_status;
1443 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1445 struct nfs4_opendata *data = calldata;
1446 struct nfs4_state_owner *sp = data->owner;
1448 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1451 * Check if we still need to send an OPEN call, or if we can use
1452 * a delegation instead.
1454 if (data->state != NULL) {
1455 struct nfs_delegation *delegation;
1457 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1460 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1461 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1462 can_open_delegated(delegation, data->o_arg.fmode))
1463 goto unlock_no_action;
1466 /* Update client id. */
1467 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1468 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1469 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1470 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1472 data->timestamp = jiffies;
1473 if (nfs4_setup_sequence(data->o_arg.server,
1474 &data->o_arg.seq_args,
1475 &data->o_res.seq_res, task))
1477 rpc_call_start(task);
1482 task->tk_action = NULL;
1486 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1488 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1489 nfs4_open_prepare(task, calldata);
1492 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1494 struct nfs4_opendata *data = calldata;
1496 data->rpc_status = task->tk_status;
1498 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1501 if (task->tk_status == 0) {
1502 switch (data->o_res.f_attr->mode & S_IFMT) {
1506 data->rpc_status = -ELOOP;
1509 data->rpc_status = -EISDIR;
1512 data->rpc_status = -ENOTDIR;
1514 renew_lease(data->o_res.server, data->timestamp);
1515 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1516 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1521 static void nfs4_open_release(void *calldata)
1523 struct nfs4_opendata *data = calldata;
1524 struct nfs4_state *state = NULL;
1526 /* If this request hasn't been cancelled, do nothing */
1527 if (data->cancelled == 0)
1529 /* In case of error, no cleanup! */
1530 if (data->rpc_status != 0 || !data->rpc_done)
1532 /* In case we need an open_confirm, no cleanup! */
1533 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1535 state = nfs4_opendata_to_nfs4_state(data);
1537 nfs4_close_state(state, data->o_arg.fmode);
1539 nfs4_opendata_put(data);
1542 static const struct rpc_call_ops nfs4_open_ops = {
1543 .rpc_call_prepare = nfs4_open_prepare,
1544 .rpc_call_done = nfs4_open_done,
1545 .rpc_release = nfs4_open_release,
1548 static const struct rpc_call_ops nfs4_recover_open_ops = {
1549 .rpc_call_prepare = nfs4_recover_open_prepare,
1550 .rpc_call_done = nfs4_open_done,
1551 .rpc_release = nfs4_open_release,
1554 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1556 struct inode *dir = data->dir->d_inode;
1557 struct nfs_server *server = NFS_SERVER(dir);
1558 struct nfs_openargs *o_arg = &data->o_arg;
1559 struct nfs_openres *o_res = &data->o_res;
1560 struct rpc_task *task;
1561 struct rpc_message msg = {
1562 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1565 .rpc_cred = data->owner->so_cred,
1567 struct rpc_task_setup task_setup_data = {
1568 .rpc_client = server->client,
1569 .rpc_message = &msg,
1570 .callback_ops = &nfs4_open_ops,
1571 .callback_data = data,
1572 .workqueue = nfsiod_workqueue,
1573 .flags = RPC_TASK_ASYNC,
1577 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1578 kref_get(&data->kref);
1580 data->rpc_status = 0;
1581 data->cancelled = 0;
1583 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1584 task = rpc_run_task(&task_setup_data);
1586 return PTR_ERR(task);
1587 status = nfs4_wait_for_completion_rpc_task(task);
1589 data->cancelled = 1;
1592 status = data->rpc_status;
1598 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1600 struct inode *dir = data->dir->d_inode;
1601 struct nfs_openres *o_res = &data->o_res;
1604 status = nfs4_run_open_task(data, 1);
1605 if (status != 0 || !data->rpc_done)
1608 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1610 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1611 status = _nfs4_proc_open_confirm(data);
1620 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1622 static int _nfs4_proc_open(struct nfs4_opendata *data)
1624 struct inode *dir = data->dir->d_inode;
1625 struct nfs_server *server = NFS_SERVER(dir);
1626 struct nfs_openargs *o_arg = &data->o_arg;
1627 struct nfs_openres *o_res = &data->o_res;
1630 status = nfs4_run_open_task(data, 0);
1631 if (!data->rpc_done)
1634 if (status == -NFS4ERR_BADNAME &&
1635 !(o_arg->open_flags & O_CREAT))
1640 nfs_fattr_map_and_free_names(server, &data->f_attr);
1642 if (o_arg->open_flags & O_CREAT)
1643 update_changeattr(dir, &o_res->cinfo);
1644 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1645 server->caps &= ~NFS_CAP_POSIX_LOCK;
1646 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1647 status = _nfs4_proc_open_confirm(data);
1651 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1652 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1656 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1661 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1662 ret = nfs4_wait_clnt_recover(clp);
1665 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1666 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1668 nfs4_schedule_state_manager(clp);
1674 static int nfs4_recover_expired_lease(struct nfs_server *server)
1676 return nfs4_client_recover_expired_lease(server->nfs_client);
1681 * reclaim state on the server after a network partition.
1682 * Assumes caller holds the appropriate lock
1684 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1686 struct nfs4_opendata *opendata;
1689 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1690 if (IS_ERR(opendata))
1691 return PTR_ERR(opendata);
1692 ret = nfs4_open_recover(opendata, state);
1694 d_drop(ctx->dentry);
1695 nfs4_opendata_put(opendata);
1699 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1701 struct nfs_server *server = NFS_SERVER(state->inode);
1702 struct nfs4_exception exception = { };
1706 err = _nfs4_open_expired(ctx, state);
1710 case -NFS4ERR_GRACE:
1711 case -NFS4ERR_DELAY:
1712 nfs4_handle_exception(server, err, &exception);
1715 } while (exception.retry);
1720 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1722 struct nfs_open_context *ctx;
1725 ctx = nfs4_state_find_open_context(state);
1727 return PTR_ERR(ctx);
1728 ret = nfs4_do_open_expired(ctx, state);
1729 put_nfs_open_context(ctx);
1733 #if defined(CONFIG_NFS_V4_1)
1734 static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
1736 int status = NFS_OK;
1737 struct nfs_server *server = NFS_SERVER(state->inode);
1739 if (state->flags & flags) {
1740 status = nfs41_test_stateid(server, stateid);
1741 if (status != NFS_OK) {
1742 nfs41_free_stateid(server, stateid);
1743 state->flags &= ~flags;
1749 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1751 int deleg_status, open_status;
1752 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1753 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1755 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1756 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1758 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1760 return nfs4_open_expired(sp, state);
1765 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1766 * fields corresponding to attributes that were used to store the verifier.
1767 * Make sure we clobber those fields in the later setattr call
1769 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1771 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1772 !(sattr->ia_valid & ATTR_ATIME_SET))
1773 sattr->ia_valid |= ATTR_ATIME;
1775 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1776 !(sattr->ia_valid & ATTR_MTIME_SET))
1777 sattr->ia_valid |= ATTR_MTIME;
1781 * Returns a referenced nfs4_state
1783 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)
1785 struct nfs4_state_owner *sp;
1786 struct nfs4_state *state = NULL;
1787 struct nfs_server *server = NFS_SERVER(dir);
1788 struct nfs4_opendata *opendata;
1791 /* Protect against reboot recovery conflicts */
1793 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1795 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1798 status = nfs4_recover_expired_lease(server);
1800 goto err_put_state_owner;
1801 if (dentry->d_inode != NULL)
1802 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1804 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1805 if (opendata == NULL)
1806 goto err_put_state_owner;
1808 if (dentry->d_inode != NULL)
1809 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1811 status = _nfs4_proc_open(opendata);
1813 goto err_opendata_put;
1815 state = nfs4_opendata_to_nfs4_state(opendata);
1816 status = PTR_ERR(state);
1818 goto err_opendata_put;
1819 if (server->caps & NFS_CAP_POSIX_LOCK)
1820 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1822 if (opendata->o_arg.open_flags & O_EXCL) {
1823 nfs4_exclusive_attrset(opendata, sattr);
1825 nfs_fattr_init(opendata->o_res.f_attr);
1826 status = nfs4_do_setattr(state->inode, cred,
1827 opendata->o_res.f_attr, sattr,
1830 nfs_setattr_update_inode(state->inode, sattr);
1831 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1833 nfs4_opendata_put(opendata);
1834 nfs4_put_state_owner(sp);
1838 nfs4_opendata_put(opendata);
1839 err_put_state_owner:
1840 nfs4_put_state_owner(sp);
1847 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)
1849 struct nfs4_exception exception = { };
1850 struct nfs4_state *res;
1854 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
1857 /* NOTE: BAD_SEQID means the server and client disagree about the
1858 * book-keeping w.r.t. state-changing operations
1859 * (OPEN/CLOSE/LOCK/LOCKU...)
1860 * It is actually a sign of a bug on the client or on the server.
1862 * If we receive a BAD_SEQID error in the particular case of
1863 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1864 * have unhashed the old state_owner for us, and that we can
1865 * therefore safely retry using a new one. We should still warn
1866 * the user though...
1868 if (status == -NFS4ERR_BAD_SEQID) {
1869 pr_warn_ratelimited("NFS: v4 server %s "
1870 " returned a bad sequence-id error!\n",
1871 NFS_SERVER(dir)->nfs_client->cl_hostname);
1872 exception.retry = 1;
1876 * BAD_STATEID on OPEN means that the server cancelled our
1877 * state before it received the OPEN_CONFIRM.
1878 * Recover by retrying the request as per the discussion
1879 * on Page 181 of RFC3530.
1881 if (status == -NFS4ERR_BAD_STATEID) {
1882 exception.retry = 1;
1885 if (status == -EAGAIN) {
1886 /* We must have found a delegation */
1887 exception.retry = 1;
1890 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1891 status, &exception));
1892 } while (exception.retry);
1896 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1897 struct nfs_fattr *fattr, struct iattr *sattr,
1898 struct nfs4_state *state)
1900 struct nfs_server *server = NFS_SERVER(inode);
1901 struct nfs_setattrargs arg = {
1902 .fh = NFS_FH(inode),
1905 .bitmask = server->attr_bitmask,
1907 struct nfs_setattrres res = {
1911 struct rpc_message msg = {
1912 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1917 unsigned long timestamp = jiffies;
1920 nfs_fattr_init(fattr);
1922 if (state != NULL) {
1923 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1924 current->files, current->tgid);
1925 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1927 /* Use that stateid */
1929 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
1931 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1932 if (status == 0 && state != NULL)
1933 renew_lease(server, timestamp);
1937 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1938 struct nfs_fattr *fattr, struct iattr *sattr,
1939 struct nfs4_state *state)
1941 struct nfs_server *server = NFS_SERVER(inode);
1942 struct nfs4_exception exception = {
1948 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
1950 case -NFS4ERR_OPENMODE:
1951 if (state && !(state->state & FMODE_WRITE)) {
1953 if (sattr->ia_valid & ATTR_OPEN)
1958 err = nfs4_handle_exception(server, err, &exception);
1959 } while (exception.retry);
1964 struct nfs4_closedata {
1965 struct inode *inode;
1966 struct nfs4_state *state;
1967 struct nfs_closeargs arg;
1968 struct nfs_closeres res;
1969 struct nfs_fattr fattr;
1970 unsigned long timestamp;
1975 static void nfs4_free_closedata(void *data)
1977 struct nfs4_closedata *calldata = data;
1978 struct nfs4_state_owner *sp = calldata->state->owner;
1979 struct super_block *sb = calldata->state->inode->i_sb;
1982 pnfs_roc_release(calldata->state->inode);
1983 nfs4_put_open_state(calldata->state);
1984 nfs_free_seqid(calldata->arg.seqid);
1985 nfs4_put_state_owner(sp);
1986 nfs_sb_deactive(sb);
1990 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1993 spin_lock(&state->owner->so_lock);
1994 if (!(fmode & FMODE_READ))
1995 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1996 if (!(fmode & FMODE_WRITE))
1997 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1998 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1999 spin_unlock(&state->owner->so_lock);
2002 static void nfs4_close_done(struct rpc_task *task, void *data)
2004 struct nfs4_closedata *calldata = data;
2005 struct nfs4_state *state = calldata->state;
2006 struct nfs_server *server = NFS_SERVER(calldata->inode);
2008 dprintk("%s: begin!\n", __func__);
2009 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2011 /* hmm. we are done with the inode, and in the process of freeing
2012 * the state_owner. we keep this around to process errors
2014 switch (task->tk_status) {
2017 pnfs_roc_set_barrier(state->inode,
2018 calldata->roc_barrier);
2019 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2020 renew_lease(server, calldata->timestamp);
2021 nfs4_close_clear_stateid_flags(state,
2022 calldata->arg.fmode);
2024 case -NFS4ERR_STALE_STATEID:
2025 case -NFS4ERR_OLD_STATEID:
2026 case -NFS4ERR_BAD_STATEID:
2027 case -NFS4ERR_EXPIRED:
2028 if (calldata->arg.fmode == 0)
2031 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2032 rpc_restart_call_prepare(task);
2034 nfs_release_seqid(calldata->arg.seqid);
2035 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2036 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2039 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2041 struct nfs4_closedata *calldata = data;
2042 struct nfs4_state *state = calldata->state;
2045 dprintk("%s: begin!\n", __func__);
2046 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2049 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2050 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2051 spin_lock(&state->owner->so_lock);
2052 /* Calculate the change in open mode */
2053 if (state->n_rdwr == 0) {
2054 if (state->n_rdonly == 0) {
2055 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2056 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2057 calldata->arg.fmode &= ~FMODE_READ;
2059 if (state->n_wronly == 0) {
2060 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2061 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2062 calldata->arg.fmode &= ~FMODE_WRITE;
2065 spin_unlock(&state->owner->so_lock);
2068 /* Note: exit _without_ calling nfs4_close_done */
2069 task->tk_action = NULL;
2073 if (calldata->arg.fmode == 0) {
2074 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2075 if (calldata->roc &&
2076 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2077 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2083 nfs_fattr_init(calldata->res.fattr);
2084 calldata->timestamp = jiffies;
2085 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
2086 &calldata->arg.seq_args,
2087 &calldata->res.seq_res,
2090 rpc_call_start(task);
2092 dprintk("%s: done!\n", __func__);
2095 static const struct rpc_call_ops nfs4_close_ops = {
2096 .rpc_call_prepare = nfs4_close_prepare,
2097 .rpc_call_done = nfs4_close_done,
2098 .rpc_release = nfs4_free_closedata,
2102 * It is possible for data to be read/written from a mem-mapped file
2103 * after the sys_close call (which hits the vfs layer as a flush).
2104 * This means that we can't safely call nfsv4 close on a file until
2105 * the inode is cleared. This in turn means that we are not good
2106 * NFSv4 citizens - we do not indicate to the server to update the file's
2107 * share state even when we are done with one of the three share
2108 * stateid's in the inode.
2110 * NOTE: Caller must be holding the sp->so_owner semaphore!
2112 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
2114 struct nfs_server *server = NFS_SERVER(state->inode);
2115 struct nfs4_closedata *calldata;
2116 struct nfs4_state_owner *sp = state->owner;
2117 struct rpc_task *task;
2118 struct rpc_message msg = {
2119 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2120 .rpc_cred = state->owner->so_cred,
2122 struct rpc_task_setup task_setup_data = {
2123 .rpc_client = server->client,
2124 .rpc_message = &msg,
2125 .callback_ops = &nfs4_close_ops,
2126 .workqueue = nfsiod_workqueue,
2127 .flags = RPC_TASK_ASYNC,
2129 int status = -ENOMEM;
2131 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2132 if (calldata == NULL)
2134 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2135 calldata->inode = state->inode;
2136 calldata->state = state;
2137 calldata->arg.fh = NFS_FH(state->inode);
2138 calldata->arg.stateid = &state->open_stateid;
2139 /* Serialization for the sequence id */
2140 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2141 if (calldata->arg.seqid == NULL)
2142 goto out_free_calldata;
2143 calldata->arg.fmode = 0;
2144 calldata->arg.bitmask = server->cache_consistency_bitmask;
2145 calldata->res.fattr = &calldata->fattr;
2146 calldata->res.seqid = calldata->arg.seqid;
2147 calldata->res.server = server;
2148 calldata->roc = roc;
2149 nfs_sb_active(calldata->inode->i_sb);
2151 msg.rpc_argp = &calldata->arg;
2152 msg.rpc_resp = &calldata->res;
2153 task_setup_data.callback_data = calldata;
2154 task = rpc_run_task(&task_setup_data);
2156 return PTR_ERR(task);
2159 status = rpc_wait_for_completion_task(task);
2166 pnfs_roc_release(state->inode);
2167 nfs4_put_open_state(state);
2168 nfs4_put_state_owner(sp);
2172 static struct inode *
2173 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2175 struct nfs4_state *state;
2177 /* Protect against concurrent sillydeletes */
2178 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
2180 return ERR_CAST(state);
2182 return igrab(state->inode);
2185 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2187 if (ctx->state == NULL)
2190 nfs4_close_sync(ctx->state, ctx->mode);
2192 nfs4_close_state(ctx->state, ctx->mode);
2195 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2197 struct nfs4_server_caps_arg args = {
2200 struct nfs4_server_caps_res res = {};
2201 struct rpc_message msg = {
2202 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2208 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2210 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2211 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2212 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2213 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2214 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2215 NFS_CAP_CTIME|NFS_CAP_MTIME);
2216 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2217 server->caps |= NFS_CAP_ACLS;
2218 if (res.has_links != 0)
2219 server->caps |= NFS_CAP_HARDLINKS;
2220 if (res.has_symlinks != 0)
2221 server->caps |= NFS_CAP_SYMLINKS;
2222 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2223 server->caps |= NFS_CAP_FILEID;
2224 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2225 server->caps |= NFS_CAP_MODE;
2226 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2227 server->caps |= NFS_CAP_NLINK;
2228 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2229 server->caps |= NFS_CAP_OWNER;
2230 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2231 server->caps |= NFS_CAP_OWNER_GROUP;
2232 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2233 server->caps |= NFS_CAP_ATIME;
2234 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2235 server->caps |= NFS_CAP_CTIME;
2236 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2237 server->caps |= NFS_CAP_MTIME;
2239 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2240 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2241 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2242 server->acl_bitmask = res.acl_bitmask;
2243 server->fh_expire_type = res.fh_expire_type;
2249 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2251 struct nfs4_exception exception = { };
2254 err = nfs4_handle_exception(server,
2255 _nfs4_server_capabilities(server, fhandle),
2257 } while (exception.retry);
2261 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2262 struct nfs_fsinfo *info)
2264 struct nfs4_lookup_root_arg args = {
2265 .bitmask = nfs4_fattr_bitmap,
2267 struct nfs4_lookup_res res = {
2269 .fattr = info->fattr,
2272 struct rpc_message msg = {
2273 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2278 nfs_fattr_init(info->fattr);
2279 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2282 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2283 struct nfs_fsinfo *info)
2285 struct nfs4_exception exception = { };
2288 err = _nfs4_lookup_root(server, fhandle, info);
2291 case -NFS4ERR_WRONGSEC:
2294 err = nfs4_handle_exception(server, err, &exception);
2296 } while (exception.retry);
2301 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2302 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2304 struct rpc_auth *auth;
2307 auth = rpcauth_create(flavor, server->client);
2312 ret = nfs4_lookup_root(server, fhandle, info);
2317 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2318 struct nfs_fsinfo *info)
2320 int i, len, status = 0;
2321 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2323 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2324 flav_array[len] = RPC_AUTH_NULL;
2327 for (i = 0; i < len; i++) {
2328 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2329 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2334 * -EACCESS could mean that the user doesn't have correct permissions
2335 * to access the mount. It could also mean that we tried to mount
2336 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2337 * existing mount programs don't handle -EACCES very well so it should
2338 * be mapped to -EPERM instead.
2340 if (status == -EACCES)
2346 * get the file handle for the "/" directory on the server
2348 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2349 struct nfs_fsinfo *info)
2351 int minor_version = server->nfs_client->cl_minorversion;
2352 int status = nfs4_lookup_root(server, fhandle, info);
2353 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2355 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2356 * by nfs4_map_errors() as this function exits.
2358 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2360 status = nfs4_server_capabilities(server, fhandle);
2362 status = nfs4_do_fsinfo(server, fhandle, info);
2363 return nfs4_map_errors(status);
2367 * Get locations and (maybe) other attributes of a referral.
2368 * Note that we'll actually follow the referral later when
2369 * we detect fsid mismatch in inode revalidation
2371 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2372 const struct qstr *name, struct nfs_fattr *fattr,
2373 struct nfs_fh *fhandle)
2375 int status = -ENOMEM;
2376 struct page *page = NULL;
2377 struct nfs4_fs_locations *locations = NULL;
2379 page = alloc_page(GFP_KERNEL);
2382 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2383 if (locations == NULL)
2386 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2389 /* Make sure server returned a different fsid for the referral */
2390 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2391 dprintk("%s: server did not return a different fsid for"
2392 " a referral at %s\n", __func__, name->name);
2396 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2397 nfs_fixup_referral_attributes(&locations->fattr);
2399 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2400 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2401 memset(fhandle, 0, sizeof(struct nfs_fh));
2409 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2411 struct nfs4_getattr_arg args = {
2413 .bitmask = server->attr_bitmask,
2415 struct nfs4_getattr_res res = {
2419 struct rpc_message msg = {
2420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2425 nfs_fattr_init(fattr);
2426 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2429 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2431 struct nfs4_exception exception = { };
2434 err = nfs4_handle_exception(server,
2435 _nfs4_proc_getattr(server, fhandle, fattr),
2437 } while (exception.retry);
2442 * The file is not closed if it is opened due to the a request to change
2443 * the size of the file. The open call will not be needed once the
2444 * VFS layer lookup-intents are implemented.
2446 * Close is called when the inode is destroyed.
2447 * If we haven't opened the file for O_WRONLY, we
2448 * need to in the size_change case to obtain a stateid.
2451 * Because OPEN is always done by name in nfsv4, it is
2452 * possible that we opened a different file by the same
2453 * name. We can recognize this race condition, but we
2454 * can't do anything about it besides returning an error.
2456 * This will be fixed with VFS changes (lookup-intent).
2459 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2460 struct iattr *sattr)
2462 struct inode *inode = dentry->d_inode;
2463 struct rpc_cred *cred = NULL;
2464 struct nfs4_state *state = NULL;
2467 if (pnfs_ld_layoutret_on_setattr(inode))
2468 pnfs_return_layout(inode);
2470 nfs_fattr_init(fattr);
2472 /* Search for an existing open(O_WRITE) file */
2473 if (sattr->ia_valid & ATTR_FILE) {
2474 struct nfs_open_context *ctx;
2476 ctx = nfs_file_open_context(sattr->ia_file);
2483 /* Deal with open(O_TRUNC) */
2484 if (sattr->ia_valid & ATTR_OPEN)
2485 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2487 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2489 nfs_setattr_update_inode(inode, sattr);
2493 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2494 const struct qstr *name, struct nfs_fh *fhandle,
2495 struct nfs_fattr *fattr)
2497 struct nfs_server *server = NFS_SERVER(dir);
2499 struct nfs4_lookup_arg args = {
2500 .bitmask = server->attr_bitmask,
2501 .dir_fh = NFS_FH(dir),
2504 struct nfs4_lookup_res res = {
2509 struct rpc_message msg = {
2510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2515 nfs_fattr_init(fattr);
2517 dprintk("NFS call lookup %s\n", name->name);
2518 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2519 dprintk("NFS reply lookup: %d\n", status);
2523 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2525 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2526 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2527 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2531 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2532 struct qstr *name, struct nfs_fh *fhandle,
2533 struct nfs_fattr *fattr)
2535 struct nfs4_exception exception = { };
2536 struct rpc_clnt *client = *clnt;
2539 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2541 case -NFS4ERR_BADNAME:
2544 case -NFS4ERR_MOVED:
2545 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
2547 case -NFS4ERR_WRONGSEC:
2549 if (client != *clnt)
2552 client = nfs4_create_sec_client(client, dir, name);
2554 return PTR_ERR(client);
2556 exception.retry = 1;
2559 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2561 } while (exception.retry);
2566 else if (client != *clnt)
2567 rpc_shutdown_client(client);
2572 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
2573 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2576 struct rpc_clnt *client = NFS_CLIENT(dir);
2578 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2579 if (client != NFS_CLIENT(dir)) {
2580 rpc_shutdown_client(client);
2581 nfs_fixup_secinfo_attributes(fattr);
2587 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2588 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2591 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2593 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2595 rpc_shutdown_client(client);
2596 return ERR_PTR(status);
2601 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2603 struct nfs_server *server = NFS_SERVER(inode);
2604 struct nfs4_accessargs args = {
2605 .fh = NFS_FH(inode),
2606 .bitmask = server->cache_consistency_bitmask,
2608 struct nfs4_accessres res = {
2611 struct rpc_message msg = {
2612 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2615 .rpc_cred = entry->cred,
2617 int mode = entry->mask;
2621 * Determine which access bits we want to ask for...
2623 if (mode & MAY_READ)
2624 args.access |= NFS4_ACCESS_READ;
2625 if (S_ISDIR(inode->i_mode)) {
2626 if (mode & MAY_WRITE)
2627 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2628 if (mode & MAY_EXEC)
2629 args.access |= NFS4_ACCESS_LOOKUP;
2631 if (mode & MAY_WRITE)
2632 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2633 if (mode & MAY_EXEC)
2634 args.access |= NFS4_ACCESS_EXECUTE;
2637 res.fattr = nfs_alloc_fattr();
2638 if (res.fattr == NULL)
2641 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2644 if (res.access & NFS4_ACCESS_READ)
2645 entry->mask |= MAY_READ;
2646 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2647 entry->mask |= MAY_WRITE;
2648 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2649 entry->mask |= MAY_EXEC;
2650 nfs_refresh_inode(inode, res.fattr);
2652 nfs_free_fattr(res.fattr);
2656 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2658 struct nfs4_exception exception = { };
2661 err = nfs4_handle_exception(NFS_SERVER(inode),
2662 _nfs4_proc_access(inode, entry),
2664 } while (exception.retry);
2669 * TODO: For the time being, we don't try to get any attributes
2670 * along with any of the zero-copy operations READ, READDIR,
2673 * In the case of the first three, we want to put the GETATTR
2674 * after the read-type operation -- this is because it is hard
2675 * to predict the length of a GETATTR response in v4, and thus
2676 * align the READ data correctly. This means that the GETATTR
2677 * may end up partially falling into the page cache, and we should
2678 * shift it into the 'tail' of the xdr_buf before processing.
2679 * To do this efficiently, we need to know the total length
2680 * of data received, which doesn't seem to be available outside
2683 * In the case of WRITE, we also want to put the GETATTR after
2684 * the operation -- in this case because we want to make sure
2685 * we get the post-operation mtime and size. This means that
2686 * we can't use xdr_encode_pages() as written: we need a variant
2687 * of it which would leave room in the 'tail' iovec.
2689 * Both of these changes to the XDR layer would in fact be quite
2690 * minor, but I decided to leave them for a subsequent patch.
2692 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2693 unsigned int pgbase, unsigned int pglen)
2695 struct nfs4_readlink args = {
2696 .fh = NFS_FH(inode),
2701 struct nfs4_readlink_res res;
2702 struct rpc_message msg = {
2703 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2708 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2711 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2712 unsigned int pgbase, unsigned int pglen)
2714 struct nfs4_exception exception = { };
2717 err = nfs4_handle_exception(NFS_SERVER(inode),
2718 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2720 } while (exception.retry);
2726 * We will need to arrange for the VFS layer to provide an atomic open.
2727 * Until then, this create/open method is prone to inefficiency and race
2728 * conditions due to the lookup, create, and open VFS calls from sys_open()
2729 * placed on the wire.
2731 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2732 * The file will be opened again in the subsequent VFS open call
2733 * (nfs4_proc_file_open).
2735 * The open for read will just hang around to be used by any process that
2736 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2740 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2741 int flags, struct nfs_open_context *ctx)
2743 struct dentry *de = dentry;
2744 struct nfs4_state *state;
2745 struct rpc_cred *cred = NULL;
2754 sattr->ia_mode &= ~current_umask();
2755 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
2757 if (IS_ERR(state)) {
2758 status = PTR_ERR(state);
2761 d_add(dentry, igrab(state->inode));
2762 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2766 nfs4_close_sync(state, fmode);
2771 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2773 struct nfs_server *server = NFS_SERVER(dir);
2774 struct nfs_removeargs args = {
2776 .name.len = name->len,
2777 .name.name = name->name,
2778 .bitmask = server->attr_bitmask,
2780 struct nfs_removeres res = {
2783 struct rpc_message msg = {
2784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2788 int status = -ENOMEM;
2790 res.dir_attr = nfs_alloc_fattr();
2791 if (res.dir_attr == NULL)
2794 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2796 update_changeattr(dir, &res.cinfo);
2797 nfs_post_op_update_inode(dir, res.dir_attr);
2799 nfs_free_fattr(res.dir_attr);
2804 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2806 struct nfs4_exception exception = { };
2809 err = nfs4_handle_exception(NFS_SERVER(dir),
2810 _nfs4_proc_remove(dir, name),
2812 } while (exception.retry);
2816 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2818 struct nfs_server *server = NFS_SERVER(dir);
2819 struct nfs_removeargs *args = msg->rpc_argp;
2820 struct nfs_removeres *res = msg->rpc_resp;
2822 args->bitmask = server->cache_consistency_bitmask;
2823 res->server = server;
2824 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2825 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
2828 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2830 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2831 &data->args.seq_args,
2835 rpc_call_start(task);
2838 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2840 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2842 if (!nfs4_sequence_done(task, &res->seq_res))
2844 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2846 update_changeattr(dir, &res->cinfo);
2847 nfs_post_op_update_inode(dir, res->dir_attr);
2851 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2853 struct nfs_server *server = NFS_SERVER(dir);
2854 struct nfs_renameargs *arg = msg->rpc_argp;
2855 struct nfs_renameres *res = msg->rpc_resp;
2857 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2858 arg->bitmask = server->attr_bitmask;
2859 res->server = server;
2860 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
2863 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2865 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2866 &data->args.seq_args,
2870 rpc_call_start(task);
2873 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2874 struct inode *new_dir)
2876 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2878 if (!nfs4_sequence_done(task, &res->seq_res))
2880 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2883 update_changeattr(old_dir, &res->old_cinfo);
2884 nfs_post_op_update_inode(old_dir, res->old_fattr);
2885 update_changeattr(new_dir, &res->new_cinfo);
2886 nfs_post_op_update_inode(new_dir, res->new_fattr);
2890 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2891 struct inode *new_dir, struct qstr *new_name)
2893 struct nfs_server *server = NFS_SERVER(old_dir);
2894 struct nfs_renameargs arg = {
2895 .old_dir = NFS_FH(old_dir),
2896 .new_dir = NFS_FH(new_dir),
2897 .old_name = old_name,
2898 .new_name = new_name,
2899 .bitmask = server->attr_bitmask,
2901 struct nfs_renameres res = {
2904 struct rpc_message msg = {
2905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2909 int status = -ENOMEM;
2911 res.old_fattr = nfs_alloc_fattr();
2912 res.new_fattr = nfs_alloc_fattr();
2913 if (res.old_fattr == NULL || res.new_fattr == NULL)
2916 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2918 update_changeattr(old_dir, &res.old_cinfo);
2919 nfs_post_op_update_inode(old_dir, res.old_fattr);
2920 update_changeattr(new_dir, &res.new_cinfo);
2921 nfs_post_op_update_inode(new_dir, res.new_fattr);
2924 nfs_free_fattr(res.new_fattr);
2925 nfs_free_fattr(res.old_fattr);
2929 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2930 struct inode *new_dir, struct qstr *new_name)
2932 struct nfs4_exception exception = { };
2935 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2936 _nfs4_proc_rename(old_dir, old_name,
2939 } while (exception.retry);
2943 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2945 struct nfs_server *server = NFS_SERVER(inode);
2946 struct nfs4_link_arg arg = {
2947 .fh = NFS_FH(inode),
2948 .dir_fh = NFS_FH(dir),
2950 .bitmask = server->attr_bitmask,
2952 struct nfs4_link_res res = {
2955 struct rpc_message msg = {
2956 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2960 int status = -ENOMEM;
2962 res.fattr = nfs_alloc_fattr();
2963 res.dir_attr = nfs_alloc_fattr();
2964 if (res.fattr == NULL || res.dir_attr == NULL)
2967 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2969 update_changeattr(dir, &res.cinfo);
2970 nfs_post_op_update_inode(dir, res.dir_attr);
2971 nfs_post_op_update_inode(inode, res.fattr);
2974 nfs_free_fattr(res.dir_attr);
2975 nfs_free_fattr(res.fattr);
2979 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2981 struct nfs4_exception exception = { };
2984 err = nfs4_handle_exception(NFS_SERVER(inode),
2985 _nfs4_proc_link(inode, dir, name),
2987 } while (exception.retry);
2991 struct nfs4_createdata {
2992 struct rpc_message msg;
2993 struct nfs4_create_arg arg;
2994 struct nfs4_create_res res;
2996 struct nfs_fattr fattr;
2997 struct nfs_fattr dir_fattr;
3000 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3001 struct qstr *name, struct iattr *sattr, u32 ftype)
3003 struct nfs4_createdata *data;
3005 data = kzalloc(sizeof(*data), GFP_KERNEL);
3007 struct nfs_server *server = NFS_SERVER(dir);
3009 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3010 data->msg.rpc_argp = &data->arg;
3011 data->msg.rpc_resp = &data->res;
3012 data->arg.dir_fh = NFS_FH(dir);
3013 data->arg.server = server;
3014 data->arg.name = name;
3015 data->arg.attrs = sattr;
3016 data->arg.ftype = ftype;
3017 data->arg.bitmask = server->attr_bitmask;
3018 data->res.server = server;
3019 data->res.fh = &data->fh;
3020 data->res.fattr = &data->fattr;
3021 data->res.dir_fattr = &data->dir_fattr;
3022 nfs_fattr_init(data->res.fattr);
3023 nfs_fattr_init(data->res.dir_fattr);
3028 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3030 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3031 &data->arg.seq_args, &data->res.seq_res, 1);
3033 update_changeattr(dir, &data->res.dir_cinfo);
3034 nfs_post_op_update_inode(dir, data->res.dir_fattr);
3035 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3040 static void nfs4_free_createdata(struct nfs4_createdata *data)
3045 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3046 struct page *page, unsigned int len, struct iattr *sattr)
3048 struct nfs4_createdata *data;
3049 int status = -ENAMETOOLONG;
3051 if (len > NFS4_MAXPATHLEN)
3055 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3059 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3060 data->arg.u.symlink.pages = &page;
3061 data->arg.u.symlink.len = len;
3063 status = nfs4_do_create(dir, dentry, data);
3065 nfs4_free_createdata(data);
3070 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3071 struct page *page, unsigned int len, struct iattr *sattr)
3073 struct nfs4_exception exception = { };
3076 err = nfs4_handle_exception(NFS_SERVER(dir),
3077 _nfs4_proc_symlink(dir, dentry, page,
3080 } while (exception.retry);
3084 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3085 struct iattr *sattr)
3087 struct nfs4_createdata *data;
3088 int status = -ENOMEM;
3090 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3094 status = nfs4_do_create(dir, dentry, data);
3096 nfs4_free_createdata(data);
3101 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3102 struct iattr *sattr)
3104 struct nfs4_exception exception = { };
3107 sattr->ia_mode &= ~current_umask();
3109 err = nfs4_handle_exception(NFS_SERVER(dir),
3110 _nfs4_proc_mkdir(dir, dentry, sattr),
3112 } while (exception.retry);
3116 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3117 u64 cookie, struct page **pages, unsigned int count, int plus)
3119 struct inode *dir = dentry->d_inode;
3120 struct nfs4_readdir_arg args = {
3125 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3128 struct nfs4_readdir_res res;
3129 struct rpc_message msg = {
3130 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3137 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3138 dentry->d_parent->d_name.name,
3139 dentry->d_name.name,
3140 (unsigned long long)cookie);
3141 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3142 res.pgbase = args.pgbase;
3143 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3145 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
3146 status += args.pgbase;
3149 nfs_invalidate_atime(dir);
3151 dprintk("%s: returns %d\n", __func__, status);
3155 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3156 u64 cookie, struct page **pages, unsigned int count, int plus)
3158 struct nfs4_exception exception = { };
3161 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3162 _nfs4_proc_readdir(dentry, cred, cookie,
3163 pages, count, plus),
3165 } while (exception.retry);
3169 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3170 struct iattr *sattr, dev_t rdev)
3172 struct nfs4_createdata *data;
3173 int mode = sattr->ia_mode;
3174 int status = -ENOMEM;
3176 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3177 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3179 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3184 data->arg.ftype = NF4FIFO;
3185 else if (S_ISBLK(mode)) {
3186 data->arg.ftype = NF4BLK;
3187 data->arg.u.device.specdata1 = MAJOR(rdev);
3188 data->arg.u.device.specdata2 = MINOR(rdev);
3190 else if (S_ISCHR(mode)) {
3191 data->arg.ftype = NF4CHR;
3192 data->arg.u.device.specdata1 = MAJOR(rdev);
3193 data->arg.u.device.specdata2 = MINOR(rdev);
3196 status = nfs4_do_create(dir, dentry, data);
3198 nfs4_free_createdata(data);
3203 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3204 struct iattr *sattr, dev_t rdev)
3206 struct nfs4_exception exception = { };
3209 sattr->ia_mode &= ~current_umask();
3211 err = nfs4_handle_exception(NFS_SERVER(dir),
3212 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3214 } while (exception.retry);
3218 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3219 struct nfs_fsstat *fsstat)
3221 struct nfs4_statfs_arg args = {
3223 .bitmask = server->attr_bitmask,
3225 struct nfs4_statfs_res res = {
3228 struct rpc_message msg = {
3229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3234 nfs_fattr_init(fsstat->fattr);
3235 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3238 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3240 struct nfs4_exception exception = { };
3243 err = nfs4_handle_exception(server,
3244 _nfs4_proc_statfs(server, fhandle, fsstat),
3246 } while (exception.retry);
3250 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3251 struct nfs_fsinfo *fsinfo)
3253 struct nfs4_fsinfo_arg args = {
3255 .bitmask = server->attr_bitmask,
3257 struct nfs4_fsinfo_res res = {
3260 struct rpc_message msg = {
3261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3266 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3269 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3271 struct nfs4_exception exception = { };
3275 err = nfs4_handle_exception(server,
3276 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3278 } while (exception.retry);
3282 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3284 nfs_fattr_init(fsinfo->fattr);
3285 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3288 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3289 struct nfs_pathconf *pathconf)
3291 struct nfs4_pathconf_arg args = {
3293 .bitmask = server->attr_bitmask,
3295 struct nfs4_pathconf_res res = {
3296 .pathconf = pathconf,
3298 struct rpc_message msg = {
3299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3304 /* None of the pathconf attributes are mandatory to implement */
3305 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3306 memset(pathconf, 0, sizeof(*pathconf));
3310 nfs_fattr_init(pathconf->fattr);
3311 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3314 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3315 struct nfs_pathconf *pathconf)
3317 struct nfs4_exception exception = { };
3321 err = nfs4_handle_exception(server,
3322 _nfs4_proc_pathconf(server, fhandle, pathconf),
3324 } while (exception.retry);
3328 void __nfs4_read_done_cb(struct nfs_read_data *data)
3330 nfs_invalidate_atime(data->header->inode);
3333 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3335 struct nfs_server *server = NFS_SERVER(data->header->inode);
3337 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3338 rpc_restart_call_prepare(task);
3342 __nfs4_read_done_cb(data);
3343 if (task->tk_status > 0)
3344 renew_lease(server, data->timestamp);
3348 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3351 dprintk("--> %s\n", __func__);
3353 if (!nfs4_sequence_done(task, &data->res.seq_res))
3356 return data->read_done_cb ? data->read_done_cb(task, data) :
3357 nfs4_read_done_cb(task, data);
3360 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3362 data->timestamp = jiffies;
3363 data->read_done_cb = nfs4_read_done_cb;
3364 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3365 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3368 static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3370 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3371 &data->args.seq_args,
3375 rpc_call_start(task);
3378 /* Reset the the nfs_read_data to send the read to the MDS. */
3379 void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3381 struct nfs_pgio_header *hdr = data->header;
3382 struct inode *inode = hdr->inode;
3384 dprintk("%s Reset task for i/o through\n", __func__);
3385 data->ds_clp = NULL;
3386 /* offsets will differ in the dense stripe case */
3387 data->args.offset = data->mds_offset;
3388 data->args.fh = NFS_FH(inode);
3389 data->read_done_cb = nfs4_read_done_cb;
3390 task->tk_ops = hdr->mds_ops;
3391 rpc_task_reset_client(task, NFS_CLIENT(inode));
3393 EXPORT_SYMBOL_GPL(nfs4_reset_read);
3395 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3397 struct inode *inode = data->header->inode;
3399 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3400 rpc_restart_call_prepare(task);
3403 if (task->tk_status >= 0) {
3404 renew_lease(NFS_SERVER(inode), data->timestamp);
3405 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3410 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3412 if (!nfs4_sequence_done(task, &data->res.seq_res))
3414 return data->write_done_cb ? data->write_done_cb(task, data) :
3415 nfs4_write_done_cb(task, data);
3418 /* Reset the the nfs_write_data to send the write to the MDS. */
3419 void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3421 struct nfs_pgio_header *hdr = data->header;
3422 struct inode *inode = hdr->inode;
3424 dprintk("%s Reset task for i/o through\n", __func__);
3425 data->ds_clp = NULL;
3426 data->write_done_cb = nfs4_write_done_cb;
3427 data->args.fh = NFS_FH(inode);
3428 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3429 data->args.offset = data->mds_offset;
3430 data->res.fattr = &data->fattr;
3431 task->tk_ops = hdr->mds_ops;
3432 rpc_task_reset_client(task, NFS_CLIENT(inode));
3434 EXPORT_SYMBOL_GPL(nfs4_reset_write);
3436 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3438 struct nfs_server *server = NFS_SERVER(data->header->inode);
3441 data->args.bitmask = NULL;
3442 data->res.fattr = NULL;
3444 data->args.bitmask = server->cache_consistency_bitmask;
3445 if (!data->write_done_cb)
3446 data->write_done_cb = nfs4_write_done_cb;
3447 data->res.server = server;
3448 data->timestamp = jiffies;
3450 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3451 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3454 static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3456 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3457 &data->args.seq_args,
3461 rpc_call_start(task);
3464 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3466 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3467 &data->args.seq_args,
3471 rpc_call_start(task);
3474 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
3476 struct inode *inode = data->inode;
3478 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3479 rpc_restart_call_prepare(task);
3482 nfs_refresh_inode(inode, data->res.fattr);
3486 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
3488 if (!nfs4_sequence_done(task, &data->res.seq_res))
3490 return data->commit_done_cb(task, data);
3493 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
3495 struct nfs_server *server = NFS_SERVER(data->inode);
3498 data->args.bitmask = NULL;
3499 data->res.fattr = NULL;
3501 data->args.bitmask = server->cache_consistency_bitmask;
3502 if (data->commit_done_cb == NULL)
3503 data->commit_done_cb = nfs4_commit_done_cb;
3504 data->res.server = server;
3505 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3506 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3509 struct nfs4_renewdata {
3510 struct nfs_client *client;
3511 unsigned long timestamp;
3515 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3516 * standalone procedure for queueing an asynchronous RENEW.
3518 static void nfs4_renew_release(void *calldata)
3520 struct nfs4_renewdata *data = calldata;
3521 struct nfs_client *clp = data->client;
3523 if (atomic_read(&clp->cl_count) > 1)
3524 nfs4_schedule_state_renewal(clp);
3525 nfs_put_client(clp);
3529 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3531 struct nfs4_renewdata *data = calldata;
3532 struct nfs_client *clp = data->client;
3533 unsigned long timestamp = data->timestamp;
3535 if (task->tk_status < 0) {
3536 /* Unless we're shutting down, schedule state recovery! */
3537 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3539 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3540 nfs4_schedule_lease_recovery(clp);
3543 nfs4_schedule_path_down_recovery(clp);
3545 do_renew_lease(clp, timestamp);
3548 static const struct rpc_call_ops nfs4_renew_ops = {
3549 .rpc_call_done = nfs4_renew_done,
3550 .rpc_release = nfs4_renew_release,
3553 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3555 struct rpc_message msg = {
3556 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3560 struct nfs4_renewdata *data;
3562 if (renew_flags == 0)
3564 if (!atomic_inc_not_zero(&clp->cl_count))
3566 data = kmalloc(sizeof(*data), GFP_NOFS);
3570 data->timestamp = jiffies;
3571 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3572 &nfs4_renew_ops, data);
3575 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3577 struct rpc_message msg = {
3578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3582 unsigned long now = jiffies;
3585 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3588 do_renew_lease(clp, now);
3592 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3594 return (server->caps & NFS_CAP_ACLS)
3595 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3596 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3599 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3600 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3603 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3605 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3606 struct page **pages, unsigned int *pgbase)
3608 struct page *newpage, **spages;
3614 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3615 newpage = alloc_page(GFP_KERNEL);
3617 if (newpage == NULL)
3619 memcpy(page_address(newpage), buf, len);
3624 } while (buflen != 0);
3630 __free_page(spages[rc-1]);
3634 struct nfs4_cached_acl {
3640 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3642 struct nfs_inode *nfsi = NFS_I(inode);
3644 spin_lock(&inode->i_lock);
3645 kfree(nfsi->nfs4_acl);
3646 nfsi->nfs4_acl = acl;
3647 spin_unlock(&inode->i_lock);
3650 static void nfs4_zap_acl_attr(struct inode *inode)
3652 nfs4_set_cached_acl(inode, NULL);
3655 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3657 struct nfs_inode *nfsi = NFS_I(inode);
3658 struct nfs4_cached_acl *acl;
3661 spin_lock(&inode->i_lock);
3662 acl = nfsi->nfs4_acl;
3665 if (buf == NULL) /* user is just asking for length */
3667 if (acl->cached == 0)
3669 ret = -ERANGE; /* see getxattr(2) man page */
3670 if (acl->len > buflen)
3672 memcpy(buf, acl->data, acl->len);
3676 spin_unlock(&inode->i_lock);
3680 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3682 struct nfs4_cached_acl *acl;
3684 if (pages && acl_len <= PAGE_SIZE) {
3685 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3689 _copy_from_pages(acl->data, pages, pgbase, acl_len);
3691 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3698 nfs4_set_cached_acl(inode, acl);
3702 * The getxattr API returns the required buffer length when called with a
3703 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3704 * the required buf. On a NULL buf, we send a page of data to the server
3705 * guessing that the ACL request can be serviced by a page. If so, we cache
3706 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3707 * the cache. If not so, we throw away the page, and cache the required
3708 * length. The next getxattr call will then produce another round trip to
3709 * the server, this time with the input buf of the required size.
3711 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3713 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3714 struct nfs_getaclargs args = {
3715 .fh = NFS_FH(inode),
3719 struct nfs_getaclres res = {
3722 struct rpc_message msg = {
3723 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3727 int ret = -ENOMEM, npages, i, acl_len = 0;
3729 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3730 /* As long as we're doing a round trip to the server anyway,
3731 * let's be prepared for a page of acl data. */
3735 /* Add an extra page to handle the bitmap returned */
3738 for (i = 0; i < npages; i++) {
3739 pages[i] = alloc_page(GFP_KERNEL);
3744 /* for decoding across pages */
3745 res.acl_scratch = alloc_page(GFP_KERNEL);
3746 if (!res.acl_scratch)
3749 args.acl_len = npages * PAGE_SIZE;
3750 args.acl_pgbase = 0;
3752 /* Let decode_getfacl know not to fail if the ACL data is larger than
3753 * the page we send as a guess */
3755 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3757 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3758 __func__, buf, buflen, npages, args.acl_len);
3759 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3760 &msg, &args.seq_args, &res.seq_res, 0);
3764 acl_len = res.acl_len - res.acl_data_offset;
3765 if (acl_len > args.acl_len)
3766 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
3768 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
3772 if (acl_len > buflen)
3774 _copy_from_pages(buf, pages, res.acl_data_offset,
3779 for (i = 0; i < npages; i++)
3781 __free_page(pages[i]);
3782 if (res.acl_scratch)
3783 __free_page(res.acl_scratch);
3787 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3789 struct nfs4_exception exception = { };
3792 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3795 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3796 } while (exception.retry);
3800 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3802 struct nfs_server *server = NFS_SERVER(inode);
3805 if (!nfs4_server_supports_acls(server))
3807 ret = nfs_revalidate_inode(server, inode);
3810 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3811 nfs_zap_acl_cache(inode);
3812 ret = nfs4_read_cached_acl(inode, buf, buflen);
3814 /* -ENOENT is returned if there is no ACL or if there is an ACL
3815 * but no cached acl data, just the acl length */
3817 return nfs4_get_acl_uncached(inode, buf, buflen);
3820 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3822 struct nfs_server *server = NFS_SERVER(inode);
3823 struct page *pages[NFS4ACL_MAXPAGES];
3824 struct nfs_setaclargs arg = {
3825 .fh = NFS_FH(inode),
3829 struct nfs_setaclres res;
3830 struct rpc_message msg = {
3831 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3837 if (!nfs4_server_supports_acls(server))
3839 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3842 nfs_inode_return_delegation(inode);
3843 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3846 * Free each page after tx, so the only ref left is
3847 * held by the network stack
3850 put_page(pages[i-1]);
3853 * Acl update can result in inode attribute update.
3854 * so mark the attribute cache invalid.
3856 spin_lock(&inode->i_lock);
3857 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3858 spin_unlock(&inode->i_lock);
3859 nfs_access_zap_cache(inode);
3860 nfs_zap_acl_cache(inode);
3864 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3866 struct nfs4_exception exception = { };
3869 err = nfs4_handle_exception(NFS_SERVER(inode),
3870 __nfs4_proc_set_acl(inode, buf, buflen),
3872 } while (exception.retry);
3877 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3879 struct nfs_client *clp = server->nfs_client;
3881 if (task->tk_status >= 0)
3883 switch(task->tk_status) {
3884 case -NFS4ERR_DELEG_REVOKED:
3885 case -NFS4ERR_ADMIN_REVOKED:
3886 case -NFS4ERR_BAD_STATEID:
3889 nfs_remove_bad_delegation(state->inode);
3890 case -NFS4ERR_OPENMODE:
3893 nfs4_schedule_stateid_recovery(server, state);
3894 goto wait_on_recovery;
3895 case -NFS4ERR_EXPIRED:
3897 nfs4_schedule_stateid_recovery(server, state);
3898 case -NFS4ERR_STALE_STATEID:
3899 case -NFS4ERR_STALE_CLIENTID:
3900 nfs4_schedule_lease_recovery(clp);
3901 goto wait_on_recovery;
3902 #if defined(CONFIG_NFS_V4_1)
3903 case -NFS4ERR_BADSESSION:
3904 case -NFS4ERR_BADSLOT:
3905 case -NFS4ERR_BAD_HIGH_SLOT:
3906 case -NFS4ERR_DEADSESSION:
3907 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3908 case -NFS4ERR_SEQ_FALSE_RETRY:
3909 case -NFS4ERR_SEQ_MISORDERED:
3910 dprintk("%s ERROR %d, Reset session\n", __func__,
3912 nfs4_schedule_session_recovery(clp->cl_session);
3913 task->tk_status = 0;
3915 #endif /* CONFIG_NFS_V4_1 */
3916 case -NFS4ERR_DELAY:
3917 nfs_inc_server_stats(server, NFSIOS_DELAY);
3918 case -NFS4ERR_GRACE:
3920 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3921 task->tk_status = 0;
3923 case -NFS4ERR_RETRY_UNCACHED_REP:
3924 case -NFS4ERR_OLD_STATEID:
3925 task->tk_status = 0;
3928 task->tk_status = nfs4_map_errors(task->tk_status);
3931 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3932 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3933 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3934 task->tk_status = 0;
3938 static void nfs4_construct_boot_verifier(struct nfs_client *clp,
3939 nfs4_verifier *bootverf)
3943 verf[0] = htonl((u32)clp->cl_boot_time.tv_sec);
3944 verf[1] = htonl((u32)clp->cl_boot_time.tv_nsec);
3945 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3948 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3949 unsigned short port, struct rpc_cred *cred,
3950 struct nfs4_setclientid_res *res)
3952 nfs4_verifier sc_verifier;
3953 struct nfs4_setclientid setclientid = {
3954 .sc_verifier = &sc_verifier,
3956 .sc_cb_ident = clp->cl_cb_ident,
3958 struct rpc_message msg = {
3959 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3960 .rpc_argp = &setclientid,
3967 nfs4_construct_boot_verifier(clp, &sc_verifier);
3971 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3972 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3974 rpc_peeraddr2str(clp->cl_rpcclient,
3976 rpc_peeraddr2str(clp->cl_rpcclient,
3978 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3979 clp->cl_id_uniquifier);
3980 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3981 sizeof(setclientid.sc_netid),
3982 rpc_peeraddr2str(clp->cl_rpcclient,
3983 RPC_DISPLAY_NETID));
3984 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3985 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3986 clp->cl_ipaddr, port >> 8, port & 255);
3989 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3990 if (status != -NFS4ERR_CLID_INUSE)
3993 ++clp->cl_id_uniquifier;
3997 ssleep(clp->cl_lease_time / HZ + 1);
4002 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4003 struct nfs4_setclientid_res *arg,
4004 struct rpc_cred *cred)
4006 struct nfs_fsinfo fsinfo;
4007 struct rpc_message msg = {
4008 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4010 .rpc_resp = &fsinfo,
4017 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4019 spin_lock(&clp->cl_lock);
4020 clp->cl_lease_time = fsinfo.lease_time * HZ;
4021 clp->cl_last_renewal = now;
4022 spin_unlock(&clp->cl_lock);
4027 struct nfs4_delegreturndata {
4028 struct nfs4_delegreturnargs args;
4029 struct nfs4_delegreturnres res;
4031 nfs4_stateid stateid;
4032 unsigned long timestamp;
4033 struct nfs_fattr fattr;
4037 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4039 struct nfs4_delegreturndata *data = calldata;
4041 if (!nfs4_sequence_done(task, &data->res.seq_res))
4044 switch (task->tk_status) {
4045 case -NFS4ERR_STALE_STATEID:
4046 case -NFS4ERR_EXPIRED:
4048 renew_lease(data->res.server, data->timestamp);
4051 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4053 rpc_restart_call_prepare(task);
4057 data->rpc_status = task->tk_status;
4060 static void nfs4_delegreturn_release(void *calldata)
4065 #if defined(CONFIG_NFS_V4_1)
4066 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4068 struct nfs4_delegreturndata *d_data;
4070 d_data = (struct nfs4_delegreturndata *)data;
4072 if (nfs4_setup_sequence(d_data->res.server,
4073 &d_data->args.seq_args,
4074 &d_data->res.seq_res, task))
4076 rpc_call_start(task);
4078 #endif /* CONFIG_NFS_V4_1 */
4080 static const struct rpc_call_ops nfs4_delegreturn_ops = {
4081 #if defined(CONFIG_NFS_V4_1)
4082 .rpc_call_prepare = nfs4_delegreturn_prepare,
4083 #endif /* CONFIG_NFS_V4_1 */
4084 .rpc_call_done = nfs4_delegreturn_done,
4085 .rpc_release = nfs4_delegreturn_release,
4088 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4090 struct nfs4_delegreturndata *data;
4091 struct nfs_server *server = NFS_SERVER(inode);
4092 struct rpc_task *task;
4093 struct rpc_message msg = {
4094 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4097 struct rpc_task_setup task_setup_data = {
4098 .rpc_client = server->client,
4099 .rpc_message = &msg,
4100 .callback_ops = &nfs4_delegreturn_ops,
4101 .flags = RPC_TASK_ASYNC,
4105 data = kzalloc(sizeof(*data), GFP_NOFS);
4108 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4109 data->args.fhandle = &data->fh;
4110 data->args.stateid = &data->stateid;
4111 data->args.bitmask = server->cache_consistency_bitmask;
4112 nfs_copy_fh(&data->fh, NFS_FH(inode));
4113 nfs4_stateid_copy(&data->stateid, stateid);
4114 data->res.fattr = &data->fattr;
4115 data->res.server = server;
4116 nfs_fattr_init(data->res.fattr);
4117 data->timestamp = jiffies;
4118 data->rpc_status = 0;
4120 task_setup_data.callback_data = data;
4121 msg.rpc_argp = &data->args;
4122 msg.rpc_resp = &data->res;
4123 task = rpc_run_task(&task_setup_data);
4125 return PTR_ERR(task);
4128 status = nfs4_wait_for_completion_rpc_task(task);
4131 status = data->rpc_status;
4133 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4135 nfs_refresh_inode(inode, &data->fattr);
4141 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4143 struct nfs_server *server = NFS_SERVER(inode);
4144 struct nfs4_exception exception = { };
4147 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4149 case -NFS4ERR_STALE_STATEID:
4150 case -NFS4ERR_EXPIRED:
4154 err = nfs4_handle_exception(server, err, &exception);
4155 } while (exception.retry);
4159 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4160 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4163 * sleep, with exponential backoff, and retry the LOCK operation.
4165 static unsigned long
4166 nfs4_set_lock_task_retry(unsigned long timeout)
4168 freezable_schedule_timeout_killable(timeout);
4170 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4171 return NFS4_LOCK_MAXTIMEOUT;
4175 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4177 struct inode *inode = state->inode;
4178 struct nfs_server *server = NFS_SERVER(inode);
4179 struct nfs_client *clp = server->nfs_client;
4180 struct nfs_lockt_args arg = {
4181 .fh = NFS_FH(inode),
4184 struct nfs_lockt_res res = {
4187 struct rpc_message msg = {
4188 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4191 .rpc_cred = state->owner->so_cred,
4193 struct nfs4_lock_state *lsp;
4196 arg.lock_owner.clientid = clp->cl_clientid;
4197 status = nfs4_set_lock_state(state, request);
4200 lsp = request->fl_u.nfs4_fl.owner;
4201 arg.lock_owner.id = lsp->ls_seqid.owner_id;
4202 arg.lock_owner.s_dev = server->s_dev;
4203 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4206 request->fl_type = F_UNLCK;
4208 case -NFS4ERR_DENIED:
4211 request->fl_ops->fl_release_private(request);
4216 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4218 struct nfs4_exception exception = { };
4222 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4223 _nfs4_proc_getlk(state, cmd, request),
4225 } while (exception.retry);
4229 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4232 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4234 res = posix_lock_file_wait(file, fl);
4237 res = flock_lock_file_wait(file, fl);
4245 struct nfs4_unlockdata {
4246 struct nfs_locku_args arg;
4247 struct nfs_locku_res res;
4248 struct nfs4_lock_state *lsp;
4249 struct nfs_open_context *ctx;
4250 struct file_lock fl;
4251 const struct nfs_server *server;
4252 unsigned long timestamp;
4255 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4256 struct nfs_open_context *ctx,
4257 struct nfs4_lock_state *lsp,
4258 struct nfs_seqid *seqid)
4260 struct nfs4_unlockdata *p;
4261 struct inode *inode = lsp->ls_state->inode;
4263 p = kzalloc(sizeof(*p), GFP_NOFS);
4266 p->arg.fh = NFS_FH(inode);
4268 p->arg.seqid = seqid;
4269 p->res.seqid = seqid;
4270 p->arg.stateid = &lsp->ls_stateid;
4272 atomic_inc(&lsp->ls_count);
4273 /* Ensure we don't close file until we're done freeing locks! */
4274 p->ctx = get_nfs_open_context(ctx);
4275 memcpy(&p->fl, fl, sizeof(p->fl));
4276 p->server = NFS_SERVER(inode);
4280 static void nfs4_locku_release_calldata(void *data)
4282 struct nfs4_unlockdata *calldata = data;
4283 nfs_free_seqid(calldata->arg.seqid);
4284 nfs4_put_lock_state(calldata->lsp);
4285 put_nfs_open_context(calldata->ctx);
4289 static void nfs4_locku_done(struct rpc_task *task, void *data)
4291 struct nfs4_unlockdata *calldata = data;
4293 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4295 switch (task->tk_status) {
4297 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4298 &calldata->res.stateid);
4299 renew_lease(calldata->server, calldata->timestamp);
4301 case -NFS4ERR_BAD_STATEID:
4302 case -NFS4ERR_OLD_STATEID:
4303 case -NFS4ERR_STALE_STATEID:
4304 case -NFS4ERR_EXPIRED:
4307 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4308 rpc_restart_call_prepare(task);
4312 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4314 struct nfs4_unlockdata *calldata = data;
4316 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4318 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
4319 /* Note: exit _without_ running nfs4_locku_done */
4320 task->tk_action = NULL;
4323 calldata->timestamp = jiffies;
4324 if (nfs4_setup_sequence(calldata->server,
4325 &calldata->arg.seq_args,
4326 &calldata->res.seq_res, task))
4328 rpc_call_start(task);
4331 static const struct rpc_call_ops nfs4_locku_ops = {
4332 .rpc_call_prepare = nfs4_locku_prepare,
4333 .rpc_call_done = nfs4_locku_done,
4334 .rpc_release = nfs4_locku_release_calldata,
4337 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4338 struct nfs_open_context *ctx,
4339 struct nfs4_lock_state *lsp,
4340 struct nfs_seqid *seqid)
4342 struct nfs4_unlockdata *data;
4343 struct rpc_message msg = {
4344 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4345 .rpc_cred = ctx->cred,
4347 struct rpc_task_setup task_setup_data = {
4348 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4349 .rpc_message = &msg,
4350 .callback_ops = &nfs4_locku_ops,
4351 .workqueue = nfsiod_workqueue,
4352 .flags = RPC_TASK_ASYNC,
4355 /* Ensure this is an unlock - when canceling a lock, the
4356 * canceled lock is passed in, and it won't be an unlock.
4358 fl->fl_type = F_UNLCK;
4360 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4362 nfs_free_seqid(seqid);
4363 return ERR_PTR(-ENOMEM);
4366 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4367 msg.rpc_argp = &data->arg;
4368 msg.rpc_resp = &data->res;
4369 task_setup_data.callback_data = data;
4370 return rpc_run_task(&task_setup_data);
4373 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4375 struct nfs_inode *nfsi = NFS_I(state->inode);
4376 struct nfs_seqid *seqid;
4377 struct nfs4_lock_state *lsp;
4378 struct rpc_task *task;
4380 unsigned char fl_flags = request->fl_flags;
4382 status = nfs4_set_lock_state(state, request);
4383 /* Unlock _before_ we do the RPC call */
4384 request->fl_flags |= FL_EXISTS;
4385 down_read(&nfsi->rwsem);
4386 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4387 up_read(&nfsi->rwsem);
4390 up_read(&nfsi->rwsem);
4393 /* Is this a delegated lock? */
4394 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4396 lsp = request->fl_u.nfs4_fl.owner;
4397 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4401 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4402 status = PTR_ERR(task);
4405 status = nfs4_wait_for_completion_rpc_task(task);
4408 request->fl_flags = fl_flags;
4412 struct nfs4_lockdata {
4413 struct nfs_lock_args arg;
4414 struct nfs_lock_res res;
4415 struct nfs4_lock_state *lsp;
4416 struct nfs_open_context *ctx;
4417 struct file_lock fl;
4418 unsigned long timestamp;
4421 struct nfs_server *server;
4424 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4425 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4428 struct nfs4_lockdata *p;
4429 struct inode *inode = lsp->ls_state->inode;
4430 struct nfs_server *server = NFS_SERVER(inode);
4432 p = kzalloc(sizeof(*p), gfp_mask);
4436 p->arg.fh = NFS_FH(inode);
4438 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4439 if (p->arg.open_seqid == NULL)
4441 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4442 if (p->arg.lock_seqid == NULL)
4443 goto out_free_seqid;
4444 p->arg.lock_stateid = &lsp->ls_stateid;
4445 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4446 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4447 p->arg.lock_owner.s_dev = server->s_dev;
4448 p->res.lock_seqid = p->arg.lock_seqid;
4451 atomic_inc(&lsp->ls_count);
4452 p->ctx = get_nfs_open_context(ctx);
4453 memcpy(&p->fl, fl, sizeof(p->fl));
4456 nfs_free_seqid(p->arg.open_seqid);
4462 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4464 struct nfs4_lockdata *data = calldata;
4465 struct nfs4_state *state = data->lsp->ls_state;
4467 dprintk("%s: begin!\n", __func__);
4468 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4470 /* Do we need to do an open_to_lock_owner? */
4471 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4472 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4474 data->arg.open_stateid = &state->stateid;
4475 data->arg.new_lock_owner = 1;
4476 data->res.open_seqid = data->arg.open_seqid;
4478 data->arg.new_lock_owner = 0;
4479 data->timestamp = jiffies;
4480 if (nfs4_setup_sequence(data->server,
4481 &data->arg.seq_args,
4482 &data->res.seq_res, task))
4484 rpc_call_start(task);
4485 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4488 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4490 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4491 nfs4_lock_prepare(task, calldata);
4494 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4496 struct nfs4_lockdata *data = calldata;
4498 dprintk("%s: begin!\n", __func__);
4500 if (!nfs4_sequence_done(task, &data->res.seq_res))
4503 data->rpc_status = task->tk_status;
4504 if (data->arg.new_lock_owner != 0) {
4505 if (data->rpc_status == 0)
4506 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4510 if (data->rpc_status == 0) {
4511 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4512 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4513 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4516 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4519 static void nfs4_lock_release(void *calldata)
4521 struct nfs4_lockdata *data = calldata;
4523 dprintk("%s: begin!\n", __func__);
4524 nfs_free_seqid(data->arg.open_seqid);
4525 if (data->cancelled != 0) {
4526 struct rpc_task *task;
4527 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4528 data->arg.lock_seqid);
4530 rpc_put_task_async(task);
4531 dprintk("%s: cancelling lock!\n", __func__);
4533 nfs_free_seqid(data->arg.lock_seqid);
4534 nfs4_put_lock_state(data->lsp);
4535 put_nfs_open_context(data->ctx);
4537 dprintk("%s: done!\n", __func__);
4540 static const struct rpc_call_ops nfs4_lock_ops = {
4541 .rpc_call_prepare = nfs4_lock_prepare,
4542 .rpc_call_done = nfs4_lock_done,
4543 .rpc_release = nfs4_lock_release,
4546 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4547 .rpc_call_prepare = nfs4_recover_lock_prepare,
4548 .rpc_call_done = nfs4_lock_done,
4549 .rpc_release = nfs4_lock_release,
4552 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4555 case -NFS4ERR_ADMIN_REVOKED:
4556 case -NFS4ERR_BAD_STATEID:
4557 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4558 if (new_lock_owner != 0 ||
4559 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4560 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4562 case -NFS4ERR_STALE_STATEID:
4563 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4564 case -NFS4ERR_EXPIRED:
4565 nfs4_schedule_lease_recovery(server->nfs_client);
4569 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4571 struct nfs4_lockdata *data;
4572 struct rpc_task *task;
4573 struct rpc_message msg = {
4574 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4575 .rpc_cred = state->owner->so_cred,
4577 struct rpc_task_setup task_setup_data = {
4578 .rpc_client = NFS_CLIENT(state->inode),
4579 .rpc_message = &msg,
4580 .callback_ops = &nfs4_lock_ops,
4581 .workqueue = nfsiod_workqueue,
4582 .flags = RPC_TASK_ASYNC,
4586 dprintk("%s: begin!\n", __func__);
4587 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4588 fl->fl_u.nfs4_fl.owner,
4589 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4593 data->arg.block = 1;
4594 if (recovery_type > NFS_LOCK_NEW) {
4595 if (recovery_type == NFS_LOCK_RECLAIM)
4596 data->arg.reclaim = NFS_LOCK_RECLAIM;
4597 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4599 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4600 msg.rpc_argp = &data->arg;
4601 msg.rpc_resp = &data->res;
4602 task_setup_data.callback_data = data;
4603 task = rpc_run_task(&task_setup_data);
4605 return PTR_ERR(task);
4606 ret = nfs4_wait_for_completion_rpc_task(task);
4608 ret = data->rpc_status;
4610 nfs4_handle_setlk_error(data->server, data->lsp,
4611 data->arg.new_lock_owner, ret);
4613 data->cancelled = 1;
4615 dprintk("%s: done, ret = %d!\n", __func__, ret);
4619 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4621 struct nfs_server *server = NFS_SERVER(state->inode);
4622 struct nfs4_exception exception = {
4623 .inode = state->inode,
4628 /* Cache the lock if possible... */
4629 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4631 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4632 if (err != -NFS4ERR_DELAY)
4634 nfs4_handle_exception(server, err, &exception);
4635 } while (exception.retry);
4639 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4641 struct nfs_server *server = NFS_SERVER(state->inode);
4642 struct nfs4_exception exception = {
4643 .inode = state->inode,
4647 err = nfs4_set_lock_state(state, request);
4651 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4653 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4657 case -NFS4ERR_GRACE:
4658 case -NFS4ERR_DELAY:
4659 nfs4_handle_exception(server, err, &exception);
4662 } while (exception.retry);
4667 #if defined(CONFIG_NFS_V4_1)
4668 static int nfs41_check_expired_locks(struct nfs4_state *state)
4670 int status, ret = NFS_OK;
4671 struct nfs4_lock_state *lsp;
4672 struct nfs_server *server = NFS_SERVER(state->inode);
4674 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4675 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4676 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4677 if (status != NFS_OK) {
4678 nfs41_free_stateid(server, &lsp->ls_stateid);
4679 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4688 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4690 int status = NFS_OK;
4692 if (test_bit(LK_STATE_IN_USE, &state->flags))
4693 status = nfs41_check_expired_locks(state);
4694 if (status == NFS_OK)
4696 return nfs4_lock_expired(state, request);
4700 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4702 struct nfs_inode *nfsi = NFS_I(state->inode);
4703 unsigned char fl_flags = request->fl_flags;
4704 int status = -ENOLCK;
4706 if ((fl_flags & FL_POSIX) &&
4707 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4709 /* Is this a delegated open? */
4710 status = nfs4_set_lock_state(state, request);
4713 request->fl_flags |= FL_ACCESS;
4714 status = do_vfs_lock(request->fl_file, request);
4717 down_read(&nfsi->rwsem);
4718 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4719 /* Yes: cache locks! */
4720 /* ...but avoid races with delegation recall... */
4721 request->fl_flags = fl_flags & ~FL_SLEEP;
4722 status = do_vfs_lock(request->fl_file, request);
4725 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4728 /* Note: we always want to sleep here! */
4729 request->fl_flags = fl_flags | FL_SLEEP;
4730 if (do_vfs_lock(request->fl_file, request) < 0)
4731 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4732 "manager!\n", __func__);
4734 up_read(&nfsi->rwsem);
4736 request->fl_flags = fl_flags;
4740 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4742 struct nfs4_exception exception = {
4744 .inode = state->inode,
4749 err = _nfs4_proc_setlk(state, cmd, request);
4750 if (err == -NFS4ERR_DENIED)
4752 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4754 } while (exception.retry);
4759 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4761 struct nfs_open_context *ctx;
4762 struct nfs4_state *state;
4763 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4766 /* verify open state */
4767 ctx = nfs_file_open_context(filp);
4770 if (request->fl_start < 0 || request->fl_end < 0)
4773 if (IS_GETLK(cmd)) {
4775 return nfs4_proc_getlk(state, F_GETLK, request);
4779 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4782 if (request->fl_type == F_UNLCK) {
4784 return nfs4_proc_unlck(state, cmd, request);
4791 * Don't rely on the VFS having checked the file open mode,
4792 * since it won't do this for flock() locks.
4794 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4796 if (!(filp->f_mode & FMODE_READ))
4800 if (!(filp->f_mode & FMODE_WRITE))
4805 status = nfs4_proc_setlk(state, cmd, request);
4806 if ((status != -EAGAIN) || IS_SETLK(cmd))
4808 timeout = nfs4_set_lock_task_retry(timeout);
4809 status = -ERESTARTSYS;
4812 } while(status < 0);
4816 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4818 struct nfs_server *server = NFS_SERVER(state->inode);
4819 struct nfs4_exception exception = { };
4822 err = nfs4_set_lock_state(state, fl);
4826 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4829 printk(KERN_ERR "NFS: %s: unhandled error "
4830 "%d.\n", __func__, err);
4834 case -NFS4ERR_EXPIRED:
4835 nfs4_schedule_stateid_recovery(server, state);
4836 case -NFS4ERR_STALE_CLIENTID:
4837 case -NFS4ERR_STALE_STATEID:
4838 nfs4_schedule_lease_recovery(server->nfs_client);
4840 case -NFS4ERR_BADSESSION:
4841 case -NFS4ERR_BADSLOT:
4842 case -NFS4ERR_BAD_HIGH_SLOT:
4843 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4844 case -NFS4ERR_DEADSESSION:
4845 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
4849 * The show must go on: exit, but mark the
4850 * stateid as needing recovery.
4852 case -NFS4ERR_DELEG_REVOKED:
4853 case -NFS4ERR_ADMIN_REVOKED:
4854 case -NFS4ERR_BAD_STATEID:
4855 case -NFS4ERR_OPENMODE:
4856 nfs4_schedule_stateid_recovery(server, state);
4861 * User RPCSEC_GSS context has expired.
4862 * We cannot recover this stateid now, so
4863 * skip it and allow recovery thread to
4869 case -NFS4ERR_DENIED:
4870 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4873 case -NFS4ERR_DELAY:
4876 err = nfs4_handle_exception(server, err, &exception);
4877 } while (exception.retry);
4882 struct nfs_release_lockowner_data {
4883 struct nfs4_lock_state *lsp;
4884 struct nfs_server *server;
4885 struct nfs_release_lockowner_args args;
4888 static void nfs4_release_lockowner_release(void *calldata)
4890 struct nfs_release_lockowner_data *data = calldata;
4891 nfs4_free_lock_state(data->server, data->lsp);
4895 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
4896 .rpc_release = nfs4_release_lockowner_release,
4899 int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
4901 struct nfs_server *server = lsp->ls_state->owner->so_server;
4902 struct nfs_release_lockowner_data *data;
4903 struct rpc_message msg = {
4904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4907 if (server->nfs_client->cl_mvops->minor_version != 0)
4909 data = kmalloc(sizeof(*data), GFP_NOFS);
4913 data->server = server;
4914 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4915 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4916 data->args.lock_owner.s_dev = server->s_dev;
4917 msg.rpc_argp = &data->args;
4918 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4922 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4924 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4925 const void *buf, size_t buflen,
4926 int flags, int type)
4928 if (strcmp(key, "") != 0)
4931 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
4934 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4935 void *buf, size_t buflen, int type)
4937 if (strcmp(key, "") != 0)
4940 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
4943 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4944 size_t list_len, const char *name,
4945 size_t name_len, int type)
4947 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
4949 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4952 if (list && len <= list_len)
4953 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
4958 * nfs_fhget will use either the mounted_on_fileid or the fileid
4960 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4962 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4963 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4964 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4965 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
4968 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4969 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
4970 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4974 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
4975 const struct qstr *name,
4976 struct nfs4_fs_locations *fs_locations,
4979 struct nfs_server *server = NFS_SERVER(dir);
4981 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4983 struct nfs4_fs_locations_arg args = {
4984 .dir_fh = NFS_FH(dir),
4989 struct nfs4_fs_locations_res res = {
4990 .fs_locations = fs_locations,
4992 struct rpc_message msg = {
4993 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4999 dprintk("%s: start\n", __func__);
5001 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5002 * is not supported */
5003 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5004 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5006 bitmask[0] |= FATTR4_WORD0_FILEID;
5008 nfs_fattr_init(&fs_locations->fattr);
5009 fs_locations->server = server;
5010 fs_locations->nlocations = 0;
5011 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
5012 dprintk("%s: returned status = %d\n", __func__, status);
5016 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5017 const struct qstr *name,
5018 struct nfs4_fs_locations *fs_locations,
5021 struct nfs4_exception exception = { };
5024 err = nfs4_handle_exception(NFS_SERVER(dir),
5025 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
5027 } while (exception.retry);
5031 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5034 struct nfs4_secinfo_arg args = {
5035 .dir_fh = NFS_FH(dir),
5038 struct nfs4_secinfo_res res = {
5041 struct rpc_message msg = {
5042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5047 dprintk("NFS call secinfo %s\n", name->name);
5048 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5049 dprintk("NFS reply secinfo: %d\n", status);
5053 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5054 struct nfs4_secinfo_flavors *flavors)
5056 struct nfs4_exception exception = { };
5059 err = nfs4_handle_exception(NFS_SERVER(dir),
5060 _nfs4_proc_secinfo(dir, name, flavors),
5062 } while (exception.retry);
5066 #ifdef CONFIG_NFS_V4_1
5068 * Check the exchange flags returned by the server for invalid flags, having
5069 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5072 static int nfs4_check_cl_exchange_flags(u32 flags)
5074 if (flags & ~EXCHGID4_FLAG_MASK_R)
5076 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5077 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5079 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5083 return -NFS4ERR_INVAL;
5087 nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
5089 if (a->server_scope_sz == b->server_scope_sz &&
5090 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5097 * nfs4_proc_exchange_id()
5099 * Since the clientid has expired, all compounds using sessions
5100 * associated with the stale clientid will be returning
5101 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5102 * be in some phase of session reset.
5104 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
5106 nfs4_verifier verifier;
5107 struct nfs41_exchange_id_args args = {
5108 .verifier = &verifier,
5110 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
5112 struct nfs41_exchange_id_res res = {
5116 struct rpc_message msg = {
5117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5123 dprintk("--> %s\n", __func__);
5124 BUG_ON(clp == NULL);
5126 nfs4_construct_boot_verifier(clp, &verifier);
5128 args.id_len = scnprintf(args.id, sizeof(args.id),
5131 init_utsname()->nodename,
5132 init_utsname()->domainname,
5133 clp->cl_rpcclient->cl_auth->au_flavor);
5135 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
5136 if (unlikely(!res.server_scope)) {
5141 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
5142 if (unlikely(!res.impl_id)) {
5144 goto out_server_scope;
5147 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5149 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
5152 /* use the most recent implementation id */
5153 kfree(clp->impl_id);
5154 clp->impl_id = res.impl_id;
5159 if (clp->server_scope &&
5160 !nfs41_same_server_scope(clp->server_scope,
5161 res.server_scope)) {
5162 dprintk("%s: server_scope mismatch detected\n",
5164 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5165 kfree(clp->server_scope);
5166 clp->server_scope = NULL;
5169 if (!clp->server_scope) {
5170 clp->server_scope = res.server_scope;
5176 kfree(res.server_scope);
5179 dprintk("%s: Server Implementation ID: "
5180 "domain: %s, name: %s, date: %llu,%u\n",
5181 __func__, clp->impl_id->domain, clp->impl_id->name,
5182 clp->impl_id->date.seconds,
5183 clp->impl_id->date.nseconds);
5184 dprintk("<-- %s status= %d\n", __func__, status);
5188 struct nfs4_get_lease_time_data {
5189 struct nfs4_get_lease_time_args *args;
5190 struct nfs4_get_lease_time_res *res;
5191 struct nfs_client *clp;
5194 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5198 struct nfs4_get_lease_time_data *data =
5199 (struct nfs4_get_lease_time_data *)calldata;
5201 dprintk("--> %s\n", __func__);
5202 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5203 /* just setup sequence, do not trigger session recovery
5204 since we're invoked within one */
5205 ret = nfs41_setup_sequence(data->clp->cl_session,
5206 &data->args->la_seq_args,
5207 &data->res->lr_seq_res, task);
5209 BUG_ON(ret == -EAGAIN);
5210 rpc_call_start(task);
5211 dprintk("<-- %s\n", __func__);
5215 * Called from nfs4_state_manager thread for session setup, so don't recover
5216 * from sequence operation or clientid errors.
5218 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5220 struct nfs4_get_lease_time_data *data =
5221 (struct nfs4_get_lease_time_data *)calldata;
5223 dprintk("--> %s\n", __func__);
5224 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5226 switch (task->tk_status) {
5227 case -NFS4ERR_DELAY:
5228 case -NFS4ERR_GRACE:
5229 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5230 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5231 task->tk_status = 0;
5233 case -NFS4ERR_RETRY_UNCACHED_REP:
5234 rpc_restart_call_prepare(task);
5237 dprintk("<-- %s\n", __func__);
5240 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5241 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5242 .rpc_call_done = nfs4_get_lease_time_done,
5245 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5247 struct rpc_task *task;
5248 struct nfs4_get_lease_time_args args;
5249 struct nfs4_get_lease_time_res res = {
5250 .lr_fsinfo = fsinfo,
5252 struct nfs4_get_lease_time_data data = {
5257 struct rpc_message msg = {
5258 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5262 struct rpc_task_setup task_setup = {
5263 .rpc_client = clp->cl_rpcclient,
5264 .rpc_message = &msg,
5265 .callback_ops = &nfs4_get_lease_time_ops,
5266 .callback_data = &data,
5267 .flags = RPC_TASK_TIMEOUT,
5271 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5272 dprintk("--> %s\n", __func__);
5273 task = rpc_run_task(&task_setup);
5276 status = PTR_ERR(task);
5278 status = task->tk_status;
5281 dprintk("<-- %s return %d\n", __func__, status);
5286 static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5288 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5291 static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5292 struct nfs4_slot *new,
5296 struct nfs4_slot *old = NULL;
5299 spin_lock(&tbl->slot_tbl_lock);
5303 tbl->max_slots = max_slots;
5305 tbl->highest_used_slotid = -1; /* no slot is currently used */
5306 for (i = 0; i < tbl->max_slots; i++)
5307 tbl->slots[i].seq_nr = ivalue;
5308 spin_unlock(&tbl->slot_tbl_lock);
5313 * (re)Initialise a slot table
5315 static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5318 struct nfs4_slot *new = NULL;
5321 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5322 max_reqs, tbl->max_slots);
5324 /* Does the newly negotiated max_reqs match the existing slot table? */
5325 if (max_reqs != tbl->max_slots) {
5326 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
5332 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
5333 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5334 tbl, tbl->slots, tbl->max_slots);
5336 dprintk("<-- %s: return %d\n", __func__, ret);
5340 /* Destroy the slot table */
5341 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5343 if (session->fc_slot_table.slots != NULL) {
5344 kfree(session->fc_slot_table.slots);
5345 session->fc_slot_table.slots = NULL;
5347 if (session->bc_slot_table.slots != NULL) {
5348 kfree(session->bc_slot_table.slots);
5349 session->bc_slot_table.slots = NULL;
5355 * Initialize or reset the forechannel and backchannel tables
5357 static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
5359 struct nfs4_slot_table *tbl;
5362 dprintk("--> %s\n", __func__);
5364 tbl = &ses->fc_slot_table;
5365 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5366 if (status) /* -ENOMEM */
5369 tbl = &ses->bc_slot_table;
5370 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5371 if (status && tbl->slots == NULL)
5372 /* Fore and back channel share a connection so get
5373 * both slot tables or neither */
5374 nfs4_destroy_slot_tables(ses);
5378 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5380 struct nfs4_session *session;
5381 struct nfs4_slot_table *tbl;
5383 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5387 tbl = &session->fc_slot_table;
5388 tbl->highest_used_slotid = NFS4_NO_SLOT;
5389 spin_lock_init(&tbl->slot_tbl_lock);
5390 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5391 init_completion(&tbl->complete);
5393 tbl = &session->bc_slot_table;
5394 tbl->highest_used_slotid = NFS4_NO_SLOT;
5395 spin_lock_init(&tbl->slot_tbl_lock);
5396 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5397 init_completion(&tbl->complete);
5399 session->session_state = 1<<NFS4_SESSION_INITING;
5405 void nfs4_destroy_session(struct nfs4_session *session)
5407 struct rpc_xprt *xprt;
5409 nfs4_proc_destroy_session(session);
5412 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5414 dprintk("%s Destroy backchannel for xprt %p\n",
5416 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
5417 nfs4_destroy_slot_tables(session);
5422 * Initialize the values to be used by the client in CREATE_SESSION
5423 * If nfs4_init_session set the fore channel request and response sizes,
5426 * Set the back channel max_resp_sz_cached to zero to force the client to
5427 * always set csa_cachethis to FALSE because the current implementation
5428 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5430 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5432 struct nfs4_session *session = args->client->cl_session;
5433 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5434 mxresp_sz = session->fc_attrs.max_resp_sz;
5437 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5439 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5440 /* Fore channel attributes */
5441 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5442 args->fc_attrs.max_resp_sz = mxresp_sz;
5443 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5444 args->fc_attrs.max_reqs = max_session_slots;
5446 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5447 "max_ops=%u max_reqs=%u\n",
5449 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5450 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5452 /* Back channel attributes */
5453 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5454 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5455 args->bc_attrs.max_resp_sz_cached = 0;
5456 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5457 args->bc_attrs.max_reqs = 1;
5459 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5460 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5462 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5463 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5464 args->bc_attrs.max_reqs);
5467 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5469 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5470 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5472 if (rcvd->max_resp_sz > sent->max_resp_sz)
5475 * Our requested max_ops is the minimum we need; we're not
5476 * prepared to break up compounds into smaller pieces than that.
5477 * So, no point even trying to continue if the server won't
5480 if (rcvd->max_ops < sent->max_ops)
5482 if (rcvd->max_reqs == 0)
5484 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5485 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5489 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5491 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5492 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5494 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5496 if (rcvd->max_resp_sz < sent->max_resp_sz)
5498 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5500 /* These would render the backchannel useless: */
5501 if (rcvd->max_ops != sent->max_ops)
5503 if (rcvd->max_reqs != sent->max_reqs)
5508 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5509 struct nfs4_session *session)
5513 ret = nfs4_verify_fore_channel_attrs(args, session);
5516 return nfs4_verify_back_channel_attrs(args, session);
5519 static int _nfs4_proc_create_session(struct nfs_client *clp)
5521 struct nfs4_session *session = clp->cl_session;
5522 struct nfs41_create_session_args args = {
5524 .cb_program = NFS4_CALLBACK,
5526 struct nfs41_create_session_res res = {
5529 struct rpc_message msg = {
5530 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5536 nfs4_init_channel_attrs(&args);
5537 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5539 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5542 /* Verify the session's negotiated channel_attrs values */
5543 status = nfs4_verify_channel_attrs(&args, session);
5545 /* Increment the clientid slot sequence id */
5553 * Issues a CREATE_SESSION operation to the server.
5554 * It is the responsibility of the caller to verify the session is
5555 * expired before calling this routine.
5557 int nfs4_proc_create_session(struct nfs_client *clp)
5561 struct nfs4_session *session = clp->cl_session;
5563 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5565 status = _nfs4_proc_create_session(clp);
5569 /* Init or reset the session slot tables */
5570 status = nfs4_setup_session_slot_tables(session);
5571 dprintk("slot table setup returned %d\n", status);
5575 ptr = (unsigned *)&session->sess_id.data[0];
5576 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5577 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5579 dprintk("<-- %s\n", __func__);
5584 * Issue the over-the-wire RPC DESTROY_SESSION.
5585 * The caller must serialize access to this routine.
5587 int nfs4_proc_destroy_session(struct nfs4_session *session)
5590 struct rpc_message msg;
5592 dprintk("--> nfs4_proc_destroy_session\n");
5594 /* session is still being setup */
5595 if (session->clp->cl_cons_state != NFS_CS_READY)
5598 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5599 msg.rpc_argp = session;
5600 msg.rpc_resp = NULL;
5601 msg.rpc_cred = NULL;
5602 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5606 "NFS: Got error %d from the server on DESTROY_SESSION. "
5607 "Session has been destroyed regardless...\n", status);
5609 dprintk("<-- nfs4_proc_destroy_session\n");
5613 int nfs4_init_session(struct nfs_server *server)
5615 struct nfs_client *clp = server->nfs_client;
5616 struct nfs4_session *session;
5617 unsigned int rsize, wsize;
5620 if (!nfs4_has_session(clp))
5623 session = clp->cl_session;
5624 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5627 rsize = server->rsize;
5629 rsize = NFS_MAX_FILE_IO_SIZE;
5630 wsize = server->wsize;
5632 wsize = NFS_MAX_FILE_IO_SIZE;
5634 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5635 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5637 ret = nfs4_recover_expired_lease(server);
5639 ret = nfs4_check_client_ready(clp);
5643 int nfs4_init_ds_session(struct nfs_client *clp)
5645 struct nfs4_session *session = clp->cl_session;
5648 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5651 ret = nfs4_client_recover_expired_lease(clp);
5653 /* Test for the DS role */
5654 if (!is_ds_client(clp))
5657 ret = nfs4_check_client_ready(clp);
5661 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5665 * Renew the cl_session lease.
5667 struct nfs4_sequence_data {
5668 struct nfs_client *clp;
5669 struct nfs4_sequence_args args;
5670 struct nfs4_sequence_res res;
5673 static void nfs41_sequence_release(void *data)
5675 struct nfs4_sequence_data *calldata = data;
5676 struct nfs_client *clp = calldata->clp;
5678 if (atomic_read(&clp->cl_count) > 1)
5679 nfs4_schedule_state_renewal(clp);
5680 nfs_put_client(clp);
5684 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5686 switch(task->tk_status) {
5687 case -NFS4ERR_DELAY:
5688 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5691 nfs4_schedule_lease_recovery(clp);
5696 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5698 struct nfs4_sequence_data *calldata = data;
5699 struct nfs_client *clp = calldata->clp;
5701 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5704 if (task->tk_status < 0) {
5705 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5706 if (atomic_read(&clp->cl_count) == 1)
5709 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5710 rpc_restart_call_prepare(task);
5714 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5716 dprintk("<-- %s\n", __func__);
5719 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5721 struct nfs4_sequence_data *calldata = data;
5722 struct nfs_client *clp = calldata->clp;
5723 struct nfs4_sequence_args *args;
5724 struct nfs4_sequence_res *res;
5726 args = task->tk_msg.rpc_argp;
5727 res = task->tk_msg.rpc_resp;
5729 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
5731 rpc_call_start(task);
5734 static const struct rpc_call_ops nfs41_sequence_ops = {
5735 .rpc_call_done = nfs41_sequence_call_done,
5736 .rpc_call_prepare = nfs41_sequence_prepare,
5737 .rpc_release = nfs41_sequence_release,
5740 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5742 struct nfs4_sequence_data *calldata;
5743 struct rpc_message msg = {
5744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5747 struct rpc_task_setup task_setup_data = {
5748 .rpc_client = clp->cl_rpcclient,
5749 .rpc_message = &msg,
5750 .callback_ops = &nfs41_sequence_ops,
5751 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5754 if (!atomic_inc_not_zero(&clp->cl_count))
5755 return ERR_PTR(-EIO);
5756 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5757 if (calldata == NULL) {
5758 nfs_put_client(clp);
5759 return ERR_PTR(-ENOMEM);
5761 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
5762 msg.rpc_argp = &calldata->args;
5763 msg.rpc_resp = &calldata->res;
5764 calldata->clp = clp;
5765 task_setup_data.callback_data = calldata;
5767 return rpc_run_task(&task_setup_data);
5770 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5772 struct rpc_task *task;
5775 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5777 task = _nfs41_proc_sequence(clp, cred);
5779 ret = PTR_ERR(task);
5781 rpc_put_task_async(task);
5782 dprintk("<-- %s status=%d\n", __func__, ret);
5786 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5788 struct rpc_task *task;
5791 task = _nfs41_proc_sequence(clp, cred);
5793 ret = PTR_ERR(task);
5796 ret = rpc_wait_for_completion_task(task);
5798 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5800 if (task->tk_status == 0)
5801 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5802 ret = task->tk_status;
5806 dprintk("<-- %s status=%d\n", __func__, ret);
5810 struct nfs4_reclaim_complete_data {
5811 struct nfs_client *clp;
5812 struct nfs41_reclaim_complete_args arg;
5813 struct nfs41_reclaim_complete_res res;
5816 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5818 struct nfs4_reclaim_complete_data *calldata = data;
5820 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5821 if (nfs41_setup_sequence(calldata->clp->cl_session,
5822 &calldata->arg.seq_args,
5823 &calldata->res.seq_res, task))
5826 rpc_call_start(task);
5829 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5831 switch(task->tk_status) {
5833 case -NFS4ERR_COMPLETE_ALREADY:
5834 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5836 case -NFS4ERR_DELAY:
5837 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5839 case -NFS4ERR_RETRY_UNCACHED_REP:
5842 nfs4_schedule_lease_recovery(clp);
5847 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5849 struct nfs4_reclaim_complete_data *calldata = data;
5850 struct nfs_client *clp = calldata->clp;
5851 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5853 dprintk("--> %s\n", __func__);
5854 if (!nfs41_sequence_done(task, res))
5857 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5858 rpc_restart_call_prepare(task);
5861 dprintk("<-- %s\n", __func__);
5864 static void nfs4_free_reclaim_complete_data(void *data)
5866 struct nfs4_reclaim_complete_data *calldata = data;
5871 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5872 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5873 .rpc_call_done = nfs4_reclaim_complete_done,
5874 .rpc_release = nfs4_free_reclaim_complete_data,
5878 * Issue a global reclaim complete.
5880 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5882 struct nfs4_reclaim_complete_data *calldata;
5883 struct rpc_task *task;
5884 struct rpc_message msg = {
5885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5887 struct rpc_task_setup task_setup_data = {
5888 .rpc_client = clp->cl_rpcclient,
5889 .rpc_message = &msg,
5890 .callback_ops = &nfs4_reclaim_complete_call_ops,
5891 .flags = RPC_TASK_ASYNC,
5893 int status = -ENOMEM;
5895 dprintk("--> %s\n", __func__);
5896 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5897 if (calldata == NULL)
5899 calldata->clp = clp;
5900 calldata->arg.one_fs = 0;
5902 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
5903 msg.rpc_argp = &calldata->arg;
5904 msg.rpc_resp = &calldata->res;
5905 task_setup_data.callback_data = calldata;
5906 task = rpc_run_task(&task_setup_data);
5908 status = PTR_ERR(task);
5911 status = nfs4_wait_for_completion_rpc_task(task);
5913 status = task->tk_status;
5917 dprintk("<-- %s status=%d\n", __func__, status);
5922 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5924 struct nfs4_layoutget *lgp = calldata;
5925 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5927 dprintk("--> %s\n", __func__);
5928 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5929 * right now covering the LAYOUTGET we are about to send.
5930 * However, that is not so catastrophic, and there seems
5931 * to be no way to prevent it completely.
5933 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5934 &lgp->res.seq_res, task))
5936 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5937 NFS_I(lgp->args.inode)->layout,
5938 lgp->args.ctx->state)) {
5939 rpc_exit(task, NFS4_OK);
5942 rpc_call_start(task);
5945 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5947 struct nfs4_layoutget *lgp = calldata;
5948 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5950 dprintk("--> %s\n", __func__);
5952 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5955 switch (task->tk_status) {
5958 case -NFS4ERR_LAYOUTTRYLATER:
5959 case -NFS4ERR_RECALLCONFLICT:
5960 task->tk_status = -NFS4ERR_DELAY;
5963 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5964 rpc_restart_call_prepare(task);
5968 dprintk("<-- %s\n", __func__);
5971 static void nfs4_layoutget_release(void *calldata)
5973 struct nfs4_layoutget *lgp = calldata;
5975 dprintk("--> %s\n", __func__);
5976 put_nfs_open_context(lgp->args.ctx);
5978 dprintk("<-- %s\n", __func__);
5981 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5982 .rpc_call_prepare = nfs4_layoutget_prepare,
5983 .rpc_call_done = nfs4_layoutget_done,
5984 .rpc_release = nfs4_layoutget_release,
5987 int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5989 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5990 struct rpc_task *task;
5991 struct rpc_message msg = {
5992 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5993 .rpc_argp = &lgp->args,
5994 .rpc_resp = &lgp->res,
5996 struct rpc_task_setup task_setup_data = {
5997 .rpc_client = server->client,
5998 .rpc_message = &msg,
5999 .callback_ops = &nfs4_layoutget_call_ops,
6000 .callback_data = lgp,
6001 .flags = RPC_TASK_ASYNC,
6005 dprintk("--> %s\n", __func__);
6007 lgp->res.layoutp = &lgp->args.layout;
6008 lgp->res.seq_res.sr_slot = NULL;
6009 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
6010 task = rpc_run_task(&task_setup_data);
6012 return PTR_ERR(task);
6013 status = nfs4_wait_for_completion_rpc_task(task);
6015 status = task->tk_status;
6017 status = pnfs_layout_process(lgp);
6019 dprintk("<-- %s status=%d\n", __func__, status);
6024 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6026 struct nfs4_layoutreturn *lrp = calldata;
6028 dprintk("--> %s\n", __func__);
6029 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
6030 &lrp->res.seq_res, task))
6032 rpc_call_start(task);
6035 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6037 struct nfs4_layoutreturn *lrp = calldata;
6038 struct nfs_server *server;
6039 struct pnfs_layout_hdr *lo = lrp->args.layout;
6041 dprintk("--> %s\n", __func__);
6043 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6046 server = NFS_SERVER(lrp->args.inode);
6047 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6048 rpc_restart_call_prepare(task);
6051 spin_lock(&lo->plh_inode->i_lock);
6052 if (task->tk_status == 0) {
6053 if (lrp->res.lrs_present) {
6054 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6056 BUG_ON(!list_empty(&lo->plh_segs));
6058 lo->plh_block_lgets--;
6059 spin_unlock(&lo->plh_inode->i_lock);
6060 dprintk("<-- %s\n", __func__);
6063 static void nfs4_layoutreturn_release(void *calldata)
6065 struct nfs4_layoutreturn *lrp = calldata;
6067 dprintk("--> %s\n", __func__);
6068 put_layout_hdr(lrp->args.layout);
6070 dprintk("<-- %s\n", __func__);
6073 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6074 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6075 .rpc_call_done = nfs4_layoutreturn_done,
6076 .rpc_release = nfs4_layoutreturn_release,
6079 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6081 struct rpc_task *task;
6082 struct rpc_message msg = {
6083 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6084 .rpc_argp = &lrp->args,
6085 .rpc_resp = &lrp->res,
6087 struct rpc_task_setup task_setup_data = {
6088 .rpc_client = lrp->clp->cl_rpcclient,
6089 .rpc_message = &msg,
6090 .callback_ops = &nfs4_layoutreturn_call_ops,
6091 .callback_data = lrp,
6095 dprintk("--> %s\n", __func__);
6096 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
6097 task = rpc_run_task(&task_setup_data);
6099 return PTR_ERR(task);
6100 status = task->tk_status;
6101 dprintk("<-- %s status=%d\n", __func__, status);
6107 * Retrieve the list of Data Server devices from the MDS.
6109 static int _nfs4_getdevicelist(struct nfs_server *server,
6110 const struct nfs_fh *fh,
6111 struct pnfs_devicelist *devlist)
6113 struct nfs4_getdevicelist_args args = {
6115 .layoutclass = server->pnfs_curr_ld->id,
6117 struct nfs4_getdevicelist_res res = {
6120 struct rpc_message msg = {
6121 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6127 dprintk("--> %s\n", __func__);
6128 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6130 dprintk("<-- %s status=%d\n", __func__, status);
6134 int nfs4_proc_getdevicelist(struct nfs_server *server,
6135 const struct nfs_fh *fh,
6136 struct pnfs_devicelist *devlist)
6138 struct nfs4_exception exception = { };
6142 err = nfs4_handle_exception(server,
6143 _nfs4_getdevicelist(server, fh, devlist),
6145 } while (exception.retry);
6147 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6148 err, devlist->num_devs);
6152 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6155 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6157 struct nfs4_getdeviceinfo_args args = {
6160 struct nfs4_getdeviceinfo_res res = {
6163 struct rpc_message msg = {
6164 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6170 dprintk("--> %s\n", __func__);
6171 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6172 dprintk("<-- %s status=%d\n", __func__, status);
6177 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6179 struct nfs4_exception exception = { };
6183 err = nfs4_handle_exception(server,
6184 _nfs4_proc_getdeviceinfo(server, pdev),
6186 } while (exception.retry);
6189 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6191 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6193 struct nfs4_layoutcommit_data *data = calldata;
6194 struct nfs_server *server = NFS_SERVER(data->args.inode);
6196 if (nfs4_setup_sequence(server, &data->args.seq_args,
6197 &data->res.seq_res, task))
6199 rpc_call_start(task);
6203 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6205 struct nfs4_layoutcommit_data *data = calldata;
6206 struct nfs_server *server = NFS_SERVER(data->args.inode);
6208 if (!nfs4_sequence_done(task, &data->res.seq_res))
6211 switch (task->tk_status) { /* Just ignore these failures */
6212 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6213 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6214 case -NFS4ERR_BADLAYOUT: /* no layout */
6215 case -NFS4ERR_GRACE: /* loca_recalim always false */
6216 task->tk_status = 0;
6219 nfs_post_op_update_inode_force_wcc(data->args.inode,
6223 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6224 rpc_restart_call_prepare(task);
6230 static void nfs4_layoutcommit_release(void *calldata)
6232 struct nfs4_layoutcommit_data *data = calldata;
6233 struct pnfs_layout_segment *lseg, *tmp;
6234 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
6236 pnfs_cleanup_layoutcommit(data);
6237 /* Matched by references in pnfs_set_layoutcommit */
6238 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6239 list_del_init(&lseg->pls_lc_list);
6240 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6245 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6246 smp_mb__after_clear_bit();
6247 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6249 put_rpccred(data->cred);
6253 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6254 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6255 .rpc_call_done = nfs4_layoutcommit_done,
6256 .rpc_release = nfs4_layoutcommit_release,
6260 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6262 struct rpc_message msg = {
6263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6264 .rpc_argp = &data->args,
6265 .rpc_resp = &data->res,
6266 .rpc_cred = data->cred,
6268 struct rpc_task_setup task_setup_data = {
6269 .task = &data->task,
6270 .rpc_client = NFS_CLIENT(data->args.inode),
6271 .rpc_message = &msg,
6272 .callback_ops = &nfs4_layoutcommit_ops,
6273 .callback_data = data,
6274 .flags = RPC_TASK_ASYNC,
6276 struct rpc_task *task;
6279 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6280 "lbw: %llu inode %lu\n",
6281 data->task.tk_pid, sync,
6282 data->args.lastbytewritten,
6283 data->args.inode->i_ino);
6285 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6286 task = rpc_run_task(&task_setup_data);
6288 return PTR_ERR(task);
6291 status = nfs4_wait_for_completion_rpc_task(task);
6294 status = task->tk_status;
6296 dprintk("%s: status %d\n", __func__, status);
6302 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6303 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6305 struct nfs41_secinfo_no_name_args args = {
6306 .style = SECINFO_STYLE_CURRENT_FH,
6308 struct nfs4_secinfo_res res = {
6311 struct rpc_message msg = {
6312 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6316 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6320 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6321 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6323 struct nfs4_exception exception = { };
6326 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6329 case -NFS4ERR_WRONGSEC:
6330 case -NFS4ERR_NOTSUPP:
6333 err = nfs4_handle_exception(server, err, &exception);
6335 } while (exception.retry);
6341 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6342 struct nfs_fsinfo *info)
6346 rpc_authflavor_t flavor;
6347 struct nfs4_secinfo_flavors *flavors;
6349 page = alloc_page(GFP_KERNEL);
6355 flavors = page_address(page);
6356 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6359 * Fall back on "guess and check" method if
6360 * the server doesn't support SECINFO_NO_NAME
6362 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6363 err = nfs4_find_root_sec(server, fhandle, info);
6369 flavor = nfs_find_best_sec(flavors);
6371 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6381 static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6384 struct nfs41_test_stateid_args args = {
6387 struct nfs41_test_stateid_res res;
6388 struct rpc_message msg = {
6389 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6394 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6395 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6397 if (status == NFS_OK)
6402 static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6404 struct nfs4_exception exception = { };
6407 err = nfs4_handle_exception(server,
6408 _nfs41_test_stateid(server, stateid),
6410 } while (exception.retry);
6414 static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6416 struct nfs41_free_stateid_args args = {
6419 struct nfs41_free_stateid_res res;
6420 struct rpc_message msg = {
6421 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6426 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6427 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6430 static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6432 struct nfs4_exception exception = { };
6435 err = nfs4_handle_exception(server,
6436 _nfs4_free_stateid(server, stateid),
6438 } while (exception.retry);
6442 static bool nfs41_match_stateid(const nfs4_stateid *s1,
6443 const nfs4_stateid *s2)
6445 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
6448 if (s1->seqid == s2->seqid)
6450 if (s1->seqid == 0 || s2->seqid == 0)
6456 #endif /* CONFIG_NFS_V4_1 */
6458 static bool nfs4_match_stateid(const nfs4_stateid *s1,
6459 const nfs4_stateid *s2)
6461 return nfs4_stateid_match(s1, s2);
6465 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6466 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6467 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6468 .recover_open = nfs4_open_reclaim,
6469 .recover_lock = nfs4_lock_reclaim,
6470 .establish_clid = nfs4_init_clientid,
6471 .get_clid_cred = nfs4_get_setclientid_cred,
6474 #if defined(CONFIG_NFS_V4_1)
6475 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6476 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6477 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6478 .recover_open = nfs4_open_reclaim,
6479 .recover_lock = nfs4_lock_reclaim,
6480 .establish_clid = nfs41_init_clientid,
6481 .get_clid_cred = nfs4_get_exchange_id_cred,
6482 .reclaim_complete = nfs41_proc_reclaim_complete,
6484 #endif /* CONFIG_NFS_V4_1 */
6486 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6487 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6488 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6489 .recover_open = nfs4_open_expired,
6490 .recover_lock = nfs4_lock_expired,
6491 .establish_clid = nfs4_init_clientid,
6492 .get_clid_cred = nfs4_get_setclientid_cred,
6495 #if defined(CONFIG_NFS_V4_1)
6496 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6497 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6498 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6499 .recover_open = nfs41_open_expired,
6500 .recover_lock = nfs41_lock_expired,
6501 .establish_clid = nfs41_init_clientid,
6502 .get_clid_cred = nfs4_get_exchange_id_cred,
6504 #endif /* CONFIG_NFS_V4_1 */
6506 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6507 .sched_state_renewal = nfs4_proc_async_renew,
6508 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6509 .renew_lease = nfs4_proc_renew,
6512 #if defined(CONFIG_NFS_V4_1)
6513 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6514 .sched_state_renewal = nfs41_proc_async_sequence,
6515 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
6516 .renew_lease = nfs4_proc_sequence,
6520 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6522 .call_sync = _nfs4_call_sync,
6523 .match_stateid = nfs4_match_stateid,
6524 .find_root_sec = nfs4_find_root_sec,
6525 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6526 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6527 .state_renewal_ops = &nfs40_state_renewal_ops,
6530 #if defined(CONFIG_NFS_V4_1)
6531 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6533 .call_sync = _nfs4_call_sync_session,
6534 .match_stateid = nfs41_match_stateid,
6535 .find_root_sec = nfs41_find_root_sec,
6536 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6537 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6538 .state_renewal_ops = &nfs41_state_renewal_ops,
6542 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6543 [0] = &nfs_v4_0_minor_ops,
6544 #if defined(CONFIG_NFS_V4_1)
6545 [1] = &nfs_v4_1_minor_ops,
6549 static const struct inode_operations nfs4_file_inode_operations = {
6550 .permission = nfs_permission,
6551 .getattr = nfs_getattr,
6552 .setattr = nfs_setattr,
6553 .getxattr = generic_getxattr,
6554 .setxattr = generic_setxattr,
6555 .listxattr = generic_listxattr,
6556 .removexattr = generic_removexattr,
6559 const struct nfs_rpc_ops nfs_v4_clientops = {
6560 .version = 4, /* protocol version */
6561 .dentry_ops = &nfs4_dentry_operations,
6562 .dir_inode_ops = &nfs4_dir_inode_operations,
6563 .file_inode_ops = &nfs4_file_inode_operations,
6564 .file_ops = &nfs4_file_operations,
6565 .getroot = nfs4_proc_get_root,
6566 .submount = nfs4_submount,
6567 .getattr = nfs4_proc_getattr,
6568 .setattr = nfs4_proc_setattr,
6569 .lookup = nfs4_proc_lookup,
6570 .access = nfs4_proc_access,
6571 .readlink = nfs4_proc_readlink,
6572 .create = nfs4_proc_create,
6573 .remove = nfs4_proc_remove,
6574 .unlink_setup = nfs4_proc_unlink_setup,
6575 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
6576 .unlink_done = nfs4_proc_unlink_done,
6577 .rename = nfs4_proc_rename,
6578 .rename_setup = nfs4_proc_rename_setup,
6579 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
6580 .rename_done = nfs4_proc_rename_done,
6581 .link = nfs4_proc_link,
6582 .symlink = nfs4_proc_symlink,
6583 .mkdir = nfs4_proc_mkdir,
6584 .rmdir = nfs4_proc_remove,
6585 .readdir = nfs4_proc_readdir,
6586 .mknod = nfs4_proc_mknod,
6587 .statfs = nfs4_proc_statfs,
6588 .fsinfo = nfs4_proc_fsinfo,
6589 .pathconf = nfs4_proc_pathconf,
6590 .set_capabilities = nfs4_server_capabilities,
6591 .decode_dirent = nfs4_decode_dirent,
6592 .read_setup = nfs4_proc_read_setup,
6593 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
6594 .read_done = nfs4_read_done,
6595 .write_setup = nfs4_proc_write_setup,
6596 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
6597 .write_done = nfs4_write_done,
6598 .commit_setup = nfs4_proc_commit_setup,
6599 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
6600 .commit_done = nfs4_commit_done,
6601 .lock = nfs4_proc_lock,
6602 .clear_acl_cache = nfs4_zap_acl_attr,
6603 .close_context = nfs4_close_context,
6604 .open_context = nfs4_atomic_open,
6605 .init_client = nfs4_init_client,
6608 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6609 .prefix = XATTR_NAME_NFSV4_ACL,
6610 .list = nfs4_xattr_list_nfs4_acl,
6611 .get = nfs4_xattr_get_nfs4_acl,
6612 .set = nfs4_xattr_set_nfs4_acl,
6615 const struct xattr_handler *nfs4_xattr_handlers[] = {
6616 &nfs4_xattr_nfs4_acl_handler,
6620 module_param(max_session_slots, ushort, 0644);
6621 MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6622 "requests the client will negotiate");