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 *, struct nfs_fh *, struct nfs_fattr *);
84 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
85 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
86 struct nfs_fattr *fattr, struct iattr *sattr,
87 struct nfs4_state *state);
88 #ifdef CONFIG_NFS_V4_1
89 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
90 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
92 /* Prevent leaks of NFSv4 errors into userland */
93 static int nfs4_map_errors(int err)
98 case -NFS4ERR_RESOURCE:
100 case -NFS4ERR_WRONGSEC:
102 case -NFS4ERR_BADOWNER:
103 case -NFS4ERR_BADNAME:
106 dprintk("%s could not handle NFSv4 error %d\n",
114 * This is our standard bitmap for GETATTR requests.
116 const u32 nfs4_fattr_bitmap[2] = {
118 | FATTR4_WORD0_CHANGE
121 | FATTR4_WORD0_FILEID,
123 | FATTR4_WORD1_NUMLINKS
125 | FATTR4_WORD1_OWNER_GROUP
126 | FATTR4_WORD1_RAWDEV
127 | FATTR4_WORD1_SPACE_USED
128 | FATTR4_WORD1_TIME_ACCESS
129 | FATTR4_WORD1_TIME_METADATA
130 | FATTR4_WORD1_TIME_MODIFY
133 const u32 nfs4_statfs_bitmap[2] = {
134 FATTR4_WORD0_FILES_AVAIL
135 | FATTR4_WORD0_FILES_FREE
136 | FATTR4_WORD0_FILES_TOTAL,
137 FATTR4_WORD1_SPACE_AVAIL
138 | FATTR4_WORD1_SPACE_FREE
139 | FATTR4_WORD1_SPACE_TOTAL
142 const u32 nfs4_pathconf_bitmap[2] = {
144 | FATTR4_WORD0_MAXNAME,
148 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
149 | FATTR4_WORD0_MAXREAD
150 | FATTR4_WORD0_MAXWRITE
151 | FATTR4_WORD0_LEASE_TIME,
152 FATTR4_WORD1_TIME_DELTA
153 | FATTR4_WORD1_FS_LAYOUT_TYPES,
154 FATTR4_WORD2_LAYOUT_BLKSIZE
157 const u32 nfs4_fs_locations_bitmap[2] = {
159 | FATTR4_WORD0_CHANGE
162 | FATTR4_WORD0_FILEID
163 | FATTR4_WORD0_FS_LOCATIONS,
165 | FATTR4_WORD1_NUMLINKS
167 | FATTR4_WORD1_OWNER_GROUP
168 | FATTR4_WORD1_RAWDEV
169 | FATTR4_WORD1_SPACE_USED
170 | FATTR4_WORD1_TIME_ACCESS
171 | FATTR4_WORD1_TIME_METADATA
172 | FATTR4_WORD1_TIME_MODIFY
173 | FATTR4_WORD1_MOUNTED_ON_FILEID
176 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
177 struct nfs4_readdir_arg *readdir)
181 BUG_ON(readdir->count < 80);
183 readdir->cookie = cookie;
184 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
189 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
194 * NFSv4 servers do not return entries for '.' and '..'
195 * Therefore, we fake these entries here. We let '.'
196 * have cookie 0 and '..' have cookie 1. Note that
197 * when talking to the server, we always send cookie 0
200 start = p = kmap_atomic(*readdir->pages);
203 *p++ = xdr_one; /* next */
204 *p++ = xdr_zero; /* cookie, first word */
205 *p++ = xdr_one; /* cookie, second word */
206 *p++ = xdr_one; /* entry len */
207 memcpy(p, ".\0\0\0", 4); /* entry */
209 *p++ = xdr_one; /* bitmap length */
210 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
211 *p++ = htonl(8); /* attribute buffer length */
212 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
215 *p++ = xdr_one; /* next */
216 *p++ = xdr_zero; /* cookie, first word */
217 *p++ = xdr_two; /* cookie, second word */
218 *p++ = xdr_two; /* entry len */
219 memcpy(p, "..\0\0", 4); /* entry */
221 *p++ = xdr_one; /* bitmap length */
222 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
223 *p++ = htonl(8); /* attribute buffer length */
224 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
226 readdir->pgbase = (char *)p - (char *)start;
227 readdir->count -= readdir->pgbase;
228 kunmap_atomic(start);
231 static int nfs4_wait_clnt_recover(struct nfs_client *clp)
237 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
238 nfs_wait_bit_killable, TASK_KILLABLE);
242 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
249 *timeout = NFS4_POLL_RETRY_MIN;
250 if (*timeout > NFS4_POLL_RETRY_MAX)
251 *timeout = NFS4_POLL_RETRY_MAX;
252 freezable_schedule_timeout_killable(*timeout);
253 if (fatal_signal_pending(current))
259 /* This is the error handling routine for processes that are allowed
262 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
264 struct nfs_client *clp = server->nfs_client;
265 struct nfs4_state *state = exception->state;
266 struct inode *inode = exception->inode;
269 exception->retry = 0;
273 case -NFS4ERR_OPENMODE:
274 if (inode && nfs_have_delegation(inode, FMODE_READ)) {
275 nfs_inode_return_delegation(inode);
276 exception->retry = 1;
281 nfs4_schedule_stateid_recovery(server, state);
282 goto wait_on_recovery;
283 case -NFS4ERR_DELEG_REVOKED:
284 case -NFS4ERR_ADMIN_REVOKED:
285 case -NFS4ERR_BAD_STATEID:
288 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;
793 struct dentry *dentry;
794 struct nfs4_state_owner *owner;
795 struct nfs4_state *state;
797 unsigned long timestamp;
798 unsigned int rpc_done : 1;
804 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
806 p->o_res.f_attr = &p->f_attr;
807 p->o_res.seqid = p->o_arg.seqid;
808 p->c_res.seqid = p->c_arg.seqid;
809 p->o_res.server = p->o_arg.server;
810 nfs_fattr_init(&p->f_attr);
811 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
814 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
815 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
816 const struct iattr *attrs,
819 struct dentry *parent = dget_parent(dentry);
820 struct inode *dir = parent->d_inode;
821 struct nfs_server *server = NFS_SERVER(dir);
822 struct nfs4_opendata *p;
824 p = kzalloc(sizeof(*p), gfp_mask);
827 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
828 if (p->o_arg.seqid == NULL)
830 nfs_sb_active(dentry->d_sb);
831 p->dentry = dget(dentry);
834 atomic_inc(&sp->so_count);
835 p->o_arg.fh = NFS_FH(dir);
836 p->o_arg.open_flags = flags;
837 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
838 p->o_arg.clientid = server->nfs_client->cl_clientid;
839 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
840 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
841 p->o_arg.name = &dentry->d_name;
842 p->o_arg.server = server;
843 p->o_arg.bitmask = server->attr_bitmask;
844 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
845 if (attrs != NULL && attrs->ia_valid != 0) {
848 p->o_arg.u.attrs = &p->attrs;
849 memcpy(&p->attrs, attrs, sizeof(p->attrs));
852 verf[1] = current->pid;
853 memcpy(p->o_arg.u.verifier.data, verf,
854 sizeof(p->o_arg.u.verifier.data));
856 p->c_arg.fh = &p->o_res.fh;
857 p->c_arg.stateid = &p->o_res.stateid;
858 p->c_arg.seqid = p->o_arg.seqid;
859 nfs4_init_opendata_res(p);
869 static void nfs4_opendata_free(struct kref *kref)
871 struct nfs4_opendata *p = container_of(kref,
872 struct nfs4_opendata, kref);
873 struct super_block *sb = p->dentry->d_sb;
875 nfs_free_seqid(p->o_arg.seqid);
876 if (p->state != NULL)
877 nfs4_put_open_state(p->state);
878 nfs4_put_state_owner(p->owner);
882 nfs_fattr_free_names(&p->f_attr);
886 static void nfs4_opendata_put(struct nfs4_opendata *p)
889 kref_put(&p->kref, nfs4_opendata_free);
892 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
896 ret = rpc_wait_for_completion_task(task);
900 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
904 if (open_mode & (O_EXCL|O_TRUNC))
906 switch (mode & (FMODE_READ|FMODE_WRITE)) {
908 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
909 && state->n_rdonly != 0;
912 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
913 && state->n_wronly != 0;
915 case FMODE_READ|FMODE_WRITE:
916 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
917 && state->n_rdwr != 0;
923 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
925 if (delegation == NULL)
927 if ((delegation->type & fmode) != fmode)
929 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
931 nfs_mark_delegation_referenced(delegation);
935 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
944 case FMODE_READ|FMODE_WRITE:
947 nfs4_state_set_mode_locked(state, state->state | fmode);
950 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
952 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
953 nfs4_stateid_copy(&state->stateid, stateid);
954 nfs4_stateid_copy(&state->open_stateid, stateid);
957 set_bit(NFS_O_RDONLY_STATE, &state->flags);
960 set_bit(NFS_O_WRONLY_STATE, &state->flags);
962 case FMODE_READ|FMODE_WRITE:
963 set_bit(NFS_O_RDWR_STATE, &state->flags);
967 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
969 write_seqlock(&state->seqlock);
970 nfs_set_open_stateid_locked(state, stateid, fmode);
971 write_sequnlock(&state->seqlock);
974 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
977 * Protect the call to nfs4_state_set_mode_locked and
978 * serialise the stateid update
980 write_seqlock(&state->seqlock);
981 if (deleg_stateid != NULL) {
982 nfs4_stateid_copy(&state->stateid, deleg_stateid);
983 set_bit(NFS_DELEGATED_STATE, &state->flags);
985 if (open_stateid != NULL)
986 nfs_set_open_stateid_locked(state, open_stateid, fmode);
987 write_sequnlock(&state->seqlock);
988 spin_lock(&state->owner->so_lock);
989 update_open_stateflags(state, fmode);
990 spin_unlock(&state->owner->so_lock);
993 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
995 struct nfs_inode *nfsi = NFS_I(state->inode);
996 struct nfs_delegation *deleg_cur;
999 fmode &= (FMODE_READ|FMODE_WRITE);
1002 deleg_cur = rcu_dereference(nfsi->delegation);
1003 if (deleg_cur == NULL)
1006 spin_lock(&deleg_cur->lock);
1007 if (nfsi->delegation != deleg_cur ||
1008 (deleg_cur->type & fmode) != fmode)
1009 goto no_delegation_unlock;
1011 if (delegation == NULL)
1012 delegation = &deleg_cur->stateid;
1013 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1014 goto no_delegation_unlock;
1016 nfs_mark_delegation_referenced(deleg_cur);
1017 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
1019 no_delegation_unlock:
1020 spin_unlock(&deleg_cur->lock);
1024 if (!ret && open_stateid != NULL) {
1025 __update_open_stateid(state, open_stateid, NULL, fmode);
1033 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1035 struct nfs_delegation *delegation;
1038 delegation = rcu_dereference(NFS_I(inode)->delegation);
1039 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1044 nfs_inode_return_delegation(inode);
1047 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1049 struct nfs4_state *state = opendata->state;
1050 struct nfs_inode *nfsi = NFS_I(state->inode);
1051 struct nfs_delegation *delegation;
1052 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
1053 fmode_t fmode = opendata->o_arg.fmode;
1054 nfs4_stateid stateid;
1058 if (can_open_cached(state, fmode, open_mode)) {
1059 spin_lock(&state->owner->so_lock);
1060 if (can_open_cached(state, fmode, open_mode)) {
1061 update_open_stateflags(state, fmode);
1062 spin_unlock(&state->owner->so_lock);
1063 goto out_return_state;
1065 spin_unlock(&state->owner->so_lock);
1068 delegation = rcu_dereference(nfsi->delegation);
1069 if (!can_open_delegated(delegation, fmode)) {
1073 /* Save the delegation */
1074 nfs4_stateid_copy(&stateid, &delegation->stateid);
1076 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1081 /* Try to update the stateid using the delegation */
1082 if (update_open_stateid(state, NULL, &stateid, fmode))
1083 goto out_return_state;
1086 return ERR_PTR(ret);
1088 atomic_inc(&state->count);
1092 static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1094 struct inode *inode;
1095 struct nfs4_state *state = NULL;
1096 struct nfs_delegation *delegation;
1099 if (!data->rpc_done) {
1100 state = nfs4_try_open_cached(data);
1105 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1107 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1108 ret = PTR_ERR(inode);
1112 state = nfs4_get_open_state(inode, data->owner);
1115 if (data->o_res.delegation_type != 0) {
1116 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1117 int delegation_flags = 0;
1120 delegation = rcu_dereference(NFS_I(inode)->delegation);
1122 delegation_flags = delegation->flags;
1124 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1125 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1126 "returning a delegation for "
1127 "OPEN(CLAIM_DELEGATE_CUR)\n",
1129 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1130 nfs_inode_set_delegation(state->inode,
1131 data->owner->so_cred,
1134 nfs_inode_reclaim_delegation(state->inode,
1135 data->owner->so_cred,
1139 update_open_stateid(state, &data->o_res.stateid, NULL,
1147 return ERR_PTR(ret);
1150 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1152 struct nfs_inode *nfsi = NFS_I(state->inode);
1153 struct nfs_open_context *ctx;
1155 spin_lock(&state->inode->i_lock);
1156 list_for_each_entry(ctx, &nfsi->open_files, list) {
1157 if (ctx->state != state)
1159 get_nfs_open_context(ctx);
1160 spin_unlock(&state->inode->i_lock);
1163 spin_unlock(&state->inode->i_lock);
1164 return ERR_PTR(-ENOENT);
1167 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1169 struct nfs4_opendata *opendata;
1171 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1172 if (opendata == NULL)
1173 return ERR_PTR(-ENOMEM);
1174 opendata->state = state;
1175 atomic_inc(&state->count);
1179 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1181 struct nfs4_state *newstate;
1184 opendata->o_arg.open_flags = 0;
1185 opendata->o_arg.fmode = fmode;
1186 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1187 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1188 nfs4_init_opendata_res(opendata);
1189 ret = _nfs4_recover_proc_open(opendata);
1192 newstate = nfs4_opendata_to_nfs4_state(opendata);
1193 if (IS_ERR(newstate))
1194 return PTR_ERR(newstate);
1195 nfs4_close_state(newstate, fmode);
1200 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1202 struct nfs4_state *newstate;
1205 /* memory barrier prior to reading state->n_* */
1206 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1208 if (state->n_rdwr != 0) {
1209 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1210 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1213 if (newstate != state)
1216 if (state->n_wronly != 0) {
1217 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1218 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1221 if (newstate != state)
1224 if (state->n_rdonly != 0) {
1225 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1226 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1229 if (newstate != state)
1233 * We may have performed cached opens for all three recoveries.
1234 * Check if we need to update the current stateid.
1236 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1237 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1238 write_seqlock(&state->seqlock);
1239 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1240 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1241 write_sequnlock(&state->seqlock);
1248 * reclaim state on the server after a reboot.
1250 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1252 struct nfs_delegation *delegation;
1253 struct nfs4_opendata *opendata;
1254 fmode_t delegation_type = 0;
1257 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1258 if (IS_ERR(opendata))
1259 return PTR_ERR(opendata);
1260 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1261 opendata->o_arg.fh = NFS_FH(state->inode);
1263 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1264 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1265 delegation_type = delegation->type;
1267 opendata->o_arg.u.delegation_type = delegation_type;
1268 status = nfs4_open_recover(opendata, state);
1269 nfs4_opendata_put(opendata);
1273 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1275 struct nfs_server *server = NFS_SERVER(state->inode);
1276 struct nfs4_exception exception = { };
1279 err = _nfs4_do_open_reclaim(ctx, state);
1280 if (err != -NFS4ERR_DELAY)
1282 nfs4_handle_exception(server, err, &exception);
1283 } while (exception.retry);
1287 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1289 struct nfs_open_context *ctx;
1292 ctx = nfs4_state_find_open_context(state);
1294 return PTR_ERR(ctx);
1295 ret = nfs4_do_open_reclaim(ctx, state);
1296 put_nfs_open_context(ctx);
1300 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1302 struct nfs4_opendata *opendata;
1305 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1306 if (IS_ERR(opendata))
1307 return PTR_ERR(opendata);
1308 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1309 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1310 ret = nfs4_open_recover(opendata, state);
1311 nfs4_opendata_put(opendata);
1315 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1317 struct nfs4_exception exception = { };
1318 struct nfs_server *server = NFS_SERVER(state->inode);
1321 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1327 case -NFS4ERR_BADSESSION:
1328 case -NFS4ERR_BADSLOT:
1329 case -NFS4ERR_BAD_HIGH_SLOT:
1330 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1331 case -NFS4ERR_DEADSESSION:
1332 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
1334 case -NFS4ERR_STALE_CLIENTID:
1335 case -NFS4ERR_STALE_STATEID:
1336 case -NFS4ERR_EXPIRED:
1337 /* Don't recall a delegation if it was lost */
1338 nfs4_schedule_lease_recovery(server->nfs_client);
1342 * The show must go on: exit, but mark the
1343 * stateid as needing recovery.
1345 case -NFS4ERR_DELEG_REVOKED:
1346 case -NFS4ERR_ADMIN_REVOKED:
1347 case -NFS4ERR_BAD_STATEID:
1348 nfs_inode_find_state_and_recover(state->inode,
1350 nfs4_schedule_stateid_recovery(server, state);
1353 * User RPCSEC_GSS context has expired.
1354 * We cannot recover this stateid now, so
1355 * skip it and allow recovery thread to
1362 err = nfs4_handle_exception(server, err, &exception);
1363 } while (exception.retry);
1368 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1370 struct nfs4_opendata *data = calldata;
1372 data->rpc_status = task->tk_status;
1373 if (data->rpc_status == 0) {
1374 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1375 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1376 renew_lease(data->o_res.server, data->timestamp);
1381 static void nfs4_open_confirm_release(void *calldata)
1383 struct nfs4_opendata *data = calldata;
1384 struct nfs4_state *state = NULL;
1386 /* If this request hasn't been cancelled, do nothing */
1387 if (data->cancelled == 0)
1389 /* In case of error, no cleanup! */
1390 if (!data->rpc_done)
1392 state = nfs4_opendata_to_nfs4_state(data);
1394 nfs4_close_state(state, data->o_arg.fmode);
1396 nfs4_opendata_put(data);
1399 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1400 .rpc_call_done = nfs4_open_confirm_done,
1401 .rpc_release = nfs4_open_confirm_release,
1405 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1407 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1409 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1410 struct rpc_task *task;
1411 struct rpc_message msg = {
1412 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1413 .rpc_argp = &data->c_arg,
1414 .rpc_resp = &data->c_res,
1415 .rpc_cred = data->owner->so_cred,
1417 struct rpc_task_setup task_setup_data = {
1418 .rpc_client = server->client,
1419 .rpc_message = &msg,
1420 .callback_ops = &nfs4_open_confirm_ops,
1421 .callback_data = data,
1422 .workqueue = nfsiod_workqueue,
1423 .flags = RPC_TASK_ASYNC,
1427 kref_get(&data->kref);
1429 data->rpc_status = 0;
1430 data->timestamp = jiffies;
1431 task = rpc_run_task(&task_setup_data);
1433 return PTR_ERR(task);
1434 status = nfs4_wait_for_completion_rpc_task(task);
1436 data->cancelled = 1;
1439 status = data->rpc_status;
1444 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1446 struct nfs4_opendata *data = calldata;
1447 struct nfs4_state_owner *sp = data->owner;
1449 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1452 * Check if we still need to send an OPEN call, or if we can use
1453 * a delegation instead.
1455 if (data->state != NULL) {
1456 struct nfs_delegation *delegation;
1458 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1461 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1462 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1463 can_open_delegated(delegation, data->o_arg.fmode))
1464 goto unlock_no_action;
1467 /* Update client id. */
1468 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1469 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1470 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1471 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1473 data->timestamp = jiffies;
1474 if (nfs4_setup_sequence(data->o_arg.server,
1475 &data->o_arg.seq_args,
1476 &data->o_res.seq_res, task))
1478 rpc_call_start(task);
1483 task->tk_action = NULL;
1487 static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1489 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1490 nfs4_open_prepare(task, calldata);
1493 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1495 struct nfs4_opendata *data = calldata;
1497 data->rpc_status = task->tk_status;
1499 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1502 if (task->tk_status == 0) {
1503 switch (data->o_res.f_attr->mode & S_IFMT) {
1507 data->rpc_status = -ELOOP;
1510 data->rpc_status = -EISDIR;
1513 data->rpc_status = -ENOTDIR;
1515 renew_lease(data->o_res.server, data->timestamp);
1516 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1517 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1522 static void nfs4_open_release(void *calldata)
1524 struct nfs4_opendata *data = calldata;
1525 struct nfs4_state *state = NULL;
1527 /* If this request hasn't been cancelled, do nothing */
1528 if (data->cancelled == 0)
1530 /* In case of error, no cleanup! */
1531 if (data->rpc_status != 0 || !data->rpc_done)
1533 /* In case we need an open_confirm, no cleanup! */
1534 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1536 state = nfs4_opendata_to_nfs4_state(data);
1538 nfs4_close_state(state, data->o_arg.fmode);
1540 nfs4_opendata_put(data);
1543 static const struct rpc_call_ops nfs4_open_ops = {
1544 .rpc_call_prepare = nfs4_open_prepare,
1545 .rpc_call_done = nfs4_open_done,
1546 .rpc_release = nfs4_open_release,
1549 static const struct rpc_call_ops nfs4_recover_open_ops = {
1550 .rpc_call_prepare = nfs4_recover_open_prepare,
1551 .rpc_call_done = nfs4_open_done,
1552 .rpc_release = nfs4_open_release,
1555 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1557 struct inode *dir = data->dir->d_inode;
1558 struct nfs_server *server = NFS_SERVER(dir);
1559 struct nfs_openargs *o_arg = &data->o_arg;
1560 struct nfs_openres *o_res = &data->o_res;
1561 struct rpc_task *task;
1562 struct rpc_message msg = {
1563 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1566 .rpc_cred = data->owner->so_cred,
1568 struct rpc_task_setup task_setup_data = {
1569 .rpc_client = server->client,
1570 .rpc_message = &msg,
1571 .callback_ops = &nfs4_open_ops,
1572 .callback_data = data,
1573 .workqueue = nfsiod_workqueue,
1574 .flags = RPC_TASK_ASYNC,
1578 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1579 kref_get(&data->kref);
1581 data->rpc_status = 0;
1582 data->cancelled = 0;
1584 task_setup_data.callback_ops = &nfs4_recover_open_ops;
1585 task = rpc_run_task(&task_setup_data);
1587 return PTR_ERR(task);
1588 status = nfs4_wait_for_completion_rpc_task(task);
1590 data->cancelled = 1;
1593 status = data->rpc_status;
1599 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1601 struct inode *dir = data->dir->d_inode;
1602 struct nfs_openres *o_res = &data->o_res;
1605 status = nfs4_run_open_task(data, 1);
1606 if (status != 0 || !data->rpc_done)
1609 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1611 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1612 status = _nfs4_proc_open_confirm(data);
1621 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1623 static int _nfs4_proc_open(struct nfs4_opendata *data)
1625 struct inode *dir = data->dir->d_inode;
1626 struct nfs_server *server = NFS_SERVER(dir);
1627 struct nfs_openargs *o_arg = &data->o_arg;
1628 struct nfs_openres *o_res = &data->o_res;
1631 status = nfs4_run_open_task(data, 0);
1632 if (!data->rpc_done)
1635 if (status == -NFS4ERR_BADNAME &&
1636 !(o_arg->open_flags & O_CREAT))
1641 nfs_fattr_map_and_free_names(server, &data->f_attr);
1643 if (o_arg->open_flags & O_CREAT)
1644 update_changeattr(dir, &o_res->cinfo);
1645 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1646 server->caps &= ~NFS_CAP_POSIX_LOCK;
1647 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1648 status = _nfs4_proc_open_confirm(data);
1652 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1653 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1657 static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1662 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1663 ret = nfs4_wait_clnt_recover(clp);
1666 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1667 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1669 nfs4_schedule_state_manager(clp);
1675 static int nfs4_recover_expired_lease(struct nfs_server *server)
1677 return nfs4_client_recover_expired_lease(server->nfs_client);
1682 * reclaim state on the server after a network partition.
1683 * Assumes caller holds the appropriate lock
1685 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1687 struct nfs4_opendata *opendata;
1690 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1691 if (IS_ERR(opendata))
1692 return PTR_ERR(opendata);
1693 ret = nfs4_open_recover(opendata, state);
1695 d_drop(ctx->dentry);
1696 nfs4_opendata_put(opendata);
1700 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1702 struct nfs_server *server = NFS_SERVER(state->inode);
1703 struct nfs4_exception exception = { };
1707 err = _nfs4_open_expired(ctx, state);
1711 case -NFS4ERR_GRACE:
1712 case -NFS4ERR_DELAY:
1713 nfs4_handle_exception(server, err, &exception);
1716 } while (exception.retry);
1721 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1723 struct nfs_open_context *ctx;
1726 ctx = nfs4_state_find_open_context(state);
1728 return PTR_ERR(ctx);
1729 ret = nfs4_do_open_expired(ctx, state);
1730 put_nfs_open_context(ctx);
1734 #if defined(CONFIG_NFS_V4_1)
1735 static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
1737 int status = NFS_OK;
1738 struct nfs_server *server = NFS_SERVER(state->inode);
1740 if (state->flags & flags) {
1741 status = nfs41_test_stateid(server, stateid);
1742 if (status != NFS_OK) {
1743 nfs41_free_stateid(server, stateid);
1744 state->flags &= ~flags;
1750 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1752 int deleg_status, open_status;
1753 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1754 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1756 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1757 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1759 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1761 return nfs4_open_expired(sp, state);
1766 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1767 * fields corresponding to attributes that were used to store the verifier.
1768 * Make sure we clobber those fields in the later setattr call
1770 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1772 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1773 !(sattr->ia_valid & ATTR_ATIME_SET))
1774 sattr->ia_valid |= ATTR_ATIME;
1776 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1777 !(sattr->ia_valid & ATTR_MTIME_SET))
1778 sattr->ia_valid |= ATTR_MTIME;
1782 * Returns a referenced nfs4_state
1784 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)
1786 struct nfs4_state_owner *sp;
1787 struct nfs4_state *state = NULL;
1788 struct nfs_server *server = NFS_SERVER(dir);
1789 struct nfs4_opendata *opendata;
1792 /* Protect against reboot recovery conflicts */
1794 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1796 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1799 status = nfs4_recover_expired_lease(server);
1801 goto err_put_state_owner;
1802 if (dentry->d_inode != NULL)
1803 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1805 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1806 if (opendata == NULL)
1807 goto err_put_state_owner;
1809 if (dentry->d_inode != NULL)
1810 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1812 status = _nfs4_proc_open(opendata);
1814 goto err_opendata_put;
1816 state = nfs4_opendata_to_nfs4_state(opendata);
1817 status = PTR_ERR(state);
1819 goto err_opendata_put;
1820 if (server->caps & NFS_CAP_POSIX_LOCK)
1821 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1823 if (opendata->o_arg.open_flags & O_EXCL) {
1824 nfs4_exclusive_attrset(opendata, sattr);
1826 nfs_fattr_init(opendata->o_res.f_attr);
1827 status = nfs4_do_setattr(state->inode, cred,
1828 opendata->o_res.f_attr, sattr,
1831 nfs_setattr_update_inode(state->inode, sattr);
1832 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1834 nfs4_opendata_put(opendata);
1835 nfs4_put_state_owner(sp);
1839 nfs4_opendata_put(opendata);
1840 err_put_state_owner:
1841 nfs4_put_state_owner(sp);
1848 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)
1850 struct nfs4_exception exception = { };
1851 struct nfs4_state *res;
1855 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
1858 /* NOTE: BAD_SEQID means the server and client disagree about the
1859 * book-keeping w.r.t. state-changing operations
1860 * (OPEN/CLOSE/LOCK/LOCKU...)
1861 * It is actually a sign of a bug on the client or on the server.
1863 * If we receive a BAD_SEQID error in the particular case of
1864 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1865 * have unhashed the old state_owner for us, and that we can
1866 * therefore safely retry using a new one. We should still warn
1867 * the user though...
1869 if (status == -NFS4ERR_BAD_SEQID) {
1870 pr_warn_ratelimited("NFS: v4 server %s "
1871 " returned a bad sequence-id error!\n",
1872 NFS_SERVER(dir)->nfs_client->cl_hostname);
1873 exception.retry = 1;
1877 * BAD_STATEID on OPEN means that the server cancelled our
1878 * state before it received the OPEN_CONFIRM.
1879 * Recover by retrying the request as per the discussion
1880 * on Page 181 of RFC3530.
1882 if (status == -NFS4ERR_BAD_STATEID) {
1883 exception.retry = 1;
1886 if (status == -EAGAIN) {
1887 /* We must have found a delegation */
1888 exception.retry = 1;
1891 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1892 status, &exception));
1893 } while (exception.retry);
1897 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1898 struct nfs_fattr *fattr, struct iattr *sattr,
1899 struct nfs4_state *state)
1901 struct nfs_server *server = NFS_SERVER(inode);
1902 struct nfs_setattrargs arg = {
1903 .fh = NFS_FH(inode),
1906 .bitmask = server->attr_bitmask,
1908 struct nfs_setattrres res = {
1912 struct rpc_message msg = {
1913 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1918 unsigned long timestamp = jiffies;
1921 nfs_fattr_init(fattr);
1923 if (state != NULL) {
1924 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1925 current->files, current->tgid);
1926 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1928 /* Use that stateid */
1930 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
1932 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
1933 if (status == 0 && state != NULL)
1934 renew_lease(server, timestamp);
1938 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1939 struct nfs_fattr *fattr, struct iattr *sattr,
1940 struct nfs4_state *state)
1942 struct nfs_server *server = NFS_SERVER(inode);
1943 struct nfs4_exception exception = {
1949 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
1951 case -NFS4ERR_OPENMODE:
1952 if (state && !(state->state & FMODE_WRITE)) {
1954 if (sattr->ia_valid & ATTR_OPEN)
1959 err = nfs4_handle_exception(server, err, &exception);
1960 } 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);
2302 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2303 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2305 struct rpc_auth *auth;
2308 auth = rpcauth_create(flavor, server->client);
2313 ret = nfs4_lookup_root(server, fhandle, info);
2318 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2319 struct nfs_fsinfo *info)
2321 int i, len, status = 0;
2322 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2324 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2325 flav_array[len] = RPC_AUTH_NULL;
2328 for (i = 0; i < len; i++) {
2329 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2330 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2335 * -EACCESS could mean that the user doesn't have correct permissions
2336 * to access the mount. It could also mean that we tried to mount
2337 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2338 * existing mount programs don't handle -EACCES very well so it should
2339 * be mapped to -EPERM instead.
2341 if (status == -EACCES)
2347 * get the file handle for the "/" directory on the server
2349 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2350 struct nfs_fsinfo *info)
2352 int minor_version = server->nfs_client->cl_minorversion;
2353 int status = nfs4_lookup_root(server, fhandle, info);
2354 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2356 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2357 * by nfs4_map_errors() as this function exits.
2359 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2361 status = nfs4_server_capabilities(server, fhandle);
2363 status = nfs4_do_fsinfo(server, fhandle, info);
2364 return nfs4_map_errors(status);
2367 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2368 struct nfs_fsinfo *info)
2371 struct nfs_fattr *fattr = info->fattr;
2373 error = nfs4_server_capabilities(server, mntfh);
2375 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2379 error = nfs4_proc_getattr(server, mntfh, fattr);
2381 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2385 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2386 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2387 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2393 * Get locations and (maybe) other attributes of a referral.
2394 * Note that we'll actually follow the referral later when
2395 * we detect fsid mismatch in inode revalidation
2397 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2398 const struct qstr *name, struct nfs_fattr *fattr,
2399 struct nfs_fh *fhandle)
2401 int status = -ENOMEM;
2402 struct page *page = NULL;
2403 struct nfs4_fs_locations *locations = NULL;
2405 page = alloc_page(GFP_KERNEL);
2408 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2409 if (locations == NULL)
2412 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2415 /* Make sure server returned a different fsid for the referral */
2416 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2417 dprintk("%s: server did not return a different fsid for"
2418 " a referral at %s\n", __func__, name->name);
2422 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2423 nfs_fixup_referral_attributes(&locations->fattr);
2425 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2426 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2427 memset(fhandle, 0, sizeof(struct nfs_fh));
2435 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2437 struct nfs4_getattr_arg args = {
2439 .bitmask = server->attr_bitmask,
2441 struct nfs4_getattr_res res = {
2445 struct rpc_message msg = {
2446 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2451 nfs_fattr_init(fattr);
2452 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2455 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2457 struct nfs4_exception exception = { };
2460 err = nfs4_handle_exception(server,
2461 _nfs4_proc_getattr(server, fhandle, fattr),
2463 } while (exception.retry);
2468 * The file is not closed if it is opened due to the a request to change
2469 * the size of the file. The open call will not be needed once the
2470 * VFS layer lookup-intents are implemented.
2472 * Close is called when the inode is destroyed.
2473 * If we haven't opened the file for O_WRONLY, we
2474 * need to in the size_change case to obtain a stateid.
2477 * Because OPEN is always done by name in nfsv4, it is
2478 * possible that we opened a different file by the same
2479 * name. We can recognize this race condition, but we
2480 * can't do anything about it besides returning an error.
2482 * This will be fixed with VFS changes (lookup-intent).
2485 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2486 struct iattr *sattr)
2488 struct inode *inode = dentry->d_inode;
2489 struct rpc_cred *cred = NULL;
2490 struct nfs4_state *state = NULL;
2493 if (pnfs_ld_layoutret_on_setattr(inode))
2494 pnfs_return_layout(inode);
2496 nfs_fattr_init(fattr);
2498 /* Search for an existing open(O_WRITE) file */
2499 if (sattr->ia_valid & ATTR_FILE) {
2500 struct nfs_open_context *ctx;
2502 ctx = nfs_file_open_context(sattr->ia_file);
2509 /* Deal with open(O_TRUNC) */
2510 if (sattr->ia_valid & ATTR_OPEN)
2511 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2513 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2515 nfs_setattr_update_inode(inode, sattr);
2519 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2520 const struct qstr *name, struct nfs_fh *fhandle,
2521 struct nfs_fattr *fattr)
2523 struct nfs_server *server = NFS_SERVER(dir);
2525 struct nfs4_lookup_arg args = {
2526 .bitmask = server->attr_bitmask,
2527 .dir_fh = NFS_FH(dir),
2530 struct nfs4_lookup_res res = {
2535 struct rpc_message msg = {
2536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2541 nfs_fattr_init(fattr);
2543 dprintk("NFS call lookup %s\n", name->name);
2544 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2545 dprintk("NFS reply lookup: %d\n", status);
2549 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2551 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2552 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2553 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2557 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2558 struct qstr *name, struct nfs_fh *fhandle,
2559 struct nfs_fattr *fattr)
2561 struct nfs4_exception exception = { };
2562 struct rpc_clnt *client = *clnt;
2565 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2567 case -NFS4ERR_BADNAME:
2570 case -NFS4ERR_MOVED:
2571 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
2573 case -NFS4ERR_WRONGSEC:
2575 if (client != *clnt)
2578 client = nfs4_create_sec_client(client, dir, name);
2580 return PTR_ERR(client);
2582 exception.retry = 1;
2585 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2587 } while (exception.retry);
2592 else if (client != *clnt)
2593 rpc_shutdown_client(client);
2598 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
2599 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2602 struct rpc_clnt *client = NFS_CLIENT(dir);
2604 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2605 if (client != NFS_CLIENT(dir)) {
2606 rpc_shutdown_client(client);
2607 nfs_fixup_secinfo_attributes(fattr);
2613 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2614 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2617 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2619 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2621 rpc_shutdown_client(client);
2622 return ERR_PTR(status);
2627 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2629 struct nfs_server *server = NFS_SERVER(inode);
2630 struct nfs4_accessargs args = {
2631 .fh = NFS_FH(inode),
2632 .bitmask = server->cache_consistency_bitmask,
2634 struct nfs4_accessres res = {
2637 struct rpc_message msg = {
2638 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2641 .rpc_cred = entry->cred,
2643 int mode = entry->mask;
2647 * Determine which access bits we want to ask for...
2649 if (mode & MAY_READ)
2650 args.access |= NFS4_ACCESS_READ;
2651 if (S_ISDIR(inode->i_mode)) {
2652 if (mode & MAY_WRITE)
2653 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2654 if (mode & MAY_EXEC)
2655 args.access |= NFS4_ACCESS_LOOKUP;
2657 if (mode & MAY_WRITE)
2658 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2659 if (mode & MAY_EXEC)
2660 args.access |= NFS4_ACCESS_EXECUTE;
2663 res.fattr = nfs_alloc_fattr();
2664 if (res.fattr == NULL)
2667 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2670 if (res.access & NFS4_ACCESS_READ)
2671 entry->mask |= MAY_READ;
2672 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2673 entry->mask |= MAY_WRITE;
2674 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2675 entry->mask |= MAY_EXEC;
2676 nfs_refresh_inode(inode, res.fattr);
2678 nfs_free_fattr(res.fattr);
2682 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2684 struct nfs4_exception exception = { };
2687 err = nfs4_handle_exception(NFS_SERVER(inode),
2688 _nfs4_proc_access(inode, entry),
2690 } while (exception.retry);
2695 * TODO: For the time being, we don't try to get any attributes
2696 * along with any of the zero-copy operations READ, READDIR,
2699 * In the case of the first three, we want to put the GETATTR
2700 * after the read-type operation -- this is because it is hard
2701 * to predict the length of a GETATTR response in v4, and thus
2702 * align the READ data correctly. This means that the GETATTR
2703 * may end up partially falling into the page cache, and we should
2704 * shift it into the 'tail' of the xdr_buf before processing.
2705 * To do this efficiently, we need to know the total length
2706 * of data received, which doesn't seem to be available outside
2709 * In the case of WRITE, we also want to put the GETATTR after
2710 * the operation -- in this case because we want to make sure
2711 * we get the post-operation mtime and size. This means that
2712 * we can't use xdr_encode_pages() as written: we need a variant
2713 * of it which would leave room in the 'tail' iovec.
2715 * Both of these changes to the XDR layer would in fact be quite
2716 * minor, but I decided to leave them for a subsequent patch.
2718 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2719 unsigned int pgbase, unsigned int pglen)
2721 struct nfs4_readlink args = {
2722 .fh = NFS_FH(inode),
2727 struct nfs4_readlink_res res;
2728 struct rpc_message msg = {
2729 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2734 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2737 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2738 unsigned int pgbase, unsigned int pglen)
2740 struct nfs4_exception exception = { };
2743 err = nfs4_handle_exception(NFS_SERVER(inode),
2744 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2746 } while (exception.retry);
2752 * We will need to arrange for the VFS layer to provide an atomic open.
2753 * Until then, this create/open method is prone to inefficiency and race
2754 * conditions due to the lookup, create, and open VFS calls from sys_open()
2755 * placed on the wire.
2757 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2758 * The file will be opened again in the subsequent VFS open call
2759 * (nfs4_proc_file_open).
2761 * The open for read will just hang around to be used by any process that
2762 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2766 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2767 int flags, struct nfs_open_context *ctx)
2769 struct dentry *de = dentry;
2770 struct nfs4_state *state;
2771 struct rpc_cred *cred = NULL;
2780 sattr->ia_mode &= ~current_umask();
2781 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
2783 if (IS_ERR(state)) {
2784 status = PTR_ERR(state);
2787 d_add(dentry, igrab(state->inode));
2788 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2792 nfs4_close_sync(state, fmode);
2797 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2799 struct nfs_server *server = NFS_SERVER(dir);
2800 struct nfs_removeargs args = {
2802 .name.len = name->len,
2803 .name.name = name->name,
2805 struct nfs_removeres res = {
2808 struct rpc_message msg = {
2809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2815 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2817 update_changeattr(dir, &res.cinfo);
2821 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2823 struct nfs4_exception exception = { };
2826 err = nfs4_handle_exception(NFS_SERVER(dir),
2827 _nfs4_proc_remove(dir, name),
2829 } while (exception.retry);
2833 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2835 struct nfs_server *server = NFS_SERVER(dir);
2836 struct nfs_removeargs *args = msg->rpc_argp;
2837 struct nfs_removeres *res = msg->rpc_resp;
2839 res->server = server;
2840 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2841 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
2844 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2846 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2847 &data->args.seq_args,
2851 rpc_call_start(task);
2854 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2856 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2858 if (!nfs4_sequence_done(task, &res->seq_res))
2860 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2862 update_changeattr(dir, &res->cinfo);
2866 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2868 struct nfs_server *server = NFS_SERVER(dir);
2869 struct nfs_renameargs *arg = msg->rpc_argp;
2870 struct nfs_renameres *res = msg->rpc_resp;
2872 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2873 res->server = server;
2874 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
2877 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2879 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2880 &data->args.seq_args,
2884 rpc_call_start(task);
2887 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2888 struct inode *new_dir)
2890 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2892 if (!nfs4_sequence_done(task, &res->seq_res))
2894 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2897 update_changeattr(old_dir, &res->old_cinfo);
2898 update_changeattr(new_dir, &res->new_cinfo);
2902 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2903 struct inode *new_dir, struct qstr *new_name)
2905 struct nfs_server *server = NFS_SERVER(old_dir);
2906 struct nfs_renameargs arg = {
2907 .old_dir = NFS_FH(old_dir),
2908 .new_dir = NFS_FH(new_dir),
2909 .old_name = old_name,
2910 .new_name = new_name,
2912 struct nfs_renameres res = {
2915 struct rpc_message msg = {
2916 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2920 int status = -ENOMEM;
2922 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2924 update_changeattr(old_dir, &res.old_cinfo);
2925 update_changeattr(new_dir, &res.new_cinfo);
2930 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2931 struct inode *new_dir, struct qstr *new_name)
2933 struct nfs4_exception exception = { };
2936 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2937 _nfs4_proc_rename(old_dir, old_name,
2940 } while (exception.retry);
2944 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2946 struct nfs_server *server = NFS_SERVER(inode);
2947 struct nfs4_link_arg arg = {
2948 .fh = NFS_FH(inode),
2949 .dir_fh = NFS_FH(dir),
2951 .bitmask = server->attr_bitmask,
2953 struct nfs4_link_res res = {
2956 struct rpc_message msg = {
2957 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2961 int status = -ENOMEM;
2963 res.fattr = nfs_alloc_fattr();
2964 if (res.fattr == NULL)
2967 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2969 update_changeattr(dir, &res.cinfo);
2970 nfs_post_op_update_inode(inode, res.fattr);
2973 nfs_free_fattr(res.fattr);
2977 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2979 struct nfs4_exception exception = { };
2982 err = nfs4_handle_exception(NFS_SERVER(inode),
2983 _nfs4_proc_link(inode, dir, name),
2985 } while (exception.retry);
2989 struct nfs4_createdata {
2990 struct rpc_message msg;
2991 struct nfs4_create_arg arg;
2992 struct nfs4_create_res res;
2994 struct nfs_fattr fattr;
2997 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2998 struct qstr *name, struct iattr *sattr, u32 ftype)
3000 struct nfs4_createdata *data;
3002 data = kzalloc(sizeof(*data), GFP_KERNEL);
3004 struct nfs_server *server = NFS_SERVER(dir);
3006 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3007 data->msg.rpc_argp = &data->arg;
3008 data->msg.rpc_resp = &data->res;
3009 data->arg.dir_fh = NFS_FH(dir);
3010 data->arg.server = server;
3011 data->arg.name = name;
3012 data->arg.attrs = sattr;
3013 data->arg.ftype = ftype;
3014 data->arg.bitmask = server->attr_bitmask;
3015 data->res.server = server;
3016 data->res.fh = &data->fh;
3017 data->res.fattr = &data->fattr;
3018 nfs_fattr_init(data->res.fattr);
3023 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3025 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3026 &data->arg.seq_args, &data->res.seq_res, 1);
3028 update_changeattr(dir, &data->res.dir_cinfo);
3029 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3034 static void nfs4_free_createdata(struct nfs4_createdata *data)
3039 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3040 struct page *page, unsigned int len, struct iattr *sattr)
3042 struct nfs4_createdata *data;
3043 int status = -ENAMETOOLONG;
3045 if (len > NFS4_MAXPATHLEN)
3049 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3053 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3054 data->arg.u.symlink.pages = &page;
3055 data->arg.u.symlink.len = len;
3057 status = nfs4_do_create(dir, dentry, data);
3059 nfs4_free_createdata(data);
3064 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3065 struct page *page, unsigned int len, struct iattr *sattr)
3067 struct nfs4_exception exception = { };
3070 err = nfs4_handle_exception(NFS_SERVER(dir),
3071 _nfs4_proc_symlink(dir, dentry, page,
3074 } while (exception.retry);
3078 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3079 struct iattr *sattr)
3081 struct nfs4_createdata *data;
3082 int status = -ENOMEM;
3084 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3088 status = nfs4_do_create(dir, dentry, data);
3090 nfs4_free_createdata(data);
3095 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3096 struct iattr *sattr)
3098 struct nfs4_exception exception = { };
3101 sattr->ia_mode &= ~current_umask();
3103 err = nfs4_handle_exception(NFS_SERVER(dir),
3104 _nfs4_proc_mkdir(dir, dentry, sattr),
3106 } while (exception.retry);
3110 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3111 u64 cookie, struct page **pages, unsigned int count, int plus)
3113 struct inode *dir = dentry->d_inode;
3114 struct nfs4_readdir_arg args = {
3119 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3122 struct nfs4_readdir_res res;
3123 struct rpc_message msg = {
3124 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3131 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3132 dentry->d_parent->d_name.name,
3133 dentry->d_name.name,
3134 (unsigned long long)cookie);
3135 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3136 res.pgbase = args.pgbase;
3137 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3139 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
3140 status += args.pgbase;
3143 nfs_invalidate_atime(dir);
3145 dprintk("%s: returns %d\n", __func__, status);
3149 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3150 u64 cookie, struct page **pages, unsigned int count, int plus)
3152 struct nfs4_exception exception = { };
3155 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3156 _nfs4_proc_readdir(dentry, cred, cookie,
3157 pages, count, plus),
3159 } while (exception.retry);
3163 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3164 struct iattr *sattr, dev_t rdev)
3166 struct nfs4_createdata *data;
3167 int mode = sattr->ia_mode;
3168 int status = -ENOMEM;
3170 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3171 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
3173 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3178 data->arg.ftype = NF4FIFO;
3179 else if (S_ISBLK(mode)) {
3180 data->arg.ftype = NF4BLK;
3181 data->arg.u.device.specdata1 = MAJOR(rdev);
3182 data->arg.u.device.specdata2 = MINOR(rdev);
3184 else if (S_ISCHR(mode)) {
3185 data->arg.ftype = NF4CHR;
3186 data->arg.u.device.specdata1 = MAJOR(rdev);
3187 data->arg.u.device.specdata2 = MINOR(rdev);
3190 status = nfs4_do_create(dir, dentry, data);
3192 nfs4_free_createdata(data);
3197 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3198 struct iattr *sattr, dev_t rdev)
3200 struct nfs4_exception exception = { };
3203 sattr->ia_mode &= ~current_umask();
3205 err = nfs4_handle_exception(NFS_SERVER(dir),
3206 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3208 } while (exception.retry);
3212 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3213 struct nfs_fsstat *fsstat)
3215 struct nfs4_statfs_arg args = {
3217 .bitmask = server->attr_bitmask,
3219 struct nfs4_statfs_res res = {
3222 struct rpc_message msg = {
3223 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3228 nfs_fattr_init(fsstat->fattr);
3229 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3232 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3234 struct nfs4_exception exception = { };
3237 err = nfs4_handle_exception(server,
3238 _nfs4_proc_statfs(server, fhandle, fsstat),
3240 } while (exception.retry);
3244 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3245 struct nfs_fsinfo *fsinfo)
3247 struct nfs4_fsinfo_arg args = {
3249 .bitmask = server->attr_bitmask,
3251 struct nfs4_fsinfo_res res = {
3254 struct rpc_message msg = {
3255 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3260 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3263 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3265 struct nfs4_exception exception = { };
3269 err = nfs4_handle_exception(server,
3270 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3272 } while (exception.retry);
3276 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3278 nfs_fattr_init(fsinfo->fattr);
3279 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3282 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3283 struct nfs_pathconf *pathconf)
3285 struct nfs4_pathconf_arg args = {
3287 .bitmask = server->attr_bitmask,
3289 struct nfs4_pathconf_res res = {
3290 .pathconf = pathconf,
3292 struct rpc_message msg = {
3293 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3298 /* None of the pathconf attributes are mandatory to implement */
3299 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3300 memset(pathconf, 0, sizeof(*pathconf));
3304 nfs_fattr_init(pathconf->fattr);
3305 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3308 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3309 struct nfs_pathconf *pathconf)
3311 struct nfs4_exception exception = { };
3315 err = nfs4_handle_exception(server,
3316 _nfs4_proc_pathconf(server, fhandle, pathconf),
3318 } while (exception.retry);
3322 void __nfs4_read_done_cb(struct nfs_read_data *data)
3324 nfs_invalidate_atime(data->header->inode);
3327 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3329 struct nfs_server *server = NFS_SERVER(data->header->inode);
3331 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3332 rpc_restart_call_prepare(task);
3336 __nfs4_read_done_cb(data);
3337 if (task->tk_status > 0)
3338 renew_lease(server, data->timestamp);
3342 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3345 dprintk("--> %s\n", __func__);
3347 if (!nfs4_sequence_done(task, &data->res.seq_res))
3350 return data->read_done_cb ? data->read_done_cb(task, data) :
3351 nfs4_read_done_cb(task, data);
3354 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3356 data->timestamp = jiffies;
3357 data->read_done_cb = nfs4_read_done_cb;
3358 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3359 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3362 static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3364 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3365 &data->args.seq_args,
3369 rpc_call_start(task);
3372 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3374 struct inode *inode = data->header->inode;
3376 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3377 rpc_restart_call_prepare(task);
3380 if (task->tk_status >= 0) {
3381 renew_lease(NFS_SERVER(inode), data->timestamp);
3382 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
3387 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3389 if (!nfs4_sequence_done(task, &data->res.seq_res))
3391 return data->write_done_cb ? data->write_done_cb(task, data) :
3392 nfs4_write_done_cb(task, data);
3396 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3398 const struct nfs_pgio_header *hdr = data->header;
3400 /* Don't request attributes for pNFS or O_DIRECT writes */
3401 if (data->ds_clp != NULL || hdr->dreq != NULL)
3403 /* Otherwise, request attributes if and only if we don't hold
3406 return nfs_have_delegation(hdr->inode, FMODE_READ) == 0;
3409 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3411 struct nfs_server *server = NFS_SERVER(data->header->inode);
3413 if (!nfs4_write_need_cache_consistency_data(data)) {
3414 data->args.bitmask = NULL;
3415 data->res.fattr = NULL;
3417 data->args.bitmask = server->cache_consistency_bitmask;
3419 if (!data->write_done_cb)
3420 data->write_done_cb = nfs4_write_done_cb;
3421 data->res.server = server;
3422 data->timestamp = jiffies;
3424 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3425 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3428 static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3430 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3431 &data->args.seq_args,
3435 rpc_call_start(task);
3438 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3440 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3441 &data->args.seq_args,
3445 rpc_call_start(task);
3448 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
3450 struct inode *inode = data->inode;
3452 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3453 rpc_restart_call_prepare(task);
3459 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
3461 if (!nfs4_sequence_done(task, &data->res.seq_res))
3463 return data->commit_done_cb(task, data);
3466 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
3468 struct nfs_server *server = NFS_SERVER(data->inode);
3470 if (data->commit_done_cb == NULL)
3471 data->commit_done_cb = nfs4_commit_done_cb;
3472 data->res.server = server;
3473 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3474 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3477 struct nfs4_renewdata {
3478 struct nfs_client *client;
3479 unsigned long timestamp;
3483 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3484 * standalone procedure for queueing an asynchronous RENEW.
3486 static void nfs4_renew_release(void *calldata)
3488 struct nfs4_renewdata *data = calldata;
3489 struct nfs_client *clp = data->client;
3491 if (atomic_read(&clp->cl_count) > 1)
3492 nfs4_schedule_state_renewal(clp);
3493 nfs_put_client(clp);
3497 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3499 struct nfs4_renewdata *data = calldata;
3500 struct nfs_client *clp = data->client;
3501 unsigned long timestamp = data->timestamp;
3503 if (task->tk_status < 0) {
3504 /* Unless we're shutting down, schedule state recovery! */
3505 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3507 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3508 nfs4_schedule_lease_recovery(clp);
3511 nfs4_schedule_path_down_recovery(clp);
3513 do_renew_lease(clp, timestamp);
3516 static const struct rpc_call_ops nfs4_renew_ops = {
3517 .rpc_call_done = nfs4_renew_done,
3518 .rpc_release = nfs4_renew_release,
3521 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3523 struct rpc_message msg = {
3524 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3528 struct nfs4_renewdata *data;
3530 if (renew_flags == 0)
3532 if (!atomic_inc_not_zero(&clp->cl_count))
3534 data = kmalloc(sizeof(*data), GFP_NOFS);
3538 data->timestamp = jiffies;
3539 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3540 &nfs4_renew_ops, data);
3543 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3545 struct rpc_message msg = {
3546 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3550 unsigned long now = jiffies;
3553 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3556 do_renew_lease(clp, now);
3560 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3562 return (server->caps & NFS_CAP_ACLS)
3563 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3564 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3567 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3568 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3571 #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3573 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3574 struct page **pages, unsigned int *pgbase)
3576 struct page *newpage, **spages;
3582 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
3583 newpage = alloc_page(GFP_KERNEL);
3585 if (newpage == NULL)
3587 memcpy(page_address(newpage), buf, len);
3592 } while (buflen != 0);
3598 __free_page(spages[rc-1]);
3602 struct nfs4_cached_acl {
3608 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3610 struct nfs_inode *nfsi = NFS_I(inode);
3612 spin_lock(&inode->i_lock);
3613 kfree(nfsi->nfs4_acl);
3614 nfsi->nfs4_acl = acl;
3615 spin_unlock(&inode->i_lock);
3618 static void nfs4_zap_acl_attr(struct inode *inode)
3620 nfs4_set_cached_acl(inode, NULL);
3623 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3625 struct nfs_inode *nfsi = NFS_I(inode);
3626 struct nfs4_cached_acl *acl;
3629 spin_lock(&inode->i_lock);
3630 acl = nfsi->nfs4_acl;
3633 if (buf == NULL) /* user is just asking for length */
3635 if (acl->cached == 0)
3637 ret = -ERANGE; /* see getxattr(2) man page */
3638 if (acl->len > buflen)
3640 memcpy(buf, acl->data, acl->len);
3644 spin_unlock(&inode->i_lock);
3648 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3650 struct nfs4_cached_acl *acl;
3652 if (pages && acl_len <= PAGE_SIZE) {
3653 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3657 _copy_from_pages(acl->data, pages, pgbase, acl_len);
3659 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3666 nfs4_set_cached_acl(inode, acl);
3670 * The getxattr API returns the required buffer length when called with a
3671 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3672 * the required buf. On a NULL buf, we send a page of data to the server
3673 * guessing that the ACL request can be serviced by a page. If so, we cache
3674 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3675 * the cache. If not so, we throw away the page, and cache the required
3676 * length. The next getxattr call will then produce another round trip to
3677 * the server, this time with the input buf of the required size.
3679 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3681 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3682 struct nfs_getaclargs args = {
3683 .fh = NFS_FH(inode),
3687 struct nfs_getaclres res = {
3690 struct rpc_message msg = {
3691 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3695 int ret = -ENOMEM, npages, i, acl_len = 0;
3697 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3698 /* As long as we're doing a round trip to the server anyway,
3699 * let's be prepared for a page of acl data. */
3703 /* Add an extra page to handle the bitmap returned */
3706 for (i = 0; i < npages; i++) {
3707 pages[i] = alloc_page(GFP_KERNEL);
3712 /* for decoding across pages */
3713 res.acl_scratch = alloc_page(GFP_KERNEL);
3714 if (!res.acl_scratch)
3717 args.acl_len = npages * PAGE_SIZE;
3718 args.acl_pgbase = 0;
3720 /* Let decode_getfacl know not to fail if the ACL data is larger than
3721 * the page we send as a guess */
3723 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3725 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3726 __func__, buf, buflen, npages, args.acl_len);
3727 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3728 &msg, &args.seq_args, &res.seq_res, 0);
3732 acl_len = res.acl_len - res.acl_data_offset;
3733 if (acl_len > args.acl_len)
3734 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
3736 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
3740 if (acl_len > buflen)
3742 _copy_from_pages(buf, pages, res.acl_data_offset,
3747 for (i = 0; i < npages; i++)
3749 __free_page(pages[i]);
3750 if (res.acl_scratch)
3751 __free_page(res.acl_scratch);
3755 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3757 struct nfs4_exception exception = { };
3760 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3763 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3764 } while (exception.retry);
3768 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3770 struct nfs_server *server = NFS_SERVER(inode);
3773 if (!nfs4_server_supports_acls(server))
3775 ret = nfs_revalidate_inode(server, inode);
3778 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3779 nfs_zap_acl_cache(inode);
3780 ret = nfs4_read_cached_acl(inode, buf, buflen);
3782 /* -ENOENT is returned if there is no ACL or if there is an ACL
3783 * but no cached acl data, just the acl length */
3785 return nfs4_get_acl_uncached(inode, buf, buflen);
3788 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3790 struct nfs_server *server = NFS_SERVER(inode);
3791 struct page *pages[NFS4ACL_MAXPAGES];
3792 struct nfs_setaclargs arg = {
3793 .fh = NFS_FH(inode),
3797 struct nfs_setaclres res;
3798 struct rpc_message msg = {
3799 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3805 if (!nfs4_server_supports_acls(server))
3807 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3810 nfs_inode_return_delegation(inode);
3811 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3814 * Free each page after tx, so the only ref left is
3815 * held by the network stack
3818 put_page(pages[i-1]);
3821 * Acl update can result in inode attribute update.
3822 * so mark the attribute cache invalid.
3824 spin_lock(&inode->i_lock);
3825 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3826 spin_unlock(&inode->i_lock);
3827 nfs_access_zap_cache(inode);
3828 nfs_zap_acl_cache(inode);
3832 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3834 struct nfs4_exception exception = { };
3837 err = nfs4_handle_exception(NFS_SERVER(inode),
3838 __nfs4_proc_set_acl(inode, buf, buflen),
3840 } while (exception.retry);
3845 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3847 struct nfs_client *clp = server->nfs_client;
3849 if (task->tk_status >= 0)
3851 switch(task->tk_status) {
3852 case -NFS4ERR_DELEG_REVOKED:
3853 case -NFS4ERR_ADMIN_REVOKED:
3854 case -NFS4ERR_BAD_STATEID:
3857 nfs_remove_bad_delegation(state->inode);
3858 case -NFS4ERR_OPENMODE:
3861 nfs4_schedule_stateid_recovery(server, state);
3862 goto wait_on_recovery;
3863 case -NFS4ERR_EXPIRED:
3865 nfs4_schedule_stateid_recovery(server, state);
3866 case -NFS4ERR_STALE_STATEID:
3867 case -NFS4ERR_STALE_CLIENTID:
3868 nfs4_schedule_lease_recovery(clp);
3869 goto wait_on_recovery;
3870 #if defined(CONFIG_NFS_V4_1)
3871 case -NFS4ERR_BADSESSION:
3872 case -NFS4ERR_BADSLOT:
3873 case -NFS4ERR_BAD_HIGH_SLOT:
3874 case -NFS4ERR_DEADSESSION:
3875 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3876 case -NFS4ERR_SEQ_FALSE_RETRY:
3877 case -NFS4ERR_SEQ_MISORDERED:
3878 dprintk("%s ERROR %d, Reset session\n", __func__,
3880 nfs4_schedule_session_recovery(clp->cl_session);
3881 task->tk_status = 0;
3883 #endif /* CONFIG_NFS_V4_1 */
3884 case -NFS4ERR_DELAY:
3885 nfs_inc_server_stats(server, NFSIOS_DELAY);
3886 case -NFS4ERR_GRACE:
3888 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3889 task->tk_status = 0;
3891 case -NFS4ERR_RETRY_UNCACHED_REP:
3892 case -NFS4ERR_OLD_STATEID:
3893 task->tk_status = 0;
3896 task->tk_status = nfs4_map_errors(task->tk_status);
3899 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3900 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3901 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3902 task->tk_status = 0;
3906 static void nfs4_construct_boot_verifier(struct nfs_client *clp,
3907 nfs4_verifier *bootverf)
3911 verf[0] = htonl((u32)clp->cl_boot_time.tv_sec);
3912 verf[1] = htonl((u32)clp->cl_boot_time.tv_nsec);
3913 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3916 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3917 unsigned short port, struct rpc_cred *cred,
3918 struct nfs4_setclientid_res *res)
3920 nfs4_verifier sc_verifier;
3921 struct nfs4_setclientid setclientid = {
3922 .sc_verifier = &sc_verifier,
3924 .sc_cb_ident = clp->cl_cb_ident,
3926 struct rpc_message msg = {
3927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3928 .rpc_argp = &setclientid,
3935 nfs4_construct_boot_verifier(clp, &sc_verifier);
3939 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
3940 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
3942 rpc_peeraddr2str(clp->cl_rpcclient,
3944 rpc_peeraddr2str(clp->cl_rpcclient,
3946 clp->cl_rpcclient->cl_auth->au_ops->au_name,
3947 clp->cl_id_uniquifier);
3948 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
3949 sizeof(setclientid.sc_netid),
3950 rpc_peeraddr2str(clp->cl_rpcclient,
3951 RPC_DISPLAY_NETID));
3952 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
3953 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
3954 clp->cl_ipaddr, port >> 8, port & 255);
3957 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3958 if (status != -NFS4ERR_CLID_INUSE)
3961 ++clp->cl_id_uniquifier;
3965 ssleep(clp->cl_lease_time / HZ + 1);
3970 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3971 struct nfs4_setclientid_res *arg,
3972 struct rpc_cred *cred)
3974 struct nfs_fsinfo fsinfo;
3975 struct rpc_message msg = {
3976 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3978 .rpc_resp = &fsinfo,
3985 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
3987 spin_lock(&clp->cl_lock);
3988 clp->cl_lease_time = fsinfo.lease_time * HZ;
3989 clp->cl_last_renewal = now;
3990 spin_unlock(&clp->cl_lock);
3995 struct nfs4_delegreturndata {
3996 struct nfs4_delegreturnargs args;
3997 struct nfs4_delegreturnres res;
3999 nfs4_stateid stateid;
4000 unsigned long timestamp;
4001 struct nfs_fattr fattr;
4005 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4007 struct nfs4_delegreturndata *data = calldata;
4009 if (!nfs4_sequence_done(task, &data->res.seq_res))
4012 switch (task->tk_status) {
4013 case -NFS4ERR_STALE_STATEID:
4014 case -NFS4ERR_EXPIRED:
4016 renew_lease(data->res.server, data->timestamp);
4019 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4021 rpc_restart_call_prepare(task);
4025 data->rpc_status = task->tk_status;
4028 static void nfs4_delegreturn_release(void *calldata)
4033 #if defined(CONFIG_NFS_V4_1)
4034 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4036 struct nfs4_delegreturndata *d_data;
4038 d_data = (struct nfs4_delegreturndata *)data;
4040 if (nfs4_setup_sequence(d_data->res.server,
4041 &d_data->args.seq_args,
4042 &d_data->res.seq_res, task))
4044 rpc_call_start(task);
4046 #endif /* CONFIG_NFS_V4_1 */
4048 static const struct rpc_call_ops nfs4_delegreturn_ops = {
4049 #if defined(CONFIG_NFS_V4_1)
4050 .rpc_call_prepare = nfs4_delegreturn_prepare,
4051 #endif /* CONFIG_NFS_V4_1 */
4052 .rpc_call_done = nfs4_delegreturn_done,
4053 .rpc_release = nfs4_delegreturn_release,
4056 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4058 struct nfs4_delegreturndata *data;
4059 struct nfs_server *server = NFS_SERVER(inode);
4060 struct rpc_task *task;
4061 struct rpc_message msg = {
4062 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4065 struct rpc_task_setup task_setup_data = {
4066 .rpc_client = server->client,
4067 .rpc_message = &msg,
4068 .callback_ops = &nfs4_delegreturn_ops,
4069 .flags = RPC_TASK_ASYNC,
4073 data = kzalloc(sizeof(*data), GFP_NOFS);
4076 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4077 data->args.fhandle = &data->fh;
4078 data->args.stateid = &data->stateid;
4079 data->args.bitmask = server->cache_consistency_bitmask;
4080 nfs_copy_fh(&data->fh, NFS_FH(inode));
4081 nfs4_stateid_copy(&data->stateid, stateid);
4082 data->res.fattr = &data->fattr;
4083 data->res.server = server;
4084 nfs_fattr_init(data->res.fattr);
4085 data->timestamp = jiffies;
4086 data->rpc_status = 0;
4088 task_setup_data.callback_data = data;
4089 msg.rpc_argp = &data->args;
4090 msg.rpc_resp = &data->res;
4091 task = rpc_run_task(&task_setup_data);
4093 return PTR_ERR(task);
4096 status = nfs4_wait_for_completion_rpc_task(task);
4099 status = data->rpc_status;
4101 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4103 nfs_refresh_inode(inode, &data->fattr);
4109 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4111 struct nfs_server *server = NFS_SERVER(inode);
4112 struct nfs4_exception exception = { };
4115 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4117 case -NFS4ERR_STALE_STATEID:
4118 case -NFS4ERR_EXPIRED:
4122 err = nfs4_handle_exception(server, err, &exception);
4123 } while (exception.retry);
4127 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4128 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4131 * sleep, with exponential backoff, and retry the LOCK operation.
4133 static unsigned long
4134 nfs4_set_lock_task_retry(unsigned long timeout)
4136 freezable_schedule_timeout_killable(timeout);
4138 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4139 return NFS4_LOCK_MAXTIMEOUT;
4143 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4145 struct inode *inode = state->inode;
4146 struct nfs_server *server = NFS_SERVER(inode);
4147 struct nfs_client *clp = server->nfs_client;
4148 struct nfs_lockt_args arg = {
4149 .fh = NFS_FH(inode),
4152 struct nfs_lockt_res res = {
4155 struct rpc_message msg = {
4156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4159 .rpc_cred = state->owner->so_cred,
4161 struct nfs4_lock_state *lsp;
4164 arg.lock_owner.clientid = clp->cl_clientid;
4165 status = nfs4_set_lock_state(state, request);
4168 lsp = request->fl_u.nfs4_fl.owner;
4169 arg.lock_owner.id = lsp->ls_seqid.owner_id;
4170 arg.lock_owner.s_dev = server->s_dev;
4171 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4174 request->fl_type = F_UNLCK;
4176 case -NFS4ERR_DENIED:
4179 request->fl_ops->fl_release_private(request);
4184 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4186 struct nfs4_exception exception = { };
4190 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4191 _nfs4_proc_getlk(state, cmd, request),
4193 } while (exception.retry);
4197 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4200 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4202 res = posix_lock_file_wait(file, fl);
4205 res = flock_lock_file_wait(file, fl);
4213 struct nfs4_unlockdata {
4214 struct nfs_locku_args arg;
4215 struct nfs_locku_res res;
4216 struct nfs4_lock_state *lsp;
4217 struct nfs_open_context *ctx;
4218 struct file_lock fl;
4219 const struct nfs_server *server;
4220 unsigned long timestamp;
4223 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4224 struct nfs_open_context *ctx,
4225 struct nfs4_lock_state *lsp,
4226 struct nfs_seqid *seqid)
4228 struct nfs4_unlockdata *p;
4229 struct inode *inode = lsp->ls_state->inode;
4231 p = kzalloc(sizeof(*p), GFP_NOFS);
4234 p->arg.fh = NFS_FH(inode);
4236 p->arg.seqid = seqid;
4237 p->res.seqid = seqid;
4238 p->arg.stateid = &lsp->ls_stateid;
4240 atomic_inc(&lsp->ls_count);
4241 /* Ensure we don't close file until we're done freeing locks! */
4242 p->ctx = get_nfs_open_context(ctx);
4243 memcpy(&p->fl, fl, sizeof(p->fl));
4244 p->server = NFS_SERVER(inode);
4248 static void nfs4_locku_release_calldata(void *data)
4250 struct nfs4_unlockdata *calldata = data;
4251 nfs_free_seqid(calldata->arg.seqid);
4252 nfs4_put_lock_state(calldata->lsp);
4253 put_nfs_open_context(calldata->ctx);
4257 static void nfs4_locku_done(struct rpc_task *task, void *data)
4259 struct nfs4_unlockdata *calldata = data;
4261 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4263 switch (task->tk_status) {
4265 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4266 &calldata->res.stateid);
4267 renew_lease(calldata->server, calldata->timestamp);
4269 case -NFS4ERR_BAD_STATEID:
4270 case -NFS4ERR_OLD_STATEID:
4271 case -NFS4ERR_STALE_STATEID:
4272 case -NFS4ERR_EXPIRED:
4275 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4276 rpc_restart_call_prepare(task);
4280 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4282 struct nfs4_unlockdata *calldata = data;
4284 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4286 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
4287 /* Note: exit _without_ running nfs4_locku_done */
4288 task->tk_action = NULL;
4291 calldata->timestamp = jiffies;
4292 if (nfs4_setup_sequence(calldata->server,
4293 &calldata->arg.seq_args,
4294 &calldata->res.seq_res, task))
4296 rpc_call_start(task);
4299 static const struct rpc_call_ops nfs4_locku_ops = {
4300 .rpc_call_prepare = nfs4_locku_prepare,
4301 .rpc_call_done = nfs4_locku_done,
4302 .rpc_release = nfs4_locku_release_calldata,
4305 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4306 struct nfs_open_context *ctx,
4307 struct nfs4_lock_state *lsp,
4308 struct nfs_seqid *seqid)
4310 struct nfs4_unlockdata *data;
4311 struct rpc_message msg = {
4312 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4313 .rpc_cred = ctx->cred,
4315 struct rpc_task_setup task_setup_data = {
4316 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4317 .rpc_message = &msg,
4318 .callback_ops = &nfs4_locku_ops,
4319 .workqueue = nfsiod_workqueue,
4320 .flags = RPC_TASK_ASYNC,
4323 /* Ensure this is an unlock - when canceling a lock, the
4324 * canceled lock is passed in, and it won't be an unlock.
4326 fl->fl_type = F_UNLCK;
4328 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4330 nfs_free_seqid(seqid);
4331 return ERR_PTR(-ENOMEM);
4334 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4335 msg.rpc_argp = &data->arg;
4336 msg.rpc_resp = &data->res;
4337 task_setup_data.callback_data = data;
4338 return rpc_run_task(&task_setup_data);
4341 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4343 struct nfs_inode *nfsi = NFS_I(state->inode);
4344 struct nfs_seqid *seqid;
4345 struct nfs4_lock_state *lsp;
4346 struct rpc_task *task;
4348 unsigned char fl_flags = request->fl_flags;
4350 status = nfs4_set_lock_state(state, request);
4351 /* Unlock _before_ we do the RPC call */
4352 request->fl_flags |= FL_EXISTS;
4353 down_read(&nfsi->rwsem);
4354 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4355 up_read(&nfsi->rwsem);
4358 up_read(&nfsi->rwsem);
4361 /* Is this a delegated lock? */
4362 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4364 lsp = request->fl_u.nfs4_fl.owner;
4365 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4369 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4370 status = PTR_ERR(task);
4373 status = nfs4_wait_for_completion_rpc_task(task);
4376 request->fl_flags = fl_flags;
4380 struct nfs4_lockdata {
4381 struct nfs_lock_args arg;
4382 struct nfs_lock_res res;
4383 struct nfs4_lock_state *lsp;
4384 struct nfs_open_context *ctx;
4385 struct file_lock fl;
4386 unsigned long timestamp;
4389 struct nfs_server *server;
4392 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4393 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4396 struct nfs4_lockdata *p;
4397 struct inode *inode = lsp->ls_state->inode;
4398 struct nfs_server *server = NFS_SERVER(inode);
4400 p = kzalloc(sizeof(*p), gfp_mask);
4404 p->arg.fh = NFS_FH(inode);
4406 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4407 if (p->arg.open_seqid == NULL)
4409 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4410 if (p->arg.lock_seqid == NULL)
4411 goto out_free_seqid;
4412 p->arg.lock_stateid = &lsp->ls_stateid;
4413 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4414 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4415 p->arg.lock_owner.s_dev = server->s_dev;
4416 p->res.lock_seqid = p->arg.lock_seqid;
4419 atomic_inc(&lsp->ls_count);
4420 p->ctx = get_nfs_open_context(ctx);
4421 memcpy(&p->fl, fl, sizeof(p->fl));
4424 nfs_free_seqid(p->arg.open_seqid);
4430 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4432 struct nfs4_lockdata *data = calldata;
4433 struct nfs4_state *state = data->lsp->ls_state;
4435 dprintk("%s: begin!\n", __func__);
4436 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4438 /* Do we need to do an open_to_lock_owner? */
4439 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4440 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4442 data->arg.open_stateid = &state->stateid;
4443 data->arg.new_lock_owner = 1;
4444 data->res.open_seqid = data->arg.open_seqid;
4446 data->arg.new_lock_owner = 0;
4447 data->timestamp = jiffies;
4448 if (nfs4_setup_sequence(data->server,
4449 &data->arg.seq_args,
4450 &data->res.seq_res, task))
4452 rpc_call_start(task);
4453 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4456 static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4458 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4459 nfs4_lock_prepare(task, calldata);
4462 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4464 struct nfs4_lockdata *data = calldata;
4466 dprintk("%s: begin!\n", __func__);
4468 if (!nfs4_sequence_done(task, &data->res.seq_res))
4471 data->rpc_status = task->tk_status;
4472 if (data->arg.new_lock_owner != 0) {
4473 if (data->rpc_status == 0)
4474 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4478 if (data->rpc_status == 0) {
4479 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4480 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
4481 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4484 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4487 static void nfs4_lock_release(void *calldata)
4489 struct nfs4_lockdata *data = calldata;
4491 dprintk("%s: begin!\n", __func__);
4492 nfs_free_seqid(data->arg.open_seqid);
4493 if (data->cancelled != 0) {
4494 struct rpc_task *task;
4495 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4496 data->arg.lock_seqid);
4498 rpc_put_task_async(task);
4499 dprintk("%s: cancelling lock!\n", __func__);
4501 nfs_free_seqid(data->arg.lock_seqid);
4502 nfs4_put_lock_state(data->lsp);
4503 put_nfs_open_context(data->ctx);
4505 dprintk("%s: done!\n", __func__);
4508 static const struct rpc_call_ops nfs4_lock_ops = {
4509 .rpc_call_prepare = nfs4_lock_prepare,
4510 .rpc_call_done = nfs4_lock_done,
4511 .rpc_release = nfs4_lock_release,
4514 static const struct rpc_call_ops nfs4_recover_lock_ops = {
4515 .rpc_call_prepare = nfs4_recover_lock_prepare,
4516 .rpc_call_done = nfs4_lock_done,
4517 .rpc_release = nfs4_lock_release,
4520 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4523 case -NFS4ERR_ADMIN_REVOKED:
4524 case -NFS4ERR_BAD_STATEID:
4525 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4526 if (new_lock_owner != 0 ||
4527 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4528 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4530 case -NFS4ERR_STALE_STATEID:
4531 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4532 case -NFS4ERR_EXPIRED:
4533 nfs4_schedule_lease_recovery(server->nfs_client);
4537 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4539 struct nfs4_lockdata *data;
4540 struct rpc_task *task;
4541 struct rpc_message msg = {
4542 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4543 .rpc_cred = state->owner->so_cred,
4545 struct rpc_task_setup task_setup_data = {
4546 .rpc_client = NFS_CLIENT(state->inode),
4547 .rpc_message = &msg,
4548 .callback_ops = &nfs4_lock_ops,
4549 .workqueue = nfsiod_workqueue,
4550 .flags = RPC_TASK_ASYNC,
4554 dprintk("%s: begin!\n", __func__);
4555 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4556 fl->fl_u.nfs4_fl.owner,
4557 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4561 data->arg.block = 1;
4562 if (recovery_type > NFS_LOCK_NEW) {
4563 if (recovery_type == NFS_LOCK_RECLAIM)
4564 data->arg.reclaim = NFS_LOCK_RECLAIM;
4565 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4567 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4568 msg.rpc_argp = &data->arg;
4569 msg.rpc_resp = &data->res;
4570 task_setup_data.callback_data = data;
4571 task = rpc_run_task(&task_setup_data);
4573 return PTR_ERR(task);
4574 ret = nfs4_wait_for_completion_rpc_task(task);
4576 ret = data->rpc_status;
4578 nfs4_handle_setlk_error(data->server, data->lsp,
4579 data->arg.new_lock_owner, ret);
4581 data->cancelled = 1;
4583 dprintk("%s: done, ret = %d!\n", __func__, ret);
4587 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4589 struct nfs_server *server = NFS_SERVER(state->inode);
4590 struct nfs4_exception exception = {
4591 .inode = state->inode,
4596 /* Cache the lock if possible... */
4597 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4599 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4600 if (err != -NFS4ERR_DELAY)
4602 nfs4_handle_exception(server, err, &exception);
4603 } while (exception.retry);
4607 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4609 struct nfs_server *server = NFS_SERVER(state->inode);
4610 struct nfs4_exception exception = {
4611 .inode = state->inode,
4615 err = nfs4_set_lock_state(state, request);
4619 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4621 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4625 case -NFS4ERR_GRACE:
4626 case -NFS4ERR_DELAY:
4627 nfs4_handle_exception(server, err, &exception);
4630 } while (exception.retry);
4635 #if defined(CONFIG_NFS_V4_1)
4636 static int nfs41_check_expired_locks(struct nfs4_state *state)
4638 int status, ret = NFS_OK;
4639 struct nfs4_lock_state *lsp;
4640 struct nfs_server *server = NFS_SERVER(state->inode);
4642 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4643 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4644 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4645 if (status != NFS_OK) {
4646 nfs41_free_stateid(server, &lsp->ls_stateid);
4647 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4656 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4658 int status = NFS_OK;
4660 if (test_bit(LK_STATE_IN_USE, &state->flags))
4661 status = nfs41_check_expired_locks(state);
4662 if (status == NFS_OK)
4664 return nfs4_lock_expired(state, request);
4668 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4670 struct nfs_inode *nfsi = NFS_I(state->inode);
4671 unsigned char fl_flags = request->fl_flags;
4672 int status = -ENOLCK;
4674 if ((fl_flags & FL_POSIX) &&
4675 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4677 /* Is this a delegated open? */
4678 status = nfs4_set_lock_state(state, request);
4681 request->fl_flags |= FL_ACCESS;
4682 status = do_vfs_lock(request->fl_file, request);
4685 down_read(&nfsi->rwsem);
4686 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4687 /* Yes: cache locks! */
4688 /* ...but avoid races with delegation recall... */
4689 request->fl_flags = fl_flags & ~FL_SLEEP;
4690 status = do_vfs_lock(request->fl_file, request);
4693 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4696 /* Note: we always want to sleep here! */
4697 request->fl_flags = fl_flags | FL_SLEEP;
4698 if (do_vfs_lock(request->fl_file, request) < 0)
4699 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4700 "manager!\n", __func__);
4702 up_read(&nfsi->rwsem);
4704 request->fl_flags = fl_flags;
4708 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4710 struct nfs4_exception exception = {
4712 .inode = state->inode,
4717 err = _nfs4_proc_setlk(state, cmd, request);
4718 if (err == -NFS4ERR_DENIED)
4720 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4722 } while (exception.retry);
4727 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4729 struct nfs_open_context *ctx;
4730 struct nfs4_state *state;
4731 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4734 /* verify open state */
4735 ctx = nfs_file_open_context(filp);
4738 if (request->fl_start < 0 || request->fl_end < 0)
4741 if (IS_GETLK(cmd)) {
4743 return nfs4_proc_getlk(state, F_GETLK, request);
4747 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4750 if (request->fl_type == F_UNLCK) {
4752 return nfs4_proc_unlck(state, cmd, request);
4759 * Don't rely on the VFS having checked the file open mode,
4760 * since it won't do this for flock() locks.
4762 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4764 if (!(filp->f_mode & FMODE_READ))
4768 if (!(filp->f_mode & FMODE_WRITE))
4773 status = nfs4_proc_setlk(state, cmd, request);
4774 if ((status != -EAGAIN) || IS_SETLK(cmd))
4776 timeout = nfs4_set_lock_task_retry(timeout);
4777 status = -ERESTARTSYS;
4780 } while(status < 0);
4784 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4786 struct nfs_server *server = NFS_SERVER(state->inode);
4787 struct nfs4_exception exception = { };
4790 err = nfs4_set_lock_state(state, fl);
4794 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4797 printk(KERN_ERR "NFS: %s: unhandled error "
4798 "%d.\n", __func__, err);
4802 case -NFS4ERR_EXPIRED:
4803 nfs4_schedule_stateid_recovery(server, state);
4804 case -NFS4ERR_STALE_CLIENTID:
4805 case -NFS4ERR_STALE_STATEID:
4806 nfs4_schedule_lease_recovery(server->nfs_client);
4808 case -NFS4ERR_BADSESSION:
4809 case -NFS4ERR_BADSLOT:
4810 case -NFS4ERR_BAD_HIGH_SLOT:
4811 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4812 case -NFS4ERR_DEADSESSION:
4813 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
4817 * The show must go on: exit, but mark the
4818 * stateid as needing recovery.
4820 case -NFS4ERR_DELEG_REVOKED:
4821 case -NFS4ERR_ADMIN_REVOKED:
4822 case -NFS4ERR_BAD_STATEID:
4823 case -NFS4ERR_OPENMODE:
4824 nfs4_schedule_stateid_recovery(server, state);
4829 * User RPCSEC_GSS context has expired.
4830 * We cannot recover this stateid now, so
4831 * skip it and allow recovery thread to
4837 case -NFS4ERR_DENIED:
4838 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4841 case -NFS4ERR_DELAY:
4844 err = nfs4_handle_exception(server, err, &exception);
4845 } while (exception.retry);
4850 struct nfs_release_lockowner_data {
4851 struct nfs4_lock_state *lsp;
4852 struct nfs_server *server;
4853 struct nfs_release_lockowner_args args;
4856 static void nfs4_release_lockowner_release(void *calldata)
4858 struct nfs_release_lockowner_data *data = calldata;
4859 nfs4_free_lock_state(data->server, data->lsp);
4863 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
4864 .rpc_release = nfs4_release_lockowner_release,
4867 int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
4869 struct nfs_server *server = lsp->ls_state->owner->so_server;
4870 struct nfs_release_lockowner_data *data;
4871 struct rpc_message msg = {
4872 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4875 if (server->nfs_client->cl_mvops->minor_version != 0)
4877 data = kmalloc(sizeof(*data), GFP_NOFS);
4881 data->server = server;
4882 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4883 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4884 data->args.lock_owner.s_dev = server->s_dev;
4885 msg.rpc_argp = &data->args;
4886 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4890 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4892 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4893 const void *buf, size_t buflen,
4894 int flags, int type)
4896 if (strcmp(key, "") != 0)
4899 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
4902 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4903 void *buf, size_t buflen, int type)
4905 if (strcmp(key, "") != 0)
4908 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
4911 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4912 size_t list_len, const char *name,
4913 size_t name_len, int type)
4915 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
4917 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4920 if (list && len <= list_len)
4921 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
4926 * nfs_fhget will use either the mounted_on_fileid or the fileid
4928 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4930 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4931 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4932 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4933 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
4936 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4937 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
4938 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4942 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
4943 const struct qstr *name,
4944 struct nfs4_fs_locations *fs_locations,
4947 struct nfs_server *server = NFS_SERVER(dir);
4949 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4951 struct nfs4_fs_locations_arg args = {
4952 .dir_fh = NFS_FH(dir),
4957 struct nfs4_fs_locations_res res = {
4958 .fs_locations = fs_locations,
4960 struct rpc_message msg = {
4961 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4967 dprintk("%s: start\n", __func__);
4969 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4970 * is not supported */
4971 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4972 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4974 bitmask[0] |= FATTR4_WORD0_FILEID;
4976 nfs_fattr_init(&fs_locations->fattr);
4977 fs_locations->server = server;
4978 fs_locations->nlocations = 0;
4979 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
4980 dprintk("%s: returned status = %d\n", __func__, status);
4984 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
4985 const struct qstr *name,
4986 struct nfs4_fs_locations *fs_locations,
4989 struct nfs4_exception exception = { };
4992 err = nfs4_handle_exception(NFS_SERVER(dir),
4993 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
4995 } while (exception.retry);
4999 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5002 struct nfs4_secinfo_arg args = {
5003 .dir_fh = NFS_FH(dir),
5006 struct nfs4_secinfo_res res = {
5009 struct rpc_message msg = {
5010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5015 dprintk("NFS call secinfo %s\n", name->name);
5016 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5017 dprintk("NFS reply secinfo: %d\n", status);
5021 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5022 struct nfs4_secinfo_flavors *flavors)
5024 struct nfs4_exception exception = { };
5027 err = nfs4_handle_exception(NFS_SERVER(dir),
5028 _nfs4_proc_secinfo(dir, name, flavors),
5030 } while (exception.retry);
5034 #ifdef CONFIG_NFS_V4_1
5036 * Check the exchange flags returned by the server for invalid flags, having
5037 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5040 static int nfs4_check_cl_exchange_flags(u32 flags)
5042 if (flags & ~EXCHGID4_FLAG_MASK_R)
5044 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5045 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5047 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5051 return -NFS4ERR_INVAL;
5055 nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
5057 if (a->server_scope_sz == b->server_scope_sz &&
5058 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5065 * nfs4_proc_exchange_id()
5067 * Since the clientid has expired, all compounds using sessions
5068 * associated with the stale clientid will be returning
5069 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5070 * be in some phase of session reset.
5072 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
5074 nfs4_verifier verifier;
5075 struct nfs41_exchange_id_args args = {
5076 .verifier = &verifier,
5078 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
5080 struct nfs41_exchange_id_res res = {
5084 struct rpc_message msg = {
5085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5091 dprintk("--> %s\n", __func__);
5092 BUG_ON(clp == NULL);
5094 nfs4_construct_boot_verifier(clp, &verifier);
5096 args.id_len = scnprintf(args.id, sizeof(args.id),
5099 clp->cl_rpcclient->cl_nodename,
5100 clp->cl_rpcclient->cl_auth->au_flavor);
5102 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
5103 if (unlikely(!res.server_scope)) {
5108 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
5109 if (unlikely(!res.impl_id)) {
5111 goto out_server_scope;
5114 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5116 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
5119 /* use the most recent implementation id */
5120 kfree(clp->impl_id);
5121 clp->impl_id = res.impl_id;
5126 if (clp->server_scope &&
5127 !nfs41_same_server_scope(clp->server_scope,
5128 res.server_scope)) {
5129 dprintk("%s: server_scope mismatch detected\n",
5131 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5132 kfree(clp->server_scope);
5133 clp->server_scope = NULL;
5136 if (!clp->server_scope) {
5137 clp->server_scope = res.server_scope;
5143 kfree(res.server_scope);
5146 dprintk("%s: Server Implementation ID: "
5147 "domain: %s, name: %s, date: %llu,%u\n",
5148 __func__, clp->impl_id->domain, clp->impl_id->name,
5149 clp->impl_id->date.seconds,
5150 clp->impl_id->date.nseconds);
5151 dprintk("<-- %s status= %d\n", __func__, status);
5155 struct nfs4_get_lease_time_data {
5156 struct nfs4_get_lease_time_args *args;
5157 struct nfs4_get_lease_time_res *res;
5158 struct nfs_client *clp;
5161 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5165 struct nfs4_get_lease_time_data *data =
5166 (struct nfs4_get_lease_time_data *)calldata;
5168 dprintk("--> %s\n", __func__);
5169 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5170 /* just setup sequence, do not trigger session recovery
5171 since we're invoked within one */
5172 ret = nfs41_setup_sequence(data->clp->cl_session,
5173 &data->args->la_seq_args,
5174 &data->res->lr_seq_res, task);
5176 BUG_ON(ret == -EAGAIN);
5177 rpc_call_start(task);
5178 dprintk("<-- %s\n", __func__);
5182 * Called from nfs4_state_manager thread for session setup, so don't recover
5183 * from sequence operation or clientid errors.
5185 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5187 struct nfs4_get_lease_time_data *data =
5188 (struct nfs4_get_lease_time_data *)calldata;
5190 dprintk("--> %s\n", __func__);
5191 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5193 switch (task->tk_status) {
5194 case -NFS4ERR_DELAY:
5195 case -NFS4ERR_GRACE:
5196 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5197 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5198 task->tk_status = 0;
5200 case -NFS4ERR_RETRY_UNCACHED_REP:
5201 rpc_restart_call_prepare(task);
5204 dprintk("<-- %s\n", __func__);
5207 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5208 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5209 .rpc_call_done = nfs4_get_lease_time_done,
5212 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5214 struct rpc_task *task;
5215 struct nfs4_get_lease_time_args args;
5216 struct nfs4_get_lease_time_res res = {
5217 .lr_fsinfo = fsinfo,
5219 struct nfs4_get_lease_time_data data = {
5224 struct rpc_message msg = {
5225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5229 struct rpc_task_setup task_setup = {
5230 .rpc_client = clp->cl_rpcclient,
5231 .rpc_message = &msg,
5232 .callback_ops = &nfs4_get_lease_time_ops,
5233 .callback_data = &data,
5234 .flags = RPC_TASK_TIMEOUT,
5238 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5239 dprintk("--> %s\n", __func__);
5240 task = rpc_run_task(&task_setup);
5243 status = PTR_ERR(task);
5245 status = task->tk_status;
5248 dprintk("<-- %s return %d\n", __func__, status);
5253 static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5255 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5258 static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5259 struct nfs4_slot *new,
5263 struct nfs4_slot *old = NULL;
5266 spin_lock(&tbl->slot_tbl_lock);
5270 tbl->max_slots = max_slots;
5272 tbl->highest_used_slotid = -1; /* no slot is currently used */
5273 for (i = 0; i < tbl->max_slots; i++)
5274 tbl->slots[i].seq_nr = ivalue;
5275 spin_unlock(&tbl->slot_tbl_lock);
5280 * (re)Initialise a slot table
5282 static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5285 struct nfs4_slot *new = NULL;
5288 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5289 max_reqs, tbl->max_slots);
5291 /* Does the newly negotiated max_reqs match the existing slot table? */
5292 if (max_reqs != tbl->max_slots) {
5293 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
5299 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
5300 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5301 tbl, tbl->slots, tbl->max_slots);
5303 dprintk("<-- %s: return %d\n", __func__, ret);
5307 /* Destroy the slot table */
5308 static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5310 if (session->fc_slot_table.slots != NULL) {
5311 kfree(session->fc_slot_table.slots);
5312 session->fc_slot_table.slots = NULL;
5314 if (session->bc_slot_table.slots != NULL) {
5315 kfree(session->bc_slot_table.slots);
5316 session->bc_slot_table.slots = NULL;
5322 * Initialize or reset the forechannel and backchannel tables
5324 static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
5326 struct nfs4_slot_table *tbl;
5329 dprintk("--> %s\n", __func__);
5331 tbl = &ses->fc_slot_table;
5332 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5333 if (status) /* -ENOMEM */
5336 tbl = &ses->bc_slot_table;
5337 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5338 if (status && tbl->slots == NULL)
5339 /* Fore and back channel share a connection so get
5340 * both slot tables or neither */
5341 nfs4_destroy_slot_tables(ses);
5345 struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5347 struct nfs4_session *session;
5348 struct nfs4_slot_table *tbl;
5350 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5354 tbl = &session->fc_slot_table;
5355 tbl->highest_used_slotid = NFS4_NO_SLOT;
5356 spin_lock_init(&tbl->slot_tbl_lock);
5357 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
5358 init_completion(&tbl->complete);
5360 tbl = &session->bc_slot_table;
5361 tbl->highest_used_slotid = NFS4_NO_SLOT;
5362 spin_lock_init(&tbl->slot_tbl_lock);
5363 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
5364 init_completion(&tbl->complete);
5366 session->session_state = 1<<NFS4_SESSION_INITING;
5372 void nfs4_destroy_session(struct nfs4_session *session)
5374 struct rpc_xprt *xprt;
5376 nfs4_proc_destroy_session(session);
5379 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5381 dprintk("%s Destroy backchannel for xprt %p\n",
5383 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
5384 nfs4_destroy_slot_tables(session);
5389 * Initialize the values to be used by the client in CREATE_SESSION
5390 * If nfs4_init_session set the fore channel request and response sizes,
5393 * Set the back channel max_resp_sz_cached to zero to force the client to
5394 * always set csa_cachethis to FALSE because the current implementation
5395 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5397 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5399 struct nfs4_session *session = args->client->cl_session;
5400 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5401 mxresp_sz = session->fc_attrs.max_resp_sz;
5404 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5406 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5407 /* Fore channel attributes */
5408 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5409 args->fc_attrs.max_resp_sz = mxresp_sz;
5410 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5411 args->fc_attrs.max_reqs = max_session_slots;
5413 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5414 "max_ops=%u max_reqs=%u\n",
5416 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5417 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5419 /* Back channel attributes */
5420 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5421 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5422 args->bc_attrs.max_resp_sz_cached = 0;
5423 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5424 args->bc_attrs.max_reqs = 1;
5426 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5427 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5429 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5430 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5431 args->bc_attrs.max_reqs);
5434 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5436 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5437 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5439 if (rcvd->max_resp_sz > sent->max_resp_sz)
5442 * Our requested max_ops is the minimum we need; we're not
5443 * prepared to break up compounds into smaller pieces than that.
5444 * So, no point even trying to continue if the server won't
5447 if (rcvd->max_ops < sent->max_ops)
5449 if (rcvd->max_reqs == 0)
5451 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5452 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5456 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5458 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5459 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5461 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5463 if (rcvd->max_resp_sz < sent->max_resp_sz)
5465 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5467 /* These would render the backchannel useless: */
5468 if (rcvd->max_ops != sent->max_ops)
5470 if (rcvd->max_reqs != sent->max_reqs)
5475 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5476 struct nfs4_session *session)
5480 ret = nfs4_verify_fore_channel_attrs(args, session);
5483 return nfs4_verify_back_channel_attrs(args, session);
5486 static int _nfs4_proc_create_session(struct nfs_client *clp)
5488 struct nfs4_session *session = clp->cl_session;
5489 struct nfs41_create_session_args args = {
5491 .cb_program = NFS4_CALLBACK,
5493 struct nfs41_create_session_res res = {
5496 struct rpc_message msg = {
5497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5503 nfs4_init_channel_attrs(&args);
5504 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5506 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5509 /* Verify the session's negotiated channel_attrs values */
5510 status = nfs4_verify_channel_attrs(&args, session);
5512 /* Increment the clientid slot sequence id */
5520 * Issues a CREATE_SESSION operation to the server.
5521 * It is the responsibility of the caller to verify the session is
5522 * expired before calling this routine.
5524 int nfs4_proc_create_session(struct nfs_client *clp)
5528 struct nfs4_session *session = clp->cl_session;
5530 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5532 status = _nfs4_proc_create_session(clp);
5536 /* Init or reset the session slot tables */
5537 status = nfs4_setup_session_slot_tables(session);
5538 dprintk("slot table setup returned %d\n", status);
5542 ptr = (unsigned *)&session->sess_id.data[0];
5543 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5544 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5546 dprintk("<-- %s\n", __func__);
5551 * Issue the over-the-wire RPC DESTROY_SESSION.
5552 * The caller must serialize access to this routine.
5554 int nfs4_proc_destroy_session(struct nfs4_session *session)
5557 struct rpc_message msg;
5559 dprintk("--> nfs4_proc_destroy_session\n");
5561 /* session is still being setup */
5562 if (session->clp->cl_cons_state != NFS_CS_READY)
5565 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5566 msg.rpc_argp = session;
5567 msg.rpc_resp = NULL;
5568 msg.rpc_cred = NULL;
5569 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5573 "NFS: Got error %d from the server on DESTROY_SESSION. "
5574 "Session has been destroyed regardless...\n", status);
5576 dprintk("<-- nfs4_proc_destroy_session\n");
5580 int nfs4_init_session(struct nfs_server *server)
5582 struct nfs_client *clp = server->nfs_client;
5583 struct nfs4_session *session;
5584 unsigned int rsize, wsize;
5587 if (!nfs4_has_session(clp))
5590 session = clp->cl_session;
5591 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5594 rsize = server->rsize;
5596 rsize = NFS_MAX_FILE_IO_SIZE;
5597 wsize = server->wsize;
5599 wsize = NFS_MAX_FILE_IO_SIZE;
5601 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5602 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5604 ret = nfs4_recover_expired_lease(server);
5606 ret = nfs4_check_client_ready(clp);
5610 int nfs4_init_ds_session(struct nfs_client *clp)
5612 struct nfs4_session *session = clp->cl_session;
5615 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5618 ret = nfs4_client_recover_expired_lease(clp);
5620 /* Test for the DS role */
5621 if (!is_ds_client(clp))
5624 ret = nfs4_check_client_ready(clp);
5628 EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5632 * Renew the cl_session lease.
5634 struct nfs4_sequence_data {
5635 struct nfs_client *clp;
5636 struct nfs4_sequence_args args;
5637 struct nfs4_sequence_res res;
5640 static void nfs41_sequence_release(void *data)
5642 struct nfs4_sequence_data *calldata = data;
5643 struct nfs_client *clp = calldata->clp;
5645 if (atomic_read(&clp->cl_count) > 1)
5646 nfs4_schedule_state_renewal(clp);
5647 nfs_put_client(clp);
5651 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5653 switch(task->tk_status) {
5654 case -NFS4ERR_DELAY:
5655 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5658 nfs4_schedule_lease_recovery(clp);
5663 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5665 struct nfs4_sequence_data *calldata = data;
5666 struct nfs_client *clp = calldata->clp;
5668 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5671 if (task->tk_status < 0) {
5672 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5673 if (atomic_read(&clp->cl_count) == 1)
5676 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5677 rpc_restart_call_prepare(task);
5681 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5683 dprintk("<-- %s\n", __func__);
5686 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5688 struct nfs4_sequence_data *calldata = data;
5689 struct nfs_client *clp = calldata->clp;
5690 struct nfs4_sequence_args *args;
5691 struct nfs4_sequence_res *res;
5693 args = task->tk_msg.rpc_argp;
5694 res = task->tk_msg.rpc_resp;
5696 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
5698 rpc_call_start(task);
5701 static const struct rpc_call_ops nfs41_sequence_ops = {
5702 .rpc_call_done = nfs41_sequence_call_done,
5703 .rpc_call_prepare = nfs41_sequence_prepare,
5704 .rpc_release = nfs41_sequence_release,
5707 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5709 struct nfs4_sequence_data *calldata;
5710 struct rpc_message msg = {
5711 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5714 struct rpc_task_setup task_setup_data = {
5715 .rpc_client = clp->cl_rpcclient,
5716 .rpc_message = &msg,
5717 .callback_ops = &nfs41_sequence_ops,
5718 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5721 if (!atomic_inc_not_zero(&clp->cl_count))
5722 return ERR_PTR(-EIO);
5723 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5724 if (calldata == NULL) {
5725 nfs_put_client(clp);
5726 return ERR_PTR(-ENOMEM);
5728 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
5729 msg.rpc_argp = &calldata->args;
5730 msg.rpc_resp = &calldata->res;
5731 calldata->clp = clp;
5732 task_setup_data.callback_data = calldata;
5734 return rpc_run_task(&task_setup_data);
5737 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5739 struct rpc_task *task;
5742 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5744 task = _nfs41_proc_sequence(clp, cred);
5746 ret = PTR_ERR(task);
5748 rpc_put_task_async(task);
5749 dprintk("<-- %s status=%d\n", __func__, ret);
5753 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5755 struct rpc_task *task;
5758 task = _nfs41_proc_sequence(clp, cred);
5760 ret = PTR_ERR(task);
5763 ret = rpc_wait_for_completion_task(task);
5765 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5767 if (task->tk_status == 0)
5768 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5769 ret = task->tk_status;
5773 dprintk("<-- %s status=%d\n", __func__, ret);
5777 struct nfs4_reclaim_complete_data {
5778 struct nfs_client *clp;
5779 struct nfs41_reclaim_complete_args arg;
5780 struct nfs41_reclaim_complete_res res;
5783 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5785 struct nfs4_reclaim_complete_data *calldata = data;
5787 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
5788 if (nfs41_setup_sequence(calldata->clp->cl_session,
5789 &calldata->arg.seq_args,
5790 &calldata->res.seq_res, task))
5793 rpc_call_start(task);
5796 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5798 switch(task->tk_status) {
5800 case -NFS4ERR_COMPLETE_ALREADY:
5801 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5803 case -NFS4ERR_DELAY:
5804 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5806 case -NFS4ERR_RETRY_UNCACHED_REP:
5809 nfs4_schedule_lease_recovery(clp);
5814 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5816 struct nfs4_reclaim_complete_data *calldata = data;
5817 struct nfs_client *clp = calldata->clp;
5818 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5820 dprintk("--> %s\n", __func__);
5821 if (!nfs41_sequence_done(task, res))
5824 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5825 rpc_restart_call_prepare(task);
5828 dprintk("<-- %s\n", __func__);
5831 static void nfs4_free_reclaim_complete_data(void *data)
5833 struct nfs4_reclaim_complete_data *calldata = data;
5838 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5839 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5840 .rpc_call_done = nfs4_reclaim_complete_done,
5841 .rpc_release = nfs4_free_reclaim_complete_data,
5845 * Issue a global reclaim complete.
5847 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5849 struct nfs4_reclaim_complete_data *calldata;
5850 struct rpc_task *task;
5851 struct rpc_message msg = {
5852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5854 struct rpc_task_setup task_setup_data = {
5855 .rpc_client = clp->cl_rpcclient,
5856 .rpc_message = &msg,
5857 .callback_ops = &nfs4_reclaim_complete_call_ops,
5858 .flags = RPC_TASK_ASYNC,
5860 int status = -ENOMEM;
5862 dprintk("--> %s\n", __func__);
5863 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5864 if (calldata == NULL)
5866 calldata->clp = clp;
5867 calldata->arg.one_fs = 0;
5869 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
5870 msg.rpc_argp = &calldata->arg;
5871 msg.rpc_resp = &calldata->res;
5872 task_setup_data.callback_data = calldata;
5873 task = rpc_run_task(&task_setup_data);
5875 status = PTR_ERR(task);
5878 status = nfs4_wait_for_completion_rpc_task(task);
5880 status = task->tk_status;
5884 dprintk("<-- %s status=%d\n", __func__, status);
5889 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5891 struct nfs4_layoutget *lgp = calldata;
5892 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5894 dprintk("--> %s\n", __func__);
5895 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5896 * right now covering the LAYOUTGET we are about to send.
5897 * However, that is not so catastrophic, and there seems
5898 * to be no way to prevent it completely.
5900 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5901 &lgp->res.seq_res, task))
5903 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5904 NFS_I(lgp->args.inode)->layout,
5905 lgp->args.ctx->state)) {
5906 rpc_exit(task, NFS4_OK);
5909 rpc_call_start(task);
5912 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5914 struct nfs4_layoutget *lgp = calldata;
5915 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5917 dprintk("--> %s\n", __func__);
5919 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5922 switch (task->tk_status) {
5925 case -NFS4ERR_LAYOUTTRYLATER:
5926 case -NFS4ERR_RECALLCONFLICT:
5927 task->tk_status = -NFS4ERR_DELAY;
5930 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5931 rpc_restart_call_prepare(task);
5935 dprintk("<-- %s\n", __func__);
5938 static void nfs4_layoutget_release(void *calldata)
5940 struct nfs4_layoutget *lgp = calldata;
5942 dprintk("--> %s\n", __func__);
5943 put_nfs_open_context(lgp->args.ctx);
5945 dprintk("<-- %s\n", __func__);
5948 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5949 .rpc_call_prepare = nfs4_layoutget_prepare,
5950 .rpc_call_done = nfs4_layoutget_done,
5951 .rpc_release = nfs4_layoutget_release,
5954 int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5956 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5957 struct rpc_task *task;
5958 struct rpc_message msg = {
5959 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5960 .rpc_argp = &lgp->args,
5961 .rpc_resp = &lgp->res,
5963 struct rpc_task_setup task_setup_data = {
5964 .rpc_client = server->client,
5965 .rpc_message = &msg,
5966 .callback_ops = &nfs4_layoutget_call_ops,
5967 .callback_data = lgp,
5968 .flags = RPC_TASK_ASYNC,
5972 dprintk("--> %s\n", __func__);
5974 lgp->res.layoutp = &lgp->args.layout;
5975 lgp->res.seq_res.sr_slot = NULL;
5976 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
5977 task = rpc_run_task(&task_setup_data);
5979 return PTR_ERR(task);
5980 status = nfs4_wait_for_completion_rpc_task(task);
5982 status = task->tk_status;
5984 status = pnfs_layout_process(lgp);
5986 dprintk("<-- %s status=%d\n", __func__, status);
5991 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5993 struct nfs4_layoutreturn *lrp = calldata;
5995 dprintk("--> %s\n", __func__);
5996 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5997 &lrp->res.seq_res, task))
5999 rpc_call_start(task);
6002 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6004 struct nfs4_layoutreturn *lrp = calldata;
6005 struct nfs_server *server;
6006 struct pnfs_layout_hdr *lo = lrp->args.layout;
6008 dprintk("--> %s\n", __func__);
6010 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6013 server = NFS_SERVER(lrp->args.inode);
6014 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6015 rpc_restart_call_prepare(task);
6018 spin_lock(&lo->plh_inode->i_lock);
6019 if (task->tk_status == 0) {
6020 if (lrp->res.lrs_present) {
6021 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6023 BUG_ON(!list_empty(&lo->plh_segs));
6025 lo->plh_block_lgets--;
6026 spin_unlock(&lo->plh_inode->i_lock);
6027 dprintk("<-- %s\n", __func__);
6030 static void nfs4_layoutreturn_release(void *calldata)
6032 struct nfs4_layoutreturn *lrp = calldata;
6034 dprintk("--> %s\n", __func__);
6035 put_layout_hdr(lrp->args.layout);
6037 dprintk("<-- %s\n", __func__);
6040 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6041 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6042 .rpc_call_done = nfs4_layoutreturn_done,
6043 .rpc_release = nfs4_layoutreturn_release,
6046 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6048 struct rpc_task *task;
6049 struct rpc_message msg = {
6050 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6051 .rpc_argp = &lrp->args,
6052 .rpc_resp = &lrp->res,
6054 struct rpc_task_setup task_setup_data = {
6055 .rpc_client = lrp->clp->cl_rpcclient,
6056 .rpc_message = &msg,
6057 .callback_ops = &nfs4_layoutreturn_call_ops,
6058 .callback_data = lrp,
6062 dprintk("--> %s\n", __func__);
6063 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
6064 task = rpc_run_task(&task_setup_data);
6066 return PTR_ERR(task);
6067 status = task->tk_status;
6068 dprintk("<-- %s status=%d\n", __func__, status);
6074 * Retrieve the list of Data Server devices from the MDS.
6076 static int _nfs4_getdevicelist(struct nfs_server *server,
6077 const struct nfs_fh *fh,
6078 struct pnfs_devicelist *devlist)
6080 struct nfs4_getdevicelist_args args = {
6082 .layoutclass = server->pnfs_curr_ld->id,
6084 struct nfs4_getdevicelist_res res = {
6087 struct rpc_message msg = {
6088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6094 dprintk("--> %s\n", __func__);
6095 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6097 dprintk("<-- %s status=%d\n", __func__, status);
6101 int nfs4_proc_getdevicelist(struct nfs_server *server,
6102 const struct nfs_fh *fh,
6103 struct pnfs_devicelist *devlist)
6105 struct nfs4_exception exception = { };
6109 err = nfs4_handle_exception(server,
6110 _nfs4_getdevicelist(server, fh, devlist),
6112 } while (exception.retry);
6114 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6115 err, devlist->num_devs);
6119 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6122 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6124 struct nfs4_getdeviceinfo_args args = {
6127 struct nfs4_getdeviceinfo_res res = {
6130 struct rpc_message msg = {
6131 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6137 dprintk("--> %s\n", __func__);
6138 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6139 dprintk("<-- %s status=%d\n", __func__, status);
6144 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6146 struct nfs4_exception exception = { };
6150 err = nfs4_handle_exception(server,
6151 _nfs4_proc_getdeviceinfo(server, pdev),
6153 } while (exception.retry);
6156 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6158 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6160 struct nfs4_layoutcommit_data *data = calldata;
6161 struct nfs_server *server = NFS_SERVER(data->args.inode);
6163 if (nfs4_setup_sequence(server, &data->args.seq_args,
6164 &data->res.seq_res, task))
6166 rpc_call_start(task);
6170 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6172 struct nfs4_layoutcommit_data *data = calldata;
6173 struct nfs_server *server = NFS_SERVER(data->args.inode);
6175 if (!nfs4_sequence_done(task, &data->res.seq_res))
6178 switch (task->tk_status) { /* Just ignore these failures */
6179 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6180 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6181 case -NFS4ERR_BADLAYOUT: /* no layout */
6182 case -NFS4ERR_GRACE: /* loca_recalim always false */
6183 task->tk_status = 0;
6186 nfs_post_op_update_inode_force_wcc(data->args.inode,
6190 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6191 rpc_restart_call_prepare(task);
6197 static void nfs4_layoutcommit_release(void *calldata)
6199 struct nfs4_layoutcommit_data *data = calldata;
6200 struct pnfs_layout_segment *lseg, *tmp;
6201 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
6203 pnfs_cleanup_layoutcommit(data);
6204 /* Matched by references in pnfs_set_layoutcommit */
6205 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6206 list_del_init(&lseg->pls_lc_list);
6207 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6212 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6213 smp_mb__after_clear_bit();
6214 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6216 put_rpccred(data->cred);
6220 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6221 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6222 .rpc_call_done = nfs4_layoutcommit_done,
6223 .rpc_release = nfs4_layoutcommit_release,
6227 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6229 struct rpc_message msg = {
6230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6231 .rpc_argp = &data->args,
6232 .rpc_resp = &data->res,
6233 .rpc_cred = data->cred,
6235 struct rpc_task_setup task_setup_data = {
6236 .task = &data->task,
6237 .rpc_client = NFS_CLIENT(data->args.inode),
6238 .rpc_message = &msg,
6239 .callback_ops = &nfs4_layoutcommit_ops,
6240 .callback_data = data,
6241 .flags = RPC_TASK_ASYNC,
6243 struct rpc_task *task;
6246 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6247 "lbw: %llu inode %lu\n",
6248 data->task.tk_pid, sync,
6249 data->args.lastbytewritten,
6250 data->args.inode->i_ino);
6252 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6253 task = rpc_run_task(&task_setup_data);
6255 return PTR_ERR(task);
6258 status = nfs4_wait_for_completion_rpc_task(task);
6261 status = task->tk_status;
6263 dprintk("%s: status %d\n", __func__, status);
6269 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6270 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6272 struct nfs41_secinfo_no_name_args args = {
6273 .style = SECINFO_STYLE_CURRENT_FH,
6275 struct nfs4_secinfo_res res = {
6278 struct rpc_message msg = {
6279 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6283 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6287 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6288 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6290 struct nfs4_exception exception = { };
6293 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6296 case -NFS4ERR_WRONGSEC:
6297 case -NFS4ERR_NOTSUPP:
6300 err = nfs4_handle_exception(server, err, &exception);
6302 } while (exception.retry);
6308 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6309 struct nfs_fsinfo *info)
6313 rpc_authflavor_t flavor;
6314 struct nfs4_secinfo_flavors *flavors;
6316 page = alloc_page(GFP_KERNEL);
6322 flavors = page_address(page);
6323 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6326 * Fall back on "guess and check" method if
6327 * the server doesn't support SECINFO_NO_NAME
6329 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6330 err = nfs4_find_root_sec(server, fhandle, info);
6336 flavor = nfs_find_best_sec(flavors);
6338 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6348 static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6351 struct nfs41_test_stateid_args args = {
6354 struct nfs41_test_stateid_res res;
6355 struct rpc_message msg = {
6356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6361 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6362 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6364 if (status == NFS_OK)
6369 static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6371 struct nfs4_exception exception = { };
6374 err = nfs4_handle_exception(server,
6375 _nfs41_test_stateid(server, stateid),
6377 } while (exception.retry);
6381 static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6383 struct nfs41_free_stateid_args args = {
6386 struct nfs41_free_stateid_res res;
6387 struct rpc_message msg = {
6388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6393 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6394 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6397 static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6399 struct nfs4_exception exception = { };
6402 err = nfs4_handle_exception(server,
6403 _nfs4_free_stateid(server, stateid),
6405 } while (exception.retry);
6409 static bool nfs41_match_stateid(const nfs4_stateid *s1,
6410 const nfs4_stateid *s2)
6412 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
6415 if (s1->seqid == s2->seqid)
6417 if (s1->seqid == 0 || s2->seqid == 0)
6423 #endif /* CONFIG_NFS_V4_1 */
6425 static bool nfs4_match_stateid(const nfs4_stateid *s1,
6426 const nfs4_stateid *s2)
6428 return nfs4_stateid_match(s1, s2);
6432 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6433 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6434 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6435 .recover_open = nfs4_open_reclaim,
6436 .recover_lock = nfs4_lock_reclaim,
6437 .establish_clid = nfs4_init_clientid,
6438 .get_clid_cred = nfs4_get_setclientid_cred,
6441 #if defined(CONFIG_NFS_V4_1)
6442 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6443 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6444 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6445 .recover_open = nfs4_open_reclaim,
6446 .recover_lock = nfs4_lock_reclaim,
6447 .establish_clid = nfs41_init_clientid,
6448 .get_clid_cred = nfs4_get_exchange_id_cred,
6449 .reclaim_complete = nfs41_proc_reclaim_complete,
6451 #endif /* CONFIG_NFS_V4_1 */
6453 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6454 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6455 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6456 .recover_open = nfs4_open_expired,
6457 .recover_lock = nfs4_lock_expired,
6458 .establish_clid = nfs4_init_clientid,
6459 .get_clid_cred = nfs4_get_setclientid_cred,
6462 #if defined(CONFIG_NFS_V4_1)
6463 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6464 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6465 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6466 .recover_open = nfs41_open_expired,
6467 .recover_lock = nfs41_lock_expired,
6468 .establish_clid = nfs41_init_clientid,
6469 .get_clid_cred = nfs4_get_exchange_id_cred,
6471 #endif /* CONFIG_NFS_V4_1 */
6473 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6474 .sched_state_renewal = nfs4_proc_async_renew,
6475 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6476 .renew_lease = nfs4_proc_renew,
6479 #if defined(CONFIG_NFS_V4_1)
6480 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6481 .sched_state_renewal = nfs41_proc_async_sequence,
6482 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
6483 .renew_lease = nfs4_proc_sequence,
6487 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6489 .call_sync = _nfs4_call_sync,
6490 .match_stateid = nfs4_match_stateid,
6491 .find_root_sec = nfs4_find_root_sec,
6492 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6493 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6494 .state_renewal_ops = &nfs40_state_renewal_ops,
6497 #if defined(CONFIG_NFS_V4_1)
6498 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6500 .call_sync = _nfs4_call_sync_session,
6501 .match_stateid = nfs41_match_stateid,
6502 .find_root_sec = nfs41_find_root_sec,
6503 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6504 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6505 .state_renewal_ops = &nfs41_state_renewal_ops,
6509 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6510 [0] = &nfs_v4_0_minor_ops,
6511 #if defined(CONFIG_NFS_V4_1)
6512 [1] = &nfs_v4_1_minor_ops,
6516 static const struct inode_operations nfs4_file_inode_operations = {
6517 .permission = nfs_permission,
6518 .getattr = nfs_getattr,
6519 .setattr = nfs_setattr,
6520 .getxattr = generic_getxattr,
6521 .setxattr = generic_setxattr,
6522 .listxattr = generic_listxattr,
6523 .removexattr = generic_removexattr,
6526 const struct nfs_rpc_ops nfs_v4_clientops = {
6527 .version = 4, /* protocol version */
6528 .dentry_ops = &nfs4_dentry_operations,
6529 .dir_inode_ops = &nfs4_dir_inode_operations,
6530 .file_inode_ops = &nfs4_file_inode_operations,
6531 .file_ops = &nfs4_file_operations,
6532 .getroot = nfs4_proc_get_root,
6533 .submount = nfs4_submount,
6534 .getattr = nfs4_proc_getattr,
6535 .setattr = nfs4_proc_setattr,
6536 .lookup = nfs4_proc_lookup,
6537 .access = nfs4_proc_access,
6538 .readlink = nfs4_proc_readlink,
6539 .create = nfs4_proc_create,
6540 .remove = nfs4_proc_remove,
6541 .unlink_setup = nfs4_proc_unlink_setup,
6542 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
6543 .unlink_done = nfs4_proc_unlink_done,
6544 .rename = nfs4_proc_rename,
6545 .rename_setup = nfs4_proc_rename_setup,
6546 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
6547 .rename_done = nfs4_proc_rename_done,
6548 .link = nfs4_proc_link,
6549 .symlink = nfs4_proc_symlink,
6550 .mkdir = nfs4_proc_mkdir,
6551 .rmdir = nfs4_proc_remove,
6552 .readdir = nfs4_proc_readdir,
6553 .mknod = nfs4_proc_mknod,
6554 .statfs = nfs4_proc_statfs,
6555 .fsinfo = nfs4_proc_fsinfo,
6556 .pathconf = nfs4_proc_pathconf,
6557 .set_capabilities = nfs4_server_capabilities,
6558 .decode_dirent = nfs4_decode_dirent,
6559 .read_setup = nfs4_proc_read_setup,
6560 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
6561 .read_done = nfs4_read_done,
6562 .write_setup = nfs4_proc_write_setup,
6563 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
6564 .write_done = nfs4_write_done,
6565 .commit_setup = nfs4_proc_commit_setup,
6566 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
6567 .commit_done = nfs4_commit_done,
6568 .lock = nfs4_proc_lock,
6569 .clear_acl_cache = nfs4_zap_acl_attr,
6570 .close_context = nfs4_close_context,
6571 .open_context = nfs4_atomic_open,
6572 .init_client = nfs4_init_client,
6575 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6576 .prefix = XATTR_NAME_NFSV4_ACL,
6577 .list = nfs4_xattr_list_nfs4_acl,
6578 .get = nfs4_xattr_get_nfs4_acl,
6579 .set = nfs4_xattr_set_nfs4_acl,
6582 const struct xattr_handler *nfs4_xattr_handlers[] = {
6583 &nfs4_xattr_nfs4_acl_handler,
6587 module_param(max_session_slots, ushort, 0644);
6588 MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6589 "requests the client will negotiate");