NFSv4.1: Fix an ABBA locking issue with session and state serialisation
[linux-2.6.git] / fs / nfs / nfs4proc.c
blobcf252dfd91f82214b0a47169ecbee036d3223509
1 /*
2 * fs/nfs/nfs4proc.c
4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
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
14 * are met:
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.
38 #include <linux/mm.h>
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/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/nfs_idmap.h>
55 #include <linux/xattr.h>
56 #include <linux/utsname.h>
57 #include <linux/freezer.h>
59 #include "nfs4_fs.h"
60 #include "delegation.h"
61 #include "internal.h"
62 #include "iostat.h"
63 #include "callback.h"
64 #include "pnfs.h"
65 #include "netns.h"
66 #include "nfs4session.h"
67 #include "fscache.h"
69 #define NFSDBG_FACILITY NFSDBG_PROC
71 #define NFS4_POLL_RETRY_MIN (HZ/10)
72 #define NFS4_POLL_RETRY_MAX (15*HZ)
74 struct nfs4_opendata;
75 static int _nfs4_proc_open(struct nfs4_opendata *data);
76 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
77 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
78 static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
79 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
80 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
81 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
82 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
83 struct nfs_fattr *fattr, struct iattr *sattr,
84 struct nfs4_state *state);
85 #ifdef CONFIG_NFS_V4_1
86 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
87 static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
88 #endif
89 /* Prevent leaks of NFSv4 errors into userland */
90 static int nfs4_map_errors(int err)
92 if (err >= -1000)
93 return err;
94 switch (err) {
95 case -NFS4ERR_RESOURCE:
96 return -EREMOTEIO;
97 case -NFS4ERR_WRONGSEC:
98 return -EPERM;
99 case -NFS4ERR_BADOWNER:
100 case -NFS4ERR_BADNAME:
101 return -EINVAL;
102 case -NFS4ERR_SHARE_DENIED:
103 return -EACCES;
104 case -NFS4ERR_MINOR_VERS_MISMATCH:
105 return -EPROTONOSUPPORT;
106 case -NFS4ERR_ACCESS:
107 return -EACCES;
108 default:
109 dprintk("%s could not handle NFSv4 error %d\n",
110 __func__, -err);
111 break;
113 return -EIO;
117 * This is our standard bitmap for GETATTR requests.
119 const u32 nfs4_fattr_bitmap[3] = {
120 FATTR4_WORD0_TYPE
121 | FATTR4_WORD0_CHANGE
122 | FATTR4_WORD0_SIZE
123 | FATTR4_WORD0_FSID
124 | FATTR4_WORD0_FILEID,
125 FATTR4_WORD1_MODE
126 | FATTR4_WORD1_NUMLINKS
127 | FATTR4_WORD1_OWNER
128 | FATTR4_WORD1_OWNER_GROUP
129 | FATTR4_WORD1_RAWDEV
130 | FATTR4_WORD1_SPACE_USED
131 | FATTR4_WORD1_TIME_ACCESS
132 | FATTR4_WORD1_TIME_METADATA
133 | FATTR4_WORD1_TIME_MODIFY
136 static const u32 nfs4_pnfs_open_bitmap[3] = {
137 FATTR4_WORD0_TYPE
138 | FATTR4_WORD0_CHANGE
139 | FATTR4_WORD0_SIZE
140 | FATTR4_WORD0_FSID
141 | FATTR4_WORD0_FILEID,
142 FATTR4_WORD1_MODE
143 | FATTR4_WORD1_NUMLINKS
144 | FATTR4_WORD1_OWNER
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY,
151 FATTR4_WORD2_MDSTHRESHOLD
154 static const u32 nfs4_open_noattr_bitmap[3] = {
155 FATTR4_WORD0_TYPE
156 | FATTR4_WORD0_CHANGE
157 | FATTR4_WORD0_FILEID,
160 const u32 nfs4_statfs_bitmap[2] = {
161 FATTR4_WORD0_FILES_AVAIL
162 | FATTR4_WORD0_FILES_FREE
163 | FATTR4_WORD0_FILES_TOTAL,
164 FATTR4_WORD1_SPACE_AVAIL
165 | FATTR4_WORD1_SPACE_FREE
166 | FATTR4_WORD1_SPACE_TOTAL
169 const u32 nfs4_pathconf_bitmap[2] = {
170 FATTR4_WORD0_MAXLINK
171 | FATTR4_WORD0_MAXNAME,
175 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
176 | FATTR4_WORD0_MAXREAD
177 | FATTR4_WORD0_MAXWRITE
178 | FATTR4_WORD0_LEASE_TIME,
179 FATTR4_WORD1_TIME_DELTA
180 | FATTR4_WORD1_FS_LAYOUT_TYPES,
181 FATTR4_WORD2_LAYOUT_BLKSIZE
184 const u32 nfs4_fs_locations_bitmap[2] = {
185 FATTR4_WORD0_TYPE
186 | FATTR4_WORD0_CHANGE
187 | FATTR4_WORD0_SIZE
188 | FATTR4_WORD0_FSID
189 | FATTR4_WORD0_FILEID
190 | FATTR4_WORD0_FS_LOCATIONS,
191 FATTR4_WORD1_MODE
192 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID
203 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
204 struct nfs4_readdir_arg *readdir)
206 __be32 *start, *p;
208 if (cookie > 2) {
209 readdir->cookie = cookie;
210 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
211 return;
214 readdir->cookie = 0;
215 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
216 if (cookie == 2)
217 return;
220 * NFSv4 servers do not return entries for '.' and '..'
221 * Therefore, we fake these entries here. We let '.'
222 * have cookie 0 and '..' have cookie 1. Note that
223 * when talking to the server, we always send cookie 0
224 * instead of 1 or 2.
226 start = p = kmap_atomic(*readdir->pages);
228 if (cookie == 0) {
229 *p++ = xdr_one; /* next */
230 *p++ = xdr_zero; /* cookie, first word */
231 *p++ = xdr_one; /* cookie, second word */
232 *p++ = xdr_one; /* entry len */
233 memcpy(p, ".\0\0\0", 4); /* entry */
234 p++;
235 *p++ = xdr_one; /* bitmap length */
236 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
237 *p++ = htonl(8); /* attribute buffer length */
238 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
241 *p++ = xdr_one; /* next */
242 *p++ = xdr_zero; /* cookie, first word */
243 *p++ = xdr_two; /* cookie, second word */
244 *p++ = xdr_two; /* entry len */
245 memcpy(p, "..\0\0", 4); /* entry */
246 p++;
247 *p++ = xdr_one; /* bitmap length */
248 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
249 *p++ = htonl(8); /* attribute buffer length */
250 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
252 readdir->pgbase = (char *)p - (char *)start;
253 readdir->count -= readdir->pgbase;
254 kunmap_atomic(start);
257 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
259 int res = 0;
261 might_sleep();
263 if (*timeout <= 0)
264 *timeout = NFS4_POLL_RETRY_MIN;
265 if (*timeout > NFS4_POLL_RETRY_MAX)
266 *timeout = NFS4_POLL_RETRY_MAX;
267 freezable_schedule_timeout_killable(*timeout);
268 if (fatal_signal_pending(current))
269 res = -ERESTARTSYS;
270 *timeout <<= 1;
271 return res;
274 /* This is the error handling routine for processes that are allowed
275 * to sleep.
277 static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
279 struct nfs_client *clp = server->nfs_client;
280 struct nfs4_state *state = exception->state;
281 struct inode *inode = exception->inode;
282 int ret = errorcode;
284 exception->retry = 0;
285 switch(errorcode) {
286 case 0:
287 return 0;
288 case -NFS4ERR_OPENMODE:
289 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
290 nfs4_inode_return_delegation(inode);
291 exception->retry = 1;
292 return 0;
294 if (state == NULL)
295 break;
296 nfs4_schedule_stateid_recovery(server, state);
297 goto wait_on_recovery;
298 case -NFS4ERR_DELEG_REVOKED:
299 case -NFS4ERR_ADMIN_REVOKED:
300 case -NFS4ERR_BAD_STATEID:
301 if (state == NULL)
302 break;
303 nfs_remove_bad_delegation(state->inode);
304 nfs4_schedule_stateid_recovery(server, state);
305 goto wait_on_recovery;
306 case -NFS4ERR_EXPIRED:
307 if (state != NULL)
308 nfs4_schedule_stateid_recovery(server, state);
309 case -NFS4ERR_STALE_STATEID:
310 case -NFS4ERR_STALE_CLIENTID:
311 nfs4_schedule_lease_recovery(clp);
312 goto wait_on_recovery;
313 #if defined(CONFIG_NFS_V4_1)
314 case -NFS4ERR_BADSESSION:
315 case -NFS4ERR_BADSLOT:
316 case -NFS4ERR_BAD_HIGH_SLOT:
317 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
318 case -NFS4ERR_DEADSESSION:
319 case -NFS4ERR_SEQ_FALSE_RETRY:
320 case -NFS4ERR_SEQ_MISORDERED:
321 dprintk("%s ERROR: %d Reset session\n", __func__,
322 errorcode);
323 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
324 goto wait_on_recovery;
325 #endif /* defined(CONFIG_NFS_V4_1) */
326 case -NFS4ERR_FILE_OPEN:
327 if (exception->timeout > HZ) {
328 /* We have retried a decent amount, time to
329 * fail
331 ret = -EBUSY;
332 break;
334 case -NFS4ERR_GRACE:
335 case -NFS4ERR_DELAY:
336 ret = nfs4_delay(server->client, &exception->timeout);
337 if (ret != 0)
338 break;
339 case -NFS4ERR_RETRY_UNCACHED_REP:
340 case -NFS4ERR_OLD_STATEID:
341 exception->retry = 1;
342 break;
343 case -NFS4ERR_BADOWNER:
344 /* The following works around a Linux server bug! */
345 case -NFS4ERR_BADNAME:
346 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
347 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
348 exception->retry = 1;
349 printk(KERN_WARNING "NFS: v4 server %s "
350 "does not accept raw "
351 "uid/gids. "
352 "Reenabling the idmapper.\n",
353 server->nfs_client->cl_hostname);
356 /* We failed to handle the error */
357 return nfs4_map_errors(ret);
358 wait_on_recovery:
359 ret = nfs4_wait_clnt_recover(clp);
360 if (ret == 0)
361 exception->retry = 1;
362 return ret;
366 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
368 spin_lock(&clp->cl_lock);
369 if (time_before(clp->cl_last_renewal,timestamp))
370 clp->cl_last_renewal = timestamp;
371 spin_unlock(&clp->cl_lock);
374 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
376 do_renew_lease(server->nfs_client, timestamp);
379 #if defined(CONFIG_NFS_V4_1)
381 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
383 struct nfs4_session *session;
384 struct nfs4_slot_table *tbl;
385 bool send_new_highest_used_slotid = false;
387 if (!res->sr_slot) {
388 /* just wake up the next guy waiting since
389 * we may have not consumed a slot after all */
390 dprintk("%s: No slot\n", __func__);
391 return;
393 tbl = res->sr_slot->table;
394 session = tbl->session;
396 spin_lock(&tbl->slot_tbl_lock);
397 /* Be nice to the server: try to ensure that the last transmitted
398 * value for highest_user_slotid <= target_highest_slotid
400 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
401 send_new_highest_used_slotid = true;
403 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
404 send_new_highest_used_slotid = false;
405 goto out_unlock;
407 nfs4_free_slot(tbl, res->sr_slot);
409 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
410 send_new_highest_used_slotid = false;
411 out_unlock:
412 spin_unlock(&tbl->slot_tbl_lock);
413 res->sr_slot = NULL;
414 if (send_new_highest_used_slotid)
415 nfs41_server_notify_highest_slotid_update(session->clp);
418 static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
420 struct nfs4_session *session;
421 struct nfs4_slot *slot;
422 struct nfs_client *clp;
423 bool interrupted = false;
424 int ret = 1;
426 /* don't increment the sequence number if the task wasn't sent */
427 if (!RPC_WAS_SENT(task))
428 goto out;
430 slot = res->sr_slot;
431 session = slot->table->session;
433 if (slot->interrupted) {
434 slot->interrupted = 0;
435 interrupted = true;
438 /* Check the SEQUENCE operation status */
439 switch (res->sr_status) {
440 case 0:
441 /* Update the slot's sequence and clientid lease timer */
442 ++slot->seq_nr;
443 clp = session->clp;
444 do_renew_lease(clp, res->sr_timestamp);
445 /* Check sequence flags */
446 if (res->sr_status_flags != 0)
447 nfs4_schedule_lease_recovery(clp);
448 nfs41_update_target_slotid(slot->table, slot, res);
449 break;
450 case 1:
452 * sr_status remains 1 if an RPC level error occurred.
453 * The server may or may not have processed the sequence
454 * operation..
455 * Mark the slot as having hosted an interrupted RPC call.
457 slot->interrupted = 1;
458 goto out;
459 case -NFS4ERR_DELAY:
460 /* The server detected a resend of the RPC call and
461 * returned NFS4ERR_DELAY as per Section 2.10.6.2
462 * of RFC5661.
464 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
465 __func__,
466 slot->slot_nr,
467 slot->seq_nr);
468 goto out_retry;
469 case -NFS4ERR_BADSLOT:
471 * The slot id we used was probably retired. Try again
472 * using a different slot id.
474 goto retry_nowait;
475 case -NFS4ERR_SEQ_MISORDERED:
477 * Was the last operation on this sequence interrupted?
478 * If so, retry after bumping the sequence number.
480 if (interrupted) {
481 ++slot->seq_nr;
482 goto retry_nowait;
485 * Could this slot have been previously retired?
486 * If so, then the server may be expecting seq_nr = 1!
488 if (slot->seq_nr != 1) {
489 slot->seq_nr = 1;
490 goto retry_nowait;
492 break;
493 case -NFS4ERR_SEQ_FALSE_RETRY:
494 ++slot->seq_nr;
495 goto retry_nowait;
496 default:
497 /* Just update the slot sequence no. */
498 ++slot->seq_nr;
500 out:
501 /* The session may be reset by one of the error handlers. */
502 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
503 nfs41_sequence_free_slot(res);
504 return ret;
505 retry_nowait:
506 if (rpc_restart_call_prepare(task)) {
507 task->tk_status = 0;
508 ret = 0;
510 goto out;
511 out_retry:
512 if (!rpc_restart_call(task))
513 goto out;
514 rpc_delay(task, NFS4_POLL_RETRY_MAX);
515 return 0;
518 static int nfs4_sequence_done(struct rpc_task *task,
519 struct nfs4_sequence_res *res)
521 if (res->sr_slot == NULL)
522 return 1;
523 return nfs41_sequence_done(task, res);
526 static void nfs41_init_sequence(struct nfs4_sequence_args *args,
527 struct nfs4_sequence_res *res, int cache_reply)
529 args->sa_slot = NULL;
530 args->sa_cache_this = 0;
531 args->sa_privileged = 0;
532 if (cache_reply)
533 args->sa_cache_this = 1;
534 res->sr_slot = NULL;
537 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
539 args->sa_privileged = 1;
542 int nfs41_setup_sequence(struct nfs4_session *session,
543 struct nfs4_sequence_args *args,
544 struct nfs4_sequence_res *res,
545 struct rpc_task *task)
547 struct nfs4_slot *slot;
548 struct nfs4_slot_table *tbl;
550 dprintk("--> %s\n", __func__);
551 /* slot already allocated? */
552 if (res->sr_slot != NULL)
553 goto out_success;
555 tbl = &session->fc_slot_table;
557 task->tk_timeout = 0;
559 spin_lock(&tbl->slot_tbl_lock);
560 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
561 !args->sa_privileged) {
562 /* The state manager will wait until the slot table is empty */
563 dprintk("%s session is draining\n", __func__);
564 goto out_sleep;
567 slot = nfs4_alloc_slot(tbl);
568 if (IS_ERR(slot)) {
569 /* If out of memory, try again in 1/4 second */
570 if (slot == ERR_PTR(-ENOMEM))
571 task->tk_timeout = HZ >> 2;
572 dprintk("<-- %s: no free slots\n", __func__);
573 goto out_sleep;
575 spin_unlock(&tbl->slot_tbl_lock);
577 args->sa_slot = slot;
579 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
580 slot->slot_nr, slot->seq_nr);
582 res->sr_slot = slot;
583 res->sr_timestamp = jiffies;
584 res->sr_status_flags = 0;
586 * sr_status is only set in decode_sequence, and so will remain
587 * set to 1 if an rpc level failure occurs.
589 res->sr_status = 1;
590 out_success:
591 rpc_call_start(task);
592 return 0;
593 out_sleep:
594 /* Privileged tasks are queued with top priority */
595 if (args->sa_privileged)
596 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
597 NULL, RPC_PRIORITY_PRIVILEGED);
598 else
599 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
600 spin_unlock(&tbl->slot_tbl_lock);
601 return -EAGAIN;
603 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
605 int nfs4_setup_sequence(const struct nfs_server *server,
606 struct nfs4_sequence_args *args,
607 struct nfs4_sequence_res *res,
608 struct rpc_task *task)
610 struct nfs4_session *session = nfs4_get_session(server);
611 int ret = 0;
613 if (session == NULL) {
614 rpc_call_start(task);
615 goto out;
618 dprintk("--> %s clp %p session %p sr_slot %d\n",
619 __func__, session->clp, session, res->sr_slot ?
620 res->sr_slot->slot_nr : -1);
622 ret = nfs41_setup_sequence(session, args, res, task);
623 out:
624 dprintk("<-- %s status=%d\n", __func__, ret);
625 return ret;
628 struct nfs41_call_sync_data {
629 const struct nfs_server *seq_server;
630 struct nfs4_sequence_args *seq_args;
631 struct nfs4_sequence_res *seq_res;
634 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
636 struct nfs41_call_sync_data *data = calldata;
637 struct nfs4_session *session = nfs4_get_session(data->seq_server);
639 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
641 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
644 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
646 struct nfs41_call_sync_data *data = calldata;
648 nfs41_sequence_done(task, data->seq_res);
651 static const struct rpc_call_ops nfs41_call_sync_ops = {
652 .rpc_call_prepare = nfs41_call_sync_prepare,
653 .rpc_call_done = nfs41_call_sync_done,
656 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
657 struct nfs_server *server,
658 struct rpc_message *msg,
659 struct nfs4_sequence_args *args,
660 struct nfs4_sequence_res *res)
662 int ret;
663 struct rpc_task *task;
664 struct nfs41_call_sync_data data = {
665 .seq_server = server,
666 .seq_args = args,
667 .seq_res = res,
669 struct rpc_task_setup task_setup = {
670 .rpc_client = clnt,
671 .rpc_message = msg,
672 .callback_ops = &nfs41_call_sync_ops,
673 .callback_data = &data
676 task = rpc_run_task(&task_setup);
677 if (IS_ERR(task))
678 ret = PTR_ERR(task);
679 else {
680 ret = task->tk_status;
681 rpc_put_task(task);
683 return ret;
686 #else
687 static
688 void nfs41_init_sequence(struct nfs4_sequence_args *args,
689 struct nfs4_sequence_res *res, int cache_reply)
693 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
698 static int nfs4_sequence_done(struct rpc_task *task,
699 struct nfs4_sequence_res *res)
701 return 1;
703 #endif /* CONFIG_NFS_V4_1 */
705 static
706 int _nfs4_call_sync(struct rpc_clnt *clnt,
707 struct nfs_server *server,
708 struct rpc_message *msg,
709 struct nfs4_sequence_args *args,
710 struct nfs4_sequence_res *res)
712 return rpc_call_sync(clnt, msg, 0);
715 static
716 int nfs4_call_sync(struct rpc_clnt *clnt,
717 struct nfs_server *server,
718 struct rpc_message *msg,
719 struct nfs4_sequence_args *args,
720 struct nfs4_sequence_res *res,
721 int cache_reply)
723 nfs41_init_sequence(args, res, cache_reply);
724 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
725 args, res);
728 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
730 struct nfs_inode *nfsi = NFS_I(dir);
732 spin_lock(&dir->i_lock);
733 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
734 if (!cinfo->atomic || cinfo->before != dir->i_version)
735 nfs_force_lookup_revalidate(dir);
736 dir->i_version = cinfo->after;
737 nfs_fscache_invalidate(dir);
738 spin_unlock(&dir->i_lock);
741 struct nfs4_opendata {
742 struct kref kref;
743 struct nfs_openargs o_arg;
744 struct nfs_openres o_res;
745 struct nfs_open_confirmargs c_arg;
746 struct nfs_open_confirmres c_res;
747 struct nfs4_string owner_name;
748 struct nfs4_string group_name;
749 struct nfs_fattr f_attr;
750 struct dentry *dir;
751 struct dentry *dentry;
752 struct nfs4_state_owner *owner;
753 struct nfs4_state *state;
754 struct iattr attrs;
755 unsigned long timestamp;
756 unsigned int rpc_done : 1;
757 int rpc_status;
758 int cancelled;
762 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
764 p->o_res.f_attr = &p->f_attr;
765 p->o_res.seqid = p->o_arg.seqid;
766 p->c_res.seqid = p->c_arg.seqid;
767 p->o_res.server = p->o_arg.server;
768 p->o_res.access_request = p->o_arg.access;
769 nfs_fattr_init(&p->f_attr);
770 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
773 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
774 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
775 const struct iattr *attrs,
776 gfp_t gfp_mask)
778 struct dentry *parent = dget_parent(dentry);
779 struct inode *dir = parent->d_inode;
780 struct nfs_server *server = NFS_SERVER(dir);
781 struct nfs4_opendata *p;
783 p = kzalloc(sizeof(*p), gfp_mask);
784 if (p == NULL)
785 goto err;
786 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
787 if (p->o_arg.seqid == NULL)
788 goto err_free;
789 nfs_sb_active(dentry->d_sb);
790 p->dentry = dget(dentry);
791 p->dir = parent;
792 p->owner = sp;
793 atomic_inc(&sp->so_count);
794 p->o_arg.fh = NFS_FH(dir);
795 p->o_arg.open_flags = flags;
796 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
797 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
798 * will return permission denied for all bits until close */
799 if (!(flags & O_EXCL)) {
800 /* ask server to check for all possible rights as results
801 * are cached */
802 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
803 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
805 p->o_arg.clientid = server->nfs_client->cl_clientid;
806 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
807 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
808 p->o_arg.name = &dentry->d_name;
809 p->o_arg.server = server;
810 p->o_arg.bitmask = server->attr_bitmask;
811 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
812 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
813 if (attrs != NULL && attrs->ia_valid != 0) {
814 __be32 verf[2];
816 p->o_arg.u.attrs = &p->attrs;
817 memcpy(&p->attrs, attrs, sizeof(p->attrs));
819 verf[0] = jiffies;
820 verf[1] = current->pid;
821 memcpy(p->o_arg.u.verifier.data, verf,
822 sizeof(p->o_arg.u.verifier.data));
824 p->c_arg.fh = &p->o_res.fh;
825 p->c_arg.stateid = &p->o_res.stateid;
826 p->c_arg.seqid = p->o_arg.seqid;
827 nfs4_init_opendata_res(p);
828 kref_init(&p->kref);
829 return p;
830 err_free:
831 kfree(p);
832 err:
833 dput(parent);
834 return NULL;
837 static void nfs4_opendata_free(struct kref *kref)
839 struct nfs4_opendata *p = container_of(kref,
840 struct nfs4_opendata, kref);
841 struct super_block *sb = p->dentry->d_sb;
843 nfs_free_seqid(p->o_arg.seqid);
844 if (p->state != NULL)
845 nfs4_put_open_state(p->state);
846 nfs4_put_state_owner(p->owner);
847 dput(p->dir);
848 dput(p->dentry);
849 nfs_sb_deactive(sb);
850 nfs_fattr_free_names(&p->f_attr);
851 kfree(p);
854 static void nfs4_opendata_put(struct nfs4_opendata *p)
856 if (p != NULL)
857 kref_put(&p->kref, nfs4_opendata_free);
860 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
862 int ret;
864 ret = rpc_wait_for_completion_task(task);
865 return ret;
868 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
870 int ret = 0;
872 if (open_mode & (O_EXCL|O_TRUNC))
873 goto out;
874 switch (mode & (FMODE_READ|FMODE_WRITE)) {
875 case FMODE_READ:
876 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
877 && state->n_rdonly != 0;
878 break;
879 case FMODE_WRITE:
880 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
881 && state->n_wronly != 0;
882 break;
883 case FMODE_READ|FMODE_WRITE:
884 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
885 && state->n_rdwr != 0;
887 out:
888 return ret;
891 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
893 if (delegation == NULL)
894 return 0;
895 if ((delegation->type & fmode) != fmode)
896 return 0;
897 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
898 return 0;
899 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
900 return 0;
901 nfs_mark_delegation_referenced(delegation);
902 return 1;
905 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
907 switch (fmode) {
908 case FMODE_WRITE:
909 state->n_wronly++;
910 break;
911 case FMODE_READ:
912 state->n_rdonly++;
913 break;
914 case FMODE_READ|FMODE_WRITE:
915 state->n_rdwr++;
917 nfs4_state_set_mode_locked(state, state->state | fmode);
920 static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
922 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
923 nfs4_stateid_copy(&state->stateid, stateid);
924 nfs4_stateid_copy(&state->open_stateid, stateid);
925 switch (fmode) {
926 case FMODE_READ:
927 set_bit(NFS_O_RDONLY_STATE, &state->flags);
928 break;
929 case FMODE_WRITE:
930 set_bit(NFS_O_WRONLY_STATE, &state->flags);
931 break;
932 case FMODE_READ|FMODE_WRITE:
933 set_bit(NFS_O_RDWR_STATE, &state->flags);
937 static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
939 write_seqlock(&state->seqlock);
940 nfs_set_open_stateid_locked(state, stateid, fmode);
941 write_sequnlock(&state->seqlock);
944 static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
947 * Protect the call to nfs4_state_set_mode_locked and
948 * serialise the stateid update
950 write_seqlock(&state->seqlock);
951 if (deleg_stateid != NULL) {
952 nfs4_stateid_copy(&state->stateid, deleg_stateid);
953 set_bit(NFS_DELEGATED_STATE, &state->flags);
955 if (open_stateid != NULL)
956 nfs_set_open_stateid_locked(state, open_stateid, fmode);
957 write_sequnlock(&state->seqlock);
958 spin_lock(&state->owner->so_lock);
959 update_open_stateflags(state, fmode);
960 spin_unlock(&state->owner->so_lock);
963 static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
965 struct nfs_inode *nfsi = NFS_I(state->inode);
966 struct nfs_delegation *deleg_cur;
967 int ret = 0;
969 fmode &= (FMODE_READ|FMODE_WRITE);
971 rcu_read_lock();
972 deleg_cur = rcu_dereference(nfsi->delegation);
973 if (deleg_cur == NULL)
974 goto no_delegation;
976 spin_lock(&deleg_cur->lock);
977 if (nfsi->delegation != deleg_cur ||
978 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
979 (deleg_cur->type & fmode) != fmode)
980 goto no_delegation_unlock;
982 if (delegation == NULL)
983 delegation = &deleg_cur->stateid;
984 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
985 goto no_delegation_unlock;
987 nfs_mark_delegation_referenced(deleg_cur);
988 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
989 ret = 1;
990 no_delegation_unlock:
991 spin_unlock(&deleg_cur->lock);
992 no_delegation:
993 rcu_read_unlock();
995 if (!ret && open_stateid != NULL) {
996 __update_open_stateid(state, open_stateid, NULL, fmode);
997 ret = 1;
1000 return ret;
1004 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1006 struct nfs_delegation *delegation;
1008 rcu_read_lock();
1009 delegation = rcu_dereference(NFS_I(inode)->delegation);
1010 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1011 rcu_read_unlock();
1012 return;
1014 rcu_read_unlock();
1015 nfs4_inode_return_delegation(inode);
1018 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1020 struct nfs4_state *state = opendata->state;
1021 struct nfs_inode *nfsi = NFS_I(state->inode);
1022 struct nfs_delegation *delegation;
1023 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
1024 fmode_t fmode = opendata->o_arg.fmode;
1025 nfs4_stateid stateid;
1026 int ret = -EAGAIN;
1028 for (;;) {
1029 if (can_open_cached(state, fmode, open_mode)) {
1030 spin_lock(&state->owner->so_lock);
1031 if (can_open_cached(state, fmode, open_mode)) {
1032 update_open_stateflags(state, fmode);
1033 spin_unlock(&state->owner->so_lock);
1034 goto out_return_state;
1036 spin_unlock(&state->owner->so_lock);
1038 rcu_read_lock();
1039 delegation = rcu_dereference(nfsi->delegation);
1040 if (!can_open_delegated(delegation, fmode)) {
1041 rcu_read_unlock();
1042 break;
1044 /* Save the delegation */
1045 nfs4_stateid_copy(&stateid, &delegation->stateid);
1046 rcu_read_unlock();
1047 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1048 if (ret != 0)
1049 goto out;
1050 ret = -EAGAIN;
1052 /* Try to update the stateid using the delegation */
1053 if (update_open_stateid(state, NULL, &stateid, fmode))
1054 goto out_return_state;
1056 out:
1057 return ERR_PTR(ret);
1058 out_return_state:
1059 atomic_inc(&state->count);
1060 return state;
1063 static void
1064 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1066 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1067 struct nfs_delegation *delegation;
1068 int delegation_flags = 0;
1070 rcu_read_lock();
1071 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1072 if (delegation)
1073 delegation_flags = delegation->flags;
1074 rcu_read_unlock();
1075 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1076 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1077 "returning a delegation for "
1078 "OPEN(CLAIM_DELEGATE_CUR)\n",
1079 clp->cl_hostname);
1080 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1081 nfs_inode_set_delegation(state->inode,
1082 data->owner->so_cred,
1083 &data->o_res);
1084 else
1085 nfs_inode_reclaim_delegation(state->inode,
1086 data->owner->so_cred,
1087 &data->o_res);
1091 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1092 * and update the nfs4_state.
1094 static struct nfs4_state *
1095 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1097 struct inode *inode = data->state->inode;
1098 struct nfs4_state *state = data->state;
1099 int ret;
1101 if (!data->rpc_done) {
1102 ret = data->rpc_status;
1103 goto err;
1106 ret = -ESTALE;
1107 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1108 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1109 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1110 goto err;
1112 ret = -ENOMEM;
1113 state = nfs4_get_open_state(inode, data->owner);
1114 if (state == NULL)
1115 goto err;
1117 ret = nfs_refresh_inode(inode, &data->f_attr);
1118 if (ret)
1119 goto err;
1121 if (data->o_res.delegation_type != 0)
1122 nfs4_opendata_check_deleg(data, state);
1123 update_open_stateid(state, &data->o_res.stateid, NULL,
1124 data->o_arg.fmode);
1126 return state;
1127 err:
1128 return ERR_PTR(ret);
1132 static struct nfs4_state *
1133 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1135 struct inode *inode;
1136 struct nfs4_state *state = NULL;
1137 int ret;
1139 if (!data->rpc_done) {
1140 state = nfs4_try_open_cached(data);
1141 goto out;
1144 ret = -EAGAIN;
1145 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1146 goto err;
1147 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
1148 ret = PTR_ERR(inode);
1149 if (IS_ERR(inode))
1150 goto err;
1151 ret = -ENOMEM;
1152 state = nfs4_get_open_state(inode, data->owner);
1153 if (state == NULL)
1154 goto err_put_inode;
1155 if (data->o_res.delegation_type != 0)
1156 nfs4_opendata_check_deleg(data, state);
1157 update_open_stateid(state, &data->o_res.stateid, NULL,
1158 data->o_arg.fmode);
1159 iput(inode);
1160 out:
1161 return state;
1162 err_put_inode:
1163 iput(inode);
1164 err:
1165 return ERR_PTR(ret);
1168 static struct nfs4_state *
1169 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1171 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1172 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1173 return _nfs4_opendata_to_nfs4_state(data);
1176 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1178 struct nfs_inode *nfsi = NFS_I(state->inode);
1179 struct nfs_open_context *ctx;
1181 spin_lock(&state->inode->i_lock);
1182 list_for_each_entry(ctx, &nfsi->open_files, list) {
1183 if (ctx->state != state)
1184 continue;
1185 get_nfs_open_context(ctx);
1186 spin_unlock(&state->inode->i_lock);
1187 return ctx;
1189 spin_unlock(&state->inode->i_lock);
1190 return ERR_PTR(-ENOENT);
1193 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1195 struct nfs4_opendata *opendata;
1197 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
1198 if (opendata == NULL)
1199 return ERR_PTR(-ENOMEM);
1200 opendata->state = state;
1201 atomic_inc(&state->count);
1202 return opendata;
1205 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
1207 struct nfs4_state *newstate;
1208 int ret;
1210 opendata->o_arg.open_flags = 0;
1211 opendata->o_arg.fmode = fmode;
1212 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1213 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1214 nfs4_init_opendata_res(opendata);
1215 ret = _nfs4_recover_proc_open(opendata);
1216 if (ret != 0)
1217 return ret;
1218 newstate = nfs4_opendata_to_nfs4_state(opendata);
1219 if (IS_ERR(newstate))
1220 return PTR_ERR(newstate);
1221 nfs4_close_state(newstate, fmode);
1222 *res = newstate;
1223 return 0;
1226 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1228 struct nfs4_state *newstate;
1229 int ret;
1231 /* memory barrier prior to reading state->n_* */
1232 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1233 smp_rmb();
1234 if (state->n_rdwr != 0) {
1235 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1236 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
1237 if (ret != 0)
1238 return ret;
1239 if (newstate != state)
1240 return -ESTALE;
1242 if (state->n_wronly != 0) {
1243 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1244 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
1245 if (ret != 0)
1246 return ret;
1247 if (newstate != state)
1248 return -ESTALE;
1250 if (state->n_rdonly != 0) {
1251 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1252 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
1253 if (ret != 0)
1254 return ret;
1255 if (newstate != state)
1256 return -ESTALE;
1259 * We may have performed cached opens for all three recoveries.
1260 * Check if we need to update the current stateid.
1262 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1263 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1264 write_seqlock(&state->seqlock);
1265 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1266 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1267 write_sequnlock(&state->seqlock);
1269 return 0;
1273 * OPEN_RECLAIM:
1274 * reclaim state on the server after a reboot.
1276 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1278 struct nfs_delegation *delegation;
1279 struct nfs4_opendata *opendata;
1280 fmode_t delegation_type = 0;
1281 int status;
1283 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1284 if (IS_ERR(opendata))
1285 return PTR_ERR(opendata);
1286 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1287 opendata->o_arg.fh = NFS_FH(state->inode);
1288 rcu_read_lock();
1289 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1290 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1291 delegation_type = delegation->type;
1292 rcu_read_unlock();
1293 opendata->o_arg.u.delegation_type = delegation_type;
1294 status = nfs4_open_recover(opendata, state);
1295 nfs4_opendata_put(opendata);
1296 return status;
1299 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1301 struct nfs_server *server = NFS_SERVER(state->inode);
1302 struct nfs4_exception exception = { };
1303 int err;
1304 do {
1305 err = _nfs4_do_open_reclaim(ctx, state);
1306 if (err != -NFS4ERR_DELAY)
1307 break;
1308 nfs4_handle_exception(server, err, &exception);
1309 } while (exception.retry);
1310 return err;
1313 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1315 struct nfs_open_context *ctx;
1316 int ret;
1318 ctx = nfs4_state_find_open_context(state);
1319 if (IS_ERR(ctx))
1320 return PTR_ERR(ctx);
1321 ret = nfs4_do_open_reclaim(ctx, state);
1322 put_nfs_open_context(ctx);
1323 return ret;
1326 static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1328 struct nfs4_opendata *opendata;
1329 int ret;
1331 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1332 if (IS_ERR(opendata))
1333 return PTR_ERR(opendata);
1334 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
1335 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1336 ret = nfs4_open_recover(opendata, state);
1337 nfs4_opendata_put(opendata);
1338 return ret;
1341 int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1343 struct nfs4_exception exception = { };
1344 struct nfs_server *server = NFS_SERVER(state->inode);
1345 int err;
1346 do {
1347 err = _nfs4_open_delegation_recall(ctx, state, stateid);
1348 switch (err) {
1349 case 0:
1350 case -ENOENT:
1351 case -ESTALE:
1352 goto out;
1353 case -NFS4ERR_BADSESSION:
1354 case -NFS4ERR_BADSLOT:
1355 case -NFS4ERR_BAD_HIGH_SLOT:
1356 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1357 case -NFS4ERR_DEADSESSION:
1358 set_bit(NFS_DELEGATED_STATE, &state->flags);
1359 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1360 err = -EAGAIN;
1361 goto out;
1362 case -NFS4ERR_STALE_CLIENTID:
1363 case -NFS4ERR_STALE_STATEID:
1364 set_bit(NFS_DELEGATED_STATE, &state->flags);
1365 case -NFS4ERR_EXPIRED:
1366 /* Don't recall a delegation if it was lost */
1367 nfs4_schedule_lease_recovery(server->nfs_client);
1368 err = -EAGAIN;
1369 goto out;
1370 case -NFS4ERR_DELEG_REVOKED:
1371 case -NFS4ERR_ADMIN_REVOKED:
1372 case -NFS4ERR_BAD_STATEID:
1373 nfs_inode_find_state_and_recover(state->inode,
1374 stateid);
1375 nfs4_schedule_stateid_recovery(server, state);
1376 case -ENOMEM:
1377 err = 0;
1378 goto out;
1380 set_bit(NFS_DELEGATED_STATE, &state->flags);
1381 err = nfs4_handle_exception(server, err, &exception);
1382 } while (exception.retry);
1383 out:
1384 return err;
1387 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1389 struct nfs4_opendata *data = calldata;
1391 data->rpc_status = task->tk_status;
1392 if (data->rpc_status == 0) {
1393 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
1394 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1395 renew_lease(data->o_res.server, data->timestamp);
1396 data->rpc_done = 1;
1400 static void nfs4_open_confirm_release(void *calldata)
1402 struct nfs4_opendata *data = calldata;
1403 struct nfs4_state *state = NULL;
1405 /* If this request hasn't been cancelled, do nothing */
1406 if (data->cancelled == 0)
1407 goto out_free;
1408 /* In case of error, no cleanup! */
1409 if (!data->rpc_done)
1410 goto out_free;
1411 state = nfs4_opendata_to_nfs4_state(data);
1412 if (!IS_ERR(state))
1413 nfs4_close_state(state, data->o_arg.fmode);
1414 out_free:
1415 nfs4_opendata_put(data);
1418 static const struct rpc_call_ops nfs4_open_confirm_ops = {
1419 .rpc_call_done = nfs4_open_confirm_done,
1420 .rpc_release = nfs4_open_confirm_release,
1424 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1426 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1428 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1429 struct rpc_task *task;
1430 struct rpc_message msg = {
1431 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1432 .rpc_argp = &data->c_arg,
1433 .rpc_resp = &data->c_res,
1434 .rpc_cred = data->owner->so_cred,
1436 struct rpc_task_setup task_setup_data = {
1437 .rpc_client = server->client,
1438 .rpc_message = &msg,
1439 .callback_ops = &nfs4_open_confirm_ops,
1440 .callback_data = data,
1441 .workqueue = nfsiod_workqueue,
1442 .flags = RPC_TASK_ASYNC,
1444 int status;
1446 kref_get(&data->kref);
1447 data->rpc_done = 0;
1448 data->rpc_status = 0;
1449 data->timestamp = jiffies;
1450 task = rpc_run_task(&task_setup_data);
1451 if (IS_ERR(task))
1452 return PTR_ERR(task);
1453 status = nfs4_wait_for_completion_rpc_task(task);
1454 if (status != 0) {
1455 data->cancelled = 1;
1456 smp_wmb();
1457 } else
1458 status = data->rpc_status;
1459 rpc_put_task(task);
1460 return status;
1463 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
1465 struct nfs4_opendata *data = calldata;
1466 struct nfs4_state_owner *sp = data->owner;
1468 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1469 goto out_wait;
1471 * Check if we still need to send an OPEN call, or if we can use
1472 * a delegation instead.
1474 if (data->state != NULL) {
1475 struct nfs_delegation *delegation;
1477 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
1478 goto out_no_action;
1479 rcu_read_lock();
1480 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1481 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1482 can_open_delegated(delegation, data->o_arg.fmode))
1483 goto unlock_no_action;
1484 rcu_read_unlock();
1486 /* Update client id. */
1487 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
1488 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
1489 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
1490 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
1491 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1493 data->timestamp = jiffies;
1494 if (nfs4_setup_sequence(data->o_arg.server,
1495 &data->o_arg.seq_args,
1496 &data->o_res.seq_res,
1497 task) != 0)
1498 nfs_release_seqid(data->o_arg.seqid);
1499 return;
1500 unlock_no_action:
1501 rcu_read_unlock();
1502 out_no_action:
1503 task->tk_action = NULL;
1504 out_wait:
1505 nfs4_sequence_done(task, &data->o_res.seq_res);
1508 static void nfs4_open_done(struct rpc_task *task, void *calldata)
1510 struct nfs4_opendata *data = calldata;
1512 data->rpc_status = task->tk_status;
1514 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1515 return;
1517 if (task->tk_status == 0) {
1518 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1519 switch (data->o_res.f_attr->mode & S_IFMT) {
1520 case S_IFREG:
1521 break;
1522 case S_IFLNK:
1523 data->rpc_status = -ELOOP;
1524 break;
1525 case S_IFDIR:
1526 data->rpc_status = -EISDIR;
1527 break;
1528 default:
1529 data->rpc_status = -ENOTDIR;
1532 renew_lease(data->o_res.server, data->timestamp);
1533 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1534 nfs_confirm_seqid(&data->owner->so_seqid, 0);
1536 data->rpc_done = 1;
1539 static void nfs4_open_release(void *calldata)
1541 struct nfs4_opendata *data = calldata;
1542 struct nfs4_state *state = NULL;
1544 /* If this request hasn't been cancelled, do nothing */
1545 if (data->cancelled == 0)
1546 goto out_free;
1547 /* In case of error, no cleanup! */
1548 if (data->rpc_status != 0 || !data->rpc_done)
1549 goto out_free;
1550 /* In case we need an open_confirm, no cleanup! */
1551 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1552 goto out_free;
1553 state = nfs4_opendata_to_nfs4_state(data);
1554 if (!IS_ERR(state))
1555 nfs4_close_state(state, data->o_arg.fmode);
1556 out_free:
1557 nfs4_opendata_put(data);
1560 static const struct rpc_call_ops nfs4_open_ops = {
1561 .rpc_call_prepare = nfs4_open_prepare,
1562 .rpc_call_done = nfs4_open_done,
1563 .rpc_release = nfs4_open_release,
1566 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
1568 struct inode *dir = data->dir->d_inode;
1569 struct nfs_server *server = NFS_SERVER(dir);
1570 struct nfs_openargs *o_arg = &data->o_arg;
1571 struct nfs_openres *o_res = &data->o_res;
1572 struct rpc_task *task;
1573 struct rpc_message msg = {
1574 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1575 .rpc_argp = o_arg,
1576 .rpc_resp = o_res,
1577 .rpc_cred = data->owner->so_cred,
1579 struct rpc_task_setup task_setup_data = {
1580 .rpc_client = server->client,
1581 .rpc_message = &msg,
1582 .callback_ops = &nfs4_open_ops,
1583 .callback_data = data,
1584 .workqueue = nfsiod_workqueue,
1585 .flags = RPC_TASK_ASYNC,
1587 int status;
1589 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
1590 kref_get(&data->kref);
1591 data->rpc_done = 0;
1592 data->rpc_status = 0;
1593 data->cancelled = 0;
1594 if (isrecover)
1595 nfs4_set_sequence_privileged(&o_arg->seq_args);
1596 task = rpc_run_task(&task_setup_data);
1597 if (IS_ERR(task))
1598 return PTR_ERR(task);
1599 status = nfs4_wait_for_completion_rpc_task(task);
1600 if (status != 0) {
1601 data->cancelled = 1;
1602 smp_wmb();
1603 } else
1604 status = data->rpc_status;
1605 rpc_put_task(task);
1607 return status;
1610 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1612 struct inode *dir = data->dir->d_inode;
1613 struct nfs_openres *o_res = &data->o_res;
1614 int status;
1616 status = nfs4_run_open_task(data, 1);
1617 if (status != 0 || !data->rpc_done)
1618 return status;
1620 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1622 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1623 status = _nfs4_proc_open_confirm(data);
1624 if (status != 0)
1625 return status;
1628 return status;
1631 static int nfs4_opendata_access(struct rpc_cred *cred,
1632 struct nfs4_opendata *opendata,
1633 struct nfs4_state *state, fmode_t fmode,
1634 int openflags)
1636 struct nfs_access_entry cache;
1637 u32 mask;
1639 /* access call failed or for some reason the server doesn't
1640 * support any access modes -- defer access call until later */
1641 if (opendata->o_res.access_supported == 0)
1642 return 0;
1644 mask = 0;
1645 /* don't check MAY_WRITE - a newly created file may not have
1646 * write mode bits, but POSIX allows the creating process to write.
1647 * use openflags to check for exec, because fmode won't
1648 * always have FMODE_EXEC set when file open for exec. */
1649 if (openflags & __FMODE_EXEC) {
1650 /* ONLY check for exec rights */
1651 mask = MAY_EXEC;
1652 } else if (fmode & FMODE_READ)
1653 mask = MAY_READ;
1655 cache.cred = cred;
1656 cache.jiffies = jiffies;
1657 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1658 nfs_access_add_cache(state->inode, &cache);
1660 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
1661 return 0;
1663 /* even though OPEN succeeded, access is denied. Close the file */
1664 nfs4_close_state(state, fmode);
1665 return -EACCES;
1669 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1671 static int _nfs4_proc_open(struct nfs4_opendata *data)
1673 struct inode *dir = data->dir->d_inode;
1674 struct nfs_server *server = NFS_SERVER(dir);
1675 struct nfs_openargs *o_arg = &data->o_arg;
1676 struct nfs_openres *o_res = &data->o_res;
1677 int status;
1679 status = nfs4_run_open_task(data, 0);
1680 if (!data->rpc_done)
1681 return status;
1682 if (status != 0) {
1683 if (status == -NFS4ERR_BADNAME &&
1684 !(o_arg->open_flags & O_CREAT))
1685 return -ENOENT;
1686 return status;
1689 nfs_fattr_map_and_free_names(server, &data->f_attr);
1691 if (o_arg->open_flags & O_CREAT)
1692 update_changeattr(dir, &o_res->cinfo);
1693 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1694 server->caps &= ~NFS_CAP_POSIX_LOCK;
1695 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1696 status = _nfs4_proc_open_confirm(data);
1697 if (status != 0)
1698 return status;
1700 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
1701 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
1702 return 0;
1705 static int nfs4_recover_expired_lease(struct nfs_server *server)
1707 return nfs4_client_recover_expired_lease(server->nfs_client);
1711 * OPEN_EXPIRED:
1712 * reclaim state on the server after a network partition.
1713 * Assumes caller holds the appropriate lock
1715 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1717 struct nfs4_opendata *opendata;
1718 int ret;
1720 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1721 if (IS_ERR(opendata))
1722 return PTR_ERR(opendata);
1723 ret = nfs4_open_recover(opendata, state);
1724 if (ret == -ESTALE)
1725 d_drop(ctx->dentry);
1726 nfs4_opendata_put(opendata);
1727 return ret;
1730 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
1732 struct nfs_server *server = NFS_SERVER(state->inode);
1733 struct nfs4_exception exception = { };
1734 int err;
1736 do {
1737 err = _nfs4_open_expired(ctx, state);
1738 switch (err) {
1739 default:
1740 goto out;
1741 case -NFS4ERR_GRACE:
1742 case -NFS4ERR_DELAY:
1743 nfs4_handle_exception(server, err, &exception);
1744 err = 0;
1746 } while (exception.retry);
1747 out:
1748 return err;
1751 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1753 struct nfs_open_context *ctx;
1754 int ret;
1756 ctx = nfs4_state_find_open_context(state);
1757 if (IS_ERR(ctx))
1758 return PTR_ERR(ctx);
1759 ret = nfs4_do_open_expired(ctx, state);
1760 put_nfs_open_context(ctx);
1761 return ret;
1764 #if defined(CONFIG_NFS_V4_1)
1765 static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
1767 struct nfs_server *server = NFS_SERVER(state->inode);
1768 nfs4_stateid *stateid = &state->stateid;
1769 int status;
1771 /* If a state reset has been done, test_stateid is unneeded */
1772 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1773 return;
1775 status = nfs41_test_stateid(server, stateid);
1776 if (status != NFS_OK) {
1777 /* Free the stateid unless the server explicitly
1778 * informs us the stateid is unrecognized. */
1779 if (status != -NFS4ERR_BAD_STATEID)
1780 nfs41_free_stateid(server, stateid);
1781 nfs_remove_bad_delegation(state->inode);
1783 write_seqlock(&state->seqlock);
1784 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1785 write_sequnlock(&state->seqlock);
1786 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1791 * nfs41_check_open_stateid - possibly free an open stateid
1793 * @state: NFSv4 state for an inode
1795 * Returns NFS_OK if recovery for this stateid is now finished.
1796 * Otherwise a negative NFS4ERR value is returned.
1798 static int nfs41_check_open_stateid(struct nfs4_state *state)
1800 struct nfs_server *server = NFS_SERVER(state->inode);
1801 nfs4_stateid *stateid = &state->open_stateid;
1802 int status;
1804 /* If a state reset has been done, test_stateid is unneeded */
1805 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1806 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1807 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1808 return -NFS4ERR_BAD_STATEID;
1810 status = nfs41_test_stateid(server, stateid);
1811 if (status != NFS_OK) {
1812 /* Free the stateid unless the server explicitly
1813 * informs us the stateid is unrecognized. */
1814 if (status != -NFS4ERR_BAD_STATEID)
1815 nfs41_free_stateid(server, stateid);
1817 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1818 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1819 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1821 return status;
1824 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1826 int status;
1828 nfs41_clear_delegation_stateid(state);
1829 status = nfs41_check_open_stateid(state);
1830 if (status != NFS_OK)
1831 status = nfs4_open_expired(sp, state);
1832 return status;
1834 #endif
1837 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1838 * fields corresponding to attributes that were used to store the verifier.
1839 * Make sure we clobber those fields in the later setattr call
1841 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1843 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1844 !(sattr->ia_valid & ATTR_ATIME_SET))
1845 sattr->ia_valid |= ATTR_ATIME;
1847 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1848 !(sattr->ia_valid & ATTR_MTIME_SET))
1849 sattr->ia_valid |= ATTR_MTIME;
1852 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
1853 fmode_t fmode,
1854 int flags,
1855 struct nfs4_state **res)
1857 struct nfs4_state_owner *sp = opendata->owner;
1858 struct nfs_server *server = sp->so_server;
1859 struct nfs4_state *state;
1860 unsigned int seq;
1861 int ret;
1863 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
1865 ret = _nfs4_proc_open(opendata);
1866 if (ret != 0)
1867 goto out;
1869 state = nfs4_opendata_to_nfs4_state(opendata);
1870 ret = PTR_ERR(state);
1871 if (IS_ERR(state))
1872 goto out;
1873 if (server->caps & NFS_CAP_POSIX_LOCK)
1874 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
1876 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
1877 if (ret != 0)
1878 goto out;
1880 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
1881 nfs4_schedule_stateid_recovery(server, state);
1882 nfs4_wait_clnt_recover(server->nfs_client);
1884 *res = state;
1885 out:
1886 return ret;
1890 * Returns a referenced nfs4_state
1892 static int _nfs4_do_open(struct inode *dir,
1893 struct dentry *dentry,
1894 fmode_t fmode,
1895 int flags,
1896 struct iattr *sattr,
1897 struct rpc_cred *cred,
1898 struct nfs4_state **res,
1899 struct nfs4_threshold **ctx_th)
1901 struct nfs4_state_owner *sp;
1902 struct nfs4_state *state = NULL;
1903 struct nfs_server *server = NFS_SERVER(dir);
1904 struct nfs4_opendata *opendata;
1905 int status;
1907 /* Protect against reboot recovery conflicts */
1908 status = -ENOMEM;
1909 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1910 if (sp == NULL) {
1911 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1912 goto out_err;
1914 status = nfs4_recover_expired_lease(server);
1915 if (status != 0)
1916 goto err_put_state_owner;
1917 if (dentry->d_inode != NULL)
1918 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
1919 status = -ENOMEM;
1920 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
1921 if (opendata == NULL)
1922 goto err_put_state_owner;
1924 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1925 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1926 if (!opendata->f_attr.mdsthreshold)
1927 goto err_opendata_put;
1928 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
1930 if (dentry->d_inode != NULL)
1931 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
1933 status = _nfs4_open_and_get_state(opendata, fmode, flags, &state);
1934 if (status != 0)
1935 goto err_opendata_put;
1937 if (opendata->o_arg.open_flags & O_EXCL) {
1938 nfs4_exclusive_attrset(opendata, sattr);
1940 nfs_fattr_init(opendata->o_res.f_attr);
1941 status = nfs4_do_setattr(state->inode, cred,
1942 opendata->o_res.f_attr, sattr,
1943 state);
1944 if (status == 0)
1945 nfs_setattr_update_inode(state->inode, sattr);
1946 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1949 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1950 *ctx_th = opendata->f_attr.mdsthreshold;
1951 else
1952 kfree(opendata->f_attr.mdsthreshold);
1953 opendata->f_attr.mdsthreshold = NULL;
1955 nfs4_opendata_put(opendata);
1956 nfs4_put_state_owner(sp);
1957 *res = state;
1958 return 0;
1959 err_opendata_put:
1960 kfree(opendata->f_attr.mdsthreshold);
1961 nfs4_opendata_put(opendata);
1962 err_put_state_owner:
1963 nfs4_put_state_owner(sp);
1964 out_err:
1965 *res = NULL;
1966 return status;
1970 static struct nfs4_state *nfs4_do_open(struct inode *dir,
1971 struct dentry *dentry,
1972 fmode_t fmode,
1973 int flags,
1974 struct iattr *sattr,
1975 struct rpc_cred *cred,
1976 struct nfs4_threshold **ctx_th)
1978 struct nfs4_exception exception = { };
1979 struct nfs4_state *res;
1980 int status;
1982 fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC;
1983 do {
1984 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1985 &res, ctx_th);
1986 if (status == 0)
1987 break;
1988 /* NOTE: BAD_SEQID means the server and client disagree about the
1989 * book-keeping w.r.t. state-changing operations
1990 * (OPEN/CLOSE/LOCK/LOCKU...)
1991 * It is actually a sign of a bug on the client or on the server.
1993 * If we receive a BAD_SEQID error in the particular case of
1994 * doing an OPEN, we assume that nfs_increment_open_seqid() will
1995 * have unhashed the old state_owner for us, and that we can
1996 * therefore safely retry using a new one. We should still warn
1997 * the user though...
1999 if (status == -NFS4ERR_BAD_SEQID) {
2000 pr_warn_ratelimited("NFS: v4 server %s "
2001 " returned a bad sequence-id error!\n",
2002 NFS_SERVER(dir)->nfs_client->cl_hostname);
2003 exception.retry = 1;
2004 continue;
2007 * BAD_STATEID on OPEN means that the server cancelled our
2008 * state before it received the OPEN_CONFIRM.
2009 * Recover by retrying the request as per the discussion
2010 * on Page 181 of RFC3530.
2012 if (status == -NFS4ERR_BAD_STATEID) {
2013 exception.retry = 1;
2014 continue;
2016 if (status == -EAGAIN) {
2017 /* We must have found a delegation */
2018 exception.retry = 1;
2019 continue;
2021 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
2022 status, &exception));
2023 } while (exception.retry);
2024 return res;
2027 static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2028 struct nfs_fattr *fattr, struct iattr *sattr,
2029 struct nfs4_state *state)
2031 struct nfs_server *server = NFS_SERVER(inode);
2032 struct nfs_setattrargs arg = {
2033 .fh = NFS_FH(inode),
2034 .iap = sattr,
2035 .server = server,
2036 .bitmask = server->attr_bitmask,
2038 struct nfs_setattrres res = {
2039 .fattr = fattr,
2040 .server = server,
2042 struct rpc_message msg = {
2043 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2044 .rpc_argp = &arg,
2045 .rpc_resp = &res,
2046 .rpc_cred = cred,
2048 unsigned long timestamp = jiffies;
2049 int status;
2051 nfs_fattr_init(fattr);
2053 if (state != NULL) {
2054 struct nfs_lockowner lockowner = {
2055 .l_owner = current->files,
2056 .l_pid = current->tgid,
2058 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2059 &lockowner);
2060 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
2061 FMODE_WRITE)) {
2062 /* Use that stateid */
2063 } else
2064 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
2066 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
2067 if (status == 0 && state != NULL)
2068 renew_lease(server, timestamp);
2069 return status;
2072 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2073 struct nfs_fattr *fattr, struct iattr *sattr,
2074 struct nfs4_state *state)
2076 struct nfs_server *server = NFS_SERVER(inode);
2077 struct nfs4_exception exception = {
2078 .state = state,
2079 .inode = inode,
2081 int err;
2082 do {
2083 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
2084 switch (err) {
2085 case -NFS4ERR_OPENMODE:
2086 if (state && !(state->state & FMODE_WRITE)) {
2087 err = -EBADF;
2088 if (sattr->ia_valid & ATTR_OPEN)
2089 err = -EACCES;
2090 goto out;
2093 err = nfs4_handle_exception(server, err, &exception);
2094 } while (exception.retry);
2095 out:
2096 return err;
2099 struct nfs4_closedata {
2100 struct inode *inode;
2101 struct nfs4_state *state;
2102 struct nfs_closeargs arg;
2103 struct nfs_closeres res;
2104 struct nfs_fattr fattr;
2105 unsigned long timestamp;
2106 bool roc;
2107 u32 roc_barrier;
2110 static void nfs4_free_closedata(void *data)
2112 struct nfs4_closedata *calldata = data;
2113 struct nfs4_state_owner *sp = calldata->state->owner;
2114 struct super_block *sb = calldata->state->inode->i_sb;
2116 if (calldata->roc)
2117 pnfs_roc_release(calldata->state->inode);
2118 nfs4_put_open_state(calldata->state);
2119 nfs_free_seqid(calldata->arg.seqid);
2120 nfs4_put_state_owner(sp);
2121 nfs_sb_deactive(sb);
2122 kfree(calldata);
2125 static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2126 fmode_t fmode)
2128 spin_lock(&state->owner->so_lock);
2129 if (!(fmode & FMODE_READ))
2130 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2131 if (!(fmode & FMODE_WRITE))
2132 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2133 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2134 spin_unlock(&state->owner->so_lock);
2137 static void nfs4_close_done(struct rpc_task *task, void *data)
2139 struct nfs4_closedata *calldata = data;
2140 struct nfs4_state *state = calldata->state;
2141 struct nfs_server *server = NFS_SERVER(calldata->inode);
2143 dprintk("%s: begin!\n", __func__);
2144 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2145 return;
2146 /* hmm. we are done with the inode, and in the process of freeing
2147 * the state_owner. we keep this around to process errors
2149 switch (task->tk_status) {
2150 case 0:
2151 if (calldata->roc)
2152 pnfs_roc_set_barrier(state->inode,
2153 calldata->roc_barrier);
2154 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
2155 renew_lease(server, calldata->timestamp);
2156 nfs4_close_clear_stateid_flags(state,
2157 calldata->arg.fmode);
2158 break;
2159 case -NFS4ERR_STALE_STATEID:
2160 case -NFS4ERR_OLD_STATEID:
2161 case -NFS4ERR_BAD_STATEID:
2162 case -NFS4ERR_EXPIRED:
2163 if (calldata->arg.fmode == 0)
2164 break;
2165 default:
2166 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2167 rpc_restart_call_prepare(task);
2169 nfs_release_seqid(calldata->arg.seqid);
2170 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
2171 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
2174 static void nfs4_close_prepare(struct rpc_task *task, void *data)
2176 struct nfs4_closedata *calldata = data;
2177 struct nfs4_state *state = calldata->state;
2178 struct inode *inode = calldata->inode;
2179 int call_close = 0;
2181 dprintk("%s: begin!\n", __func__);
2182 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
2183 goto out_wait;
2185 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2186 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
2187 spin_lock(&state->owner->so_lock);
2188 /* Calculate the change in open mode */
2189 if (state->n_rdwr == 0) {
2190 if (state->n_rdonly == 0) {
2191 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2192 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2193 calldata->arg.fmode &= ~FMODE_READ;
2195 if (state->n_wronly == 0) {
2196 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2197 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2198 calldata->arg.fmode &= ~FMODE_WRITE;
2201 spin_unlock(&state->owner->so_lock);
2203 if (!call_close) {
2204 /* Note: exit _without_ calling nfs4_close_done */
2205 goto out_no_action;
2208 if (calldata->arg.fmode == 0) {
2209 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
2210 if (calldata->roc &&
2211 pnfs_roc_drain(inode, &calldata->roc_barrier, task))
2212 goto out_wait;
2215 nfs_fattr_init(calldata->res.fattr);
2216 calldata->timestamp = jiffies;
2217 if (nfs4_setup_sequence(NFS_SERVER(inode),
2218 &calldata->arg.seq_args,
2219 &calldata->res.seq_res,
2220 task) != 0)
2221 nfs_release_seqid(calldata->arg.seqid);
2222 dprintk("%s: done!\n", __func__);
2223 return;
2224 out_no_action:
2225 task->tk_action = NULL;
2226 out_wait:
2227 nfs4_sequence_done(task, &calldata->res.seq_res);
2230 static const struct rpc_call_ops nfs4_close_ops = {
2231 .rpc_call_prepare = nfs4_close_prepare,
2232 .rpc_call_done = nfs4_close_done,
2233 .rpc_release = nfs4_free_closedata,
2237 * It is possible for data to be read/written from a mem-mapped file
2238 * after the sys_close call (which hits the vfs layer as a flush).
2239 * This means that we can't safely call nfsv4 close on a file until
2240 * the inode is cleared. This in turn means that we are not good
2241 * NFSv4 citizens - we do not indicate to the server to update the file's
2242 * share state even when we are done with one of the three share
2243 * stateid's in the inode.
2245 * NOTE: Caller must be holding the sp->so_owner semaphore!
2247 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
2249 struct nfs_server *server = NFS_SERVER(state->inode);
2250 struct nfs4_closedata *calldata;
2251 struct nfs4_state_owner *sp = state->owner;
2252 struct rpc_task *task;
2253 struct rpc_message msg = {
2254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2255 .rpc_cred = state->owner->so_cred,
2257 struct rpc_task_setup task_setup_data = {
2258 .rpc_client = server->client,
2259 .rpc_message = &msg,
2260 .callback_ops = &nfs4_close_ops,
2261 .workqueue = nfsiod_workqueue,
2262 .flags = RPC_TASK_ASYNC,
2264 int status = -ENOMEM;
2266 calldata = kzalloc(sizeof(*calldata), gfp_mask);
2267 if (calldata == NULL)
2268 goto out;
2269 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
2270 calldata->inode = state->inode;
2271 calldata->state = state;
2272 calldata->arg.fh = NFS_FH(state->inode);
2273 calldata->arg.stateid = &state->open_stateid;
2274 /* Serialization for the sequence id */
2275 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
2276 if (calldata->arg.seqid == NULL)
2277 goto out_free_calldata;
2278 calldata->arg.fmode = 0;
2279 calldata->arg.bitmask = server->cache_consistency_bitmask;
2280 calldata->res.fattr = &calldata->fattr;
2281 calldata->res.seqid = calldata->arg.seqid;
2282 calldata->res.server = server;
2283 calldata->roc = pnfs_roc(state->inode);
2284 nfs_sb_active(calldata->inode->i_sb);
2286 msg.rpc_argp = &calldata->arg;
2287 msg.rpc_resp = &calldata->res;
2288 task_setup_data.callback_data = calldata;
2289 task = rpc_run_task(&task_setup_data);
2290 if (IS_ERR(task))
2291 return PTR_ERR(task);
2292 status = 0;
2293 if (wait)
2294 status = rpc_wait_for_completion_task(task);
2295 rpc_put_task(task);
2296 return status;
2297 out_free_calldata:
2298 kfree(calldata);
2299 out:
2300 nfs4_put_open_state(state);
2301 nfs4_put_state_owner(sp);
2302 return status;
2305 static struct inode *
2306 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
2308 struct nfs4_state *state;
2310 /* Protect against concurrent sillydeletes */
2311 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2312 ctx->cred, &ctx->mdsthreshold);
2313 if (IS_ERR(state))
2314 return ERR_CAST(state);
2315 ctx->state = state;
2316 return igrab(state->inode);
2319 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
2321 if (ctx->state == NULL)
2322 return;
2323 if (is_sync)
2324 nfs4_close_sync(ctx->state, ctx->mode);
2325 else
2326 nfs4_close_state(ctx->state, ctx->mode);
2329 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2331 struct nfs4_server_caps_arg args = {
2332 .fhandle = fhandle,
2334 struct nfs4_server_caps_res res = {};
2335 struct rpc_message msg = {
2336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
2337 .rpc_argp = &args,
2338 .rpc_resp = &res,
2340 int status;
2342 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2343 if (status == 0) {
2344 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
2345 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2346 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2347 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2348 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2349 NFS_CAP_CTIME|NFS_CAP_MTIME);
2350 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2351 server->caps |= NFS_CAP_ACLS;
2352 if (res.has_links != 0)
2353 server->caps |= NFS_CAP_HARDLINKS;
2354 if (res.has_symlinks != 0)
2355 server->caps |= NFS_CAP_SYMLINKS;
2356 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2357 server->caps |= NFS_CAP_FILEID;
2358 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2359 server->caps |= NFS_CAP_MODE;
2360 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2361 server->caps |= NFS_CAP_NLINK;
2362 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2363 server->caps |= NFS_CAP_OWNER;
2364 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2365 server->caps |= NFS_CAP_OWNER_GROUP;
2366 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2367 server->caps |= NFS_CAP_ATIME;
2368 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2369 server->caps |= NFS_CAP_CTIME;
2370 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2371 server->caps |= NFS_CAP_MTIME;
2373 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2374 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2375 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
2376 server->acl_bitmask = res.acl_bitmask;
2377 server->fh_expire_type = res.fh_expire_type;
2380 return status;
2383 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2385 struct nfs4_exception exception = { };
2386 int err;
2387 do {
2388 err = nfs4_handle_exception(server,
2389 _nfs4_server_capabilities(server, fhandle),
2390 &exception);
2391 } while (exception.retry);
2392 return err;
2395 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2396 struct nfs_fsinfo *info)
2398 struct nfs4_lookup_root_arg args = {
2399 .bitmask = nfs4_fattr_bitmap,
2401 struct nfs4_lookup_res res = {
2402 .server = server,
2403 .fattr = info->fattr,
2404 .fh = fhandle,
2406 struct rpc_message msg = {
2407 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2408 .rpc_argp = &args,
2409 .rpc_resp = &res,
2412 nfs_fattr_init(info->fattr);
2413 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2416 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2417 struct nfs_fsinfo *info)
2419 struct nfs4_exception exception = { };
2420 int err;
2421 do {
2422 err = _nfs4_lookup_root(server, fhandle, info);
2423 switch (err) {
2424 case 0:
2425 case -NFS4ERR_WRONGSEC:
2426 goto out;
2427 default:
2428 err = nfs4_handle_exception(server, err, &exception);
2430 } while (exception.retry);
2431 out:
2432 return err;
2435 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2436 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2438 struct rpc_auth *auth;
2439 int ret;
2441 auth = rpcauth_create(flavor, server->client);
2442 if (IS_ERR(auth)) {
2443 ret = -EIO;
2444 goto out;
2446 ret = nfs4_lookup_root(server, fhandle, info);
2447 out:
2448 return ret;
2451 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2452 struct nfs_fsinfo *info)
2454 int i, len, status = 0;
2455 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
2457 len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array));
2458 if (len < 0)
2459 return len;
2461 for (i = 0; i < len; i++) {
2462 /* AUTH_UNIX is the default flavor if none was specified,
2463 * thus has already been tried. */
2464 if (flav_array[i] == RPC_AUTH_UNIX)
2465 continue;
2467 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
2468 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2469 continue;
2470 break;
2473 * -EACCESS could mean that the user doesn't have correct permissions
2474 * to access the mount. It could also mean that we tried to mount
2475 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2476 * existing mount programs don't handle -EACCES very well so it should
2477 * be mapped to -EPERM instead.
2479 if (status == -EACCES)
2480 status = -EPERM;
2481 return status;
2485 * get the file handle for the "/" directory on the server
2487 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2488 struct nfs_fsinfo *info)
2490 int minor_version = server->nfs_client->cl_minorversion;
2491 int status = nfs4_lookup_root(server, fhandle, info);
2492 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2494 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2495 * by nfs4_map_errors() as this function exits.
2497 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
2498 if (status == 0)
2499 status = nfs4_server_capabilities(server, fhandle);
2500 if (status == 0)
2501 status = nfs4_do_fsinfo(server, fhandle, info);
2502 return nfs4_map_errors(status);
2505 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2506 struct nfs_fsinfo *info)
2508 int error;
2509 struct nfs_fattr *fattr = info->fattr;
2511 error = nfs4_server_capabilities(server, mntfh);
2512 if (error < 0) {
2513 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2514 return error;
2517 error = nfs4_proc_getattr(server, mntfh, fattr);
2518 if (error < 0) {
2519 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2520 return error;
2523 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2524 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2525 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2527 return error;
2531 * Get locations and (maybe) other attributes of a referral.
2532 * Note that we'll actually follow the referral later when
2533 * we detect fsid mismatch in inode revalidation
2535 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2536 const struct qstr *name, struct nfs_fattr *fattr,
2537 struct nfs_fh *fhandle)
2539 int status = -ENOMEM;
2540 struct page *page = NULL;
2541 struct nfs4_fs_locations *locations = NULL;
2543 page = alloc_page(GFP_KERNEL);
2544 if (page == NULL)
2545 goto out;
2546 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2547 if (locations == NULL)
2548 goto out;
2550 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
2551 if (status != 0)
2552 goto out;
2553 /* Make sure server returned a different fsid for the referral */
2554 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
2555 dprintk("%s: server did not return a different fsid for"
2556 " a referral at %s\n", __func__, name->name);
2557 status = -EIO;
2558 goto out;
2560 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2561 nfs_fixup_referral_attributes(&locations->fattr);
2563 /* replace the lookup nfs_fattr with the locations nfs_fattr */
2564 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2565 memset(fhandle, 0, sizeof(struct nfs_fh));
2566 out:
2567 if (page)
2568 __free_page(page);
2569 kfree(locations);
2570 return status;
2573 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2575 struct nfs4_getattr_arg args = {
2576 .fh = fhandle,
2577 .bitmask = server->attr_bitmask,
2579 struct nfs4_getattr_res res = {
2580 .fattr = fattr,
2581 .server = server,
2583 struct rpc_message msg = {
2584 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2585 .rpc_argp = &args,
2586 .rpc_resp = &res,
2589 nfs_fattr_init(fattr);
2590 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2593 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2595 struct nfs4_exception exception = { };
2596 int err;
2597 do {
2598 err = nfs4_handle_exception(server,
2599 _nfs4_proc_getattr(server, fhandle, fattr),
2600 &exception);
2601 } while (exception.retry);
2602 return err;
2606 * The file is not closed if it is opened due to the a request to change
2607 * the size of the file. The open call will not be needed once the
2608 * VFS layer lookup-intents are implemented.
2610 * Close is called when the inode is destroyed.
2611 * If we haven't opened the file for O_WRONLY, we
2612 * need to in the size_change case to obtain a stateid.
2614 * Got race?
2615 * Because OPEN is always done by name in nfsv4, it is
2616 * possible that we opened a different file by the same
2617 * name. We can recognize this race condition, but we
2618 * can't do anything about it besides returning an error.
2620 * This will be fixed with VFS changes (lookup-intent).
2622 static int
2623 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2624 struct iattr *sattr)
2626 struct inode *inode = dentry->d_inode;
2627 struct rpc_cred *cred = NULL;
2628 struct nfs4_state *state = NULL;
2629 int status;
2631 if (pnfs_ld_layoutret_on_setattr(inode))
2632 pnfs_return_layout(inode);
2634 nfs_fattr_init(fattr);
2636 /* Deal with open(O_TRUNC) */
2637 if (sattr->ia_valid & ATTR_OPEN)
2638 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2640 /* Optimization: if the end result is no change, don't RPC */
2641 if ((sattr->ia_valid & ~(ATTR_FILE)) == 0)
2642 return 0;
2644 /* Search for an existing open(O_WRITE) file */
2645 if (sattr->ia_valid & ATTR_FILE) {
2646 struct nfs_open_context *ctx;
2648 ctx = nfs_file_open_context(sattr->ia_file);
2649 if (ctx) {
2650 cred = ctx->cred;
2651 state = ctx->state;
2655 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
2656 if (status == 0)
2657 nfs_setattr_update_inode(inode, sattr);
2658 return status;
2661 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2662 const struct qstr *name, struct nfs_fh *fhandle,
2663 struct nfs_fattr *fattr)
2665 struct nfs_server *server = NFS_SERVER(dir);
2666 int status;
2667 struct nfs4_lookup_arg args = {
2668 .bitmask = server->attr_bitmask,
2669 .dir_fh = NFS_FH(dir),
2670 .name = name,
2672 struct nfs4_lookup_res res = {
2673 .server = server,
2674 .fattr = fattr,
2675 .fh = fhandle,
2677 struct rpc_message msg = {
2678 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2679 .rpc_argp = &args,
2680 .rpc_resp = &res,
2683 nfs_fattr_init(fattr);
2685 dprintk("NFS call lookup %s\n", name->name);
2686 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
2687 dprintk("NFS reply lookup: %d\n", status);
2688 return status;
2691 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
2693 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2694 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
2695 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2696 fattr->nlink = 2;
2699 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2700 struct qstr *name, struct nfs_fh *fhandle,
2701 struct nfs_fattr *fattr)
2703 struct nfs4_exception exception = { };
2704 struct rpc_clnt *client = *clnt;
2705 int err;
2706 do {
2707 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2708 switch (err) {
2709 case -NFS4ERR_BADNAME:
2710 err = -ENOENT;
2711 goto out;
2712 case -NFS4ERR_MOVED:
2713 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
2714 goto out;
2715 case -NFS4ERR_WRONGSEC:
2716 err = -EPERM;
2717 if (client != *clnt)
2718 goto out;
2720 client = nfs4_create_sec_client(client, dir, name);
2721 if (IS_ERR(client))
2722 return PTR_ERR(client);
2724 exception.retry = 1;
2725 break;
2726 default:
2727 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2729 } while (exception.retry);
2731 out:
2732 if (err == 0)
2733 *clnt = client;
2734 else if (client != *clnt)
2735 rpc_shutdown_client(client);
2737 return err;
2740 static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
2741 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2743 int status;
2744 struct rpc_clnt *client = NFS_CLIENT(dir);
2746 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2747 if (client != NFS_CLIENT(dir)) {
2748 rpc_shutdown_client(client);
2749 nfs_fixup_secinfo_attributes(fattr);
2751 return status;
2754 struct rpc_clnt *
2755 nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2756 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2758 int status;
2759 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2761 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2762 if (status < 0) {
2763 rpc_shutdown_client(client);
2764 return ERR_PTR(status);
2766 return client;
2769 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2771 struct nfs_server *server = NFS_SERVER(inode);
2772 struct nfs4_accessargs args = {
2773 .fh = NFS_FH(inode),
2774 .bitmask = server->cache_consistency_bitmask,
2776 struct nfs4_accessres res = {
2777 .server = server,
2779 struct rpc_message msg = {
2780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2781 .rpc_argp = &args,
2782 .rpc_resp = &res,
2783 .rpc_cred = entry->cred,
2785 int mode = entry->mask;
2786 int status;
2789 * Determine which access bits we want to ask for...
2791 if (mode & MAY_READ)
2792 args.access |= NFS4_ACCESS_READ;
2793 if (S_ISDIR(inode->i_mode)) {
2794 if (mode & MAY_WRITE)
2795 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2796 if (mode & MAY_EXEC)
2797 args.access |= NFS4_ACCESS_LOOKUP;
2798 } else {
2799 if (mode & MAY_WRITE)
2800 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2801 if (mode & MAY_EXEC)
2802 args.access |= NFS4_ACCESS_EXECUTE;
2805 res.fattr = nfs_alloc_fattr();
2806 if (res.fattr == NULL)
2807 return -ENOMEM;
2809 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
2810 if (!status) {
2811 nfs_access_set_mask(entry, res.access);
2812 nfs_refresh_inode(inode, res.fattr);
2814 nfs_free_fattr(res.fattr);
2815 return status;
2818 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2820 struct nfs4_exception exception = { };
2821 int err;
2822 do {
2823 err = nfs4_handle_exception(NFS_SERVER(inode),
2824 _nfs4_proc_access(inode, entry),
2825 &exception);
2826 } while (exception.retry);
2827 return err;
2831 * TODO: For the time being, we don't try to get any attributes
2832 * along with any of the zero-copy operations READ, READDIR,
2833 * READLINK, WRITE.
2835 * In the case of the first three, we want to put the GETATTR
2836 * after the read-type operation -- this is because it is hard
2837 * to predict the length of a GETATTR response in v4, and thus
2838 * align the READ data correctly. This means that the GETATTR
2839 * may end up partially falling into the page cache, and we should
2840 * shift it into the 'tail' of the xdr_buf before processing.
2841 * To do this efficiently, we need to know the total length
2842 * of data received, which doesn't seem to be available outside
2843 * of the RPC layer.
2845 * In the case of WRITE, we also want to put the GETATTR after
2846 * the operation -- in this case because we want to make sure
2847 * we get the post-operation mtime and size.
2849 * Both of these changes to the XDR layer would in fact be quite
2850 * minor, but I decided to leave them for a subsequent patch.
2852 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2853 unsigned int pgbase, unsigned int pglen)
2855 struct nfs4_readlink args = {
2856 .fh = NFS_FH(inode),
2857 .pgbase = pgbase,
2858 .pglen = pglen,
2859 .pages = &page,
2861 struct nfs4_readlink_res res;
2862 struct rpc_message msg = {
2863 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2864 .rpc_argp = &args,
2865 .rpc_resp = &res,
2868 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
2871 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2872 unsigned int pgbase, unsigned int pglen)
2874 struct nfs4_exception exception = { };
2875 int err;
2876 do {
2877 err = nfs4_handle_exception(NFS_SERVER(inode),
2878 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2879 &exception);
2880 } while (exception.retry);
2881 return err;
2885 * This is just for mknod. open(O_CREAT) will always do ->open_context().
2887 static int
2888 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
2889 int flags)
2891 struct nfs_open_context *ctx;
2892 struct nfs4_state *state;
2893 int status = 0;
2895 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
2896 if (IS_ERR(ctx))
2897 return PTR_ERR(ctx);
2899 sattr->ia_mode &= ~current_umask();
2900 state = nfs4_do_open(dir, dentry, ctx->mode,
2901 flags, sattr, ctx->cred,
2902 &ctx->mdsthreshold);
2903 d_drop(dentry);
2904 if (IS_ERR(state)) {
2905 status = PTR_ERR(state);
2906 goto out;
2908 d_add(dentry, igrab(state->inode));
2909 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2910 ctx->state = state;
2911 out:
2912 put_nfs_open_context(ctx);
2913 return status;
2916 static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2918 struct nfs_server *server = NFS_SERVER(dir);
2919 struct nfs_removeargs args = {
2920 .fh = NFS_FH(dir),
2921 .name = *name,
2923 struct nfs_removeres res = {
2924 .server = server,
2926 struct rpc_message msg = {
2927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2928 .rpc_argp = &args,
2929 .rpc_resp = &res,
2931 int status;
2933 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
2934 if (status == 0)
2935 update_changeattr(dir, &res.cinfo);
2936 return status;
2939 static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2941 struct nfs4_exception exception = { };
2942 int err;
2943 do {
2944 err = nfs4_handle_exception(NFS_SERVER(dir),
2945 _nfs4_proc_remove(dir, name),
2946 &exception);
2947 } while (exception.retry);
2948 return err;
2951 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
2953 struct nfs_server *server = NFS_SERVER(dir);
2954 struct nfs_removeargs *args = msg->rpc_argp;
2955 struct nfs_removeres *res = msg->rpc_resp;
2957 res->server = server;
2958 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
2959 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
2962 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2964 nfs4_setup_sequence(NFS_SERVER(data->dir),
2965 &data->args.seq_args,
2966 &data->res.seq_res,
2967 task);
2970 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
2972 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2974 if (!nfs4_sequence_done(task, &res->seq_res))
2975 return 0;
2976 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2977 return 0;
2978 update_changeattr(dir, &res->cinfo);
2979 return 1;
2982 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2984 struct nfs_server *server = NFS_SERVER(dir);
2985 struct nfs_renameargs *arg = msg->rpc_argp;
2986 struct nfs_renameres *res = msg->rpc_resp;
2988 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2989 res->server = server;
2990 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
2993 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2995 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2996 &data->args.seq_args,
2997 &data->res.seq_res,
2998 task);
3001 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3002 struct inode *new_dir)
3004 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3006 if (!nfs4_sequence_done(task, &res->seq_res))
3007 return 0;
3008 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3009 return 0;
3011 update_changeattr(old_dir, &res->old_cinfo);
3012 update_changeattr(new_dir, &res->new_cinfo);
3013 return 1;
3016 static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3017 struct inode *new_dir, struct qstr *new_name)
3019 struct nfs_server *server = NFS_SERVER(old_dir);
3020 struct nfs_renameargs arg = {
3021 .old_dir = NFS_FH(old_dir),
3022 .new_dir = NFS_FH(new_dir),
3023 .old_name = old_name,
3024 .new_name = new_name,
3026 struct nfs_renameres res = {
3027 .server = server,
3029 struct rpc_message msg = {
3030 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3031 .rpc_argp = &arg,
3032 .rpc_resp = &res,
3034 int status = -ENOMEM;
3036 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3037 if (!status) {
3038 update_changeattr(old_dir, &res.old_cinfo);
3039 update_changeattr(new_dir, &res.new_cinfo);
3041 return status;
3044 static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3045 struct inode *new_dir, struct qstr *new_name)
3047 struct nfs4_exception exception = { };
3048 int err;
3049 do {
3050 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3051 _nfs4_proc_rename(old_dir, old_name,
3052 new_dir, new_name),
3053 &exception);
3054 } while (exception.retry);
3055 return err;
3058 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3060 struct nfs_server *server = NFS_SERVER(inode);
3061 struct nfs4_link_arg arg = {
3062 .fh = NFS_FH(inode),
3063 .dir_fh = NFS_FH(dir),
3064 .name = name,
3065 .bitmask = server->attr_bitmask,
3067 struct nfs4_link_res res = {
3068 .server = server,
3070 struct rpc_message msg = {
3071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3072 .rpc_argp = &arg,
3073 .rpc_resp = &res,
3075 int status = -ENOMEM;
3077 res.fattr = nfs_alloc_fattr();
3078 if (res.fattr == NULL)
3079 goto out;
3081 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3082 if (!status) {
3083 update_changeattr(dir, &res.cinfo);
3084 nfs_post_op_update_inode(inode, res.fattr);
3086 out:
3087 nfs_free_fattr(res.fattr);
3088 return status;
3091 static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3093 struct nfs4_exception exception = { };
3094 int err;
3095 do {
3096 err = nfs4_handle_exception(NFS_SERVER(inode),
3097 _nfs4_proc_link(inode, dir, name),
3098 &exception);
3099 } while (exception.retry);
3100 return err;
3103 struct nfs4_createdata {
3104 struct rpc_message msg;
3105 struct nfs4_create_arg arg;
3106 struct nfs4_create_res res;
3107 struct nfs_fh fh;
3108 struct nfs_fattr fattr;
3111 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3112 struct qstr *name, struct iattr *sattr, u32 ftype)
3114 struct nfs4_createdata *data;
3116 data = kzalloc(sizeof(*data), GFP_KERNEL);
3117 if (data != NULL) {
3118 struct nfs_server *server = NFS_SERVER(dir);
3120 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3121 data->msg.rpc_argp = &data->arg;
3122 data->msg.rpc_resp = &data->res;
3123 data->arg.dir_fh = NFS_FH(dir);
3124 data->arg.server = server;
3125 data->arg.name = name;
3126 data->arg.attrs = sattr;
3127 data->arg.ftype = ftype;
3128 data->arg.bitmask = server->attr_bitmask;
3129 data->res.server = server;
3130 data->res.fh = &data->fh;
3131 data->res.fattr = &data->fattr;
3132 nfs_fattr_init(data->res.fattr);
3134 return data;
3137 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3139 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
3140 &data->arg.seq_args, &data->res.seq_res, 1);
3141 if (status == 0) {
3142 update_changeattr(dir, &data->res.dir_cinfo);
3143 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3145 return status;
3148 static void nfs4_free_createdata(struct nfs4_createdata *data)
3150 kfree(data);
3153 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3154 struct page *page, unsigned int len, struct iattr *sattr)
3156 struct nfs4_createdata *data;
3157 int status = -ENAMETOOLONG;
3159 if (len > NFS4_MAXPATHLEN)
3160 goto out;
3162 status = -ENOMEM;
3163 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3164 if (data == NULL)
3165 goto out;
3167 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3168 data->arg.u.symlink.pages = &page;
3169 data->arg.u.symlink.len = len;
3171 status = nfs4_do_create(dir, dentry, data);
3173 nfs4_free_createdata(data);
3174 out:
3175 return status;
3178 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
3179 struct page *page, unsigned int len, struct iattr *sattr)
3181 struct nfs4_exception exception = { };
3182 int err;
3183 do {
3184 err = nfs4_handle_exception(NFS_SERVER(dir),
3185 _nfs4_proc_symlink(dir, dentry, page,
3186 len, sattr),
3187 &exception);
3188 } while (exception.retry);
3189 return err;
3192 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3193 struct iattr *sattr)
3195 struct nfs4_createdata *data;
3196 int status = -ENOMEM;
3198 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3199 if (data == NULL)
3200 goto out;
3202 status = nfs4_do_create(dir, dentry, data);
3204 nfs4_free_createdata(data);
3205 out:
3206 return status;
3209 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3210 struct iattr *sattr)
3212 struct nfs4_exception exception = { };
3213 int err;
3215 sattr->ia_mode &= ~current_umask();
3216 do {
3217 err = nfs4_handle_exception(NFS_SERVER(dir),
3218 _nfs4_proc_mkdir(dir, dentry, sattr),
3219 &exception);
3220 } while (exception.retry);
3221 return err;
3224 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3225 u64 cookie, struct page **pages, unsigned int count, int plus)
3227 struct inode *dir = dentry->d_inode;
3228 struct nfs4_readdir_arg args = {
3229 .fh = NFS_FH(dir),
3230 .pages = pages,
3231 .pgbase = 0,
3232 .count = count,
3233 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
3234 .plus = plus,
3236 struct nfs4_readdir_res res;
3237 struct rpc_message msg = {
3238 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3239 .rpc_argp = &args,
3240 .rpc_resp = &res,
3241 .rpc_cred = cred,
3243 int status;
3245 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
3246 dentry->d_parent->d_name.name,
3247 dentry->d_name.name,
3248 (unsigned long long)cookie);
3249 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
3250 res.pgbase = args.pgbase;
3251 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
3252 if (status >= 0) {
3253 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
3254 status += args.pgbase;
3257 nfs_invalidate_atime(dir);
3259 dprintk("%s: returns %d\n", __func__, status);
3260 return status;
3263 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
3264 u64 cookie, struct page **pages, unsigned int count, int plus)
3266 struct nfs4_exception exception = { };
3267 int err;
3268 do {
3269 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3270 _nfs4_proc_readdir(dentry, cred, cookie,
3271 pages, count, plus),
3272 &exception);
3273 } while (exception.retry);
3274 return err;
3277 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3278 struct iattr *sattr, dev_t rdev)
3280 struct nfs4_createdata *data;
3281 int mode = sattr->ia_mode;
3282 int status = -ENOMEM;
3284 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3285 if (data == NULL)
3286 goto out;
3288 if (S_ISFIFO(mode))
3289 data->arg.ftype = NF4FIFO;
3290 else if (S_ISBLK(mode)) {
3291 data->arg.ftype = NF4BLK;
3292 data->arg.u.device.specdata1 = MAJOR(rdev);
3293 data->arg.u.device.specdata2 = MINOR(rdev);
3295 else if (S_ISCHR(mode)) {
3296 data->arg.ftype = NF4CHR;
3297 data->arg.u.device.specdata1 = MAJOR(rdev);
3298 data->arg.u.device.specdata2 = MINOR(rdev);
3299 } else if (!S_ISSOCK(mode)) {
3300 status = -EINVAL;
3301 goto out_free;
3304 status = nfs4_do_create(dir, dentry, data);
3305 out_free:
3306 nfs4_free_createdata(data);
3307 out:
3308 return status;
3311 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3312 struct iattr *sattr, dev_t rdev)
3314 struct nfs4_exception exception = { };
3315 int err;
3317 sattr->ia_mode &= ~current_umask();
3318 do {
3319 err = nfs4_handle_exception(NFS_SERVER(dir),
3320 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3321 &exception);
3322 } while (exception.retry);
3323 return err;
3326 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3327 struct nfs_fsstat *fsstat)
3329 struct nfs4_statfs_arg args = {
3330 .fh = fhandle,
3331 .bitmask = server->attr_bitmask,
3333 struct nfs4_statfs_res res = {
3334 .fsstat = fsstat,
3336 struct rpc_message msg = {
3337 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3338 .rpc_argp = &args,
3339 .rpc_resp = &res,
3342 nfs_fattr_init(fsstat->fattr);
3343 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3346 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3348 struct nfs4_exception exception = { };
3349 int err;
3350 do {
3351 err = nfs4_handle_exception(server,
3352 _nfs4_proc_statfs(server, fhandle, fsstat),
3353 &exception);
3354 } while (exception.retry);
3355 return err;
3358 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3359 struct nfs_fsinfo *fsinfo)
3361 struct nfs4_fsinfo_arg args = {
3362 .fh = fhandle,
3363 .bitmask = server->attr_bitmask,
3365 struct nfs4_fsinfo_res res = {
3366 .fsinfo = fsinfo,
3368 struct rpc_message msg = {
3369 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3370 .rpc_argp = &args,
3371 .rpc_resp = &res,
3374 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3377 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3379 struct nfs4_exception exception = { };
3380 int err;
3382 do {
3383 err = nfs4_handle_exception(server,
3384 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3385 &exception);
3386 } while (exception.retry);
3387 return err;
3390 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3392 int error;
3394 nfs_fattr_init(fsinfo->fattr);
3395 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
3396 if (error == 0) {
3397 /* block layout checks this! */
3398 server->pnfs_blksize = fsinfo->blksize;
3399 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
3402 return error;
3405 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3406 struct nfs_pathconf *pathconf)
3408 struct nfs4_pathconf_arg args = {
3409 .fh = fhandle,
3410 .bitmask = server->attr_bitmask,
3412 struct nfs4_pathconf_res res = {
3413 .pathconf = pathconf,
3415 struct rpc_message msg = {
3416 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3417 .rpc_argp = &args,
3418 .rpc_resp = &res,
3421 /* None of the pathconf attributes are mandatory to implement */
3422 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3423 memset(pathconf, 0, sizeof(*pathconf));
3424 return 0;
3427 nfs_fattr_init(pathconf->fattr);
3428 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3431 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3432 struct nfs_pathconf *pathconf)
3434 struct nfs4_exception exception = { };
3435 int err;
3437 do {
3438 err = nfs4_handle_exception(server,
3439 _nfs4_proc_pathconf(server, fhandle, pathconf),
3440 &exception);
3441 } while (exception.retry);
3442 return err;
3445 void __nfs4_read_done_cb(struct nfs_read_data *data)
3447 nfs_invalidate_atime(data->header->inode);
3450 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
3452 struct nfs_server *server = NFS_SERVER(data->header->inode);
3454 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
3455 rpc_restart_call_prepare(task);
3456 return -EAGAIN;
3459 __nfs4_read_done_cb(data);
3460 if (task->tk_status > 0)
3461 renew_lease(server, data->timestamp);
3462 return 0;
3465 static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3468 dprintk("--> %s\n", __func__);
3470 if (!nfs4_sequence_done(task, &data->res.seq_res))
3471 return -EAGAIN;
3473 return data->read_done_cb ? data->read_done_cb(task, data) :
3474 nfs4_read_done_cb(task, data);
3477 static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
3479 data->timestamp = jiffies;
3480 data->read_done_cb = nfs4_read_done_cb;
3481 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
3482 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
3485 static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3487 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3488 &data->args.seq_args,
3489 &data->res.seq_res,
3490 task);
3493 static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
3495 struct inode *inode = data->header->inode;
3497 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
3498 rpc_restart_call_prepare(task);
3499 return -EAGAIN;
3501 if (task->tk_status >= 0) {
3502 renew_lease(NFS_SERVER(inode), data->timestamp);
3503 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
3505 return 0;
3508 static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3510 if (!nfs4_sequence_done(task, &data->res.seq_res))
3511 return -EAGAIN;
3512 return data->write_done_cb ? data->write_done_cb(task, data) :
3513 nfs4_write_done_cb(task, data);
3516 static
3517 bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3519 const struct nfs_pgio_header *hdr = data->header;
3521 /* Don't request attributes for pNFS or O_DIRECT writes */
3522 if (data->ds_clp != NULL || hdr->dreq != NULL)
3523 return false;
3524 /* Otherwise, request attributes if and only if we don't hold
3525 * a delegation
3527 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
3530 static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
3532 struct nfs_server *server = NFS_SERVER(data->header->inode);
3534 if (!nfs4_write_need_cache_consistency_data(data)) {
3535 data->args.bitmask = NULL;
3536 data->res.fattr = NULL;
3537 } else
3538 data->args.bitmask = server->cache_consistency_bitmask;
3540 if (!data->write_done_cb)
3541 data->write_done_cb = nfs4_write_done_cb;
3542 data->res.server = server;
3543 data->timestamp = jiffies;
3545 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
3546 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3549 static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3551 nfs4_setup_sequence(NFS_SERVER(data->header->inode),
3552 &data->args.seq_args,
3553 &data->res.seq_res,
3554 task);
3557 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3559 nfs4_setup_sequence(NFS_SERVER(data->inode),
3560 &data->args.seq_args,
3561 &data->res.seq_res,
3562 task);
3565 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
3567 struct inode *inode = data->inode;
3569 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
3570 rpc_restart_call_prepare(task);
3571 return -EAGAIN;
3573 return 0;
3576 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
3578 if (!nfs4_sequence_done(task, &data->res.seq_res))
3579 return -EAGAIN;
3580 return data->commit_done_cb(task, data);
3583 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
3585 struct nfs_server *server = NFS_SERVER(data->inode);
3587 if (data->commit_done_cb == NULL)
3588 data->commit_done_cb = nfs4_commit_done_cb;
3589 data->res.server = server;
3590 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
3591 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
3594 struct nfs4_renewdata {
3595 struct nfs_client *client;
3596 unsigned long timestamp;
3600 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3601 * standalone procedure for queueing an asynchronous RENEW.
3603 static void nfs4_renew_release(void *calldata)
3605 struct nfs4_renewdata *data = calldata;
3606 struct nfs_client *clp = data->client;
3608 if (atomic_read(&clp->cl_count) > 1)
3609 nfs4_schedule_state_renewal(clp);
3610 nfs_put_client(clp);
3611 kfree(data);
3614 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
3616 struct nfs4_renewdata *data = calldata;
3617 struct nfs_client *clp = data->client;
3618 unsigned long timestamp = data->timestamp;
3620 if (task->tk_status < 0) {
3621 /* Unless we're shutting down, schedule state recovery! */
3622 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3623 return;
3624 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
3625 nfs4_schedule_lease_recovery(clp);
3626 return;
3628 nfs4_schedule_path_down_recovery(clp);
3630 do_renew_lease(clp, timestamp);
3633 static const struct rpc_call_ops nfs4_renew_ops = {
3634 .rpc_call_done = nfs4_renew_done,
3635 .rpc_release = nfs4_renew_release,
3638 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
3640 struct rpc_message msg = {
3641 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3642 .rpc_argp = clp,
3643 .rpc_cred = cred,
3645 struct nfs4_renewdata *data;
3647 if (renew_flags == 0)
3648 return 0;
3649 if (!atomic_inc_not_zero(&clp->cl_count))
3650 return -EIO;
3651 data = kmalloc(sizeof(*data), GFP_NOFS);
3652 if (data == NULL)
3653 return -ENOMEM;
3654 data->client = clp;
3655 data->timestamp = jiffies;
3656 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
3657 &nfs4_renew_ops, data);
3660 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
3662 struct rpc_message msg = {
3663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3664 .rpc_argp = clp,
3665 .rpc_cred = cred,
3667 unsigned long now = jiffies;
3668 int status;
3670 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3671 if (status < 0)
3672 return status;
3673 do_renew_lease(clp, now);
3674 return 0;
3677 static inline int nfs4_server_supports_acls(struct nfs_server *server)
3679 return (server->caps & NFS_CAP_ACLS)
3680 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3681 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3684 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
3685 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
3686 * the stack.
3688 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
3690 static int buf_to_pages_noslab(const void *buf, size_t buflen,
3691 struct page **pages, unsigned int *pgbase)
3693 struct page *newpage, **spages;
3694 int rc = 0;
3695 size_t len;
3696 spages = pages;
3698 do {
3699 len = min_t(size_t, PAGE_SIZE, buflen);
3700 newpage = alloc_page(GFP_KERNEL);
3702 if (newpage == NULL)
3703 goto unwind;
3704 memcpy(page_address(newpage), buf, len);
3705 buf += len;
3706 buflen -= len;
3707 *pages++ = newpage;
3708 rc++;
3709 } while (buflen != 0);
3711 return rc;
3713 unwind:
3714 for(; rc > 0; rc--)
3715 __free_page(spages[rc-1]);
3716 return -ENOMEM;
3719 struct nfs4_cached_acl {
3720 int cached;
3721 size_t len;
3722 char data[0];
3725 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
3727 struct nfs_inode *nfsi = NFS_I(inode);
3729 spin_lock(&inode->i_lock);
3730 kfree(nfsi->nfs4_acl);
3731 nfsi->nfs4_acl = acl;
3732 spin_unlock(&inode->i_lock);
3735 static void nfs4_zap_acl_attr(struct inode *inode)
3737 nfs4_set_cached_acl(inode, NULL);
3740 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3742 struct nfs_inode *nfsi = NFS_I(inode);
3743 struct nfs4_cached_acl *acl;
3744 int ret = -ENOENT;
3746 spin_lock(&inode->i_lock);
3747 acl = nfsi->nfs4_acl;
3748 if (acl == NULL)
3749 goto out;
3750 if (buf == NULL) /* user is just asking for length */
3751 goto out_len;
3752 if (acl->cached == 0)
3753 goto out;
3754 ret = -ERANGE; /* see getxattr(2) man page */
3755 if (acl->len > buflen)
3756 goto out;
3757 memcpy(buf, acl->data, acl->len);
3758 out_len:
3759 ret = acl->len;
3760 out:
3761 spin_unlock(&inode->i_lock);
3762 return ret;
3765 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
3767 struct nfs4_cached_acl *acl;
3768 size_t buflen = sizeof(*acl) + acl_len;
3770 if (buflen <= PAGE_SIZE) {
3771 acl = kmalloc(buflen, GFP_KERNEL);
3772 if (acl == NULL)
3773 goto out;
3774 acl->cached = 1;
3775 _copy_from_pages(acl->data, pages, pgbase, acl_len);
3776 } else {
3777 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3778 if (acl == NULL)
3779 goto out;
3780 acl->cached = 0;
3782 acl->len = acl_len;
3783 out:
3784 nfs4_set_cached_acl(inode, acl);
3788 * The getxattr API returns the required buffer length when called with a
3789 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3790 * the required buf. On a NULL buf, we send a page of data to the server
3791 * guessing that the ACL request can be serviced by a page. If so, we cache
3792 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3793 * the cache. If not so, we throw away the page, and cache the required
3794 * length. The next getxattr call will then produce another round trip to
3795 * the server, this time with the input buf of the required size.
3797 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3799 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
3800 struct nfs_getaclargs args = {
3801 .fh = NFS_FH(inode),
3802 .acl_pages = pages,
3803 .acl_len = buflen,
3805 struct nfs_getaclres res = {
3806 .acl_len = buflen,
3808 struct rpc_message msg = {
3809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3810 .rpc_argp = &args,
3811 .rpc_resp = &res,
3813 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3814 int ret = -ENOMEM, i;
3816 /* As long as we're doing a round trip to the server anyway,
3817 * let's be prepared for a page of acl data. */
3818 if (npages == 0)
3819 npages = 1;
3820 if (npages > ARRAY_SIZE(pages))
3821 return -ERANGE;
3823 for (i = 0; i < npages; i++) {
3824 pages[i] = alloc_page(GFP_KERNEL);
3825 if (!pages[i])
3826 goto out_free;
3829 /* for decoding across pages */
3830 res.acl_scratch = alloc_page(GFP_KERNEL);
3831 if (!res.acl_scratch)
3832 goto out_free;
3834 args.acl_len = npages * PAGE_SIZE;
3835 args.acl_pgbase = 0;
3837 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
3838 __func__, buf, buflen, npages, args.acl_len);
3839 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3840 &msg, &args.seq_args, &res.seq_res, 0);
3841 if (ret)
3842 goto out_free;
3844 /* Handle the case where the passed-in buffer is too short */
3845 if (res.acl_flags & NFS4_ACL_TRUNC) {
3846 /* Did the user only issue a request for the acl length? */
3847 if (buf == NULL)
3848 goto out_ok;
3849 ret = -ERANGE;
3850 goto out_free;
3852 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
3853 if (buf) {
3854 if (res.acl_len > buflen) {
3855 ret = -ERANGE;
3856 goto out_free;
3858 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
3860 out_ok:
3861 ret = res.acl_len;
3862 out_free:
3863 for (i = 0; i < npages; i++)
3864 if (pages[i])
3865 __free_page(pages[i]);
3866 if (res.acl_scratch)
3867 __free_page(res.acl_scratch);
3868 return ret;
3871 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3873 struct nfs4_exception exception = { };
3874 ssize_t ret;
3875 do {
3876 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3877 if (ret >= 0)
3878 break;
3879 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3880 } while (exception.retry);
3881 return ret;
3884 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3886 struct nfs_server *server = NFS_SERVER(inode);
3887 int ret;
3889 if (!nfs4_server_supports_acls(server))
3890 return -EOPNOTSUPP;
3891 ret = nfs_revalidate_inode(server, inode);
3892 if (ret < 0)
3893 return ret;
3894 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3895 nfs_zap_acl_cache(inode);
3896 ret = nfs4_read_cached_acl(inode, buf, buflen);
3897 if (ret != -ENOENT)
3898 /* -ENOENT is returned if there is no ACL or if there is an ACL
3899 * but no cached acl data, just the acl length */
3900 return ret;
3901 return nfs4_get_acl_uncached(inode, buf, buflen);
3904 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3906 struct nfs_server *server = NFS_SERVER(inode);
3907 struct page *pages[NFS4ACL_MAXPAGES];
3908 struct nfs_setaclargs arg = {
3909 .fh = NFS_FH(inode),
3910 .acl_pages = pages,
3911 .acl_len = buflen,
3913 struct nfs_setaclres res;
3914 struct rpc_message msg = {
3915 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3916 .rpc_argp = &arg,
3917 .rpc_resp = &res,
3919 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
3920 int ret, i;
3922 if (!nfs4_server_supports_acls(server))
3923 return -EOPNOTSUPP;
3924 if (npages > ARRAY_SIZE(pages))
3925 return -ERANGE;
3926 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3927 if (i < 0)
3928 return i;
3929 nfs4_inode_return_delegation(inode);
3930 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
3933 * Free each page after tx, so the only ref left is
3934 * held by the network stack
3936 for (; i > 0; i--)
3937 put_page(pages[i-1]);
3940 * Acl update can result in inode attribute update.
3941 * so mark the attribute cache invalid.
3943 spin_lock(&inode->i_lock);
3944 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3945 spin_unlock(&inode->i_lock);
3946 nfs_access_zap_cache(inode);
3947 nfs_zap_acl_cache(inode);
3948 return ret;
3951 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3953 struct nfs4_exception exception = { };
3954 int err;
3955 do {
3956 err = nfs4_handle_exception(NFS_SERVER(inode),
3957 __nfs4_proc_set_acl(inode, buf, buflen),
3958 &exception);
3959 } while (exception.retry);
3960 return err;
3963 static int
3964 nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3966 struct nfs_client *clp = server->nfs_client;
3968 if (task->tk_status >= 0)
3969 return 0;
3970 switch(task->tk_status) {
3971 case -NFS4ERR_DELEG_REVOKED:
3972 case -NFS4ERR_ADMIN_REVOKED:
3973 case -NFS4ERR_BAD_STATEID:
3974 if (state == NULL)
3975 break;
3976 nfs_remove_bad_delegation(state->inode);
3977 case -NFS4ERR_OPENMODE:
3978 if (state == NULL)
3979 break;
3980 nfs4_schedule_stateid_recovery(server, state);
3981 goto wait_on_recovery;
3982 case -NFS4ERR_EXPIRED:
3983 if (state != NULL)
3984 nfs4_schedule_stateid_recovery(server, state);
3985 case -NFS4ERR_STALE_STATEID:
3986 case -NFS4ERR_STALE_CLIENTID:
3987 nfs4_schedule_lease_recovery(clp);
3988 goto wait_on_recovery;
3989 #if defined(CONFIG_NFS_V4_1)
3990 case -NFS4ERR_BADSESSION:
3991 case -NFS4ERR_BADSLOT:
3992 case -NFS4ERR_BAD_HIGH_SLOT:
3993 case -NFS4ERR_DEADSESSION:
3994 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3995 case -NFS4ERR_SEQ_FALSE_RETRY:
3996 case -NFS4ERR_SEQ_MISORDERED:
3997 dprintk("%s ERROR %d, Reset session\n", __func__,
3998 task->tk_status);
3999 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
4000 task->tk_status = 0;
4001 return -EAGAIN;
4002 #endif /* CONFIG_NFS_V4_1 */
4003 case -NFS4ERR_DELAY:
4004 nfs_inc_server_stats(server, NFSIOS_DELAY);
4005 case -NFS4ERR_GRACE:
4006 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4007 task->tk_status = 0;
4008 return -EAGAIN;
4009 case -NFS4ERR_RETRY_UNCACHED_REP:
4010 case -NFS4ERR_OLD_STATEID:
4011 task->tk_status = 0;
4012 return -EAGAIN;
4014 task->tk_status = nfs4_map_errors(task->tk_status);
4015 return 0;
4016 wait_on_recovery:
4017 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
4018 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4019 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4020 task->tk_status = 0;
4021 return -EAGAIN;
4024 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4025 nfs4_verifier *bootverf)
4027 __be32 verf[2];
4029 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4030 /* An impossible timestamp guarantees this value
4031 * will never match a generated boot time. */
4032 verf[0] = 0;
4033 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4034 } else {
4035 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4036 verf[0] = (__be32)nn->boot_time.tv_sec;
4037 verf[1] = (__be32)nn->boot_time.tv_nsec;
4039 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4042 static unsigned int
4043 nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4044 char *buf, size_t len)
4046 unsigned int result;
4048 rcu_read_lock();
4049 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4050 clp->cl_ipaddr,
4051 rpc_peeraddr2str(clp->cl_rpcclient,
4052 RPC_DISPLAY_ADDR),
4053 rpc_peeraddr2str(clp->cl_rpcclient,
4054 RPC_DISPLAY_PROTO));
4055 rcu_read_unlock();
4056 return result;
4059 static unsigned int
4060 nfs4_init_uniform_client_string(const struct nfs_client *clp,
4061 char *buf, size_t len)
4063 char *nodename = clp->cl_rpcclient->cl_nodename;
4065 if (nfs4_client_id_uniquifier[0] != '\0')
4066 nodename = nfs4_client_id_uniquifier;
4067 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4068 clp->rpc_ops->version, clp->cl_minorversion,
4069 nodename);
4073 * nfs4_proc_setclientid - Negotiate client ID
4074 * @clp: state data structure
4075 * @program: RPC program for NFSv4 callback service
4076 * @port: IP port number for NFS4 callback service
4077 * @cred: RPC credential to use for this call
4078 * @res: where to place the result
4080 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4082 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4083 unsigned short port, struct rpc_cred *cred,
4084 struct nfs4_setclientid_res *res)
4086 nfs4_verifier sc_verifier;
4087 struct nfs4_setclientid setclientid = {
4088 .sc_verifier = &sc_verifier,
4089 .sc_prog = program,
4090 .sc_cb_ident = clp->cl_cb_ident,
4092 struct rpc_message msg = {
4093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4094 .rpc_argp = &setclientid,
4095 .rpc_resp = res,
4096 .rpc_cred = cred,
4098 int status;
4100 /* nfs_client_id4 */
4101 nfs4_init_boot_verifier(clp, &sc_verifier);
4102 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4103 setclientid.sc_name_len =
4104 nfs4_init_uniform_client_string(clp,
4105 setclientid.sc_name,
4106 sizeof(setclientid.sc_name));
4107 else
4108 setclientid.sc_name_len =
4109 nfs4_init_nonuniform_client_string(clp,
4110 setclientid.sc_name,
4111 sizeof(setclientid.sc_name));
4112 /* cb_client4 */
4113 rcu_read_lock();
4114 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
4115 sizeof(setclientid.sc_netid),
4116 rpc_peeraddr2str(clp->cl_rpcclient,
4117 RPC_DISPLAY_NETID));
4118 rcu_read_unlock();
4119 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
4120 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
4121 clp->cl_ipaddr, port >> 8, port & 255);
4123 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4124 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4125 setclientid.sc_name_len, setclientid.sc_name);
4126 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4127 dprintk("NFS reply setclientid: %d\n", status);
4128 return status;
4132 * nfs4_proc_setclientid_confirm - Confirm client ID
4133 * @clp: state data structure
4134 * @res: result of a previous SETCLIENTID
4135 * @cred: RPC credential to use for this call
4137 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4139 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
4140 struct nfs4_setclientid_res *arg,
4141 struct rpc_cred *cred)
4143 struct nfs_fsinfo fsinfo;
4144 struct rpc_message msg = {
4145 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
4146 .rpc_argp = arg,
4147 .rpc_resp = &fsinfo,
4148 .rpc_cred = cred,
4150 unsigned long now;
4151 int status;
4153 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4154 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4155 clp->cl_clientid);
4156 now = jiffies;
4157 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4158 if (status == 0) {
4159 spin_lock(&clp->cl_lock);
4160 clp->cl_lease_time = fsinfo.lease_time * HZ;
4161 clp->cl_last_renewal = now;
4162 spin_unlock(&clp->cl_lock);
4164 dprintk("NFS reply setclientid_confirm: %d\n", status);
4165 return status;
4168 struct nfs4_delegreturndata {
4169 struct nfs4_delegreturnargs args;
4170 struct nfs4_delegreturnres res;
4171 struct nfs_fh fh;
4172 nfs4_stateid stateid;
4173 unsigned long timestamp;
4174 struct nfs_fattr fattr;
4175 int rpc_status;
4178 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4180 struct nfs4_delegreturndata *data = calldata;
4182 if (!nfs4_sequence_done(task, &data->res.seq_res))
4183 return;
4185 switch (task->tk_status) {
4186 case -NFS4ERR_STALE_STATEID:
4187 case -NFS4ERR_EXPIRED:
4188 case 0:
4189 renew_lease(data->res.server, data->timestamp);
4190 break;
4191 default:
4192 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4193 -EAGAIN) {
4194 rpc_restart_call_prepare(task);
4195 return;
4198 data->rpc_status = task->tk_status;
4201 static void nfs4_delegreturn_release(void *calldata)
4203 kfree(calldata);
4206 #if defined(CONFIG_NFS_V4_1)
4207 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4209 struct nfs4_delegreturndata *d_data;
4211 d_data = (struct nfs4_delegreturndata *)data;
4213 nfs4_setup_sequence(d_data->res.server,
4214 &d_data->args.seq_args,
4215 &d_data->res.seq_res,
4216 task);
4218 #endif /* CONFIG_NFS_V4_1 */
4220 static const struct rpc_call_ops nfs4_delegreturn_ops = {
4221 #if defined(CONFIG_NFS_V4_1)
4222 .rpc_call_prepare = nfs4_delegreturn_prepare,
4223 #endif /* CONFIG_NFS_V4_1 */
4224 .rpc_call_done = nfs4_delegreturn_done,
4225 .rpc_release = nfs4_delegreturn_release,
4228 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4230 struct nfs4_delegreturndata *data;
4231 struct nfs_server *server = NFS_SERVER(inode);
4232 struct rpc_task *task;
4233 struct rpc_message msg = {
4234 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4235 .rpc_cred = cred,
4237 struct rpc_task_setup task_setup_data = {
4238 .rpc_client = server->client,
4239 .rpc_message = &msg,
4240 .callback_ops = &nfs4_delegreturn_ops,
4241 .flags = RPC_TASK_ASYNC,
4243 int status = 0;
4245 data = kzalloc(sizeof(*data), GFP_NOFS);
4246 if (data == NULL)
4247 return -ENOMEM;
4248 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4249 data->args.fhandle = &data->fh;
4250 data->args.stateid = &data->stateid;
4251 data->args.bitmask = server->cache_consistency_bitmask;
4252 nfs_copy_fh(&data->fh, NFS_FH(inode));
4253 nfs4_stateid_copy(&data->stateid, stateid);
4254 data->res.fattr = &data->fattr;
4255 data->res.server = server;
4256 nfs_fattr_init(data->res.fattr);
4257 data->timestamp = jiffies;
4258 data->rpc_status = 0;
4260 task_setup_data.callback_data = data;
4261 msg.rpc_argp = &data->args;
4262 msg.rpc_resp = &data->res;
4263 task = rpc_run_task(&task_setup_data);
4264 if (IS_ERR(task))
4265 return PTR_ERR(task);
4266 if (!issync)
4267 goto out;
4268 status = nfs4_wait_for_completion_rpc_task(task);
4269 if (status != 0)
4270 goto out;
4271 status = data->rpc_status;
4272 if (status == 0)
4273 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4274 else
4275 nfs_refresh_inode(inode, &data->fattr);
4276 out:
4277 rpc_put_task(task);
4278 return status;
4281 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
4283 struct nfs_server *server = NFS_SERVER(inode);
4284 struct nfs4_exception exception = { };
4285 int err;
4286 do {
4287 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
4288 switch (err) {
4289 case -NFS4ERR_STALE_STATEID:
4290 case -NFS4ERR_EXPIRED:
4291 case 0:
4292 return 0;
4294 err = nfs4_handle_exception(server, err, &exception);
4295 } while (exception.retry);
4296 return err;
4299 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4300 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4303 * sleep, with exponential backoff, and retry the LOCK operation.
4305 static unsigned long
4306 nfs4_set_lock_task_retry(unsigned long timeout)
4308 freezable_schedule_timeout_killable(timeout);
4309 timeout <<= 1;
4310 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4311 return NFS4_LOCK_MAXTIMEOUT;
4312 return timeout;
4315 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4317 struct inode *inode = state->inode;
4318 struct nfs_server *server = NFS_SERVER(inode);
4319 struct nfs_client *clp = server->nfs_client;
4320 struct nfs_lockt_args arg = {
4321 .fh = NFS_FH(inode),
4322 .fl = request,
4324 struct nfs_lockt_res res = {
4325 .denied = request,
4327 struct rpc_message msg = {
4328 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4329 .rpc_argp = &arg,
4330 .rpc_resp = &res,
4331 .rpc_cred = state->owner->so_cred,
4333 struct nfs4_lock_state *lsp;
4334 int status;
4336 arg.lock_owner.clientid = clp->cl_clientid;
4337 status = nfs4_set_lock_state(state, request);
4338 if (status != 0)
4339 goto out;
4340 lsp = request->fl_u.nfs4_fl.owner;
4341 arg.lock_owner.id = lsp->ls_seqid.owner_id;
4342 arg.lock_owner.s_dev = server->s_dev;
4343 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4344 switch (status) {
4345 case 0:
4346 request->fl_type = F_UNLCK;
4347 break;
4348 case -NFS4ERR_DENIED:
4349 status = 0;
4351 request->fl_ops->fl_release_private(request);
4352 out:
4353 return status;
4356 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4358 struct nfs4_exception exception = { };
4359 int err;
4361 do {
4362 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4363 _nfs4_proc_getlk(state, cmd, request),
4364 &exception);
4365 } while (exception.retry);
4366 return err;
4369 static int do_vfs_lock(struct file *file, struct file_lock *fl)
4371 int res = 0;
4372 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4373 case FL_POSIX:
4374 res = posix_lock_file_wait(file, fl);
4375 break;
4376 case FL_FLOCK:
4377 res = flock_lock_file_wait(file, fl);
4378 break;
4379 default:
4380 BUG();
4382 return res;
4385 struct nfs4_unlockdata {
4386 struct nfs_locku_args arg;
4387 struct nfs_locku_res res;
4388 struct nfs4_lock_state *lsp;
4389 struct nfs_open_context *ctx;
4390 struct file_lock fl;
4391 const struct nfs_server *server;
4392 unsigned long timestamp;
4395 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4396 struct nfs_open_context *ctx,
4397 struct nfs4_lock_state *lsp,
4398 struct nfs_seqid *seqid)
4400 struct nfs4_unlockdata *p;
4401 struct inode *inode = lsp->ls_state->inode;
4403 p = kzalloc(sizeof(*p), GFP_NOFS);
4404 if (p == NULL)
4405 return NULL;
4406 p->arg.fh = NFS_FH(inode);
4407 p->arg.fl = &p->fl;
4408 p->arg.seqid = seqid;
4409 p->res.seqid = seqid;
4410 p->arg.stateid = &lsp->ls_stateid;
4411 p->lsp = lsp;
4412 atomic_inc(&lsp->ls_count);
4413 /* Ensure we don't close file until we're done freeing locks! */
4414 p->ctx = get_nfs_open_context(ctx);
4415 memcpy(&p->fl, fl, sizeof(p->fl));
4416 p->server = NFS_SERVER(inode);
4417 return p;
4420 static void nfs4_locku_release_calldata(void *data)
4422 struct nfs4_unlockdata *calldata = data;
4423 nfs_free_seqid(calldata->arg.seqid);
4424 nfs4_put_lock_state(calldata->lsp);
4425 put_nfs_open_context(calldata->ctx);
4426 kfree(calldata);
4429 static void nfs4_locku_done(struct rpc_task *task, void *data)
4431 struct nfs4_unlockdata *calldata = data;
4433 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4434 return;
4435 switch (task->tk_status) {
4436 case 0:
4437 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4438 &calldata->res.stateid);
4439 renew_lease(calldata->server, calldata->timestamp);
4440 break;
4441 case -NFS4ERR_BAD_STATEID:
4442 case -NFS4ERR_OLD_STATEID:
4443 case -NFS4ERR_STALE_STATEID:
4444 case -NFS4ERR_EXPIRED:
4445 break;
4446 default:
4447 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
4448 rpc_restart_call_prepare(task);
4450 nfs_release_seqid(calldata->arg.seqid);
4453 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
4455 struct nfs4_unlockdata *calldata = data;
4457 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
4458 goto out_wait;
4459 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
4460 /* Note: exit _without_ running nfs4_locku_done */
4461 goto out_no_action;
4463 calldata->timestamp = jiffies;
4464 if (nfs4_setup_sequence(calldata->server,
4465 &calldata->arg.seq_args,
4466 &calldata->res.seq_res,
4467 task) != 0)
4468 nfs_release_seqid(calldata->arg.seqid);
4469 return;
4470 out_no_action:
4471 task->tk_action = NULL;
4472 out_wait:
4473 nfs4_sequence_done(task, &calldata->res.seq_res);
4476 static const struct rpc_call_ops nfs4_locku_ops = {
4477 .rpc_call_prepare = nfs4_locku_prepare,
4478 .rpc_call_done = nfs4_locku_done,
4479 .rpc_release = nfs4_locku_release_calldata,
4482 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4483 struct nfs_open_context *ctx,
4484 struct nfs4_lock_state *lsp,
4485 struct nfs_seqid *seqid)
4487 struct nfs4_unlockdata *data;
4488 struct rpc_message msg = {
4489 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4490 .rpc_cred = ctx->cred,
4492 struct rpc_task_setup task_setup_data = {
4493 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
4494 .rpc_message = &msg,
4495 .callback_ops = &nfs4_locku_ops,
4496 .workqueue = nfsiod_workqueue,
4497 .flags = RPC_TASK_ASYNC,
4500 /* Ensure this is an unlock - when canceling a lock, the
4501 * canceled lock is passed in, and it won't be an unlock.
4503 fl->fl_type = F_UNLCK;
4505 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4506 if (data == NULL) {
4507 nfs_free_seqid(seqid);
4508 return ERR_PTR(-ENOMEM);
4511 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4512 msg.rpc_argp = &data->arg;
4513 msg.rpc_resp = &data->res;
4514 task_setup_data.callback_data = data;
4515 return rpc_run_task(&task_setup_data);
4518 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4520 struct inode *inode = state->inode;
4521 struct nfs4_state_owner *sp = state->owner;
4522 struct nfs_inode *nfsi = NFS_I(inode);
4523 struct nfs_seqid *seqid;
4524 struct nfs4_lock_state *lsp;
4525 struct rpc_task *task;
4526 int status = 0;
4527 unsigned char fl_flags = request->fl_flags;
4529 status = nfs4_set_lock_state(state, request);
4530 /* Unlock _before_ we do the RPC call */
4531 request->fl_flags |= FL_EXISTS;
4532 /* Exclude nfs_delegation_claim_locks() */
4533 mutex_lock(&sp->so_delegreturn_mutex);
4534 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
4535 down_read(&nfsi->rwsem);
4536 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4537 up_read(&nfsi->rwsem);
4538 mutex_unlock(&sp->so_delegreturn_mutex);
4539 goto out;
4541 up_read(&nfsi->rwsem);
4542 mutex_unlock(&sp->so_delegreturn_mutex);
4543 if (status != 0)
4544 goto out;
4545 /* Is this a delegated lock? */
4546 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
4547 goto out;
4548 lsp = request->fl_u.nfs4_fl.owner;
4549 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
4550 status = -ENOMEM;
4551 if (seqid == NULL)
4552 goto out;
4553 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
4554 status = PTR_ERR(task);
4555 if (IS_ERR(task))
4556 goto out;
4557 status = nfs4_wait_for_completion_rpc_task(task);
4558 rpc_put_task(task);
4559 out:
4560 request->fl_flags = fl_flags;
4561 return status;
4564 struct nfs4_lockdata {
4565 struct nfs_lock_args arg;
4566 struct nfs_lock_res res;
4567 struct nfs4_lock_state *lsp;
4568 struct nfs_open_context *ctx;
4569 struct file_lock fl;
4570 unsigned long timestamp;
4571 int rpc_status;
4572 int cancelled;
4573 struct nfs_server *server;
4576 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
4577 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4578 gfp_t gfp_mask)
4580 struct nfs4_lockdata *p;
4581 struct inode *inode = lsp->ls_state->inode;
4582 struct nfs_server *server = NFS_SERVER(inode);
4584 p = kzalloc(sizeof(*p), gfp_mask);
4585 if (p == NULL)
4586 return NULL;
4588 p->arg.fh = NFS_FH(inode);
4589 p->arg.fl = &p->fl;
4590 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
4591 if (p->arg.open_seqid == NULL)
4592 goto out_free;
4593 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
4594 if (p->arg.lock_seqid == NULL)
4595 goto out_free_seqid;
4596 p->arg.lock_stateid = &lsp->ls_stateid;
4597 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
4598 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
4599 p->arg.lock_owner.s_dev = server->s_dev;
4600 p->res.lock_seqid = p->arg.lock_seqid;
4601 p->lsp = lsp;
4602 p->server = server;
4603 atomic_inc(&lsp->ls_count);
4604 p->ctx = get_nfs_open_context(ctx);
4605 memcpy(&p->fl, fl, sizeof(p->fl));
4606 return p;
4607 out_free_seqid:
4608 nfs_free_seqid(p->arg.open_seqid);
4609 out_free:
4610 kfree(p);
4611 return NULL;
4614 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4616 struct nfs4_lockdata *data = calldata;
4617 struct nfs4_state *state = data->lsp->ls_state;
4619 dprintk("%s: begin!\n", __func__);
4620 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4621 goto out_wait;
4622 /* Do we need to do an open_to_lock_owner? */
4623 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
4624 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
4625 goto out_release_lock_seqid;
4627 data->arg.open_stateid = &state->stateid;
4628 data->arg.new_lock_owner = 1;
4629 data->res.open_seqid = data->arg.open_seqid;
4630 } else
4631 data->arg.new_lock_owner = 0;
4632 data->timestamp = jiffies;
4633 if (nfs4_setup_sequence(data->server,
4634 &data->arg.seq_args,
4635 &data->res.seq_res,
4636 task) == 0)
4637 return;
4638 nfs_release_seqid(data->arg.open_seqid);
4639 out_release_lock_seqid:
4640 nfs_release_seqid(data->arg.lock_seqid);
4641 out_wait:
4642 nfs4_sequence_done(task, &data->res.seq_res);
4643 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
4646 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4648 struct nfs4_lockdata *data = calldata;
4650 dprintk("%s: begin!\n", __func__);
4652 if (!nfs4_sequence_done(task, &data->res.seq_res))
4653 return;
4655 data->rpc_status = task->tk_status;
4656 if (data->arg.new_lock_owner != 0) {
4657 if (data->rpc_status == 0)
4658 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4659 else
4660 goto out;
4662 if (data->rpc_status == 0) {
4663 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
4664 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
4665 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
4667 out:
4668 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
4671 static void nfs4_lock_release(void *calldata)
4673 struct nfs4_lockdata *data = calldata;
4675 dprintk("%s: begin!\n", __func__);
4676 nfs_free_seqid(data->arg.open_seqid);
4677 if (data->cancelled != 0) {
4678 struct rpc_task *task;
4679 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4680 data->arg.lock_seqid);
4681 if (!IS_ERR(task))
4682 rpc_put_task_async(task);
4683 dprintk("%s: cancelling lock!\n", __func__);
4684 } else
4685 nfs_free_seqid(data->arg.lock_seqid);
4686 nfs4_put_lock_state(data->lsp);
4687 put_nfs_open_context(data->ctx);
4688 kfree(data);
4689 dprintk("%s: done!\n", __func__);
4692 static const struct rpc_call_ops nfs4_lock_ops = {
4693 .rpc_call_prepare = nfs4_lock_prepare,
4694 .rpc_call_done = nfs4_lock_done,
4695 .rpc_release = nfs4_lock_release,
4698 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4700 switch (error) {
4701 case -NFS4ERR_ADMIN_REVOKED:
4702 case -NFS4ERR_BAD_STATEID:
4703 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4704 if (new_lock_owner != 0 ||
4705 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
4706 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
4707 break;
4708 case -NFS4ERR_STALE_STATEID:
4709 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
4710 case -NFS4ERR_EXPIRED:
4711 nfs4_schedule_lease_recovery(server->nfs_client);
4715 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
4717 struct nfs4_lockdata *data;
4718 struct rpc_task *task;
4719 struct rpc_message msg = {
4720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4721 .rpc_cred = state->owner->so_cred,
4723 struct rpc_task_setup task_setup_data = {
4724 .rpc_client = NFS_CLIENT(state->inode),
4725 .rpc_message = &msg,
4726 .callback_ops = &nfs4_lock_ops,
4727 .workqueue = nfsiod_workqueue,
4728 .flags = RPC_TASK_ASYNC,
4730 int ret;
4732 dprintk("%s: begin!\n", __func__);
4733 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
4734 fl->fl_u.nfs4_fl.owner,
4735 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
4736 if (data == NULL)
4737 return -ENOMEM;
4738 if (IS_SETLKW(cmd))
4739 data->arg.block = 1;
4740 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
4741 msg.rpc_argp = &data->arg;
4742 msg.rpc_resp = &data->res;
4743 task_setup_data.callback_data = data;
4744 if (recovery_type > NFS_LOCK_NEW) {
4745 if (recovery_type == NFS_LOCK_RECLAIM)
4746 data->arg.reclaim = NFS_LOCK_RECLAIM;
4747 nfs4_set_sequence_privileged(&data->arg.seq_args);
4749 task = rpc_run_task(&task_setup_data);
4750 if (IS_ERR(task))
4751 return PTR_ERR(task);
4752 ret = nfs4_wait_for_completion_rpc_task(task);
4753 if (ret == 0) {
4754 ret = data->rpc_status;
4755 if (ret)
4756 nfs4_handle_setlk_error(data->server, data->lsp,
4757 data->arg.new_lock_owner, ret);
4758 } else
4759 data->cancelled = 1;
4760 rpc_put_task(task);
4761 dprintk("%s: done, ret = %d!\n", __func__, ret);
4762 return ret;
4765 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4767 struct nfs_server *server = NFS_SERVER(state->inode);
4768 struct nfs4_exception exception = {
4769 .inode = state->inode,
4771 int err;
4773 do {
4774 /* Cache the lock if possible... */
4775 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4776 return 0;
4777 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
4778 if (err != -NFS4ERR_DELAY)
4779 break;
4780 nfs4_handle_exception(server, err, &exception);
4781 } while (exception.retry);
4782 return err;
4785 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4787 struct nfs_server *server = NFS_SERVER(state->inode);
4788 struct nfs4_exception exception = {
4789 .inode = state->inode,
4791 int err;
4793 err = nfs4_set_lock_state(state, request);
4794 if (err != 0)
4795 return err;
4796 do {
4797 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4798 return 0;
4799 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
4800 switch (err) {
4801 default:
4802 goto out;
4803 case -NFS4ERR_GRACE:
4804 case -NFS4ERR_DELAY:
4805 nfs4_handle_exception(server, err, &exception);
4806 err = 0;
4808 } while (exception.retry);
4809 out:
4810 return err;
4813 #if defined(CONFIG_NFS_V4_1)
4815 * nfs41_check_expired_locks - possibly free a lock stateid
4817 * @state: NFSv4 state for an inode
4819 * Returns NFS_OK if recovery for this stateid is now finished.
4820 * Otherwise a negative NFS4ERR value is returned.
4822 static int nfs41_check_expired_locks(struct nfs4_state *state)
4824 int status, ret = -NFS4ERR_BAD_STATEID;
4825 struct nfs4_lock_state *lsp;
4826 struct nfs_server *server = NFS_SERVER(state->inode);
4828 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4829 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
4830 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4831 if (status != NFS_OK) {
4832 /* Free the stateid unless the server
4833 * informs us the stateid is unrecognized. */
4834 if (status != -NFS4ERR_BAD_STATEID)
4835 nfs41_free_stateid(server,
4836 &lsp->ls_stateid);
4837 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
4838 ret = status;
4843 return ret;
4846 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4848 int status = NFS_OK;
4850 if (test_bit(LK_STATE_IN_USE, &state->flags))
4851 status = nfs41_check_expired_locks(state);
4852 if (status != NFS_OK)
4853 status = nfs4_lock_expired(state, request);
4854 return status;
4856 #endif
4858 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4860 struct nfs4_state_owner *sp = state->owner;
4861 struct nfs_inode *nfsi = NFS_I(state->inode);
4862 unsigned char fl_flags = request->fl_flags;
4863 unsigned int seq;
4864 int status = -ENOLCK;
4866 if ((fl_flags & FL_POSIX) &&
4867 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4868 goto out;
4869 /* Is this a delegated open? */
4870 status = nfs4_set_lock_state(state, request);
4871 if (status != 0)
4872 goto out;
4873 request->fl_flags |= FL_ACCESS;
4874 status = do_vfs_lock(request->fl_file, request);
4875 if (status < 0)
4876 goto out;
4877 down_read(&nfsi->rwsem);
4878 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
4879 /* Yes: cache locks! */
4880 /* ...but avoid races with delegation recall... */
4881 request->fl_flags = fl_flags & ~FL_SLEEP;
4882 status = do_vfs_lock(request->fl_file, request);
4883 goto out_unlock;
4885 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
4886 up_read(&nfsi->rwsem);
4887 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
4888 if (status != 0)
4889 goto out;
4890 down_read(&nfsi->rwsem);
4891 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
4892 status = -NFS4ERR_DELAY;
4893 goto out_unlock;
4895 /* Note: we always want to sleep here! */
4896 request->fl_flags = fl_flags | FL_SLEEP;
4897 if (do_vfs_lock(request->fl_file, request) < 0)
4898 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4899 "manager!\n", __func__);
4900 out_unlock:
4901 up_read(&nfsi->rwsem);
4902 out:
4903 request->fl_flags = fl_flags;
4904 return status;
4907 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4909 struct nfs4_exception exception = {
4910 .state = state,
4911 .inode = state->inode,
4913 int err;
4915 do {
4916 err = _nfs4_proc_setlk(state, cmd, request);
4917 if (err == -NFS4ERR_DENIED)
4918 err = -EAGAIN;
4919 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4920 err, &exception);
4921 } while (exception.retry);
4922 return err;
4925 static int
4926 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4928 struct nfs_open_context *ctx;
4929 struct nfs4_state *state;
4930 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4931 int status;
4933 /* verify open state */
4934 ctx = nfs_file_open_context(filp);
4935 state = ctx->state;
4937 if (request->fl_start < 0 || request->fl_end < 0)
4938 return -EINVAL;
4940 if (IS_GETLK(cmd)) {
4941 if (state != NULL)
4942 return nfs4_proc_getlk(state, F_GETLK, request);
4943 return 0;
4946 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4947 return -EINVAL;
4949 if (request->fl_type == F_UNLCK) {
4950 if (state != NULL)
4951 return nfs4_proc_unlck(state, cmd, request);
4952 return 0;
4955 if (state == NULL)
4956 return -ENOLCK;
4958 * Don't rely on the VFS having checked the file open mode,
4959 * since it won't do this for flock() locks.
4961 switch (request->fl_type) {
4962 case F_RDLCK:
4963 if (!(filp->f_mode & FMODE_READ))
4964 return -EBADF;
4965 break;
4966 case F_WRLCK:
4967 if (!(filp->f_mode & FMODE_WRITE))
4968 return -EBADF;
4971 do {
4972 status = nfs4_proc_setlk(state, cmd, request);
4973 if ((status != -EAGAIN) || IS_SETLK(cmd))
4974 break;
4975 timeout = nfs4_set_lock_task_retry(timeout);
4976 status = -ERESTARTSYS;
4977 if (signalled())
4978 break;
4979 } while(status < 0);
4980 return status;
4983 int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4985 struct nfs_server *server = NFS_SERVER(state->inode);
4986 struct nfs4_exception exception = { };
4987 int err;
4989 err = nfs4_set_lock_state(state, fl);
4990 if (err != 0)
4991 goto out;
4992 do {
4993 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
4994 switch (err) {
4995 default:
4996 printk(KERN_ERR "NFS: %s: unhandled error "
4997 "%d.\n", __func__, err);
4998 case 0:
4999 case -ESTALE:
5000 goto out;
5001 case -NFS4ERR_STALE_CLIENTID:
5002 case -NFS4ERR_STALE_STATEID:
5003 set_bit(NFS_DELEGATED_STATE, &state->flags);
5004 case -NFS4ERR_EXPIRED:
5005 nfs4_schedule_lease_recovery(server->nfs_client);
5006 err = -EAGAIN;
5007 goto out;
5008 case -NFS4ERR_BADSESSION:
5009 case -NFS4ERR_BADSLOT:
5010 case -NFS4ERR_BAD_HIGH_SLOT:
5011 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
5012 case -NFS4ERR_DEADSESSION:
5013 set_bit(NFS_DELEGATED_STATE, &state->flags);
5014 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
5015 err = -EAGAIN;
5016 goto out;
5017 case -NFS4ERR_DELEG_REVOKED:
5018 case -NFS4ERR_ADMIN_REVOKED:
5019 case -NFS4ERR_BAD_STATEID:
5020 case -NFS4ERR_OPENMODE:
5021 nfs4_schedule_stateid_recovery(server, state);
5022 err = 0;
5023 goto out;
5024 case -ENOMEM:
5025 case -NFS4ERR_DENIED:
5026 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
5027 err = 0;
5028 goto out;
5030 set_bit(NFS_DELEGATED_STATE, &state->flags);
5031 err = nfs4_handle_exception(server, err, &exception);
5032 } while (exception.retry);
5033 out:
5034 return err;
5037 struct nfs_release_lockowner_data {
5038 struct nfs4_lock_state *lsp;
5039 struct nfs_server *server;
5040 struct nfs_release_lockowner_args args;
5043 static void nfs4_release_lockowner_release(void *calldata)
5045 struct nfs_release_lockowner_data *data = calldata;
5046 nfs4_free_lock_state(data->server, data->lsp);
5047 kfree(calldata);
5050 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
5051 .rpc_release = nfs4_release_lockowner_release,
5054 int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
5056 struct nfs_server *server = lsp->ls_state->owner->so_server;
5057 struct nfs_release_lockowner_data *data;
5058 struct rpc_message msg = {
5059 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5062 if (server->nfs_client->cl_mvops->minor_version != 0)
5063 return -EINVAL;
5064 data = kmalloc(sizeof(*data), GFP_NOFS);
5065 if (!data)
5066 return -ENOMEM;
5067 data->lsp = lsp;
5068 data->server = server;
5069 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5070 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5071 data->args.lock_owner.s_dev = server->s_dev;
5072 msg.rpc_argp = &data->args;
5073 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5074 return 0;
5077 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5079 static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5080 const void *buf, size_t buflen,
5081 int flags, int type)
5083 if (strcmp(key, "") != 0)
5084 return -EINVAL;
5086 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
5089 static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5090 void *buf, size_t buflen, int type)
5092 if (strcmp(key, "") != 0)
5093 return -EINVAL;
5095 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
5098 static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5099 size_t list_len, const char *name,
5100 size_t name_len, int type)
5102 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
5104 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5105 return 0;
5107 if (list && len <= list_len)
5108 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
5109 return len;
5113 * nfs_fhget will use either the mounted_on_fileid or the fileid
5115 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5117 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5118 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5119 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
5120 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
5121 return;
5123 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
5124 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
5125 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5126 fattr->nlink = 2;
5129 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5130 const struct qstr *name,
5131 struct nfs4_fs_locations *fs_locations,
5132 struct page *page)
5134 struct nfs_server *server = NFS_SERVER(dir);
5135 u32 bitmask[2] = {
5136 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
5138 struct nfs4_fs_locations_arg args = {
5139 .dir_fh = NFS_FH(dir),
5140 .name = name,
5141 .page = page,
5142 .bitmask = bitmask,
5144 struct nfs4_fs_locations_res res = {
5145 .fs_locations = fs_locations,
5147 struct rpc_message msg = {
5148 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5149 .rpc_argp = &args,
5150 .rpc_resp = &res,
5152 int status;
5154 dprintk("%s: start\n", __func__);
5156 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5157 * is not supported */
5158 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5159 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5160 else
5161 bitmask[0] |= FATTR4_WORD0_FILEID;
5163 nfs_fattr_init(&fs_locations->fattr);
5164 fs_locations->server = server;
5165 fs_locations->nlocations = 0;
5166 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
5167 dprintk("%s: returned status = %d\n", __func__, status);
5168 return status;
5171 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5172 const struct qstr *name,
5173 struct nfs4_fs_locations *fs_locations,
5174 struct page *page)
5176 struct nfs4_exception exception = { };
5177 int err;
5178 do {
5179 err = nfs4_handle_exception(NFS_SERVER(dir),
5180 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
5181 &exception);
5182 } while (exception.retry);
5183 return err;
5186 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5188 int status;
5189 struct nfs4_secinfo_arg args = {
5190 .dir_fh = NFS_FH(dir),
5191 .name = name,
5193 struct nfs4_secinfo_res res = {
5194 .flavors = flavors,
5196 struct rpc_message msg = {
5197 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5198 .rpc_argp = &args,
5199 .rpc_resp = &res,
5202 dprintk("NFS call secinfo %s\n", name->name);
5203 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5204 dprintk("NFS reply secinfo: %d\n", status);
5205 return status;
5208 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5209 struct nfs4_secinfo_flavors *flavors)
5211 struct nfs4_exception exception = { };
5212 int err;
5213 do {
5214 err = nfs4_handle_exception(NFS_SERVER(dir),
5215 _nfs4_proc_secinfo(dir, name, flavors),
5216 &exception);
5217 } while (exception.retry);
5218 return err;
5221 #ifdef CONFIG_NFS_V4_1
5223 * Check the exchange flags returned by the server for invalid flags, having
5224 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5225 * DS flags set.
5227 static int nfs4_check_cl_exchange_flags(u32 flags)
5229 if (flags & ~EXCHGID4_FLAG_MASK_R)
5230 goto out_inval;
5231 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5232 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5233 goto out_inval;
5234 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5235 goto out_inval;
5236 return NFS_OK;
5237 out_inval:
5238 return -NFS4ERR_INVAL;
5241 static bool
5242 nfs41_same_server_scope(struct nfs41_server_scope *a,
5243 struct nfs41_server_scope *b)
5245 if (a->server_scope_sz == b->server_scope_sz &&
5246 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5247 return true;
5249 return false;
5253 * nfs4_proc_bind_conn_to_session()
5255 * The 4.1 client currently uses the same TCP connection for the
5256 * fore and backchannel.
5258 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
5260 int status;
5261 struct nfs41_bind_conn_to_session_res res;
5262 struct rpc_message msg = {
5263 .rpc_proc =
5264 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5265 .rpc_argp = clp,
5266 .rpc_resp = &res,
5267 .rpc_cred = cred,
5270 dprintk("--> %s\n", __func__);
5272 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5273 if (unlikely(res.session == NULL)) {
5274 status = -ENOMEM;
5275 goto out;
5278 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5279 if (status == 0) {
5280 if (memcmp(res.session->sess_id.data,
5281 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5282 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5283 status = -EIO;
5284 goto out_session;
5286 if (res.dir != NFS4_CDFS4_BOTH) {
5287 dprintk("NFS: %s: Unexpected direction from server\n",
5288 __func__);
5289 status = -EIO;
5290 goto out_session;
5292 if (res.use_conn_in_rdma_mode) {
5293 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5294 __func__);
5295 status = -EIO;
5296 goto out_session;
5299 out_session:
5300 kfree(res.session);
5301 out:
5302 dprintk("<-- %s status= %d\n", __func__, status);
5303 return status;
5307 * nfs4_proc_exchange_id()
5309 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5311 * Since the clientid has expired, all compounds using sessions
5312 * associated with the stale clientid will be returning
5313 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5314 * be in some phase of session reset.
5316 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
5318 nfs4_verifier verifier;
5319 struct nfs41_exchange_id_args args = {
5320 .verifier = &verifier,
5321 .client = clp,
5322 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
5324 struct nfs41_exchange_id_res res = {
5327 int status;
5328 struct rpc_message msg = {
5329 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5330 .rpc_argp = &args,
5331 .rpc_resp = &res,
5332 .rpc_cred = cred,
5335 nfs4_init_boot_verifier(clp, &verifier);
5336 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5337 sizeof(args.id));
5338 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5339 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5340 args.id_len, args.id);
5342 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
5343 GFP_NOFS);
5344 if (unlikely(res.server_owner == NULL)) {
5345 status = -ENOMEM;
5346 goto out;
5349 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
5350 GFP_NOFS);
5351 if (unlikely(res.server_scope == NULL)) {
5352 status = -ENOMEM;
5353 goto out_server_owner;
5356 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
5357 if (unlikely(res.impl_id == NULL)) {
5358 status = -ENOMEM;
5359 goto out_server_scope;
5362 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5363 if (status == 0)
5364 status = nfs4_check_cl_exchange_flags(res.flags);
5366 if (status == 0) {
5367 clp->cl_clientid = res.clientid;
5368 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5369 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5370 clp->cl_seqid = res.seqid;
5372 kfree(clp->cl_serverowner);
5373 clp->cl_serverowner = res.server_owner;
5374 res.server_owner = NULL;
5376 /* use the most recent implementation id */
5377 kfree(clp->cl_implid);
5378 clp->cl_implid = res.impl_id;
5380 if (clp->cl_serverscope != NULL &&
5381 !nfs41_same_server_scope(clp->cl_serverscope,
5382 res.server_scope)) {
5383 dprintk("%s: server_scope mismatch detected\n",
5384 __func__);
5385 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5386 kfree(clp->cl_serverscope);
5387 clp->cl_serverscope = NULL;
5390 if (clp->cl_serverscope == NULL) {
5391 clp->cl_serverscope = res.server_scope;
5392 goto out;
5394 } else
5395 kfree(res.impl_id);
5397 out_server_owner:
5398 kfree(res.server_owner);
5399 out_server_scope:
5400 kfree(res.server_scope);
5401 out:
5402 if (clp->cl_implid != NULL)
5403 dprintk("NFS reply exchange_id: Server Implementation ID: "
5404 "domain: %s, name: %s, date: %llu,%u\n",
5405 clp->cl_implid->domain, clp->cl_implid->name,
5406 clp->cl_implid->date.seconds,
5407 clp->cl_implid->date.nseconds);
5408 dprintk("NFS reply exchange_id: %d\n", status);
5409 return status;
5412 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5413 struct rpc_cred *cred)
5415 struct rpc_message msg = {
5416 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5417 .rpc_argp = clp,
5418 .rpc_cred = cred,
5420 int status;
5422 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5423 if (status)
5424 dprintk("NFS: Got error %d from the server %s on "
5425 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5426 return status;
5429 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5430 struct rpc_cred *cred)
5432 unsigned int loop;
5433 int ret;
5435 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5436 ret = _nfs4_proc_destroy_clientid(clp, cred);
5437 switch (ret) {
5438 case -NFS4ERR_DELAY:
5439 case -NFS4ERR_CLIENTID_BUSY:
5440 ssleep(1);
5441 break;
5442 default:
5443 return ret;
5446 return 0;
5449 int nfs4_destroy_clientid(struct nfs_client *clp)
5451 struct rpc_cred *cred;
5452 int ret = 0;
5454 if (clp->cl_mvops->minor_version < 1)
5455 goto out;
5456 if (clp->cl_exchange_flags == 0)
5457 goto out;
5458 if (clp->cl_preserve_clid)
5459 goto out;
5460 cred = nfs4_get_exchange_id_cred(clp);
5461 ret = nfs4_proc_destroy_clientid(clp, cred);
5462 if (cred)
5463 put_rpccred(cred);
5464 switch (ret) {
5465 case 0:
5466 case -NFS4ERR_STALE_CLIENTID:
5467 clp->cl_exchange_flags = 0;
5469 out:
5470 return ret;
5473 struct nfs4_get_lease_time_data {
5474 struct nfs4_get_lease_time_args *args;
5475 struct nfs4_get_lease_time_res *res;
5476 struct nfs_client *clp;
5479 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5480 void *calldata)
5482 struct nfs4_get_lease_time_data *data =
5483 (struct nfs4_get_lease_time_data *)calldata;
5485 dprintk("--> %s\n", __func__);
5486 /* just setup sequence, do not trigger session recovery
5487 since we're invoked within one */
5488 nfs41_setup_sequence(data->clp->cl_session,
5489 &data->args->la_seq_args,
5490 &data->res->lr_seq_res,
5491 task);
5492 dprintk("<-- %s\n", __func__);
5496 * Called from nfs4_state_manager thread for session setup, so don't recover
5497 * from sequence operation or clientid errors.
5499 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5501 struct nfs4_get_lease_time_data *data =
5502 (struct nfs4_get_lease_time_data *)calldata;
5504 dprintk("--> %s\n", __func__);
5505 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5506 return;
5507 switch (task->tk_status) {
5508 case -NFS4ERR_DELAY:
5509 case -NFS4ERR_GRACE:
5510 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5511 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5512 task->tk_status = 0;
5513 /* fall through */
5514 case -NFS4ERR_RETRY_UNCACHED_REP:
5515 rpc_restart_call_prepare(task);
5516 return;
5518 dprintk("<-- %s\n", __func__);
5521 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
5522 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5523 .rpc_call_done = nfs4_get_lease_time_done,
5526 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5528 struct rpc_task *task;
5529 struct nfs4_get_lease_time_args args;
5530 struct nfs4_get_lease_time_res res = {
5531 .lr_fsinfo = fsinfo,
5533 struct nfs4_get_lease_time_data data = {
5534 .args = &args,
5535 .res = &res,
5536 .clp = clp,
5538 struct rpc_message msg = {
5539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5540 .rpc_argp = &args,
5541 .rpc_resp = &res,
5543 struct rpc_task_setup task_setup = {
5544 .rpc_client = clp->cl_rpcclient,
5545 .rpc_message = &msg,
5546 .callback_ops = &nfs4_get_lease_time_ops,
5547 .callback_data = &data,
5548 .flags = RPC_TASK_TIMEOUT,
5550 int status;
5552 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
5553 nfs4_set_sequence_privileged(&args.la_seq_args);
5554 dprintk("--> %s\n", __func__);
5555 task = rpc_run_task(&task_setup);
5557 if (IS_ERR(task))
5558 status = PTR_ERR(task);
5559 else {
5560 status = task->tk_status;
5561 rpc_put_task(task);
5563 dprintk("<-- %s return %d\n", __func__, status);
5565 return status;
5569 * Initialize the values to be used by the client in CREATE_SESSION
5570 * If nfs4_init_session set the fore channel request and response sizes,
5571 * use them.
5573 * Set the back channel max_resp_sz_cached to zero to force the client to
5574 * always set csa_cachethis to FALSE because the current implementation
5575 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5577 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5579 struct nfs4_session *session = args->client->cl_session;
5580 unsigned int mxrqst_sz = session->fc_target_max_rqst_sz,
5581 mxresp_sz = session->fc_target_max_resp_sz;
5583 if (mxrqst_sz == 0)
5584 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5585 if (mxresp_sz == 0)
5586 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5587 /* Fore channel attributes */
5588 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5589 args->fc_attrs.max_resp_sz = mxresp_sz;
5590 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5591 args->fc_attrs.max_reqs = max_session_slots;
5593 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
5594 "max_ops=%u max_reqs=%u\n",
5595 __func__,
5596 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
5597 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
5599 /* Back channel attributes */
5600 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5601 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5602 args->bc_attrs.max_resp_sz_cached = 0;
5603 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5604 args->bc_attrs.max_reqs = 1;
5606 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5607 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5608 __func__,
5609 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5610 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5611 args->bc_attrs.max_reqs);
5614 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5616 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5617 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5619 if (rcvd->max_resp_sz > sent->max_resp_sz)
5620 return -EINVAL;
5622 * Our requested max_ops is the minimum we need; we're not
5623 * prepared to break up compounds into smaller pieces than that.
5624 * So, no point even trying to continue if the server won't
5625 * cooperate:
5627 if (rcvd->max_ops < sent->max_ops)
5628 return -EINVAL;
5629 if (rcvd->max_reqs == 0)
5630 return -EINVAL;
5631 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5632 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
5633 return 0;
5636 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5638 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5639 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
5641 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5642 return -EINVAL;
5643 if (rcvd->max_resp_sz < sent->max_resp_sz)
5644 return -EINVAL;
5645 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5646 return -EINVAL;
5647 /* These would render the backchannel useless: */
5648 if (rcvd->max_ops != sent->max_ops)
5649 return -EINVAL;
5650 if (rcvd->max_reqs != sent->max_reqs)
5651 return -EINVAL;
5652 return 0;
5655 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5656 struct nfs4_session *session)
5658 int ret;
5660 ret = nfs4_verify_fore_channel_attrs(args, session);
5661 if (ret)
5662 return ret;
5663 return nfs4_verify_back_channel_attrs(args, session);
5666 static int _nfs4_proc_create_session(struct nfs_client *clp,
5667 struct rpc_cred *cred)
5669 struct nfs4_session *session = clp->cl_session;
5670 struct nfs41_create_session_args args = {
5671 .client = clp,
5672 .cb_program = NFS4_CALLBACK,
5674 struct nfs41_create_session_res res = {
5675 .client = clp,
5677 struct rpc_message msg = {
5678 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5679 .rpc_argp = &args,
5680 .rpc_resp = &res,
5681 .rpc_cred = cred,
5683 int status;
5685 nfs4_init_channel_attrs(&args);
5686 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
5688 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5690 if (!status) {
5691 /* Verify the session's negotiated channel_attrs values */
5692 status = nfs4_verify_channel_attrs(&args, session);
5693 /* Increment the clientid slot sequence id */
5694 clp->cl_seqid++;
5697 return status;
5701 * Issues a CREATE_SESSION operation to the server.
5702 * It is the responsibility of the caller to verify the session is
5703 * expired before calling this routine.
5705 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
5707 int status;
5708 unsigned *ptr;
5709 struct nfs4_session *session = clp->cl_session;
5711 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5713 status = _nfs4_proc_create_session(clp, cred);
5714 if (status)
5715 goto out;
5717 /* Init or reset the session slot tables */
5718 status = nfs4_setup_session_slot_tables(session);
5719 dprintk("slot table setup returned %d\n", status);
5720 if (status)
5721 goto out;
5723 ptr = (unsigned *)&session->sess_id.data[0];
5724 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5725 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
5726 out:
5727 dprintk("<-- %s\n", __func__);
5728 return status;
5732 * Issue the over-the-wire RPC DESTROY_SESSION.
5733 * The caller must serialize access to this routine.
5735 int nfs4_proc_destroy_session(struct nfs4_session *session,
5736 struct rpc_cred *cred)
5738 struct rpc_message msg = {
5739 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5740 .rpc_argp = session,
5741 .rpc_cred = cred,
5743 int status = 0;
5745 dprintk("--> nfs4_proc_destroy_session\n");
5747 /* session is still being setup */
5748 if (session->clp->cl_cons_state != NFS_CS_READY)
5749 return status;
5751 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5753 if (status)
5754 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
5755 "Session has been destroyed regardless...\n", status);
5757 dprintk("<-- nfs4_proc_destroy_session\n");
5758 return status;
5762 * Renew the cl_session lease.
5764 struct nfs4_sequence_data {
5765 struct nfs_client *clp;
5766 struct nfs4_sequence_args args;
5767 struct nfs4_sequence_res res;
5770 static void nfs41_sequence_release(void *data)
5772 struct nfs4_sequence_data *calldata = data;
5773 struct nfs_client *clp = calldata->clp;
5775 if (atomic_read(&clp->cl_count) > 1)
5776 nfs4_schedule_state_renewal(clp);
5777 nfs_put_client(clp);
5778 kfree(calldata);
5781 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5783 switch(task->tk_status) {
5784 case -NFS4ERR_DELAY:
5785 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5786 return -EAGAIN;
5787 default:
5788 nfs4_schedule_lease_recovery(clp);
5790 return 0;
5793 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
5795 struct nfs4_sequence_data *calldata = data;
5796 struct nfs_client *clp = calldata->clp;
5798 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5799 return;
5801 if (task->tk_status < 0) {
5802 dprintk("%s ERROR %d\n", __func__, task->tk_status);
5803 if (atomic_read(&clp->cl_count) == 1)
5804 goto out;
5806 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5807 rpc_restart_call_prepare(task);
5808 return;
5811 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
5812 out:
5813 dprintk("<-- %s\n", __func__);
5816 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5818 struct nfs4_sequence_data *calldata = data;
5819 struct nfs_client *clp = calldata->clp;
5820 struct nfs4_sequence_args *args;
5821 struct nfs4_sequence_res *res;
5823 args = task->tk_msg.rpc_argp;
5824 res = task->tk_msg.rpc_resp;
5826 nfs41_setup_sequence(clp->cl_session, args, res, task);
5829 static const struct rpc_call_ops nfs41_sequence_ops = {
5830 .rpc_call_done = nfs41_sequence_call_done,
5831 .rpc_call_prepare = nfs41_sequence_prepare,
5832 .rpc_release = nfs41_sequence_release,
5835 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
5836 struct rpc_cred *cred,
5837 bool is_privileged)
5839 struct nfs4_sequence_data *calldata;
5840 struct rpc_message msg = {
5841 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5842 .rpc_cred = cred,
5844 struct rpc_task_setup task_setup_data = {
5845 .rpc_client = clp->cl_rpcclient,
5846 .rpc_message = &msg,
5847 .callback_ops = &nfs41_sequence_ops,
5848 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5851 if (!atomic_inc_not_zero(&clp->cl_count))
5852 return ERR_PTR(-EIO);
5853 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5854 if (calldata == NULL) {
5855 nfs_put_client(clp);
5856 return ERR_PTR(-ENOMEM);
5858 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
5859 if (is_privileged)
5860 nfs4_set_sequence_privileged(&calldata->args);
5861 msg.rpc_argp = &calldata->args;
5862 msg.rpc_resp = &calldata->res;
5863 calldata->clp = clp;
5864 task_setup_data.callback_data = calldata;
5866 return rpc_run_task(&task_setup_data);
5869 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5871 struct rpc_task *task;
5872 int ret = 0;
5874 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5875 return 0;
5876 task = _nfs41_proc_sequence(clp, cred, false);
5877 if (IS_ERR(task))
5878 ret = PTR_ERR(task);
5879 else
5880 rpc_put_task_async(task);
5881 dprintk("<-- %s status=%d\n", __func__, ret);
5882 return ret;
5885 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5887 struct rpc_task *task;
5888 int ret;
5890 task = _nfs41_proc_sequence(clp, cred, true);
5891 if (IS_ERR(task)) {
5892 ret = PTR_ERR(task);
5893 goto out;
5895 ret = rpc_wait_for_completion_task(task);
5896 if (!ret) {
5897 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5899 if (task->tk_status == 0)
5900 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
5901 ret = task->tk_status;
5903 rpc_put_task(task);
5904 out:
5905 dprintk("<-- %s status=%d\n", __func__, ret);
5906 return ret;
5909 struct nfs4_reclaim_complete_data {
5910 struct nfs_client *clp;
5911 struct nfs41_reclaim_complete_args arg;
5912 struct nfs41_reclaim_complete_res res;
5915 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5917 struct nfs4_reclaim_complete_data *calldata = data;
5919 nfs41_setup_sequence(calldata->clp->cl_session,
5920 &calldata->arg.seq_args,
5921 &calldata->res.seq_res,
5922 task);
5925 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5927 switch(task->tk_status) {
5928 case 0:
5929 case -NFS4ERR_COMPLETE_ALREADY:
5930 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5931 break;
5932 case -NFS4ERR_DELAY:
5933 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5934 /* fall through */
5935 case -NFS4ERR_RETRY_UNCACHED_REP:
5936 return -EAGAIN;
5937 default:
5938 nfs4_schedule_lease_recovery(clp);
5940 return 0;
5943 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5945 struct nfs4_reclaim_complete_data *calldata = data;
5946 struct nfs_client *clp = calldata->clp;
5947 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5949 dprintk("--> %s\n", __func__);
5950 if (!nfs41_sequence_done(task, res))
5951 return;
5953 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5954 rpc_restart_call_prepare(task);
5955 return;
5957 dprintk("<-- %s\n", __func__);
5960 static void nfs4_free_reclaim_complete_data(void *data)
5962 struct nfs4_reclaim_complete_data *calldata = data;
5964 kfree(calldata);
5967 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5968 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5969 .rpc_call_done = nfs4_reclaim_complete_done,
5970 .rpc_release = nfs4_free_reclaim_complete_data,
5974 * Issue a global reclaim complete.
5976 static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5978 struct nfs4_reclaim_complete_data *calldata;
5979 struct rpc_task *task;
5980 struct rpc_message msg = {
5981 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5983 struct rpc_task_setup task_setup_data = {
5984 .rpc_client = clp->cl_rpcclient,
5985 .rpc_message = &msg,
5986 .callback_ops = &nfs4_reclaim_complete_call_ops,
5987 .flags = RPC_TASK_ASYNC,
5989 int status = -ENOMEM;
5991 dprintk("--> %s\n", __func__);
5992 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
5993 if (calldata == NULL)
5994 goto out;
5995 calldata->clp = clp;
5996 calldata->arg.one_fs = 0;
5998 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
5999 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
6000 msg.rpc_argp = &calldata->arg;
6001 msg.rpc_resp = &calldata->res;
6002 task_setup_data.callback_data = calldata;
6003 task = rpc_run_task(&task_setup_data);
6004 if (IS_ERR(task)) {
6005 status = PTR_ERR(task);
6006 goto out;
6008 status = nfs4_wait_for_completion_rpc_task(task);
6009 if (status == 0)
6010 status = task->tk_status;
6011 rpc_put_task(task);
6012 return 0;
6013 out:
6014 dprintk("<-- %s status=%d\n", __func__, status);
6015 return status;
6018 static void
6019 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6021 struct nfs4_layoutget *lgp = calldata;
6022 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6023 struct nfs4_session *session = nfs4_get_session(server);
6025 dprintk("--> %s\n", __func__);
6026 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6027 * right now covering the LAYOUTGET we are about to send.
6028 * However, that is not so catastrophic, and there seems
6029 * to be no way to prevent it completely.
6031 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
6032 &lgp->res.seq_res, task))
6033 return;
6034 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6035 NFS_I(lgp->args.inode)->layout,
6036 lgp->args.ctx->state)) {
6037 rpc_exit(task, NFS4_OK);
6041 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6043 struct nfs4_layoutget *lgp = calldata;
6044 struct inode *inode = lgp->args.inode;
6045 struct nfs_server *server = NFS_SERVER(inode);
6046 struct pnfs_layout_hdr *lo;
6047 struct nfs4_state *state = NULL;
6049 dprintk("--> %s\n", __func__);
6051 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
6052 goto out;
6054 switch (task->tk_status) {
6055 case 0:
6056 goto out;
6057 case -NFS4ERR_LAYOUTTRYLATER:
6058 case -NFS4ERR_RECALLCONFLICT:
6059 task->tk_status = -NFS4ERR_DELAY;
6060 break;
6061 case -NFS4ERR_EXPIRED:
6062 case -NFS4ERR_BAD_STATEID:
6063 spin_lock(&inode->i_lock);
6064 lo = NFS_I(inode)->layout;
6065 if (!lo || list_empty(&lo->plh_segs)) {
6066 spin_unlock(&inode->i_lock);
6067 /* If the open stateid was bad, then recover it. */
6068 state = lgp->args.ctx->state;
6069 } else {
6070 LIST_HEAD(head);
6072 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6073 spin_unlock(&inode->i_lock);
6074 /* Mark the bad layout state as invalid, then
6075 * retry using the open stateid. */
6076 pnfs_free_lseg_list(&head);
6079 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6080 rpc_restart_call_prepare(task);
6081 out:
6082 dprintk("<-- %s\n", __func__);
6085 static size_t max_response_pages(struct nfs_server *server)
6087 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6088 return nfs_page_array_len(0, max_resp_sz);
6091 static void nfs4_free_pages(struct page **pages, size_t size)
6093 int i;
6095 if (!pages)
6096 return;
6098 for (i = 0; i < size; i++) {
6099 if (!pages[i])
6100 break;
6101 __free_page(pages[i]);
6103 kfree(pages);
6106 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6108 struct page **pages;
6109 int i;
6111 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6112 if (!pages) {
6113 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6114 return NULL;
6117 for (i = 0; i < size; i++) {
6118 pages[i] = alloc_page(gfp_flags);
6119 if (!pages[i]) {
6120 dprintk("%s: failed to allocate page\n", __func__);
6121 nfs4_free_pages(pages, size);
6122 return NULL;
6126 return pages;
6129 static void nfs4_layoutget_release(void *calldata)
6131 struct nfs4_layoutget *lgp = calldata;
6132 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6133 size_t max_pages = max_response_pages(server);
6135 dprintk("--> %s\n", __func__);
6136 nfs4_free_pages(lgp->args.layout.pages, max_pages);
6137 put_nfs_open_context(lgp->args.ctx);
6138 kfree(calldata);
6139 dprintk("<-- %s\n", __func__);
6142 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6143 .rpc_call_prepare = nfs4_layoutget_prepare,
6144 .rpc_call_done = nfs4_layoutget_done,
6145 .rpc_release = nfs4_layoutget_release,
6148 struct pnfs_layout_segment *
6149 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
6151 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6152 size_t max_pages = max_response_pages(server);
6153 struct rpc_task *task;
6154 struct rpc_message msg = {
6155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6156 .rpc_argp = &lgp->args,
6157 .rpc_resp = &lgp->res,
6159 struct rpc_task_setup task_setup_data = {
6160 .rpc_client = server->client,
6161 .rpc_message = &msg,
6162 .callback_ops = &nfs4_layoutget_call_ops,
6163 .callback_data = lgp,
6164 .flags = RPC_TASK_ASYNC,
6166 struct pnfs_layout_segment *lseg = NULL;
6167 int status = 0;
6169 dprintk("--> %s\n", __func__);
6171 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6172 if (!lgp->args.layout.pages) {
6173 nfs4_layoutget_release(lgp);
6174 return ERR_PTR(-ENOMEM);
6176 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
6178 lgp->res.layoutp = &lgp->args.layout;
6179 lgp->res.seq_res.sr_slot = NULL;
6180 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
6181 task = rpc_run_task(&task_setup_data);
6182 if (IS_ERR(task))
6183 return ERR_CAST(task);
6184 status = nfs4_wait_for_completion_rpc_task(task);
6185 if (status == 0)
6186 status = task->tk_status;
6187 if (status == 0)
6188 lseg = pnfs_layout_process(lgp);
6189 rpc_put_task(task);
6190 dprintk("<-- %s status=%d\n", __func__, status);
6191 if (status)
6192 return ERR_PTR(status);
6193 return lseg;
6196 static void
6197 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6199 struct nfs4_layoutreturn *lrp = calldata;
6201 dprintk("--> %s\n", __func__);
6202 nfs41_setup_sequence(lrp->clp->cl_session,
6203 &lrp->args.seq_args,
6204 &lrp->res.seq_res,
6205 task);
6208 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6210 struct nfs4_layoutreturn *lrp = calldata;
6211 struct nfs_server *server;
6213 dprintk("--> %s\n", __func__);
6215 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
6216 return;
6218 server = NFS_SERVER(lrp->args.inode);
6219 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6220 rpc_restart_call_prepare(task);
6221 return;
6223 dprintk("<-- %s\n", __func__);
6226 static void nfs4_layoutreturn_release(void *calldata)
6228 struct nfs4_layoutreturn *lrp = calldata;
6229 struct pnfs_layout_hdr *lo = lrp->args.layout;
6231 dprintk("--> %s\n", __func__);
6232 spin_lock(&lo->plh_inode->i_lock);
6233 if (lrp->res.lrs_present)
6234 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6235 lo->plh_block_lgets--;
6236 spin_unlock(&lo->plh_inode->i_lock);
6237 pnfs_put_layout_hdr(lrp->args.layout);
6238 kfree(calldata);
6239 dprintk("<-- %s\n", __func__);
6242 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6243 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6244 .rpc_call_done = nfs4_layoutreturn_done,
6245 .rpc_release = nfs4_layoutreturn_release,
6248 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6250 struct rpc_task *task;
6251 struct rpc_message msg = {
6252 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6253 .rpc_argp = &lrp->args,
6254 .rpc_resp = &lrp->res,
6256 struct rpc_task_setup task_setup_data = {
6257 .rpc_client = lrp->clp->cl_rpcclient,
6258 .rpc_message = &msg,
6259 .callback_ops = &nfs4_layoutreturn_call_ops,
6260 .callback_data = lrp,
6262 int status;
6264 dprintk("--> %s\n", __func__);
6265 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
6266 task = rpc_run_task(&task_setup_data);
6267 if (IS_ERR(task))
6268 return PTR_ERR(task);
6269 status = task->tk_status;
6270 dprintk("<-- %s status=%d\n", __func__, status);
6271 rpc_put_task(task);
6272 return status;
6276 * Retrieve the list of Data Server devices from the MDS.
6278 static int _nfs4_getdevicelist(struct nfs_server *server,
6279 const struct nfs_fh *fh,
6280 struct pnfs_devicelist *devlist)
6282 struct nfs4_getdevicelist_args args = {
6283 .fh = fh,
6284 .layoutclass = server->pnfs_curr_ld->id,
6286 struct nfs4_getdevicelist_res res = {
6287 .devlist = devlist,
6289 struct rpc_message msg = {
6290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6291 .rpc_argp = &args,
6292 .rpc_resp = &res,
6294 int status;
6296 dprintk("--> %s\n", __func__);
6297 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6298 &res.seq_res, 0);
6299 dprintk("<-- %s status=%d\n", __func__, status);
6300 return status;
6303 int nfs4_proc_getdevicelist(struct nfs_server *server,
6304 const struct nfs_fh *fh,
6305 struct pnfs_devicelist *devlist)
6307 struct nfs4_exception exception = { };
6308 int err;
6310 do {
6311 err = nfs4_handle_exception(server,
6312 _nfs4_getdevicelist(server, fh, devlist),
6313 &exception);
6314 } while (exception.retry);
6316 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6317 err, devlist->num_devs);
6319 return err;
6321 EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6323 static int
6324 _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6326 struct nfs4_getdeviceinfo_args args = {
6327 .pdev = pdev,
6329 struct nfs4_getdeviceinfo_res res = {
6330 .pdev = pdev,
6332 struct rpc_message msg = {
6333 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6334 .rpc_argp = &args,
6335 .rpc_resp = &res,
6337 int status;
6339 dprintk("--> %s\n", __func__);
6340 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6341 dprintk("<-- %s status=%d\n", __func__, status);
6343 return status;
6346 int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6348 struct nfs4_exception exception = { };
6349 int err;
6351 do {
6352 err = nfs4_handle_exception(server,
6353 _nfs4_proc_getdeviceinfo(server, pdev),
6354 &exception);
6355 } while (exception.retry);
6356 return err;
6358 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6360 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6362 struct nfs4_layoutcommit_data *data = calldata;
6363 struct nfs_server *server = NFS_SERVER(data->args.inode);
6364 struct nfs4_session *session = nfs4_get_session(server);
6366 nfs41_setup_sequence(session,
6367 &data->args.seq_args,
6368 &data->res.seq_res,
6369 task);
6372 static void
6373 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6375 struct nfs4_layoutcommit_data *data = calldata;
6376 struct nfs_server *server = NFS_SERVER(data->args.inode);
6378 if (!nfs41_sequence_done(task, &data->res.seq_res))
6379 return;
6381 switch (task->tk_status) { /* Just ignore these failures */
6382 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6383 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6384 case -NFS4ERR_BADLAYOUT: /* no layout */
6385 case -NFS4ERR_GRACE: /* loca_recalim always false */
6386 task->tk_status = 0;
6387 break;
6388 case 0:
6389 nfs_post_op_update_inode_force_wcc(data->args.inode,
6390 data->res.fattr);
6391 break;
6392 default:
6393 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6394 rpc_restart_call_prepare(task);
6395 return;
6400 static void nfs4_layoutcommit_release(void *calldata)
6402 struct nfs4_layoutcommit_data *data = calldata;
6403 struct pnfs_layout_segment *lseg, *tmp;
6404 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
6406 pnfs_cleanup_layoutcommit(data);
6407 /* Matched by references in pnfs_set_layoutcommit */
6408 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6409 list_del_init(&lseg->pls_lc_list);
6410 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6411 &lseg->pls_flags))
6412 pnfs_put_lseg(lseg);
6415 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6416 smp_mb__after_clear_bit();
6417 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6419 put_rpccred(data->cred);
6420 kfree(data);
6423 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6424 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6425 .rpc_call_done = nfs4_layoutcommit_done,
6426 .rpc_release = nfs4_layoutcommit_release,
6430 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
6432 struct rpc_message msg = {
6433 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6434 .rpc_argp = &data->args,
6435 .rpc_resp = &data->res,
6436 .rpc_cred = data->cred,
6438 struct rpc_task_setup task_setup_data = {
6439 .task = &data->task,
6440 .rpc_client = NFS_CLIENT(data->args.inode),
6441 .rpc_message = &msg,
6442 .callback_ops = &nfs4_layoutcommit_ops,
6443 .callback_data = data,
6444 .flags = RPC_TASK_ASYNC,
6446 struct rpc_task *task;
6447 int status = 0;
6449 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6450 "lbw: %llu inode %lu\n",
6451 data->task.tk_pid, sync,
6452 data->args.lastbytewritten,
6453 data->args.inode->i_ino);
6455 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
6456 task = rpc_run_task(&task_setup_data);
6457 if (IS_ERR(task))
6458 return PTR_ERR(task);
6459 if (sync == false)
6460 goto out;
6461 status = nfs4_wait_for_completion_rpc_task(task);
6462 if (status != 0)
6463 goto out;
6464 status = task->tk_status;
6465 out:
6466 dprintk("%s: status %d\n", __func__, status);
6467 rpc_put_task(task);
6468 return status;
6471 static int
6472 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6473 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6475 struct nfs41_secinfo_no_name_args args = {
6476 .style = SECINFO_STYLE_CURRENT_FH,
6478 struct nfs4_secinfo_res res = {
6479 .flavors = flavors,
6481 struct rpc_message msg = {
6482 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6483 .rpc_argp = &args,
6484 .rpc_resp = &res,
6486 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6489 static int
6490 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6491 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6493 struct nfs4_exception exception = { };
6494 int err;
6495 do {
6496 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6497 switch (err) {
6498 case 0:
6499 case -NFS4ERR_WRONGSEC:
6500 case -NFS4ERR_NOTSUPP:
6501 goto out;
6502 default:
6503 err = nfs4_handle_exception(server, err, &exception);
6505 } while (exception.retry);
6506 out:
6507 return err;
6510 static int
6511 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6512 struct nfs_fsinfo *info)
6514 int err;
6515 struct page *page;
6516 rpc_authflavor_t flavor;
6517 struct nfs4_secinfo_flavors *flavors;
6519 page = alloc_page(GFP_KERNEL);
6520 if (!page) {
6521 err = -ENOMEM;
6522 goto out;
6525 flavors = page_address(page);
6526 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6529 * Fall back on "guess and check" method if
6530 * the server doesn't support SECINFO_NO_NAME
6532 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6533 err = nfs4_find_root_sec(server, fhandle, info);
6534 goto out_freepage;
6536 if (err)
6537 goto out_freepage;
6539 flavor = nfs_find_best_sec(flavors);
6540 if (err == 0)
6541 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6543 out_freepage:
6544 put_page(page);
6545 if (err == -EACCES)
6546 return -EPERM;
6547 out:
6548 return err;
6551 static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6553 int status;
6554 struct nfs41_test_stateid_args args = {
6555 .stateid = stateid,
6557 struct nfs41_test_stateid_res res;
6558 struct rpc_message msg = {
6559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6560 .rpc_argp = &args,
6561 .rpc_resp = &res,
6564 dprintk("NFS call test_stateid %p\n", stateid);
6565 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6566 nfs4_set_sequence_privileged(&args.seq_args);
6567 status = nfs4_call_sync_sequence(server->client, server, &msg,
6568 &args.seq_args, &res.seq_res);
6569 if (status != NFS_OK) {
6570 dprintk("NFS reply test_stateid: failed, %d\n", status);
6571 return status;
6573 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
6574 return -res.status;
6578 * nfs41_test_stateid - perform a TEST_STATEID operation
6580 * @server: server / transport on which to perform the operation
6581 * @stateid: state ID to test
6583 * Returns NFS_OK if the server recognizes that "stateid" is valid.
6584 * Otherwise a negative NFS4ERR value is returned if the operation
6585 * failed or the state ID is not currently valid.
6587 static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6589 struct nfs4_exception exception = { };
6590 int err;
6591 do {
6592 err = _nfs41_test_stateid(server, stateid);
6593 if (err != -NFS4ERR_DELAY)
6594 break;
6595 nfs4_handle_exception(server, err, &exception);
6596 } while (exception.retry);
6597 return err;
6600 static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6602 struct nfs41_free_stateid_args args = {
6603 .stateid = stateid,
6605 struct nfs41_free_stateid_res res;
6606 struct rpc_message msg = {
6607 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6608 .rpc_argp = &args,
6609 .rpc_resp = &res,
6611 int status;
6613 dprintk("NFS call free_stateid %p\n", stateid);
6614 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6615 nfs4_set_sequence_privileged(&args.seq_args);
6616 status = nfs4_call_sync_sequence(server->client, server, &msg,
6617 &args.seq_args, &res.seq_res);
6618 dprintk("NFS reply free_stateid: %d\n", status);
6619 return status;
6623 * nfs41_free_stateid - perform a FREE_STATEID operation
6625 * @server: server / transport on which to perform the operation
6626 * @stateid: state ID to release
6628 * Returns NFS_OK if the server freed "stateid". Otherwise a
6629 * negative NFS4ERR value is returned.
6631 static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
6633 struct nfs4_exception exception = { };
6634 int err;
6635 do {
6636 err = _nfs4_free_stateid(server, stateid);
6637 if (err != -NFS4ERR_DELAY)
6638 break;
6639 nfs4_handle_exception(server, err, &exception);
6640 } while (exception.retry);
6641 return err;
6644 static bool nfs41_match_stateid(const nfs4_stateid *s1,
6645 const nfs4_stateid *s2)
6647 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
6648 return false;
6650 if (s1->seqid == s2->seqid)
6651 return true;
6652 if (s1->seqid == 0 || s2->seqid == 0)
6653 return true;
6655 return false;
6658 #endif /* CONFIG_NFS_V4_1 */
6660 static bool nfs4_match_stateid(const nfs4_stateid *s1,
6661 const nfs4_stateid *s2)
6663 return nfs4_stateid_match(s1, s2);
6667 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
6668 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6669 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6670 .recover_open = nfs4_open_reclaim,
6671 .recover_lock = nfs4_lock_reclaim,
6672 .establish_clid = nfs4_init_clientid,
6673 .get_clid_cred = nfs4_get_setclientid_cred,
6674 .detect_trunking = nfs40_discover_server_trunking,
6677 #if defined(CONFIG_NFS_V4_1)
6678 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6679 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6680 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6681 .recover_open = nfs4_open_reclaim,
6682 .recover_lock = nfs4_lock_reclaim,
6683 .establish_clid = nfs41_init_clientid,
6684 .get_clid_cred = nfs4_get_exchange_id_cred,
6685 .reclaim_complete = nfs41_proc_reclaim_complete,
6686 .detect_trunking = nfs41_discover_server_trunking,
6688 #endif /* CONFIG_NFS_V4_1 */
6690 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
6691 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6692 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6693 .recover_open = nfs4_open_expired,
6694 .recover_lock = nfs4_lock_expired,
6695 .establish_clid = nfs4_init_clientid,
6696 .get_clid_cred = nfs4_get_setclientid_cred,
6699 #if defined(CONFIG_NFS_V4_1)
6700 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6701 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6702 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
6703 .recover_open = nfs41_open_expired,
6704 .recover_lock = nfs41_lock_expired,
6705 .establish_clid = nfs41_init_clientid,
6706 .get_clid_cred = nfs4_get_exchange_id_cred,
6708 #endif /* CONFIG_NFS_V4_1 */
6710 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6711 .sched_state_renewal = nfs4_proc_async_renew,
6712 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
6713 .renew_lease = nfs4_proc_renew,
6716 #if defined(CONFIG_NFS_V4_1)
6717 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6718 .sched_state_renewal = nfs41_proc_async_sequence,
6719 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
6720 .renew_lease = nfs4_proc_sequence,
6722 #endif
6724 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6725 .minor_version = 0,
6726 .call_sync = _nfs4_call_sync,
6727 .match_stateid = nfs4_match_stateid,
6728 .find_root_sec = nfs4_find_root_sec,
6729 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6730 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6731 .state_renewal_ops = &nfs40_state_renewal_ops,
6734 #if defined(CONFIG_NFS_V4_1)
6735 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6736 .minor_version = 1,
6737 .call_sync = nfs4_call_sync_sequence,
6738 .match_stateid = nfs41_match_stateid,
6739 .find_root_sec = nfs41_find_root_sec,
6740 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6741 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6742 .state_renewal_ops = &nfs41_state_renewal_ops,
6744 #endif
6746 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6747 [0] = &nfs_v4_0_minor_ops,
6748 #if defined(CONFIG_NFS_V4_1)
6749 [1] = &nfs_v4_1_minor_ops,
6750 #endif
6753 const struct inode_operations nfs4_dir_inode_operations = {
6754 .create = nfs_create,
6755 .lookup = nfs_lookup,
6756 .atomic_open = nfs_atomic_open,
6757 .link = nfs_link,
6758 .unlink = nfs_unlink,
6759 .symlink = nfs_symlink,
6760 .mkdir = nfs_mkdir,
6761 .rmdir = nfs_rmdir,
6762 .mknod = nfs_mknod,
6763 .rename = nfs_rename,
6764 .permission = nfs_permission,
6765 .getattr = nfs_getattr,
6766 .setattr = nfs_setattr,
6767 .getxattr = generic_getxattr,
6768 .setxattr = generic_setxattr,
6769 .listxattr = generic_listxattr,
6770 .removexattr = generic_removexattr,
6773 static const struct inode_operations nfs4_file_inode_operations = {
6774 .permission = nfs_permission,
6775 .getattr = nfs_getattr,
6776 .setattr = nfs_setattr,
6777 .getxattr = generic_getxattr,
6778 .setxattr = generic_setxattr,
6779 .listxattr = generic_listxattr,
6780 .removexattr = generic_removexattr,
6783 const struct nfs_rpc_ops nfs_v4_clientops = {
6784 .version = 4, /* protocol version */
6785 .dentry_ops = &nfs4_dentry_operations,
6786 .dir_inode_ops = &nfs4_dir_inode_operations,
6787 .file_inode_ops = &nfs4_file_inode_operations,
6788 .file_ops = &nfs4_file_operations,
6789 .getroot = nfs4_proc_get_root,
6790 .submount = nfs4_submount,
6791 .try_mount = nfs4_try_mount,
6792 .getattr = nfs4_proc_getattr,
6793 .setattr = nfs4_proc_setattr,
6794 .lookup = nfs4_proc_lookup,
6795 .access = nfs4_proc_access,
6796 .readlink = nfs4_proc_readlink,
6797 .create = nfs4_proc_create,
6798 .remove = nfs4_proc_remove,
6799 .unlink_setup = nfs4_proc_unlink_setup,
6800 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
6801 .unlink_done = nfs4_proc_unlink_done,
6802 .rename = nfs4_proc_rename,
6803 .rename_setup = nfs4_proc_rename_setup,
6804 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
6805 .rename_done = nfs4_proc_rename_done,
6806 .link = nfs4_proc_link,
6807 .symlink = nfs4_proc_symlink,
6808 .mkdir = nfs4_proc_mkdir,
6809 .rmdir = nfs4_proc_remove,
6810 .readdir = nfs4_proc_readdir,
6811 .mknod = nfs4_proc_mknod,
6812 .statfs = nfs4_proc_statfs,
6813 .fsinfo = nfs4_proc_fsinfo,
6814 .pathconf = nfs4_proc_pathconf,
6815 .set_capabilities = nfs4_server_capabilities,
6816 .decode_dirent = nfs4_decode_dirent,
6817 .read_setup = nfs4_proc_read_setup,
6818 .read_pageio_init = pnfs_pageio_init_read,
6819 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
6820 .read_done = nfs4_read_done,
6821 .write_setup = nfs4_proc_write_setup,
6822 .write_pageio_init = pnfs_pageio_init_write,
6823 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
6824 .write_done = nfs4_write_done,
6825 .commit_setup = nfs4_proc_commit_setup,
6826 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
6827 .commit_done = nfs4_commit_done,
6828 .lock = nfs4_proc_lock,
6829 .clear_acl_cache = nfs4_zap_acl_attr,
6830 .close_context = nfs4_close_context,
6831 .open_context = nfs4_atomic_open,
6832 .have_delegation = nfs4_have_delegation,
6833 .return_delegation = nfs4_inode_return_delegation,
6834 .alloc_client = nfs4_alloc_client,
6835 .init_client = nfs4_init_client,
6836 .free_client = nfs4_free_client,
6837 .create_server = nfs4_create_server,
6838 .clone_server = nfs_clone_server,
6841 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6842 .prefix = XATTR_NAME_NFSV4_ACL,
6843 .list = nfs4_xattr_list_nfs4_acl,
6844 .get = nfs4_xattr_get_nfs4_acl,
6845 .set = nfs4_xattr_set_nfs4_acl,
6848 const struct xattr_handler *nfs4_xattr_handlers[] = {
6849 &nfs4_xattr_nfs4_acl_handler,
6850 NULL
6854 * Local variables:
6855 * c-basic-offset: 8
6856 * End: