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;
791 struct nfs_fattr dir_attr;
793 struct dentry *dentry;
794 struct nfs4_state_owner *owner;
795 struct nfs4_state *state;
797 unsigned long timestamp;
798 unsigned int rpc_done : 1;
804 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
806 p->o_res.f_attr = &p->f_attr;
807 p->o_res.dir_attr = &p->dir_attr;
808 p->o_res.seqid = p->o_arg.seqid;
809 p->c_res.seqid = p->c_arg.seqid;
810 p->o_res.server = p->o_arg.server;
811 nfs_fattr_init(&p->f_attr);
812 nfs_fattr_init(&p->dir_attr);
813 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
816 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
817 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
818 const struct iattr *attrs,
821 struct dentry *parent = dget_parent(dentry);
822 struct inode *dir = parent->d_inode;
823 struct nfs_server *server = NFS_SERVER(dir);
824 struct nfs4_opendata *p;
826 p = kzalloc(sizeof(*p), gfp_mask);
829 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
830 if (p->o_arg.seqid == NULL)
832 nfs_sb_active(dentry->d_sb);
833 p->dentry = dget(dentry);
836 atomic_inc(&sp->so_count);
837 p->o_arg.fh = NFS_FH(dir);
838 p->o_arg.open_flags = flags;
839 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
840 p->o_arg.clientid = server->nfs_client->cl_clientid;
841 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
842 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
843 p->o_arg.name = &dentry->d_name;
844 p->o_arg.server = server;
845 p->o_arg.bitmask = server->attr_bitmask;
846 p->o_arg.dir_bitmask = server->cache_consistency_bitmask;
847 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
848 if (attrs != NULL && attrs->ia_valid != 0) {
851 p->o_arg.u.attrs = &p->attrs;
852 memcpy(&p->attrs, attrs, sizeof(p->attrs));
855 verf[1] = current->pid;
856 memcpy(p->o_arg.u.verifier.data, verf,
857 sizeof(p->o_arg.u.verifier.data));
859 p->c_arg.fh = &p->o_res.fh;
860 p->c_arg.stateid = &p->o_res.stateid;
861 p->c_arg.seqid = p->o_arg.seqid;
862 nfs4_init_opendata_res(p);
872 static void nfs4_opendata_free(struct kref *kref)
874 struct nfs4_opendata *p = container_of(kref,
875 struct nfs4_opendata, kref);
876 struct super_block *sb = p->dentry->d_sb;
878 nfs_free_seqid(p->o_arg.seqid);
879 if (p->state != NULL)
880 nfs4_put_open_state(p->state);
881 nfs4_put_state_owner(p->owner);
885 nfs_fattr_free_names(&p->f_attr);
889 static void nfs4_opendata_put(struct nfs4_opendata *p)
892 kref_put(&p->kref, nfs4_opendata_free);
895 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
899 ret = rpc_wait_for_completion_task(task);
903 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
907 if (open_mode & (O_EXCL|O_TRUNC))
909 switch (mode & (FMODE_READ|FMODE_WRITE)) {
911 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
912 && state->n_rdonly != 0;
915 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
916 && state->n_wronly != 0;
918 case FMODE_READ|FMODE_WRITE:
919 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
920 && state->n_rdwr != 0;
926 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
928 if (delegation == NULL)
930 if ((delegation->type & fmode) != fmode)
932 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
934 nfs_mark_delegation_referenced(delegation);
938 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
947 case FMODE_READ|FMODE_WRITE:
950 nfs4_state_set_mode_locked(state, state->state | fmode);
953 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
955 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
956 nfs4_stateid_copy(&state->stateid, stateid);
957 nfs4_stateid_copy(&state->open_stateid, stateid);
960 set_bit(NFS_O_RDONLY_STATE, &state->flags);
963 set_bit(NFS_O_WRONLY_STATE, &state->flags);
965 case FMODE_READ|FMODE_WRITE:
966 set_bit(NFS_O_RDWR_STATE, &state->flags);
970 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
972 write_seqlock(&state->seqlock);
973 nfs_set_open_stateid_locked(state, stateid, fmode);
974 write_sequnlock(&state->seqlock);
977 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
980 * Protect the call to nfs4_state_set_mode_locked and
981 * serialise the stateid update
983 write_seqlock(&state->seqlock);
984 if (deleg_stateid != NULL) {
985 nfs4_stateid_copy(&state->stateid, deleg_stateid);
986 set_bit(NFS_DELEGATED_STATE, &state->flags);
988 if (open_stateid != NULL)
989 nfs_set_open_stateid_locked(state, open_stateid, fmode);
990 write_sequnlock(&state->seqlock);
991 spin_lock(&state->owner->so_lock);
992 update_open_stateflags(state, fmode);
993 spin_unlock(&state->owner->so_lock);
996 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
998 struct nfs_inode *nfsi = NFS_I(state->inode);
999 struct nfs_delegation *deleg_cur;
1002 fmode &= (FMODE_READ|FMODE_WRITE);
1005 deleg_cur = rcu_dereference(nfsi->delegation);
1006 if (deleg_cur == NULL)
1009 spin_lock(&deleg_cur->lock);
1010 if (nfsi->delegation != deleg_cur ||
1011 (deleg_cur->type & fmode) != fmode)
1012 goto no_delegation_unlock;
1014 if (delegation == NULL)
1015 delegation = &deleg_cur->stateid;
1016 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1017 goto no_delegation_unlock;
1019 nfs_mark_delegation_referenced(deleg_cur);
1020 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1022 no_delegation_unlock:
1023 spin_unlock(&deleg_cur->lock);
1027 if (!ret && open_stateid != NULL) {
1028 __update_open_stateid(state, open_stateid, NULL, fmode);
1036 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1038 struct nfs_delegation *delegation;
1041 delegation = rcu_dereference(NFS_I(inode)->delegation);
1042 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1047 nfs_inode_return_delegation(inode);
1050 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1052 struct nfs4_state *state = opendata->state;
1053 struct nfs_inode *nfsi = NFS_I(state->inode);
1054 struct nfs_delegation *delegation;
1055 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
1056 fmode_t fmode = opendata->o_arg.fmode;
1057 nfs4_stateid stateid;
1061 if (can_open_cached(state, fmode, open_mode)) {
1062 spin_lock(&state->owner->so_lock);
1063 if (can_open_cached(state, fmode, open_mode)) {
1064 update_open_stateflags(state, fmode);
1065 spin_unlock(&state->owner->so_lock);
1066 goto out_return_state;
1068 spin_unlock(&state->owner->so_lock);
1071 delegation = rcu_dereference(nfsi->delegation);
1072 if (!can_open_delegated(delegation, fmode)) {
1076 /* Save the delegation */
1077 nfs4_stateid_copy(&stateid, &delegation->stateid);
1079 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1084 /* Try to update the stateid using the delegation */
1085 if (update_open_stateid(state, NULL, &stateid, fmode))
1086 goto out_return_state;
1089 return ERR_PTR(ret);
1091 atomic_inc(&state->count);
1095 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1097 struct inode *inode;
1098 struct nfs4_state *state = NULL;
1099 struct nfs_delegation *delegation;
1102 if (!data->rpc_done) {
1103 state = nfs4_try_open_cached(data);
1108 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1110 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1111 ret = PTR_ERR(inode);
1115 state = nfs4_get_open_state(inode, data->owner);
1118 if (data->o_res.delegation_type != 0) {
1119 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1120 int delegation_flags = 0;
1123 delegation = rcu_dereference(NFS_I(inode)->delegation);
1125 delegation_flags = delegation->flags;
1127 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1128 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1129 "returning a delegation for "
1130 "OPEN(CLAIM_DELEGATE_CUR)\n",
1132 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1133 nfs_inode_set_delegation(state->inode,
1134 data->owner->so_cred,
1137 nfs_inode_reclaim_delegation(state->inode,
1138 data->owner->so_cred,
1142 update_open_stateid(state, &data->o_res.stateid, NULL,
1150 return ERR_PTR(ret);
1153 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1155 struct nfs_inode *nfsi = NFS_I(state->inode);
1156 struct nfs_open_context *ctx;
1158 spin_lock(&state->inode->i_lock);
1159 list_for_each_entry(ctx, &nfsi->open_files, list) {
1160 if (ctx->state != state)
1162 get_nfs_open_context(ctx);
1163 spin_unlock(&state->inode->i_lock);
1166 spin_unlock(&state->inode->i_lock);
1167 return ERR_PTR(-ENOENT);
1170 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1172 struct nfs4_opendata *opendata;
1174 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1175 if (opendata == NULL)
1176 return ERR_PTR(-ENOMEM);
1177 opendata->state = state;
1178 atomic_inc(&state->count);
1182 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1184 struct nfs4_state *newstate;
1187 opendata->o_arg.open_flags = 0;
1188 opendata->o_arg.fmode = fmode;
1189 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1190 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1191 nfs4_init_opendata_res(opendata);
1192 ret = _nfs4_recover_proc_open(opendata);
1195 newstate = nfs4_opendata_to_nfs4_state(opendata);
1196 if (IS_ERR(newstate))
1197 return PTR_ERR(newstate);
1198 nfs4_close_state(newstate, fmode);
1203 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1205 struct nfs4_state *newstate;
1208 /* memory barrier prior to reading state->n_* */
1209 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1211 if (state->n_rdwr != 0) {
1212 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1213 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1216 if (newstate != state)
1219 if (state->n_wronly != 0) {
1220 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1221 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1224 if (newstate != state)
1227 if (state->n_rdonly != 0) {
1228 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1229 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1232 if (newstate != state)
1236 * We may have performed cached opens for all three recoveries.
1237 * Check if we need to update the current stateid.
1239 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1240 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1241 write_seqlock(&state->seqlock);
1242 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1243 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1244 write_sequnlock(&state->seqlock);
1251 * reclaim state on the server after a reboot.
1253 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1255 struct nfs_delegation *delegation;
1256 struct nfs4_opendata *opendata;
1257 fmode_t delegation_type = 0;
1260 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1261 if (IS_ERR(opendata))
1262 return PTR_ERR(opendata);
1263 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1264 opendata->o_arg.fh = NFS_FH(state->inode);
1266 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1267 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1268 delegation_type = delegation->type;
1270 opendata->o_arg.u.delegation_type = delegation_type;
1271 status = nfs4_open_recover(opendata, state);
1272 nfs4_opendata_put(opendata);
1276 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1278 struct nfs_server *server = NFS_SERVER(state->inode);
1279 struct nfs4_exception exception = { };
1282 err = _nfs4_do_open_reclaim(ctx, state);
1283 if (err != -NFS4ERR_DELAY)
1285 nfs4_handle_exception(server, err, &exception);
1286 } while (exception.retry);
1290 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1292 struct nfs_open_context *ctx;
1295 ctx = nfs4_state_find_open_context(state);
1297 return PTR_ERR(ctx);
1298 ret = nfs4_do_open_reclaim(ctx, state);
1299 put_nfs_open_context(ctx);
1303 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1305 struct nfs4_opendata *opendata;
1308 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1309 if (IS_ERR(opendata))
1310 return PTR_ERR(opendata);
1311 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1312 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1313 ret = nfs4_open_recover(opendata, state);
1314 nfs4_opendata_put(opendata);
1318 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1320 struct nfs4_exception exception = { };
1321 struct nfs_server *server = NFS_SERVER(state->inode);
1324 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1330 case -NFS4ERR_BADSESSION:
1331 case -NFS4ERR_BADSLOT:
1332 case -NFS4ERR_BAD_HIGH_SLOT:
1333 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1334 case -NFS4ERR_DEADSESSION:
1335 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
1337 case -NFS4ERR_STALE_CLIENTID:
1338 case -NFS4ERR_STALE_STATEID:
1339 case -NFS4ERR_EXPIRED:
1340 /* Don't recall a delegation if it was lost */
1341 nfs4_schedule_lease_recovery(server->nfs_client);
1345 * The show must go on: exit, but mark the
1346 * stateid as needing recovery.
1348 case -NFS4ERR_DELEG_REVOKED:
1349 case -NFS4ERR_ADMIN_REVOKED:
1350 case -NFS4ERR_BAD_STATEID:
1351 nfs_inode_find_state_and_recover(state->inode,
1353 nfs4_schedule_stateid_recovery(server, state);
1356 * User RPCSEC_GSS context has expired.
1357 * We cannot recover this stateid now, so
1358 * skip it and allow recovery thread to
1365 err = nfs4_handle_exception(server, err, &exception);
1366 } while (exception.retry);
1371 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1373 struct nfs4_opendata *data = calldata;
1375 data->rpc_status = task->tk_status;
1376 if (data->rpc_status == 0) {
1377 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1378 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1379 renew_lease(data->o_res.server, data->timestamp);
1384 static void nfs4_open_confirm_release(void *calldata)
1386 struct nfs4_opendata *data = calldata;
1387 struct nfs4_state *state = NULL;
1389 /* If this request hasn't been cancelled, do nothing */
1390 if (data->cancelled == 0)
1392 /* In case of error, no cleanup! */
1393 if (!data->rpc_done)
1395 state = nfs4_opendata_to_nfs4_state(data);
1397 nfs4_close_state(state, data->o_arg.fmode);
1399 nfs4_opendata_put(data);
1402 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1403 .rpc_call_done = nfs4_open_confirm_done,
1404 .rpc_release = nfs4_open_confirm_release,
1408 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1410 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1412 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1413 struct rpc_task *task;
1414 struct rpc_message msg = {
1415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1416 .rpc_argp = &data->c_arg,
1417 .rpc_resp = &data->c_res,
1418 .rpc_cred = data->owner->so_cred,
1420 struct rpc_task_setup task_setup_data = {
1421 .rpc_client = server->client,
1422 .rpc_message = &msg,
1423 .callback_ops = &nfs4_open_confirm_ops,
1424 .callback_data = data,
1425 .workqueue = nfsiod_workqueue,
1426 .flags = RPC_TASK_ASYNC,
1430 kref_get(&data->kref);
1432 data->rpc_status = 0;
1433 data->timestamp = jiffies;
1434 task = rpc_run_task(&task_setup_data);
1436 return PTR_ERR(task);
1437 status = nfs4_wait_for_completion_rpc_task(task);
1439 data->cancelled = 1;
1442 status = data->rpc_status;
1447 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1449 struct nfs4_opendata *data = calldata;
1450 struct nfs4_state_owner *sp = data->owner;
1452 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1455 * Check if we still need to send an OPEN call, or if we can use
1456 * a delegation instead.
1458 if (data->state != NULL) {
1459 struct nfs_delegation *delegation;
1461 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1464 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1465 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1466 can_open_delegated(delegation, data->o_arg.fmode))
1467 goto unlock_no_action;
1470 /* Update client id. */
1471 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1472 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1473 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1474 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1476 data->timestamp = jiffies;
1477 if (nfs4_setup_sequence(data->o_arg.server,
1478 &data->o_arg.seq_args,
1479 &data->o_res.seq_res, task))
1481 rpc_call_start(task);
1486 task->tk_action = NULL;
1490 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1492 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1493 nfs4_open_prepare(task, calldata);
1496 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1498 struct nfs4_opendata *data = calldata;
1500 data->rpc_status = task->tk_status;
1502 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1505 if (task->tk_status == 0) {
1506 switch (data->o_res.f_attr->mode & S_IFMT) {
1510 data->rpc_status = -ELOOP;
1513 data->rpc_status = -EISDIR;
1516 data->rpc_status = -ENOTDIR;
1518 renew_lease(data->o_res.server, data->timestamp);
1519 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1520 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1525 static void nfs4_open_release(void *calldata)
1527 struct nfs4_opendata *data = calldata;
1528 struct nfs4_state *state = NULL;
1530 /* If this request hasn't been cancelled, do nothing */
1531 if (data->cancelled == 0)
1533 /* In case of error, no cleanup! */
1534 if (data->rpc_status != 0 || !data->rpc_done)
1536 /* In case we need an open_confirm, no cleanup! */
1537 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1539 state = nfs4_opendata_to_nfs4_state(data);
1541 nfs4_close_state(state, data->o_arg.fmode);
1543 nfs4_opendata_put(data);
1546 static const struct rpc_call_ops nfs4_open_ops = {
1547 .rpc_call_prepare = nfs4_open_prepare,
1548 .rpc_call_done = nfs4_open_done,
1549 .rpc_release = nfs4_open_release,
1552 static const struct rpc_call_ops nfs4_recover_open_ops = {
1553 .rpc_call_prepare = nfs4_recover_open_prepare,
1554 .rpc_call_done = nfs4_open_done,
1555 .rpc_release = nfs4_open_release,
1558 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1560 struct inode *dir = data->dir->d_inode;
1561 struct nfs_server *server = NFS_SERVER(dir);
1562 struct nfs_openargs *o_arg = &data->o_arg;
1563 struct nfs_openres *o_res = &data->o_res;
1564 struct rpc_task *task;
1565 struct rpc_message msg = {
1566 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1569 .rpc_cred = data->owner->so_cred,
1571 struct rpc_task_setup task_setup_data = {
1572 .rpc_client = server->client,
1573 .rpc_message = &msg,
1574 .callback_ops = &nfs4_open_ops,
1575 .callback_data = data,
1576 .workqueue = nfsiod_workqueue,
1577 .flags = RPC_TASK_ASYNC,
1581 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1582 kref_get(&data->kref);
1584 data->rpc_status = 0;
1585 data->cancelled = 0;
1587 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1588 task = rpc_run_task(&task_setup_data);
1590 return PTR_ERR(task);
1591 status = nfs4_wait_for_completion_rpc_task(task);
1593 data->cancelled = 1;
1596 status = data->rpc_status;
1602 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1604 struct inode *dir = data->dir->d_inode;
1605 struct nfs_openres *o_res = &data->o_res;
1608 status = nfs4_run_open_task(data, 1);
1609 if (status != 0 || !data->rpc_done)
1612 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1614 nfs_refresh_inode(dir, o_res->dir_attr);
1616 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1617 status = _nfs4_proc_open_confirm(data);
1626 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1628 static int _nfs4_proc_open(struct nfs4_opendata *data)
1630 struct inode *dir = data->dir->d_inode;
1631 struct nfs_server *server = NFS_SERVER(dir);
1632 struct nfs_openargs *o_arg = &data->o_arg;
1633 struct nfs_openres *o_res = &data->o_res;
1636 status = nfs4_run_open_task(data, 0);
1637 if (!data->rpc_done)
1640 if (status == -NFS4ERR_BADNAME &&
1641 !(o_arg->open_flags & O_CREAT))
1646 nfs_fattr_map_and_free_names(server, &data->f_attr);
1648 if (o_arg->open_flags & O_CREAT) {
1649 update_changeattr(dir, &o_res->cinfo);
1650 nfs_post_op_update_inode(dir, o_res->dir_attr);
1652 nfs_refresh_inode(dir, o_res->dir_attr);
1653 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1654 server->caps &= ~NFS_CAP_POSIX_LOCK;
1655 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1656 status = _nfs4_proc_open_confirm(data);
1660 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1661 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1665 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1670 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1671 ret = nfs4_wait_clnt_recover(clp);
1674 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1675 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1677 nfs4_schedule_state_manager(clp);
1683 static int nfs4_recover_expired_lease(struct nfs_server *server)
1685 return nfs4_client_recover_expired_lease(server->nfs_client);
1690 * reclaim state on the server after a network partition.
1691 * Assumes caller holds the appropriate lock
1693 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1695 struct nfs4_opendata *opendata;
1698 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1699 if (IS_ERR(opendata))
1700 return PTR_ERR(opendata);
1701 ret = nfs4_open_recover(opendata, state);
1703 d_drop(ctx->dentry);
1704 nfs4_opendata_put(opendata);
1708 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1710 struct nfs_server *server = NFS_SERVER(state->inode);
1711 struct nfs4_exception exception = { };
1715 err = _nfs4_open_expired(ctx, state);
1719 case -NFS4ERR_GRACE:
1720 case -NFS4ERR_DELAY:
1721 nfs4_handle_exception(server, err, &exception);
1724 } while (exception.retry);
1729 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1731 struct nfs_open_context *ctx;
1734 ctx = nfs4_state_find_open_context(state);
1736 return PTR_ERR(ctx);
1737 ret = nfs4_do_open_expired(ctx, state);
1738 put_nfs_open_context(ctx);
1742 #if defined(CONFIG_NFS_V4_1)
1743 static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
1745 int status = NFS_OK;
1746 struct nfs_server *server = NFS_SERVER(state->inode);
1748 if (state->flags & flags) {
1749 status = nfs41_test_stateid(server, stateid);
1750 if (status != NFS_OK) {
1751 nfs41_free_stateid(server, stateid);
1752 state->flags &= ~flags;
1758 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1760 int deleg_status, open_status;
1761 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1762 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1764 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1765 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1767 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1769 return nfs4_open_expired(sp, state);
1774 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1775 * fields corresponding to attributes that were used to store the verifier.
1776 * Make sure we clobber those fields in the later setattr call
1778 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1780 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1781 !(sattr->ia_valid & ATTR_ATIME_SET))
1782 sattr->ia_valid |= ATTR_ATIME;
1784 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1785 !(sattr->ia_valid & ATTR_MTIME_SET))
1786 sattr->ia_valid |= ATTR_MTIME;
1790 * Returns a referenced nfs4_state
1792 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)
1794 struct nfs4_state_owner *sp;
1795 struct nfs4_state *state = NULL;
1796 struct nfs_server *server = NFS_SERVER(dir);
1797 struct nfs4_opendata *opendata;
1800 /* Protect against reboot recovery conflicts */
1802 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1804 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1807 status = nfs4_recover_expired_lease(server);
1809 goto err_put_state_owner;
1810 if (dentry->d_inode != NULL)
1811 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1813 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1814 if (opendata == NULL)
1815 goto err_put_state_owner;
1817 if (dentry->d_inode != NULL)
1818 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1820 status = _nfs4_proc_open(opendata);
1822 goto err_opendata_put;
1824 state = nfs4_opendata_to_nfs4_state(opendata);
1825 status = PTR_ERR(state);
1827 goto err_opendata_put;
1828 if (server->caps & NFS_CAP_POSIX_LOCK)
1829 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1831 if (opendata->o_arg.open_flags & O_EXCL) {
1832 nfs4_exclusive_attrset(opendata, sattr);
1834 nfs_fattr_init(opendata->o_res.f_attr);
1835 status = nfs4_do_setattr(state->inode, cred,
1836 opendata->o_res.f_attr, sattr,
1839 nfs_setattr_update_inode(state->inode, sattr);
1840 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1842 nfs4_opendata_put(opendata);
1843 nfs4_put_state_owner(sp);
1847 nfs4_opendata_put(opendata);
1848 err_put_state_owner:
1849 nfs4_put_state_owner(sp);
1856 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)
1858 struct nfs4_exception exception = { };
1859 struct nfs4_state *res;
1863 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
1866 /* NOTE: BAD_SEQID means the server and client disagree about the
1867 * book-keeping w.r.t. state-changing operations
1868 * (OPEN/CLOSE/LOCK/LOCKU...)
1869 * It is actually a sign of a bug on the client or on the server.
1871 * If we receive a BAD_SEQID error in the particular case of
1872 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1873 * have unhashed the old state_owner for us, and that we can
1874 * therefore safely retry using a new one. We should still warn
1875 * the user though...
1877 if (status == -NFS4ERR_BAD_SEQID) {
1878 pr_warn_ratelimited("NFS: v4 server %s "
1879 " returned a bad sequence-id error!\n",
1880 NFS_SERVER(dir)->nfs_client->cl_hostname);
1881 exception.retry = 1;
1885 * BAD_STATEID on OPEN means that the server cancelled our
1886 * state before it received the OPEN_CONFIRM.
1887 * Recover by retrying the request as per the discussion
1888 * on Page 181 of RFC3530.
1890 if (status == -NFS4ERR_BAD_STATEID) {
1891 exception.retry = 1;
1894 if (status == -EAGAIN) {
1895 /* We must have found a delegation */
1896 exception.retry = 1;
1899 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1900 status, &exception));
1901 } while (exception.retry);
1905 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1906 struct nfs_fattr *fattr, struct iattr *sattr,
1907 struct nfs4_state *state)
1909 struct nfs_server *server = NFS_SERVER(inode);
1910 struct nfs_setattrargs arg = {
1911 .fh = NFS_FH(inode),
1914 .bitmask = server->attr_bitmask,
1916 struct nfs_setattrres res = {
1920 struct rpc_message msg = {
1921 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1926 unsigned long timestamp = jiffies;
1929 nfs_fattr_init(fattr);
1931 if (state != NULL) {
1932 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1933 current->files, current->tgid);
1934 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1936 /* Use that stateid */
1938 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
1940 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1941 if (status == 0 && state != NULL)
1942 renew_lease(server, timestamp);
1946 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1947 struct nfs_fattr *fattr, struct iattr *sattr,
1948 struct nfs4_state *state)
1950 struct nfs_server *server = NFS_SERVER(inode);
1951 struct nfs4_exception exception = {
1957 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
1959 case -NFS4ERR_OPENMODE:
1960 if (state && !(state->state & FMODE_WRITE)) {
1962 if (sattr->ia_valid & ATTR_OPEN)
1967 err = nfs4_handle_exception(server, err, &exception);
1968 } while (exception.retry);
1973 struct nfs4_closedata {
1974 struct inode *inode;
1975 struct nfs4_state *state;
1976 struct nfs_closeargs arg;
1977 struct nfs_closeres res;
1978 struct nfs_fattr fattr;
1979 unsigned long timestamp;
1984 static void nfs4_free_closedata(void *data)
1986 struct nfs4_closedata *calldata = data;
1987 struct nfs4_state_owner *sp = calldata->state->owner;
1988 struct super_block *sb = calldata->state->inode->i_sb;
1991 pnfs_roc_release(calldata->state->inode);
1992 nfs4_put_open_state(calldata->state);
1993 nfs_free_seqid(calldata->arg.seqid);
1994 nfs4_put_state_owner(sp);
1995 nfs_sb_deactive(sb);
1999 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2002 spin_lock(&state->owner->so_lock);
2003 if (!(fmode & FMODE_READ))
2004 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2005 if (!(fmode & FMODE_WRITE))
2006 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2007 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2008 spin_unlock(&state->owner->so_lock);
2011 static void nfs4_close_done(struct rpc_task *task, void *data)
2013 struct nfs4_closedata *calldata = data;
2014 struct nfs4_state *state = calldata->state;
2015 struct nfs_server *server = NFS_SERVER(calldata->inode);
2017 dprintk("%s: begin!\n", __func__);
2018 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2020 /* hmm. we are done with the inode, and in the process of freeing
2021 * the state_owner. we keep this around to process errors
2023 switch (task->tk_status) {
2026 pnfs_roc_set_barrier(state->inode,
2027 calldata->roc_barrier);
2028 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2029 renew_lease(server, calldata->timestamp);
2030 nfs4_close_clear_stateid_flags(state,
2031 calldata->arg.fmode);
2033 case -NFS4ERR_STALE_STATEID:
2034 case -NFS4ERR_OLD_STATEID:
2035 case -NFS4ERR_BAD_STATEID:
2036 case -NFS4ERR_EXPIRED:
2037 if (calldata->arg.fmode == 0)
2040 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2041 rpc_restart_call_prepare(task);
2043 nfs_release_seqid(calldata->arg.seqid);
2044 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2045 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2048 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2050 struct nfs4_closedata *calldata = data;
2051 struct nfs4_state *state = calldata->state;
2054 dprintk("%s: begin!\n", __func__);
2055 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2058 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2059 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2060 spin_lock(&state->owner->so_lock);
2061 /* Calculate the change in open mode */
2062 if (state->n_rdwr == 0) {
2063 if (state->n_rdonly == 0) {
2064 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2065 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2066 calldata->arg.fmode &= ~FMODE_READ;
2068 if (state->n_wronly == 0) {
2069 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2070 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2071 calldata->arg.fmode &= ~FMODE_WRITE;
2074 spin_unlock(&state->owner->so_lock);
2077 /* Note: exit _without_ calling nfs4_close_done */
2078 task->tk_action = NULL;
2082 if (calldata->arg.fmode == 0) {
2083 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2084 if (calldata->roc &&
2085 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2086 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2092 nfs_fattr_init(calldata->res.fattr);
2093 calldata->timestamp = jiffies;
2094 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
2095 &calldata->arg.seq_args,
2096 &calldata->res.seq_res,
2099 rpc_call_start(task);
2101 dprintk("%s: done!\n", __func__);
2104 static const struct rpc_call_ops nfs4_close_ops = {
2105 .rpc_call_prepare = nfs4_close_prepare,
2106 .rpc_call_done = nfs4_close_done,
2107 .rpc_release = nfs4_free_closedata,
2111 * It is possible for data to be read/written from a mem-mapped file
2112 * after the sys_close call (which hits the vfs layer as a flush).
2113 * This means that we can't safely call nfsv4 close on a file until
2114 * the inode is cleared. This in turn means that we are not good
2115 * NFSv4 citizens - we do not indicate to the server to update the file's
2116 * share state even when we are done with one of the three share
2117 * stateid's in the inode.
2119 * NOTE: Caller must be holding the sp->so_owner semaphore!
2121 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
2123 struct nfs_server *server = NFS_SERVER(state->inode);
2124 struct nfs4_closedata *calldata;
2125 struct nfs4_state_owner *sp = state->owner;
2126 struct rpc_task *task;
2127 struct rpc_message msg = {
2128 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2129 .rpc_cred = state->owner->so_cred,
2131 struct rpc_task_setup task_setup_data = {
2132 .rpc_client = server->client,
2133 .rpc_message = &msg,
2134 .callback_ops = &nfs4_close_ops,
2135 .workqueue = nfsiod_workqueue,
2136 .flags = RPC_TASK_ASYNC,
2138 int status = -ENOMEM;
2140 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2141 if (calldata == NULL)
2143 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2144 calldata->inode = state->inode;
2145 calldata->state = state;
2146 calldata->arg.fh = NFS_FH(state->inode);
2147 calldata->arg.stateid = &state->open_stateid;
2148 /* Serialization for the sequence id */
2149 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2150 if (calldata->arg.seqid == NULL)
2151 goto out_free_calldata;
2152 calldata->arg.fmode = 0;
2153 calldata->arg.bitmask = server->cache_consistency_bitmask;
2154 calldata->res.fattr = &calldata->fattr;
2155 calldata->res.seqid = calldata->arg.seqid;
2156 calldata->res.server = server;
2157 calldata->roc = roc;
2158 nfs_sb_active(calldata->inode->i_sb);
2160 msg.rpc_argp = &calldata->arg;
2161 msg.rpc_resp = &calldata->res;
2162 task_setup_data.callback_data = calldata;
2163 task = rpc_run_task(&task_setup_data);
2165 return PTR_ERR(task);
2168 status = rpc_wait_for_completion_task(task);
2175 pnfs_roc_release(state->inode);
2176 nfs4_put_open_state(state);
2177 nfs4_put_state_owner(sp);
2181 static struct inode *
2182 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2184 struct nfs4_state *state;
2186 /* Protect against concurrent sillydeletes */
2187 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
2189 return ERR_CAST(state);
2191 return igrab(state->inode);
2194 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2196 if (ctx->state == NULL)
2199 nfs4_close_sync(ctx->state, ctx->mode);
2201 nfs4_close_state(ctx->state, ctx->mode);
2204 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2206 struct nfs4_server_caps_arg args = {
2209 struct nfs4_server_caps_res res = {};
2210 struct rpc_message msg = {
2211 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2217 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2219 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2220 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2221 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2222 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2223 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2224 NFS_CAP_CTIME|NFS_CAP_MTIME);
2225 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2226 server->caps |= NFS_CAP_ACLS;
2227 if (res.has_links != 0)
2228 server->caps |= NFS_CAP_HARDLINKS;
2229 if (res.has_symlinks != 0)
2230 server->caps |= NFS_CAP_SYMLINKS;
2231 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2232 server->caps |= NFS_CAP_FILEID;
2233 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2234 server->caps |= NFS_CAP_MODE;
2235 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2236 server->caps |= NFS_CAP_NLINK;
2237 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2238 server->caps |= NFS_CAP_OWNER;
2239 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2240 server->caps |= NFS_CAP_OWNER_GROUP;
2241 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2242 server->caps |= NFS_CAP_ATIME;
2243 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2244 server->caps |= NFS_CAP_CTIME;
2245 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2246 server->caps |= NFS_CAP_MTIME;
2248 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2249 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2250 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2251 server->acl_bitmask = res.acl_bitmask;
2252 server->fh_expire_type = res.fh_expire_type;
2258 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2260 struct nfs4_exception exception = { };
2263 err = nfs4_handle_exception(server,
2264 _nfs4_server_capabilities(server, fhandle),
2266 } while (exception.retry);
2270 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2271 struct nfs_fsinfo *info)
2273 struct nfs4_lookup_root_arg args = {
2274 .bitmask = nfs4_fattr_bitmap,
2276 struct nfs4_lookup_res res = {
2278 .fattr = info->fattr,
2281 struct rpc_message msg = {
2282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2287 nfs_fattr_init(info->fattr);
2288 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2291 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2292 struct nfs_fsinfo *info)
2294 struct nfs4_exception exception = { };
2297 err = _nfs4_lookup_root(server, fhandle, info);
2300 case -NFS4ERR_WRONGSEC:
2303 err = nfs4_handle_exception(server, err, &exception);
2305 } while (exception.retry);
2310 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2311 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2313 struct rpc_auth *auth;
2316 auth = rpcauth_create(flavor, server->client);
2321 ret = nfs4_lookup_root(server, fhandle, info);
2326 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2327 struct nfs_fsinfo *info)
2329 int i, len, status = 0;
2330 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2332 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2333 flav_array[len] = RPC_AUTH_NULL;
2336 for (i = 0; i < len; i++) {
2337 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2338 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2343 * -EACCESS could mean that the user doesn't have correct permissions
2344 * to access the mount. It could also mean that we tried to mount
2345 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2346 * existing mount programs don't handle -EACCES very well so it should
2347 * be mapped to -EPERM instead.
2349 if (status == -EACCES)
2355 * get the file handle for the "/" directory on the server
2357 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2358 struct nfs_fsinfo *info)
2360 int minor_version = server->nfs_client->cl_minorversion;
2361 int status = nfs4_lookup_root(server, fhandle, info);
2362 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2364 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2365 * by nfs4_map_errors() as this function exits.
2367 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2369 status = nfs4_server_capabilities(server, fhandle);
2371 status = nfs4_do_fsinfo(server, fhandle, info);
2372 return nfs4_map_errors(status);
2376 * Get locations and (maybe) other attributes of a referral.
2377 * Note that we'll actually follow the referral later when
2378 * we detect fsid mismatch in inode revalidation
2380 static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2381 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2383 int status = -ENOMEM;
2384 struct page *page = NULL;
2385 struct nfs4_fs_locations *locations = NULL;
2387 page = alloc_page(GFP_KERNEL);
2390 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2391 if (locations == NULL)
2394 status = nfs4_proc_fs_locations(dir, name, locations, page);
2397 /* Make sure server returned a different fsid for the referral */
2398 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2399 dprintk("%s: server did not return a different fsid for"
2400 " a referral at %s\n", __func__, name->name);
2404 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2405 nfs_fixup_referral_attributes(&locations->fattr);
2407 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2408 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2409 memset(fhandle, 0, sizeof(struct nfs_fh));
2417 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2419 struct nfs4_getattr_arg args = {
2421 .bitmask = server->attr_bitmask,
2423 struct nfs4_getattr_res res = {
2427 struct rpc_message msg = {
2428 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2433 nfs_fattr_init(fattr);
2434 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2437 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2439 struct nfs4_exception exception = { };
2442 err = nfs4_handle_exception(server,
2443 _nfs4_proc_getattr(server, fhandle, fattr),
2445 } while (exception.retry);
2450 * The file is not closed if it is opened due to the a request to change
2451 * the size of the file. The open call will not be needed once the
2452 * VFS layer lookup-intents are implemented.
2454 * Close is called when the inode is destroyed.
2455 * If we haven't opened the file for O_WRONLY, we
2456 * need to in the size_change case to obtain a stateid.
2459 * Because OPEN is always done by name in nfsv4, it is
2460 * possible that we opened a different file by the same
2461 * name. We can recognize this race condition, but we
2462 * can't do anything about it besides returning an error.
2464 * This will be fixed with VFS changes (lookup-intent).
2467 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2468 struct iattr *sattr)
2470 struct inode *inode = dentry->d_inode;
2471 struct rpc_cred *cred = NULL;
2472 struct nfs4_state *state = NULL;
2475 if (pnfs_ld_layoutret_on_setattr(inode))
2476 pnfs_return_layout(inode);
2478 nfs_fattr_init(fattr);
2480 /* Search for an existing open(O_WRITE) file */
2481 if (sattr->ia_valid & ATTR_FILE) {
2482 struct nfs_open_context *ctx;
2484 ctx = nfs_file_open_context(sattr->ia_file);
2491 /* Deal with open(O_TRUNC) */
2492 if (sattr->ia_valid & ATTR_OPEN)
2493 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2495 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2497 nfs_setattr_update_inode(inode, sattr);
2501 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2502 const struct qstr *name, struct nfs_fh *fhandle,
2503 struct nfs_fattr *fattr)
2505 struct nfs_server *server = NFS_SERVER(dir);
2507 struct nfs4_lookup_arg args = {
2508 .bitmask = server->attr_bitmask,
2509 .dir_fh = NFS_FH(dir),
2512 struct nfs4_lookup_res res = {
2517 struct rpc_message msg = {
2518 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2523 nfs_fattr_init(fattr);
2525 dprintk("NFS call lookup %s\n", name->name);
2526 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2527 dprintk("NFS reply lookup: %d\n", status);
2531 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2533 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2534 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2535 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2539 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2540 struct qstr *name, struct nfs_fh *fhandle,
2541 struct nfs_fattr *fattr)
2543 struct nfs4_exception exception = { };
2544 struct rpc_clnt *client = *clnt;
2547 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2549 case -NFS4ERR_BADNAME:
2552 case -NFS4ERR_MOVED:
2553 err = nfs4_get_referral(dir, name, fattr, fhandle);
2555 case -NFS4ERR_WRONGSEC:
2557 if (client != *clnt)
2560 client = nfs4_create_sec_client(client, dir, name);
2562 return PTR_ERR(client);
2564 exception.retry = 1;
2567 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2569 } while (exception.retry);
2574 else if (client != *clnt)
2575 rpc_shutdown_client(client);
2580 static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2581 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2584 struct rpc_clnt *client = NFS_CLIENT(dir);
2586 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2587 if (client != NFS_CLIENT(dir)) {
2588 rpc_shutdown_client(client);
2589 nfs_fixup_secinfo_attributes(fattr);
2594 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2596 struct nfs_server *server = NFS_SERVER(inode);
2597 struct nfs4_accessargs args = {
2598 .fh = NFS_FH(inode),
2599 .bitmask = server->cache_consistency_bitmask,
2601 struct nfs4_accessres res = {
2604 struct rpc_message msg = {
2605 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2608 .rpc_cred = entry->cred,
2610 int mode = entry->mask;
2614 * Determine which access bits we want to ask for...
2616 if (mode & MAY_READ)
2617 args.access |= NFS4_ACCESS_READ;
2618 if (S_ISDIR(inode->i_mode)) {
2619 if (mode & MAY_WRITE)
2620 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2621 if (mode & MAY_EXEC)
2622 args.access |= NFS4_ACCESS_LOOKUP;
2624 if (mode & MAY_WRITE)
2625 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2626 if (mode & MAY_EXEC)
2627 args.access |= NFS4_ACCESS_EXECUTE;
2630 res.fattr = nfs_alloc_fattr();
2631 if (res.fattr == NULL)
2634 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2637 if (res.access & NFS4_ACCESS_READ)
2638 entry->mask |= MAY_READ;
2639 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2640 entry->mask |= MAY_WRITE;
2641 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2642 entry->mask |= MAY_EXEC;
2643 nfs_refresh_inode(inode, res.fattr);
2645 nfs_free_fattr(res.fattr);
2649 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2651 struct nfs4_exception exception = { };
2654 err = nfs4_handle_exception(NFS_SERVER(inode),
2655 _nfs4_proc_access(inode, entry),
2657 } while (exception.retry);
2662 * TODO: For the time being, we don't try to get any attributes
2663 * along with any of the zero-copy operations READ, READDIR,
2666 * In the case of the first three, we want to put the GETATTR
2667 * after the read-type operation -- this is because it is hard
2668 * to predict the length of a GETATTR response in v4, and thus
2669 * align the READ data correctly. This means that the GETATTR
2670 * may end up partially falling into the page cache, and we should
2671 * shift it into the 'tail' of the xdr_buf before processing.
2672 * To do this efficiently, we need to know the total length
2673 * of data received, which doesn't seem to be available outside
2676 * In the case of WRITE, we also want to put the GETATTR after
2677 * the operation -- in this case because we want to make sure
2678 * we get the post-operation mtime and size. This means that
2679 * we can't use xdr_encode_pages() as written: we need a variant
2680 * of it which would leave room in the 'tail' iovec.
2682 * Both of these changes to the XDR layer would in fact be quite
2683 * minor, but I decided to leave them for a subsequent patch.
2685 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2686 unsigned int pgbase, unsigned int pglen)
2688 struct nfs4_readlink args = {
2689 .fh = NFS_FH(inode),
2694 struct nfs4_readlink_res res;
2695 struct rpc_message msg = {
2696 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2701 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2704 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2705 unsigned int pgbase, unsigned int pglen)
2707 struct nfs4_exception exception = { };
2710 err = nfs4_handle_exception(NFS_SERVER(inode),
2711 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2713 } while (exception.retry);
2719 * We will need to arrange for the VFS layer to provide an atomic open.
2720 * Until then, this create/open method is prone to inefficiency and race
2721 * conditions due to the lookup, create, and open VFS calls from sys_open()
2722 * placed on the wire.
2724 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2725 * The file will be opened again in the subsequent VFS open call
2726 * (nfs4_proc_file_open).
2728 * The open for read will just hang around to be used by any process that
2729 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2733 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2734 int flags, struct nfs_open_context *ctx)
2736 struct dentry *de = dentry;
2737 struct nfs4_state *state;
2738 struct rpc_cred *cred = NULL;
2747 sattr->ia_mode &= ~current_umask();
2748 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
2750 if (IS_ERR(state)) {
2751 status = PTR_ERR(state);
2754 d_add(dentry, igrab(state->inode));
2755 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2759 nfs4_close_sync(state, fmode);
2764 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2766 struct nfs_server *server = NFS_SERVER(dir);
2767 struct nfs_removeargs args = {
2769 .name.len = name->len,
2770 .name.name = name->name,
2771 .bitmask = server->attr_bitmask,
2773 struct nfs_removeres res = {
2776 struct rpc_message msg = {
2777 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2781 int status = -ENOMEM;
2783 res.dir_attr = nfs_alloc_fattr();
2784 if (res.dir_attr == NULL)
2787 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2789 update_changeattr(dir, &res.cinfo);
2790 nfs_post_op_update_inode(dir, res.dir_attr);
2792 nfs_free_fattr(res.dir_attr);
2797 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2799 struct nfs4_exception exception = { };
2802 err = nfs4_handle_exception(NFS_SERVER(dir),
2803 _nfs4_proc_remove(dir, name),
2805 } while (exception.retry);
2809 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2811 struct nfs_server *server = NFS_SERVER(dir);
2812 struct nfs_removeargs *args = msg->rpc_argp;
2813 struct nfs_removeres *res = msg->rpc_resp;
2815 args->bitmask = server->cache_consistency_bitmask;
2816 res->server = server;
2817 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2818 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
2821 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2823 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2824 &data->args.seq_args,
2828 rpc_call_start(task);
2831 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2833 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2835 if (!nfs4_sequence_done(task, &res->seq_res))
2837 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2839 update_changeattr(dir, &res->cinfo);
2840 nfs_post_op_update_inode(dir, res->dir_attr);
2844 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2846 struct nfs_server *server = NFS_SERVER(dir);
2847 struct nfs_renameargs *arg = msg->rpc_argp;
2848 struct nfs_renameres *res = msg->rpc_resp;
2850 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2851 arg->bitmask = server->attr_bitmask;
2852 res->server = server;
2853 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
2856 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2858 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2859 &data->args.seq_args,
2863 rpc_call_start(task);
2866 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2867 struct inode *new_dir)
2869 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2871 if (!nfs4_sequence_done(task, &res->seq_res))
2873 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2876 update_changeattr(old_dir, &res->old_cinfo);
2877 nfs_post_op_update_inode(old_dir, res->old_fattr);
2878 update_changeattr(new_dir, &res->new_cinfo);
2879 nfs_post_op_update_inode(new_dir, res->new_fattr);
2883 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2884 struct inode *new_dir, struct qstr *new_name)
2886 struct nfs_server *server = NFS_SERVER(old_dir);
2887 struct nfs_renameargs arg = {
2888 .old_dir = NFS_FH(old_dir),
2889 .new_dir = NFS_FH(new_dir),
2890 .old_name = old_name,
2891 .new_name = new_name,
2892 .bitmask = server->attr_bitmask,
2894 struct nfs_renameres res = {
2897 struct rpc_message msg = {
2898 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2902 int status = -ENOMEM;
2904 res.old_fattr = nfs_alloc_fattr();
2905 res.new_fattr = nfs_alloc_fattr();
2906 if (res.old_fattr == NULL || res.new_fattr == NULL)
2909 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2911 update_changeattr(old_dir, &res.old_cinfo);
2912 nfs_post_op_update_inode(old_dir, res.old_fattr);
2913 update_changeattr(new_dir, &res.new_cinfo);
2914 nfs_post_op_update_inode(new_dir, res.new_fattr);
2917 nfs_free_fattr(res.new_fattr);
2918 nfs_free_fattr(res.old_fattr);
2922 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2923 struct inode *new_dir, struct qstr *new_name)
2925 struct nfs4_exception exception = { };
2928 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2929 _nfs4_proc_rename(old_dir, old_name,
2932 } while (exception.retry);
2936 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2938 struct nfs_server *server = NFS_SERVER(inode);
2939 struct nfs4_link_arg arg = {
2940 .fh = NFS_FH(inode),
2941 .dir_fh = NFS_FH(dir),
2943 .bitmask = server->attr_bitmask,
2945 struct nfs4_link_res res = {
2948 struct rpc_message msg = {
2949 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2953 int status = -ENOMEM;
2955 res.fattr = nfs_alloc_fattr();
2956 res.dir_attr = nfs_alloc_fattr();
2957 if (res.fattr == NULL || res.dir_attr == NULL)
2960 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2962 update_changeattr(dir, &res.cinfo);
2963 nfs_post_op_update_inode(dir, res.dir_attr);
2964 nfs_post_op_update_inode(inode, res.fattr);
2967 nfs_free_fattr(res.dir_attr);
2968 nfs_free_fattr(res.fattr);
2972 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2974 struct nfs4_exception exception = { };
2977 err = nfs4_handle_exception(NFS_SERVER(inode),
2978 _nfs4_proc_link(inode, dir, name),
2980 } while (exception.retry);
2984 struct nfs4_createdata {
2985 struct rpc_message msg;
2986 struct nfs4_create_arg arg;
2987 struct nfs4_create_res res;
2989 struct nfs_fattr fattr;
2990 struct nfs_fattr dir_fattr;
2993 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2994 struct qstr *name, struct iattr *sattr, u32 ftype)
2996 struct nfs4_createdata *data;
2998 data = kzalloc(sizeof(*data), GFP_KERNEL);
3000 struct nfs_server *server = NFS_SERVER(dir);
3002 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3003 data->msg.rpc_argp = &data->arg;
3004 data->msg.rpc_resp = &data->res;
3005 data->arg.dir_fh = NFS_FH(dir);
3006 data->arg.server = server;
3007 data->arg.name = name;
3008 data->arg.attrs = sattr;
3009 data->arg.ftype = ftype;
3010 data->arg.bitmask = server->attr_bitmask;
3011 data->res.server = server;
3012 data->res.fh = &data->fh;
3013 data->res.fattr = &data->fattr;
3014 data->res.dir_fattr = &data->dir_fattr;
3015 nfs_fattr_init(data->res.fattr);
3016 nfs_fattr_init(data->res.dir_fattr);
3021 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3023 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3024 &data->arg.seq_args, &data->res.seq_res, 1);
3026 update_changeattr(dir, &data->res.dir_cinfo);
3027 nfs_post_op_update_inode(dir, data->res.dir_fattr);
3028 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3033 static void nfs4_free_createdata(struct nfs4_createdata *data)
3038 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3039 struct page *page, unsigned int len, struct iattr *sattr)
3041 struct nfs4_createdata *data;
3042 int status = -ENAMETOOLONG;
3044 if (len > NFS4_MAXPATHLEN)
3048 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3052 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3053 data->arg.u.symlink.pages = &page;
3054 data->arg.u.symlink.len = len;
3056 status = nfs4_do_create(dir, dentry, data);
3058 nfs4_free_createdata(data);
3063 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3064 struct page *page, unsigned int len, struct iattr *sattr)
3066 struct nfs4_exception exception = { };
3069 err = nfs4_handle_exception(NFS_SERVER(dir),
3070 _nfs4_proc_symlink(dir, dentry, page,
3073 } while (exception.retry);
3077 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3078 struct iattr *sattr)
3080 struct nfs4_createdata *data;
3081 int status = -ENOMEM;
3083 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3087 status = nfs4_do_create(dir, dentry, data);
3089 nfs4_free_createdata(data);
3094 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3095 struct iattr *sattr)
3097 struct nfs4_exception exception = { };
3100 sattr->ia_mode &= ~current_umask();
3102 err = nfs4_handle_exception(NFS_SERVER(dir),
3103 _nfs4_proc_mkdir(dir, dentry, sattr),
3105 } while (exception.retry);
3109 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3110 u64 cookie, struct page **pages, unsigned int count, int plus)
3112 struct inode *dir = dentry->d_inode;
3113 struct nfs4_readdir_arg args = {
3118 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3121 struct nfs4_readdir_res res;
3122 struct rpc_message msg = {
3123 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3130 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3131 dentry->d_parent->d_name.name,
3132 dentry->d_name.name,
3133 (unsigned long long)cookie);
3134 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3135 res.pgbase = args.pgbase;
3136 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3138 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
3139 status += args.pgbase;
3142 nfs_invalidate_atime(dir);
3144 dprintk("%s: returns %d\n", __func__, status);
3148 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3149 u64 cookie, struct page **pages, unsigned int count, int plus)
3151 struct nfs4_exception exception = { };
3154 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3155 _nfs4_proc_readdir(dentry, cred, cookie,
3156 pages, count, plus),
3158 } while (exception.retry);
3162 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3163 struct iattr *sattr, dev_t rdev)
3165 struct nfs4_createdata *data;
3166 int mode = sattr->ia_mode;
3167 int status = -ENOMEM;
3169 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3170 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3172 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3177 data->arg.ftype = NF4FIFO;
3178 else if (S_ISBLK(mode)) {
3179 data->arg.ftype = NF4BLK;
3180 data->arg.u.device.specdata1 = MAJOR(rdev);
3181 data->arg.u.device.specdata2 = MINOR(rdev);
3183 else if (S_ISCHR(mode)) {
3184 data->arg.ftype = NF4CHR;
3185 data->arg.u.device.specdata1 = MAJOR(rdev);
3186 data->arg.u.device.specdata2 = MINOR(rdev);
3189 status = nfs4_do_create(dir, dentry, data);
3191 nfs4_free_createdata(data);
3196 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3197 struct iattr *sattr, dev_t rdev)
3199 struct nfs4_exception exception = { };
3202 sattr->ia_mode &= ~current_umask();
3204 err = nfs4_handle_exception(NFS_SERVER(dir),
3205 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3207 } while (exception.retry);
3211 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3212 struct nfs_fsstat *fsstat)
3214 struct nfs4_statfs_arg args = {
3216 .bitmask = server->attr_bitmask,
3218 struct nfs4_statfs_res res = {
3221 struct rpc_message msg = {
3222 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3227 nfs_fattr_init(fsstat->fattr);
3228 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3231 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3233 struct nfs4_exception exception = { };
3236 err = nfs4_handle_exception(server,
3237 _nfs4_proc_statfs(server, fhandle, fsstat),
3239 } while (exception.retry);
3243 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3244 struct nfs_fsinfo *fsinfo)
3246 struct nfs4_fsinfo_arg args = {
3248 .bitmask = server->attr_bitmask,
3250 struct nfs4_fsinfo_res res = {
3253 struct rpc_message msg = {
3254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3259 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3262 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3264 struct nfs4_exception exception = { };
3268 err = nfs4_handle_exception(server,
3269 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3271 } while (exception.retry);
3275 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3277 nfs_fattr_init(fsinfo->fattr);
3278 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3281 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3282 struct nfs_pathconf *pathconf)
3284 struct nfs4_pathconf_arg args = {
3286 .bitmask = server->attr_bitmask,
3288 struct nfs4_pathconf_res res = {
3289 .pathconf = pathconf,
3291 struct rpc_message msg = {
3292 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3297 /* None of the pathconf attributes are mandatory to implement */
3298 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3299 memset(pathconf, 0, sizeof(*pathconf));
3303 nfs_fattr_init(pathconf->fattr);
3304 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3307 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3308 struct nfs_pathconf *pathconf)
3310 struct nfs4_exception exception = { };
3314 err = nfs4_handle_exception(server,
3315 _nfs4_proc_pathconf(server, fhandle, pathconf),
3317 } while (exception.retry);
3321 void __nfs4_read_done_cb(struct nfs_read_data *data)
3323 nfs_invalidate_atime(data->inode);
3326 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3328 struct nfs_server *server = NFS_SERVER(data->inode);
3330 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3331 rpc_restart_call_prepare(task);
3335 __nfs4_read_done_cb(data);
3336 if (task->tk_status > 0)
3337 renew_lease(server, data->timestamp);
3341 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3344 dprintk("--> %s\n", __func__);
3346 if (!nfs4_sequence_done(task, &data->res.seq_res))
3349 return data->read_done_cb ? data->read_done_cb(task, data) :
3350 nfs4_read_done_cb(task, data);
3353 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3355 data->timestamp = jiffies;
3356 data->read_done_cb = nfs4_read_done_cb;
3357 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3358 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3361 static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3363 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3364 &data->args.seq_args,
3368 rpc_call_start(task);
3371 /* Reset the the nfs_read_data to send the read to the MDS. */
3372 void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3374 dprintk("%s Reset task for i/o through\n", __func__);
3375 put_lseg(data->lseg);
3377 /* offsets will differ in the dense stripe case */
3378 data->args.offset = data->mds_offset;
3379 data->ds_clp = NULL;
3380 data->args.fh = NFS_FH(data->inode);
3381 data->read_done_cb = nfs4_read_done_cb;
3382 task->tk_ops = data->mds_ops;
3383 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3385 EXPORT_SYMBOL_GPL(nfs4_reset_read);
3387 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3389 struct inode *inode = data->inode;
3391 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3392 rpc_restart_call_prepare(task);
3395 if (task->tk_status >= 0) {
3396 renew_lease(NFS_SERVER(inode), data->timestamp);
3397 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3402 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3404 if (!nfs4_sequence_done(task, &data->res.seq_res))
3406 return data->write_done_cb ? data->write_done_cb(task, data) :
3407 nfs4_write_done_cb(task, data);
3410 /* Reset the the nfs_write_data to send the write to the MDS. */
3411 void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3413 dprintk("%s Reset task for i/o through\n", __func__);
3414 put_lseg(data->lseg);
3416 data->ds_clp = NULL;
3417 data->write_done_cb = nfs4_write_done_cb;
3418 data->args.fh = NFS_FH(data->inode);
3419 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3420 data->args.offset = data->mds_offset;
3421 data->res.fattr = &data->fattr;
3422 task->tk_ops = data->mds_ops;
3423 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3425 EXPORT_SYMBOL_GPL(nfs4_reset_write);
3427 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3429 struct nfs_server *server = NFS_SERVER(data->inode);
3432 data->args.bitmask = NULL;
3433 data->res.fattr = NULL;
3435 data->args.bitmask = server->cache_consistency_bitmask;
3436 if (!data->write_done_cb)
3437 data->write_done_cb = nfs4_write_done_cb;
3438 data->res.server = server;
3439 data->timestamp = jiffies;
3441 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3442 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3445 static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3447 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3448 &data->args.seq_args,
3452 rpc_call_start(task);
3455 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3457 struct inode *inode = data->inode;
3459 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3460 rpc_restart_call_prepare(task);
3463 nfs_refresh_inode(inode, data->res.fattr);
3467 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3469 if (!nfs4_sequence_done(task, &data->res.seq_res))
3471 return data->write_done_cb(task, data);
3474 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3476 struct nfs_server *server = NFS_SERVER(data->inode);
3479 data->args.bitmask = NULL;
3480 data->res.fattr = NULL;
3482 data->args.bitmask = server->cache_consistency_bitmask;
3483 if (!data->write_done_cb)
3484 data->write_done_cb = nfs4_commit_done_cb;
3485 data->res.server = server;
3486 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3487 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3490 struct nfs4_renewdata {
3491 struct nfs_client *client;
3492 unsigned long timestamp;
3496 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3497 * standalone procedure for queueing an asynchronous RENEW.
3499 static void nfs4_renew_release(void *calldata)
3501 struct nfs4_renewdata *data = calldata;
3502 struct nfs_client *clp = data->client;
3504 if (atomic_read(&clp->cl_count) > 1)
3505 nfs4_schedule_state_renewal(clp);
3506 nfs_put_client(clp);
3510 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3512 struct nfs4_renewdata *data = calldata;
3513 struct nfs_client *clp = data->client;
3514 unsigned long timestamp = data->timestamp;
3516 if (task->tk_status < 0) {
3517 /* Unless we're shutting down, schedule state recovery! */
3518 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3520 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3521 nfs4_schedule_lease_recovery(clp);
3524 nfs4_schedule_path_down_recovery(clp);
3526 do_renew_lease(clp, timestamp);
3529 static const struct rpc_call_ops nfs4_renew_ops = {
3530 .rpc_call_done = nfs4_renew_done,
3531 .rpc_release = nfs4_renew_release,
3534 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3536 struct rpc_message msg = {
3537 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3541 struct nfs4_renewdata *data;
3543 if (renew_flags == 0)
3545 if (!atomic_inc_not_zero(&clp->cl_count))
3547 data = kmalloc(sizeof(*data), GFP_NOFS);
3551 data->timestamp = jiffies;
3552 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3553 &nfs4_renew_ops, data);
3556 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3558 struct rpc_message msg = {
3559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3563 unsigned long now = jiffies;
3566 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3569 do_renew_lease(clp, now);
3573 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3575 return (server->caps & NFS_CAP_ACLS)
3576 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3577 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3580 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3581 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3584 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3586 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3587 struct page **pages, unsigned int *pgbase)
3589 struct page *newpage, **spages;
3595 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3596 newpage = alloc_page(GFP_KERNEL);
3598 if (newpage == NULL)
3600 memcpy(page_address(newpage), buf, len);
3605 } while (buflen != 0);
3611 __free_page(spages[rc-1]);
3615 struct nfs4_cached_acl {
3621 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3623 struct nfs_inode *nfsi = NFS_I(inode);
3625 spin_lock(&inode->i_lock);
3626 kfree(nfsi->nfs4_acl);
3627 nfsi->nfs4_acl = acl;
3628 spin_unlock(&inode->i_lock);
3631 static void nfs4_zap_acl_attr(struct inode *inode)
3633 nfs4_set_cached_acl(inode, NULL);
3636 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3638 struct nfs_inode *nfsi = NFS_I(inode);
3639 struct nfs4_cached_acl *acl;
3642 spin_lock(&inode->i_lock);
3643 acl = nfsi->nfs4_acl;
3646 if (buf == NULL) /* user is just asking for length */
3648 if (acl->cached == 0)
3650 ret = -ERANGE; /* see getxattr(2) man page */
3651 if (acl->len > buflen)
3653 memcpy(buf, acl->data, acl->len);
3657 spin_unlock(&inode->i_lock);
3661 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3663 struct nfs4_cached_acl *acl;
3665 if (pages && acl_len <= PAGE_SIZE) {
3666 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3670 _copy_from_pages(acl->data, pages, pgbase, acl_len);
3672 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3679 nfs4_set_cached_acl(inode, acl);
3683 * The getxattr API returns the required buffer length when called with a
3684 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3685 * the required buf. On a NULL buf, we send a page of data to the server
3686 * guessing that the ACL request can be serviced by a page. If so, we cache
3687 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3688 * the cache. If not so, we throw away the page, and cache the required
3689 * length. The next getxattr call will then produce another round trip to
3690 * the server, this time with the input buf of the required size.
3692 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3694 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3695 struct nfs_getaclargs args = {
3696 .fh = NFS_FH(inode),
3700 struct nfs_getaclres res = {
3703 struct rpc_message msg = {
3704 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3708 int ret = -ENOMEM, npages, i, acl_len = 0;
3710 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3711 /* As long as we're doing a round trip to the server anyway,
3712 * let's be prepared for a page of acl data. */
3716 /* Add an extra page to handle the bitmap returned */
3719 for (i = 0; i < npages; i++) {
3720 pages[i] = alloc_page(GFP_KERNEL);
3725 /* for decoding across pages */
3726 res.acl_scratch = alloc_page(GFP_KERNEL);
3727 if (!res.acl_scratch)
3730 args.acl_len = npages * PAGE_SIZE;
3731 args.acl_pgbase = 0;
3733 /* Let decode_getfacl know not to fail if the ACL data is larger than
3734 * the page we send as a guess */
3736 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3738 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3739 __func__, buf, buflen, npages, args.acl_len);
3740 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3741 &msg, &args.seq_args, &res.seq_res, 0);
3745 acl_len = res.acl_len - res.acl_data_offset;
3746 if (acl_len > args.acl_len)
3747 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
3749 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
3753 if (acl_len > buflen)
3755 _copy_from_pages(buf, pages, res.acl_data_offset,
3760 for (i = 0; i < npages; i++)
3762 __free_page(pages[i]);
3763 if (res.acl_scratch)
3764 __free_page(res.acl_scratch);
3768 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3770 struct nfs4_exception exception = { };
3773 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3776 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3777 } while (exception.retry);
3781 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3783 struct nfs_server *server = NFS_SERVER(inode);
3786 if (!nfs4_server_supports_acls(server))
3788 ret = nfs_revalidate_inode(server, inode);
3791 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3792 nfs_zap_acl_cache(inode);
3793 ret = nfs4_read_cached_acl(inode, buf, buflen);
3795 /* -ENOENT is returned if there is no ACL or if there is an ACL
3796 * but no cached acl data, just the acl length */
3798 return nfs4_get_acl_uncached(inode, buf, buflen);
3801 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3803 struct nfs_server *server = NFS_SERVER(inode);
3804 struct page *pages[NFS4ACL_MAXPAGES];
3805 struct nfs_setaclargs arg = {
3806 .fh = NFS_FH(inode),
3810 struct nfs_setaclres res;
3811 struct rpc_message msg = {
3812 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3818 if (!nfs4_server_supports_acls(server))
3820 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3823 nfs_inode_return_delegation(inode);
3824 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3827 * Free each page after tx, so the only ref left is
3828 * held by the network stack
3831 put_page(pages[i-1]);
3834 * Acl update can result in inode attribute update.
3835 * so mark the attribute cache invalid.
3837 spin_lock(&inode->i_lock);
3838 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3839 spin_unlock(&inode->i_lock);
3840 nfs_access_zap_cache(inode);
3841 nfs_zap_acl_cache(inode);
3845 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3847 struct nfs4_exception exception = { };
3850 err = nfs4_handle_exception(NFS_SERVER(inode),
3851 __nfs4_proc_set_acl(inode, buf, buflen),
3853 } while (exception.retry);
3858 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3860 struct nfs_client *clp = server->nfs_client;
3862 if (task->tk_status >= 0)
3864 switch(task->tk_status) {
3865 case -NFS4ERR_DELEG_REVOKED:
3866 case -NFS4ERR_ADMIN_REVOKED:
3867 case -NFS4ERR_BAD_STATEID:
3870 nfs_remove_bad_delegation(state->inode);
3871 case -NFS4ERR_OPENMODE:
3874 nfs4_schedule_stateid_recovery(server, state);
3875 goto wait_on_recovery;
3876 case -NFS4ERR_EXPIRED:
3878 nfs4_schedule_stateid_recovery(server, state);
3879 case -NFS4ERR_STALE_STATEID:
3880 case -NFS4ERR_STALE_CLIENTID:
3881 nfs4_schedule_lease_recovery(clp);
3882 goto wait_on_recovery;
3883 #if defined(CONFIG_NFS_V4_1)
3884 case -NFS4ERR_BADSESSION:
3885 case -NFS4ERR_BADSLOT:
3886 case -NFS4ERR_BAD_HIGH_SLOT:
3887 case -NFS4ERR_DEADSESSION:
3888 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3889 case -NFS4ERR_SEQ_FALSE_RETRY:
3890 case -NFS4ERR_SEQ_MISORDERED:
3891 dprintk("%s ERROR %d, Reset session\n", __func__,
3893 nfs4_schedule_session_recovery(clp->cl_session);
3894 task->tk_status = 0;
3896 #endif /* CONFIG_NFS_V4_1 */
3897 case -NFS4ERR_DELAY:
3898 nfs_inc_server_stats(server, NFSIOS_DELAY);
3899 case -NFS4ERR_GRACE:
3901 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3902 task->tk_status = 0;
3904 case -NFS4ERR_RETRY_UNCACHED_REP:
3905 case -NFS4ERR_OLD_STATEID:
3906 task->tk_status = 0;
3909 task->tk_status = nfs4_map_errors(task->tk_status);
3912 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3913 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3914 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3915 task->tk_status = 0;
3919 static void nfs4_construct_boot_verifier(struct nfs_client *clp,
3920 nfs4_verifier *bootverf)
3924 verf[0] = htonl((u32)clp->cl_boot_time.tv_sec);
3925 verf[1] = htonl((u32)clp->cl_boot_time.tv_nsec);
3926 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3929 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3930 unsigned short port, struct rpc_cred *cred,
3931 struct nfs4_setclientid_res *res)
3933 nfs4_verifier sc_verifier;
3934 struct nfs4_setclientid setclientid = {
3935 .sc_verifier = &sc_verifier,
3937 .sc_cb_ident = clp->cl_cb_ident,
3939 struct rpc_message msg = {
3940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3941 .rpc_argp = &setclientid,
3948 nfs4_construct_boot_verifier(clp, &sc_verifier);
3952 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3953 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3955 rpc_peeraddr2str(clp->cl_rpcclient,
3957 rpc_peeraddr2str(clp->cl_rpcclient,
3959 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3960 clp->cl_id_uniquifier);
3961 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3962 sizeof(setclientid.sc_netid),
3963 rpc_peeraddr2str(clp->cl_rpcclient,
3964 RPC_DISPLAY_NETID));
3965 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3966 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3967 clp->cl_ipaddr, port >> 8, port & 255);
3970 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3971 if (status != -NFS4ERR_CLID_INUSE)
3974 ++clp->cl_id_uniquifier;
3978 ssleep(clp->cl_lease_time / HZ + 1);
3983 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3984 struct nfs4_setclientid_res *arg,
3985 struct rpc_cred *cred)
3987 struct nfs_fsinfo fsinfo;
3988 struct rpc_message msg = {
3989 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3991 .rpc_resp = &fsinfo,
3998 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4000 spin_lock(&clp->cl_lock);
4001 clp->cl_lease_time = fsinfo.lease_time * HZ;
4002 clp->cl_last_renewal = now;
4003 spin_unlock(&clp->cl_lock);
4008 struct nfs4_delegreturndata {
4009 struct nfs4_delegreturnargs args;
4010 struct nfs4_delegreturnres res;
4012 nfs4_stateid stateid;
4013 unsigned long timestamp;
4014 struct nfs_fattr fattr;
4018 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4020 struct nfs4_delegreturndata *data = calldata;
4022 if (!nfs4_sequence_done(task, &data->res.seq_res))
4025 switch (task->tk_status) {
4026 case -NFS4ERR_STALE_STATEID:
4027 case -NFS4ERR_EXPIRED:
4029 renew_lease(data->res.server, data->timestamp);
4032 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4034 rpc_restart_call_prepare(task);
4038 data->rpc_status = task->tk_status;
4041 static void nfs4_delegreturn_release(void *calldata)
4046 #if defined(CONFIG_NFS_V4_1)
4047 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4049 struct nfs4_delegreturndata *d_data;
4051 d_data = (struct nfs4_delegreturndata *)data;
4053 if (nfs4_setup_sequence(d_data->res.server,
4054 &d_data->args.seq_args,
4055 &d_data->res.seq_res, task))
4057 rpc_call_start(task);
4059 #endif /* CONFIG_NFS_V4_1 */
4061 static const struct rpc_call_ops nfs4_delegreturn_ops = {
4062 #if defined(CONFIG_NFS_V4_1)
4063 .rpc_call_prepare = nfs4_delegreturn_prepare,
4064 #endif /* CONFIG_NFS_V4_1 */
4065 .rpc_call_done = nfs4_delegreturn_done,
4066 .rpc_release = nfs4_delegreturn_release,
4069 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4071 struct nfs4_delegreturndata *data;
4072 struct nfs_server *server = NFS_SERVER(inode);
4073 struct rpc_task *task;
4074 struct rpc_message msg = {
4075 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4078 struct rpc_task_setup task_setup_data = {
4079 .rpc_client = server->client,
4080 .rpc_message = &msg,
4081 .callback_ops = &nfs4_delegreturn_ops,
4082 .flags = RPC_TASK_ASYNC,
4086 data = kzalloc(sizeof(*data), GFP_NOFS);
4089 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4090 data->args.fhandle = &data->fh;
4091 data->args.stateid = &data->stateid;
4092 data->args.bitmask = server->attr_bitmask;
4093 nfs_copy_fh(&data->fh, NFS_FH(inode));
4094 nfs4_stateid_copy(&data->stateid, stateid);
4095 data->res.fattr = &data->fattr;
4096 data->res.server = server;
4097 nfs_fattr_init(data->res.fattr);
4098 data->timestamp = jiffies;
4099 data->rpc_status = 0;
4101 task_setup_data.callback_data = data;
4102 msg.rpc_argp = &data->args;
4103 msg.rpc_resp = &data->res;
4104 task = rpc_run_task(&task_setup_data);
4106 return PTR_ERR(task);
4109 status = nfs4_wait_for_completion_rpc_task(task);
4112 status = data->rpc_status;
4115 nfs_refresh_inode(inode, &data->fattr);
4121 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4123 struct nfs_server *server = NFS_SERVER(inode);
4124 struct nfs4_exception exception = { };
4127 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4129 case -NFS4ERR_STALE_STATEID:
4130 case -NFS4ERR_EXPIRED:
4134 err = nfs4_handle_exception(server, err, &exception);
4135 } while (exception.retry);
4139 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4140 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4143 * sleep, with exponential backoff, and retry the LOCK operation.
4145 static unsigned long
4146 nfs4_set_lock_task_retry(unsigned long timeout)
4148 freezable_schedule_timeout_killable(timeout);
4150 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4151 return NFS4_LOCK_MAXTIMEOUT;
4155 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4157 struct inode *inode = state->inode;
4158 struct nfs_server *server = NFS_SERVER(inode);
4159 struct nfs_client *clp = server->nfs_client;
4160 struct nfs_lockt_args arg = {
4161 .fh = NFS_FH(inode),
4164 struct nfs_lockt_res res = {
4167 struct rpc_message msg = {
4168 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4171 .rpc_cred = state->owner->so_cred,
4173 struct nfs4_lock_state *lsp;
4176 arg.lock_owner.clientid = clp->cl_clientid;
4177 status = nfs4_set_lock_state(state, request);
4180 lsp = request->fl_u.nfs4_fl.owner;
4181 arg.lock_owner.id = lsp->ls_seqid.owner_id;
4182 arg.lock_owner.s_dev = server->s_dev;
4183 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4186 request->fl_type = F_UNLCK;
4188 case -NFS4ERR_DENIED:
4191 request->fl_ops->fl_release_private(request);
4196 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4198 struct nfs4_exception exception = { };
4202 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4203 _nfs4_proc_getlk(state, cmd, request),
4205 } while (exception.retry);
4209 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4212 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4214 res = posix_lock_file_wait(file, fl);
4217 res = flock_lock_file_wait(file, fl);
4225 struct nfs4_unlockdata {
4226 struct nfs_locku_args arg;
4227 struct nfs_locku_res res;
4228 struct nfs4_lock_state *lsp;
4229 struct nfs_open_context *ctx;
4230 struct file_lock fl;
4231 const struct nfs_server *server;
4232 unsigned long timestamp;
4235 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4236 struct nfs_open_context *ctx,
4237 struct nfs4_lock_state *lsp,
4238 struct nfs_seqid *seqid)
4240 struct nfs4_unlockdata *p;
4241 struct inode *inode = lsp->ls_state->inode;
4243 p = kzalloc(sizeof(*p), GFP_NOFS);
4246 p->arg.fh = NFS_FH(inode);
4248 p->arg.seqid = seqid;
4249 p->res.seqid = seqid;
4250 p->arg.stateid = &lsp->ls_stateid;
4252 atomic_inc(&lsp->ls_count);
4253 /* Ensure we don't close file until we're done freeing locks! */
4254 p->ctx = get_nfs_open_context(ctx);
4255 memcpy(&p->fl, fl, sizeof(p->fl));
4256 p->server = NFS_SERVER(inode);
4260 static void nfs4_locku_release_calldata(void *data)
4262 struct nfs4_unlockdata *calldata = data;
4263 nfs_free_seqid(calldata->arg.seqid);
4264 nfs4_put_lock_state(calldata->lsp);
4265 put_nfs_open_context(calldata->ctx);
4269 static void nfs4_locku_done(struct rpc_task *task, void *data)
4271 struct nfs4_unlockdata *calldata = data;
4273 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4275 switch (task->tk_status) {
4277 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4278 &calldata->res.stateid);
4279 renew_lease(calldata->server, calldata->timestamp);
4281 case -NFS4ERR_BAD_STATEID:
4282 case -NFS4ERR_OLD_STATEID:
4283 case -NFS4ERR_STALE_STATEID:
4284 case -NFS4ERR_EXPIRED:
4287 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4288 rpc_restart_call_prepare(task);
4292 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4294 struct nfs4_unlockdata *calldata = data;
4296 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4298 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
4299 /* Note: exit _without_ running nfs4_locku_done */
4300 task->tk_action = NULL;
4303 calldata->timestamp = jiffies;
4304 if (nfs4_setup_sequence(calldata->server,
4305 &calldata->arg.seq_args,
4306 &calldata->res.seq_res, task))
4308 rpc_call_start(task);
4311 static const struct rpc_call_ops nfs4_locku_ops = {
4312 .rpc_call_prepare = nfs4_locku_prepare,
4313 .rpc_call_done = nfs4_locku_done,
4314 .rpc_release = nfs4_locku_release_calldata,
4317 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4318 struct nfs_open_context *ctx,
4319 struct nfs4_lock_state *lsp,
4320 struct nfs_seqid *seqid)
4322 struct nfs4_unlockdata *data;
4323 struct rpc_message msg = {
4324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4325 .rpc_cred = ctx->cred,
4327 struct rpc_task_setup task_setup_data = {
4328 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4329 .rpc_message = &msg,
4330 .callback_ops = &nfs4_locku_ops,
4331 .workqueue = nfsiod_workqueue,
4332 .flags = RPC_TASK_ASYNC,
4335 /* Ensure this is an unlock - when canceling a lock, the
4336 * canceled lock is passed in, and it won't be an unlock.
4338 fl->fl_type = F_UNLCK;
4340 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4342 nfs_free_seqid(seqid);
4343 return ERR_PTR(-ENOMEM);
4346 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4347 msg.rpc_argp = &data->arg;
4348 msg.rpc_resp = &data->res;
4349 task_setup_data.callback_data = data;
4350 return rpc_run_task(&task_setup_data);
4353 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4355 struct nfs_inode *nfsi = NFS_I(state->inode);
4356 struct nfs_seqid *seqid;
4357 struct nfs4_lock_state *lsp;
4358 struct rpc_task *task;
4360 unsigned char fl_flags = request->fl_flags;
4362 status = nfs4_set_lock_state(state, request);
4363 /* Unlock _before_ we do the RPC call */
4364 request->fl_flags |= FL_EXISTS;
4365 down_read(&nfsi->rwsem);
4366 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4367 up_read(&nfsi->rwsem);
4370 up_read(&nfsi->rwsem);
4373 /* Is this a delegated lock? */
4374 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4376 lsp = request->fl_u.nfs4_fl.owner;
4377 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4381 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4382 status = PTR_ERR(task);
4385 status = nfs4_wait_for_completion_rpc_task(task);
4388 request->fl_flags = fl_flags;
4392 struct nfs4_lockdata {
4393 struct nfs_lock_args arg;
4394 struct nfs_lock_res res;
4395 struct nfs4_lock_state *lsp;
4396 struct nfs_open_context *ctx;
4397 struct file_lock fl;
4398 unsigned long timestamp;
4401 struct nfs_server *server;
4404 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4405 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4408 struct nfs4_lockdata *p;
4409 struct inode *inode = lsp->ls_state->inode;
4410 struct nfs_server *server = NFS_SERVER(inode);
4412 p = kzalloc(sizeof(*p), gfp_mask);
4416 p->arg.fh = NFS_FH(inode);
4418 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4419 if (p->arg.open_seqid == NULL)
4421 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4422 if (p->arg.lock_seqid == NULL)
4423 goto out_free_seqid;
4424 p->arg.lock_stateid = &lsp->ls_stateid;
4425 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4426 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4427 p->arg.lock_owner.s_dev = server->s_dev;
4428 p->res.lock_seqid = p->arg.lock_seqid;
4431 atomic_inc(&lsp->ls_count);
4432 p->ctx = get_nfs_open_context(ctx);
4433 memcpy(&p->fl, fl, sizeof(p->fl));
4436 nfs_free_seqid(p->arg.open_seqid);
4442 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4444 struct nfs4_lockdata *data = calldata;
4445 struct nfs4_state *state = data->lsp->ls_state;
4447 dprintk("%s: begin!\n", __func__);
4448 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4450 /* Do we need to do an open_to_lock_owner? */
4451 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4452 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4454 data->arg.open_stateid = &state->stateid;
4455 data->arg.new_lock_owner = 1;
4456 data->res.open_seqid = data->arg.open_seqid;
4458 data->arg.new_lock_owner = 0;
4459 data->timestamp = jiffies;
4460 if (nfs4_setup_sequence(data->server,
4461 &data->arg.seq_args,
4462 &data->res.seq_res, task))
4464 rpc_call_start(task);
4465 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4468 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4470 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4471 nfs4_lock_prepare(task, calldata);
4474 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4476 struct nfs4_lockdata *data = calldata;
4478 dprintk("%s: begin!\n", __func__);
4480 if (!nfs4_sequence_done(task, &data->res.seq_res))
4483 data->rpc_status = task->tk_status;
4484 if (data->arg.new_lock_owner != 0) {
4485 if (data->rpc_status == 0)
4486 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4490 if (data->rpc_status == 0) {
4491 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4492 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4493 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4496 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4499 static void nfs4_lock_release(void *calldata)
4501 struct nfs4_lockdata *data = calldata;
4503 dprintk("%s: begin!\n", __func__);
4504 nfs_free_seqid(data->arg.open_seqid);
4505 if (data->cancelled != 0) {
4506 struct rpc_task *task;
4507 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4508 data->arg.lock_seqid);
4510 rpc_put_task_async(task);
4511 dprintk("%s: cancelling lock!\n", __func__);
4513 nfs_free_seqid(data->arg.lock_seqid);
4514 nfs4_put_lock_state(data->lsp);
4515 put_nfs_open_context(data->ctx);
4517 dprintk("%s: done!\n", __func__);
4520 static const struct rpc_call_ops nfs4_lock_ops = {
4521 .rpc_call_prepare = nfs4_lock_prepare,
4522 .rpc_call_done = nfs4_lock_done,
4523 .rpc_release = nfs4_lock_release,
4526 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4527 .rpc_call_prepare = nfs4_recover_lock_prepare,
4528 .rpc_call_done = nfs4_lock_done,
4529 .rpc_release = nfs4_lock_release,
4532 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4535 case -NFS4ERR_ADMIN_REVOKED:
4536 case -NFS4ERR_BAD_STATEID:
4537 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4538 if (new_lock_owner != 0 ||
4539 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4540 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4542 case -NFS4ERR_STALE_STATEID:
4543 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4544 case -NFS4ERR_EXPIRED:
4545 nfs4_schedule_lease_recovery(server->nfs_client);
4549 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4551 struct nfs4_lockdata *data;
4552 struct rpc_task *task;
4553 struct rpc_message msg = {
4554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4555 .rpc_cred = state->owner->so_cred,
4557 struct rpc_task_setup task_setup_data = {
4558 .rpc_client = NFS_CLIENT(state->inode),
4559 .rpc_message = &msg,
4560 .callback_ops = &nfs4_lock_ops,
4561 .workqueue = nfsiod_workqueue,
4562 .flags = RPC_TASK_ASYNC,
4566 dprintk("%s: begin!\n", __func__);
4567 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4568 fl->fl_u.nfs4_fl.owner,
4569 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4573 data->arg.block = 1;
4574 if (recovery_type > NFS_LOCK_NEW) {
4575 if (recovery_type == NFS_LOCK_RECLAIM)
4576 data->arg.reclaim = NFS_LOCK_RECLAIM;
4577 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4579 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4580 msg.rpc_argp = &data->arg;
4581 msg.rpc_resp = &data->res;
4582 task_setup_data.callback_data = data;
4583 task = rpc_run_task(&task_setup_data);
4585 return PTR_ERR(task);
4586 ret = nfs4_wait_for_completion_rpc_task(task);
4588 ret = data->rpc_status;
4590 nfs4_handle_setlk_error(data->server, data->lsp,
4591 data->arg.new_lock_owner, ret);
4593 data->cancelled = 1;
4595 dprintk("%s: done, ret = %d!\n", __func__, ret);
4599 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4601 struct nfs_server *server = NFS_SERVER(state->inode);
4602 struct nfs4_exception exception = {
4603 .inode = state->inode,
4608 /* Cache the lock if possible... */
4609 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4611 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4612 if (err != -NFS4ERR_DELAY)
4614 nfs4_handle_exception(server, err, &exception);
4615 } while (exception.retry);
4619 static int nfs4_lock_expired(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,
4627 err = nfs4_set_lock_state(state, request);
4631 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4633 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4637 case -NFS4ERR_GRACE:
4638 case -NFS4ERR_DELAY:
4639 nfs4_handle_exception(server, err, &exception);
4642 } while (exception.retry);
4647 #if defined(CONFIG_NFS_V4_1)
4648 static int nfs41_check_expired_locks(struct nfs4_state *state)
4650 int status, ret = NFS_OK;
4651 struct nfs4_lock_state *lsp;
4652 struct nfs_server *server = NFS_SERVER(state->inode);
4654 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4655 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4656 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4657 if (status != NFS_OK) {
4658 nfs41_free_stateid(server, &lsp->ls_stateid);
4659 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4668 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4670 int status = NFS_OK;
4672 if (test_bit(LK_STATE_IN_USE, &state->flags))
4673 status = nfs41_check_expired_locks(state);
4674 if (status == NFS_OK)
4676 return nfs4_lock_expired(state, request);
4680 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4682 struct nfs_inode *nfsi = NFS_I(state->inode);
4683 unsigned char fl_flags = request->fl_flags;
4684 int status = -ENOLCK;
4686 if ((fl_flags & FL_POSIX) &&
4687 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4689 /* Is this a delegated open? */
4690 status = nfs4_set_lock_state(state, request);
4693 request->fl_flags |= FL_ACCESS;
4694 status = do_vfs_lock(request->fl_file, request);
4697 down_read(&nfsi->rwsem);
4698 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4699 /* Yes: cache locks! */
4700 /* ...but avoid races with delegation recall... */
4701 request->fl_flags = fl_flags & ~FL_SLEEP;
4702 status = do_vfs_lock(request->fl_file, request);
4705 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4708 /* Note: we always want to sleep here! */
4709 request->fl_flags = fl_flags | FL_SLEEP;
4710 if (do_vfs_lock(request->fl_file, request) < 0)
4711 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4712 "manager!\n", __func__);
4714 up_read(&nfsi->rwsem);
4716 request->fl_flags = fl_flags;
4720 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4722 struct nfs4_exception exception = {
4724 .inode = state->inode,
4729 err = _nfs4_proc_setlk(state, cmd, request);
4730 if (err == -NFS4ERR_DENIED)
4732 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4734 } while (exception.retry);
4739 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4741 struct nfs_open_context *ctx;
4742 struct nfs4_state *state;
4743 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4746 /* verify open state */
4747 ctx = nfs_file_open_context(filp);
4750 if (request->fl_start < 0 || request->fl_end < 0)
4753 if (IS_GETLK(cmd)) {
4755 return nfs4_proc_getlk(state, F_GETLK, request);
4759 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4762 if (request->fl_type == F_UNLCK) {
4764 return nfs4_proc_unlck(state, cmd, request);
4771 * Don't rely on the VFS having checked the file open mode,
4772 * since it won't do this for flock() locks.
4774 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4776 if (!(filp->f_mode & FMODE_READ))
4780 if (!(filp->f_mode & FMODE_WRITE))
4785 status = nfs4_proc_setlk(state, cmd, request);
4786 if ((status != -EAGAIN) || IS_SETLK(cmd))
4788 timeout = nfs4_set_lock_task_retry(timeout);
4789 status = -ERESTARTSYS;
4792 } while(status < 0);
4796 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4798 struct nfs_server *server = NFS_SERVER(state->inode);
4799 struct nfs4_exception exception = { };
4802 err = nfs4_set_lock_state(state, fl);
4806 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4809 printk(KERN_ERR "NFS: %s: unhandled error "
4810 "%d.\n", __func__, err);
4814 case -NFS4ERR_EXPIRED:
4815 nfs4_schedule_stateid_recovery(server, state);
4816 case -NFS4ERR_STALE_CLIENTID:
4817 case -NFS4ERR_STALE_STATEID:
4818 nfs4_schedule_lease_recovery(server->nfs_client);
4820 case -NFS4ERR_BADSESSION:
4821 case -NFS4ERR_BADSLOT:
4822 case -NFS4ERR_BAD_HIGH_SLOT:
4823 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4824 case -NFS4ERR_DEADSESSION:
4825 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
4829 * The show must go on: exit, but mark the
4830 * stateid as needing recovery.
4832 case -NFS4ERR_DELEG_REVOKED:
4833 case -NFS4ERR_ADMIN_REVOKED:
4834 case -NFS4ERR_BAD_STATEID:
4835 case -NFS4ERR_OPENMODE:
4836 nfs4_schedule_stateid_recovery(server, state);
4841 * User RPCSEC_GSS context has expired.
4842 * We cannot recover this stateid now, so
4843 * skip it and allow recovery thread to
4849 case -NFS4ERR_DENIED:
4850 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4853 case -NFS4ERR_DELAY:
4856 err = nfs4_handle_exception(server, err, &exception);
4857 } while (exception.retry);
4862 struct nfs_release_lockowner_data {
4863 struct nfs4_lock_state *lsp;
4864 struct nfs_server *server;
4865 struct nfs_release_lockowner_args args;
4868 static void nfs4_release_lockowner_release(void *calldata)
4870 struct nfs_release_lockowner_data *data = calldata;
4871 nfs4_free_lock_state(data->server, data->lsp);
4875 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
4876 .rpc_release = nfs4_release_lockowner_release,
4879 int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
4881 struct nfs_server *server = lsp->ls_state->owner->so_server;
4882 struct nfs_release_lockowner_data *data;
4883 struct rpc_message msg = {
4884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4887 if (server->nfs_client->cl_mvops->minor_version != 0)
4889 data = kmalloc(sizeof(*data), GFP_NOFS);
4893 data->server = server;
4894 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4895 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4896 data->args.lock_owner.s_dev = server->s_dev;
4897 msg.rpc_argp = &data->args;
4898 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4902 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4904 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4905 const void *buf, size_t buflen,
4906 int flags, int type)
4908 if (strcmp(key, "") != 0)
4911 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
4914 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4915 void *buf, size_t buflen, int type)
4917 if (strcmp(key, "") != 0)
4920 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
4923 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4924 size_t list_len, const char *name,
4925 size_t name_len, int type)
4927 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
4929 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4932 if (list && len <= list_len)
4933 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
4938 * nfs_fhget will use either the mounted_on_fileid or the fileid
4940 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4942 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4943 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4944 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4945 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
4948 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4949 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
4950 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4954 static int _nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4955 struct nfs4_fs_locations *fs_locations, struct page *page)
4957 struct nfs_server *server = NFS_SERVER(dir);
4959 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4961 struct nfs4_fs_locations_arg args = {
4962 .dir_fh = NFS_FH(dir),
4967 struct nfs4_fs_locations_res res = {
4968 .fs_locations = fs_locations,
4970 struct rpc_message msg = {
4971 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4977 dprintk("%s: start\n", __func__);
4979 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4980 * is not supported */
4981 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4982 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4984 bitmask[0] |= FATTR4_WORD0_FILEID;
4986 nfs_fattr_init(&fs_locations->fattr);
4987 fs_locations->server = server;
4988 fs_locations->nlocations = 0;
4989 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4990 dprintk("%s: returned status = %d\n", __func__, status);
4994 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4995 struct nfs4_fs_locations *fs_locations, struct page *page)
4997 struct nfs4_exception exception = { };
5000 err = nfs4_handle_exception(NFS_SERVER(dir),
5001 _nfs4_proc_fs_locations(dir, name, fs_locations, page),
5003 } while (exception.retry);
5007 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5010 struct nfs4_secinfo_arg args = {
5011 .dir_fh = NFS_FH(dir),
5014 struct nfs4_secinfo_res res = {
5017 struct rpc_message msg = {
5018 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5023 dprintk("NFS call secinfo %s\n", name->name);
5024 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5025 dprintk("NFS reply secinfo: %d\n", status);
5029 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5030 struct nfs4_secinfo_flavors *flavors)
5032 struct nfs4_exception exception = { };
5035 err = nfs4_handle_exception(NFS_SERVER(dir),
5036 _nfs4_proc_secinfo(dir, name, flavors),
5038 } while (exception.retry);
5042 #ifdef CONFIG_NFS_V4_1
5044 * Check the exchange flags returned by the server for invalid flags, having
5045 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5048 static int nfs4_check_cl_exchange_flags(u32 flags)
5050 if (flags & ~EXCHGID4_FLAG_MASK_R)
5052 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5053 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5055 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5059 return -NFS4ERR_INVAL;
5063 nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
5065 if (a->server_scope_sz == b->server_scope_sz &&
5066 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5073 * nfs4_proc_exchange_id()
5075 * Since the clientid has expired, all compounds using sessions
5076 * associated with the stale clientid will be returning
5077 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5078 * be in some phase of session reset.
5080 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
5082 nfs4_verifier verifier;
5083 struct nfs41_exchange_id_args args = {
5084 .verifier = &verifier,
5086 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
5088 struct nfs41_exchange_id_res res = {
5092 struct rpc_message msg = {
5093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5099 dprintk("--> %s\n", __func__);
5100 BUG_ON(clp == NULL);
5102 nfs4_construct_boot_verifier(clp, &verifier);
5104 args.id_len = scnprintf(args.id, sizeof(args.id),
5107 init_utsname()->nodename,
5108 init_utsname()->domainname,
5109 clp->cl_rpcclient->cl_auth->au_flavor);
5111 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
5112 if (unlikely(!res.server_scope)) {
5117 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
5118 if (unlikely(!res.impl_id)) {
5120 goto out_server_scope;
5123 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5125 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
5128 /* use the most recent implementation id */
5129 kfree(clp->impl_id);
5130 clp->impl_id = res.impl_id;
5135 if (clp->server_scope &&
5136 !nfs41_same_server_scope(clp->server_scope,
5137 res.server_scope)) {
5138 dprintk("%s: server_scope mismatch detected\n",
5140 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5141 kfree(clp->server_scope);
5142 clp->server_scope = NULL;
5145 if (!clp->server_scope) {
5146 clp->server_scope = res.server_scope;
5152 kfree(res.server_scope);
5155 dprintk("%s: Server Implementation ID: "
5156 "domain: %s, name: %s, date: %llu,%u\n",
5157 __func__, clp->impl_id->domain, clp->impl_id->name,
5158 clp->impl_id->date.seconds,
5159 clp->impl_id->date.nseconds);
5160 dprintk("<-- %s status= %d\n", __func__, status);
5164 struct nfs4_get_lease_time_data {
5165 struct nfs4_get_lease_time_args *args;
5166 struct nfs4_get_lease_time_res *res;
5167 struct nfs_client *clp;
5170 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5174 struct nfs4_get_lease_time_data *data =
5175 (struct nfs4_get_lease_time_data *)calldata;
5177 dprintk("--> %s\n", __func__);
5178 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5179 /* just setup sequence, do not trigger session recovery
5180 since we're invoked within one */
5181 ret = nfs41_setup_sequence(data->clp->cl_session,
5182 &data->args->la_seq_args,
5183 &data->res->lr_seq_res, task);
5185 BUG_ON(ret == -EAGAIN);
5186 rpc_call_start(task);
5187 dprintk("<-- %s\n", __func__);
5191 * Called from nfs4_state_manager thread for session setup, so don't recover
5192 * from sequence operation or clientid errors.
5194 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5196 struct nfs4_get_lease_time_data *data =
5197 (struct nfs4_get_lease_time_data *)calldata;
5199 dprintk("--> %s\n", __func__);
5200 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5202 switch (task->tk_status) {
5203 case -NFS4ERR_DELAY:
5204 case -NFS4ERR_GRACE:
5205 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5206 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5207 task->tk_status = 0;
5209 case -NFS4ERR_RETRY_UNCACHED_REP:
5210 rpc_restart_call_prepare(task);
5213 dprintk("<-- %s\n", __func__);
5216 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5217 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5218 .rpc_call_done = nfs4_get_lease_time_done,
5221 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5223 struct rpc_task *task;
5224 struct nfs4_get_lease_time_args args;
5225 struct nfs4_get_lease_time_res res = {
5226 .lr_fsinfo = fsinfo,
5228 struct nfs4_get_lease_time_data data = {
5233 struct rpc_message msg = {
5234 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5238 struct rpc_task_setup task_setup = {
5239 .rpc_client = clp->cl_rpcclient,
5240 .rpc_message = &msg,
5241 .callback_ops = &nfs4_get_lease_time_ops,
5242 .callback_data = &data,
5243 .flags = RPC_TASK_TIMEOUT,
5247 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5248 dprintk("--> %s\n", __func__);
5249 task = rpc_run_task(&task_setup);
5252 status = PTR_ERR(task);
5254 status = task->tk_status;
5257 dprintk("<-- %s return %d\n", __func__, status);
5262 static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5264 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5267 static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5268 struct nfs4_slot *new,
5272 struct nfs4_slot *old = NULL;
5275 spin_lock(&tbl->slot_tbl_lock);
5279 tbl->max_slots = max_slots;
5281 tbl->highest_used_slotid = -1; /* no slot is currently used */
5282 for (i = 0; i < tbl->max_slots; i++)
5283 tbl->slots[i].seq_nr = ivalue;
5284 spin_unlock(&tbl->slot_tbl_lock);
5289 * (re)Initialise a slot table
5291 static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5294 struct nfs4_slot *new = NULL;
5297 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5298 max_reqs, tbl->max_slots);
5300 /* Does the newly negotiated max_reqs match the existing slot table? */
5301 if (max_reqs != tbl->max_slots) {
5302 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
5308 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
5309 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5310 tbl, tbl->slots, tbl->max_slots);
5312 dprintk("<-- %s: return %d\n", __func__, ret);
5316 /* Destroy the slot table */
5317 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5319 if (session->fc_slot_table.slots != NULL) {
5320 kfree(session->fc_slot_table.slots);
5321 session->fc_slot_table.slots = NULL;
5323 if (session->bc_slot_table.slots != NULL) {
5324 kfree(session->bc_slot_table.slots);
5325 session->bc_slot_table.slots = NULL;
5331 * Initialize or reset the forechannel and backchannel tables
5333 static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
5335 struct nfs4_slot_table *tbl;
5338 dprintk("--> %s\n", __func__);
5340 tbl = &ses->fc_slot_table;
5341 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5342 if (status) /* -ENOMEM */
5345 tbl = &ses->bc_slot_table;
5346 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5347 if (status && tbl->slots == NULL)
5348 /* Fore and back channel share a connection so get
5349 * both slot tables or neither */
5350 nfs4_destroy_slot_tables(ses);
5354 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5356 struct nfs4_session *session;
5357 struct nfs4_slot_table *tbl;
5359 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5363 tbl = &session->fc_slot_table;
5364 tbl->highest_used_slotid = NFS4_NO_SLOT;
5365 spin_lock_init(&tbl->slot_tbl_lock);
5366 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5367 init_completion(&tbl->complete);
5369 tbl = &session->bc_slot_table;
5370 tbl->highest_used_slotid = NFS4_NO_SLOT;
5371 spin_lock_init(&tbl->slot_tbl_lock);
5372 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5373 init_completion(&tbl->complete);
5375 session->session_state = 1<<NFS4_SESSION_INITING;
5381 void nfs4_destroy_session(struct nfs4_session *session)
5383 struct rpc_xprt *xprt;
5385 nfs4_proc_destroy_session(session);
5388 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5390 dprintk("%s Destroy backchannel for xprt %p\n",
5392 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
5393 nfs4_destroy_slot_tables(session);
5398 * Initialize the values to be used by the client in CREATE_SESSION
5399 * If nfs4_init_session set the fore channel request and response sizes,
5402 * Set the back channel max_resp_sz_cached to zero to force the client to
5403 * always set csa_cachethis to FALSE because the current implementation
5404 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5406 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5408 struct nfs4_session *session = args->client->cl_session;
5409 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5410 mxresp_sz = session->fc_attrs.max_resp_sz;
5413 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5415 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5416 /* Fore channel attributes */
5417 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5418 args->fc_attrs.max_resp_sz = mxresp_sz;
5419 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5420 args->fc_attrs.max_reqs = max_session_slots;
5422 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5423 "max_ops=%u max_reqs=%u\n",
5425 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5426 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5428 /* Back channel attributes */
5429 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5430 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5431 args->bc_attrs.max_resp_sz_cached = 0;
5432 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5433 args->bc_attrs.max_reqs = 1;
5435 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5436 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5438 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5439 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5440 args->bc_attrs.max_reqs);
5443 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5445 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5446 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5448 if (rcvd->max_resp_sz > sent->max_resp_sz)
5451 * Our requested max_ops is the minimum we need; we're not
5452 * prepared to break up compounds into smaller pieces than that.
5453 * So, no point even trying to continue if the server won't
5456 if (rcvd->max_ops < sent->max_ops)
5458 if (rcvd->max_reqs == 0)
5460 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5461 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5465 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5467 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5468 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5470 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5472 if (rcvd->max_resp_sz < sent->max_resp_sz)
5474 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5476 /* These would render the backchannel useless: */
5477 if (rcvd->max_ops != sent->max_ops)
5479 if (rcvd->max_reqs != sent->max_reqs)
5484 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5485 struct nfs4_session *session)
5489 ret = nfs4_verify_fore_channel_attrs(args, session);
5492 return nfs4_verify_back_channel_attrs(args, session);
5495 static int _nfs4_proc_create_session(struct nfs_client *clp)
5497 struct nfs4_session *session = clp->cl_session;
5498 struct nfs41_create_session_args args = {
5500 .cb_program = NFS4_CALLBACK,
5502 struct nfs41_create_session_res res = {
5505 struct rpc_message msg = {
5506 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5512 nfs4_init_channel_attrs(&args);
5513 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5515 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5518 /* Verify the session's negotiated channel_attrs values */
5519 status = nfs4_verify_channel_attrs(&args, session);
5521 /* Increment the clientid slot sequence id */
5529 * Issues a CREATE_SESSION operation to the server.
5530 * It is the responsibility of the caller to verify the session is
5531 * expired before calling this routine.
5533 int nfs4_proc_create_session(struct nfs_client *clp)
5537 struct nfs4_session *session = clp->cl_session;
5539 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5541 status = _nfs4_proc_create_session(clp);
5545 /* Init or reset the session slot tables */
5546 status = nfs4_setup_session_slot_tables(session);
5547 dprintk("slot table setup returned %d\n", status);
5551 ptr = (unsigned *)&session->sess_id.data[0];
5552 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5553 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5555 dprintk("<-- %s\n", __func__);
5560 * Issue the over-the-wire RPC DESTROY_SESSION.
5561 * The caller must serialize access to this routine.
5563 int nfs4_proc_destroy_session(struct nfs4_session *session)
5566 struct rpc_message msg;
5568 dprintk("--> nfs4_proc_destroy_session\n");
5570 /* session is still being setup */
5571 if (session->clp->cl_cons_state != NFS_CS_READY)
5574 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5575 msg.rpc_argp = session;
5576 msg.rpc_resp = NULL;
5577 msg.rpc_cred = NULL;
5578 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5582 "NFS: Got error %d from the server on DESTROY_SESSION. "
5583 "Session has been destroyed regardless...\n", status);
5585 dprintk("<-- nfs4_proc_destroy_session\n");
5589 int nfs4_init_session(struct nfs_server *server)
5591 struct nfs_client *clp = server->nfs_client;
5592 struct nfs4_session *session;
5593 unsigned int rsize, wsize;
5596 if (!nfs4_has_session(clp))
5599 session = clp->cl_session;
5600 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5603 rsize = server->rsize;
5605 rsize = NFS_MAX_FILE_IO_SIZE;
5606 wsize = server->wsize;
5608 wsize = NFS_MAX_FILE_IO_SIZE;
5610 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5611 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5613 ret = nfs4_recover_expired_lease(server);
5615 ret = nfs4_check_client_ready(clp);
5619 int nfs4_init_ds_session(struct nfs_client *clp)
5621 struct nfs4_session *session = clp->cl_session;
5624 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5627 ret = nfs4_client_recover_expired_lease(clp);
5629 /* Test for the DS role */
5630 if (!is_ds_client(clp))
5633 ret = nfs4_check_client_ready(clp);
5637 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5641 * Renew the cl_session lease.
5643 struct nfs4_sequence_data {
5644 struct nfs_client *clp;
5645 struct nfs4_sequence_args args;
5646 struct nfs4_sequence_res res;
5649 static void nfs41_sequence_release(void *data)
5651 struct nfs4_sequence_data *calldata = data;
5652 struct nfs_client *clp = calldata->clp;
5654 if (atomic_read(&clp->cl_count) > 1)
5655 nfs4_schedule_state_renewal(clp);
5656 nfs_put_client(clp);
5660 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5662 switch(task->tk_status) {
5663 case -NFS4ERR_DELAY:
5664 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5667 nfs4_schedule_lease_recovery(clp);
5672 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5674 struct nfs4_sequence_data *calldata = data;
5675 struct nfs_client *clp = calldata->clp;
5677 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5680 if (task->tk_status < 0) {
5681 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5682 if (atomic_read(&clp->cl_count) == 1)
5685 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5686 rpc_restart_call_prepare(task);
5690 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5692 dprintk("<-- %s\n", __func__);
5695 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5697 struct nfs4_sequence_data *calldata = data;
5698 struct nfs_client *clp = calldata->clp;
5699 struct nfs4_sequence_args *args;
5700 struct nfs4_sequence_res *res;
5702 args = task->tk_msg.rpc_argp;
5703 res = task->tk_msg.rpc_resp;
5705 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
5707 rpc_call_start(task);
5710 static const struct rpc_call_ops nfs41_sequence_ops = {
5711 .rpc_call_done = nfs41_sequence_call_done,
5712 .rpc_call_prepare = nfs41_sequence_prepare,
5713 .rpc_release = nfs41_sequence_release,
5716 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5718 struct nfs4_sequence_data *calldata;
5719 struct rpc_message msg = {
5720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5723 struct rpc_task_setup task_setup_data = {
5724 .rpc_client = clp->cl_rpcclient,
5725 .rpc_message = &msg,
5726 .callback_ops = &nfs41_sequence_ops,
5727 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5730 if (!atomic_inc_not_zero(&clp->cl_count))
5731 return ERR_PTR(-EIO);
5732 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5733 if (calldata == NULL) {
5734 nfs_put_client(clp);
5735 return ERR_PTR(-ENOMEM);
5737 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
5738 msg.rpc_argp = &calldata->args;
5739 msg.rpc_resp = &calldata->res;
5740 calldata->clp = clp;
5741 task_setup_data.callback_data = calldata;
5743 return rpc_run_task(&task_setup_data);
5746 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5748 struct rpc_task *task;
5751 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5753 task = _nfs41_proc_sequence(clp, cred);
5755 ret = PTR_ERR(task);
5757 rpc_put_task_async(task);
5758 dprintk("<-- %s status=%d\n", __func__, ret);
5762 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5764 struct rpc_task *task;
5767 task = _nfs41_proc_sequence(clp, cred);
5769 ret = PTR_ERR(task);
5772 ret = rpc_wait_for_completion_task(task);
5774 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5776 if (task->tk_status == 0)
5777 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5778 ret = task->tk_status;
5782 dprintk("<-- %s status=%d\n", __func__, ret);
5786 struct nfs4_reclaim_complete_data {
5787 struct nfs_client *clp;
5788 struct nfs41_reclaim_complete_args arg;
5789 struct nfs41_reclaim_complete_res res;
5792 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5794 struct nfs4_reclaim_complete_data *calldata = data;
5796 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5797 if (nfs41_setup_sequence(calldata->clp->cl_session,
5798 &calldata->arg.seq_args,
5799 &calldata->res.seq_res, task))
5802 rpc_call_start(task);
5805 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5807 switch(task->tk_status) {
5809 case -NFS4ERR_COMPLETE_ALREADY:
5810 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5812 case -NFS4ERR_DELAY:
5813 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5815 case -NFS4ERR_RETRY_UNCACHED_REP:
5818 nfs4_schedule_lease_recovery(clp);
5823 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5825 struct nfs4_reclaim_complete_data *calldata = data;
5826 struct nfs_client *clp = calldata->clp;
5827 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5829 dprintk("--> %s\n", __func__);
5830 if (!nfs41_sequence_done(task, res))
5833 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5834 rpc_restart_call_prepare(task);
5837 dprintk("<-- %s\n", __func__);
5840 static void nfs4_free_reclaim_complete_data(void *data)
5842 struct nfs4_reclaim_complete_data *calldata = data;
5847 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5848 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5849 .rpc_call_done = nfs4_reclaim_complete_done,
5850 .rpc_release = nfs4_free_reclaim_complete_data,
5854 * Issue a global reclaim complete.
5856 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5858 struct nfs4_reclaim_complete_data *calldata;
5859 struct rpc_task *task;
5860 struct rpc_message msg = {
5861 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5863 struct rpc_task_setup task_setup_data = {
5864 .rpc_client = clp->cl_rpcclient,
5865 .rpc_message = &msg,
5866 .callback_ops = &nfs4_reclaim_complete_call_ops,
5867 .flags = RPC_TASK_ASYNC,
5869 int status = -ENOMEM;
5871 dprintk("--> %s\n", __func__);
5872 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5873 if (calldata == NULL)
5875 calldata->clp = clp;
5876 calldata->arg.one_fs = 0;
5878 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
5879 msg.rpc_argp = &calldata->arg;
5880 msg.rpc_resp = &calldata->res;
5881 task_setup_data.callback_data = calldata;
5882 task = rpc_run_task(&task_setup_data);
5884 status = PTR_ERR(task);
5887 status = nfs4_wait_for_completion_rpc_task(task);
5889 status = task->tk_status;
5893 dprintk("<-- %s status=%d\n", __func__, status);
5898 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5900 struct nfs4_layoutget *lgp = calldata;
5901 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5903 dprintk("--> %s\n", __func__);
5904 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5905 * right now covering the LAYOUTGET we are about to send.
5906 * However, that is not so catastrophic, and there seems
5907 * to be no way to prevent it completely.
5909 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5910 &lgp->res.seq_res, task))
5912 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5913 NFS_I(lgp->args.inode)->layout,
5914 lgp->args.ctx->state)) {
5915 rpc_exit(task, NFS4_OK);
5918 rpc_call_start(task);
5921 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5923 struct nfs4_layoutget *lgp = calldata;
5924 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5926 dprintk("--> %s\n", __func__);
5928 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5931 switch (task->tk_status) {
5934 case -NFS4ERR_LAYOUTTRYLATER:
5935 case -NFS4ERR_RECALLCONFLICT:
5936 task->tk_status = -NFS4ERR_DELAY;
5939 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5940 rpc_restart_call_prepare(task);
5944 dprintk("<-- %s\n", __func__);
5947 static void nfs4_layoutget_release(void *calldata)
5949 struct nfs4_layoutget *lgp = calldata;
5951 dprintk("--> %s\n", __func__);
5952 put_nfs_open_context(lgp->args.ctx);
5954 dprintk("<-- %s\n", __func__);
5957 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5958 .rpc_call_prepare = nfs4_layoutget_prepare,
5959 .rpc_call_done = nfs4_layoutget_done,
5960 .rpc_release = nfs4_layoutget_release,
5963 int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5965 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5966 struct rpc_task *task;
5967 struct rpc_message msg = {
5968 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5969 .rpc_argp = &lgp->args,
5970 .rpc_resp = &lgp->res,
5972 struct rpc_task_setup task_setup_data = {
5973 .rpc_client = server->client,
5974 .rpc_message = &msg,
5975 .callback_ops = &nfs4_layoutget_call_ops,
5976 .callback_data = lgp,
5977 .flags = RPC_TASK_ASYNC,
5981 dprintk("--> %s\n", __func__);
5983 lgp->res.layoutp = &lgp->args.layout;
5984 lgp->res.seq_res.sr_slot = NULL;
5985 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
5986 task = rpc_run_task(&task_setup_data);
5988 return PTR_ERR(task);
5989 status = nfs4_wait_for_completion_rpc_task(task);
5991 status = task->tk_status;
5993 status = pnfs_layout_process(lgp);
5995 dprintk("<-- %s status=%d\n", __func__, status);
6000 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6002 struct nfs4_layoutreturn *lrp = calldata;
6004 dprintk("--> %s\n", __func__);
6005 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
6006 &lrp->res.seq_res, task))
6008 rpc_call_start(task);
6011 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6013 struct nfs4_layoutreturn *lrp = calldata;
6014 struct nfs_server *server;
6015 struct pnfs_layout_hdr *lo = lrp->args.layout;
6017 dprintk("--> %s\n", __func__);
6019 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6022 server = NFS_SERVER(lrp->args.inode);
6023 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6024 rpc_restart_call_prepare(task);
6027 spin_lock(&lo->plh_inode->i_lock);
6028 if (task->tk_status == 0) {
6029 if (lrp->res.lrs_present) {
6030 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6032 BUG_ON(!list_empty(&lo->plh_segs));
6034 lo->plh_block_lgets--;
6035 spin_unlock(&lo->plh_inode->i_lock);
6036 dprintk("<-- %s\n", __func__);
6039 static void nfs4_layoutreturn_release(void *calldata)
6041 struct nfs4_layoutreturn *lrp = calldata;
6043 dprintk("--> %s\n", __func__);
6044 put_layout_hdr(lrp->args.layout);
6046 dprintk("<-- %s\n", __func__);
6049 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6050 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6051 .rpc_call_done = nfs4_layoutreturn_done,
6052 .rpc_release = nfs4_layoutreturn_release,
6055 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6057 struct rpc_task *task;
6058 struct rpc_message msg = {
6059 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6060 .rpc_argp = &lrp->args,
6061 .rpc_resp = &lrp->res,
6063 struct rpc_task_setup task_setup_data = {
6064 .rpc_client = lrp->clp->cl_rpcclient,
6065 .rpc_message = &msg,
6066 .callback_ops = &nfs4_layoutreturn_call_ops,
6067 .callback_data = lrp,
6071 dprintk("--> %s\n", __func__);
6072 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
6073 task = rpc_run_task(&task_setup_data);
6075 return PTR_ERR(task);
6076 status = task->tk_status;
6077 dprintk("<-- %s status=%d\n", __func__, status);
6083 * Retrieve the list of Data Server devices from the MDS.
6085 static int _nfs4_getdevicelist(struct nfs_server *server,
6086 const struct nfs_fh *fh,
6087 struct pnfs_devicelist *devlist)
6089 struct nfs4_getdevicelist_args args = {
6091 .layoutclass = server->pnfs_curr_ld->id,
6093 struct nfs4_getdevicelist_res res = {
6096 struct rpc_message msg = {
6097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6103 dprintk("--> %s\n", __func__);
6104 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6106 dprintk("<-- %s status=%d\n", __func__, status);
6110 int nfs4_proc_getdevicelist(struct nfs_server *server,
6111 const struct nfs_fh *fh,
6112 struct pnfs_devicelist *devlist)
6114 struct nfs4_exception exception = { };
6118 err = nfs4_handle_exception(server,
6119 _nfs4_getdevicelist(server, fh, devlist),
6121 } while (exception.retry);
6123 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6124 err, devlist->num_devs);
6128 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6131 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6133 struct nfs4_getdeviceinfo_args args = {
6136 struct nfs4_getdeviceinfo_res res = {
6139 struct rpc_message msg = {
6140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6146 dprintk("--> %s\n", __func__);
6147 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6148 dprintk("<-- %s status=%d\n", __func__, status);
6153 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6155 struct nfs4_exception exception = { };
6159 err = nfs4_handle_exception(server,
6160 _nfs4_proc_getdeviceinfo(server, pdev),
6162 } while (exception.retry);
6165 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6167 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6169 struct nfs4_layoutcommit_data *data = calldata;
6170 struct nfs_server *server = NFS_SERVER(data->args.inode);
6172 if (nfs4_setup_sequence(server, &data->args.seq_args,
6173 &data->res.seq_res, task))
6175 rpc_call_start(task);
6179 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6181 struct nfs4_layoutcommit_data *data = calldata;
6182 struct nfs_server *server = NFS_SERVER(data->args.inode);
6184 if (!nfs4_sequence_done(task, &data->res.seq_res))
6187 switch (task->tk_status) { /* Just ignore these failures */
6188 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6189 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6190 case -NFS4ERR_BADLAYOUT: /* no layout */
6191 case -NFS4ERR_GRACE: /* loca_recalim always false */
6192 task->tk_status = 0;
6195 nfs_post_op_update_inode_force_wcc(data->args.inode,
6199 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6200 rpc_restart_call_prepare(task);
6206 static void nfs4_layoutcommit_release(void *calldata)
6208 struct nfs4_layoutcommit_data *data = calldata;
6209 struct pnfs_layout_segment *lseg, *tmp;
6210 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
6212 pnfs_cleanup_layoutcommit(data);
6213 /* Matched by references in pnfs_set_layoutcommit */
6214 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6215 list_del_init(&lseg->pls_lc_list);
6216 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6221 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6222 smp_mb__after_clear_bit();
6223 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6225 put_rpccred(data->cred);
6229 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6230 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6231 .rpc_call_done = nfs4_layoutcommit_done,
6232 .rpc_release = nfs4_layoutcommit_release,
6236 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6238 struct rpc_message msg = {
6239 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6240 .rpc_argp = &data->args,
6241 .rpc_resp = &data->res,
6242 .rpc_cred = data->cred,
6244 struct rpc_task_setup task_setup_data = {
6245 .task = &data->task,
6246 .rpc_client = NFS_CLIENT(data->args.inode),
6247 .rpc_message = &msg,
6248 .callback_ops = &nfs4_layoutcommit_ops,
6249 .callback_data = data,
6250 .flags = RPC_TASK_ASYNC,
6252 struct rpc_task *task;
6255 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6256 "lbw: %llu inode %lu\n",
6257 data->task.tk_pid, sync,
6258 data->args.lastbytewritten,
6259 data->args.inode->i_ino);
6261 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6262 task = rpc_run_task(&task_setup_data);
6264 return PTR_ERR(task);
6267 status = nfs4_wait_for_completion_rpc_task(task);
6270 status = task->tk_status;
6272 dprintk("%s: status %d\n", __func__, status);
6278 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6279 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6281 struct nfs41_secinfo_no_name_args args = {
6282 .style = SECINFO_STYLE_CURRENT_FH,
6284 struct nfs4_secinfo_res res = {
6287 struct rpc_message msg = {
6288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6292 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6296 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6297 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6299 struct nfs4_exception exception = { };
6302 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6305 case -NFS4ERR_WRONGSEC:
6306 case -NFS4ERR_NOTSUPP:
6309 err = nfs4_handle_exception(server, err, &exception);
6311 } while (exception.retry);
6317 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6318 struct nfs_fsinfo *info)
6322 rpc_authflavor_t flavor;
6323 struct nfs4_secinfo_flavors *flavors;
6325 page = alloc_page(GFP_KERNEL);
6331 flavors = page_address(page);
6332 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6335 * Fall back on "guess and check" method if
6336 * the server doesn't support SECINFO_NO_NAME
6338 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6339 err = nfs4_find_root_sec(server, fhandle, info);
6345 flavor = nfs_find_best_sec(flavors);
6347 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6357 static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6360 struct nfs41_test_stateid_args args = {
6363 struct nfs41_test_stateid_res res;
6364 struct rpc_message msg = {
6365 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6370 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6371 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6373 if (status == NFS_OK)
6378 static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6380 struct nfs4_exception exception = { };
6383 err = nfs4_handle_exception(server,
6384 _nfs41_test_stateid(server, stateid),
6386 } while (exception.retry);
6390 static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6392 struct nfs41_free_stateid_args args = {
6395 struct nfs41_free_stateid_res res;
6396 struct rpc_message msg = {
6397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6402 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6403 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6406 static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6408 struct nfs4_exception exception = { };
6411 err = nfs4_handle_exception(server,
6412 _nfs4_free_stateid(server, stateid),
6414 } while (exception.retry);
6418 static bool nfs41_match_stateid(const nfs4_stateid *s1,
6419 const nfs4_stateid *s2)
6421 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
6424 if (s1->seqid == s2->seqid)
6426 if (s1->seqid == 0 || s2->seqid == 0)
6432 #endif /* CONFIG_NFS_V4_1 */
6434 static bool nfs4_match_stateid(const nfs4_stateid *s1,
6435 const nfs4_stateid *s2)
6437 return nfs4_stateid_match(s1, s2);
6441 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6442 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6443 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6444 .recover_open = nfs4_open_reclaim,
6445 .recover_lock = nfs4_lock_reclaim,
6446 .establish_clid = nfs4_init_clientid,
6447 .get_clid_cred = nfs4_get_setclientid_cred,
6450 #if defined(CONFIG_NFS_V4_1)
6451 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6452 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6453 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6454 .recover_open = nfs4_open_reclaim,
6455 .recover_lock = nfs4_lock_reclaim,
6456 .establish_clid = nfs41_init_clientid,
6457 .get_clid_cred = nfs4_get_exchange_id_cred,
6458 .reclaim_complete = nfs41_proc_reclaim_complete,
6460 #endif /* CONFIG_NFS_V4_1 */
6462 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6463 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6464 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6465 .recover_open = nfs4_open_expired,
6466 .recover_lock = nfs4_lock_expired,
6467 .establish_clid = nfs4_init_clientid,
6468 .get_clid_cred = nfs4_get_setclientid_cred,
6471 #if defined(CONFIG_NFS_V4_1)
6472 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6473 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6474 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6475 .recover_open = nfs41_open_expired,
6476 .recover_lock = nfs41_lock_expired,
6477 .establish_clid = nfs41_init_clientid,
6478 .get_clid_cred = nfs4_get_exchange_id_cred,
6480 #endif /* CONFIG_NFS_V4_1 */
6482 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6483 .sched_state_renewal = nfs4_proc_async_renew,
6484 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6485 .renew_lease = nfs4_proc_renew,
6488 #if defined(CONFIG_NFS_V4_1)
6489 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6490 .sched_state_renewal = nfs41_proc_async_sequence,
6491 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
6492 .renew_lease = nfs4_proc_sequence,
6496 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6498 .call_sync = _nfs4_call_sync,
6499 .match_stateid = nfs4_match_stateid,
6500 .find_root_sec = nfs4_find_root_sec,
6501 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6502 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6503 .state_renewal_ops = &nfs40_state_renewal_ops,
6506 #if defined(CONFIG_NFS_V4_1)
6507 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6509 .call_sync = _nfs4_call_sync_session,
6510 .match_stateid = nfs41_match_stateid,
6511 .find_root_sec = nfs41_find_root_sec,
6512 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6513 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6514 .state_renewal_ops = &nfs41_state_renewal_ops,
6518 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6519 [0] = &nfs_v4_0_minor_ops,
6520 #if defined(CONFIG_NFS_V4_1)
6521 [1] = &nfs_v4_1_minor_ops,
6525 static const struct inode_operations nfs4_file_inode_operations = {
6526 .permission = nfs_permission,
6527 .getattr = nfs_getattr,
6528 .setattr = nfs_setattr,
6529 .getxattr = generic_getxattr,
6530 .setxattr = generic_setxattr,
6531 .listxattr = generic_listxattr,
6532 .removexattr = generic_removexattr,
6535 const struct nfs_rpc_ops nfs_v4_clientops = {
6536 .version = 4, /* protocol version */
6537 .dentry_ops = &nfs4_dentry_operations,
6538 .dir_inode_ops = &nfs4_dir_inode_operations,
6539 .file_inode_ops = &nfs4_file_inode_operations,
6540 .file_ops = &nfs4_file_operations,
6541 .getroot = nfs4_proc_get_root,
6542 .getattr = nfs4_proc_getattr,
6543 .setattr = nfs4_proc_setattr,
6544 .lookup = nfs4_proc_lookup,
6545 .access = nfs4_proc_access,
6546 .readlink = nfs4_proc_readlink,
6547 .create = nfs4_proc_create,
6548 .remove = nfs4_proc_remove,
6549 .unlink_setup = nfs4_proc_unlink_setup,
6550 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
6551 .unlink_done = nfs4_proc_unlink_done,
6552 .rename = nfs4_proc_rename,
6553 .rename_setup = nfs4_proc_rename_setup,
6554 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
6555 .rename_done = nfs4_proc_rename_done,
6556 .link = nfs4_proc_link,
6557 .symlink = nfs4_proc_symlink,
6558 .mkdir = nfs4_proc_mkdir,
6559 .rmdir = nfs4_proc_remove,
6560 .readdir = nfs4_proc_readdir,
6561 .mknod = nfs4_proc_mknod,
6562 .statfs = nfs4_proc_statfs,
6563 .fsinfo = nfs4_proc_fsinfo,
6564 .pathconf = nfs4_proc_pathconf,
6565 .set_capabilities = nfs4_server_capabilities,
6566 .decode_dirent = nfs4_decode_dirent,
6567 .read_setup = nfs4_proc_read_setup,
6568 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
6569 .read_done = nfs4_read_done,
6570 .write_setup = nfs4_proc_write_setup,
6571 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
6572 .write_done = nfs4_write_done,
6573 .commit_setup = nfs4_proc_commit_setup,
6574 .commit_done = nfs4_commit_done,
6575 .lock = nfs4_proc_lock,
6576 .clear_acl_cache = nfs4_zap_acl_attr,
6577 .close_context = nfs4_close_context,
6578 .open_context = nfs4_atomic_open,
6579 .init_client = nfs4_init_client,
6580 .secinfo = nfs4_proc_secinfo,
6583 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6584 .prefix = XATTR_NAME_NFSV4_ACL,
6585 .list = nfs4_xattr_list_nfs4_acl,
6586 .get = nfs4_xattr_get_nfs4_acl,
6587 .set = nfs4_xattr_set_nfs4_acl,
6590 const struct xattr_handler *nfs4_xattr_handlers[] = {
6591 &nfs4_xattr_nfs4_acl_handler,
6595 module_param(max_session_slots, ushort, 0644);
6596 MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6597 "requests the client will negotiate");