4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/sunrpc/gss_api.h>
47 #include <linux/nfs.h>
48 #include <linux/nfs4.h>
49 #include <linux/nfs_fs.h>
50 #include <linux/nfs_page.h>
51 #include <linux/nfs_mount.h>
52 #include <linux/namei.h>
53 #include <linux/mount.h>
54 #include <linux/module.h>
55 #include <linux/nfs_idmap.h>
56 #include <linux/sunrpc/bc_xprt.h>
57 #include <linux/xattr.h>
58 #include <linux/utsname.h>
59 #include <linux/freezer.h>
62 #include "delegation.h"
68 #define NFSDBG_FACILITY NFSDBG_PROC
70 #define NFS4_POLL_RETRY_MIN (HZ/10)
71 #define NFS4_POLL_RETRY_MAX (15*HZ)
73 #define NFS4_MAX_LOOP_ON_RECOVER (10)
75 static unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
78 static int _nfs4_proc_open(struct nfs4_opendata *data);
79 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
80 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
81 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
82 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
83 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
84 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
86 struct nfs4_state *state);
87 #ifdef CONFIG_NFS_V4_1
88 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
89 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
91 /* Prevent leaks of NFSv4 errors into userland */
92 static int nfs4_map_errors(int err)
97 case -NFS4ERR_RESOURCE:
99 case -NFS4ERR_WRONGSEC:
101 case -NFS4ERR_BADOWNER:
102 case -NFS4ERR_BADNAME:
105 dprintk("%s could not handle NFSv4 error %d\n",
113 * This is our standard bitmap for GETATTR requests.
115 const u32 nfs4_fattr_bitmap[2] = {
117 | FATTR4_WORD0_CHANGE
120 | FATTR4_WORD0_FILEID,
122 | FATTR4_WORD1_NUMLINKS
124 | FATTR4_WORD1_OWNER_GROUP
125 | FATTR4_WORD1_RAWDEV
126 | FATTR4_WORD1_SPACE_USED
127 | FATTR4_WORD1_TIME_ACCESS
128 | FATTR4_WORD1_TIME_METADATA
129 | FATTR4_WORD1_TIME_MODIFY
132 const u32 nfs4_statfs_bitmap[2] = {
133 FATTR4_WORD0_FILES_AVAIL
134 | FATTR4_WORD0_FILES_FREE
135 | FATTR4_WORD0_FILES_TOTAL,
136 FATTR4_WORD1_SPACE_AVAIL
137 | FATTR4_WORD1_SPACE_FREE
138 | FATTR4_WORD1_SPACE_TOTAL
141 const u32 nfs4_pathconf_bitmap[2] = {
143 | FATTR4_WORD0_MAXNAME,
147 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
148 | FATTR4_WORD0_MAXREAD
149 | FATTR4_WORD0_MAXWRITE
150 | FATTR4_WORD0_LEASE_TIME,
151 FATTR4_WORD1_TIME_DELTA
152 | FATTR4_WORD1_FS_LAYOUT_TYPES,
153 FATTR4_WORD2_LAYOUT_BLKSIZE
156 const u32 nfs4_fs_locations_bitmap[2] = {
158 | FATTR4_WORD0_CHANGE
161 | FATTR4_WORD0_FILEID
162 | FATTR4_WORD0_FS_LOCATIONS,
164 | FATTR4_WORD1_NUMLINKS
166 | FATTR4_WORD1_OWNER_GROUP
167 | FATTR4_WORD1_RAWDEV
168 | FATTR4_WORD1_SPACE_USED
169 | FATTR4_WORD1_TIME_ACCESS
170 | FATTR4_WORD1_TIME_METADATA
171 | FATTR4_WORD1_TIME_MODIFY
172 | FATTR4_WORD1_MOUNTED_ON_FILEID
175 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
176 struct nfs4_readdir_arg *readdir)
180 BUG_ON(readdir->count < 80);
182 readdir->cookie = cookie;
183 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
188 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
193 * NFSv4 servers do not return entries for '.' and '..'
194 * Therefore, we fake these entries here. We let '.'
195 * have cookie 0 and '..' have cookie 1. Note that
196 * when talking to the server, we always send cookie 0
199 start = p = kmap_atomic(*readdir->pages, KM_USER0);
202 *p++ = xdr_one; /* next */
203 *p++ = xdr_zero; /* cookie, first word */
204 *p++ = xdr_one; /* cookie, second word */
205 *p++ = xdr_one; /* entry len */
206 memcpy(p, ".\0\0\0", 4); /* entry */
208 *p++ = xdr_one; /* bitmap length */
209 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
210 *p++ = htonl(8); /* attribute buffer length */
211 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
214 *p++ = xdr_one; /* next */
215 *p++ = xdr_zero; /* cookie, first word */
216 *p++ = xdr_two; /* cookie, second word */
217 *p++ = xdr_two; /* entry len */
218 memcpy(p, "..\0\0", 4); /* entry */
220 *p++ = xdr_one; /* bitmap length */
221 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
222 *p++ = htonl(8); /* attribute buffer length */
223 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
225 readdir->pgbase = (char *)p - (char *)start;
226 readdir->count -= readdir->pgbase;
227 kunmap_atomic(start, KM_USER0);
230 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
236 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
237 nfs_wait_bit_killable, TASK_KILLABLE);
241 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
248 *timeout = NFS4_POLL_RETRY_MIN;
249 if (*timeout > NFS4_POLL_RETRY_MAX)
250 *timeout = NFS4_POLL_RETRY_MAX;
251 freezable_schedule_timeout_killable(*timeout);
252 if (fatal_signal_pending(current))
258 /* This is the error handling routine for processes that are allowed
261 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
263 struct nfs_client *clp = server->nfs_client;
264 struct nfs4_state *state = exception->state;
265 struct inode *inode = exception->inode;
268 exception->retry = 0;
272 case -NFS4ERR_OPENMODE:
273 if (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:
286 nfs_remove_bad_delegation(state->inode);
289 nfs4_schedule_stateid_recovery(server, state);
290 goto wait_on_recovery;
291 case -NFS4ERR_EXPIRED:
293 nfs4_schedule_stateid_recovery(server, state);
294 case -NFS4ERR_STALE_STATEID:
295 case -NFS4ERR_STALE_CLIENTID:
296 nfs4_schedule_lease_recovery(clp);
297 goto wait_on_recovery;
298 #if defined(CONFIG_NFS_V4_1)
299 case -NFS4ERR_BADSESSION:
300 case -NFS4ERR_BADSLOT:
301 case -NFS4ERR_BAD_HIGH_SLOT:
302 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
303 case -NFS4ERR_DEADSESSION:
304 case -NFS4ERR_SEQ_FALSE_RETRY:
305 case -NFS4ERR_SEQ_MISORDERED:
306 dprintk("%s ERROR: %d Reset session\n", __func__,
308 nfs4_schedule_session_recovery(clp->cl_session);
309 exception->retry = 1;
311 #endif /* defined(CONFIG_NFS_V4_1) */
312 case -NFS4ERR_FILE_OPEN:
313 if (exception->timeout > HZ) {
314 /* We have retried a decent amount, time to
323 ret = nfs4_delay(server->client, &exception->timeout);
326 case -NFS4ERR_RETRY_UNCACHED_REP:
327 case -NFS4ERR_OLD_STATEID:
328 exception->retry = 1;
330 case -NFS4ERR_BADOWNER:
331 /* The following works around a Linux server bug! */
332 case -NFS4ERR_BADNAME:
333 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
334 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
335 exception->retry = 1;
336 printk(KERN_WARNING "NFS: v4 server %s "
337 "does not accept raw "
339 "Reenabling the idmapper.\n",
340 server->nfs_client->cl_hostname);
343 /* We failed to handle the error */
344 return nfs4_map_errors(ret);
346 ret = nfs4_wait_clnt_recover(clp);
348 exception->retry = 1;
353 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
355 spin_lock(&clp->cl_lock);
356 if (time_before(clp->cl_last_renewal,timestamp))
357 clp->cl_last_renewal = timestamp;
358 spin_unlock(&clp->cl_lock);
361 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
363 do_renew_lease(server->nfs_client, timestamp);
366 #if defined(CONFIG_NFS_V4_1)
369 * nfs4_free_slot - free a slot and efficiently update slot table.
371 * freeing a slot is trivially done by clearing its respective bit
373 * If the freed slotid equals highest_used_slotid we want to update it
374 * so that the server would be able to size down the slot table if needed,
375 * otherwise we know that the highest_used_slotid is still in use.
376 * When updating highest_used_slotid there may be "holes" in the bitmap
377 * so we need to scan down from highest_used_slotid to 0 looking for the now
378 * highest slotid in use.
379 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
381 * Must be called while holding tbl->slot_tbl_lock
384 nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
386 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
387 /* clear used bit in bitmap */
388 __clear_bit(slotid, tbl->used_slots);
390 /* update highest_used_slotid when it is freed */
391 if (slotid == tbl->highest_used_slotid) {
392 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
393 if (slotid < tbl->max_slots)
394 tbl->highest_used_slotid = slotid;
396 tbl->highest_used_slotid = NFS4_NO_SLOT;
398 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
399 slotid, tbl->highest_used_slotid);
402 bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
404 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
409 * Signal state manager thread if session fore channel is drained
411 static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
413 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
414 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
415 nfs4_set_task_privileged, NULL);
419 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
422 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
423 complete(&ses->fc_slot_table.complete);
427 * Signal state manager thread if session back channel is drained
429 void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
431 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
432 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
434 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
435 complete(&ses->bc_slot_table.complete);
438 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
440 struct nfs4_slot_table *tbl;
442 tbl = &res->sr_session->fc_slot_table;
444 /* just wake up the next guy waiting since
445 * we may have not consumed a slot after all */
446 dprintk("%s: No slot\n", __func__);
450 spin_lock(&tbl->slot_tbl_lock);
451 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
452 nfs4_check_drain_fc_complete(res->sr_session);
453 spin_unlock(&tbl->slot_tbl_lock);
457 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
459 unsigned long timestamp;
460 struct nfs_client *clp;
463 * sr_status remains 1 if an RPC level error occurred. The server
464 * may or may not have processed the sequence operation..
465 * Proceed as if the server received and processed the sequence
468 if (res->sr_status == 1)
469 res->sr_status = NFS_OK;
471 /* don't increment the sequence number if the task wasn't sent */
472 if (!RPC_WAS_SENT(task))
475 /* Check the SEQUENCE operation status */
476 switch (res->sr_status) {
478 /* Update the slot's sequence and clientid lease timer */
479 ++res->sr_slot->seq_nr;
480 timestamp = res->sr_renewal_time;
481 clp = res->sr_session->clp;
482 do_renew_lease(clp, timestamp);
483 /* Check sequence flags */
484 if (res->sr_status_flags != 0)
485 nfs4_schedule_lease_recovery(clp);
488 /* The server detected a resend of the RPC call and
489 * returned NFS4ERR_DELAY as per Section 2.10.6.2
492 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
494 res->sr_slot - res->sr_session->fc_slot_table.slots,
495 res->sr_slot->seq_nr);
498 /* Just update the slot sequence no. */
499 ++res->sr_slot->seq_nr;
502 /* The session may be reset by one of the error handlers. */
503 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
504 nfs41_sequence_free_slot(res);
507 if (!rpc_restart_call(task))
509 rpc_delay(task, NFS4_POLL_RETRY_MAX);
513 static int nfs4_sequence_done(struct rpc_task *task,
514 struct nfs4_sequence_res *res)
516 if (res->sr_session == NULL)
518 return nfs41_sequence_done(task, res);
522 * nfs4_find_slot - efficiently look for a free slot
524 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
525 * If found, we mark the slot as used, update the highest_used_slotid,
526 * and respectively set up the sequence operation args.
527 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
529 * Note: must be called with under the slot_tbl_lock.
532 nfs4_find_slot(struct nfs4_slot_table *tbl)
535 u32 ret_id = NFS4_NO_SLOT;
537 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
538 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
540 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
541 if (slotid >= tbl->max_slots)
543 __set_bit(slotid, tbl->used_slots);
544 if (slotid > tbl->highest_used_slotid ||
545 tbl->highest_used_slotid == NFS4_NO_SLOT)
546 tbl->highest_used_slotid = slotid;
549 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
550 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
554 static void nfs41_init_sequence(struct nfs4_sequence_args *args,
555 struct nfs4_sequence_res *res, int cache_reply)
557 args->sa_session = NULL;
558 args->sa_cache_this = 0;
560 args->sa_cache_this = 1;
561 res->sr_session = NULL;
565 int nfs41_setup_sequence(struct nfs4_session *session,
566 struct nfs4_sequence_args *args,
567 struct nfs4_sequence_res *res,
568 struct rpc_task *task)
570 struct nfs4_slot *slot;
571 struct nfs4_slot_table *tbl;
574 dprintk("--> %s\n", __func__);
575 /* slot already allocated? */
576 if (res->sr_slot != NULL)
579 tbl = &session->fc_slot_table;
581 spin_lock(&tbl->slot_tbl_lock);
582 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
583 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
584 /* The state manager will wait until the slot table is empty */
585 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
586 spin_unlock(&tbl->slot_tbl_lock);
587 dprintk("%s session is draining\n", __func__);
591 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
592 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
593 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
594 spin_unlock(&tbl->slot_tbl_lock);
595 dprintk("%s enforce FIFO order\n", __func__);
599 slotid = nfs4_find_slot(tbl);
600 if (slotid == NFS4_NO_SLOT) {
601 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
602 spin_unlock(&tbl->slot_tbl_lock);
603 dprintk("<-- %s: no free slots\n", __func__);
606 spin_unlock(&tbl->slot_tbl_lock);
608 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
609 slot = tbl->slots + slotid;
610 args->sa_session = session;
611 args->sa_slotid = slotid;
613 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
615 res->sr_session = session;
617 res->sr_renewal_time = jiffies;
618 res->sr_status_flags = 0;
620 * sr_status is only set in decode_sequence, and so will remain
621 * set to 1 if an rpc level failure occurs.
626 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
628 int nfs4_setup_sequence(const struct nfs_server *server,
629 struct nfs4_sequence_args *args,
630 struct nfs4_sequence_res *res,
631 struct rpc_task *task)
633 struct nfs4_session *session = nfs4_get_session(server);
639 dprintk("--> %s clp %p session %p sr_slot %td\n",
640 __func__, session->clp, session, res->sr_slot ?
641 res->sr_slot - session->fc_slot_table.slots : -1);
643 ret = nfs41_setup_sequence(session, args, res, task);
645 dprintk("<-- %s status=%d\n", __func__, ret);
649 struct nfs41_call_sync_data {
650 const struct nfs_server *seq_server;
651 struct nfs4_sequence_args *seq_args;
652 struct nfs4_sequence_res *seq_res;
655 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
657 struct nfs41_call_sync_data *data = calldata;
659 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
661 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
662 data->seq_res, task))
664 rpc_call_start(task);
667 static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
669 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
670 nfs41_call_sync_prepare(task, calldata);
673 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
675 struct nfs41_call_sync_data *data = calldata;
677 nfs41_sequence_done(task, data->seq_res);
680 struct rpc_call_ops nfs41_call_sync_ops = {
681 .rpc_call_prepare = nfs41_call_sync_prepare,
682 .rpc_call_done = nfs41_call_sync_done,
685 struct rpc_call_ops nfs41_call_priv_sync_ops = {
686 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
687 .rpc_call_done = nfs41_call_sync_done,
690 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
691 struct nfs_server *server,
692 struct rpc_message *msg,
693 struct nfs4_sequence_args *args,
694 struct nfs4_sequence_res *res,
698 struct rpc_task *task;
699 struct nfs41_call_sync_data data = {
700 .seq_server = server,
704 struct rpc_task_setup task_setup = {
707 .callback_ops = &nfs41_call_sync_ops,
708 .callback_data = &data
712 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
713 task = rpc_run_task(&task_setup);
717 ret = task->tk_status;
723 int _nfs4_call_sync_session(struct rpc_clnt *clnt,
724 struct nfs_server *server,
725 struct rpc_message *msg,
726 struct nfs4_sequence_args *args,
727 struct nfs4_sequence_res *res,
730 nfs41_init_sequence(args, res, cache_reply);
731 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
736 void nfs41_init_sequence(struct nfs4_sequence_args *args,
737 struct nfs4_sequence_res *res, int cache_reply)
741 static int nfs4_sequence_done(struct rpc_task *task,
742 struct nfs4_sequence_res *res)
746 #endif /* CONFIG_NFS_V4_1 */
748 int _nfs4_call_sync(struct rpc_clnt *clnt,
749 struct nfs_server *server,
750 struct rpc_message *msg,
751 struct nfs4_sequence_args *args,
752 struct nfs4_sequence_res *res,
755 nfs41_init_sequence(args, res, cache_reply);
756 return rpc_call_sync(clnt, msg, 0);
760 int nfs4_call_sync(struct rpc_clnt *clnt,
761 struct nfs_server *server,
762 struct rpc_message *msg,
763 struct nfs4_sequence_args *args,
764 struct nfs4_sequence_res *res,
767 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
768 args, res, cache_reply);
771 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
773 struct nfs_inode *nfsi = NFS_I(dir);
775 spin_lock(&dir->i_lock);
776 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
777 if (!cinfo->atomic || cinfo->before != dir->i_version)
778 nfs_force_lookup_revalidate(dir);
779 dir->i_version = cinfo->after;
780 spin_unlock(&dir->i_lock);
783 struct nfs4_opendata {
785 struct nfs_openargs o_arg;
786 struct nfs_openres o_res;
787 struct nfs_open_confirmargs c_arg;
788 struct nfs_open_confirmres c_res;
789 struct nfs4_string owner_name;
790 struct nfs4_string group_name;
791 struct nfs_fattr f_attr;
792 struct nfs_fattr dir_attr;
794 struct dentry *dentry;
795 struct nfs4_state_owner *owner;
796 struct nfs4_state *state;
798 unsigned long timestamp;
799 unsigned int rpc_done : 1;
805 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
807 p->o_res.f_attr = &p->f_attr;
808 p->o_res.dir_attr = &p->dir_attr;
809 p->o_res.seqid = p->o_arg.seqid;
810 p->c_res.seqid = p->c_arg.seqid;
811 p->o_res.server = p->o_arg.server;
812 nfs_fattr_init(&p->f_attr);
813 nfs_fattr_init(&p->dir_attr);
814 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
817 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
818 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
819 const struct iattr *attrs,
822 struct dentry *parent = dget_parent(dentry);
823 struct inode *dir = parent->d_inode;
824 struct nfs_server *server = NFS_SERVER(dir);
825 struct nfs4_opendata *p;
827 p = kzalloc(sizeof(*p), gfp_mask);
830 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
831 if (p->o_arg.seqid == NULL)
833 nfs_sb_active(dentry->d_sb);
834 p->dentry = dget(dentry);
837 atomic_inc(&sp->so_count);
838 p->o_arg.fh = NFS_FH(dir);
839 p->o_arg.open_flags = flags;
840 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
841 p->o_arg.clientid = server->nfs_client->cl_clientid;
842 p->o_arg.id = 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 sequence id. */
1471 data->o_arg.id = sp->so_seqid.owner_id;
1472 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1473 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1474 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1475 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1477 data->timestamp = jiffies;
1478 if (nfs4_setup_sequence(data->o_arg.server,
1479 &data->o_arg.seq_args,
1480 &data->o_res.seq_res, task))
1482 rpc_call_start(task);
1487 task->tk_action = NULL;
1491 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1493 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1494 nfs4_open_prepare(task, calldata);
1497 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1499 struct nfs4_opendata *data = calldata;
1501 data->rpc_status = task->tk_status;
1503 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1506 if (task->tk_status == 0) {
1507 switch (data->o_res.f_attr->mode & S_IFMT) {
1511 data->rpc_status = -ELOOP;
1514 data->rpc_status = -EISDIR;
1517 data->rpc_status = -ENOTDIR;
1519 renew_lease(data->o_res.server, data->timestamp);
1520 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1521 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1526 static void nfs4_open_release(void *calldata)
1528 struct nfs4_opendata *data = calldata;
1529 struct nfs4_state *state = NULL;
1531 /* If this request hasn't been cancelled, do nothing */
1532 if (data->cancelled == 0)
1534 /* In case of error, no cleanup! */
1535 if (data->rpc_status != 0 || !data->rpc_done)
1537 /* In case we need an open_confirm, no cleanup! */
1538 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1540 state = nfs4_opendata_to_nfs4_state(data);
1542 nfs4_close_state(state, data->o_arg.fmode);
1544 nfs4_opendata_put(data);
1547 static const struct rpc_call_ops nfs4_open_ops = {
1548 .rpc_call_prepare = nfs4_open_prepare,
1549 .rpc_call_done = nfs4_open_done,
1550 .rpc_release = nfs4_open_release,
1553 static const struct rpc_call_ops nfs4_recover_open_ops = {
1554 .rpc_call_prepare = nfs4_recover_open_prepare,
1555 .rpc_call_done = nfs4_open_done,
1556 .rpc_release = nfs4_open_release,
1559 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1561 struct inode *dir = data->dir->d_inode;
1562 struct nfs_server *server = NFS_SERVER(dir);
1563 struct nfs_openargs *o_arg = &data->o_arg;
1564 struct nfs_openres *o_res = &data->o_res;
1565 struct rpc_task *task;
1566 struct rpc_message msg = {
1567 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1570 .rpc_cred = data->owner->so_cred,
1572 struct rpc_task_setup task_setup_data = {
1573 .rpc_client = server->client,
1574 .rpc_message = &msg,
1575 .callback_ops = &nfs4_open_ops,
1576 .callback_data = data,
1577 .workqueue = nfsiod_workqueue,
1578 .flags = RPC_TASK_ASYNC,
1582 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1583 kref_get(&data->kref);
1585 data->rpc_status = 0;
1586 data->cancelled = 0;
1588 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1589 task = rpc_run_task(&task_setup_data);
1591 return PTR_ERR(task);
1592 status = nfs4_wait_for_completion_rpc_task(task);
1594 data->cancelled = 1;
1597 status = data->rpc_status;
1603 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1605 struct inode *dir = data->dir->d_inode;
1606 struct nfs_openres *o_res = &data->o_res;
1609 status = nfs4_run_open_task(data, 1);
1610 if (status != 0 || !data->rpc_done)
1613 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1615 nfs_refresh_inode(dir, o_res->dir_attr);
1617 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1618 status = _nfs4_proc_open_confirm(data);
1627 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1629 static int _nfs4_proc_open(struct nfs4_opendata *data)
1631 struct inode *dir = data->dir->d_inode;
1632 struct nfs_server *server = NFS_SERVER(dir);
1633 struct nfs_openargs *o_arg = &data->o_arg;
1634 struct nfs_openres *o_res = &data->o_res;
1637 status = nfs4_run_open_task(data, 0);
1638 if (!data->rpc_done)
1641 if (status == -NFS4ERR_BADNAME &&
1642 !(o_arg->open_flags & O_CREAT))
1647 nfs_fattr_map_and_free_names(server, &data->f_attr);
1649 if (o_arg->open_flags & O_CREAT) {
1650 update_changeattr(dir, &o_res->cinfo);
1651 nfs_post_op_update_inode(dir, o_res->dir_attr);
1653 nfs_refresh_inode(dir, o_res->dir_attr);
1654 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1655 server->caps &= ~NFS_CAP_POSIX_LOCK;
1656 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1657 status = _nfs4_proc_open_confirm(data);
1661 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1662 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1666 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1671 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1672 ret = nfs4_wait_clnt_recover(clp);
1675 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1676 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1678 nfs4_schedule_state_manager(clp);
1684 static int nfs4_recover_expired_lease(struct nfs_server *server)
1686 return nfs4_client_recover_expired_lease(server->nfs_client);
1691 * reclaim state on the server after a network partition.
1692 * Assumes caller holds the appropriate lock
1694 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1696 struct nfs4_opendata *opendata;
1699 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1700 if (IS_ERR(opendata))
1701 return PTR_ERR(opendata);
1702 ret = nfs4_open_recover(opendata, state);
1704 d_drop(ctx->dentry);
1705 nfs4_opendata_put(opendata);
1709 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1711 struct nfs_server *server = NFS_SERVER(state->inode);
1712 struct nfs4_exception exception = { };
1716 err = _nfs4_open_expired(ctx, state);
1720 case -NFS4ERR_GRACE:
1721 case -NFS4ERR_DELAY:
1722 nfs4_handle_exception(server, err, &exception);
1725 } while (exception.retry);
1730 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1732 struct nfs_open_context *ctx;
1735 ctx = nfs4_state_find_open_context(state);
1737 return PTR_ERR(ctx);
1738 ret = nfs4_do_open_expired(ctx, state);
1739 put_nfs_open_context(ctx);
1743 #if defined(CONFIG_NFS_V4_1)
1744 static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
1746 int status = NFS_OK;
1747 struct nfs_server *server = NFS_SERVER(state->inode);
1749 if (state->flags & flags) {
1750 status = nfs41_test_stateid(server, stateid);
1751 if (status != NFS_OK) {
1752 nfs41_free_stateid(server, stateid);
1753 state->flags &= ~flags;
1759 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1761 int deleg_status, open_status;
1762 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1763 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1765 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1766 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1768 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1770 return nfs4_open_expired(sp, state);
1775 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1776 * fields corresponding to attributes that were used to store the verifier.
1777 * Make sure we clobber those fields in the later setattr call
1779 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1781 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1782 !(sattr->ia_valid & ATTR_ATIME_SET))
1783 sattr->ia_valid |= ATTR_ATIME;
1785 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1786 !(sattr->ia_valid & ATTR_MTIME_SET))
1787 sattr->ia_valid |= ATTR_MTIME;
1791 * Returns a referenced nfs4_state
1793 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)
1795 struct nfs4_state_owner *sp;
1796 struct nfs4_state *state = NULL;
1797 struct nfs_server *server = NFS_SERVER(dir);
1798 struct nfs4_opendata *opendata;
1801 /* Protect against reboot recovery conflicts */
1803 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1805 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1808 status = nfs4_recover_expired_lease(server);
1810 goto err_put_state_owner;
1811 if (dentry->d_inode != NULL)
1812 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1814 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1815 if (opendata == NULL)
1816 goto err_put_state_owner;
1818 if (dentry->d_inode != NULL)
1819 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1821 status = _nfs4_proc_open(opendata);
1823 goto err_opendata_put;
1825 state = nfs4_opendata_to_nfs4_state(opendata);
1826 status = PTR_ERR(state);
1828 goto err_opendata_put;
1829 if (server->caps & NFS_CAP_POSIX_LOCK)
1830 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1832 if (opendata->o_arg.open_flags & O_EXCL) {
1833 nfs4_exclusive_attrset(opendata, sattr);
1835 nfs_fattr_init(opendata->o_res.f_attr);
1836 status = nfs4_do_setattr(state->inode, cred,
1837 opendata->o_res.f_attr, sattr,
1840 nfs_setattr_update_inode(state->inode, sattr);
1841 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1843 nfs4_opendata_put(opendata);
1844 nfs4_put_state_owner(sp);
1848 nfs4_opendata_put(opendata);
1849 err_put_state_owner:
1850 nfs4_put_state_owner(sp);
1857 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)
1859 struct nfs4_exception exception = { };
1860 struct nfs4_state *res;
1864 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
1867 /* NOTE: BAD_SEQID means the server and client disagree about the
1868 * book-keeping w.r.t. state-changing operations
1869 * (OPEN/CLOSE/LOCK/LOCKU...)
1870 * It is actually a sign of a bug on the client or on the server.
1872 * If we receive a BAD_SEQID error in the particular case of
1873 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1874 * have unhashed the old state_owner for us, and that we can
1875 * therefore safely retry using a new one. We should still warn
1876 * the user though...
1878 if (status == -NFS4ERR_BAD_SEQID) {
1879 printk(KERN_WARNING "NFS: v4 server %s "
1880 " returned a bad sequence-id error!\n",
1881 NFS_SERVER(dir)->nfs_client->cl_hostname);
1882 exception.retry = 1;
1886 * BAD_STATEID on OPEN means that the server cancelled our
1887 * state before it received the OPEN_CONFIRM.
1888 * Recover by retrying the request as per the discussion
1889 * on Page 181 of RFC3530.
1891 if (status == -NFS4ERR_BAD_STATEID) {
1892 exception.retry = 1;
1895 if (status == -EAGAIN) {
1896 /* We must have found a delegation */
1897 exception.retry = 1;
1900 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1901 status, &exception));
1902 } while (exception.retry);
1906 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1907 struct nfs_fattr *fattr, struct iattr *sattr,
1908 struct nfs4_state *state)
1910 struct nfs_server *server = NFS_SERVER(inode);
1911 struct nfs_setattrargs arg = {
1912 .fh = NFS_FH(inode),
1915 .bitmask = server->attr_bitmask,
1917 struct nfs_setattrres res = {
1921 struct rpc_message msg = {
1922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1927 unsigned long timestamp = jiffies;
1930 nfs_fattr_init(fattr);
1932 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1933 /* Use that stateid */
1934 } else if (state != NULL) {
1935 nfs4_select_rw_stateid(&arg.stateid, state, current->files, current->tgid);
1937 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
1939 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1940 if (status == 0 && state != NULL)
1941 renew_lease(server, timestamp);
1945 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1946 struct nfs_fattr *fattr, struct iattr *sattr,
1947 struct nfs4_state *state)
1949 struct nfs_server *server = NFS_SERVER(inode);
1950 struct nfs4_exception exception = {
1956 err = nfs4_handle_exception(server,
1957 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1959 } while (exception.retry);
1963 struct nfs4_closedata {
1964 struct inode *inode;
1965 struct nfs4_state *state;
1966 struct nfs_closeargs arg;
1967 struct nfs_closeres res;
1968 struct nfs_fattr fattr;
1969 unsigned long timestamp;
1974 static void nfs4_free_closedata(void *data)
1976 struct nfs4_closedata *calldata = data;
1977 struct nfs4_state_owner *sp = calldata->state->owner;
1978 struct super_block *sb = calldata->state->inode->i_sb;
1981 pnfs_roc_release(calldata->state->inode);
1982 nfs4_put_open_state(calldata->state);
1983 nfs_free_seqid(calldata->arg.seqid);
1984 nfs4_put_state_owner(sp);
1985 nfs_sb_deactive(sb);
1989 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1992 spin_lock(&state->owner->so_lock);
1993 if (!(fmode & FMODE_READ))
1994 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1995 if (!(fmode & FMODE_WRITE))
1996 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1997 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1998 spin_unlock(&state->owner->so_lock);
2001 static void nfs4_close_done(struct rpc_task *task, void *data)
2003 struct nfs4_closedata *calldata = data;
2004 struct nfs4_state *state = calldata->state;
2005 struct nfs_server *server = NFS_SERVER(calldata->inode);
2007 dprintk("%s: begin!\n", __func__);
2008 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2010 /* hmm. we are done with the inode, and in the process of freeing
2011 * the state_owner. we keep this around to process errors
2013 switch (task->tk_status) {
2016 pnfs_roc_set_barrier(state->inode,
2017 calldata->roc_barrier);
2018 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2019 renew_lease(server, calldata->timestamp);
2020 nfs4_close_clear_stateid_flags(state,
2021 calldata->arg.fmode);
2023 case -NFS4ERR_STALE_STATEID:
2024 case -NFS4ERR_OLD_STATEID:
2025 case -NFS4ERR_BAD_STATEID:
2026 case -NFS4ERR_EXPIRED:
2027 if (calldata->arg.fmode == 0)
2030 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2031 rpc_restart_call_prepare(task);
2033 nfs_release_seqid(calldata->arg.seqid);
2034 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2035 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2038 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2040 struct nfs4_closedata *calldata = data;
2041 struct nfs4_state *state = calldata->state;
2044 dprintk("%s: begin!\n", __func__);
2045 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2048 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2049 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2050 spin_lock(&state->owner->so_lock);
2051 /* Calculate the change in open mode */
2052 if (state->n_rdwr == 0) {
2053 if (state->n_rdonly == 0) {
2054 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2055 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2056 calldata->arg.fmode &= ~FMODE_READ;
2058 if (state->n_wronly == 0) {
2059 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2060 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2061 calldata->arg.fmode &= ~FMODE_WRITE;
2064 spin_unlock(&state->owner->so_lock);
2067 /* Note: exit _without_ calling nfs4_close_done */
2068 task->tk_action = NULL;
2072 if (calldata->arg.fmode == 0) {
2073 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2074 if (calldata->roc &&
2075 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2076 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2082 nfs_fattr_init(calldata->res.fattr);
2083 calldata->timestamp = jiffies;
2084 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
2085 &calldata->arg.seq_args,
2086 &calldata->res.seq_res,
2089 rpc_call_start(task);
2091 dprintk("%s: done!\n", __func__);
2094 static const struct rpc_call_ops nfs4_close_ops = {
2095 .rpc_call_prepare = nfs4_close_prepare,
2096 .rpc_call_done = nfs4_close_done,
2097 .rpc_release = nfs4_free_closedata,
2101 * It is possible for data to be read/written from a mem-mapped file
2102 * after the sys_close call (which hits the vfs layer as a flush).
2103 * This means that we can't safely call nfsv4 close on a file until
2104 * the inode is cleared. This in turn means that we are not good
2105 * NFSv4 citizens - we do not indicate to the server to update the file's
2106 * share state even when we are done with one of the three share
2107 * stateid's in the inode.
2109 * NOTE: Caller must be holding the sp->so_owner semaphore!
2111 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
2113 struct nfs_server *server = NFS_SERVER(state->inode);
2114 struct nfs4_closedata *calldata;
2115 struct nfs4_state_owner *sp = state->owner;
2116 struct rpc_task *task;
2117 struct rpc_message msg = {
2118 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2119 .rpc_cred = state->owner->so_cred,
2121 struct rpc_task_setup task_setup_data = {
2122 .rpc_client = server->client,
2123 .rpc_message = &msg,
2124 .callback_ops = &nfs4_close_ops,
2125 .workqueue = nfsiod_workqueue,
2126 .flags = RPC_TASK_ASYNC,
2128 int status = -ENOMEM;
2130 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2131 if (calldata == NULL)
2133 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2134 calldata->inode = state->inode;
2135 calldata->state = state;
2136 calldata->arg.fh = NFS_FH(state->inode);
2137 calldata->arg.stateid = &state->open_stateid;
2138 /* Serialization for the sequence id */
2139 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2140 if (calldata->arg.seqid == NULL)
2141 goto out_free_calldata;
2142 calldata->arg.fmode = 0;
2143 calldata->arg.bitmask = server->cache_consistency_bitmask;
2144 calldata->res.fattr = &calldata->fattr;
2145 calldata->res.seqid = calldata->arg.seqid;
2146 calldata->res.server = server;
2147 calldata->roc = roc;
2148 nfs_sb_active(calldata->inode->i_sb);
2150 msg.rpc_argp = &calldata->arg;
2151 msg.rpc_resp = &calldata->res;
2152 task_setup_data.callback_data = calldata;
2153 task = rpc_run_task(&task_setup_data);
2155 return PTR_ERR(task);
2158 status = rpc_wait_for_completion_task(task);
2165 pnfs_roc_release(state->inode);
2166 nfs4_put_open_state(state);
2167 nfs4_put_state_owner(sp);
2171 static struct inode *
2172 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2174 struct nfs4_state *state;
2176 /* Protect against concurrent sillydeletes */
2177 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
2179 return ERR_CAST(state);
2181 return igrab(state->inode);
2184 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2186 if (ctx->state == NULL)
2189 nfs4_close_sync(ctx->state, ctx->mode);
2191 nfs4_close_state(ctx->state, ctx->mode);
2194 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2196 struct nfs4_server_caps_arg args = {
2199 struct nfs4_server_caps_res res = {};
2200 struct rpc_message msg = {
2201 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2207 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2209 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2210 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2211 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2212 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2213 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2214 NFS_CAP_CTIME|NFS_CAP_MTIME);
2215 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2216 server->caps |= NFS_CAP_ACLS;
2217 if (res.has_links != 0)
2218 server->caps |= NFS_CAP_HARDLINKS;
2219 if (res.has_symlinks != 0)
2220 server->caps |= NFS_CAP_SYMLINKS;
2221 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2222 server->caps |= NFS_CAP_FILEID;
2223 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2224 server->caps |= NFS_CAP_MODE;
2225 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2226 server->caps |= NFS_CAP_NLINK;
2227 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2228 server->caps |= NFS_CAP_OWNER;
2229 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2230 server->caps |= NFS_CAP_OWNER_GROUP;
2231 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2232 server->caps |= NFS_CAP_ATIME;
2233 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2234 server->caps |= NFS_CAP_CTIME;
2235 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2236 server->caps |= NFS_CAP_MTIME;
2238 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2239 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2240 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2241 server->acl_bitmask = res.acl_bitmask;
2242 server->fh_expire_type = res.fh_expire_type;
2248 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2250 struct nfs4_exception exception = { };
2253 err = nfs4_handle_exception(server,
2254 _nfs4_server_capabilities(server, fhandle),
2256 } while (exception.retry);
2260 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2261 struct nfs_fsinfo *info)
2263 struct nfs4_lookup_root_arg args = {
2264 .bitmask = nfs4_fattr_bitmap,
2266 struct nfs4_lookup_res res = {
2268 .fattr = info->fattr,
2271 struct rpc_message msg = {
2272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2277 nfs_fattr_init(info->fattr);
2278 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2281 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2282 struct nfs_fsinfo *info)
2284 struct nfs4_exception exception = { };
2287 err = _nfs4_lookup_root(server, fhandle, info);
2290 case -NFS4ERR_WRONGSEC:
2293 err = nfs4_handle_exception(server, err, &exception);
2295 } while (exception.retry);
2299 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2300 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2302 struct rpc_auth *auth;
2305 auth = rpcauth_create(flavor, server->client);
2310 ret = nfs4_lookup_root(server, fhandle, info);
2315 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2316 struct nfs_fsinfo *info)
2318 int i, len, status = 0;
2319 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2321 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2322 flav_array[len] = RPC_AUTH_NULL;
2325 for (i = 0; i < len; i++) {
2326 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2327 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2332 * -EACCESS could mean that the user doesn't have correct permissions
2333 * to access the mount. It could also mean that we tried to mount
2334 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2335 * existing mount programs don't handle -EACCES very well so it should
2336 * be mapped to -EPERM instead.
2338 if (status == -EACCES)
2344 * get the file handle for the "/" directory on the server
2346 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2347 struct nfs_fsinfo *info)
2349 int minor_version = server->nfs_client->cl_minorversion;
2350 int status = nfs4_lookup_root(server, fhandle, info);
2351 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2353 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2354 * by nfs4_map_errors() as this function exits.
2356 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2358 status = nfs4_server_capabilities(server, fhandle);
2360 status = nfs4_do_fsinfo(server, fhandle, info);
2361 return nfs4_map_errors(status);
2365 * Get locations and (maybe) other attributes of a referral.
2366 * Note that we'll actually follow the referral later when
2367 * we detect fsid mismatch in inode revalidation
2369 static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2370 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2372 int status = -ENOMEM;
2373 struct page *page = NULL;
2374 struct nfs4_fs_locations *locations = NULL;
2376 page = alloc_page(GFP_KERNEL);
2379 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2380 if (locations == NULL)
2383 status = nfs4_proc_fs_locations(dir, name, locations, page);
2386 /* Make sure server returned a different fsid for the referral */
2387 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2388 dprintk("%s: server did not return a different fsid for"
2389 " a referral at %s\n", __func__, name->name);
2393 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2394 nfs_fixup_referral_attributes(&locations->fattr);
2396 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2397 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2398 memset(fhandle, 0, sizeof(struct nfs_fh));
2406 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2408 struct nfs4_getattr_arg args = {
2410 .bitmask = server->attr_bitmask,
2412 struct nfs4_getattr_res res = {
2416 struct rpc_message msg = {
2417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2422 nfs_fattr_init(fattr);
2423 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2426 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2428 struct nfs4_exception exception = { };
2431 err = nfs4_handle_exception(server,
2432 _nfs4_proc_getattr(server, fhandle, fattr),
2434 } while (exception.retry);
2439 * The file is not closed if it is opened due to the a request to change
2440 * the size of the file. The open call will not be needed once the
2441 * VFS layer lookup-intents are implemented.
2443 * Close is called when the inode is destroyed.
2444 * If we haven't opened the file for O_WRONLY, we
2445 * need to in the size_change case to obtain a stateid.
2448 * Because OPEN is always done by name in nfsv4, it is
2449 * possible that we opened a different file by the same
2450 * name. We can recognize this race condition, but we
2451 * can't do anything about it besides returning an error.
2453 * This will be fixed with VFS changes (lookup-intent).
2456 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2457 struct iattr *sattr)
2459 struct inode *inode = dentry->d_inode;
2460 struct rpc_cred *cred = NULL;
2461 struct nfs4_state *state = NULL;
2464 if (pnfs_ld_layoutret_on_setattr(inode))
2465 pnfs_return_layout(inode);
2467 nfs_fattr_init(fattr);
2469 /* Search for an existing open(O_WRITE) file */
2470 if (sattr->ia_valid & ATTR_FILE) {
2471 struct nfs_open_context *ctx;
2473 ctx = nfs_file_open_context(sattr->ia_file);
2480 /* Deal with open(O_TRUNC) */
2481 if (sattr->ia_valid & ATTR_OPEN)
2482 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2484 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2486 nfs_setattr_update_inode(inode, sattr);
2490 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2491 const struct qstr *name, struct nfs_fh *fhandle,
2492 struct nfs_fattr *fattr)
2494 struct nfs_server *server = NFS_SERVER(dir);
2496 struct nfs4_lookup_arg args = {
2497 .bitmask = server->attr_bitmask,
2498 .dir_fh = NFS_FH(dir),
2501 struct nfs4_lookup_res res = {
2506 struct rpc_message msg = {
2507 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2512 nfs_fattr_init(fattr);
2514 dprintk("NFS call lookup %s\n", name->name);
2515 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2516 dprintk("NFS reply lookup: %d\n", status);
2520 void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2522 memset(fh, 0, sizeof(struct nfs_fh));
2523 fattr->fsid.major = 1;
2524 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2525 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2526 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2530 static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2531 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2533 struct nfs4_exception exception = { };
2538 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2540 case -NFS4ERR_BADNAME:
2542 case -NFS4ERR_MOVED:
2543 return nfs4_get_referral(dir, name, fattr, fhandle);
2544 case -NFS4ERR_WRONGSEC:
2545 nfs_fixup_secinfo_attributes(fattr, fhandle);
2547 err = nfs4_handle_exception(NFS_SERVER(dir),
2548 status, &exception);
2549 } while (exception.retry);
2553 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2555 struct nfs_server *server = NFS_SERVER(inode);
2556 struct nfs4_accessargs args = {
2557 .fh = NFS_FH(inode),
2558 .bitmask = server->cache_consistency_bitmask,
2560 struct nfs4_accessres res = {
2563 struct rpc_message msg = {
2564 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2567 .rpc_cred = entry->cred,
2569 int mode = entry->mask;
2573 * Determine which access bits we want to ask for...
2575 if (mode & MAY_READ)
2576 args.access |= NFS4_ACCESS_READ;
2577 if (S_ISDIR(inode->i_mode)) {
2578 if (mode & MAY_WRITE)
2579 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2580 if (mode & MAY_EXEC)
2581 args.access |= NFS4_ACCESS_LOOKUP;
2583 if (mode & MAY_WRITE)
2584 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2585 if (mode & MAY_EXEC)
2586 args.access |= NFS4_ACCESS_EXECUTE;
2589 res.fattr = nfs_alloc_fattr();
2590 if (res.fattr == NULL)
2593 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2596 if (res.access & NFS4_ACCESS_READ)
2597 entry->mask |= MAY_READ;
2598 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2599 entry->mask |= MAY_WRITE;
2600 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2601 entry->mask |= MAY_EXEC;
2602 nfs_refresh_inode(inode, res.fattr);
2604 nfs_free_fattr(res.fattr);
2608 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2610 struct nfs4_exception exception = { };
2613 err = nfs4_handle_exception(NFS_SERVER(inode),
2614 _nfs4_proc_access(inode, entry),
2616 } while (exception.retry);
2621 * TODO: For the time being, we don't try to get any attributes
2622 * along with any of the zero-copy operations READ, READDIR,
2625 * In the case of the first three, we want to put the GETATTR
2626 * after the read-type operation -- this is because it is hard
2627 * to predict the length of a GETATTR response in v4, and thus
2628 * align the READ data correctly. This means that the GETATTR
2629 * may end up partially falling into the page cache, and we should
2630 * shift it into the 'tail' of the xdr_buf before processing.
2631 * To do this efficiently, we need to know the total length
2632 * of data received, which doesn't seem to be available outside
2635 * In the case of WRITE, we also want to put the GETATTR after
2636 * the operation -- in this case because we want to make sure
2637 * we get the post-operation mtime and size. This means that
2638 * we can't use xdr_encode_pages() as written: we need a variant
2639 * of it which would leave room in the 'tail' iovec.
2641 * Both of these changes to the XDR layer would in fact be quite
2642 * minor, but I decided to leave them for a subsequent patch.
2644 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2645 unsigned int pgbase, unsigned int pglen)
2647 struct nfs4_readlink args = {
2648 .fh = NFS_FH(inode),
2653 struct nfs4_readlink_res res;
2654 struct rpc_message msg = {
2655 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2660 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2663 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2664 unsigned int pgbase, unsigned int pglen)
2666 struct nfs4_exception exception = { };
2669 err = nfs4_handle_exception(NFS_SERVER(inode),
2670 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2672 } while (exception.retry);
2678 * We will need to arrange for the VFS layer to provide an atomic open.
2679 * Until then, this create/open method is prone to inefficiency and race
2680 * conditions due to the lookup, create, and open VFS calls from sys_open()
2681 * placed on the wire.
2683 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2684 * The file will be opened again in the subsequent VFS open call
2685 * (nfs4_proc_file_open).
2687 * The open for read will just hang around to be used by any process that
2688 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2692 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2693 int flags, struct nfs_open_context *ctx)
2695 struct dentry *de = dentry;
2696 struct nfs4_state *state;
2697 struct rpc_cred *cred = NULL;
2706 sattr->ia_mode &= ~current_umask();
2707 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
2709 if (IS_ERR(state)) {
2710 status = PTR_ERR(state);
2713 d_add(dentry, igrab(state->inode));
2714 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2718 nfs4_close_sync(state, fmode);
2723 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2725 struct nfs_server *server = NFS_SERVER(dir);
2726 struct nfs_removeargs args = {
2728 .name.len = name->len,
2729 .name.name = name->name,
2730 .bitmask = server->attr_bitmask,
2732 struct nfs_removeres res = {
2735 struct rpc_message msg = {
2736 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2740 int status = -ENOMEM;
2742 res.dir_attr = nfs_alloc_fattr();
2743 if (res.dir_attr == NULL)
2746 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2748 update_changeattr(dir, &res.cinfo);
2749 nfs_post_op_update_inode(dir, res.dir_attr);
2751 nfs_free_fattr(res.dir_attr);
2756 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2758 struct nfs4_exception exception = { };
2761 err = nfs4_handle_exception(NFS_SERVER(dir),
2762 _nfs4_proc_remove(dir, name),
2764 } while (exception.retry);
2768 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2770 struct nfs_server *server = NFS_SERVER(dir);
2771 struct nfs_removeargs *args = msg->rpc_argp;
2772 struct nfs_removeres *res = msg->rpc_resp;
2774 args->bitmask = server->cache_consistency_bitmask;
2775 res->server = server;
2776 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2777 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
2780 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2782 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2784 if (!nfs4_sequence_done(task, &res->seq_res))
2786 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2788 update_changeattr(dir, &res->cinfo);
2789 nfs_post_op_update_inode(dir, res->dir_attr);
2793 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2795 struct nfs_server *server = NFS_SERVER(dir);
2796 struct nfs_renameargs *arg = msg->rpc_argp;
2797 struct nfs_renameres *res = msg->rpc_resp;
2799 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2800 arg->bitmask = server->attr_bitmask;
2801 res->server = server;
2802 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
2805 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2806 struct inode *new_dir)
2808 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2810 if (!nfs4_sequence_done(task, &res->seq_res))
2812 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2815 update_changeattr(old_dir, &res->old_cinfo);
2816 nfs_post_op_update_inode(old_dir, res->old_fattr);
2817 update_changeattr(new_dir, &res->new_cinfo);
2818 nfs_post_op_update_inode(new_dir, res->new_fattr);
2822 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2823 struct inode *new_dir, struct qstr *new_name)
2825 struct nfs_server *server = NFS_SERVER(old_dir);
2826 struct nfs_renameargs arg = {
2827 .old_dir = NFS_FH(old_dir),
2828 .new_dir = NFS_FH(new_dir),
2829 .old_name = old_name,
2830 .new_name = new_name,
2831 .bitmask = server->attr_bitmask,
2833 struct nfs_renameres res = {
2836 struct rpc_message msg = {
2837 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2841 int status = -ENOMEM;
2843 res.old_fattr = nfs_alloc_fattr();
2844 res.new_fattr = nfs_alloc_fattr();
2845 if (res.old_fattr == NULL || res.new_fattr == NULL)
2848 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2850 update_changeattr(old_dir, &res.old_cinfo);
2851 nfs_post_op_update_inode(old_dir, res.old_fattr);
2852 update_changeattr(new_dir, &res.new_cinfo);
2853 nfs_post_op_update_inode(new_dir, res.new_fattr);
2856 nfs_free_fattr(res.new_fattr);
2857 nfs_free_fattr(res.old_fattr);
2861 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2862 struct inode *new_dir, struct qstr *new_name)
2864 struct nfs4_exception exception = { };
2867 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2868 _nfs4_proc_rename(old_dir, old_name,
2871 } while (exception.retry);
2875 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2877 struct nfs_server *server = NFS_SERVER(inode);
2878 struct nfs4_link_arg arg = {
2879 .fh = NFS_FH(inode),
2880 .dir_fh = NFS_FH(dir),
2882 .bitmask = server->attr_bitmask,
2884 struct nfs4_link_res res = {
2887 struct rpc_message msg = {
2888 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2892 int status = -ENOMEM;
2894 res.fattr = nfs_alloc_fattr();
2895 res.dir_attr = nfs_alloc_fattr();
2896 if (res.fattr == NULL || res.dir_attr == NULL)
2899 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2901 update_changeattr(dir, &res.cinfo);
2902 nfs_post_op_update_inode(dir, res.dir_attr);
2903 nfs_post_op_update_inode(inode, res.fattr);
2906 nfs_free_fattr(res.dir_attr);
2907 nfs_free_fattr(res.fattr);
2911 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2913 struct nfs4_exception exception = { };
2916 err = nfs4_handle_exception(NFS_SERVER(inode),
2917 _nfs4_proc_link(inode, dir, name),
2919 } while (exception.retry);
2923 struct nfs4_createdata {
2924 struct rpc_message msg;
2925 struct nfs4_create_arg arg;
2926 struct nfs4_create_res res;
2928 struct nfs_fattr fattr;
2929 struct nfs_fattr dir_fattr;
2932 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2933 struct qstr *name, struct iattr *sattr, u32 ftype)
2935 struct nfs4_createdata *data;
2937 data = kzalloc(sizeof(*data), GFP_KERNEL);
2939 struct nfs_server *server = NFS_SERVER(dir);
2941 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2942 data->msg.rpc_argp = &data->arg;
2943 data->msg.rpc_resp = &data->res;
2944 data->arg.dir_fh = NFS_FH(dir);
2945 data->arg.server = server;
2946 data->arg.name = name;
2947 data->arg.attrs = sattr;
2948 data->arg.ftype = ftype;
2949 data->arg.bitmask = server->attr_bitmask;
2950 data->res.server = server;
2951 data->res.fh = &data->fh;
2952 data->res.fattr = &data->fattr;
2953 data->res.dir_fattr = &data->dir_fattr;
2954 nfs_fattr_init(data->res.fattr);
2955 nfs_fattr_init(data->res.dir_fattr);
2960 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2962 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
2963 &data->arg.seq_args, &data->res.seq_res, 1);
2965 update_changeattr(dir, &data->res.dir_cinfo);
2966 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2967 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2972 static void nfs4_free_createdata(struct nfs4_createdata *data)
2977 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2978 struct page *page, unsigned int len, struct iattr *sattr)
2980 struct nfs4_createdata *data;
2981 int status = -ENAMETOOLONG;
2983 if (len > NFS4_MAXPATHLEN)
2987 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2991 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2992 data->arg.u.symlink.pages = &page;
2993 data->arg.u.symlink.len = len;
2995 status = nfs4_do_create(dir, dentry, data);
2997 nfs4_free_createdata(data);
3002 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3003 struct page *page, unsigned int len, struct iattr *sattr)
3005 struct nfs4_exception exception = { };
3008 err = nfs4_handle_exception(NFS_SERVER(dir),
3009 _nfs4_proc_symlink(dir, dentry, page,
3012 } while (exception.retry);
3016 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3017 struct iattr *sattr)
3019 struct nfs4_createdata *data;
3020 int status = -ENOMEM;
3022 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3026 status = nfs4_do_create(dir, dentry, data);
3028 nfs4_free_createdata(data);
3033 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3034 struct iattr *sattr)
3036 struct nfs4_exception exception = { };
3039 sattr->ia_mode &= ~current_umask();
3041 err = nfs4_handle_exception(NFS_SERVER(dir),
3042 _nfs4_proc_mkdir(dir, dentry, sattr),
3044 } while (exception.retry);
3048 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3049 u64 cookie, struct page **pages, unsigned int count, int plus)
3051 struct inode *dir = dentry->d_inode;
3052 struct nfs4_readdir_arg args = {
3057 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3060 struct nfs4_readdir_res res;
3061 struct rpc_message msg = {
3062 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3069 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3070 dentry->d_parent->d_name.name,
3071 dentry->d_name.name,
3072 (unsigned long long)cookie);
3073 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3074 res.pgbase = args.pgbase;
3075 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3077 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
3078 status += args.pgbase;
3081 nfs_invalidate_atime(dir);
3083 dprintk("%s: returns %d\n", __func__, status);
3087 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3088 u64 cookie, struct page **pages, unsigned int count, int plus)
3090 struct nfs4_exception exception = { };
3093 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3094 _nfs4_proc_readdir(dentry, cred, cookie,
3095 pages, count, plus),
3097 } while (exception.retry);
3101 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3102 struct iattr *sattr, dev_t rdev)
3104 struct nfs4_createdata *data;
3105 int mode = sattr->ia_mode;
3106 int status = -ENOMEM;
3108 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3109 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3111 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3116 data->arg.ftype = NF4FIFO;
3117 else if (S_ISBLK(mode)) {
3118 data->arg.ftype = NF4BLK;
3119 data->arg.u.device.specdata1 = MAJOR(rdev);
3120 data->arg.u.device.specdata2 = MINOR(rdev);
3122 else if (S_ISCHR(mode)) {
3123 data->arg.ftype = NF4CHR;
3124 data->arg.u.device.specdata1 = MAJOR(rdev);
3125 data->arg.u.device.specdata2 = MINOR(rdev);
3128 status = nfs4_do_create(dir, dentry, data);
3130 nfs4_free_createdata(data);
3135 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3136 struct iattr *sattr, dev_t rdev)
3138 struct nfs4_exception exception = { };
3141 sattr->ia_mode &= ~current_umask();
3143 err = nfs4_handle_exception(NFS_SERVER(dir),
3144 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3146 } while (exception.retry);
3150 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3151 struct nfs_fsstat *fsstat)
3153 struct nfs4_statfs_arg args = {
3155 .bitmask = server->attr_bitmask,
3157 struct nfs4_statfs_res res = {
3160 struct rpc_message msg = {
3161 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3166 nfs_fattr_init(fsstat->fattr);
3167 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3170 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3172 struct nfs4_exception exception = { };
3175 err = nfs4_handle_exception(server,
3176 _nfs4_proc_statfs(server, fhandle, fsstat),
3178 } while (exception.retry);
3182 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3183 struct nfs_fsinfo *fsinfo)
3185 struct nfs4_fsinfo_arg args = {
3187 .bitmask = server->attr_bitmask,
3189 struct nfs4_fsinfo_res res = {
3192 struct rpc_message msg = {
3193 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3198 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3201 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3203 struct nfs4_exception exception = { };
3207 err = nfs4_handle_exception(server,
3208 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3210 } while (exception.retry);
3214 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3216 nfs_fattr_init(fsinfo->fattr);
3217 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3220 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3221 struct nfs_pathconf *pathconf)
3223 struct nfs4_pathconf_arg args = {
3225 .bitmask = server->attr_bitmask,
3227 struct nfs4_pathconf_res res = {
3228 .pathconf = pathconf,
3230 struct rpc_message msg = {
3231 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3236 /* None of the pathconf attributes are mandatory to implement */
3237 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3238 memset(pathconf, 0, sizeof(*pathconf));
3242 nfs_fattr_init(pathconf->fattr);
3243 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3246 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3247 struct nfs_pathconf *pathconf)
3249 struct nfs4_exception exception = { };
3253 err = nfs4_handle_exception(server,
3254 _nfs4_proc_pathconf(server, fhandle, pathconf),
3256 } while (exception.retry);
3260 void __nfs4_read_done_cb(struct nfs_read_data *data)
3262 nfs_invalidate_atime(data->inode);
3265 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3267 struct nfs_server *server = NFS_SERVER(data->inode);
3269 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3270 rpc_restart_call_prepare(task);
3274 __nfs4_read_done_cb(data);
3275 if (task->tk_status > 0)
3276 renew_lease(server, data->timestamp);
3280 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3283 dprintk("--> %s\n", __func__);
3285 if (!nfs4_sequence_done(task, &data->res.seq_res))
3288 return data->read_done_cb ? data->read_done_cb(task, data) :
3289 nfs4_read_done_cb(task, data);
3292 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3294 data->timestamp = jiffies;
3295 data->read_done_cb = nfs4_read_done_cb;
3296 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3297 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3300 /* Reset the the nfs_read_data to send the read to the MDS. */
3301 void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3303 dprintk("%s Reset task for i/o through\n", __func__);
3304 put_lseg(data->lseg);
3306 /* offsets will differ in the dense stripe case */
3307 data->args.offset = data->mds_offset;
3308 data->ds_clp = NULL;
3309 data->args.fh = NFS_FH(data->inode);
3310 data->read_done_cb = nfs4_read_done_cb;
3311 task->tk_ops = data->mds_ops;
3312 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3314 EXPORT_SYMBOL_GPL(nfs4_reset_read);
3316 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3318 struct inode *inode = data->inode;
3320 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3321 rpc_restart_call_prepare(task);
3324 if (task->tk_status >= 0) {
3325 renew_lease(NFS_SERVER(inode), data->timestamp);
3326 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3331 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3333 if (!nfs4_sequence_done(task, &data->res.seq_res))
3335 return data->write_done_cb ? data->write_done_cb(task, data) :
3336 nfs4_write_done_cb(task, data);
3339 /* Reset the the nfs_write_data to send the write to the MDS. */
3340 void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3342 dprintk("%s Reset task for i/o through\n", __func__);
3343 put_lseg(data->lseg);
3345 data->ds_clp = NULL;
3346 data->write_done_cb = nfs4_write_done_cb;
3347 data->args.fh = NFS_FH(data->inode);
3348 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3349 data->args.offset = data->mds_offset;
3350 data->res.fattr = &data->fattr;
3351 task->tk_ops = data->mds_ops;
3352 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3354 EXPORT_SYMBOL_GPL(nfs4_reset_write);
3356 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3358 struct nfs_server *server = NFS_SERVER(data->inode);
3361 data->args.bitmask = NULL;
3362 data->res.fattr = NULL;
3364 data->args.bitmask = server->cache_consistency_bitmask;
3365 if (!data->write_done_cb)
3366 data->write_done_cb = nfs4_write_done_cb;
3367 data->res.server = server;
3368 data->timestamp = jiffies;
3370 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3371 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3374 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3376 struct inode *inode = data->inode;
3378 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3379 rpc_restart_call_prepare(task);
3382 nfs_refresh_inode(inode, data->res.fattr);
3386 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3388 if (!nfs4_sequence_done(task, &data->res.seq_res))
3390 return data->write_done_cb(task, data);
3393 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3395 struct nfs_server *server = NFS_SERVER(data->inode);
3398 data->args.bitmask = NULL;
3399 data->res.fattr = NULL;
3401 data->args.bitmask = server->cache_consistency_bitmask;
3402 if (!data->write_done_cb)
3403 data->write_done_cb = nfs4_commit_done_cb;
3404 data->res.server = server;
3405 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3406 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3409 struct nfs4_renewdata {
3410 struct nfs_client *client;
3411 unsigned long timestamp;
3415 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3416 * standalone procedure for queueing an asynchronous RENEW.
3418 static void nfs4_renew_release(void *calldata)
3420 struct nfs4_renewdata *data = calldata;
3421 struct nfs_client *clp = data->client;
3423 if (atomic_read(&clp->cl_count) > 1)
3424 nfs4_schedule_state_renewal(clp);
3425 nfs_put_client(clp);
3429 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3431 struct nfs4_renewdata *data = calldata;
3432 struct nfs_client *clp = data->client;
3433 unsigned long timestamp = data->timestamp;
3435 if (task->tk_status < 0) {
3436 /* Unless we're shutting down, schedule state recovery! */
3437 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3439 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3440 nfs4_schedule_lease_recovery(clp);
3443 nfs4_schedule_path_down_recovery(clp);
3445 do_renew_lease(clp, timestamp);
3448 static const struct rpc_call_ops nfs4_renew_ops = {
3449 .rpc_call_done = nfs4_renew_done,
3450 .rpc_release = nfs4_renew_release,
3453 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3455 struct rpc_message msg = {
3456 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3460 struct nfs4_renewdata *data;
3462 if (renew_flags == 0)
3464 if (!atomic_inc_not_zero(&clp->cl_count))
3466 data = kmalloc(sizeof(*data), GFP_NOFS);
3470 data->timestamp = jiffies;
3471 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3472 &nfs4_renew_ops, data);
3475 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3477 struct rpc_message msg = {
3478 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3482 unsigned long now = jiffies;
3485 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3488 do_renew_lease(clp, now);
3492 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3494 return (server->caps & NFS_CAP_ACLS)
3495 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3496 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3499 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3500 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3503 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3505 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3506 struct page **pages, unsigned int *pgbase)
3508 struct page *newpage, **spages;
3514 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3515 newpage = alloc_page(GFP_KERNEL);
3517 if (newpage == NULL)
3519 memcpy(page_address(newpage), buf, len);
3524 } while (buflen != 0);
3530 __free_page(spages[rc-1]);
3534 struct nfs4_cached_acl {
3540 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3542 struct nfs_inode *nfsi = NFS_I(inode);
3544 spin_lock(&inode->i_lock);
3545 kfree(nfsi->nfs4_acl);
3546 nfsi->nfs4_acl = acl;
3547 spin_unlock(&inode->i_lock);
3550 static void nfs4_zap_acl_attr(struct inode *inode)
3552 nfs4_set_cached_acl(inode, NULL);
3555 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3557 struct nfs_inode *nfsi = NFS_I(inode);
3558 struct nfs4_cached_acl *acl;
3561 spin_lock(&inode->i_lock);
3562 acl = nfsi->nfs4_acl;
3565 if (buf == NULL) /* user is just asking for length */
3567 if (acl->cached == 0)
3569 ret = -ERANGE; /* see getxattr(2) man page */
3570 if (acl->len > buflen)
3572 memcpy(buf, acl->data, acl->len);
3576 spin_unlock(&inode->i_lock);
3580 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3582 struct nfs4_cached_acl *acl;
3584 if (buf && acl_len <= PAGE_SIZE) {
3585 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3589 memcpy(acl->data, buf, acl_len);
3591 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3598 nfs4_set_cached_acl(inode, acl);
3602 * The getxattr API returns the required buffer length when called with a
3603 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3604 * the required buf. On a NULL buf, we send a page of data to the server
3605 * guessing that the ACL request can be serviced by a page. If so, we cache
3606 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3607 * the cache. If not so, we throw away the page, and cache the required
3608 * length. The next getxattr call will then produce another round trip to
3609 * the server, this time with the input buf of the required size.
3611 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3613 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3614 struct nfs_getaclargs args = {
3615 .fh = NFS_FH(inode),
3619 struct nfs_getaclres res = {
3623 struct rpc_message msg = {
3624 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3628 int ret = -ENOMEM, npages, i, acl_len = 0;
3630 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3631 /* As long as we're doing a round trip to the server anyway,
3632 * let's be prepared for a page of acl data. */
3636 for (i = 0; i < npages; i++) {
3637 pages[i] = alloc_page(GFP_KERNEL);
3642 /* for decoding across pages */
3643 res.acl_scratch = alloc_page(GFP_KERNEL);
3644 if (!res.acl_scratch)
3647 args.acl_len = npages * PAGE_SIZE;
3648 args.acl_pgbase = 0;
3649 /* Let decode_getfacl know not to fail if the ACL data is larger than
3650 * the page we send as a guess */
3652 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3653 resp_buf = page_address(pages[0]);
3655 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3656 __func__, buf, buflen, npages, args.acl_len);
3657 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3658 &msg, &args.seq_args, &res.seq_res, 0);
3662 acl_len = res.acl_len - res.acl_data_offset;
3663 if (acl_len > args.acl_len)
3664 nfs4_write_cached_acl(inode, NULL, acl_len);
3666 nfs4_write_cached_acl(inode, resp_buf + res.acl_data_offset,
3670 if (acl_len > buflen)
3672 _copy_from_pages(buf, pages, res.acl_data_offset,
3677 for (i = 0; i < npages; i++)
3679 __free_page(pages[i]);
3680 if (res.acl_scratch)
3681 __free_page(res.acl_scratch);
3685 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3687 struct nfs4_exception exception = { };
3690 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3693 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3694 } while (exception.retry);
3698 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3700 struct nfs_server *server = NFS_SERVER(inode);
3703 if (!nfs4_server_supports_acls(server))
3705 ret = nfs_revalidate_inode(server, inode);
3708 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3709 nfs_zap_acl_cache(inode);
3710 ret = nfs4_read_cached_acl(inode, buf, buflen);
3712 /* -ENOENT is returned if there is no ACL or if there is an ACL
3713 * but no cached acl data, just the acl length */
3715 return nfs4_get_acl_uncached(inode, buf, buflen);
3718 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3720 struct nfs_server *server = NFS_SERVER(inode);
3721 struct page *pages[NFS4ACL_MAXPAGES];
3722 struct nfs_setaclargs arg = {
3723 .fh = NFS_FH(inode),
3727 struct nfs_setaclres res;
3728 struct rpc_message msg = {
3729 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3735 if (!nfs4_server_supports_acls(server))
3737 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3740 nfs_inode_return_delegation(inode);
3741 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3744 * Free each page after tx, so the only ref left is
3745 * held by the network stack
3748 put_page(pages[i-1]);
3751 * Acl update can result in inode attribute update.
3752 * so mark the attribute cache invalid.
3754 spin_lock(&inode->i_lock);
3755 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3756 spin_unlock(&inode->i_lock);
3757 nfs_access_zap_cache(inode);
3758 nfs_zap_acl_cache(inode);
3762 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3764 struct nfs4_exception exception = { };
3767 err = nfs4_handle_exception(NFS_SERVER(inode),
3768 __nfs4_proc_set_acl(inode, buf, buflen),
3770 } while (exception.retry);
3775 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3777 struct nfs_client *clp = server->nfs_client;
3779 if (task->tk_status >= 0)
3781 switch(task->tk_status) {
3782 case -NFS4ERR_DELEG_REVOKED:
3783 case -NFS4ERR_ADMIN_REVOKED:
3784 case -NFS4ERR_BAD_STATEID:
3786 nfs_remove_bad_delegation(state->inode);
3787 case -NFS4ERR_OPENMODE:
3790 nfs4_schedule_stateid_recovery(server, state);
3791 goto wait_on_recovery;
3792 case -NFS4ERR_EXPIRED:
3794 nfs4_schedule_stateid_recovery(server, state);
3795 case -NFS4ERR_STALE_STATEID:
3796 case -NFS4ERR_STALE_CLIENTID:
3797 nfs4_schedule_lease_recovery(clp);
3798 goto wait_on_recovery;
3799 #if defined(CONFIG_NFS_V4_1)
3800 case -NFS4ERR_BADSESSION:
3801 case -NFS4ERR_BADSLOT:
3802 case -NFS4ERR_BAD_HIGH_SLOT:
3803 case -NFS4ERR_DEADSESSION:
3804 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3805 case -NFS4ERR_SEQ_FALSE_RETRY:
3806 case -NFS4ERR_SEQ_MISORDERED:
3807 dprintk("%s ERROR %d, Reset session\n", __func__,
3809 nfs4_schedule_session_recovery(clp->cl_session);
3810 task->tk_status = 0;
3812 #endif /* CONFIG_NFS_V4_1 */
3813 case -NFS4ERR_DELAY:
3814 nfs_inc_server_stats(server, NFSIOS_DELAY);
3815 case -NFS4ERR_GRACE:
3817 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3818 task->tk_status = 0;
3820 case -NFS4ERR_RETRY_UNCACHED_REP:
3821 case -NFS4ERR_OLD_STATEID:
3822 task->tk_status = 0;
3825 task->tk_status = nfs4_map_errors(task->tk_status);
3828 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3829 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3830 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3831 task->tk_status = 0;
3835 static void nfs4_construct_boot_verifier(struct nfs_client *clp,
3836 nfs4_verifier *bootverf)
3840 verf[0] = htonl((u32)clp->cl_boot_time.tv_sec);
3841 verf[1] = htonl((u32)clp->cl_boot_time.tv_nsec);
3842 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3845 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3846 unsigned short port, struct rpc_cred *cred,
3847 struct nfs4_setclientid_res *res)
3849 nfs4_verifier sc_verifier;
3850 struct nfs4_setclientid setclientid = {
3851 .sc_verifier = &sc_verifier,
3853 .sc_cb_ident = clp->cl_cb_ident,
3855 struct rpc_message msg = {
3856 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3857 .rpc_argp = &setclientid,
3864 nfs4_construct_boot_verifier(clp, &sc_verifier);
3868 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3869 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3871 rpc_peeraddr2str(clp->cl_rpcclient,
3873 rpc_peeraddr2str(clp->cl_rpcclient,
3875 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3876 clp->cl_id_uniquifier);
3877 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3878 sizeof(setclientid.sc_netid),
3879 rpc_peeraddr2str(clp->cl_rpcclient,
3880 RPC_DISPLAY_NETID));
3881 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3882 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3883 clp->cl_ipaddr, port >> 8, port & 255);
3886 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3887 if (status != -NFS4ERR_CLID_INUSE)
3890 ++clp->cl_id_uniquifier;
3894 ssleep(clp->cl_lease_time / HZ + 1);
3899 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3900 struct nfs4_setclientid_res *arg,
3901 struct rpc_cred *cred)
3903 struct nfs_fsinfo fsinfo;
3904 struct rpc_message msg = {
3905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3907 .rpc_resp = &fsinfo,
3914 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3916 spin_lock(&clp->cl_lock);
3917 clp->cl_lease_time = fsinfo.lease_time * HZ;
3918 clp->cl_last_renewal = now;
3919 spin_unlock(&clp->cl_lock);
3924 struct nfs4_delegreturndata {
3925 struct nfs4_delegreturnargs args;
3926 struct nfs4_delegreturnres res;
3928 nfs4_stateid stateid;
3929 unsigned long timestamp;
3930 struct nfs_fattr fattr;
3934 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3936 struct nfs4_delegreturndata *data = calldata;
3938 if (!nfs4_sequence_done(task, &data->res.seq_res))
3941 switch (task->tk_status) {
3942 case -NFS4ERR_STALE_STATEID:
3943 case -NFS4ERR_EXPIRED:
3945 renew_lease(data->res.server, data->timestamp);
3948 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3950 rpc_restart_call_prepare(task);
3954 data->rpc_status = task->tk_status;
3957 static void nfs4_delegreturn_release(void *calldata)
3962 #if defined(CONFIG_NFS_V4_1)
3963 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3965 struct nfs4_delegreturndata *d_data;
3967 d_data = (struct nfs4_delegreturndata *)data;
3969 if (nfs4_setup_sequence(d_data->res.server,
3970 &d_data->args.seq_args,
3971 &d_data->res.seq_res, task))
3973 rpc_call_start(task);
3975 #endif /* CONFIG_NFS_V4_1 */
3977 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3978 #if defined(CONFIG_NFS_V4_1)
3979 .rpc_call_prepare = nfs4_delegreturn_prepare,
3980 #endif /* CONFIG_NFS_V4_1 */
3981 .rpc_call_done = nfs4_delegreturn_done,
3982 .rpc_release = nfs4_delegreturn_release,
3985 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3987 struct nfs4_delegreturndata *data;
3988 struct nfs_server *server = NFS_SERVER(inode);
3989 struct rpc_task *task;
3990 struct rpc_message msg = {
3991 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3994 struct rpc_task_setup task_setup_data = {
3995 .rpc_client = server->client,
3996 .rpc_message = &msg,
3997 .callback_ops = &nfs4_delegreturn_ops,
3998 .flags = RPC_TASK_ASYNC,
4002 data = kzalloc(sizeof(*data), GFP_NOFS);
4005 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4006 data->args.fhandle = &data->fh;
4007 data->args.stateid = &data->stateid;
4008 data->args.bitmask = server->attr_bitmask;
4009 nfs_copy_fh(&data->fh, NFS_FH(inode));
4010 nfs4_stateid_copy(&data->stateid, stateid);
4011 data->res.fattr = &data->fattr;
4012 data->res.server = server;
4013 nfs_fattr_init(data->res.fattr);
4014 data->timestamp = jiffies;
4015 data->rpc_status = 0;
4017 task_setup_data.callback_data = data;
4018 msg.rpc_argp = &data->args;
4019 msg.rpc_resp = &data->res;
4020 task = rpc_run_task(&task_setup_data);
4022 return PTR_ERR(task);
4025 status = nfs4_wait_for_completion_rpc_task(task);
4028 status = data->rpc_status;
4031 nfs_refresh_inode(inode, &data->fattr);
4037 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4039 struct nfs_server *server = NFS_SERVER(inode);
4040 struct nfs4_exception exception = { };
4043 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4045 case -NFS4ERR_STALE_STATEID:
4046 case -NFS4ERR_EXPIRED:
4050 err = nfs4_handle_exception(server, err, &exception);
4051 } while (exception.retry);
4055 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4056 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4059 * sleep, with exponential backoff, and retry the LOCK operation.
4061 static unsigned long
4062 nfs4_set_lock_task_retry(unsigned long timeout)
4064 freezable_schedule_timeout_killable(timeout);
4066 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4067 return NFS4_LOCK_MAXTIMEOUT;
4071 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4073 struct inode *inode = state->inode;
4074 struct nfs_server *server = NFS_SERVER(inode);
4075 struct nfs_client *clp = server->nfs_client;
4076 struct nfs_lockt_args arg = {
4077 .fh = NFS_FH(inode),
4080 struct nfs_lockt_res res = {
4083 struct rpc_message msg = {
4084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4087 .rpc_cred = state->owner->so_cred,
4089 struct nfs4_lock_state *lsp;
4092 arg.lock_owner.clientid = clp->cl_clientid;
4093 status = nfs4_set_lock_state(state, request);
4096 lsp = request->fl_u.nfs4_fl.owner;
4097 arg.lock_owner.id = lsp->ls_seqid.owner_id;
4098 arg.lock_owner.s_dev = server->s_dev;
4099 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4102 request->fl_type = F_UNLCK;
4104 case -NFS4ERR_DENIED:
4107 request->fl_ops->fl_release_private(request);
4112 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4114 struct nfs4_exception exception = { };
4118 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4119 _nfs4_proc_getlk(state, cmd, request),
4121 } while (exception.retry);
4125 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4128 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4130 res = posix_lock_file_wait(file, fl);
4133 res = flock_lock_file_wait(file, fl);
4141 struct nfs4_unlockdata {
4142 struct nfs_locku_args arg;
4143 struct nfs_locku_res res;
4144 struct nfs4_lock_state *lsp;
4145 struct nfs_open_context *ctx;
4146 struct file_lock fl;
4147 const struct nfs_server *server;
4148 unsigned long timestamp;
4151 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4152 struct nfs_open_context *ctx,
4153 struct nfs4_lock_state *lsp,
4154 struct nfs_seqid *seqid)
4156 struct nfs4_unlockdata *p;
4157 struct inode *inode = lsp->ls_state->inode;
4159 p = kzalloc(sizeof(*p), GFP_NOFS);
4162 p->arg.fh = NFS_FH(inode);
4164 p->arg.seqid = seqid;
4165 p->res.seqid = seqid;
4166 p->arg.stateid = &lsp->ls_stateid;
4168 atomic_inc(&lsp->ls_count);
4169 /* Ensure we don't close file until we're done freeing locks! */
4170 p->ctx = get_nfs_open_context(ctx);
4171 memcpy(&p->fl, fl, sizeof(p->fl));
4172 p->server = NFS_SERVER(inode);
4176 static void nfs4_locku_release_calldata(void *data)
4178 struct nfs4_unlockdata *calldata = data;
4179 nfs_free_seqid(calldata->arg.seqid);
4180 nfs4_put_lock_state(calldata->lsp);
4181 put_nfs_open_context(calldata->ctx);
4185 static void nfs4_locku_done(struct rpc_task *task, void *data)
4187 struct nfs4_unlockdata *calldata = data;
4189 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4191 switch (task->tk_status) {
4193 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4194 &calldata->res.stateid);
4195 renew_lease(calldata->server, calldata->timestamp);
4197 case -NFS4ERR_BAD_STATEID:
4198 case -NFS4ERR_OLD_STATEID:
4199 case -NFS4ERR_STALE_STATEID:
4200 case -NFS4ERR_EXPIRED:
4203 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4204 rpc_restart_call_prepare(task);
4208 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4210 struct nfs4_unlockdata *calldata = data;
4212 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4214 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
4215 /* Note: exit _without_ running nfs4_locku_done */
4216 task->tk_action = NULL;
4219 calldata->timestamp = jiffies;
4220 if (nfs4_setup_sequence(calldata->server,
4221 &calldata->arg.seq_args,
4222 &calldata->res.seq_res, task))
4224 rpc_call_start(task);
4227 static const struct rpc_call_ops nfs4_locku_ops = {
4228 .rpc_call_prepare = nfs4_locku_prepare,
4229 .rpc_call_done = nfs4_locku_done,
4230 .rpc_release = nfs4_locku_release_calldata,
4233 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4234 struct nfs_open_context *ctx,
4235 struct nfs4_lock_state *lsp,
4236 struct nfs_seqid *seqid)
4238 struct nfs4_unlockdata *data;
4239 struct rpc_message msg = {
4240 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4241 .rpc_cred = ctx->cred,
4243 struct rpc_task_setup task_setup_data = {
4244 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4245 .rpc_message = &msg,
4246 .callback_ops = &nfs4_locku_ops,
4247 .workqueue = nfsiod_workqueue,
4248 .flags = RPC_TASK_ASYNC,
4251 /* Ensure this is an unlock - when canceling a lock, the
4252 * canceled lock is passed in, and it won't be an unlock.
4254 fl->fl_type = F_UNLCK;
4256 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4258 nfs_free_seqid(seqid);
4259 return ERR_PTR(-ENOMEM);
4262 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4263 msg.rpc_argp = &data->arg;
4264 msg.rpc_resp = &data->res;
4265 task_setup_data.callback_data = data;
4266 return rpc_run_task(&task_setup_data);
4269 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4271 struct nfs_inode *nfsi = NFS_I(state->inode);
4272 struct nfs_seqid *seqid;
4273 struct nfs4_lock_state *lsp;
4274 struct rpc_task *task;
4276 unsigned char fl_flags = request->fl_flags;
4278 status = nfs4_set_lock_state(state, request);
4279 /* Unlock _before_ we do the RPC call */
4280 request->fl_flags |= FL_EXISTS;
4281 down_read(&nfsi->rwsem);
4282 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4283 up_read(&nfsi->rwsem);
4286 up_read(&nfsi->rwsem);
4289 /* Is this a delegated lock? */
4290 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4292 lsp = request->fl_u.nfs4_fl.owner;
4293 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4297 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4298 status = PTR_ERR(task);
4301 status = nfs4_wait_for_completion_rpc_task(task);
4304 request->fl_flags = fl_flags;
4308 struct nfs4_lockdata {
4309 struct nfs_lock_args arg;
4310 struct nfs_lock_res res;
4311 struct nfs4_lock_state *lsp;
4312 struct nfs_open_context *ctx;
4313 struct file_lock fl;
4314 unsigned long timestamp;
4317 struct nfs_server *server;
4320 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4321 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4324 struct nfs4_lockdata *p;
4325 struct inode *inode = lsp->ls_state->inode;
4326 struct nfs_server *server = NFS_SERVER(inode);
4328 p = kzalloc(sizeof(*p), gfp_mask);
4332 p->arg.fh = NFS_FH(inode);
4334 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4335 if (p->arg.open_seqid == NULL)
4337 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4338 if (p->arg.lock_seqid == NULL)
4339 goto out_free_seqid;
4340 p->arg.lock_stateid = &lsp->ls_stateid;
4341 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4342 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4343 p->arg.lock_owner.s_dev = server->s_dev;
4344 p->res.lock_seqid = p->arg.lock_seqid;
4347 atomic_inc(&lsp->ls_count);
4348 p->ctx = get_nfs_open_context(ctx);
4349 memcpy(&p->fl, fl, sizeof(p->fl));
4352 nfs_free_seqid(p->arg.open_seqid);
4358 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4360 struct nfs4_lockdata *data = calldata;
4361 struct nfs4_state *state = data->lsp->ls_state;
4363 dprintk("%s: begin!\n", __func__);
4364 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4366 /* Do we need to do an open_to_lock_owner? */
4367 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4368 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4370 data->arg.open_stateid = &state->stateid;
4371 data->arg.new_lock_owner = 1;
4372 data->res.open_seqid = data->arg.open_seqid;
4374 data->arg.new_lock_owner = 0;
4375 data->timestamp = jiffies;
4376 if (nfs4_setup_sequence(data->server,
4377 &data->arg.seq_args,
4378 &data->res.seq_res, task))
4380 rpc_call_start(task);
4381 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4384 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4386 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4387 nfs4_lock_prepare(task, calldata);
4390 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4392 struct nfs4_lockdata *data = calldata;
4394 dprintk("%s: begin!\n", __func__);
4396 if (!nfs4_sequence_done(task, &data->res.seq_res))
4399 data->rpc_status = task->tk_status;
4400 if (data->arg.new_lock_owner != 0) {
4401 if (data->rpc_status == 0)
4402 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4406 if (data->rpc_status == 0) {
4407 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4408 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4409 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4412 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4415 static void nfs4_lock_release(void *calldata)
4417 struct nfs4_lockdata *data = calldata;
4419 dprintk("%s: begin!\n", __func__);
4420 nfs_free_seqid(data->arg.open_seqid);
4421 if (data->cancelled != 0) {
4422 struct rpc_task *task;
4423 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4424 data->arg.lock_seqid);
4426 rpc_put_task_async(task);
4427 dprintk("%s: cancelling lock!\n", __func__);
4429 nfs_free_seqid(data->arg.lock_seqid);
4430 nfs4_put_lock_state(data->lsp);
4431 put_nfs_open_context(data->ctx);
4433 dprintk("%s: done!\n", __func__);
4436 static const struct rpc_call_ops nfs4_lock_ops = {
4437 .rpc_call_prepare = nfs4_lock_prepare,
4438 .rpc_call_done = nfs4_lock_done,
4439 .rpc_release = nfs4_lock_release,
4442 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4443 .rpc_call_prepare = nfs4_recover_lock_prepare,
4444 .rpc_call_done = nfs4_lock_done,
4445 .rpc_release = nfs4_lock_release,
4448 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4451 case -NFS4ERR_ADMIN_REVOKED:
4452 case -NFS4ERR_BAD_STATEID:
4453 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4454 if (new_lock_owner != 0 ||
4455 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4456 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4458 case -NFS4ERR_STALE_STATEID:
4459 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4460 case -NFS4ERR_EXPIRED:
4461 nfs4_schedule_lease_recovery(server->nfs_client);
4465 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4467 struct nfs4_lockdata *data;
4468 struct rpc_task *task;
4469 struct rpc_message msg = {
4470 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4471 .rpc_cred = state->owner->so_cred,
4473 struct rpc_task_setup task_setup_data = {
4474 .rpc_client = NFS_CLIENT(state->inode),
4475 .rpc_message = &msg,
4476 .callback_ops = &nfs4_lock_ops,
4477 .workqueue = nfsiod_workqueue,
4478 .flags = RPC_TASK_ASYNC,
4482 dprintk("%s: begin!\n", __func__);
4483 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4484 fl->fl_u.nfs4_fl.owner,
4485 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4489 data->arg.block = 1;
4490 if (recovery_type > NFS_LOCK_NEW) {
4491 if (recovery_type == NFS_LOCK_RECLAIM)
4492 data->arg.reclaim = NFS_LOCK_RECLAIM;
4493 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4495 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4496 msg.rpc_argp = &data->arg;
4497 msg.rpc_resp = &data->res;
4498 task_setup_data.callback_data = data;
4499 task = rpc_run_task(&task_setup_data);
4501 return PTR_ERR(task);
4502 ret = nfs4_wait_for_completion_rpc_task(task);
4504 ret = data->rpc_status;
4506 nfs4_handle_setlk_error(data->server, data->lsp,
4507 data->arg.new_lock_owner, ret);
4509 data->cancelled = 1;
4511 dprintk("%s: done, ret = %d!\n", __func__, ret);
4515 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4517 struct nfs_server *server = NFS_SERVER(state->inode);
4518 struct nfs4_exception exception = { };
4522 /* Cache the lock if possible... */
4523 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4525 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4526 if (err != -NFS4ERR_DELAY)
4528 nfs4_handle_exception(server, err, &exception);
4529 } while (exception.retry);
4533 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4535 struct nfs_server *server = NFS_SERVER(state->inode);
4536 struct nfs4_exception exception = { };
4539 err = nfs4_set_lock_state(state, request);
4543 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4545 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4549 case -NFS4ERR_GRACE:
4550 case -NFS4ERR_DELAY:
4551 nfs4_handle_exception(server, err, &exception);
4554 } while (exception.retry);
4559 #if defined(CONFIG_NFS_V4_1)
4560 static int nfs41_check_expired_locks(struct nfs4_state *state)
4562 int status, ret = NFS_OK;
4563 struct nfs4_lock_state *lsp;
4564 struct nfs_server *server = NFS_SERVER(state->inode);
4566 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4567 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4568 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4569 if (status != NFS_OK) {
4570 nfs41_free_stateid(server, &lsp->ls_stateid);
4571 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4580 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4582 int status = NFS_OK;
4584 if (test_bit(LK_STATE_IN_USE, &state->flags))
4585 status = nfs41_check_expired_locks(state);
4586 if (status == NFS_OK)
4588 return nfs4_lock_expired(state, request);
4592 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4594 struct nfs_inode *nfsi = NFS_I(state->inode);
4595 unsigned char fl_flags = request->fl_flags;
4596 int status = -ENOLCK;
4598 if ((fl_flags & FL_POSIX) &&
4599 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4601 /* Is this a delegated open? */
4602 status = nfs4_set_lock_state(state, request);
4605 request->fl_flags |= FL_ACCESS;
4606 status = do_vfs_lock(request->fl_file, request);
4609 down_read(&nfsi->rwsem);
4610 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4611 /* Yes: cache locks! */
4612 /* ...but avoid races with delegation recall... */
4613 request->fl_flags = fl_flags & ~FL_SLEEP;
4614 status = do_vfs_lock(request->fl_file, request);
4617 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4620 /* Note: we always want to sleep here! */
4621 request->fl_flags = fl_flags | FL_SLEEP;
4622 if (do_vfs_lock(request->fl_file, request) < 0)
4623 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4624 "manager!\n", __func__);
4626 up_read(&nfsi->rwsem);
4628 request->fl_flags = fl_flags;
4632 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4634 struct nfs4_exception exception = {
4640 err = _nfs4_proc_setlk(state, cmd, request);
4641 if (err == -NFS4ERR_DENIED)
4643 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4645 } while (exception.retry);
4650 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4652 struct nfs_open_context *ctx;
4653 struct nfs4_state *state;
4654 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4657 /* verify open state */
4658 ctx = nfs_file_open_context(filp);
4661 if (request->fl_start < 0 || request->fl_end < 0)
4664 if (IS_GETLK(cmd)) {
4666 return nfs4_proc_getlk(state, F_GETLK, request);
4670 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4673 if (request->fl_type == F_UNLCK) {
4675 return nfs4_proc_unlck(state, cmd, request);
4682 status = nfs4_proc_setlk(state, cmd, request);
4683 if ((status != -EAGAIN) || IS_SETLK(cmd))
4685 timeout = nfs4_set_lock_task_retry(timeout);
4686 status = -ERESTARTSYS;
4689 } while(status < 0);
4693 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4695 struct nfs_server *server = NFS_SERVER(state->inode);
4696 struct nfs4_exception exception = { };
4699 err = nfs4_set_lock_state(state, fl);
4703 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4706 printk(KERN_ERR "NFS: %s: unhandled error "
4707 "%d.\n", __func__, err);
4711 case -NFS4ERR_EXPIRED:
4712 nfs4_schedule_stateid_recovery(server, state);
4713 case -NFS4ERR_STALE_CLIENTID:
4714 case -NFS4ERR_STALE_STATEID:
4715 nfs4_schedule_lease_recovery(server->nfs_client);
4717 case -NFS4ERR_BADSESSION:
4718 case -NFS4ERR_BADSLOT:
4719 case -NFS4ERR_BAD_HIGH_SLOT:
4720 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4721 case -NFS4ERR_DEADSESSION:
4722 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
4726 * The show must go on: exit, but mark the
4727 * stateid as needing recovery.
4729 case -NFS4ERR_DELEG_REVOKED:
4730 case -NFS4ERR_ADMIN_REVOKED:
4731 case -NFS4ERR_BAD_STATEID:
4732 case -NFS4ERR_OPENMODE:
4733 nfs4_schedule_stateid_recovery(server, state);
4738 * User RPCSEC_GSS context has expired.
4739 * We cannot recover this stateid now, so
4740 * skip it and allow recovery thread to
4746 case -NFS4ERR_DENIED:
4747 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4750 case -NFS4ERR_DELAY:
4753 err = nfs4_handle_exception(server, err, &exception);
4754 } while (exception.retry);
4759 struct nfs_release_lockowner_data {
4760 struct nfs4_lock_state *lsp;
4761 struct nfs_release_lockowner_args args;
4764 static void nfs4_release_lockowner_release(void *calldata)
4766 struct nfs_release_lockowner_data *data = calldata;
4767 nfs4_free_lock_state(data->lsp);
4771 const struct rpc_call_ops nfs4_release_lockowner_ops = {
4772 .rpc_release = nfs4_release_lockowner_release,
4775 int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
4777 struct nfs_server *server = lsp->ls_state->owner->so_server;
4778 struct nfs_release_lockowner_data *data;
4779 struct rpc_message msg = {
4780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4783 if (server->nfs_client->cl_mvops->minor_version != 0)
4785 data = kmalloc(sizeof(*data), GFP_NOFS);
4789 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4790 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4791 data->args.lock_owner.s_dev = server->s_dev;
4792 msg.rpc_argp = &data->args;
4793 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4797 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4799 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4800 const void *buf, size_t buflen,
4801 int flags, int type)
4803 if (strcmp(key, "") != 0)
4806 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
4809 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4810 void *buf, size_t buflen, int type)
4812 if (strcmp(key, "") != 0)
4815 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
4818 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4819 size_t list_len, const char *name,
4820 size_t name_len, int type)
4822 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
4824 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4827 if (list && len <= list_len)
4828 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
4833 * nfs_fhget will use either the mounted_on_fileid or the fileid
4835 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4837 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4838 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4839 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4840 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
4843 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4844 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
4845 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4849 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4850 struct nfs4_fs_locations *fs_locations, struct page *page)
4852 struct nfs_server *server = NFS_SERVER(dir);
4854 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4856 struct nfs4_fs_locations_arg args = {
4857 .dir_fh = NFS_FH(dir),
4862 struct nfs4_fs_locations_res res = {
4863 .fs_locations = fs_locations,
4865 struct rpc_message msg = {
4866 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4872 dprintk("%s: start\n", __func__);
4874 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4875 * is not supported */
4876 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4877 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4879 bitmask[0] |= FATTR4_WORD0_FILEID;
4881 nfs_fattr_init(&fs_locations->fattr);
4882 fs_locations->server = server;
4883 fs_locations->nlocations = 0;
4884 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4885 dprintk("%s: returned status = %d\n", __func__, status);
4889 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4892 struct nfs4_secinfo_arg args = {
4893 .dir_fh = NFS_FH(dir),
4896 struct nfs4_secinfo_res res = {
4899 struct rpc_message msg = {
4900 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4905 dprintk("NFS call secinfo %s\n", name->name);
4906 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4907 dprintk("NFS reply secinfo: %d\n", status);
4911 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4913 struct nfs4_exception exception = { };
4916 err = nfs4_handle_exception(NFS_SERVER(dir),
4917 _nfs4_proc_secinfo(dir, name, flavors),
4919 } while (exception.retry);
4923 #ifdef CONFIG_NFS_V4_1
4925 * Check the exchange flags returned by the server for invalid flags, having
4926 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4929 static int nfs4_check_cl_exchange_flags(u32 flags)
4931 if (flags & ~EXCHGID4_FLAG_MASK_R)
4933 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4934 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4936 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4940 return -NFS4ERR_INVAL;
4944 nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4946 if (a->server_scope_sz == b->server_scope_sz &&
4947 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4954 * nfs4_proc_exchange_id()
4956 * Since the clientid has expired, all compounds using sessions
4957 * associated with the stale clientid will be returning
4958 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4959 * be in some phase of session reset.
4961 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4963 nfs4_verifier verifier;
4964 struct nfs41_exchange_id_args args = {
4965 .verifier = &verifier,
4967 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
4969 struct nfs41_exchange_id_res res = {
4973 struct rpc_message msg = {
4974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4980 dprintk("--> %s\n", __func__);
4981 BUG_ON(clp == NULL);
4983 nfs4_construct_boot_verifier(clp, &verifier);
4985 args.id_len = scnprintf(args.id, sizeof(args.id),
4988 init_utsname()->nodename,
4989 init_utsname()->domainname,
4990 clp->cl_rpcclient->cl_auth->au_flavor);
4992 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
4993 if (unlikely(!res.server_scope)) {
4998 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
4999 if (unlikely(!res.impl_id)) {
5001 goto out_server_scope;
5004 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5006 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
5009 /* use the most recent implementation id */
5010 kfree(clp->impl_id);
5011 clp->impl_id = res.impl_id;
5016 if (clp->server_scope &&
5017 !nfs41_same_server_scope(clp->server_scope,
5018 res.server_scope)) {
5019 dprintk("%s: server_scope mismatch detected\n",
5021 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5022 kfree(clp->server_scope);
5023 clp->server_scope = NULL;
5026 if (!clp->server_scope) {
5027 clp->server_scope = res.server_scope;
5033 kfree(res.server_scope);
5036 dprintk("%s: Server Implementation ID: "
5037 "domain: %s, name: %s, date: %llu,%u\n",
5038 __func__, clp->impl_id->domain, clp->impl_id->name,
5039 clp->impl_id->date.seconds,
5040 clp->impl_id->date.nseconds);
5041 dprintk("<-- %s status= %d\n", __func__, status);
5045 struct nfs4_get_lease_time_data {
5046 struct nfs4_get_lease_time_args *args;
5047 struct nfs4_get_lease_time_res *res;
5048 struct nfs_client *clp;
5051 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5055 struct nfs4_get_lease_time_data *data =
5056 (struct nfs4_get_lease_time_data *)calldata;
5058 dprintk("--> %s\n", __func__);
5059 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5060 /* just setup sequence, do not trigger session recovery
5061 since we're invoked within one */
5062 ret = nfs41_setup_sequence(data->clp->cl_session,
5063 &data->args->la_seq_args,
5064 &data->res->lr_seq_res, task);
5066 BUG_ON(ret == -EAGAIN);
5067 rpc_call_start(task);
5068 dprintk("<-- %s\n", __func__);
5072 * Called from nfs4_state_manager thread for session setup, so don't recover
5073 * from sequence operation or clientid errors.
5075 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5077 struct nfs4_get_lease_time_data *data =
5078 (struct nfs4_get_lease_time_data *)calldata;
5080 dprintk("--> %s\n", __func__);
5081 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5083 switch (task->tk_status) {
5084 case -NFS4ERR_DELAY:
5085 case -NFS4ERR_GRACE:
5086 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5087 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5088 task->tk_status = 0;
5090 case -NFS4ERR_RETRY_UNCACHED_REP:
5091 rpc_restart_call_prepare(task);
5094 dprintk("<-- %s\n", __func__);
5097 struct rpc_call_ops nfs4_get_lease_time_ops = {
5098 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5099 .rpc_call_done = nfs4_get_lease_time_done,
5102 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5104 struct rpc_task *task;
5105 struct nfs4_get_lease_time_args args;
5106 struct nfs4_get_lease_time_res res = {
5107 .lr_fsinfo = fsinfo,
5109 struct nfs4_get_lease_time_data data = {
5114 struct rpc_message msg = {
5115 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5119 struct rpc_task_setup task_setup = {
5120 .rpc_client = clp->cl_rpcclient,
5121 .rpc_message = &msg,
5122 .callback_ops = &nfs4_get_lease_time_ops,
5123 .callback_data = &data,
5124 .flags = RPC_TASK_TIMEOUT,
5128 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5129 dprintk("--> %s\n", __func__);
5130 task = rpc_run_task(&task_setup);
5133 status = PTR_ERR(task);
5135 status = task->tk_status;
5138 dprintk("<-- %s return %d\n", __func__, status);
5143 static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5145 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5148 static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5149 struct nfs4_slot *new,
5153 struct nfs4_slot *old = NULL;
5156 spin_lock(&tbl->slot_tbl_lock);
5160 tbl->max_slots = max_slots;
5162 tbl->highest_used_slotid = -1; /* no slot is currently used */
5163 for (i = 0; i < tbl->max_slots; i++)
5164 tbl->slots[i].seq_nr = ivalue;
5165 spin_unlock(&tbl->slot_tbl_lock);
5170 * (re)Initialise a slot table
5172 static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5175 struct nfs4_slot *new = NULL;
5178 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5179 max_reqs, tbl->max_slots);
5181 /* Does the newly negotiated max_reqs match the existing slot table? */
5182 if (max_reqs != tbl->max_slots) {
5183 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
5189 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
5190 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5191 tbl, tbl->slots, tbl->max_slots);
5193 dprintk("<-- %s: return %d\n", __func__, ret);
5197 /* Destroy the slot table */
5198 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5200 if (session->fc_slot_table.slots != NULL) {
5201 kfree(session->fc_slot_table.slots);
5202 session->fc_slot_table.slots = NULL;
5204 if (session->bc_slot_table.slots != NULL) {
5205 kfree(session->bc_slot_table.slots);
5206 session->bc_slot_table.slots = NULL;
5212 * Initialize or reset the forechannel and backchannel tables
5214 static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
5216 struct nfs4_slot_table *tbl;
5219 dprintk("--> %s\n", __func__);
5221 tbl = &ses->fc_slot_table;
5222 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5223 if (status) /* -ENOMEM */
5226 tbl = &ses->bc_slot_table;
5227 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5228 if (status && tbl->slots == NULL)
5229 /* Fore and back channel share a connection so get
5230 * both slot tables or neither */
5231 nfs4_destroy_slot_tables(ses);
5235 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5237 struct nfs4_session *session;
5238 struct nfs4_slot_table *tbl;
5240 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5244 tbl = &session->fc_slot_table;
5245 tbl->highest_used_slotid = NFS4_NO_SLOT;
5246 spin_lock_init(&tbl->slot_tbl_lock);
5247 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5248 init_completion(&tbl->complete);
5250 tbl = &session->bc_slot_table;
5251 tbl->highest_used_slotid = NFS4_NO_SLOT;
5252 spin_lock_init(&tbl->slot_tbl_lock);
5253 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5254 init_completion(&tbl->complete);
5256 session->session_state = 1<<NFS4_SESSION_INITING;
5262 void nfs4_destroy_session(struct nfs4_session *session)
5264 struct rpc_xprt *xprt;
5266 nfs4_proc_destroy_session(session);
5269 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5271 dprintk("%s Destroy backchannel for xprt %p\n",
5273 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
5274 nfs4_destroy_slot_tables(session);
5279 * Initialize the values to be used by the client in CREATE_SESSION
5280 * If nfs4_init_session set the fore channel request and response sizes,
5283 * Set the back channel max_resp_sz_cached to zero to force the client to
5284 * always set csa_cachethis to FALSE because the current implementation
5285 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5287 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5289 struct nfs4_session *session = args->client->cl_session;
5290 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5291 mxresp_sz = session->fc_attrs.max_resp_sz;
5294 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5296 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5297 /* Fore channel attributes */
5298 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5299 args->fc_attrs.max_resp_sz = mxresp_sz;
5300 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5301 args->fc_attrs.max_reqs = max_session_slots;
5303 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5304 "max_ops=%u max_reqs=%u\n",
5306 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5307 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5309 /* Back channel attributes */
5310 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5311 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5312 args->bc_attrs.max_resp_sz_cached = 0;
5313 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5314 args->bc_attrs.max_reqs = 1;
5316 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5317 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5319 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5320 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5321 args->bc_attrs.max_reqs);
5324 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5326 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5327 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5329 if (rcvd->max_resp_sz > sent->max_resp_sz)
5332 * Our requested max_ops is the minimum we need; we're not
5333 * prepared to break up compounds into smaller pieces than that.
5334 * So, no point even trying to continue if the server won't
5337 if (rcvd->max_ops < sent->max_ops)
5339 if (rcvd->max_reqs == 0)
5341 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5342 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5346 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5348 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5349 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5351 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5353 if (rcvd->max_resp_sz < sent->max_resp_sz)
5355 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5357 /* These would render the backchannel useless: */
5358 if (rcvd->max_ops != sent->max_ops)
5360 if (rcvd->max_reqs != sent->max_reqs)
5365 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5366 struct nfs4_session *session)
5370 ret = nfs4_verify_fore_channel_attrs(args, session);
5373 return nfs4_verify_back_channel_attrs(args, session);
5376 static int _nfs4_proc_create_session(struct nfs_client *clp)
5378 struct nfs4_session *session = clp->cl_session;
5379 struct nfs41_create_session_args args = {
5381 .cb_program = NFS4_CALLBACK,
5383 struct nfs41_create_session_res res = {
5386 struct rpc_message msg = {
5387 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5393 nfs4_init_channel_attrs(&args);
5394 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5396 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5399 /* Verify the session's negotiated channel_attrs values */
5400 status = nfs4_verify_channel_attrs(&args, session);
5402 /* Increment the clientid slot sequence id */
5410 * Issues a CREATE_SESSION operation to the server.
5411 * It is the responsibility of the caller to verify the session is
5412 * expired before calling this routine.
5414 int nfs4_proc_create_session(struct nfs_client *clp)
5418 struct nfs4_session *session = clp->cl_session;
5420 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5422 status = _nfs4_proc_create_session(clp);
5426 /* Init or reset the session slot tables */
5427 status = nfs4_setup_session_slot_tables(session);
5428 dprintk("slot table setup returned %d\n", status);
5432 ptr = (unsigned *)&session->sess_id.data[0];
5433 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5434 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5436 dprintk("<-- %s\n", __func__);
5441 * Issue the over-the-wire RPC DESTROY_SESSION.
5442 * The caller must serialize access to this routine.
5444 int nfs4_proc_destroy_session(struct nfs4_session *session)
5447 struct rpc_message msg;
5449 dprintk("--> nfs4_proc_destroy_session\n");
5451 /* session is still being setup */
5452 if (session->clp->cl_cons_state != NFS_CS_READY)
5455 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5456 msg.rpc_argp = session;
5457 msg.rpc_resp = NULL;
5458 msg.rpc_cred = NULL;
5459 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5463 "NFS: Got error %d from the server on DESTROY_SESSION. "
5464 "Session has been destroyed regardless...\n", status);
5466 dprintk("<-- nfs4_proc_destroy_session\n");
5470 int nfs4_init_session(struct nfs_server *server)
5472 struct nfs_client *clp = server->nfs_client;
5473 struct nfs4_session *session;
5474 unsigned int rsize, wsize;
5477 if (!nfs4_has_session(clp))
5480 session = clp->cl_session;
5481 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5484 rsize = server->rsize;
5486 rsize = NFS_MAX_FILE_IO_SIZE;
5487 wsize = server->wsize;
5489 wsize = NFS_MAX_FILE_IO_SIZE;
5491 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5492 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5494 ret = nfs4_recover_expired_lease(server);
5496 ret = nfs4_check_client_ready(clp);
5500 int nfs4_init_ds_session(struct nfs_client *clp)
5502 struct nfs4_session *session = clp->cl_session;
5505 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5508 ret = nfs4_client_recover_expired_lease(clp);
5510 /* Test for the DS role */
5511 if (!is_ds_client(clp))
5514 ret = nfs4_check_client_ready(clp);
5518 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5522 * Renew the cl_session lease.
5524 struct nfs4_sequence_data {
5525 struct nfs_client *clp;
5526 struct nfs4_sequence_args args;
5527 struct nfs4_sequence_res res;
5530 static void nfs41_sequence_release(void *data)
5532 struct nfs4_sequence_data *calldata = data;
5533 struct nfs_client *clp = calldata->clp;
5535 if (atomic_read(&clp->cl_count) > 1)
5536 nfs4_schedule_state_renewal(clp);
5537 nfs_put_client(clp);
5541 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5543 switch(task->tk_status) {
5544 case -NFS4ERR_DELAY:
5545 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5548 nfs4_schedule_lease_recovery(clp);
5553 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5555 struct nfs4_sequence_data *calldata = data;
5556 struct nfs_client *clp = calldata->clp;
5558 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5561 if (task->tk_status < 0) {
5562 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5563 if (atomic_read(&clp->cl_count) == 1)
5566 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5567 rpc_restart_call_prepare(task);
5571 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5573 dprintk("<-- %s\n", __func__);
5576 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5578 struct nfs4_sequence_data *calldata = data;
5579 struct nfs_client *clp = calldata->clp;
5580 struct nfs4_sequence_args *args;
5581 struct nfs4_sequence_res *res;
5583 args = task->tk_msg.rpc_argp;
5584 res = task->tk_msg.rpc_resp;
5586 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
5588 rpc_call_start(task);
5591 static const struct rpc_call_ops nfs41_sequence_ops = {
5592 .rpc_call_done = nfs41_sequence_call_done,
5593 .rpc_call_prepare = nfs41_sequence_prepare,
5594 .rpc_release = nfs41_sequence_release,
5597 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5599 struct nfs4_sequence_data *calldata;
5600 struct rpc_message msg = {
5601 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5604 struct rpc_task_setup task_setup_data = {
5605 .rpc_client = clp->cl_rpcclient,
5606 .rpc_message = &msg,
5607 .callback_ops = &nfs41_sequence_ops,
5608 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5611 if (!atomic_inc_not_zero(&clp->cl_count))
5612 return ERR_PTR(-EIO);
5613 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5614 if (calldata == NULL) {
5615 nfs_put_client(clp);
5616 return ERR_PTR(-ENOMEM);
5618 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
5619 msg.rpc_argp = &calldata->args;
5620 msg.rpc_resp = &calldata->res;
5621 calldata->clp = clp;
5622 task_setup_data.callback_data = calldata;
5624 return rpc_run_task(&task_setup_data);
5627 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5629 struct rpc_task *task;
5632 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5634 task = _nfs41_proc_sequence(clp, cred);
5636 ret = PTR_ERR(task);
5638 rpc_put_task_async(task);
5639 dprintk("<-- %s status=%d\n", __func__, ret);
5643 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5645 struct rpc_task *task;
5648 task = _nfs41_proc_sequence(clp, cred);
5650 ret = PTR_ERR(task);
5653 ret = rpc_wait_for_completion_task(task);
5655 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5657 if (task->tk_status == 0)
5658 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5659 ret = task->tk_status;
5663 dprintk("<-- %s status=%d\n", __func__, ret);
5667 struct nfs4_reclaim_complete_data {
5668 struct nfs_client *clp;
5669 struct nfs41_reclaim_complete_args arg;
5670 struct nfs41_reclaim_complete_res res;
5673 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5675 struct nfs4_reclaim_complete_data *calldata = data;
5677 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5678 if (nfs41_setup_sequence(calldata->clp->cl_session,
5679 &calldata->arg.seq_args,
5680 &calldata->res.seq_res, task))
5683 rpc_call_start(task);
5686 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5688 switch(task->tk_status) {
5690 case -NFS4ERR_COMPLETE_ALREADY:
5691 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5693 case -NFS4ERR_DELAY:
5694 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5696 case -NFS4ERR_RETRY_UNCACHED_REP:
5699 nfs4_schedule_lease_recovery(clp);
5704 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5706 struct nfs4_reclaim_complete_data *calldata = data;
5707 struct nfs_client *clp = calldata->clp;
5708 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5710 dprintk("--> %s\n", __func__);
5711 if (!nfs41_sequence_done(task, res))
5714 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5715 rpc_restart_call_prepare(task);
5718 dprintk("<-- %s\n", __func__);
5721 static void nfs4_free_reclaim_complete_data(void *data)
5723 struct nfs4_reclaim_complete_data *calldata = data;
5728 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5729 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5730 .rpc_call_done = nfs4_reclaim_complete_done,
5731 .rpc_release = nfs4_free_reclaim_complete_data,
5735 * Issue a global reclaim complete.
5737 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5739 struct nfs4_reclaim_complete_data *calldata;
5740 struct rpc_task *task;
5741 struct rpc_message msg = {
5742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5744 struct rpc_task_setup task_setup_data = {
5745 .rpc_client = clp->cl_rpcclient,
5746 .rpc_message = &msg,
5747 .callback_ops = &nfs4_reclaim_complete_call_ops,
5748 .flags = RPC_TASK_ASYNC,
5750 int status = -ENOMEM;
5752 dprintk("--> %s\n", __func__);
5753 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5754 if (calldata == NULL)
5756 calldata->clp = clp;
5757 calldata->arg.one_fs = 0;
5759 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
5760 msg.rpc_argp = &calldata->arg;
5761 msg.rpc_resp = &calldata->res;
5762 task_setup_data.callback_data = calldata;
5763 task = rpc_run_task(&task_setup_data);
5765 status = PTR_ERR(task);
5768 status = nfs4_wait_for_completion_rpc_task(task);
5770 status = task->tk_status;
5774 dprintk("<-- %s status=%d\n", __func__, status);
5779 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5781 struct nfs4_layoutget *lgp = calldata;
5782 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5784 dprintk("--> %s\n", __func__);
5785 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5786 * right now covering the LAYOUTGET we are about to send.
5787 * However, that is not so catastrophic, and there seems
5788 * to be no way to prevent it completely.
5790 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5791 &lgp->res.seq_res, task))
5793 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5794 NFS_I(lgp->args.inode)->layout,
5795 lgp->args.ctx->state)) {
5796 rpc_exit(task, NFS4_OK);
5799 rpc_call_start(task);
5802 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5804 struct nfs4_layoutget *lgp = calldata;
5805 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5807 dprintk("--> %s\n", __func__);
5809 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5812 switch (task->tk_status) {
5815 case -NFS4ERR_LAYOUTTRYLATER:
5816 case -NFS4ERR_RECALLCONFLICT:
5817 task->tk_status = -NFS4ERR_DELAY;
5820 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5821 rpc_restart_call_prepare(task);
5825 dprintk("<-- %s\n", __func__);
5828 static void nfs4_layoutget_release(void *calldata)
5830 struct nfs4_layoutget *lgp = calldata;
5832 dprintk("--> %s\n", __func__);
5833 put_nfs_open_context(lgp->args.ctx);
5835 dprintk("<-- %s\n", __func__);
5838 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5839 .rpc_call_prepare = nfs4_layoutget_prepare,
5840 .rpc_call_done = nfs4_layoutget_done,
5841 .rpc_release = nfs4_layoutget_release,
5844 int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5846 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5847 struct rpc_task *task;
5848 struct rpc_message msg = {
5849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5850 .rpc_argp = &lgp->args,
5851 .rpc_resp = &lgp->res,
5853 struct rpc_task_setup task_setup_data = {
5854 .rpc_client = server->client,
5855 .rpc_message = &msg,
5856 .callback_ops = &nfs4_layoutget_call_ops,
5857 .callback_data = lgp,
5858 .flags = RPC_TASK_ASYNC,
5862 dprintk("--> %s\n", __func__);
5864 lgp->res.layoutp = &lgp->args.layout;
5865 lgp->res.seq_res.sr_slot = NULL;
5866 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
5867 task = rpc_run_task(&task_setup_data);
5869 return PTR_ERR(task);
5870 status = nfs4_wait_for_completion_rpc_task(task);
5872 status = task->tk_status;
5874 status = pnfs_layout_process(lgp);
5876 dprintk("<-- %s status=%d\n", __func__, status);
5881 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5883 struct nfs4_layoutreturn *lrp = calldata;
5885 dprintk("--> %s\n", __func__);
5886 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5887 &lrp->res.seq_res, task))
5889 rpc_call_start(task);
5892 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5894 struct nfs4_layoutreturn *lrp = calldata;
5895 struct nfs_server *server;
5896 struct pnfs_layout_hdr *lo = lrp->args.layout;
5898 dprintk("--> %s\n", __func__);
5900 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5903 server = NFS_SERVER(lrp->args.inode);
5904 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5905 rpc_restart_call_prepare(task);
5908 spin_lock(&lo->plh_inode->i_lock);
5909 if (task->tk_status == 0) {
5910 if (lrp->res.lrs_present) {
5911 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
5913 BUG_ON(!list_empty(&lo->plh_segs));
5915 lo->plh_block_lgets--;
5916 spin_unlock(&lo->plh_inode->i_lock);
5917 dprintk("<-- %s\n", __func__);
5920 static void nfs4_layoutreturn_release(void *calldata)
5922 struct nfs4_layoutreturn *lrp = calldata;
5924 dprintk("--> %s\n", __func__);
5925 put_layout_hdr(lrp->args.layout);
5927 dprintk("<-- %s\n", __func__);
5930 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5931 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5932 .rpc_call_done = nfs4_layoutreturn_done,
5933 .rpc_release = nfs4_layoutreturn_release,
5936 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5938 struct rpc_task *task;
5939 struct rpc_message msg = {
5940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5941 .rpc_argp = &lrp->args,
5942 .rpc_resp = &lrp->res,
5944 struct rpc_task_setup task_setup_data = {
5945 .rpc_client = lrp->clp->cl_rpcclient,
5946 .rpc_message = &msg,
5947 .callback_ops = &nfs4_layoutreturn_call_ops,
5948 .callback_data = lrp,
5952 dprintk("--> %s\n", __func__);
5953 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
5954 task = rpc_run_task(&task_setup_data);
5956 return PTR_ERR(task);
5957 status = task->tk_status;
5958 dprintk("<-- %s status=%d\n", __func__, status);
5964 * Retrieve the list of Data Server devices from the MDS.
5966 static int _nfs4_getdevicelist(struct nfs_server *server,
5967 const struct nfs_fh *fh,
5968 struct pnfs_devicelist *devlist)
5970 struct nfs4_getdevicelist_args args = {
5972 .layoutclass = server->pnfs_curr_ld->id,
5974 struct nfs4_getdevicelist_res res = {
5977 struct rpc_message msg = {
5978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5984 dprintk("--> %s\n", __func__);
5985 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5987 dprintk("<-- %s status=%d\n", __func__, status);
5991 int nfs4_proc_getdevicelist(struct nfs_server *server,
5992 const struct nfs_fh *fh,
5993 struct pnfs_devicelist *devlist)
5995 struct nfs4_exception exception = { };
5999 err = nfs4_handle_exception(server,
6000 _nfs4_getdevicelist(server, fh, devlist),
6002 } while (exception.retry);
6004 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6005 err, devlist->num_devs);
6009 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6012 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6014 struct nfs4_getdeviceinfo_args args = {
6017 struct nfs4_getdeviceinfo_res res = {
6020 struct rpc_message msg = {
6021 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6027 dprintk("--> %s\n", __func__);
6028 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6029 dprintk("<-- %s status=%d\n", __func__, status);
6034 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6036 struct nfs4_exception exception = { };
6040 err = nfs4_handle_exception(server,
6041 _nfs4_proc_getdeviceinfo(server, pdev),
6043 } while (exception.retry);
6046 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6048 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6050 struct nfs4_layoutcommit_data *data = calldata;
6051 struct nfs_server *server = NFS_SERVER(data->args.inode);
6053 if (nfs4_setup_sequence(server, &data->args.seq_args,
6054 &data->res.seq_res, task))
6056 rpc_call_start(task);
6060 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6062 struct nfs4_layoutcommit_data *data = calldata;
6063 struct nfs_server *server = NFS_SERVER(data->args.inode);
6065 if (!nfs4_sequence_done(task, &data->res.seq_res))
6068 switch (task->tk_status) { /* Just ignore these failures */
6069 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6070 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6071 case NFS4ERR_BADLAYOUT: /* no layout */
6072 case NFS4ERR_GRACE: /* loca_recalim always false */
6073 task->tk_status = 0;
6076 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6077 rpc_restart_call_prepare(task);
6081 if (task->tk_status == 0)
6082 nfs_post_op_update_inode_force_wcc(data->args.inode,
6086 static void nfs4_layoutcommit_release(void *calldata)
6088 struct nfs4_layoutcommit_data *data = calldata;
6089 struct pnfs_layout_segment *lseg, *tmp;
6090 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
6092 pnfs_cleanup_layoutcommit(data);
6093 /* Matched by references in pnfs_set_layoutcommit */
6094 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6095 list_del_init(&lseg->pls_lc_list);
6096 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6101 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6102 smp_mb__after_clear_bit();
6103 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6105 put_rpccred(data->cred);
6109 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6110 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6111 .rpc_call_done = nfs4_layoutcommit_done,
6112 .rpc_release = nfs4_layoutcommit_release,
6116 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6118 struct rpc_message msg = {
6119 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6120 .rpc_argp = &data->args,
6121 .rpc_resp = &data->res,
6122 .rpc_cred = data->cred,
6124 struct rpc_task_setup task_setup_data = {
6125 .task = &data->task,
6126 .rpc_client = NFS_CLIENT(data->args.inode),
6127 .rpc_message = &msg,
6128 .callback_ops = &nfs4_layoutcommit_ops,
6129 .callback_data = data,
6130 .flags = RPC_TASK_ASYNC,
6132 struct rpc_task *task;
6135 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6136 "lbw: %llu inode %lu\n",
6137 data->task.tk_pid, sync,
6138 data->args.lastbytewritten,
6139 data->args.inode->i_ino);
6141 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6142 task = rpc_run_task(&task_setup_data);
6144 return PTR_ERR(task);
6147 status = nfs4_wait_for_completion_rpc_task(task);
6150 status = task->tk_status;
6152 dprintk("%s: status %d\n", __func__, status);
6158 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6159 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6161 struct nfs41_secinfo_no_name_args args = {
6162 .style = SECINFO_STYLE_CURRENT_FH,
6164 struct nfs4_secinfo_res res = {
6167 struct rpc_message msg = {
6168 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6172 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6176 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6177 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6179 struct nfs4_exception exception = { };
6182 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6185 case -NFS4ERR_WRONGSEC:
6186 case -NFS4ERR_NOTSUPP:
6189 err = nfs4_handle_exception(server, err, &exception);
6191 } while (exception.retry);
6196 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6197 struct nfs_fsinfo *info)
6201 rpc_authflavor_t flavor;
6202 struct nfs4_secinfo_flavors *flavors;
6204 page = alloc_page(GFP_KERNEL);
6210 flavors = page_address(page);
6211 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6214 * Fall back on "guess and check" method if
6215 * the server doesn't support SECINFO_NO_NAME
6217 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6218 err = nfs4_find_root_sec(server, fhandle, info);
6224 flavor = nfs_find_best_sec(flavors);
6226 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6236 static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6239 struct nfs41_test_stateid_args args = {
6242 struct nfs41_test_stateid_res res;
6243 struct rpc_message msg = {
6244 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6249 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6250 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6252 if (status == NFS_OK)
6257 static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6259 struct nfs4_exception exception = { };
6262 err = nfs4_handle_exception(server,
6263 _nfs41_test_stateid(server, stateid),
6265 } while (exception.retry);
6269 static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6271 struct nfs41_free_stateid_args args = {
6274 struct nfs41_free_stateid_res res;
6275 struct rpc_message msg = {
6276 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6281 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6282 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6285 static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6287 struct nfs4_exception exception = { };
6290 err = nfs4_handle_exception(server,
6291 _nfs4_free_stateid(server, stateid),
6293 } while (exception.retry);
6297 static bool nfs41_match_stateid(const nfs4_stateid *s1,
6298 const nfs4_stateid *s2)
6300 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
6303 if (s1->seqid == s2->seqid)
6305 if (s1->seqid == 0 || s2->seqid == 0)
6311 #endif /* CONFIG_NFS_V4_1 */
6313 static bool nfs4_match_stateid(const nfs4_stateid *s1,
6314 const nfs4_stateid *s2)
6316 return nfs4_stateid_match(s1, s2);
6320 struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6321 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6322 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6323 .recover_open = nfs4_open_reclaim,
6324 .recover_lock = nfs4_lock_reclaim,
6325 .establish_clid = nfs4_init_clientid,
6326 .get_clid_cred = nfs4_get_setclientid_cred,
6329 #if defined(CONFIG_NFS_V4_1)
6330 struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6331 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6332 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6333 .recover_open = nfs4_open_reclaim,
6334 .recover_lock = nfs4_lock_reclaim,
6335 .establish_clid = nfs41_init_clientid,
6336 .get_clid_cred = nfs4_get_exchange_id_cred,
6337 .reclaim_complete = nfs41_proc_reclaim_complete,
6339 #endif /* CONFIG_NFS_V4_1 */
6341 struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6342 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6343 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6344 .recover_open = nfs4_open_expired,
6345 .recover_lock = nfs4_lock_expired,
6346 .establish_clid = nfs4_init_clientid,
6347 .get_clid_cred = nfs4_get_setclientid_cred,
6350 #if defined(CONFIG_NFS_V4_1)
6351 struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6352 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6353 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6354 .recover_open = nfs41_open_expired,
6355 .recover_lock = nfs41_lock_expired,
6356 .establish_clid = nfs41_init_clientid,
6357 .get_clid_cred = nfs4_get_exchange_id_cred,
6359 #endif /* CONFIG_NFS_V4_1 */
6361 struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6362 .sched_state_renewal = nfs4_proc_async_renew,
6363 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6364 .renew_lease = nfs4_proc_renew,
6367 #if defined(CONFIG_NFS_V4_1)
6368 struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6369 .sched_state_renewal = nfs41_proc_async_sequence,
6370 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
6371 .renew_lease = nfs4_proc_sequence,
6375 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6377 .call_sync = _nfs4_call_sync,
6378 .match_stateid = nfs4_match_stateid,
6379 .find_root_sec = nfs4_find_root_sec,
6380 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6381 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6382 .state_renewal_ops = &nfs40_state_renewal_ops,
6385 #if defined(CONFIG_NFS_V4_1)
6386 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6388 .call_sync = _nfs4_call_sync_session,
6389 .match_stateid = nfs41_match_stateid,
6390 .find_root_sec = nfs41_find_root_sec,
6391 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6392 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6393 .state_renewal_ops = &nfs41_state_renewal_ops,
6397 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6398 [0] = &nfs_v4_0_minor_ops,
6399 #if defined(CONFIG_NFS_V4_1)
6400 [1] = &nfs_v4_1_minor_ops,
6404 static const struct inode_operations nfs4_file_inode_operations = {
6405 .permission = nfs_permission,
6406 .getattr = nfs_getattr,
6407 .setattr = nfs_setattr,
6408 .getxattr = generic_getxattr,
6409 .setxattr = generic_setxattr,
6410 .listxattr = generic_listxattr,
6411 .removexattr = generic_removexattr,
6414 const struct nfs_rpc_ops nfs_v4_clientops = {
6415 .version = 4, /* protocol version */
6416 .dentry_ops = &nfs4_dentry_operations,
6417 .dir_inode_ops = &nfs4_dir_inode_operations,
6418 .file_inode_ops = &nfs4_file_inode_operations,
6419 .file_ops = &nfs4_file_operations,
6420 .getroot = nfs4_proc_get_root,
6421 .getattr = nfs4_proc_getattr,
6422 .setattr = nfs4_proc_setattr,
6423 .lookup = nfs4_proc_lookup,
6424 .access = nfs4_proc_access,
6425 .readlink = nfs4_proc_readlink,
6426 .create = nfs4_proc_create,
6427 .remove = nfs4_proc_remove,
6428 .unlink_setup = nfs4_proc_unlink_setup,
6429 .unlink_done = nfs4_proc_unlink_done,
6430 .rename = nfs4_proc_rename,
6431 .rename_setup = nfs4_proc_rename_setup,
6432 .rename_done = nfs4_proc_rename_done,
6433 .link = nfs4_proc_link,
6434 .symlink = nfs4_proc_symlink,
6435 .mkdir = nfs4_proc_mkdir,
6436 .rmdir = nfs4_proc_remove,
6437 .readdir = nfs4_proc_readdir,
6438 .mknod = nfs4_proc_mknod,
6439 .statfs = nfs4_proc_statfs,
6440 .fsinfo = nfs4_proc_fsinfo,
6441 .pathconf = nfs4_proc_pathconf,
6442 .set_capabilities = nfs4_server_capabilities,
6443 .decode_dirent = nfs4_decode_dirent,
6444 .read_setup = nfs4_proc_read_setup,
6445 .read_done = nfs4_read_done,
6446 .write_setup = nfs4_proc_write_setup,
6447 .write_done = nfs4_write_done,
6448 .commit_setup = nfs4_proc_commit_setup,
6449 .commit_done = nfs4_commit_done,
6450 .lock = nfs4_proc_lock,
6451 .clear_acl_cache = nfs4_zap_acl_attr,
6452 .close_context = nfs4_close_context,
6453 .open_context = nfs4_atomic_open,
6454 .init_client = nfs4_init_client,
6455 .secinfo = nfs4_proc_secinfo,
6458 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6459 .prefix = XATTR_NAME_NFSV4_ACL,
6460 .list = nfs4_xattr_list_nfs4_acl,
6461 .get = nfs4_xattr_get_nfs4_acl,
6462 .set = nfs4_xattr_set_nfs4_acl,
6465 const struct xattr_handler *nfs4_xattr_handlers[] = {
6466 &nfs4_xattr_nfs4_acl_handler,
6470 module_param(max_session_slots, ushort, 0644);
6471 MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6472 "requests the client will negotiate");