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 static const 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 static const 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 pr_warn_ratelimited("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 (state != NULL) {
1933 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1934 current->files, current->tgid);
1935 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1937 /* Use that stateid */
1939 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
1941 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1942 if (status == 0 && state != NULL)
1943 renew_lease(server, timestamp);
1947 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1948 struct nfs_fattr *fattr, struct iattr *sattr,
1949 struct nfs4_state *state)
1951 struct nfs_server *server = NFS_SERVER(inode);
1952 struct nfs4_exception exception = {
1958 err = nfs4_handle_exception(server,
1959 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
1961 } while (exception.retry);
1965 struct nfs4_closedata {
1966 struct inode *inode;
1967 struct nfs4_state *state;
1968 struct nfs_closeargs arg;
1969 struct nfs_closeres res;
1970 struct nfs_fattr fattr;
1971 unsigned long timestamp;
1976 static void nfs4_free_closedata(void *data)
1978 struct nfs4_closedata *calldata = data;
1979 struct nfs4_state_owner *sp = calldata->state->owner;
1980 struct super_block *sb = calldata->state->inode->i_sb;
1983 pnfs_roc_release(calldata->state->inode);
1984 nfs4_put_open_state(calldata->state);
1985 nfs_free_seqid(calldata->arg.seqid);
1986 nfs4_put_state_owner(sp);
1987 nfs_sb_deactive(sb);
1991 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1994 spin_lock(&state->owner->so_lock);
1995 if (!(fmode & FMODE_READ))
1996 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1997 if (!(fmode & FMODE_WRITE))
1998 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1999 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2000 spin_unlock(&state->owner->so_lock);
2003 static void nfs4_close_done(struct rpc_task *task, void *data)
2005 struct nfs4_closedata *calldata = data;
2006 struct nfs4_state *state = calldata->state;
2007 struct nfs_server *server = NFS_SERVER(calldata->inode);
2009 dprintk("%s: begin!\n", __func__);
2010 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2012 /* hmm. we are done with the inode, and in the process of freeing
2013 * the state_owner. we keep this around to process errors
2015 switch (task->tk_status) {
2018 pnfs_roc_set_barrier(state->inode,
2019 calldata->roc_barrier);
2020 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2021 renew_lease(server, calldata->timestamp);
2022 nfs4_close_clear_stateid_flags(state,
2023 calldata->arg.fmode);
2025 case -NFS4ERR_STALE_STATEID:
2026 case -NFS4ERR_OLD_STATEID:
2027 case -NFS4ERR_BAD_STATEID:
2028 case -NFS4ERR_EXPIRED:
2029 if (calldata->arg.fmode == 0)
2032 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2033 rpc_restart_call_prepare(task);
2035 nfs_release_seqid(calldata->arg.seqid);
2036 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2037 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2040 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2042 struct nfs4_closedata *calldata = data;
2043 struct nfs4_state *state = calldata->state;
2046 dprintk("%s: begin!\n", __func__);
2047 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2050 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2051 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2052 spin_lock(&state->owner->so_lock);
2053 /* Calculate the change in open mode */
2054 if (state->n_rdwr == 0) {
2055 if (state->n_rdonly == 0) {
2056 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2057 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2058 calldata->arg.fmode &= ~FMODE_READ;
2060 if (state->n_wronly == 0) {
2061 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2062 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2063 calldata->arg.fmode &= ~FMODE_WRITE;
2066 spin_unlock(&state->owner->so_lock);
2069 /* Note: exit _without_ calling nfs4_close_done */
2070 task->tk_action = NULL;
2074 if (calldata->arg.fmode == 0) {
2075 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2076 if (calldata->roc &&
2077 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2078 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2084 nfs_fattr_init(calldata->res.fattr);
2085 calldata->timestamp = jiffies;
2086 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
2087 &calldata->arg.seq_args,
2088 &calldata->res.seq_res,
2091 rpc_call_start(task);
2093 dprintk("%s: done!\n", __func__);
2096 static const struct rpc_call_ops nfs4_close_ops = {
2097 .rpc_call_prepare = nfs4_close_prepare,
2098 .rpc_call_done = nfs4_close_done,
2099 .rpc_release = nfs4_free_closedata,
2103 * It is possible for data to be read/written from a mem-mapped file
2104 * after the sys_close call (which hits the vfs layer as a flush).
2105 * This means that we can't safely call nfsv4 close on a file until
2106 * the inode is cleared. This in turn means that we are not good
2107 * NFSv4 citizens - we do not indicate to the server to update the file's
2108 * share state even when we are done with one of the three share
2109 * stateid's in the inode.
2111 * NOTE: Caller must be holding the sp->so_owner semaphore!
2113 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
2115 struct nfs_server *server = NFS_SERVER(state->inode);
2116 struct nfs4_closedata *calldata;
2117 struct nfs4_state_owner *sp = state->owner;
2118 struct rpc_task *task;
2119 struct rpc_message msg = {
2120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2121 .rpc_cred = state->owner->so_cred,
2123 struct rpc_task_setup task_setup_data = {
2124 .rpc_client = server->client,
2125 .rpc_message = &msg,
2126 .callback_ops = &nfs4_close_ops,
2127 .workqueue = nfsiod_workqueue,
2128 .flags = RPC_TASK_ASYNC,
2130 int status = -ENOMEM;
2132 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2133 if (calldata == NULL)
2135 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2136 calldata->inode = state->inode;
2137 calldata->state = state;
2138 calldata->arg.fh = NFS_FH(state->inode);
2139 calldata->arg.stateid = &state->open_stateid;
2140 /* Serialization for the sequence id */
2141 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2142 if (calldata->arg.seqid == NULL)
2143 goto out_free_calldata;
2144 calldata->arg.fmode = 0;
2145 calldata->arg.bitmask = server->cache_consistency_bitmask;
2146 calldata->res.fattr = &calldata->fattr;
2147 calldata->res.seqid = calldata->arg.seqid;
2148 calldata->res.server = server;
2149 calldata->roc = roc;
2150 nfs_sb_active(calldata->inode->i_sb);
2152 msg.rpc_argp = &calldata->arg;
2153 msg.rpc_resp = &calldata->res;
2154 task_setup_data.callback_data = calldata;
2155 task = rpc_run_task(&task_setup_data);
2157 return PTR_ERR(task);
2160 status = rpc_wait_for_completion_task(task);
2167 pnfs_roc_release(state->inode);
2168 nfs4_put_open_state(state);
2169 nfs4_put_state_owner(sp);
2173 static struct inode *
2174 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2176 struct nfs4_state *state;
2178 /* Protect against concurrent sillydeletes */
2179 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
2181 return ERR_CAST(state);
2183 return igrab(state->inode);
2186 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2188 if (ctx->state == NULL)
2191 nfs4_close_sync(ctx->state, ctx->mode);
2193 nfs4_close_state(ctx->state, ctx->mode);
2196 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2198 struct nfs4_server_caps_arg args = {
2201 struct nfs4_server_caps_res res = {};
2202 struct rpc_message msg = {
2203 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2209 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2211 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2212 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2213 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2214 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2215 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2216 NFS_CAP_CTIME|NFS_CAP_MTIME);
2217 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2218 server->caps |= NFS_CAP_ACLS;
2219 if (res.has_links != 0)
2220 server->caps |= NFS_CAP_HARDLINKS;
2221 if (res.has_symlinks != 0)
2222 server->caps |= NFS_CAP_SYMLINKS;
2223 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2224 server->caps |= NFS_CAP_FILEID;
2225 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2226 server->caps |= NFS_CAP_MODE;
2227 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2228 server->caps |= NFS_CAP_NLINK;
2229 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2230 server->caps |= NFS_CAP_OWNER;
2231 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2232 server->caps |= NFS_CAP_OWNER_GROUP;
2233 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2234 server->caps |= NFS_CAP_ATIME;
2235 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2236 server->caps |= NFS_CAP_CTIME;
2237 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2238 server->caps |= NFS_CAP_MTIME;
2240 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2241 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2242 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2243 server->acl_bitmask = res.acl_bitmask;
2244 server->fh_expire_type = res.fh_expire_type;
2250 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2252 struct nfs4_exception exception = { };
2255 err = nfs4_handle_exception(server,
2256 _nfs4_server_capabilities(server, fhandle),
2258 } while (exception.retry);
2262 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2263 struct nfs_fsinfo *info)
2265 struct nfs4_lookup_root_arg args = {
2266 .bitmask = nfs4_fattr_bitmap,
2268 struct nfs4_lookup_res res = {
2270 .fattr = info->fattr,
2273 struct rpc_message msg = {
2274 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2279 nfs_fattr_init(info->fattr);
2280 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2283 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2284 struct nfs_fsinfo *info)
2286 struct nfs4_exception exception = { };
2289 err = _nfs4_lookup_root(server, fhandle, info);
2292 case -NFS4ERR_WRONGSEC:
2295 err = nfs4_handle_exception(server, err, &exception);
2297 } while (exception.retry);
2301 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2302 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2304 struct rpc_auth *auth;
2307 auth = rpcauth_create(flavor, server->client);
2312 ret = nfs4_lookup_root(server, fhandle, info);
2317 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2318 struct nfs_fsinfo *info)
2320 int i, len, status = 0;
2321 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2323 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2324 flav_array[len] = RPC_AUTH_NULL;
2327 for (i = 0; i < len; i++) {
2328 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2329 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2334 * -EACCESS could mean that the user doesn't have correct permissions
2335 * to access the mount. It could also mean that we tried to mount
2336 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2337 * existing mount programs don't handle -EACCES very well so it should
2338 * be mapped to -EPERM instead.
2340 if (status == -EACCES)
2346 * get the file handle for the "/" directory on the server
2348 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2349 struct nfs_fsinfo *info)
2351 int minor_version = server->nfs_client->cl_minorversion;
2352 int status = nfs4_lookup_root(server, fhandle, info);
2353 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2355 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2356 * by nfs4_map_errors() as this function exits.
2358 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2360 status = nfs4_server_capabilities(server, fhandle);
2362 status = nfs4_do_fsinfo(server, fhandle, info);
2363 return nfs4_map_errors(status);
2367 * Get locations and (maybe) other attributes of a referral.
2368 * Note that we'll actually follow the referral later when
2369 * we detect fsid mismatch in inode revalidation
2371 static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2372 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
2374 int status = -ENOMEM;
2375 struct page *page = NULL;
2376 struct nfs4_fs_locations *locations = NULL;
2378 page = alloc_page(GFP_KERNEL);
2381 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2382 if (locations == NULL)
2385 status = nfs4_proc_fs_locations(dir, name, locations, page);
2388 /* Make sure server returned a different fsid for the referral */
2389 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2390 dprintk("%s: server did not return a different fsid for"
2391 " a referral at %s\n", __func__, name->name);
2395 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2396 nfs_fixup_referral_attributes(&locations->fattr);
2398 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2399 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2400 memset(fhandle, 0, sizeof(struct nfs_fh));
2408 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2410 struct nfs4_getattr_arg args = {
2412 .bitmask = server->attr_bitmask,
2414 struct nfs4_getattr_res res = {
2418 struct rpc_message msg = {
2419 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2424 nfs_fattr_init(fattr);
2425 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2428 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2430 struct nfs4_exception exception = { };
2433 err = nfs4_handle_exception(server,
2434 _nfs4_proc_getattr(server, fhandle, fattr),
2436 } while (exception.retry);
2441 * The file is not closed if it is opened due to the a request to change
2442 * the size of the file. The open call will not be needed once the
2443 * VFS layer lookup-intents are implemented.
2445 * Close is called when the inode is destroyed.
2446 * If we haven't opened the file for O_WRONLY, we
2447 * need to in the size_change case to obtain a stateid.
2450 * Because OPEN is always done by name in nfsv4, it is
2451 * possible that we opened a different file by the same
2452 * name. We can recognize this race condition, but we
2453 * can't do anything about it besides returning an error.
2455 * This will be fixed with VFS changes (lookup-intent).
2458 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2459 struct iattr *sattr)
2461 struct inode *inode = dentry->d_inode;
2462 struct rpc_cred *cred = NULL;
2463 struct nfs4_state *state = NULL;
2466 if (pnfs_ld_layoutret_on_setattr(inode))
2467 pnfs_return_layout(inode);
2469 nfs_fattr_init(fattr);
2471 /* Search for an existing open(O_WRITE) file */
2472 if (sattr->ia_valid & ATTR_FILE) {
2473 struct nfs_open_context *ctx;
2475 ctx = nfs_file_open_context(sattr->ia_file);
2482 /* Deal with open(O_TRUNC) */
2483 if (sattr->ia_valid & ATTR_OPEN)
2484 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2486 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2488 nfs_setattr_update_inode(inode, sattr);
2492 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2493 const struct qstr *name, struct nfs_fh *fhandle,
2494 struct nfs_fattr *fattr)
2496 struct nfs_server *server = NFS_SERVER(dir);
2498 struct nfs4_lookup_arg args = {
2499 .bitmask = server->attr_bitmask,
2500 .dir_fh = NFS_FH(dir),
2503 struct nfs4_lookup_res res = {
2508 struct rpc_message msg = {
2509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2514 nfs_fattr_init(fattr);
2516 dprintk("NFS call lookup %s\n", name->name);
2517 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2518 dprintk("NFS reply lookup: %d\n", status);
2522 void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2524 memset(fh, 0, sizeof(struct nfs_fh));
2525 fattr->fsid.major = 1;
2526 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2527 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2528 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2532 static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2533 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2535 struct nfs4_exception exception = { };
2540 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2542 case -NFS4ERR_BADNAME:
2544 case -NFS4ERR_MOVED:
2545 return nfs4_get_referral(dir, name, fattr, fhandle);
2546 case -NFS4ERR_WRONGSEC:
2547 nfs_fixup_secinfo_attributes(fattr, fhandle);
2549 err = nfs4_handle_exception(NFS_SERVER(dir),
2550 status, &exception);
2551 } while (exception.retry);
2555 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2557 struct nfs_server *server = NFS_SERVER(inode);
2558 struct nfs4_accessargs args = {
2559 .fh = NFS_FH(inode),
2560 .bitmask = server->cache_consistency_bitmask,
2562 struct nfs4_accessres res = {
2565 struct rpc_message msg = {
2566 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2569 .rpc_cred = entry->cred,
2571 int mode = entry->mask;
2575 * Determine which access bits we want to ask for...
2577 if (mode & MAY_READ)
2578 args.access |= NFS4_ACCESS_READ;
2579 if (S_ISDIR(inode->i_mode)) {
2580 if (mode & MAY_WRITE)
2581 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2582 if (mode & MAY_EXEC)
2583 args.access |= NFS4_ACCESS_LOOKUP;
2585 if (mode & MAY_WRITE)
2586 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2587 if (mode & MAY_EXEC)
2588 args.access |= NFS4_ACCESS_EXECUTE;
2591 res.fattr = nfs_alloc_fattr();
2592 if (res.fattr == NULL)
2595 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2598 if (res.access & NFS4_ACCESS_READ)
2599 entry->mask |= MAY_READ;
2600 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2601 entry->mask |= MAY_WRITE;
2602 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2603 entry->mask |= MAY_EXEC;
2604 nfs_refresh_inode(inode, res.fattr);
2606 nfs_free_fattr(res.fattr);
2610 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2612 struct nfs4_exception exception = { };
2615 err = nfs4_handle_exception(NFS_SERVER(inode),
2616 _nfs4_proc_access(inode, entry),
2618 } while (exception.retry);
2623 * TODO: For the time being, we don't try to get any attributes
2624 * along with any of the zero-copy operations READ, READDIR,
2627 * In the case of the first three, we want to put the GETATTR
2628 * after the read-type operation -- this is because it is hard
2629 * to predict the length of a GETATTR response in v4, and thus
2630 * align the READ data correctly. This means that the GETATTR
2631 * may end up partially falling into the page cache, and we should
2632 * shift it into the 'tail' of the xdr_buf before processing.
2633 * To do this efficiently, we need to know the total length
2634 * of data received, which doesn't seem to be available outside
2637 * In the case of WRITE, we also want to put the GETATTR after
2638 * the operation -- in this case because we want to make sure
2639 * we get the post-operation mtime and size. This means that
2640 * we can't use xdr_encode_pages() as written: we need a variant
2641 * of it which would leave room in the 'tail' iovec.
2643 * Both of these changes to the XDR layer would in fact be quite
2644 * minor, but I decided to leave them for a subsequent patch.
2646 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2647 unsigned int pgbase, unsigned int pglen)
2649 struct nfs4_readlink args = {
2650 .fh = NFS_FH(inode),
2655 struct nfs4_readlink_res res;
2656 struct rpc_message msg = {
2657 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2662 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2665 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2666 unsigned int pgbase, unsigned int pglen)
2668 struct nfs4_exception exception = { };
2671 err = nfs4_handle_exception(NFS_SERVER(inode),
2672 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2674 } while (exception.retry);
2680 * We will need to arrange for the VFS layer to provide an atomic open.
2681 * Until then, this create/open method is prone to inefficiency and race
2682 * conditions due to the lookup, create, and open VFS calls from sys_open()
2683 * placed on the wire.
2685 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2686 * The file will be opened again in the subsequent VFS open call
2687 * (nfs4_proc_file_open).
2689 * The open for read will just hang around to be used by any process that
2690 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2694 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2695 int flags, struct nfs_open_context *ctx)
2697 struct dentry *de = dentry;
2698 struct nfs4_state *state;
2699 struct rpc_cred *cred = NULL;
2708 sattr->ia_mode &= ~current_umask();
2709 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
2711 if (IS_ERR(state)) {
2712 status = PTR_ERR(state);
2715 d_add(dentry, igrab(state->inode));
2716 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2720 nfs4_close_sync(state, fmode);
2725 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2727 struct nfs_server *server = NFS_SERVER(dir);
2728 struct nfs_removeargs args = {
2730 .name.len = name->len,
2731 .name.name = name->name,
2732 .bitmask = server->attr_bitmask,
2734 struct nfs_removeres res = {
2737 struct rpc_message msg = {
2738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2742 int status = -ENOMEM;
2744 res.dir_attr = nfs_alloc_fattr();
2745 if (res.dir_attr == NULL)
2748 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2750 update_changeattr(dir, &res.cinfo);
2751 nfs_post_op_update_inode(dir, res.dir_attr);
2753 nfs_free_fattr(res.dir_attr);
2758 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2760 struct nfs4_exception exception = { };
2763 err = nfs4_handle_exception(NFS_SERVER(dir),
2764 _nfs4_proc_remove(dir, name),
2766 } while (exception.retry);
2770 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2772 struct nfs_server *server = NFS_SERVER(dir);
2773 struct nfs_removeargs *args = msg->rpc_argp;
2774 struct nfs_removeres *res = msg->rpc_resp;
2776 args->bitmask = server->cache_consistency_bitmask;
2777 res->server = server;
2778 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2779 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
2782 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2784 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2786 if (!nfs4_sequence_done(task, &res->seq_res))
2788 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2790 update_changeattr(dir, &res->cinfo);
2791 nfs_post_op_update_inode(dir, res->dir_attr);
2795 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2797 struct nfs_server *server = NFS_SERVER(dir);
2798 struct nfs_renameargs *arg = msg->rpc_argp;
2799 struct nfs_renameres *res = msg->rpc_resp;
2801 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2802 arg->bitmask = server->attr_bitmask;
2803 res->server = server;
2804 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
2807 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2808 struct inode *new_dir)
2810 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2812 if (!nfs4_sequence_done(task, &res->seq_res))
2814 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2817 update_changeattr(old_dir, &res->old_cinfo);
2818 nfs_post_op_update_inode(old_dir, res->old_fattr);
2819 update_changeattr(new_dir, &res->new_cinfo);
2820 nfs_post_op_update_inode(new_dir, res->new_fattr);
2824 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2825 struct inode *new_dir, struct qstr *new_name)
2827 struct nfs_server *server = NFS_SERVER(old_dir);
2828 struct nfs_renameargs arg = {
2829 .old_dir = NFS_FH(old_dir),
2830 .new_dir = NFS_FH(new_dir),
2831 .old_name = old_name,
2832 .new_name = new_name,
2833 .bitmask = server->attr_bitmask,
2835 struct nfs_renameres res = {
2838 struct rpc_message msg = {
2839 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2843 int status = -ENOMEM;
2845 res.old_fattr = nfs_alloc_fattr();
2846 res.new_fattr = nfs_alloc_fattr();
2847 if (res.old_fattr == NULL || res.new_fattr == NULL)
2850 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2852 update_changeattr(old_dir, &res.old_cinfo);
2853 nfs_post_op_update_inode(old_dir, res.old_fattr);
2854 update_changeattr(new_dir, &res.new_cinfo);
2855 nfs_post_op_update_inode(new_dir, res.new_fattr);
2858 nfs_free_fattr(res.new_fattr);
2859 nfs_free_fattr(res.old_fattr);
2863 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2864 struct inode *new_dir, struct qstr *new_name)
2866 struct nfs4_exception exception = { };
2869 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2870 _nfs4_proc_rename(old_dir, old_name,
2873 } while (exception.retry);
2877 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2879 struct nfs_server *server = NFS_SERVER(inode);
2880 struct nfs4_link_arg arg = {
2881 .fh = NFS_FH(inode),
2882 .dir_fh = NFS_FH(dir),
2884 .bitmask = server->attr_bitmask,
2886 struct nfs4_link_res res = {
2889 struct rpc_message msg = {
2890 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2894 int status = -ENOMEM;
2896 res.fattr = nfs_alloc_fattr();
2897 res.dir_attr = nfs_alloc_fattr();
2898 if (res.fattr == NULL || res.dir_attr == NULL)
2901 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2903 update_changeattr(dir, &res.cinfo);
2904 nfs_post_op_update_inode(dir, res.dir_attr);
2905 nfs_post_op_update_inode(inode, res.fattr);
2908 nfs_free_fattr(res.dir_attr);
2909 nfs_free_fattr(res.fattr);
2913 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2915 struct nfs4_exception exception = { };
2918 err = nfs4_handle_exception(NFS_SERVER(inode),
2919 _nfs4_proc_link(inode, dir, name),
2921 } while (exception.retry);
2925 struct nfs4_createdata {
2926 struct rpc_message msg;
2927 struct nfs4_create_arg arg;
2928 struct nfs4_create_res res;
2930 struct nfs_fattr fattr;
2931 struct nfs_fattr dir_fattr;
2934 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2935 struct qstr *name, struct iattr *sattr, u32 ftype)
2937 struct nfs4_createdata *data;
2939 data = kzalloc(sizeof(*data), GFP_KERNEL);
2941 struct nfs_server *server = NFS_SERVER(dir);
2943 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2944 data->msg.rpc_argp = &data->arg;
2945 data->msg.rpc_resp = &data->res;
2946 data->arg.dir_fh = NFS_FH(dir);
2947 data->arg.server = server;
2948 data->arg.name = name;
2949 data->arg.attrs = sattr;
2950 data->arg.ftype = ftype;
2951 data->arg.bitmask = server->attr_bitmask;
2952 data->res.server = server;
2953 data->res.fh = &data->fh;
2954 data->res.fattr = &data->fattr;
2955 data->res.dir_fattr = &data->dir_fattr;
2956 nfs_fattr_init(data->res.fattr);
2957 nfs_fattr_init(data->res.dir_fattr);
2962 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2964 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
2965 &data->arg.seq_args, &data->res.seq_res, 1);
2967 update_changeattr(dir, &data->res.dir_cinfo);
2968 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2969 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2974 static void nfs4_free_createdata(struct nfs4_createdata *data)
2979 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
2980 struct page *page, unsigned int len, struct iattr *sattr)
2982 struct nfs4_createdata *data;
2983 int status = -ENAMETOOLONG;
2985 if (len > NFS4_MAXPATHLEN)
2989 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2993 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2994 data->arg.u.symlink.pages = &page;
2995 data->arg.u.symlink.len = len;
2997 status = nfs4_do_create(dir, dentry, data);
2999 nfs4_free_createdata(data);
3004 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3005 struct page *page, unsigned int len, struct iattr *sattr)
3007 struct nfs4_exception exception = { };
3010 err = nfs4_handle_exception(NFS_SERVER(dir),
3011 _nfs4_proc_symlink(dir, dentry, page,
3014 } while (exception.retry);
3018 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3019 struct iattr *sattr)
3021 struct nfs4_createdata *data;
3022 int status = -ENOMEM;
3024 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3028 status = nfs4_do_create(dir, dentry, data);
3030 nfs4_free_createdata(data);
3035 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3036 struct iattr *sattr)
3038 struct nfs4_exception exception = { };
3041 sattr->ia_mode &= ~current_umask();
3043 err = nfs4_handle_exception(NFS_SERVER(dir),
3044 _nfs4_proc_mkdir(dir, dentry, sattr),
3046 } while (exception.retry);
3050 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3051 u64 cookie, struct page **pages, unsigned int count, int plus)
3053 struct inode *dir = dentry->d_inode;
3054 struct nfs4_readdir_arg args = {
3059 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3062 struct nfs4_readdir_res res;
3063 struct rpc_message msg = {
3064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3071 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3072 dentry->d_parent->d_name.name,
3073 dentry->d_name.name,
3074 (unsigned long long)cookie);
3075 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3076 res.pgbase = args.pgbase;
3077 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3079 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
3080 status += args.pgbase;
3083 nfs_invalidate_atime(dir);
3085 dprintk("%s: returns %d\n", __func__, status);
3089 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3090 u64 cookie, struct page **pages, unsigned int count, int plus)
3092 struct nfs4_exception exception = { };
3095 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3096 _nfs4_proc_readdir(dentry, cred, cookie,
3097 pages, count, plus),
3099 } while (exception.retry);
3103 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3104 struct iattr *sattr, dev_t rdev)
3106 struct nfs4_createdata *data;
3107 int mode = sattr->ia_mode;
3108 int status = -ENOMEM;
3110 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3111 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3113 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3118 data->arg.ftype = NF4FIFO;
3119 else if (S_ISBLK(mode)) {
3120 data->arg.ftype = NF4BLK;
3121 data->arg.u.device.specdata1 = MAJOR(rdev);
3122 data->arg.u.device.specdata2 = MINOR(rdev);
3124 else if (S_ISCHR(mode)) {
3125 data->arg.ftype = NF4CHR;
3126 data->arg.u.device.specdata1 = MAJOR(rdev);
3127 data->arg.u.device.specdata2 = MINOR(rdev);
3130 status = nfs4_do_create(dir, dentry, data);
3132 nfs4_free_createdata(data);
3137 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3138 struct iattr *sattr, dev_t rdev)
3140 struct nfs4_exception exception = { };
3143 sattr->ia_mode &= ~current_umask();
3145 err = nfs4_handle_exception(NFS_SERVER(dir),
3146 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3148 } while (exception.retry);
3152 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3153 struct nfs_fsstat *fsstat)
3155 struct nfs4_statfs_arg args = {
3157 .bitmask = server->attr_bitmask,
3159 struct nfs4_statfs_res res = {
3162 struct rpc_message msg = {
3163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3168 nfs_fattr_init(fsstat->fattr);
3169 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3172 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3174 struct nfs4_exception exception = { };
3177 err = nfs4_handle_exception(server,
3178 _nfs4_proc_statfs(server, fhandle, fsstat),
3180 } while (exception.retry);
3184 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3185 struct nfs_fsinfo *fsinfo)
3187 struct nfs4_fsinfo_arg args = {
3189 .bitmask = server->attr_bitmask,
3191 struct nfs4_fsinfo_res res = {
3194 struct rpc_message msg = {
3195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3200 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3203 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3205 struct nfs4_exception exception = { };
3209 err = nfs4_handle_exception(server,
3210 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3212 } while (exception.retry);
3216 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3218 nfs_fattr_init(fsinfo->fattr);
3219 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3222 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3223 struct nfs_pathconf *pathconf)
3225 struct nfs4_pathconf_arg args = {
3227 .bitmask = server->attr_bitmask,
3229 struct nfs4_pathconf_res res = {
3230 .pathconf = pathconf,
3232 struct rpc_message msg = {
3233 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3238 /* None of the pathconf attributes are mandatory to implement */
3239 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3240 memset(pathconf, 0, sizeof(*pathconf));
3244 nfs_fattr_init(pathconf->fattr);
3245 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3248 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3249 struct nfs_pathconf *pathconf)
3251 struct nfs4_exception exception = { };
3255 err = nfs4_handle_exception(server,
3256 _nfs4_proc_pathconf(server, fhandle, pathconf),
3258 } while (exception.retry);
3262 void __nfs4_read_done_cb(struct nfs_read_data *data)
3264 nfs_invalidate_atime(data->inode);
3267 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3269 struct nfs_server *server = NFS_SERVER(data->inode);
3271 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3272 rpc_restart_call_prepare(task);
3276 __nfs4_read_done_cb(data);
3277 if (task->tk_status > 0)
3278 renew_lease(server, data->timestamp);
3282 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3285 dprintk("--> %s\n", __func__);
3287 if (!nfs4_sequence_done(task, &data->res.seq_res))
3290 return data->read_done_cb ? data->read_done_cb(task, data) :
3291 nfs4_read_done_cb(task, data);
3294 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3296 data->timestamp = jiffies;
3297 data->read_done_cb = nfs4_read_done_cb;
3298 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3299 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3302 /* Reset the the nfs_read_data to send the read to the MDS. */
3303 void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3305 dprintk("%s Reset task for i/o through\n", __func__);
3306 put_lseg(data->lseg);
3308 /* offsets will differ in the dense stripe case */
3309 data->args.offset = data->mds_offset;
3310 data->ds_clp = NULL;
3311 data->args.fh = NFS_FH(data->inode);
3312 data->read_done_cb = nfs4_read_done_cb;
3313 task->tk_ops = data->mds_ops;
3314 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3316 EXPORT_SYMBOL_GPL(nfs4_reset_read);
3318 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3320 struct inode *inode = data->inode;
3322 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3323 rpc_restart_call_prepare(task);
3326 if (task->tk_status >= 0) {
3327 renew_lease(NFS_SERVER(inode), data->timestamp);
3328 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
3333 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3335 if (!nfs4_sequence_done(task, &data->res.seq_res))
3337 return data->write_done_cb ? data->write_done_cb(task, data) :
3338 nfs4_write_done_cb(task, data);
3341 /* Reset the the nfs_write_data to send the write to the MDS. */
3342 void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3344 dprintk("%s Reset task for i/o through\n", __func__);
3345 put_lseg(data->lseg);
3347 data->ds_clp = NULL;
3348 data->write_done_cb = nfs4_write_done_cb;
3349 data->args.fh = NFS_FH(data->inode);
3350 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3351 data->args.offset = data->mds_offset;
3352 data->res.fattr = &data->fattr;
3353 task->tk_ops = data->mds_ops;
3354 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3356 EXPORT_SYMBOL_GPL(nfs4_reset_write);
3358 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3360 struct nfs_server *server = NFS_SERVER(data->inode);
3363 data->args.bitmask = NULL;
3364 data->res.fattr = NULL;
3366 data->args.bitmask = server->cache_consistency_bitmask;
3367 if (!data->write_done_cb)
3368 data->write_done_cb = nfs4_write_done_cb;
3369 data->res.server = server;
3370 data->timestamp = jiffies;
3372 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3373 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3376 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3378 struct inode *inode = data->inode;
3380 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3381 rpc_restart_call_prepare(task);
3384 nfs_refresh_inode(inode, data->res.fattr);
3388 static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3390 if (!nfs4_sequence_done(task, &data->res.seq_res))
3392 return data->write_done_cb(task, data);
3395 static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
3397 struct nfs_server *server = NFS_SERVER(data->inode);
3400 data->args.bitmask = NULL;
3401 data->res.fattr = NULL;
3403 data->args.bitmask = server->cache_consistency_bitmask;
3404 if (!data->write_done_cb)
3405 data->write_done_cb = nfs4_commit_done_cb;
3406 data->res.server = server;
3407 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3408 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3411 struct nfs4_renewdata {
3412 struct nfs_client *client;
3413 unsigned long timestamp;
3417 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3418 * standalone procedure for queueing an asynchronous RENEW.
3420 static void nfs4_renew_release(void *calldata)
3422 struct nfs4_renewdata *data = calldata;
3423 struct nfs_client *clp = data->client;
3425 if (atomic_read(&clp->cl_count) > 1)
3426 nfs4_schedule_state_renewal(clp);
3427 nfs_put_client(clp);
3431 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3433 struct nfs4_renewdata *data = calldata;
3434 struct nfs_client *clp = data->client;
3435 unsigned long timestamp = data->timestamp;
3437 if (task->tk_status < 0) {
3438 /* Unless we're shutting down, schedule state recovery! */
3439 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3441 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3442 nfs4_schedule_lease_recovery(clp);
3445 nfs4_schedule_path_down_recovery(clp);
3447 do_renew_lease(clp, timestamp);
3450 static const struct rpc_call_ops nfs4_renew_ops = {
3451 .rpc_call_done = nfs4_renew_done,
3452 .rpc_release = nfs4_renew_release,
3455 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3457 struct rpc_message msg = {
3458 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3462 struct nfs4_renewdata *data;
3464 if (renew_flags == 0)
3466 if (!atomic_inc_not_zero(&clp->cl_count))
3468 data = kmalloc(sizeof(*data), GFP_NOFS);
3472 data->timestamp = jiffies;
3473 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3474 &nfs4_renew_ops, data);
3477 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3479 struct rpc_message msg = {
3480 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3484 unsigned long now = jiffies;
3487 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3490 do_renew_lease(clp, now);
3494 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3496 return (server->caps & NFS_CAP_ACLS)
3497 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3498 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3501 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3502 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3505 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3507 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3508 struct page **pages, unsigned int *pgbase)
3510 struct page *newpage, **spages;
3516 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3517 newpage = alloc_page(GFP_KERNEL);
3519 if (newpage == NULL)
3521 memcpy(page_address(newpage), buf, len);
3526 } while (buflen != 0);
3532 __free_page(spages[rc-1]);
3536 struct nfs4_cached_acl {
3542 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3544 struct nfs_inode *nfsi = NFS_I(inode);
3546 spin_lock(&inode->i_lock);
3547 kfree(nfsi->nfs4_acl);
3548 nfsi->nfs4_acl = acl;
3549 spin_unlock(&inode->i_lock);
3552 static void nfs4_zap_acl_attr(struct inode *inode)
3554 nfs4_set_cached_acl(inode, NULL);
3557 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3559 struct nfs_inode *nfsi = NFS_I(inode);
3560 struct nfs4_cached_acl *acl;
3563 spin_lock(&inode->i_lock);
3564 acl = nfsi->nfs4_acl;
3567 if (buf == NULL) /* user is just asking for length */
3569 if (acl->cached == 0)
3571 ret = -ERANGE; /* see getxattr(2) man page */
3572 if (acl->len > buflen)
3574 memcpy(buf, acl->data, acl->len);
3578 spin_unlock(&inode->i_lock);
3582 static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3584 struct nfs4_cached_acl *acl;
3586 if (buf && acl_len <= PAGE_SIZE) {
3587 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3591 memcpy(acl->data, buf, acl_len);
3593 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3600 nfs4_set_cached_acl(inode, acl);
3604 * The getxattr API returns the required buffer length when called with a
3605 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3606 * the required buf. On a NULL buf, we send a page of data to the server
3607 * guessing that the ACL request can be serviced by a page. If so, we cache
3608 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3609 * the cache. If not so, we throw away the page, and cache the required
3610 * length. The next getxattr call will then produce another round trip to
3611 * the server, this time with the input buf of the required size.
3613 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3615 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3616 struct nfs_getaclargs args = {
3617 .fh = NFS_FH(inode),
3621 struct nfs_getaclres res = {
3625 struct rpc_message msg = {
3626 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3630 int ret = -ENOMEM, npages, i, acl_len = 0;
3632 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3633 /* As long as we're doing a round trip to the server anyway,
3634 * let's be prepared for a page of acl data. */
3638 for (i = 0; i < npages; i++) {
3639 pages[i] = alloc_page(GFP_KERNEL);
3644 /* for decoding across pages */
3645 res.acl_scratch = alloc_page(GFP_KERNEL);
3646 if (!res.acl_scratch)
3649 args.acl_len = npages * PAGE_SIZE;
3650 args.acl_pgbase = 0;
3651 /* Let decode_getfacl know not to fail if the ACL data is larger than
3652 * the page we send as a guess */
3654 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3655 resp_buf = page_address(pages[0]);
3657 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3658 __func__, buf, buflen, npages, args.acl_len);
3659 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3660 &msg, &args.seq_args, &res.seq_res, 0);
3664 acl_len = res.acl_len - res.acl_data_offset;
3665 if (acl_len > args.acl_len)
3666 nfs4_write_cached_acl(inode, NULL, acl_len);
3668 nfs4_write_cached_acl(inode, resp_buf + res.acl_data_offset,
3672 if (acl_len > buflen)
3674 _copy_from_pages(buf, pages, res.acl_data_offset,
3679 for (i = 0; i < npages; i++)
3681 __free_page(pages[i]);
3682 if (res.acl_scratch)
3683 __free_page(res.acl_scratch);
3687 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3689 struct nfs4_exception exception = { };
3692 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3695 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3696 } while (exception.retry);
3700 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3702 struct nfs_server *server = NFS_SERVER(inode);
3705 if (!nfs4_server_supports_acls(server))
3707 ret = nfs_revalidate_inode(server, inode);
3710 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3711 nfs_zap_acl_cache(inode);
3712 ret = nfs4_read_cached_acl(inode, buf, buflen);
3714 /* -ENOENT is returned if there is no ACL or if there is an ACL
3715 * but no cached acl data, just the acl length */
3717 return nfs4_get_acl_uncached(inode, buf, buflen);
3720 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3722 struct nfs_server *server = NFS_SERVER(inode);
3723 struct page *pages[NFS4ACL_MAXPAGES];
3724 struct nfs_setaclargs arg = {
3725 .fh = NFS_FH(inode),
3729 struct nfs_setaclres res;
3730 struct rpc_message msg = {
3731 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3737 if (!nfs4_server_supports_acls(server))
3739 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3742 nfs_inode_return_delegation(inode);
3743 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3746 * Free each page after tx, so the only ref left is
3747 * held by the network stack
3750 put_page(pages[i-1]);
3753 * Acl update can result in inode attribute update.
3754 * so mark the attribute cache invalid.
3756 spin_lock(&inode->i_lock);
3757 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3758 spin_unlock(&inode->i_lock);
3759 nfs_access_zap_cache(inode);
3760 nfs_zap_acl_cache(inode);
3764 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3766 struct nfs4_exception exception = { };
3769 err = nfs4_handle_exception(NFS_SERVER(inode),
3770 __nfs4_proc_set_acl(inode, buf, buflen),
3772 } while (exception.retry);
3777 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3779 struct nfs_client *clp = server->nfs_client;
3781 if (task->tk_status >= 0)
3783 switch(task->tk_status) {
3784 case -NFS4ERR_DELEG_REVOKED:
3785 case -NFS4ERR_ADMIN_REVOKED:
3786 case -NFS4ERR_BAD_STATEID:
3788 nfs_remove_bad_delegation(state->inode);
3789 case -NFS4ERR_OPENMODE:
3792 nfs4_schedule_stateid_recovery(server, state);
3793 goto wait_on_recovery;
3794 case -NFS4ERR_EXPIRED:
3796 nfs4_schedule_stateid_recovery(server, state);
3797 case -NFS4ERR_STALE_STATEID:
3798 case -NFS4ERR_STALE_CLIENTID:
3799 nfs4_schedule_lease_recovery(clp);
3800 goto wait_on_recovery;
3801 #if defined(CONFIG_NFS_V4_1)
3802 case -NFS4ERR_BADSESSION:
3803 case -NFS4ERR_BADSLOT:
3804 case -NFS4ERR_BAD_HIGH_SLOT:
3805 case -NFS4ERR_DEADSESSION:
3806 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3807 case -NFS4ERR_SEQ_FALSE_RETRY:
3808 case -NFS4ERR_SEQ_MISORDERED:
3809 dprintk("%s ERROR %d, Reset session\n", __func__,
3811 nfs4_schedule_session_recovery(clp->cl_session);
3812 task->tk_status = 0;
3814 #endif /* CONFIG_NFS_V4_1 */
3815 case -NFS4ERR_DELAY:
3816 nfs_inc_server_stats(server, NFSIOS_DELAY);
3817 case -NFS4ERR_GRACE:
3819 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3820 task->tk_status = 0;
3822 case -NFS4ERR_RETRY_UNCACHED_REP:
3823 case -NFS4ERR_OLD_STATEID:
3824 task->tk_status = 0;
3827 task->tk_status = nfs4_map_errors(task->tk_status);
3830 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3831 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3832 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3833 task->tk_status = 0;
3837 static void nfs4_construct_boot_verifier(struct nfs_client *clp,
3838 nfs4_verifier *bootverf)
3842 verf[0] = htonl((u32)clp->cl_boot_time.tv_sec);
3843 verf[1] = htonl((u32)clp->cl_boot_time.tv_nsec);
3844 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3847 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3848 unsigned short port, struct rpc_cred *cred,
3849 struct nfs4_setclientid_res *res)
3851 nfs4_verifier sc_verifier;
3852 struct nfs4_setclientid setclientid = {
3853 .sc_verifier = &sc_verifier,
3855 .sc_cb_ident = clp->cl_cb_ident,
3857 struct rpc_message msg = {
3858 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3859 .rpc_argp = &setclientid,
3866 nfs4_construct_boot_verifier(clp, &sc_verifier);
3870 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3871 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3873 rpc_peeraddr2str(clp->cl_rpcclient,
3875 rpc_peeraddr2str(clp->cl_rpcclient,
3877 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3878 clp->cl_id_uniquifier);
3879 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3880 sizeof(setclientid.sc_netid),
3881 rpc_peeraddr2str(clp->cl_rpcclient,
3882 RPC_DISPLAY_NETID));
3883 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3884 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3885 clp->cl_ipaddr, port >> 8, port & 255);
3888 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3889 if (status != -NFS4ERR_CLID_INUSE)
3892 ++clp->cl_id_uniquifier;
3896 ssleep(clp->cl_lease_time / HZ + 1);
3901 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3902 struct nfs4_setclientid_res *arg,
3903 struct rpc_cred *cred)
3905 struct nfs_fsinfo fsinfo;
3906 struct rpc_message msg = {
3907 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3909 .rpc_resp = &fsinfo,
3916 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3918 spin_lock(&clp->cl_lock);
3919 clp->cl_lease_time = fsinfo.lease_time * HZ;
3920 clp->cl_last_renewal = now;
3921 spin_unlock(&clp->cl_lock);
3926 struct nfs4_delegreturndata {
3927 struct nfs4_delegreturnargs args;
3928 struct nfs4_delegreturnres res;
3930 nfs4_stateid stateid;
3931 unsigned long timestamp;
3932 struct nfs_fattr fattr;
3936 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3938 struct nfs4_delegreturndata *data = calldata;
3940 if (!nfs4_sequence_done(task, &data->res.seq_res))
3943 switch (task->tk_status) {
3944 case -NFS4ERR_STALE_STATEID:
3945 case -NFS4ERR_EXPIRED:
3947 renew_lease(data->res.server, data->timestamp);
3950 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3952 rpc_restart_call_prepare(task);
3956 data->rpc_status = task->tk_status;
3959 static void nfs4_delegreturn_release(void *calldata)
3964 #if defined(CONFIG_NFS_V4_1)
3965 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3967 struct nfs4_delegreturndata *d_data;
3969 d_data = (struct nfs4_delegreturndata *)data;
3971 if (nfs4_setup_sequence(d_data->res.server,
3972 &d_data->args.seq_args,
3973 &d_data->res.seq_res, task))
3975 rpc_call_start(task);
3977 #endif /* CONFIG_NFS_V4_1 */
3979 static const struct rpc_call_ops nfs4_delegreturn_ops = {
3980 #if defined(CONFIG_NFS_V4_1)
3981 .rpc_call_prepare = nfs4_delegreturn_prepare,
3982 #endif /* CONFIG_NFS_V4_1 */
3983 .rpc_call_done = nfs4_delegreturn_done,
3984 .rpc_release = nfs4_delegreturn_release,
3987 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
3989 struct nfs4_delegreturndata *data;
3990 struct nfs_server *server = NFS_SERVER(inode);
3991 struct rpc_task *task;
3992 struct rpc_message msg = {
3993 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3996 struct rpc_task_setup task_setup_data = {
3997 .rpc_client = server->client,
3998 .rpc_message = &msg,
3999 .callback_ops = &nfs4_delegreturn_ops,
4000 .flags = RPC_TASK_ASYNC,
4004 data = kzalloc(sizeof(*data), GFP_NOFS);
4007 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4008 data->args.fhandle = &data->fh;
4009 data->args.stateid = &data->stateid;
4010 data->args.bitmask = server->attr_bitmask;
4011 nfs_copy_fh(&data->fh, NFS_FH(inode));
4012 nfs4_stateid_copy(&data->stateid, stateid);
4013 data->res.fattr = &data->fattr;
4014 data->res.server = server;
4015 nfs_fattr_init(data->res.fattr);
4016 data->timestamp = jiffies;
4017 data->rpc_status = 0;
4019 task_setup_data.callback_data = data;
4020 msg.rpc_argp = &data->args;
4021 msg.rpc_resp = &data->res;
4022 task = rpc_run_task(&task_setup_data);
4024 return PTR_ERR(task);
4027 status = nfs4_wait_for_completion_rpc_task(task);
4030 status = data->rpc_status;
4033 nfs_refresh_inode(inode, &data->fattr);
4039 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4041 struct nfs_server *server = NFS_SERVER(inode);
4042 struct nfs4_exception exception = { };
4045 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4047 case -NFS4ERR_STALE_STATEID:
4048 case -NFS4ERR_EXPIRED:
4052 err = nfs4_handle_exception(server, err, &exception);
4053 } while (exception.retry);
4057 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4058 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4061 * sleep, with exponential backoff, and retry the LOCK operation.
4063 static unsigned long
4064 nfs4_set_lock_task_retry(unsigned long timeout)
4066 freezable_schedule_timeout_killable(timeout);
4068 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4069 return NFS4_LOCK_MAXTIMEOUT;
4073 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4075 struct inode *inode = state->inode;
4076 struct nfs_server *server = NFS_SERVER(inode);
4077 struct nfs_client *clp = server->nfs_client;
4078 struct nfs_lockt_args arg = {
4079 .fh = NFS_FH(inode),
4082 struct nfs_lockt_res res = {
4085 struct rpc_message msg = {
4086 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4089 .rpc_cred = state->owner->so_cred,
4091 struct nfs4_lock_state *lsp;
4094 arg.lock_owner.clientid = clp->cl_clientid;
4095 status = nfs4_set_lock_state(state, request);
4098 lsp = request->fl_u.nfs4_fl.owner;
4099 arg.lock_owner.id = lsp->ls_seqid.owner_id;
4100 arg.lock_owner.s_dev = server->s_dev;
4101 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4104 request->fl_type = F_UNLCK;
4106 case -NFS4ERR_DENIED:
4109 request->fl_ops->fl_release_private(request);
4114 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4116 struct nfs4_exception exception = { };
4120 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4121 _nfs4_proc_getlk(state, cmd, request),
4123 } while (exception.retry);
4127 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4130 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4132 res = posix_lock_file_wait(file, fl);
4135 res = flock_lock_file_wait(file, fl);
4143 struct nfs4_unlockdata {
4144 struct nfs_locku_args arg;
4145 struct nfs_locku_res res;
4146 struct nfs4_lock_state *lsp;
4147 struct nfs_open_context *ctx;
4148 struct file_lock fl;
4149 const struct nfs_server *server;
4150 unsigned long timestamp;
4153 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4154 struct nfs_open_context *ctx,
4155 struct nfs4_lock_state *lsp,
4156 struct nfs_seqid *seqid)
4158 struct nfs4_unlockdata *p;
4159 struct inode *inode = lsp->ls_state->inode;
4161 p = kzalloc(sizeof(*p), GFP_NOFS);
4164 p->arg.fh = NFS_FH(inode);
4166 p->arg.seqid = seqid;
4167 p->res.seqid = seqid;
4168 p->arg.stateid = &lsp->ls_stateid;
4170 atomic_inc(&lsp->ls_count);
4171 /* Ensure we don't close file until we're done freeing locks! */
4172 p->ctx = get_nfs_open_context(ctx);
4173 memcpy(&p->fl, fl, sizeof(p->fl));
4174 p->server = NFS_SERVER(inode);
4178 static void nfs4_locku_release_calldata(void *data)
4180 struct nfs4_unlockdata *calldata = data;
4181 nfs_free_seqid(calldata->arg.seqid);
4182 nfs4_put_lock_state(calldata->lsp);
4183 put_nfs_open_context(calldata->ctx);
4187 static void nfs4_locku_done(struct rpc_task *task, void *data)
4189 struct nfs4_unlockdata *calldata = data;
4191 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4193 switch (task->tk_status) {
4195 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4196 &calldata->res.stateid);
4197 renew_lease(calldata->server, calldata->timestamp);
4199 case -NFS4ERR_BAD_STATEID:
4200 case -NFS4ERR_OLD_STATEID:
4201 case -NFS4ERR_STALE_STATEID:
4202 case -NFS4ERR_EXPIRED:
4205 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4206 rpc_restart_call_prepare(task);
4210 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4212 struct nfs4_unlockdata *calldata = data;
4214 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4216 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
4217 /* Note: exit _without_ running nfs4_locku_done */
4218 task->tk_action = NULL;
4221 calldata->timestamp = jiffies;
4222 if (nfs4_setup_sequence(calldata->server,
4223 &calldata->arg.seq_args,
4224 &calldata->res.seq_res, task))
4226 rpc_call_start(task);
4229 static const struct rpc_call_ops nfs4_locku_ops = {
4230 .rpc_call_prepare = nfs4_locku_prepare,
4231 .rpc_call_done = nfs4_locku_done,
4232 .rpc_release = nfs4_locku_release_calldata,
4235 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4236 struct nfs_open_context *ctx,
4237 struct nfs4_lock_state *lsp,
4238 struct nfs_seqid *seqid)
4240 struct nfs4_unlockdata *data;
4241 struct rpc_message msg = {
4242 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4243 .rpc_cred = ctx->cred,
4245 struct rpc_task_setup task_setup_data = {
4246 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4247 .rpc_message = &msg,
4248 .callback_ops = &nfs4_locku_ops,
4249 .workqueue = nfsiod_workqueue,
4250 .flags = RPC_TASK_ASYNC,
4253 /* Ensure this is an unlock - when canceling a lock, the
4254 * canceled lock is passed in, and it won't be an unlock.
4256 fl->fl_type = F_UNLCK;
4258 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4260 nfs_free_seqid(seqid);
4261 return ERR_PTR(-ENOMEM);
4264 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4265 msg.rpc_argp = &data->arg;
4266 msg.rpc_resp = &data->res;
4267 task_setup_data.callback_data = data;
4268 return rpc_run_task(&task_setup_data);
4271 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4273 struct nfs_inode *nfsi = NFS_I(state->inode);
4274 struct nfs_seqid *seqid;
4275 struct nfs4_lock_state *lsp;
4276 struct rpc_task *task;
4278 unsigned char fl_flags = request->fl_flags;
4280 status = nfs4_set_lock_state(state, request);
4281 /* Unlock _before_ we do the RPC call */
4282 request->fl_flags |= FL_EXISTS;
4283 down_read(&nfsi->rwsem);
4284 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4285 up_read(&nfsi->rwsem);
4288 up_read(&nfsi->rwsem);
4291 /* Is this a delegated lock? */
4292 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4294 lsp = request->fl_u.nfs4_fl.owner;
4295 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4299 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4300 status = PTR_ERR(task);
4303 status = nfs4_wait_for_completion_rpc_task(task);
4306 request->fl_flags = fl_flags;
4310 struct nfs4_lockdata {
4311 struct nfs_lock_args arg;
4312 struct nfs_lock_res res;
4313 struct nfs4_lock_state *lsp;
4314 struct nfs_open_context *ctx;
4315 struct file_lock fl;
4316 unsigned long timestamp;
4319 struct nfs_server *server;
4322 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4323 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4326 struct nfs4_lockdata *p;
4327 struct inode *inode = lsp->ls_state->inode;
4328 struct nfs_server *server = NFS_SERVER(inode);
4330 p = kzalloc(sizeof(*p), gfp_mask);
4334 p->arg.fh = NFS_FH(inode);
4336 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4337 if (p->arg.open_seqid == NULL)
4339 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4340 if (p->arg.lock_seqid == NULL)
4341 goto out_free_seqid;
4342 p->arg.lock_stateid = &lsp->ls_stateid;
4343 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4344 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4345 p->arg.lock_owner.s_dev = server->s_dev;
4346 p->res.lock_seqid = p->arg.lock_seqid;
4349 atomic_inc(&lsp->ls_count);
4350 p->ctx = get_nfs_open_context(ctx);
4351 memcpy(&p->fl, fl, sizeof(p->fl));
4354 nfs_free_seqid(p->arg.open_seqid);
4360 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4362 struct nfs4_lockdata *data = calldata;
4363 struct nfs4_state *state = data->lsp->ls_state;
4365 dprintk("%s: begin!\n", __func__);
4366 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4368 /* Do we need to do an open_to_lock_owner? */
4369 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4370 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4372 data->arg.open_stateid = &state->stateid;
4373 data->arg.new_lock_owner = 1;
4374 data->res.open_seqid = data->arg.open_seqid;
4376 data->arg.new_lock_owner = 0;
4377 data->timestamp = jiffies;
4378 if (nfs4_setup_sequence(data->server,
4379 &data->arg.seq_args,
4380 &data->res.seq_res, task))
4382 rpc_call_start(task);
4383 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4386 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4388 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4389 nfs4_lock_prepare(task, calldata);
4392 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4394 struct nfs4_lockdata *data = calldata;
4396 dprintk("%s: begin!\n", __func__);
4398 if (!nfs4_sequence_done(task, &data->res.seq_res))
4401 data->rpc_status = task->tk_status;
4402 if (data->arg.new_lock_owner != 0) {
4403 if (data->rpc_status == 0)
4404 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4408 if (data->rpc_status == 0) {
4409 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4410 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4411 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4414 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4417 static void nfs4_lock_release(void *calldata)
4419 struct nfs4_lockdata *data = calldata;
4421 dprintk("%s: begin!\n", __func__);
4422 nfs_free_seqid(data->arg.open_seqid);
4423 if (data->cancelled != 0) {
4424 struct rpc_task *task;
4425 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4426 data->arg.lock_seqid);
4428 rpc_put_task_async(task);
4429 dprintk("%s: cancelling lock!\n", __func__);
4431 nfs_free_seqid(data->arg.lock_seqid);
4432 nfs4_put_lock_state(data->lsp);
4433 put_nfs_open_context(data->ctx);
4435 dprintk("%s: done!\n", __func__);
4438 static const struct rpc_call_ops nfs4_lock_ops = {
4439 .rpc_call_prepare = nfs4_lock_prepare,
4440 .rpc_call_done = nfs4_lock_done,
4441 .rpc_release = nfs4_lock_release,
4444 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4445 .rpc_call_prepare = nfs4_recover_lock_prepare,
4446 .rpc_call_done = nfs4_lock_done,
4447 .rpc_release = nfs4_lock_release,
4450 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4453 case -NFS4ERR_ADMIN_REVOKED:
4454 case -NFS4ERR_BAD_STATEID:
4455 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4456 if (new_lock_owner != 0 ||
4457 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4458 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4460 case -NFS4ERR_STALE_STATEID:
4461 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4462 case -NFS4ERR_EXPIRED:
4463 nfs4_schedule_lease_recovery(server->nfs_client);
4467 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4469 struct nfs4_lockdata *data;
4470 struct rpc_task *task;
4471 struct rpc_message msg = {
4472 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4473 .rpc_cred = state->owner->so_cred,
4475 struct rpc_task_setup task_setup_data = {
4476 .rpc_client = NFS_CLIENT(state->inode),
4477 .rpc_message = &msg,
4478 .callback_ops = &nfs4_lock_ops,
4479 .workqueue = nfsiod_workqueue,
4480 .flags = RPC_TASK_ASYNC,
4484 dprintk("%s: begin!\n", __func__);
4485 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4486 fl->fl_u.nfs4_fl.owner,
4487 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4491 data->arg.block = 1;
4492 if (recovery_type > NFS_LOCK_NEW) {
4493 if (recovery_type == NFS_LOCK_RECLAIM)
4494 data->arg.reclaim = NFS_LOCK_RECLAIM;
4495 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4497 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4498 msg.rpc_argp = &data->arg;
4499 msg.rpc_resp = &data->res;
4500 task_setup_data.callback_data = data;
4501 task = rpc_run_task(&task_setup_data);
4503 return PTR_ERR(task);
4504 ret = nfs4_wait_for_completion_rpc_task(task);
4506 ret = data->rpc_status;
4508 nfs4_handle_setlk_error(data->server, data->lsp,
4509 data->arg.new_lock_owner, ret);
4511 data->cancelled = 1;
4513 dprintk("%s: done, ret = %d!\n", __func__, ret);
4517 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4519 struct nfs_server *server = NFS_SERVER(state->inode);
4520 struct nfs4_exception exception = { };
4524 /* Cache the lock if possible... */
4525 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4527 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4528 if (err != -NFS4ERR_DELAY)
4530 nfs4_handle_exception(server, err, &exception);
4531 } while (exception.retry);
4535 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4537 struct nfs_server *server = NFS_SERVER(state->inode);
4538 struct nfs4_exception exception = { };
4541 err = nfs4_set_lock_state(state, request);
4545 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4547 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4551 case -NFS4ERR_GRACE:
4552 case -NFS4ERR_DELAY:
4553 nfs4_handle_exception(server, err, &exception);
4556 } while (exception.retry);
4561 #if defined(CONFIG_NFS_V4_1)
4562 static int nfs41_check_expired_locks(struct nfs4_state *state)
4564 int status, ret = NFS_OK;
4565 struct nfs4_lock_state *lsp;
4566 struct nfs_server *server = NFS_SERVER(state->inode);
4568 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4569 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4570 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4571 if (status != NFS_OK) {
4572 nfs41_free_stateid(server, &lsp->ls_stateid);
4573 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4582 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4584 int status = NFS_OK;
4586 if (test_bit(LK_STATE_IN_USE, &state->flags))
4587 status = nfs41_check_expired_locks(state);
4588 if (status == NFS_OK)
4590 return nfs4_lock_expired(state, request);
4594 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4596 struct nfs_inode *nfsi = NFS_I(state->inode);
4597 unsigned char fl_flags = request->fl_flags;
4598 int status = -ENOLCK;
4600 if ((fl_flags & FL_POSIX) &&
4601 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4603 /* Is this a delegated open? */
4604 status = nfs4_set_lock_state(state, request);
4607 request->fl_flags |= FL_ACCESS;
4608 status = do_vfs_lock(request->fl_file, request);
4611 down_read(&nfsi->rwsem);
4612 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4613 /* Yes: cache locks! */
4614 /* ...but avoid races with delegation recall... */
4615 request->fl_flags = fl_flags & ~FL_SLEEP;
4616 status = do_vfs_lock(request->fl_file, request);
4619 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4622 /* Note: we always want to sleep here! */
4623 request->fl_flags = fl_flags | FL_SLEEP;
4624 if (do_vfs_lock(request->fl_file, request) < 0)
4625 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4626 "manager!\n", __func__);
4628 up_read(&nfsi->rwsem);
4630 request->fl_flags = fl_flags;
4634 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4636 struct nfs4_exception exception = {
4642 err = _nfs4_proc_setlk(state, cmd, request);
4643 if (err == -NFS4ERR_DENIED)
4645 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4647 } while (exception.retry);
4652 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4654 struct nfs_open_context *ctx;
4655 struct nfs4_state *state;
4656 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4659 /* verify open state */
4660 ctx = nfs_file_open_context(filp);
4663 if (request->fl_start < 0 || request->fl_end < 0)
4666 if (IS_GETLK(cmd)) {
4668 return nfs4_proc_getlk(state, F_GETLK, request);
4672 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4675 if (request->fl_type == F_UNLCK) {
4677 return nfs4_proc_unlck(state, cmd, request);
4684 status = nfs4_proc_setlk(state, cmd, request);
4685 if ((status != -EAGAIN) || IS_SETLK(cmd))
4687 timeout = nfs4_set_lock_task_retry(timeout);
4688 status = -ERESTARTSYS;
4691 } while(status < 0);
4695 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4697 struct nfs_server *server = NFS_SERVER(state->inode);
4698 struct nfs4_exception exception = { };
4701 err = nfs4_set_lock_state(state, fl);
4705 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4708 printk(KERN_ERR "NFS: %s: unhandled error "
4709 "%d.\n", __func__, err);
4713 case -NFS4ERR_EXPIRED:
4714 nfs4_schedule_stateid_recovery(server, state);
4715 case -NFS4ERR_STALE_CLIENTID:
4716 case -NFS4ERR_STALE_STATEID:
4717 nfs4_schedule_lease_recovery(server->nfs_client);
4719 case -NFS4ERR_BADSESSION:
4720 case -NFS4ERR_BADSLOT:
4721 case -NFS4ERR_BAD_HIGH_SLOT:
4722 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4723 case -NFS4ERR_DEADSESSION:
4724 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
4728 * The show must go on: exit, but mark the
4729 * stateid as needing recovery.
4731 case -NFS4ERR_DELEG_REVOKED:
4732 case -NFS4ERR_ADMIN_REVOKED:
4733 case -NFS4ERR_BAD_STATEID:
4734 case -NFS4ERR_OPENMODE:
4735 nfs4_schedule_stateid_recovery(server, state);
4740 * User RPCSEC_GSS context has expired.
4741 * We cannot recover this stateid now, so
4742 * skip it and allow recovery thread to
4748 case -NFS4ERR_DENIED:
4749 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4752 case -NFS4ERR_DELAY:
4755 err = nfs4_handle_exception(server, err, &exception);
4756 } while (exception.retry);
4761 struct nfs_release_lockowner_data {
4762 struct nfs4_lock_state *lsp;
4763 struct nfs_server *server;
4764 struct nfs_release_lockowner_args args;
4767 static void nfs4_release_lockowner_release(void *calldata)
4769 struct nfs_release_lockowner_data *data = calldata;
4770 nfs4_free_lock_state(data->server, data->lsp);
4774 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
4775 .rpc_release = nfs4_release_lockowner_release,
4778 int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
4780 struct nfs_server *server = lsp->ls_state->owner->so_server;
4781 struct nfs_release_lockowner_data *data;
4782 struct rpc_message msg = {
4783 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4786 if (server->nfs_client->cl_mvops->minor_version != 0)
4788 data = kmalloc(sizeof(*data), GFP_NOFS);
4792 data->server = server;
4793 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4794 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4795 data->args.lock_owner.s_dev = server->s_dev;
4796 msg.rpc_argp = &data->args;
4797 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4801 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4803 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4804 const void *buf, size_t buflen,
4805 int flags, int type)
4807 if (strcmp(key, "") != 0)
4810 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
4813 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4814 void *buf, size_t buflen, int type)
4816 if (strcmp(key, "") != 0)
4819 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
4822 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4823 size_t list_len, const char *name,
4824 size_t name_len, int type)
4826 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
4828 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4831 if (list && len <= list_len)
4832 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
4837 * nfs_fhget will use either the mounted_on_fileid or the fileid
4839 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4841 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4842 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4843 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4844 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
4847 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4848 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
4849 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4853 int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
4854 struct nfs4_fs_locations *fs_locations, struct page *page)
4856 struct nfs_server *server = NFS_SERVER(dir);
4858 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4860 struct nfs4_fs_locations_arg args = {
4861 .dir_fh = NFS_FH(dir),
4866 struct nfs4_fs_locations_res res = {
4867 .fs_locations = fs_locations,
4869 struct rpc_message msg = {
4870 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4876 dprintk("%s: start\n", __func__);
4878 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4879 * is not supported */
4880 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4881 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4883 bitmask[0] |= FATTR4_WORD0_FILEID;
4885 nfs_fattr_init(&fs_locations->fattr);
4886 fs_locations->server = server;
4887 fs_locations->nlocations = 0;
4888 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4889 dprintk("%s: returned status = %d\n", __func__, status);
4893 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4896 struct nfs4_secinfo_arg args = {
4897 .dir_fh = NFS_FH(dir),
4900 struct nfs4_secinfo_res res = {
4903 struct rpc_message msg = {
4904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4909 dprintk("NFS call secinfo %s\n", name->name);
4910 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4911 dprintk("NFS reply secinfo: %d\n", status);
4915 static int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
4916 struct nfs4_secinfo_flavors *flavors)
4918 struct nfs4_exception exception = { };
4921 err = nfs4_handle_exception(NFS_SERVER(dir),
4922 _nfs4_proc_secinfo(dir, name, flavors),
4924 } while (exception.retry);
4928 #ifdef CONFIG_NFS_V4_1
4930 * Check the exchange flags returned by the server for invalid flags, having
4931 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4934 static int nfs4_check_cl_exchange_flags(u32 flags)
4936 if (flags & ~EXCHGID4_FLAG_MASK_R)
4938 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4939 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4941 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4945 return -NFS4ERR_INVAL;
4949 nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4951 if (a->server_scope_sz == b->server_scope_sz &&
4952 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4959 * nfs4_proc_exchange_id()
4961 * Since the clientid has expired, all compounds using sessions
4962 * associated with the stale clientid will be returning
4963 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4964 * be in some phase of session reset.
4966 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
4968 nfs4_verifier verifier;
4969 struct nfs41_exchange_id_args args = {
4970 .verifier = &verifier,
4972 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
4974 struct nfs41_exchange_id_res res = {
4978 struct rpc_message msg = {
4979 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4985 dprintk("--> %s\n", __func__);
4986 BUG_ON(clp == NULL);
4988 nfs4_construct_boot_verifier(clp, &verifier);
4990 args.id_len = scnprintf(args.id, sizeof(args.id),
4993 init_utsname()->nodename,
4994 init_utsname()->domainname,
4995 clp->cl_rpcclient->cl_auth->au_flavor);
4997 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
4998 if (unlikely(!res.server_scope)) {
5003 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
5004 if (unlikely(!res.impl_id)) {
5006 goto out_server_scope;
5009 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5011 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
5014 /* use the most recent implementation id */
5015 kfree(clp->impl_id);
5016 clp->impl_id = res.impl_id;
5021 if (clp->server_scope &&
5022 !nfs41_same_server_scope(clp->server_scope,
5023 res.server_scope)) {
5024 dprintk("%s: server_scope mismatch detected\n",
5026 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5027 kfree(clp->server_scope);
5028 clp->server_scope = NULL;
5031 if (!clp->server_scope) {
5032 clp->server_scope = res.server_scope;
5038 kfree(res.server_scope);
5041 dprintk("%s: Server Implementation ID: "
5042 "domain: %s, name: %s, date: %llu,%u\n",
5043 __func__, clp->impl_id->domain, clp->impl_id->name,
5044 clp->impl_id->date.seconds,
5045 clp->impl_id->date.nseconds);
5046 dprintk("<-- %s status= %d\n", __func__, status);
5050 struct nfs4_get_lease_time_data {
5051 struct nfs4_get_lease_time_args *args;
5052 struct nfs4_get_lease_time_res *res;
5053 struct nfs_client *clp;
5056 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5060 struct nfs4_get_lease_time_data *data =
5061 (struct nfs4_get_lease_time_data *)calldata;
5063 dprintk("--> %s\n", __func__);
5064 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5065 /* just setup sequence, do not trigger session recovery
5066 since we're invoked within one */
5067 ret = nfs41_setup_sequence(data->clp->cl_session,
5068 &data->args->la_seq_args,
5069 &data->res->lr_seq_res, task);
5071 BUG_ON(ret == -EAGAIN);
5072 rpc_call_start(task);
5073 dprintk("<-- %s\n", __func__);
5077 * Called from nfs4_state_manager thread for session setup, so don't recover
5078 * from sequence operation or clientid errors.
5080 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5082 struct nfs4_get_lease_time_data *data =
5083 (struct nfs4_get_lease_time_data *)calldata;
5085 dprintk("--> %s\n", __func__);
5086 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5088 switch (task->tk_status) {
5089 case -NFS4ERR_DELAY:
5090 case -NFS4ERR_GRACE:
5091 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5092 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5093 task->tk_status = 0;
5095 case -NFS4ERR_RETRY_UNCACHED_REP:
5096 rpc_restart_call_prepare(task);
5099 dprintk("<-- %s\n", __func__);
5102 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5103 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5104 .rpc_call_done = nfs4_get_lease_time_done,
5107 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5109 struct rpc_task *task;
5110 struct nfs4_get_lease_time_args args;
5111 struct nfs4_get_lease_time_res res = {
5112 .lr_fsinfo = fsinfo,
5114 struct nfs4_get_lease_time_data data = {
5119 struct rpc_message msg = {
5120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5124 struct rpc_task_setup task_setup = {
5125 .rpc_client = clp->cl_rpcclient,
5126 .rpc_message = &msg,
5127 .callback_ops = &nfs4_get_lease_time_ops,
5128 .callback_data = &data,
5129 .flags = RPC_TASK_TIMEOUT,
5133 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5134 dprintk("--> %s\n", __func__);
5135 task = rpc_run_task(&task_setup);
5138 status = PTR_ERR(task);
5140 status = task->tk_status;
5143 dprintk("<-- %s return %d\n", __func__, status);
5148 static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5150 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5153 static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5154 struct nfs4_slot *new,
5158 struct nfs4_slot *old = NULL;
5161 spin_lock(&tbl->slot_tbl_lock);
5165 tbl->max_slots = max_slots;
5167 tbl->highest_used_slotid = -1; /* no slot is currently used */
5168 for (i = 0; i < tbl->max_slots; i++)
5169 tbl->slots[i].seq_nr = ivalue;
5170 spin_unlock(&tbl->slot_tbl_lock);
5175 * (re)Initialise a slot table
5177 static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5180 struct nfs4_slot *new = NULL;
5183 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5184 max_reqs, tbl->max_slots);
5186 /* Does the newly negotiated max_reqs match the existing slot table? */
5187 if (max_reqs != tbl->max_slots) {
5188 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
5194 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
5195 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5196 tbl, tbl->slots, tbl->max_slots);
5198 dprintk("<-- %s: return %d\n", __func__, ret);
5202 /* Destroy the slot table */
5203 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5205 if (session->fc_slot_table.slots != NULL) {
5206 kfree(session->fc_slot_table.slots);
5207 session->fc_slot_table.slots = NULL;
5209 if (session->bc_slot_table.slots != NULL) {
5210 kfree(session->bc_slot_table.slots);
5211 session->bc_slot_table.slots = NULL;
5217 * Initialize or reset the forechannel and backchannel tables
5219 static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
5221 struct nfs4_slot_table *tbl;
5224 dprintk("--> %s\n", __func__);
5226 tbl = &ses->fc_slot_table;
5227 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5228 if (status) /* -ENOMEM */
5231 tbl = &ses->bc_slot_table;
5232 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5233 if (status && tbl->slots == NULL)
5234 /* Fore and back channel share a connection so get
5235 * both slot tables or neither */
5236 nfs4_destroy_slot_tables(ses);
5240 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5242 struct nfs4_session *session;
5243 struct nfs4_slot_table *tbl;
5245 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5249 tbl = &session->fc_slot_table;
5250 tbl->highest_used_slotid = NFS4_NO_SLOT;
5251 spin_lock_init(&tbl->slot_tbl_lock);
5252 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5253 init_completion(&tbl->complete);
5255 tbl = &session->bc_slot_table;
5256 tbl->highest_used_slotid = NFS4_NO_SLOT;
5257 spin_lock_init(&tbl->slot_tbl_lock);
5258 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5259 init_completion(&tbl->complete);
5261 session->session_state = 1<<NFS4_SESSION_INITING;
5267 void nfs4_destroy_session(struct nfs4_session *session)
5269 struct rpc_xprt *xprt;
5271 nfs4_proc_destroy_session(session);
5274 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5276 dprintk("%s Destroy backchannel for xprt %p\n",
5278 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
5279 nfs4_destroy_slot_tables(session);
5284 * Initialize the values to be used by the client in CREATE_SESSION
5285 * If nfs4_init_session set the fore channel request and response sizes,
5288 * Set the back channel max_resp_sz_cached to zero to force the client to
5289 * always set csa_cachethis to FALSE because the current implementation
5290 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5292 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5294 struct nfs4_session *session = args->client->cl_session;
5295 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5296 mxresp_sz = session->fc_attrs.max_resp_sz;
5299 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5301 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5302 /* Fore channel attributes */
5303 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5304 args->fc_attrs.max_resp_sz = mxresp_sz;
5305 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5306 args->fc_attrs.max_reqs = max_session_slots;
5308 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5309 "max_ops=%u max_reqs=%u\n",
5311 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5312 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5314 /* Back channel attributes */
5315 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5316 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5317 args->bc_attrs.max_resp_sz_cached = 0;
5318 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5319 args->bc_attrs.max_reqs = 1;
5321 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5322 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5324 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5325 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5326 args->bc_attrs.max_reqs);
5329 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5331 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5332 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5334 if (rcvd->max_resp_sz > sent->max_resp_sz)
5337 * Our requested max_ops is the minimum we need; we're not
5338 * prepared to break up compounds into smaller pieces than that.
5339 * So, no point even trying to continue if the server won't
5342 if (rcvd->max_ops < sent->max_ops)
5344 if (rcvd->max_reqs == 0)
5346 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5347 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5351 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5353 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5354 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5356 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5358 if (rcvd->max_resp_sz < sent->max_resp_sz)
5360 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5362 /* These would render the backchannel useless: */
5363 if (rcvd->max_ops != sent->max_ops)
5365 if (rcvd->max_reqs != sent->max_reqs)
5370 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5371 struct nfs4_session *session)
5375 ret = nfs4_verify_fore_channel_attrs(args, session);
5378 return nfs4_verify_back_channel_attrs(args, session);
5381 static int _nfs4_proc_create_session(struct nfs_client *clp)
5383 struct nfs4_session *session = clp->cl_session;
5384 struct nfs41_create_session_args args = {
5386 .cb_program = NFS4_CALLBACK,
5388 struct nfs41_create_session_res res = {
5391 struct rpc_message msg = {
5392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5398 nfs4_init_channel_attrs(&args);
5399 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5401 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5404 /* Verify the session's negotiated channel_attrs values */
5405 status = nfs4_verify_channel_attrs(&args, session);
5407 /* Increment the clientid slot sequence id */
5415 * Issues a CREATE_SESSION operation to the server.
5416 * It is the responsibility of the caller to verify the session is
5417 * expired before calling this routine.
5419 int nfs4_proc_create_session(struct nfs_client *clp)
5423 struct nfs4_session *session = clp->cl_session;
5425 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5427 status = _nfs4_proc_create_session(clp);
5431 /* Init or reset the session slot tables */
5432 status = nfs4_setup_session_slot_tables(session);
5433 dprintk("slot table setup returned %d\n", status);
5437 ptr = (unsigned *)&session->sess_id.data[0];
5438 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5439 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5441 dprintk("<-- %s\n", __func__);
5446 * Issue the over-the-wire RPC DESTROY_SESSION.
5447 * The caller must serialize access to this routine.
5449 int nfs4_proc_destroy_session(struct nfs4_session *session)
5452 struct rpc_message msg;
5454 dprintk("--> nfs4_proc_destroy_session\n");
5456 /* session is still being setup */
5457 if (session->clp->cl_cons_state != NFS_CS_READY)
5460 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5461 msg.rpc_argp = session;
5462 msg.rpc_resp = NULL;
5463 msg.rpc_cred = NULL;
5464 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5468 "NFS: Got error %d from the server on DESTROY_SESSION. "
5469 "Session has been destroyed regardless...\n", status);
5471 dprintk("<-- nfs4_proc_destroy_session\n");
5475 int nfs4_init_session(struct nfs_server *server)
5477 struct nfs_client *clp = server->nfs_client;
5478 struct nfs4_session *session;
5479 unsigned int rsize, wsize;
5482 if (!nfs4_has_session(clp))
5485 session = clp->cl_session;
5486 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5489 rsize = server->rsize;
5491 rsize = NFS_MAX_FILE_IO_SIZE;
5492 wsize = server->wsize;
5494 wsize = NFS_MAX_FILE_IO_SIZE;
5496 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5497 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5499 ret = nfs4_recover_expired_lease(server);
5501 ret = nfs4_check_client_ready(clp);
5505 int nfs4_init_ds_session(struct nfs_client *clp)
5507 struct nfs4_session *session = clp->cl_session;
5510 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5513 ret = nfs4_client_recover_expired_lease(clp);
5515 /* Test for the DS role */
5516 if (!is_ds_client(clp))
5519 ret = nfs4_check_client_ready(clp);
5523 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5527 * Renew the cl_session lease.
5529 struct nfs4_sequence_data {
5530 struct nfs_client *clp;
5531 struct nfs4_sequence_args args;
5532 struct nfs4_sequence_res res;
5535 static void nfs41_sequence_release(void *data)
5537 struct nfs4_sequence_data *calldata = data;
5538 struct nfs_client *clp = calldata->clp;
5540 if (atomic_read(&clp->cl_count) > 1)
5541 nfs4_schedule_state_renewal(clp);
5542 nfs_put_client(clp);
5546 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5548 switch(task->tk_status) {
5549 case -NFS4ERR_DELAY:
5550 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5553 nfs4_schedule_lease_recovery(clp);
5558 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5560 struct nfs4_sequence_data *calldata = data;
5561 struct nfs_client *clp = calldata->clp;
5563 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5566 if (task->tk_status < 0) {
5567 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5568 if (atomic_read(&clp->cl_count) == 1)
5571 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5572 rpc_restart_call_prepare(task);
5576 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5578 dprintk("<-- %s\n", __func__);
5581 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5583 struct nfs4_sequence_data *calldata = data;
5584 struct nfs_client *clp = calldata->clp;
5585 struct nfs4_sequence_args *args;
5586 struct nfs4_sequence_res *res;
5588 args = task->tk_msg.rpc_argp;
5589 res = task->tk_msg.rpc_resp;
5591 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
5593 rpc_call_start(task);
5596 static const struct rpc_call_ops nfs41_sequence_ops = {
5597 .rpc_call_done = nfs41_sequence_call_done,
5598 .rpc_call_prepare = nfs41_sequence_prepare,
5599 .rpc_release = nfs41_sequence_release,
5602 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5604 struct nfs4_sequence_data *calldata;
5605 struct rpc_message msg = {
5606 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5609 struct rpc_task_setup task_setup_data = {
5610 .rpc_client = clp->cl_rpcclient,
5611 .rpc_message = &msg,
5612 .callback_ops = &nfs41_sequence_ops,
5613 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5616 if (!atomic_inc_not_zero(&clp->cl_count))
5617 return ERR_PTR(-EIO);
5618 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5619 if (calldata == NULL) {
5620 nfs_put_client(clp);
5621 return ERR_PTR(-ENOMEM);
5623 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
5624 msg.rpc_argp = &calldata->args;
5625 msg.rpc_resp = &calldata->res;
5626 calldata->clp = clp;
5627 task_setup_data.callback_data = calldata;
5629 return rpc_run_task(&task_setup_data);
5632 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5634 struct rpc_task *task;
5637 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5639 task = _nfs41_proc_sequence(clp, cred);
5641 ret = PTR_ERR(task);
5643 rpc_put_task_async(task);
5644 dprintk("<-- %s status=%d\n", __func__, ret);
5648 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5650 struct rpc_task *task;
5653 task = _nfs41_proc_sequence(clp, cred);
5655 ret = PTR_ERR(task);
5658 ret = rpc_wait_for_completion_task(task);
5660 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5662 if (task->tk_status == 0)
5663 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5664 ret = task->tk_status;
5668 dprintk("<-- %s status=%d\n", __func__, ret);
5672 struct nfs4_reclaim_complete_data {
5673 struct nfs_client *clp;
5674 struct nfs41_reclaim_complete_args arg;
5675 struct nfs41_reclaim_complete_res res;
5678 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5680 struct nfs4_reclaim_complete_data *calldata = data;
5682 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5683 if (nfs41_setup_sequence(calldata->clp->cl_session,
5684 &calldata->arg.seq_args,
5685 &calldata->res.seq_res, task))
5688 rpc_call_start(task);
5691 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5693 switch(task->tk_status) {
5695 case -NFS4ERR_COMPLETE_ALREADY:
5696 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5698 case -NFS4ERR_DELAY:
5699 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5701 case -NFS4ERR_RETRY_UNCACHED_REP:
5704 nfs4_schedule_lease_recovery(clp);
5709 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5711 struct nfs4_reclaim_complete_data *calldata = data;
5712 struct nfs_client *clp = calldata->clp;
5713 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5715 dprintk("--> %s\n", __func__);
5716 if (!nfs41_sequence_done(task, res))
5719 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5720 rpc_restart_call_prepare(task);
5723 dprintk("<-- %s\n", __func__);
5726 static void nfs4_free_reclaim_complete_data(void *data)
5728 struct nfs4_reclaim_complete_data *calldata = data;
5733 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5734 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5735 .rpc_call_done = nfs4_reclaim_complete_done,
5736 .rpc_release = nfs4_free_reclaim_complete_data,
5740 * Issue a global reclaim complete.
5742 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5744 struct nfs4_reclaim_complete_data *calldata;
5745 struct rpc_task *task;
5746 struct rpc_message msg = {
5747 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5749 struct rpc_task_setup task_setup_data = {
5750 .rpc_client = clp->cl_rpcclient,
5751 .rpc_message = &msg,
5752 .callback_ops = &nfs4_reclaim_complete_call_ops,
5753 .flags = RPC_TASK_ASYNC,
5755 int status = -ENOMEM;
5757 dprintk("--> %s\n", __func__);
5758 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5759 if (calldata == NULL)
5761 calldata->clp = clp;
5762 calldata->arg.one_fs = 0;
5764 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
5765 msg.rpc_argp = &calldata->arg;
5766 msg.rpc_resp = &calldata->res;
5767 task_setup_data.callback_data = calldata;
5768 task = rpc_run_task(&task_setup_data);
5770 status = PTR_ERR(task);
5773 status = nfs4_wait_for_completion_rpc_task(task);
5775 status = task->tk_status;
5779 dprintk("<-- %s status=%d\n", __func__, status);
5784 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5786 struct nfs4_layoutget *lgp = calldata;
5787 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5789 dprintk("--> %s\n", __func__);
5790 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5791 * right now covering the LAYOUTGET we are about to send.
5792 * However, that is not so catastrophic, and there seems
5793 * to be no way to prevent it completely.
5795 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5796 &lgp->res.seq_res, task))
5798 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5799 NFS_I(lgp->args.inode)->layout,
5800 lgp->args.ctx->state)) {
5801 rpc_exit(task, NFS4_OK);
5804 rpc_call_start(task);
5807 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5809 struct nfs4_layoutget *lgp = calldata;
5810 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5812 dprintk("--> %s\n", __func__);
5814 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5817 switch (task->tk_status) {
5820 case -NFS4ERR_LAYOUTTRYLATER:
5821 case -NFS4ERR_RECALLCONFLICT:
5822 task->tk_status = -NFS4ERR_DELAY;
5825 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5826 rpc_restart_call_prepare(task);
5830 dprintk("<-- %s\n", __func__);
5833 static void nfs4_layoutget_release(void *calldata)
5835 struct nfs4_layoutget *lgp = calldata;
5837 dprintk("--> %s\n", __func__);
5838 put_nfs_open_context(lgp->args.ctx);
5840 dprintk("<-- %s\n", __func__);
5843 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5844 .rpc_call_prepare = nfs4_layoutget_prepare,
5845 .rpc_call_done = nfs4_layoutget_done,
5846 .rpc_release = nfs4_layoutget_release,
5849 int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5851 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5852 struct rpc_task *task;
5853 struct rpc_message msg = {
5854 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5855 .rpc_argp = &lgp->args,
5856 .rpc_resp = &lgp->res,
5858 struct rpc_task_setup task_setup_data = {
5859 .rpc_client = server->client,
5860 .rpc_message = &msg,
5861 .callback_ops = &nfs4_layoutget_call_ops,
5862 .callback_data = lgp,
5863 .flags = RPC_TASK_ASYNC,
5867 dprintk("--> %s\n", __func__);
5869 lgp->res.layoutp = &lgp->args.layout;
5870 lgp->res.seq_res.sr_slot = NULL;
5871 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
5872 task = rpc_run_task(&task_setup_data);
5874 return PTR_ERR(task);
5875 status = nfs4_wait_for_completion_rpc_task(task);
5877 status = task->tk_status;
5879 status = pnfs_layout_process(lgp);
5881 dprintk("<-- %s status=%d\n", __func__, status);
5886 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5888 struct nfs4_layoutreturn *lrp = calldata;
5890 dprintk("--> %s\n", __func__);
5891 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5892 &lrp->res.seq_res, task))
5894 rpc_call_start(task);
5897 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5899 struct nfs4_layoutreturn *lrp = calldata;
5900 struct nfs_server *server;
5901 struct pnfs_layout_hdr *lo = lrp->args.layout;
5903 dprintk("--> %s\n", __func__);
5905 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5908 server = NFS_SERVER(lrp->args.inode);
5909 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5910 rpc_restart_call_prepare(task);
5913 spin_lock(&lo->plh_inode->i_lock);
5914 if (task->tk_status == 0) {
5915 if (lrp->res.lrs_present) {
5916 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
5918 BUG_ON(!list_empty(&lo->plh_segs));
5920 lo->plh_block_lgets--;
5921 spin_unlock(&lo->plh_inode->i_lock);
5922 dprintk("<-- %s\n", __func__);
5925 static void nfs4_layoutreturn_release(void *calldata)
5927 struct nfs4_layoutreturn *lrp = calldata;
5929 dprintk("--> %s\n", __func__);
5930 put_layout_hdr(lrp->args.layout);
5932 dprintk("<-- %s\n", __func__);
5935 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5936 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5937 .rpc_call_done = nfs4_layoutreturn_done,
5938 .rpc_release = nfs4_layoutreturn_release,
5941 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5943 struct rpc_task *task;
5944 struct rpc_message msg = {
5945 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5946 .rpc_argp = &lrp->args,
5947 .rpc_resp = &lrp->res,
5949 struct rpc_task_setup task_setup_data = {
5950 .rpc_client = lrp->clp->cl_rpcclient,
5951 .rpc_message = &msg,
5952 .callback_ops = &nfs4_layoutreturn_call_ops,
5953 .callback_data = lrp,
5957 dprintk("--> %s\n", __func__);
5958 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
5959 task = rpc_run_task(&task_setup_data);
5961 return PTR_ERR(task);
5962 status = task->tk_status;
5963 dprintk("<-- %s status=%d\n", __func__, status);
5969 * Retrieve the list of Data Server devices from the MDS.
5971 static int _nfs4_getdevicelist(struct nfs_server *server,
5972 const struct nfs_fh *fh,
5973 struct pnfs_devicelist *devlist)
5975 struct nfs4_getdevicelist_args args = {
5977 .layoutclass = server->pnfs_curr_ld->id,
5979 struct nfs4_getdevicelist_res res = {
5982 struct rpc_message msg = {
5983 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5989 dprintk("--> %s\n", __func__);
5990 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5992 dprintk("<-- %s status=%d\n", __func__, status);
5996 int nfs4_proc_getdevicelist(struct nfs_server *server,
5997 const struct nfs_fh *fh,
5998 struct pnfs_devicelist *devlist)
6000 struct nfs4_exception exception = { };
6004 err = nfs4_handle_exception(server,
6005 _nfs4_getdevicelist(server, fh, devlist),
6007 } while (exception.retry);
6009 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6010 err, devlist->num_devs);
6014 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6017 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6019 struct nfs4_getdeviceinfo_args args = {
6022 struct nfs4_getdeviceinfo_res res = {
6025 struct rpc_message msg = {
6026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6032 dprintk("--> %s\n", __func__);
6033 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6034 dprintk("<-- %s status=%d\n", __func__, status);
6039 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6041 struct nfs4_exception exception = { };
6045 err = nfs4_handle_exception(server,
6046 _nfs4_proc_getdeviceinfo(server, pdev),
6048 } while (exception.retry);
6051 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6053 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6055 struct nfs4_layoutcommit_data *data = calldata;
6056 struct nfs_server *server = NFS_SERVER(data->args.inode);
6058 if (nfs4_setup_sequence(server, &data->args.seq_args,
6059 &data->res.seq_res, task))
6061 rpc_call_start(task);
6065 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6067 struct nfs4_layoutcommit_data *data = calldata;
6068 struct nfs_server *server = NFS_SERVER(data->args.inode);
6070 if (!nfs4_sequence_done(task, &data->res.seq_res))
6073 switch (task->tk_status) { /* Just ignore these failures */
6074 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6075 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6076 case NFS4ERR_BADLAYOUT: /* no layout */
6077 case NFS4ERR_GRACE: /* loca_recalim always false */
6078 task->tk_status = 0;
6081 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6082 rpc_restart_call_prepare(task);
6086 if (task->tk_status == 0)
6087 nfs_post_op_update_inode_force_wcc(data->args.inode,
6091 static void nfs4_layoutcommit_release(void *calldata)
6093 struct nfs4_layoutcommit_data *data = calldata;
6094 struct pnfs_layout_segment *lseg, *tmp;
6095 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
6097 pnfs_cleanup_layoutcommit(data);
6098 /* Matched by references in pnfs_set_layoutcommit */
6099 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6100 list_del_init(&lseg->pls_lc_list);
6101 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6106 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6107 smp_mb__after_clear_bit();
6108 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6110 put_rpccred(data->cred);
6114 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6115 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6116 .rpc_call_done = nfs4_layoutcommit_done,
6117 .rpc_release = nfs4_layoutcommit_release,
6121 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6123 struct rpc_message msg = {
6124 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6125 .rpc_argp = &data->args,
6126 .rpc_resp = &data->res,
6127 .rpc_cred = data->cred,
6129 struct rpc_task_setup task_setup_data = {
6130 .task = &data->task,
6131 .rpc_client = NFS_CLIENT(data->args.inode),
6132 .rpc_message = &msg,
6133 .callback_ops = &nfs4_layoutcommit_ops,
6134 .callback_data = data,
6135 .flags = RPC_TASK_ASYNC,
6137 struct rpc_task *task;
6140 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6141 "lbw: %llu inode %lu\n",
6142 data->task.tk_pid, sync,
6143 data->args.lastbytewritten,
6144 data->args.inode->i_ino);
6146 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6147 task = rpc_run_task(&task_setup_data);
6149 return PTR_ERR(task);
6152 status = nfs4_wait_for_completion_rpc_task(task);
6155 status = task->tk_status;
6157 dprintk("%s: status %d\n", __func__, status);
6163 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6164 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6166 struct nfs41_secinfo_no_name_args args = {
6167 .style = SECINFO_STYLE_CURRENT_FH,
6169 struct nfs4_secinfo_res res = {
6172 struct rpc_message msg = {
6173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6177 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6181 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6182 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6184 struct nfs4_exception exception = { };
6187 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6190 case -NFS4ERR_WRONGSEC:
6191 case -NFS4ERR_NOTSUPP:
6194 err = nfs4_handle_exception(server, err, &exception);
6196 } while (exception.retry);
6201 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6202 struct nfs_fsinfo *info)
6206 rpc_authflavor_t flavor;
6207 struct nfs4_secinfo_flavors *flavors;
6209 page = alloc_page(GFP_KERNEL);
6215 flavors = page_address(page);
6216 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6219 * Fall back on "guess and check" method if
6220 * the server doesn't support SECINFO_NO_NAME
6222 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6223 err = nfs4_find_root_sec(server, fhandle, info);
6229 flavor = nfs_find_best_sec(flavors);
6231 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6241 static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6244 struct nfs41_test_stateid_args args = {
6247 struct nfs41_test_stateid_res res;
6248 struct rpc_message msg = {
6249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6254 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6255 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6257 if (status == NFS_OK)
6262 static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6264 struct nfs4_exception exception = { };
6267 err = nfs4_handle_exception(server,
6268 _nfs41_test_stateid(server, stateid),
6270 } while (exception.retry);
6274 static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6276 struct nfs41_free_stateid_args args = {
6279 struct nfs41_free_stateid_res res;
6280 struct rpc_message msg = {
6281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6286 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6287 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6290 static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6292 struct nfs4_exception exception = { };
6295 err = nfs4_handle_exception(server,
6296 _nfs4_free_stateid(server, stateid),
6298 } while (exception.retry);
6302 static bool nfs41_match_stateid(const nfs4_stateid *s1,
6303 const nfs4_stateid *s2)
6305 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
6308 if (s1->seqid == s2->seqid)
6310 if (s1->seqid == 0 || s2->seqid == 0)
6316 #endif /* CONFIG_NFS_V4_1 */
6318 static bool nfs4_match_stateid(const nfs4_stateid *s1,
6319 const nfs4_stateid *s2)
6321 return nfs4_stateid_match(s1, s2);
6325 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6326 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6327 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6328 .recover_open = nfs4_open_reclaim,
6329 .recover_lock = nfs4_lock_reclaim,
6330 .establish_clid = nfs4_init_clientid,
6331 .get_clid_cred = nfs4_get_setclientid_cred,
6334 #if defined(CONFIG_NFS_V4_1)
6335 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6336 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6337 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6338 .recover_open = nfs4_open_reclaim,
6339 .recover_lock = nfs4_lock_reclaim,
6340 .establish_clid = nfs41_init_clientid,
6341 .get_clid_cred = nfs4_get_exchange_id_cred,
6342 .reclaim_complete = nfs41_proc_reclaim_complete,
6344 #endif /* CONFIG_NFS_V4_1 */
6346 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6347 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6348 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6349 .recover_open = nfs4_open_expired,
6350 .recover_lock = nfs4_lock_expired,
6351 .establish_clid = nfs4_init_clientid,
6352 .get_clid_cred = nfs4_get_setclientid_cred,
6355 #if defined(CONFIG_NFS_V4_1)
6356 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6357 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6358 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6359 .recover_open = nfs41_open_expired,
6360 .recover_lock = nfs41_lock_expired,
6361 .establish_clid = nfs41_init_clientid,
6362 .get_clid_cred = nfs4_get_exchange_id_cred,
6364 #endif /* CONFIG_NFS_V4_1 */
6366 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6367 .sched_state_renewal = nfs4_proc_async_renew,
6368 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6369 .renew_lease = nfs4_proc_renew,
6372 #if defined(CONFIG_NFS_V4_1)
6373 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6374 .sched_state_renewal = nfs41_proc_async_sequence,
6375 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
6376 .renew_lease = nfs4_proc_sequence,
6380 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6382 .call_sync = _nfs4_call_sync,
6383 .match_stateid = nfs4_match_stateid,
6384 .find_root_sec = nfs4_find_root_sec,
6385 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6386 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6387 .state_renewal_ops = &nfs40_state_renewal_ops,
6390 #if defined(CONFIG_NFS_V4_1)
6391 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6393 .call_sync = _nfs4_call_sync_session,
6394 .match_stateid = nfs41_match_stateid,
6395 .find_root_sec = nfs41_find_root_sec,
6396 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6397 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6398 .state_renewal_ops = &nfs41_state_renewal_ops,
6402 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6403 [0] = &nfs_v4_0_minor_ops,
6404 #if defined(CONFIG_NFS_V4_1)
6405 [1] = &nfs_v4_1_minor_ops,
6409 static const struct inode_operations nfs4_file_inode_operations = {
6410 .permission = nfs_permission,
6411 .getattr = nfs_getattr,
6412 .setattr = nfs_setattr,
6413 .getxattr = generic_getxattr,
6414 .setxattr = generic_setxattr,
6415 .listxattr = generic_listxattr,
6416 .removexattr = generic_removexattr,
6419 const struct nfs_rpc_ops nfs_v4_clientops = {
6420 .version = 4, /* protocol version */
6421 .dentry_ops = &nfs4_dentry_operations,
6422 .dir_inode_ops = &nfs4_dir_inode_operations,
6423 .file_inode_ops = &nfs4_file_inode_operations,
6424 .file_ops = &nfs4_file_operations,
6425 .getroot = nfs4_proc_get_root,
6426 .getattr = nfs4_proc_getattr,
6427 .setattr = nfs4_proc_setattr,
6428 .lookup = nfs4_proc_lookup,
6429 .access = nfs4_proc_access,
6430 .readlink = nfs4_proc_readlink,
6431 .create = nfs4_proc_create,
6432 .remove = nfs4_proc_remove,
6433 .unlink_setup = nfs4_proc_unlink_setup,
6434 .unlink_done = nfs4_proc_unlink_done,
6435 .rename = nfs4_proc_rename,
6436 .rename_setup = nfs4_proc_rename_setup,
6437 .rename_done = nfs4_proc_rename_done,
6438 .link = nfs4_proc_link,
6439 .symlink = nfs4_proc_symlink,
6440 .mkdir = nfs4_proc_mkdir,
6441 .rmdir = nfs4_proc_remove,
6442 .readdir = nfs4_proc_readdir,
6443 .mknod = nfs4_proc_mknod,
6444 .statfs = nfs4_proc_statfs,
6445 .fsinfo = nfs4_proc_fsinfo,
6446 .pathconf = nfs4_proc_pathconf,
6447 .set_capabilities = nfs4_server_capabilities,
6448 .decode_dirent = nfs4_decode_dirent,
6449 .read_setup = nfs4_proc_read_setup,
6450 .read_done = nfs4_read_done,
6451 .write_setup = nfs4_proc_write_setup,
6452 .write_done = nfs4_write_done,
6453 .commit_setup = nfs4_proc_commit_setup,
6454 .commit_done = nfs4_commit_done,
6455 .lock = nfs4_proc_lock,
6456 .clear_acl_cache = nfs4_zap_acl_attr,
6457 .close_context = nfs4_close_context,
6458 .open_context = nfs4_atomic_open,
6459 .init_client = nfs4_init_client,
6460 .secinfo = nfs4_proc_secinfo,
6463 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6464 .prefix = XATTR_NAME_NFSV4_ACL,
6465 .list = nfs4_xattr_list_nfs4_acl,
6466 .get = nfs4_xattr_get_nfs4_acl,
6467 .set = nfs4_xattr_set_nfs4_acl,
6470 const struct xattr_handler *nfs4_xattr_handlers[] = {
6471 &nfs4_xattr_nfs4_acl_handler,
6475 module_param(max_session_slots, ushort, 0644);
6476 MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6477 "requests the client will negotiate");