vfio-pci: use 32-bit comparisons for register address for gcc-4.5
[linux/fpc-iii.git] / fs / nfs / nfs4proc.c
blob46ca7881d80d4339ca5756e9f0573d5312acf898
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/file.h>
42 #include <linux/string.h>
43 #include <linux/ratelimit.h>
44 #include <linux/printk.h>
45 #include <linux/slab.h>
46 #include <linux/sunrpc/clnt.h>
47 #include <linux/nfs.h>
48 #include <linux/nfs4.h>
49 #include <linux/nfs_fs.h>
50 #include <linux/nfs_page.h>
51 #include <linux/nfs_mount.h>
52 #include <linux/namei.h>
53 #include <linux/mount.h>
54 #include <linux/module.h>
55 #include <linux/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 "nfs4idmap.h"
67 #include "nfs4session.h"
68 #include "fscache.h"
70 #include "nfs4trace.h"
72 #define NFSDBG_FACILITY NFSDBG_PROC
74 #define NFS4_POLL_RETRY_MIN (HZ/10)
75 #define NFS4_POLL_RETRY_MAX (15*HZ)
77 /* file attributes which can be mapped to nfs attributes */
78 #define NFS4_VALID_ATTRS (ATTR_MODE \
79 | ATTR_UID \
80 | ATTR_GID \
81 | ATTR_SIZE \
82 | ATTR_ATIME \
83 | ATTR_MTIME \
84 | ATTR_CTIME \
85 | ATTR_ATIME_SET \
86 | ATTR_MTIME_SET)
88 struct nfs4_opendata;
89 static int _nfs4_proc_open(struct nfs4_opendata *data);
90 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
91 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
92 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
93 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
94 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
95 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
96 struct nfs_fattr *fattr, struct iattr *sattr,
97 struct nfs4_state *state, struct nfs4_label *ilabel,
98 struct nfs4_label *olabel);
99 #ifdef CONFIG_NFS_V4_1
100 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
101 struct rpc_cred *);
102 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
103 struct rpc_cred *, bool);
104 #endif
106 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
107 static inline struct nfs4_label *
108 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
109 struct iattr *sattr, struct nfs4_label *label)
111 int err;
113 if (label == NULL)
114 return NULL;
116 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
117 return NULL;
119 err = security_dentry_init_security(dentry, sattr->ia_mode,
120 &dentry->d_name, (void **)&label->label, &label->len);
121 if (err == 0)
122 return label;
124 return NULL;
126 static inline void
127 nfs4_label_release_security(struct nfs4_label *label)
129 if (label)
130 security_release_secctx(label->label, label->len);
132 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
134 if (label)
135 return server->attr_bitmask;
137 return server->attr_bitmask_nl;
139 #else
140 static inline struct nfs4_label *
141 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
142 struct iattr *sattr, struct nfs4_label *l)
143 { return NULL; }
144 static inline void
145 nfs4_label_release_security(struct nfs4_label *label)
146 { return; }
147 static inline u32 *
148 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
149 { return server->attr_bitmask; }
150 #endif
152 /* Prevent leaks of NFSv4 errors into userland */
153 static int nfs4_map_errors(int err)
155 if (err >= -1000)
156 return err;
157 switch (err) {
158 case -NFS4ERR_RESOURCE:
159 case -NFS4ERR_LAYOUTTRYLATER:
160 case -NFS4ERR_RECALLCONFLICT:
161 return -EREMOTEIO;
162 case -NFS4ERR_WRONGSEC:
163 case -NFS4ERR_WRONG_CRED:
164 return -EPERM;
165 case -NFS4ERR_BADOWNER:
166 case -NFS4ERR_BADNAME:
167 return -EINVAL;
168 case -NFS4ERR_SHARE_DENIED:
169 return -EACCES;
170 case -NFS4ERR_MINOR_VERS_MISMATCH:
171 return -EPROTONOSUPPORT;
172 case -NFS4ERR_FILE_OPEN:
173 return -EBUSY;
174 default:
175 dprintk("%s could not handle NFSv4 error %d\n",
176 __func__, -err);
177 break;
179 return -EIO;
183 * This is our standard bitmap for GETATTR requests.
185 const u32 nfs4_fattr_bitmap[3] = {
186 FATTR4_WORD0_TYPE
187 | FATTR4_WORD0_CHANGE
188 | FATTR4_WORD0_SIZE
189 | FATTR4_WORD0_FSID
190 | FATTR4_WORD0_FILEID,
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,
201 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
202 FATTR4_WORD2_SECURITY_LABEL
203 #endif
206 static const u32 nfs4_pnfs_open_bitmap[3] = {
207 FATTR4_WORD0_TYPE
208 | FATTR4_WORD0_CHANGE
209 | FATTR4_WORD0_SIZE
210 | FATTR4_WORD0_FSID
211 | FATTR4_WORD0_FILEID,
212 FATTR4_WORD1_MODE
213 | FATTR4_WORD1_NUMLINKS
214 | FATTR4_WORD1_OWNER
215 | FATTR4_WORD1_OWNER_GROUP
216 | FATTR4_WORD1_RAWDEV
217 | FATTR4_WORD1_SPACE_USED
218 | FATTR4_WORD1_TIME_ACCESS
219 | FATTR4_WORD1_TIME_METADATA
220 | FATTR4_WORD1_TIME_MODIFY,
221 FATTR4_WORD2_MDSTHRESHOLD
222 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
223 | FATTR4_WORD2_SECURITY_LABEL
224 #endif
227 static const u32 nfs4_open_noattr_bitmap[3] = {
228 FATTR4_WORD0_TYPE
229 | FATTR4_WORD0_CHANGE
230 | FATTR4_WORD0_FILEID,
233 const u32 nfs4_statfs_bitmap[3] = {
234 FATTR4_WORD0_FILES_AVAIL
235 | FATTR4_WORD0_FILES_FREE
236 | FATTR4_WORD0_FILES_TOTAL,
237 FATTR4_WORD1_SPACE_AVAIL
238 | FATTR4_WORD1_SPACE_FREE
239 | FATTR4_WORD1_SPACE_TOTAL
242 const u32 nfs4_pathconf_bitmap[3] = {
243 FATTR4_WORD0_MAXLINK
244 | FATTR4_WORD0_MAXNAME,
248 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
249 | FATTR4_WORD0_MAXREAD
250 | FATTR4_WORD0_MAXWRITE
251 | FATTR4_WORD0_LEASE_TIME,
252 FATTR4_WORD1_TIME_DELTA
253 | FATTR4_WORD1_FS_LAYOUT_TYPES,
254 FATTR4_WORD2_LAYOUT_BLKSIZE
255 | FATTR4_WORD2_CLONE_BLKSIZE
258 const u32 nfs4_fs_locations_bitmap[3] = {
259 FATTR4_WORD0_TYPE
260 | FATTR4_WORD0_CHANGE
261 | FATTR4_WORD0_SIZE
262 | FATTR4_WORD0_FSID
263 | FATTR4_WORD0_FILEID
264 | FATTR4_WORD0_FS_LOCATIONS,
265 FATTR4_WORD1_MODE
266 | FATTR4_WORD1_NUMLINKS
267 | FATTR4_WORD1_OWNER
268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
274 | FATTR4_WORD1_MOUNTED_ON_FILEID,
277 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
278 struct nfs4_readdir_arg *readdir)
280 __be32 *start, *p;
282 if (cookie > 2) {
283 readdir->cookie = cookie;
284 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
285 return;
288 readdir->cookie = 0;
289 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
290 if (cookie == 2)
291 return;
294 * NFSv4 servers do not return entries for '.' and '..'
295 * Therefore, we fake these entries here. We let '.'
296 * have cookie 0 and '..' have cookie 1. Note that
297 * when talking to the server, we always send cookie 0
298 * instead of 1 or 2.
300 start = p = kmap_atomic(*readdir->pages);
302 if (cookie == 0) {
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_one; /* cookie, second word */
306 *p++ = xdr_one; /* entry len */
307 memcpy(p, ".\0\0\0", 4); /* entry */
308 p++;
309 *p++ = xdr_one; /* bitmap length */
310 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
311 *p++ = htonl(8); /* attribute buffer length */
312 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
315 *p++ = xdr_one; /* next */
316 *p++ = xdr_zero; /* cookie, first word */
317 *p++ = xdr_two; /* cookie, second word */
318 *p++ = xdr_two; /* entry len */
319 memcpy(p, "..\0\0", 4); /* entry */
320 p++;
321 *p++ = xdr_one; /* bitmap length */
322 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
323 *p++ = htonl(8); /* attribute buffer length */
324 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
326 readdir->pgbase = (char *)p - (char *)start;
327 readdir->count -= readdir->pgbase;
328 kunmap_atomic(start);
331 static void nfs4_test_and_free_stateid(struct nfs_server *server,
332 nfs4_stateid *stateid,
333 struct rpc_cred *cred)
335 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
337 ops->test_and_free_expired(server, stateid, cred);
340 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
341 nfs4_stateid *stateid,
342 struct rpc_cred *cred)
344 stateid->type = NFS4_REVOKED_STATEID_TYPE;
345 nfs4_test_and_free_stateid(server, stateid, cred);
348 static void nfs4_free_revoked_stateid(struct nfs_server *server,
349 const nfs4_stateid *stateid,
350 struct rpc_cred *cred)
352 nfs4_stateid tmp;
354 nfs4_stateid_copy(&tmp, stateid);
355 __nfs4_free_revoked_stateid(server, &tmp, cred);
358 static long nfs4_update_delay(long *timeout)
360 long ret;
361 if (!timeout)
362 return NFS4_POLL_RETRY_MAX;
363 if (*timeout <= 0)
364 *timeout = NFS4_POLL_RETRY_MIN;
365 if (*timeout > NFS4_POLL_RETRY_MAX)
366 *timeout = NFS4_POLL_RETRY_MAX;
367 ret = *timeout;
368 *timeout <<= 1;
369 return ret;
372 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
374 int res = 0;
376 might_sleep();
378 freezable_schedule_timeout_killable_unsafe(
379 nfs4_update_delay(timeout));
380 if (fatal_signal_pending(current))
381 res = -ERESTARTSYS;
382 return res;
385 /* This is the error handling routine for processes that are allowed
386 * to sleep.
388 static int nfs4_do_handle_exception(struct nfs_server *server,
389 int errorcode, struct nfs4_exception *exception)
391 struct nfs_client *clp = server->nfs_client;
392 struct nfs4_state *state = exception->state;
393 const nfs4_stateid *stateid = exception->stateid;
394 struct inode *inode = exception->inode;
395 int ret = errorcode;
397 exception->delay = 0;
398 exception->recovering = 0;
399 exception->retry = 0;
401 if (stateid == NULL && state != NULL)
402 stateid = &state->stateid;
404 switch(errorcode) {
405 case 0:
406 return 0;
407 case -NFS4ERR_DELEG_REVOKED:
408 case -NFS4ERR_ADMIN_REVOKED:
409 case -NFS4ERR_EXPIRED:
410 case -NFS4ERR_BAD_STATEID:
411 if (inode != NULL && stateid != NULL) {
412 nfs_inode_find_state_and_recover(inode,
413 stateid);
414 goto wait_on_recovery;
416 case -NFS4ERR_OPENMODE:
417 if (inode) {
418 int err;
420 err = nfs_async_inode_return_delegation(inode,
421 stateid);
422 if (err == 0)
423 goto wait_on_recovery;
424 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
425 exception->retry = 1;
426 break;
429 if (state == NULL)
430 break;
431 ret = nfs4_schedule_stateid_recovery(server, state);
432 if (ret < 0)
433 break;
434 goto wait_on_recovery;
435 case -NFS4ERR_STALE_STATEID:
436 case -NFS4ERR_STALE_CLIENTID:
437 nfs4_schedule_lease_recovery(clp);
438 goto wait_on_recovery;
439 case -NFS4ERR_MOVED:
440 ret = nfs4_schedule_migration_recovery(server);
441 if (ret < 0)
442 break;
443 goto wait_on_recovery;
444 case -NFS4ERR_LEASE_MOVED:
445 nfs4_schedule_lease_moved_recovery(clp);
446 goto wait_on_recovery;
447 #if defined(CONFIG_NFS_V4_1)
448 case -NFS4ERR_BADSESSION:
449 case -NFS4ERR_BADSLOT:
450 case -NFS4ERR_BAD_HIGH_SLOT:
451 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
452 case -NFS4ERR_DEADSESSION:
453 case -NFS4ERR_SEQ_FALSE_RETRY:
454 case -NFS4ERR_SEQ_MISORDERED:
455 dprintk("%s ERROR: %d Reset session\n", __func__,
456 errorcode);
457 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
458 goto wait_on_recovery;
459 #endif /* defined(CONFIG_NFS_V4_1) */
460 case -NFS4ERR_FILE_OPEN:
461 if (exception->timeout > HZ) {
462 /* We have retried a decent amount, time to
463 * fail
465 ret = -EBUSY;
466 break;
468 case -NFS4ERR_DELAY:
469 nfs_inc_server_stats(server, NFSIOS_DELAY);
470 case -NFS4ERR_GRACE:
471 case -NFS4ERR_LAYOUTTRYLATER:
472 case -NFS4ERR_RECALLCONFLICT:
473 exception->delay = 1;
474 return 0;
476 case -NFS4ERR_RETRY_UNCACHED_REP:
477 case -NFS4ERR_OLD_STATEID:
478 exception->retry = 1;
479 break;
480 case -NFS4ERR_BADOWNER:
481 /* The following works around a Linux server bug! */
482 case -NFS4ERR_BADNAME:
483 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
484 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
485 exception->retry = 1;
486 printk(KERN_WARNING "NFS: v4 server %s "
487 "does not accept raw "
488 "uid/gids. "
489 "Reenabling the idmapper.\n",
490 server->nfs_client->cl_hostname);
493 /* We failed to handle the error */
494 return nfs4_map_errors(ret);
495 wait_on_recovery:
496 exception->recovering = 1;
497 return 0;
500 /* This is the error handling routine for processes that are allowed
501 * to sleep.
503 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
505 struct nfs_client *clp = server->nfs_client;
506 int ret;
508 ret = nfs4_do_handle_exception(server, errorcode, exception);
509 if (exception->delay) {
510 ret = nfs4_delay(server->client, &exception->timeout);
511 goto out_retry;
513 if (exception->recovering) {
514 ret = nfs4_wait_clnt_recover(clp);
515 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
516 return -EIO;
517 goto out_retry;
519 return ret;
520 out_retry:
521 if (ret == 0)
522 exception->retry = 1;
523 return ret;
526 static int
527 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
528 int errorcode, struct nfs4_exception *exception)
530 struct nfs_client *clp = server->nfs_client;
531 int ret;
533 ret = nfs4_do_handle_exception(server, errorcode, exception);
534 if (exception->delay) {
535 rpc_delay(task, nfs4_update_delay(&exception->timeout));
536 goto out_retry;
538 if (exception->recovering) {
539 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
540 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
541 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
542 goto out_retry;
544 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
545 ret = -EIO;
546 return ret;
547 out_retry:
548 if (ret == 0)
549 exception->retry = 1;
550 return ret;
553 static int
554 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
555 struct nfs4_state *state, long *timeout)
557 struct nfs4_exception exception = {
558 .state = state,
561 if (task->tk_status >= 0)
562 return 0;
563 if (timeout)
564 exception.timeout = *timeout;
565 task->tk_status = nfs4_async_handle_exception(task, server,
566 task->tk_status,
567 &exception);
568 if (exception.delay && timeout)
569 *timeout = exception.timeout;
570 if (exception.retry)
571 return -EAGAIN;
572 return 0;
576 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
577 * or 'false' otherwise.
579 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
581 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
583 if (flavor == RPC_AUTH_GSS_KRB5I ||
584 flavor == RPC_AUTH_GSS_KRB5P)
585 return true;
587 return false;
590 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
592 spin_lock(&clp->cl_lock);
593 if (time_before(clp->cl_last_renewal,timestamp))
594 clp->cl_last_renewal = timestamp;
595 spin_unlock(&clp->cl_lock);
598 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
600 struct nfs_client *clp = server->nfs_client;
602 if (!nfs4_has_session(clp))
603 do_renew_lease(clp, timestamp);
606 struct nfs4_call_sync_data {
607 const struct nfs_server *seq_server;
608 struct nfs4_sequence_args *seq_args;
609 struct nfs4_sequence_res *seq_res;
612 void nfs4_init_sequence(struct nfs4_sequence_args *args,
613 struct nfs4_sequence_res *res, int cache_reply)
615 args->sa_slot = NULL;
616 args->sa_cache_this = cache_reply;
617 args->sa_privileged = 0;
619 res->sr_slot = NULL;
622 static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
624 args->sa_privileged = 1;
627 int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
628 struct nfs4_sequence_args *args,
629 struct nfs4_sequence_res *res,
630 struct rpc_task *task)
632 struct nfs4_slot *slot;
634 /* slot already allocated? */
635 if (res->sr_slot != NULL)
636 goto out_start;
638 spin_lock(&tbl->slot_tbl_lock);
639 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
640 goto out_sleep;
642 slot = nfs4_alloc_slot(tbl);
643 if (IS_ERR(slot)) {
644 if (slot == ERR_PTR(-ENOMEM))
645 task->tk_timeout = HZ >> 2;
646 goto out_sleep;
648 spin_unlock(&tbl->slot_tbl_lock);
650 slot->privileged = args->sa_privileged ? 1 : 0;
651 args->sa_slot = slot;
652 res->sr_slot = slot;
654 out_start:
655 rpc_call_start(task);
656 return 0;
658 out_sleep:
659 if (args->sa_privileged)
660 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
661 NULL, RPC_PRIORITY_PRIVILEGED);
662 else
663 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
664 spin_unlock(&tbl->slot_tbl_lock);
665 return -EAGAIN;
667 EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
669 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
671 struct nfs4_slot *slot = res->sr_slot;
672 struct nfs4_slot_table *tbl;
674 tbl = slot->table;
675 spin_lock(&tbl->slot_tbl_lock);
676 if (!nfs41_wake_and_assign_slot(tbl, slot))
677 nfs4_free_slot(tbl, slot);
678 spin_unlock(&tbl->slot_tbl_lock);
680 res->sr_slot = NULL;
683 static int nfs40_sequence_done(struct rpc_task *task,
684 struct nfs4_sequence_res *res)
686 if (res->sr_slot != NULL)
687 nfs40_sequence_free_slot(res);
688 return 1;
691 #if defined(CONFIG_NFS_V4_1)
693 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
695 struct nfs4_session *session;
696 struct nfs4_slot_table *tbl;
697 struct nfs4_slot *slot = res->sr_slot;
698 bool send_new_highest_used_slotid = false;
700 tbl = slot->table;
701 session = tbl->session;
703 /* Bump the slot sequence number */
704 if (slot->seq_done)
705 slot->seq_nr++;
706 slot->seq_done = 0;
708 spin_lock(&tbl->slot_tbl_lock);
709 /* Be nice to the server: try to ensure that the last transmitted
710 * value for highest_user_slotid <= target_highest_slotid
712 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
713 send_new_highest_used_slotid = true;
715 if (nfs41_wake_and_assign_slot(tbl, slot)) {
716 send_new_highest_used_slotid = false;
717 goto out_unlock;
719 nfs4_free_slot(tbl, slot);
721 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
722 send_new_highest_used_slotid = false;
723 out_unlock:
724 spin_unlock(&tbl->slot_tbl_lock);
725 res->sr_slot = NULL;
726 if (send_new_highest_used_slotid)
727 nfs41_notify_server(session->clp);
728 if (waitqueue_active(&tbl->slot_waitq))
729 wake_up_all(&tbl->slot_waitq);
732 static int nfs41_sequence_process(struct rpc_task *task,
733 struct nfs4_sequence_res *res)
735 struct nfs4_session *session;
736 struct nfs4_slot *slot = res->sr_slot;
737 struct nfs_client *clp;
738 bool interrupted = false;
739 int ret = 1;
741 if (slot == NULL)
742 goto out_noaction;
743 /* don't increment the sequence number if the task wasn't sent */
744 if (!RPC_WAS_SENT(task))
745 goto out;
747 session = slot->table->session;
749 if (slot->interrupted) {
750 slot->interrupted = 0;
751 interrupted = true;
754 trace_nfs4_sequence_done(session, res);
755 /* Check the SEQUENCE operation status */
756 switch (res->sr_status) {
757 case 0:
758 /* If previous op on slot was interrupted and we reused
759 * the seq# and got a reply from the cache, then retry
761 if (task->tk_status == -EREMOTEIO && interrupted) {
762 ++slot->seq_nr;
763 goto retry_nowait;
765 /* Update the slot's sequence and clientid lease timer */
766 slot->seq_done = 1;
767 clp = session->clp;
768 do_renew_lease(clp, res->sr_timestamp);
769 /* Check sequence flags */
770 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
771 !!slot->privileged);
772 nfs41_update_target_slotid(slot->table, slot, res);
773 break;
774 case 1:
776 * sr_status remains 1 if an RPC level error occurred.
777 * The server may or may not have processed the sequence
778 * operation..
779 * Mark the slot as having hosted an interrupted RPC call.
781 slot->interrupted = 1;
782 goto out;
783 case -NFS4ERR_DELAY:
784 /* The server detected a resend of the RPC call and
785 * returned NFS4ERR_DELAY as per Section 2.10.6.2
786 * of RFC5661.
788 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
789 __func__,
790 slot->slot_nr,
791 slot->seq_nr);
792 goto out_retry;
793 case -NFS4ERR_BADSLOT:
795 * The slot id we used was probably retired. Try again
796 * using a different slot id.
798 goto retry_nowait;
799 case -NFS4ERR_SEQ_MISORDERED:
801 * Was the last operation on this sequence interrupted?
802 * If so, retry after bumping the sequence number.
804 if (interrupted) {
805 ++slot->seq_nr;
806 goto retry_nowait;
809 * Could this slot have been previously retired?
810 * If so, then the server may be expecting seq_nr = 1!
812 if (slot->seq_nr != 1) {
813 slot->seq_nr = 1;
814 goto retry_nowait;
816 break;
817 case -NFS4ERR_SEQ_FALSE_RETRY:
818 ++slot->seq_nr;
819 goto retry_nowait;
820 default:
821 /* Just update the slot sequence no. */
822 slot->seq_done = 1;
824 out:
825 /* The session may be reset by one of the error handlers. */
826 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
827 out_noaction:
828 return ret;
829 retry_nowait:
830 if (rpc_restart_call_prepare(task)) {
831 nfs41_sequence_free_slot(res);
832 task->tk_status = 0;
833 ret = 0;
835 goto out;
836 out_retry:
837 if (!rpc_restart_call(task))
838 goto out;
839 rpc_delay(task, NFS4_POLL_RETRY_MAX);
840 return 0;
843 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
845 if (!nfs41_sequence_process(task, res))
846 return 0;
847 if (res->sr_slot != NULL)
848 nfs41_sequence_free_slot(res);
849 return 1;
852 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
854 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
856 if (res->sr_slot == NULL)
857 return 1;
858 if (res->sr_slot->table->session != NULL)
859 return nfs41_sequence_process(task, res);
860 return nfs40_sequence_done(task, res);
863 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
865 if (res->sr_slot != NULL) {
866 if (res->sr_slot->table->session != NULL)
867 nfs41_sequence_free_slot(res);
868 else
869 nfs40_sequence_free_slot(res);
873 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
875 if (res->sr_slot == NULL)
876 return 1;
877 if (!res->sr_slot->table->session)
878 return nfs40_sequence_done(task, res);
879 return nfs41_sequence_done(task, res);
881 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
883 int nfs41_setup_sequence(struct nfs4_session *session,
884 struct nfs4_sequence_args *args,
885 struct nfs4_sequence_res *res,
886 struct rpc_task *task)
888 struct nfs4_slot *slot;
889 struct nfs4_slot_table *tbl;
891 dprintk("--> %s\n", __func__);
892 /* slot already allocated? */
893 if (res->sr_slot != NULL)
894 goto out_success;
896 tbl = &session->fc_slot_table;
898 task->tk_timeout = 0;
900 spin_lock(&tbl->slot_tbl_lock);
901 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
902 !args->sa_privileged) {
903 /* The state manager will wait until the slot table is empty */
904 dprintk("%s session is draining\n", __func__);
905 goto out_sleep;
908 slot = nfs4_alloc_slot(tbl);
909 if (IS_ERR(slot)) {
910 /* If out of memory, try again in 1/4 second */
911 if (slot == ERR_PTR(-ENOMEM))
912 task->tk_timeout = HZ >> 2;
913 dprintk("<-- %s: no free slots\n", __func__);
914 goto out_sleep;
916 spin_unlock(&tbl->slot_tbl_lock);
918 slot->privileged = args->sa_privileged ? 1 : 0;
919 args->sa_slot = slot;
921 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
922 slot->slot_nr, slot->seq_nr);
924 res->sr_slot = slot;
925 res->sr_timestamp = jiffies;
926 res->sr_status_flags = 0;
928 * sr_status is only set in decode_sequence, and so will remain
929 * set to 1 if an rpc level failure occurs.
931 res->sr_status = 1;
932 trace_nfs4_setup_sequence(session, args);
933 out_success:
934 rpc_call_start(task);
935 return 0;
936 out_sleep:
937 /* Privileged tasks are queued with top priority */
938 if (args->sa_privileged)
939 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
940 NULL, RPC_PRIORITY_PRIVILEGED);
941 else
942 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
943 spin_unlock(&tbl->slot_tbl_lock);
944 return -EAGAIN;
946 EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
948 static int nfs4_setup_sequence(const struct nfs_server *server,
949 struct nfs4_sequence_args *args,
950 struct nfs4_sequence_res *res,
951 struct rpc_task *task)
953 struct nfs4_session *session = nfs4_get_session(server);
954 int ret = 0;
956 if (!session)
957 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
958 args, res, task);
960 dprintk("--> %s clp %p session %p sr_slot %u\n",
961 __func__, session->clp, session, res->sr_slot ?
962 res->sr_slot->slot_nr : NFS4_NO_SLOT);
964 ret = nfs41_setup_sequence(session, args, res, task);
966 dprintk("<-- %s status=%d\n", __func__, ret);
967 return ret;
970 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
972 struct nfs4_call_sync_data *data = calldata;
973 struct nfs4_session *session = nfs4_get_session(data->seq_server);
975 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
977 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
980 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
982 struct nfs4_call_sync_data *data = calldata;
984 nfs41_sequence_done(task, data->seq_res);
987 static const struct rpc_call_ops nfs41_call_sync_ops = {
988 .rpc_call_prepare = nfs41_call_sync_prepare,
989 .rpc_call_done = nfs41_call_sync_done,
992 #else /* !CONFIG_NFS_V4_1 */
994 static int nfs4_setup_sequence(const struct nfs_server *server,
995 struct nfs4_sequence_args *args,
996 struct nfs4_sequence_res *res,
997 struct rpc_task *task)
999 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
1000 args, res, task);
1003 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1005 return nfs40_sequence_done(task, res);
1008 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1010 if (res->sr_slot != NULL)
1011 nfs40_sequence_free_slot(res);
1014 int nfs4_sequence_done(struct rpc_task *task,
1015 struct nfs4_sequence_res *res)
1017 return nfs40_sequence_done(task, res);
1019 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1021 #endif /* !CONFIG_NFS_V4_1 */
1023 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1025 struct nfs4_call_sync_data *data = calldata;
1026 nfs4_setup_sequence(data->seq_server,
1027 data->seq_args, data->seq_res, task);
1030 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1032 struct nfs4_call_sync_data *data = calldata;
1033 nfs4_sequence_done(task, data->seq_res);
1036 static const struct rpc_call_ops nfs40_call_sync_ops = {
1037 .rpc_call_prepare = nfs40_call_sync_prepare,
1038 .rpc_call_done = nfs40_call_sync_done,
1041 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1042 struct nfs_server *server,
1043 struct rpc_message *msg,
1044 struct nfs4_sequence_args *args,
1045 struct nfs4_sequence_res *res)
1047 int ret;
1048 struct rpc_task *task;
1049 struct nfs_client *clp = server->nfs_client;
1050 struct nfs4_call_sync_data data = {
1051 .seq_server = server,
1052 .seq_args = args,
1053 .seq_res = res,
1055 struct rpc_task_setup task_setup = {
1056 .rpc_client = clnt,
1057 .rpc_message = msg,
1058 .callback_ops = clp->cl_mvops->call_sync_ops,
1059 .callback_data = &data
1062 task = rpc_run_task(&task_setup);
1063 if (IS_ERR(task))
1064 ret = PTR_ERR(task);
1065 else {
1066 ret = task->tk_status;
1067 rpc_put_task(task);
1069 return ret;
1072 int nfs4_call_sync(struct rpc_clnt *clnt,
1073 struct nfs_server *server,
1074 struct rpc_message *msg,
1075 struct nfs4_sequence_args *args,
1076 struct nfs4_sequence_res *res,
1077 int cache_reply)
1079 nfs4_init_sequence(args, res, cache_reply);
1080 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1083 static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1085 struct nfs_inode *nfsi = NFS_I(dir);
1087 spin_lock(&dir->i_lock);
1088 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
1089 if (!cinfo->atomic || cinfo->before != dir->i_version)
1090 nfs_force_lookup_revalidate(dir);
1091 dir->i_version = cinfo->after;
1092 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1093 nfs_fscache_invalidate(dir);
1094 spin_unlock(&dir->i_lock);
1097 struct nfs4_opendata {
1098 struct kref kref;
1099 struct nfs_openargs o_arg;
1100 struct nfs_openres o_res;
1101 struct nfs_open_confirmargs c_arg;
1102 struct nfs_open_confirmres c_res;
1103 struct nfs4_string owner_name;
1104 struct nfs4_string group_name;
1105 struct nfs4_label *a_label;
1106 struct nfs_fattr f_attr;
1107 struct nfs4_label *f_label;
1108 struct dentry *dir;
1109 struct dentry *dentry;
1110 struct nfs4_state_owner *owner;
1111 struct nfs4_state *state;
1112 struct iattr attrs;
1113 unsigned long timestamp;
1114 unsigned int rpc_done : 1;
1115 unsigned int file_created : 1;
1116 unsigned int is_recover : 1;
1117 int rpc_status;
1118 int cancelled;
1121 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1122 int err, struct nfs4_exception *exception)
1124 if (err != -EINVAL)
1125 return false;
1126 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1127 return false;
1128 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1129 exception->retry = 1;
1130 return true;
1133 static u32
1134 nfs4_map_atomic_open_share(struct nfs_server *server,
1135 fmode_t fmode, int openflags)
1137 u32 res = 0;
1139 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1140 case FMODE_READ:
1141 res = NFS4_SHARE_ACCESS_READ;
1142 break;
1143 case FMODE_WRITE:
1144 res = NFS4_SHARE_ACCESS_WRITE;
1145 break;
1146 case FMODE_READ|FMODE_WRITE:
1147 res = NFS4_SHARE_ACCESS_BOTH;
1149 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1150 goto out;
1151 /* Want no delegation if we're using O_DIRECT */
1152 if (openflags & O_DIRECT)
1153 res |= NFS4_SHARE_WANT_NO_DELEG;
1154 out:
1155 return res;
1158 static enum open_claim_type4
1159 nfs4_map_atomic_open_claim(struct nfs_server *server,
1160 enum open_claim_type4 claim)
1162 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1163 return claim;
1164 switch (claim) {
1165 default:
1166 return claim;
1167 case NFS4_OPEN_CLAIM_FH:
1168 return NFS4_OPEN_CLAIM_NULL;
1169 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1170 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1171 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1172 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1176 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1178 p->o_res.f_attr = &p->f_attr;
1179 p->o_res.f_label = p->f_label;
1180 p->o_res.seqid = p->o_arg.seqid;
1181 p->c_res.seqid = p->c_arg.seqid;
1182 p->o_res.server = p->o_arg.server;
1183 p->o_res.access_request = p->o_arg.access;
1184 nfs_fattr_init(&p->f_attr);
1185 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1188 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1189 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1190 const struct iattr *attrs,
1191 struct nfs4_label *label,
1192 enum open_claim_type4 claim,
1193 gfp_t gfp_mask)
1195 struct dentry *parent = dget_parent(dentry);
1196 struct inode *dir = d_inode(parent);
1197 struct nfs_server *server = NFS_SERVER(dir);
1198 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1199 struct nfs4_opendata *p;
1201 p = kzalloc(sizeof(*p), gfp_mask);
1202 if (p == NULL)
1203 goto err;
1205 p->f_label = nfs4_label_alloc(server, gfp_mask);
1206 if (IS_ERR(p->f_label))
1207 goto err_free_p;
1209 p->a_label = nfs4_label_alloc(server, gfp_mask);
1210 if (IS_ERR(p->a_label))
1211 goto err_free_f;
1213 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1214 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1215 if (IS_ERR(p->o_arg.seqid))
1216 goto err_free_label;
1217 nfs_sb_active(dentry->d_sb);
1218 p->dentry = dget(dentry);
1219 p->dir = parent;
1220 p->owner = sp;
1221 atomic_inc(&sp->so_count);
1222 p->o_arg.open_flags = flags;
1223 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1224 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1225 fmode, flags);
1226 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1227 * will return permission denied for all bits until close */
1228 if (!(flags & O_EXCL)) {
1229 /* ask server to check for all possible rights as results
1230 * are cached */
1231 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1232 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
1234 p->o_arg.clientid = server->nfs_client->cl_clientid;
1235 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1236 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1237 p->o_arg.name = &dentry->d_name;
1238 p->o_arg.server = server;
1239 p->o_arg.bitmask = nfs4_bitmask(server, label);
1240 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1241 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1242 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1243 switch (p->o_arg.claim) {
1244 case NFS4_OPEN_CLAIM_NULL:
1245 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1246 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1247 p->o_arg.fh = NFS_FH(dir);
1248 break;
1249 case NFS4_OPEN_CLAIM_PREVIOUS:
1250 case NFS4_OPEN_CLAIM_FH:
1251 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1252 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1253 p->o_arg.fh = NFS_FH(d_inode(dentry));
1255 if (attrs != NULL && attrs->ia_valid != 0) {
1256 __u32 verf[2];
1258 p->o_arg.u.attrs = &p->attrs;
1259 memcpy(&p->attrs, attrs, sizeof(p->attrs));
1261 verf[0] = jiffies;
1262 verf[1] = current->pid;
1263 memcpy(p->o_arg.u.verifier.data, verf,
1264 sizeof(p->o_arg.u.verifier.data));
1266 p->c_arg.fh = &p->o_res.fh;
1267 p->c_arg.stateid = &p->o_res.stateid;
1268 p->c_arg.seqid = p->o_arg.seqid;
1269 nfs4_init_opendata_res(p);
1270 kref_init(&p->kref);
1271 return p;
1273 err_free_label:
1274 nfs4_label_free(p->a_label);
1275 err_free_f:
1276 nfs4_label_free(p->f_label);
1277 err_free_p:
1278 kfree(p);
1279 err:
1280 dput(parent);
1281 return NULL;
1284 static void nfs4_opendata_free(struct kref *kref)
1286 struct nfs4_opendata *p = container_of(kref,
1287 struct nfs4_opendata, kref);
1288 struct super_block *sb = p->dentry->d_sb;
1290 nfs_free_seqid(p->o_arg.seqid);
1291 nfs4_sequence_free_slot(&p->o_res.seq_res);
1292 if (p->state != NULL)
1293 nfs4_put_open_state(p->state);
1294 nfs4_put_state_owner(p->owner);
1296 nfs4_label_free(p->a_label);
1297 nfs4_label_free(p->f_label);
1299 dput(p->dir);
1300 dput(p->dentry);
1301 nfs_sb_deactive(sb);
1302 nfs_fattr_free_names(&p->f_attr);
1303 kfree(p->f_attr.mdsthreshold);
1304 kfree(p);
1307 static void nfs4_opendata_put(struct nfs4_opendata *p)
1309 if (p != NULL)
1310 kref_put(&p->kref, nfs4_opendata_free);
1313 static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1315 int ret;
1317 ret = rpc_wait_for_completion_task(task);
1318 return ret;
1321 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1322 fmode_t fmode)
1324 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1325 case FMODE_READ|FMODE_WRITE:
1326 return state->n_rdwr != 0;
1327 case FMODE_WRITE:
1328 return state->n_wronly != 0;
1329 case FMODE_READ:
1330 return state->n_rdonly != 0;
1332 WARN_ON_ONCE(1);
1333 return false;
1336 static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
1338 int ret = 0;
1340 if (open_mode & (O_EXCL|O_TRUNC))
1341 goto out;
1342 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1343 case FMODE_READ:
1344 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1345 && state->n_rdonly != 0;
1346 break;
1347 case FMODE_WRITE:
1348 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1349 && state->n_wronly != 0;
1350 break;
1351 case FMODE_READ|FMODE_WRITE:
1352 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1353 && state->n_rdwr != 0;
1355 out:
1356 return ret;
1359 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1360 enum open_claim_type4 claim)
1362 if (delegation == NULL)
1363 return 0;
1364 if ((delegation->type & fmode) != fmode)
1365 return 0;
1366 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1367 return 0;
1368 switch (claim) {
1369 case NFS4_OPEN_CLAIM_NULL:
1370 case NFS4_OPEN_CLAIM_FH:
1371 break;
1372 case NFS4_OPEN_CLAIM_PREVIOUS:
1373 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1374 break;
1375 default:
1376 return 0;
1378 nfs_mark_delegation_referenced(delegation);
1379 return 1;
1382 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1384 switch (fmode) {
1385 case FMODE_WRITE:
1386 state->n_wronly++;
1387 break;
1388 case FMODE_READ:
1389 state->n_rdonly++;
1390 break;
1391 case FMODE_READ|FMODE_WRITE:
1392 state->n_rdwr++;
1394 nfs4_state_set_mode_locked(state, state->state | fmode);
1397 #ifdef CONFIG_NFS_V4_1
1398 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1400 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1401 return true;
1402 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1403 return true;
1404 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1405 return true;
1406 return false;
1408 #endif /* CONFIG_NFS_V4_1 */
1410 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1412 struct nfs_client *clp = state->owner->so_server->nfs_client;
1413 bool need_recover = false;
1415 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1416 need_recover = true;
1417 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1418 need_recover = true;
1419 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1420 need_recover = true;
1421 if (need_recover)
1422 nfs4_state_mark_reclaim_nograce(clp, state);
1425 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1426 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1428 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1429 return true;
1430 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1431 nfs4_stateid_copy(freeme, &state->open_stateid);
1432 nfs_test_and_clear_all_open_stateid(state);
1433 return true;
1435 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1436 return true;
1437 return false;
1440 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1442 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1443 return;
1444 if (state->n_wronly)
1445 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1446 if (state->n_rdonly)
1447 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1448 if (state->n_rdwr)
1449 set_bit(NFS_O_RDWR_STATE, &state->flags);
1450 set_bit(NFS_OPEN_STATE, &state->flags);
1453 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1454 nfs4_stateid *stateid, fmode_t fmode)
1456 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1457 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1458 case FMODE_WRITE:
1459 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1460 break;
1461 case FMODE_READ:
1462 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1463 break;
1464 case 0:
1465 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1466 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1467 clear_bit(NFS_OPEN_STATE, &state->flags);
1469 if (stateid == NULL)
1470 return;
1471 /* Handle OPEN+OPEN_DOWNGRADE races */
1472 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1473 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1474 nfs_resync_open_stateid_locked(state);
1475 return;
1477 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1478 nfs4_stateid_copy(&state->stateid, stateid);
1479 nfs4_stateid_copy(&state->open_stateid, stateid);
1482 static void nfs_clear_open_stateid(struct nfs4_state *state,
1483 nfs4_stateid *arg_stateid,
1484 nfs4_stateid *stateid, fmode_t fmode)
1486 write_seqlock(&state->seqlock);
1487 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1488 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1489 nfs_clear_open_stateid_locked(state, stateid, fmode);
1490 write_sequnlock(&state->seqlock);
1491 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1492 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1495 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1496 const nfs4_stateid *stateid, fmode_t fmode,
1497 nfs4_stateid *freeme)
1499 switch (fmode) {
1500 case FMODE_READ:
1501 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1502 break;
1503 case FMODE_WRITE:
1504 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1505 break;
1506 case FMODE_READ|FMODE_WRITE:
1507 set_bit(NFS_O_RDWR_STATE, &state->flags);
1509 if (!nfs_need_update_open_stateid(state, stateid, freeme))
1510 return;
1511 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1512 nfs4_stateid_copy(&state->stateid, stateid);
1513 nfs4_stateid_copy(&state->open_stateid, stateid);
1516 static void __update_open_stateid(struct nfs4_state *state,
1517 const nfs4_stateid *open_stateid,
1518 const nfs4_stateid *deleg_stateid,
1519 fmode_t fmode,
1520 nfs4_stateid *freeme)
1523 * Protect the call to nfs4_state_set_mode_locked and
1524 * serialise the stateid update
1526 spin_lock(&state->owner->so_lock);
1527 write_seqlock(&state->seqlock);
1528 if (deleg_stateid != NULL) {
1529 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1530 set_bit(NFS_DELEGATED_STATE, &state->flags);
1532 if (open_stateid != NULL)
1533 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
1534 write_sequnlock(&state->seqlock);
1535 update_open_stateflags(state, fmode);
1536 spin_unlock(&state->owner->so_lock);
1539 static int update_open_stateid(struct nfs4_state *state,
1540 const nfs4_stateid *open_stateid,
1541 const nfs4_stateid *delegation,
1542 fmode_t fmode)
1544 struct nfs_server *server = NFS_SERVER(state->inode);
1545 struct nfs_client *clp = server->nfs_client;
1546 struct nfs_inode *nfsi = NFS_I(state->inode);
1547 struct nfs_delegation *deleg_cur;
1548 nfs4_stateid freeme = { };
1549 int ret = 0;
1551 fmode &= (FMODE_READ|FMODE_WRITE);
1553 rcu_read_lock();
1554 deleg_cur = rcu_dereference(nfsi->delegation);
1555 if (deleg_cur == NULL)
1556 goto no_delegation;
1558 spin_lock(&deleg_cur->lock);
1559 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1560 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1561 (deleg_cur->type & fmode) != fmode)
1562 goto no_delegation_unlock;
1564 if (delegation == NULL)
1565 delegation = &deleg_cur->stateid;
1566 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1567 goto no_delegation_unlock;
1569 nfs_mark_delegation_referenced(deleg_cur);
1570 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1571 fmode, &freeme);
1572 ret = 1;
1573 no_delegation_unlock:
1574 spin_unlock(&deleg_cur->lock);
1575 no_delegation:
1576 rcu_read_unlock();
1578 if (!ret && open_stateid != NULL) {
1579 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
1580 ret = 1;
1582 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1583 nfs4_schedule_state_manager(clp);
1584 if (freeme.type != 0)
1585 nfs4_test_and_free_stateid(server, &freeme,
1586 state->owner->so_cred);
1588 return ret;
1591 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1592 const nfs4_stateid *stateid)
1594 struct nfs4_state *state = lsp->ls_state;
1595 bool ret = false;
1597 spin_lock(&state->state_lock);
1598 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1599 goto out_noupdate;
1600 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1601 goto out_noupdate;
1602 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1603 ret = true;
1604 out_noupdate:
1605 spin_unlock(&state->state_lock);
1606 return ret;
1609 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1611 struct nfs_delegation *delegation;
1613 rcu_read_lock();
1614 delegation = rcu_dereference(NFS_I(inode)->delegation);
1615 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1616 rcu_read_unlock();
1617 return;
1619 rcu_read_unlock();
1620 nfs4_inode_return_delegation(inode);
1623 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1625 struct nfs4_state *state = opendata->state;
1626 struct nfs_inode *nfsi = NFS_I(state->inode);
1627 struct nfs_delegation *delegation;
1628 int open_mode = opendata->o_arg.open_flags;
1629 fmode_t fmode = opendata->o_arg.fmode;
1630 enum open_claim_type4 claim = opendata->o_arg.claim;
1631 nfs4_stateid stateid;
1632 int ret = -EAGAIN;
1634 for (;;) {
1635 spin_lock(&state->owner->so_lock);
1636 if (can_open_cached(state, fmode, open_mode)) {
1637 update_open_stateflags(state, fmode);
1638 spin_unlock(&state->owner->so_lock);
1639 goto out_return_state;
1641 spin_unlock(&state->owner->so_lock);
1642 rcu_read_lock();
1643 delegation = rcu_dereference(nfsi->delegation);
1644 if (!can_open_delegated(delegation, fmode, claim)) {
1645 rcu_read_unlock();
1646 break;
1648 /* Save the delegation */
1649 nfs4_stateid_copy(&stateid, &delegation->stateid);
1650 rcu_read_unlock();
1651 nfs_release_seqid(opendata->o_arg.seqid);
1652 if (!opendata->is_recover) {
1653 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1654 if (ret != 0)
1655 goto out;
1657 ret = -EAGAIN;
1659 /* Try to update the stateid using the delegation */
1660 if (update_open_stateid(state, NULL, &stateid, fmode))
1661 goto out_return_state;
1663 out:
1664 return ERR_PTR(ret);
1665 out_return_state:
1666 atomic_inc(&state->count);
1667 return state;
1670 static void
1671 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1673 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1674 struct nfs_delegation *delegation;
1675 int delegation_flags = 0;
1677 rcu_read_lock();
1678 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1679 if (delegation)
1680 delegation_flags = delegation->flags;
1681 rcu_read_unlock();
1682 switch (data->o_arg.claim) {
1683 default:
1684 break;
1685 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1686 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1687 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1688 "returning a delegation for "
1689 "OPEN(CLAIM_DELEGATE_CUR)\n",
1690 clp->cl_hostname);
1691 return;
1693 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1694 nfs_inode_set_delegation(state->inode,
1695 data->owner->so_cred,
1696 &data->o_res);
1697 else
1698 nfs_inode_reclaim_delegation(state->inode,
1699 data->owner->so_cred,
1700 &data->o_res);
1704 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1705 * and update the nfs4_state.
1707 static struct nfs4_state *
1708 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1710 struct inode *inode = data->state->inode;
1711 struct nfs4_state *state = data->state;
1712 int ret;
1714 if (!data->rpc_done) {
1715 if (data->rpc_status) {
1716 ret = data->rpc_status;
1717 goto err;
1719 /* cached opens have already been processed */
1720 goto update;
1723 ret = nfs_refresh_inode(inode, &data->f_attr);
1724 if (ret)
1725 goto err;
1727 if (data->o_res.delegation_type != 0)
1728 nfs4_opendata_check_deleg(data, state);
1729 update:
1730 update_open_stateid(state, &data->o_res.stateid, NULL,
1731 data->o_arg.fmode);
1732 atomic_inc(&state->count);
1734 return state;
1735 err:
1736 return ERR_PTR(ret);
1740 static struct nfs4_state *
1741 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1743 struct inode *inode;
1744 struct nfs4_state *state = NULL;
1745 int ret;
1747 if (!data->rpc_done) {
1748 state = nfs4_try_open_cached(data);
1749 trace_nfs4_cached_open(data->state);
1750 goto out;
1753 ret = -EAGAIN;
1754 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1755 goto err;
1756 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
1757 ret = PTR_ERR(inode);
1758 if (IS_ERR(inode))
1759 goto err;
1760 ret = -ENOMEM;
1761 state = nfs4_get_open_state(inode, data->owner);
1762 if (state == NULL)
1763 goto err_put_inode;
1764 if (data->o_res.delegation_type != 0)
1765 nfs4_opendata_check_deleg(data, state);
1766 update_open_stateid(state, &data->o_res.stateid, NULL,
1767 data->o_arg.fmode);
1768 iput(inode);
1769 out:
1770 nfs_release_seqid(data->o_arg.seqid);
1771 return state;
1772 err_put_inode:
1773 iput(inode);
1774 err:
1775 return ERR_PTR(ret);
1778 static struct nfs4_state *
1779 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1781 struct nfs4_state *ret;
1783 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1784 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1785 else
1786 ret = _nfs4_opendata_to_nfs4_state(data);
1787 nfs4_sequence_free_slot(&data->o_res.seq_res);
1788 return ret;
1791 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1793 struct nfs_inode *nfsi = NFS_I(state->inode);
1794 struct nfs_open_context *ctx;
1796 spin_lock(&state->inode->i_lock);
1797 list_for_each_entry(ctx, &nfsi->open_files, list) {
1798 if (ctx->state != state)
1799 continue;
1800 get_nfs_open_context(ctx);
1801 spin_unlock(&state->inode->i_lock);
1802 return ctx;
1804 spin_unlock(&state->inode->i_lock);
1805 return ERR_PTR(-ENOENT);
1808 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1809 struct nfs4_state *state, enum open_claim_type4 claim)
1811 struct nfs4_opendata *opendata;
1813 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1814 NULL, NULL, claim, GFP_NOFS);
1815 if (opendata == NULL)
1816 return ERR_PTR(-ENOMEM);
1817 opendata->state = state;
1818 atomic_inc(&state->count);
1819 return opendata;
1822 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1823 fmode_t fmode)
1825 struct nfs4_state *newstate;
1826 int ret;
1828 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
1829 return 0;
1830 opendata->o_arg.open_flags = 0;
1831 opendata->o_arg.fmode = fmode;
1832 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1833 NFS_SB(opendata->dentry->d_sb),
1834 fmode, 0);
1835 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1836 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1837 nfs4_init_opendata_res(opendata);
1838 ret = _nfs4_recover_proc_open(opendata);
1839 if (ret != 0)
1840 return ret;
1841 newstate = nfs4_opendata_to_nfs4_state(opendata);
1842 if (IS_ERR(newstate))
1843 return PTR_ERR(newstate);
1844 if (newstate != opendata->state)
1845 ret = -ESTALE;
1846 nfs4_close_state(newstate, fmode);
1847 return ret;
1850 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1852 int ret;
1854 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1855 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1856 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1857 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1858 /* memory barrier prior to reading state->n_* */
1859 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1860 clear_bit(NFS_OPEN_STATE, &state->flags);
1861 smp_rmb();
1862 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1863 if (ret != 0)
1864 return ret;
1865 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1866 if (ret != 0)
1867 return ret;
1868 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1869 if (ret != 0)
1870 return ret;
1872 * We may have performed cached opens for all three recoveries.
1873 * Check if we need to update the current stateid.
1875 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1876 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
1877 write_seqlock(&state->seqlock);
1878 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1879 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1880 write_sequnlock(&state->seqlock);
1882 return 0;
1886 * OPEN_RECLAIM:
1887 * reclaim state on the server after a reboot.
1889 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1891 struct nfs_delegation *delegation;
1892 struct nfs4_opendata *opendata;
1893 fmode_t delegation_type = 0;
1894 int status;
1896 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1897 NFS4_OPEN_CLAIM_PREVIOUS);
1898 if (IS_ERR(opendata))
1899 return PTR_ERR(opendata);
1900 rcu_read_lock();
1901 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1902 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
1903 delegation_type = delegation->type;
1904 rcu_read_unlock();
1905 opendata->o_arg.u.delegation_type = delegation_type;
1906 status = nfs4_open_recover(opendata, state);
1907 nfs4_opendata_put(opendata);
1908 return status;
1911 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
1913 struct nfs_server *server = NFS_SERVER(state->inode);
1914 struct nfs4_exception exception = { };
1915 int err;
1916 do {
1917 err = _nfs4_do_open_reclaim(ctx, state);
1918 trace_nfs4_open_reclaim(ctx, 0, err);
1919 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1920 continue;
1921 if (err != -NFS4ERR_DELAY)
1922 break;
1923 nfs4_handle_exception(server, err, &exception);
1924 } while (exception.retry);
1925 return err;
1928 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1930 struct nfs_open_context *ctx;
1931 int ret;
1933 ctx = nfs4_state_find_open_context(state);
1934 if (IS_ERR(ctx))
1935 return -EAGAIN;
1936 ret = nfs4_do_open_reclaim(ctx, state);
1937 put_nfs_open_context(ctx);
1938 return ret;
1941 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
1943 switch (err) {
1944 default:
1945 printk(KERN_ERR "NFS: %s: unhandled error "
1946 "%d.\n", __func__, err);
1947 case 0:
1948 case -ENOENT:
1949 case -EAGAIN:
1950 case -ESTALE:
1951 break;
1952 case -NFS4ERR_BADSESSION:
1953 case -NFS4ERR_BADSLOT:
1954 case -NFS4ERR_BAD_HIGH_SLOT:
1955 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1956 case -NFS4ERR_DEADSESSION:
1957 set_bit(NFS_DELEGATED_STATE, &state->flags);
1958 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1959 return -EAGAIN;
1960 case -NFS4ERR_STALE_CLIENTID:
1961 case -NFS4ERR_STALE_STATEID:
1962 set_bit(NFS_DELEGATED_STATE, &state->flags);
1963 /* Don't recall a delegation if it was lost */
1964 nfs4_schedule_lease_recovery(server->nfs_client);
1965 return -EAGAIN;
1966 case -NFS4ERR_MOVED:
1967 nfs4_schedule_migration_recovery(server);
1968 return -EAGAIN;
1969 case -NFS4ERR_LEASE_MOVED:
1970 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1971 return -EAGAIN;
1972 case -NFS4ERR_DELEG_REVOKED:
1973 case -NFS4ERR_ADMIN_REVOKED:
1974 case -NFS4ERR_EXPIRED:
1975 case -NFS4ERR_BAD_STATEID:
1976 case -NFS4ERR_OPENMODE:
1977 nfs_inode_find_state_and_recover(state->inode,
1978 stateid);
1979 nfs4_schedule_stateid_recovery(server, state);
1980 return -EAGAIN;
1981 case -NFS4ERR_DELAY:
1982 case -NFS4ERR_GRACE:
1983 set_bit(NFS_DELEGATED_STATE, &state->flags);
1984 ssleep(1);
1985 return -EAGAIN;
1986 case -ENOMEM:
1987 case -NFS4ERR_DENIED:
1988 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1989 return 0;
1991 return err;
1994 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1995 struct nfs4_state *state, const nfs4_stateid *stateid,
1996 fmode_t type)
1998 struct nfs_server *server = NFS_SERVER(state->inode);
1999 struct nfs4_opendata *opendata;
2000 int err = 0;
2002 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2003 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2004 if (IS_ERR(opendata))
2005 return PTR_ERR(opendata);
2006 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2007 write_seqlock(&state->seqlock);
2008 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2009 write_sequnlock(&state->seqlock);
2010 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2011 switch (type & (FMODE_READ|FMODE_WRITE)) {
2012 case FMODE_READ|FMODE_WRITE:
2013 case FMODE_WRITE:
2014 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2015 if (err)
2016 break;
2017 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2018 if (err)
2019 break;
2020 case FMODE_READ:
2021 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2023 nfs4_opendata_put(opendata);
2024 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2027 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2029 struct nfs4_opendata *data = calldata;
2031 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2032 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2035 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2037 struct nfs4_opendata *data = calldata;
2039 nfs40_sequence_done(task, &data->c_res.seq_res);
2041 data->rpc_status = task->tk_status;
2042 if (data->rpc_status == 0) {
2043 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2044 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2045 renew_lease(data->o_res.server, data->timestamp);
2046 data->rpc_done = 1;
2050 static void nfs4_open_confirm_release(void *calldata)
2052 struct nfs4_opendata *data = calldata;
2053 struct nfs4_state *state = NULL;
2055 /* If this request hasn't been cancelled, do nothing */
2056 if (data->cancelled == 0)
2057 goto out_free;
2058 /* In case of error, no cleanup! */
2059 if (!data->rpc_done)
2060 goto out_free;
2061 state = nfs4_opendata_to_nfs4_state(data);
2062 if (!IS_ERR(state))
2063 nfs4_close_state(state, data->o_arg.fmode);
2064 out_free:
2065 nfs4_opendata_put(data);
2068 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2069 .rpc_call_prepare = nfs4_open_confirm_prepare,
2070 .rpc_call_done = nfs4_open_confirm_done,
2071 .rpc_release = nfs4_open_confirm_release,
2075 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2077 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2079 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2080 struct rpc_task *task;
2081 struct rpc_message msg = {
2082 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2083 .rpc_argp = &data->c_arg,
2084 .rpc_resp = &data->c_res,
2085 .rpc_cred = data->owner->so_cred,
2087 struct rpc_task_setup task_setup_data = {
2088 .rpc_client = server->client,
2089 .rpc_message = &msg,
2090 .callback_ops = &nfs4_open_confirm_ops,
2091 .callback_data = data,
2092 .workqueue = nfsiod_workqueue,
2093 .flags = RPC_TASK_ASYNC,
2095 int status;
2097 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
2098 kref_get(&data->kref);
2099 data->rpc_done = 0;
2100 data->rpc_status = 0;
2101 data->timestamp = jiffies;
2102 if (data->is_recover)
2103 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
2104 task = rpc_run_task(&task_setup_data);
2105 if (IS_ERR(task))
2106 return PTR_ERR(task);
2107 status = nfs4_wait_for_completion_rpc_task(task);
2108 if (status != 0) {
2109 data->cancelled = 1;
2110 smp_wmb();
2111 } else
2112 status = data->rpc_status;
2113 rpc_put_task(task);
2114 return status;
2117 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2119 struct nfs4_opendata *data = calldata;
2120 struct nfs4_state_owner *sp = data->owner;
2121 struct nfs_client *clp = sp->so_server->nfs_client;
2122 enum open_claim_type4 claim = data->o_arg.claim;
2124 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2125 goto out_wait;
2127 * Check if we still need to send an OPEN call, or if we can use
2128 * a delegation instead.
2130 if (data->state != NULL) {
2131 struct nfs_delegation *delegation;
2133 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
2134 goto out_no_action;
2135 rcu_read_lock();
2136 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
2137 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2138 goto unlock_no_action;
2139 rcu_read_unlock();
2141 /* Update client id. */
2142 data->o_arg.clientid = clp->cl_clientid;
2143 switch (claim) {
2144 default:
2145 break;
2146 case NFS4_OPEN_CLAIM_PREVIOUS:
2147 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2148 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2149 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2150 case NFS4_OPEN_CLAIM_FH:
2151 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2152 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2154 data->timestamp = jiffies;
2155 if (nfs4_setup_sequence(data->o_arg.server,
2156 &data->o_arg.seq_args,
2157 &data->o_res.seq_res,
2158 task) != 0)
2159 nfs_release_seqid(data->o_arg.seqid);
2161 /* Set the create mode (note dependency on the session type) */
2162 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2163 if (data->o_arg.open_flags & O_EXCL) {
2164 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2165 if (nfs4_has_persistent_session(clp))
2166 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2167 else if (clp->cl_mvops->minor_version > 0)
2168 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2170 return;
2171 unlock_no_action:
2172 trace_nfs4_cached_open(data->state);
2173 rcu_read_unlock();
2174 out_no_action:
2175 task->tk_action = NULL;
2176 out_wait:
2177 nfs4_sequence_done(task, &data->o_res.seq_res);
2180 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2182 struct nfs4_opendata *data = calldata;
2184 data->rpc_status = task->tk_status;
2186 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2187 return;
2189 if (task->tk_status == 0) {
2190 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2191 switch (data->o_res.f_attr->mode & S_IFMT) {
2192 case S_IFREG:
2193 break;
2194 case S_IFLNK:
2195 data->rpc_status = -ELOOP;
2196 break;
2197 case S_IFDIR:
2198 data->rpc_status = -EISDIR;
2199 break;
2200 default:
2201 data->rpc_status = -ENOTDIR;
2204 renew_lease(data->o_res.server, data->timestamp);
2205 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2206 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2208 data->rpc_done = 1;
2211 static void nfs4_open_release(void *calldata)
2213 struct nfs4_opendata *data = calldata;
2214 struct nfs4_state *state = NULL;
2216 /* If this request hasn't been cancelled, do nothing */
2217 if (data->cancelled == 0)
2218 goto out_free;
2219 /* In case of error, no cleanup! */
2220 if (data->rpc_status != 0 || !data->rpc_done)
2221 goto out_free;
2222 /* In case we need an open_confirm, no cleanup! */
2223 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2224 goto out_free;
2225 state = nfs4_opendata_to_nfs4_state(data);
2226 if (!IS_ERR(state))
2227 nfs4_close_state(state, data->o_arg.fmode);
2228 out_free:
2229 nfs4_opendata_put(data);
2232 static const struct rpc_call_ops nfs4_open_ops = {
2233 .rpc_call_prepare = nfs4_open_prepare,
2234 .rpc_call_done = nfs4_open_done,
2235 .rpc_release = nfs4_open_release,
2238 static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
2240 struct inode *dir = d_inode(data->dir);
2241 struct nfs_server *server = NFS_SERVER(dir);
2242 struct nfs_openargs *o_arg = &data->o_arg;
2243 struct nfs_openres *o_res = &data->o_res;
2244 struct rpc_task *task;
2245 struct rpc_message msg = {
2246 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2247 .rpc_argp = o_arg,
2248 .rpc_resp = o_res,
2249 .rpc_cred = data->owner->so_cred,
2251 struct rpc_task_setup task_setup_data = {
2252 .rpc_client = server->client,
2253 .rpc_message = &msg,
2254 .callback_ops = &nfs4_open_ops,
2255 .callback_data = data,
2256 .workqueue = nfsiod_workqueue,
2257 .flags = RPC_TASK_ASYNC,
2259 int status;
2261 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
2262 kref_get(&data->kref);
2263 data->rpc_done = 0;
2264 data->rpc_status = 0;
2265 data->cancelled = 0;
2266 data->is_recover = 0;
2267 if (isrecover) {
2268 nfs4_set_sequence_privileged(&o_arg->seq_args);
2269 data->is_recover = 1;
2271 task = rpc_run_task(&task_setup_data);
2272 if (IS_ERR(task))
2273 return PTR_ERR(task);
2274 status = nfs4_wait_for_completion_rpc_task(task);
2275 if (status != 0) {
2276 data->cancelled = 1;
2277 smp_wmb();
2278 } else
2279 status = data->rpc_status;
2280 rpc_put_task(task);
2282 return status;
2285 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2287 struct inode *dir = d_inode(data->dir);
2288 struct nfs_openres *o_res = &data->o_res;
2289 int status;
2291 status = nfs4_run_open_task(data, 1);
2292 if (status != 0 || !data->rpc_done)
2293 return status;
2295 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2297 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2298 status = _nfs4_proc_open_confirm(data);
2299 if (status != 0)
2300 return status;
2303 return status;
2307 * Additional permission checks in order to distinguish between an
2308 * open for read, and an open for execute. This works around the
2309 * fact that NFSv4 OPEN treats read and execute permissions as being
2310 * the same.
2311 * Note that in the non-execute case, we want to turn off permission
2312 * checking if we just created a new file (POSIX open() semantics).
2314 static int nfs4_opendata_access(struct rpc_cred *cred,
2315 struct nfs4_opendata *opendata,
2316 struct nfs4_state *state, fmode_t fmode,
2317 int openflags)
2319 struct nfs_access_entry cache;
2320 u32 mask;
2322 /* access call failed or for some reason the server doesn't
2323 * support any access modes -- defer access call until later */
2324 if (opendata->o_res.access_supported == 0)
2325 return 0;
2327 mask = 0;
2329 * Use openflags to check for exec, because fmode won't
2330 * always have FMODE_EXEC set when file open for exec.
2332 if (openflags & __FMODE_EXEC) {
2333 /* ONLY check for exec rights */
2334 mask = MAY_EXEC;
2335 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2336 mask = MAY_READ;
2338 cache.cred = cred;
2339 cache.jiffies = jiffies;
2340 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2341 nfs_access_add_cache(state->inode, &cache);
2343 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
2344 return 0;
2346 return -EACCES;
2350 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2352 static int _nfs4_proc_open(struct nfs4_opendata *data)
2354 struct inode *dir = d_inode(data->dir);
2355 struct nfs_server *server = NFS_SERVER(dir);
2356 struct nfs_openargs *o_arg = &data->o_arg;
2357 struct nfs_openres *o_res = &data->o_res;
2358 int status;
2360 status = nfs4_run_open_task(data, 0);
2361 if (!data->rpc_done)
2362 return status;
2363 if (status != 0) {
2364 if (status == -NFS4ERR_BADNAME &&
2365 !(o_arg->open_flags & O_CREAT))
2366 return -ENOENT;
2367 return status;
2370 nfs_fattr_map_and_free_names(server, &data->f_attr);
2372 if (o_arg->open_flags & O_CREAT) {
2373 update_changeattr(dir, &o_res->cinfo);
2374 if (o_arg->open_flags & O_EXCL)
2375 data->file_created = 1;
2376 else if (o_res->cinfo.before != o_res->cinfo.after)
2377 data->file_created = 1;
2379 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2380 server->caps &= ~NFS_CAP_POSIX_LOCK;
2381 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2382 status = _nfs4_proc_open_confirm(data);
2383 if (status != 0)
2384 return status;
2386 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2387 nfs4_sequence_free_slot(&o_res->seq_res);
2388 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
2390 return 0;
2393 static int nfs4_recover_expired_lease(struct nfs_server *server)
2395 return nfs4_client_recover_expired_lease(server->nfs_client);
2399 * OPEN_EXPIRED:
2400 * reclaim state on the server after a network partition.
2401 * Assumes caller holds the appropriate lock
2403 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2405 struct nfs4_opendata *opendata;
2406 int ret;
2408 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2409 NFS4_OPEN_CLAIM_FH);
2410 if (IS_ERR(opendata))
2411 return PTR_ERR(opendata);
2412 ret = nfs4_open_recover(opendata, state);
2413 if (ret == -ESTALE)
2414 d_drop(ctx->dentry);
2415 nfs4_opendata_put(opendata);
2416 return ret;
2419 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2421 struct nfs_server *server = NFS_SERVER(state->inode);
2422 struct nfs4_exception exception = { };
2423 int err;
2425 do {
2426 err = _nfs4_open_expired(ctx, state);
2427 trace_nfs4_open_expired(ctx, 0, err);
2428 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2429 continue;
2430 switch (err) {
2431 default:
2432 goto out;
2433 case -NFS4ERR_GRACE:
2434 case -NFS4ERR_DELAY:
2435 nfs4_handle_exception(server, err, &exception);
2436 err = 0;
2438 } while (exception.retry);
2439 out:
2440 return err;
2443 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2445 struct nfs_open_context *ctx;
2446 int ret;
2448 ctx = nfs4_state_find_open_context(state);
2449 if (IS_ERR(ctx))
2450 return -EAGAIN;
2451 ret = nfs4_do_open_expired(ctx, state);
2452 put_nfs_open_context(ctx);
2453 return ret;
2456 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2457 const nfs4_stateid *stateid)
2459 nfs_remove_bad_delegation(state->inode, stateid);
2460 write_seqlock(&state->seqlock);
2461 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2462 write_sequnlock(&state->seqlock);
2463 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2466 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2468 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2469 nfs_finish_clear_delegation_stateid(state, NULL);
2472 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2474 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2475 nfs40_clear_delegation_stateid(state);
2476 return nfs4_open_expired(sp, state);
2479 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2480 nfs4_stateid *stateid,
2481 struct rpc_cred *cred)
2483 return -NFS4ERR_BAD_STATEID;
2486 #if defined(CONFIG_NFS_V4_1)
2487 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2488 nfs4_stateid *stateid,
2489 struct rpc_cred *cred)
2491 int status;
2493 switch (stateid->type) {
2494 default:
2495 break;
2496 case NFS4_INVALID_STATEID_TYPE:
2497 case NFS4_SPECIAL_STATEID_TYPE:
2498 return -NFS4ERR_BAD_STATEID;
2499 case NFS4_REVOKED_STATEID_TYPE:
2500 goto out_free;
2503 status = nfs41_test_stateid(server, stateid, cred);
2504 switch (status) {
2505 case -NFS4ERR_EXPIRED:
2506 case -NFS4ERR_ADMIN_REVOKED:
2507 case -NFS4ERR_DELEG_REVOKED:
2508 break;
2509 default:
2510 return status;
2512 out_free:
2513 /* Ack the revoked state to the server */
2514 nfs41_free_stateid(server, stateid, cred, true);
2515 return -NFS4ERR_EXPIRED;
2518 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2520 struct nfs_server *server = NFS_SERVER(state->inode);
2521 nfs4_stateid stateid;
2522 struct nfs_delegation *delegation;
2523 struct rpc_cred *cred;
2524 int status;
2526 /* Get the delegation credential for use by test/free_stateid */
2527 rcu_read_lock();
2528 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2529 if (delegation == NULL) {
2530 rcu_read_unlock();
2531 return;
2534 nfs4_stateid_copy(&stateid, &delegation->stateid);
2535 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2536 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2537 &delegation->flags)) {
2538 rcu_read_unlock();
2539 nfs_finish_clear_delegation_stateid(state, &stateid);
2540 return;
2543 cred = get_rpccred(delegation->cred);
2544 rcu_read_unlock();
2545 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2546 trace_nfs4_test_delegation_stateid(state, NULL, status);
2547 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2548 nfs_finish_clear_delegation_stateid(state, &stateid);
2550 put_rpccred(cred);
2554 * nfs41_check_expired_locks - possibly free a lock stateid
2556 * @state: NFSv4 state for an inode
2558 * Returns NFS_OK if recovery for this stateid is now finished.
2559 * Otherwise a negative NFS4ERR value is returned.
2561 static int nfs41_check_expired_locks(struct nfs4_state *state)
2563 int status, ret = NFS_OK;
2564 struct nfs4_lock_state *lsp, *prev = NULL;
2565 struct nfs_server *server = NFS_SERVER(state->inode);
2567 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2568 goto out;
2570 spin_lock(&state->state_lock);
2571 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2572 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2573 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2575 atomic_inc(&lsp->ls_count);
2576 spin_unlock(&state->state_lock);
2578 nfs4_put_lock_state(prev);
2579 prev = lsp;
2581 status = nfs41_test_and_free_expired_stateid(server,
2582 &lsp->ls_stateid,
2583 cred);
2584 trace_nfs4_test_lock_stateid(state, lsp, status);
2585 if (status == -NFS4ERR_EXPIRED ||
2586 status == -NFS4ERR_BAD_STATEID) {
2587 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2588 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2589 if (!recover_lost_locks)
2590 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2591 } else if (status != NFS_OK) {
2592 ret = status;
2593 nfs4_put_lock_state(prev);
2594 goto out;
2596 spin_lock(&state->state_lock);
2599 spin_unlock(&state->state_lock);
2600 nfs4_put_lock_state(prev);
2601 out:
2602 return ret;
2606 * nfs41_check_open_stateid - possibly free an open stateid
2608 * @state: NFSv4 state for an inode
2610 * Returns NFS_OK if recovery for this stateid is now finished.
2611 * Otherwise a negative NFS4ERR value is returned.
2613 static int nfs41_check_open_stateid(struct nfs4_state *state)
2615 struct nfs_server *server = NFS_SERVER(state->inode);
2616 nfs4_stateid *stateid = &state->open_stateid;
2617 struct rpc_cred *cred = state->owner->so_cred;
2618 int status;
2620 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
2621 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2622 if (nfs4_have_delegation(state->inode, state->state))
2623 return NFS_OK;
2624 return -NFS4ERR_OPENMODE;
2626 return -NFS4ERR_BAD_STATEID;
2628 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2629 trace_nfs4_test_open_stateid(state, NULL, status);
2630 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2631 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2632 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2633 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2634 clear_bit(NFS_OPEN_STATE, &state->flags);
2635 stateid->type = NFS4_INVALID_STATEID_TYPE;
2637 if (status != NFS_OK)
2638 return status;
2639 if (nfs_open_stateid_recover_openmode(state))
2640 return -NFS4ERR_OPENMODE;
2641 return NFS_OK;
2644 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2646 int status;
2648 nfs41_check_delegation_stateid(state);
2649 status = nfs41_check_expired_locks(state);
2650 if (status != NFS_OK)
2651 return status;
2652 status = nfs41_check_open_stateid(state);
2653 if (status != NFS_OK)
2654 status = nfs4_open_expired(sp, state);
2655 return status;
2657 #endif
2660 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2661 * fields corresponding to attributes that were used to store the verifier.
2662 * Make sure we clobber those fields in the later setattr call
2664 static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2665 struct iattr *sattr, struct nfs4_label **label)
2667 const u32 *attrset = opendata->o_res.attrset;
2669 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2670 !(sattr->ia_valid & ATTR_ATIME_SET))
2671 sattr->ia_valid |= ATTR_ATIME;
2673 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2674 !(sattr->ia_valid & ATTR_MTIME_SET))
2675 sattr->ia_valid |= ATTR_MTIME;
2677 /* Except MODE, it seems harmless of setting twice. */
2678 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2679 attrset[1] & FATTR4_WORD1_MODE)
2680 sattr->ia_valid &= ~ATTR_MODE;
2682 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2683 *label = NULL;
2686 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2687 fmode_t fmode,
2688 int flags,
2689 struct nfs_open_context *ctx)
2691 struct nfs4_state_owner *sp = opendata->owner;
2692 struct nfs_server *server = sp->so_server;
2693 struct dentry *dentry;
2694 struct nfs4_state *state;
2695 unsigned int seq;
2696 int ret;
2698 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2700 ret = _nfs4_proc_open(opendata);
2701 if (ret != 0)
2702 goto out;
2704 state = nfs4_opendata_to_nfs4_state(opendata);
2705 ret = PTR_ERR(state);
2706 if (IS_ERR(state))
2707 goto out;
2708 ctx->state = state;
2709 if (server->caps & NFS_CAP_POSIX_LOCK)
2710 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2711 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2712 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2714 dentry = opendata->dentry;
2715 if (d_really_is_negative(dentry)) {
2716 struct dentry *alias;
2717 d_drop(dentry);
2718 alias = d_exact_alias(dentry, state->inode);
2719 if (!alias)
2720 alias = d_splice_alias(igrab(state->inode), dentry);
2721 /* d_splice_alias() can't fail here - it's a non-directory */
2722 if (alias) {
2723 dput(ctx->dentry);
2724 ctx->dentry = dentry = alias;
2726 nfs_set_verifier(dentry,
2727 nfs_save_change_attribute(d_inode(opendata->dir)));
2730 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2731 if (ret != 0)
2732 goto out;
2734 if (d_inode(dentry) == state->inode) {
2735 nfs_inode_attach_open_context(ctx);
2736 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2737 nfs4_schedule_stateid_recovery(server, state);
2739 out:
2740 return ret;
2744 * Returns a referenced nfs4_state
2746 static int _nfs4_do_open(struct inode *dir,
2747 struct nfs_open_context *ctx,
2748 int flags,
2749 struct iattr *sattr,
2750 struct nfs4_label *label,
2751 int *opened)
2753 struct nfs4_state_owner *sp;
2754 struct nfs4_state *state = NULL;
2755 struct nfs_server *server = NFS_SERVER(dir);
2756 struct nfs4_opendata *opendata;
2757 struct dentry *dentry = ctx->dentry;
2758 struct rpc_cred *cred = ctx->cred;
2759 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2760 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2761 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2762 struct nfs4_label *olabel = NULL;
2763 int status;
2765 /* Protect against reboot recovery conflicts */
2766 status = -ENOMEM;
2767 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2768 if (sp == NULL) {
2769 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2770 goto out_err;
2772 status = nfs4_recover_expired_lease(server);
2773 if (status != 0)
2774 goto err_put_state_owner;
2775 if (d_really_is_positive(dentry))
2776 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2777 status = -ENOMEM;
2778 if (d_really_is_positive(dentry))
2779 claim = NFS4_OPEN_CLAIM_FH;
2780 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
2781 label, claim, GFP_KERNEL);
2782 if (opendata == NULL)
2783 goto err_put_state_owner;
2785 if (label) {
2786 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2787 if (IS_ERR(olabel)) {
2788 status = PTR_ERR(olabel);
2789 goto err_opendata_put;
2793 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2794 if (!opendata->f_attr.mdsthreshold) {
2795 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2796 if (!opendata->f_attr.mdsthreshold)
2797 goto err_free_label;
2799 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2801 if (d_really_is_positive(dentry))
2802 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2804 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2805 if (status != 0)
2806 goto err_free_label;
2807 state = ctx->state;
2809 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
2810 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2811 nfs4_exclusive_attrset(opendata, sattr, &label);
2813 * send create attributes which was not set by open
2814 * with an extra setattr.
2816 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2817 nfs_fattr_init(opendata->o_res.f_attr);
2818 status = nfs4_do_setattr(state->inode, cred,
2819 opendata->o_res.f_attr, sattr,
2820 state, label, olabel);
2821 if (status == 0) {
2822 nfs_setattr_update_inode(state->inode, sattr,
2823 opendata->o_res.f_attr);
2824 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2828 if (opened && opendata->file_created)
2829 *opened |= FILE_CREATED;
2831 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
2832 *ctx_th = opendata->f_attr.mdsthreshold;
2833 opendata->f_attr.mdsthreshold = NULL;
2836 nfs4_label_free(olabel);
2838 nfs4_opendata_put(opendata);
2839 nfs4_put_state_owner(sp);
2840 return 0;
2841 err_free_label:
2842 nfs4_label_free(olabel);
2843 err_opendata_put:
2844 nfs4_opendata_put(opendata);
2845 err_put_state_owner:
2846 nfs4_put_state_owner(sp);
2847 out_err:
2848 return status;
2852 static struct nfs4_state *nfs4_do_open(struct inode *dir,
2853 struct nfs_open_context *ctx,
2854 int flags,
2855 struct iattr *sattr,
2856 struct nfs4_label *label,
2857 int *opened)
2859 struct nfs_server *server = NFS_SERVER(dir);
2860 struct nfs4_exception exception = { };
2861 struct nfs4_state *res;
2862 int status;
2864 do {
2865 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
2866 res = ctx->state;
2867 trace_nfs4_open_file(ctx, flags, status);
2868 if (status == 0)
2869 break;
2870 /* NOTE: BAD_SEQID means the server and client disagree about the
2871 * book-keeping w.r.t. state-changing operations
2872 * (OPEN/CLOSE/LOCK/LOCKU...)
2873 * It is actually a sign of a bug on the client or on the server.
2875 * If we receive a BAD_SEQID error in the particular case of
2876 * doing an OPEN, we assume that nfs_increment_open_seqid() will
2877 * have unhashed the old state_owner for us, and that we can
2878 * therefore safely retry using a new one. We should still warn
2879 * the user though...
2881 if (status == -NFS4ERR_BAD_SEQID) {
2882 pr_warn_ratelimited("NFS: v4 server %s "
2883 " returned a bad sequence-id error!\n",
2884 NFS_SERVER(dir)->nfs_client->cl_hostname);
2885 exception.retry = 1;
2886 continue;
2889 * BAD_STATEID on OPEN means that the server cancelled our
2890 * state before it received the OPEN_CONFIRM.
2891 * Recover by retrying the request as per the discussion
2892 * on Page 181 of RFC3530.
2894 if (status == -NFS4ERR_BAD_STATEID) {
2895 exception.retry = 1;
2896 continue;
2898 if (status == -EAGAIN) {
2899 /* We must have found a delegation */
2900 exception.retry = 1;
2901 continue;
2903 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2904 continue;
2905 res = ERR_PTR(nfs4_handle_exception(server,
2906 status, &exception));
2907 } while (exception.retry);
2908 return res;
2911 static int _nfs4_do_setattr(struct inode *inode,
2912 struct nfs_setattrargs *arg,
2913 struct nfs_setattrres *res,
2914 struct rpc_cred *cred,
2915 struct nfs4_state *state)
2917 struct nfs_server *server = NFS_SERVER(inode);
2918 struct rpc_message msg = {
2919 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2920 .rpc_argp = arg,
2921 .rpc_resp = res,
2922 .rpc_cred = cred,
2924 struct rpc_cred *delegation_cred = NULL;
2925 unsigned long timestamp = jiffies;
2926 fmode_t fmode;
2927 bool truncate;
2928 int status;
2930 nfs_fattr_init(res->fattr);
2932 /* Servers should only apply open mode checks for file size changes */
2933 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2934 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2936 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2937 /* Use that stateid */
2938 } else if (truncate && state != NULL) {
2939 struct nfs_lockowner lockowner = {
2940 .l_owner = current->files,
2941 .l_pid = current->tgid,
2943 if (!nfs4_valid_open_stateid(state))
2944 return -EBADF;
2945 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2946 &arg->stateid, &delegation_cred) == -EIO)
2947 return -EBADF;
2948 } else
2949 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2950 if (delegation_cred)
2951 msg.rpc_cred = delegation_cred;
2953 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2955 put_rpccred(delegation_cred);
2956 if (status == 0 && state != NULL)
2957 renew_lease(server, timestamp);
2958 trace_nfs4_setattr(inode, &arg->stateid, status);
2959 return status;
2962 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2963 struct nfs_fattr *fattr, struct iattr *sattr,
2964 struct nfs4_state *state, struct nfs4_label *ilabel,
2965 struct nfs4_label *olabel)
2967 struct nfs_server *server = NFS_SERVER(inode);
2968 struct nfs_setattrargs arg = {
2969 .fh = NFS_FH(inode),
2970 .iap = sattr,
2971 .server = server,
2972 .bitmask = server->attr_bitmask,
2973 .label = ilabel,
2975 struct nfs_setattrres res = {
2976 .fattr = fattr,
2977 .label = olabel,
2978 .server = server,
2980 struct nfs4_exception exception = {
2981 .state = state,
2982 .inode = inode,
2983 .stateid = &arg.stateid,
2985 int err;
2987 arg.bitmask = nfs4_bitmask(server, ilabel);
2988 if (ilabel)
2989 arg.bitmask = nfs4_bitmask(server, olabel);
2991 do {
2992 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
2993 switch (err) {
2994 case -NFS4ERR_OPENMODE:
2995 if (!(sattr->ia_valid & ATTR_SIZE)) {
2996 pr_warn_once("NFSv4: server %s is incorrectly "
2997 "applying open mode checks to "
2998 "a SETATTR that is not "
2999 "changing file size.\n",
3000 server->nfs_client->cl_hostname);
3002 if (state && !(state->state & FMODE_WRITE)) {
3003 err = -EBADF;
3004 if (sattr->ia_valid & ATTR_OPEN)
3005 err = -EACCES;
3006 goto out;
3009 err = nfs4_handle_exception(server, err, &exception);
3010 } while (exception.retry);
3011 out:
3012 return err;
3015 static bool
3016 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3018 if (inode == NULL || !nfs_have_layout(inode))
3019 return false;
3021 return pnfs_wait_on_layoutreturn(inode, task);
3024 struct nfs4_closedata {
3025 struct inode *inode;
3026 struct nfs4_state *state;
3027 struct nfs_closeargs arg;
3028 struct nfs_closeres res;
3029 struct nfs_fattr fattr;
3030 unsigned long timestamp;
3031 bool roc;
3032 u32 roc_barrier;
3035 static void nfs4_free_closedata(void *data)
3037 struct nfs4_closedata *calldata = data;
3038 struct nfs4_state_owner *sp = calldata->state->owner;
3039 struct super_block *sb = calldata->state->inode->i_sb;
3041 if (calldata->roc)
3042 pnfs_roc_release(calldata->state->inode);
3043 nfs4_put_open_state(calldata->state);
3044 nfs_free_seqid(calldata->arg.seqid);
3045 nfs4_put_state_owner(sp);
3046 nfs_sb_deactive(sb);
3047 kfree(calldata);
3050 static void nfs4_close_done(struct rpc_task *task, void *data)
3052 struct nfs4_closedata *calldata = data;
3053 struct nfs4_state *state = calldata->state;
3054 struct nfs_server *server = NFS_SERVER(calldata->inode);
3055 nfs4_stateid *res_stateid = NULL;
3057 dprintk("%s: begin!\n", __func__);
3058 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3059 return;
3060 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3061 /* hmm. we are done with the inode, and in the process of freeing
3062 * the state_owner. we keep this around to process errors
3064 switch (task->tk_status) {
3065 case 0:
3066 res_stateid = &calldata->res.stateid;
3067 if (calldata->roc)
3068 pnfs_roc_set_barrier(state->inode,
3069 calldata->roc_barrier);
3070 renew_lease(server, calldata->timestamp);
3071 break;
3072 case -NFS4ERR_ADMIN_REVOKED:
3073 case -NFS4ERR_STALE_STATEID:
3074 case -NFS4ERR_EXPIRED:
3075 nfs4_free_revoked_stateid(server,
3076 &calldata->arg.stateid,
3077 task->tk_msg.rpc_cred);
3078 case -NFS4ERR_OLD_STATEID:
3079 case -NFS4ERR_BAD_STATEID:
3080 if (!nfs4_stateid_match(&calldata->arg.stateid,
3081 &state->open_stateid)) {
3082 rpc_restart_call_prepare(task);
3083 goto out_release;
3085 if (calldata->arg.fmode == 0)
3086 break;
3087 default:
3088 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
3089 rpc_restart_call_prepare(task);
3090 goto out_release;
3093 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3094 res_stateid, calldata->arg.fmode);
3095 out_release:
3096 nfs_release_seqid(calldata->arg.seqid);
3097 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
3098 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3101 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3103 struct nfs4_closedata *calldata = data;
3104 struct nfs4_state *state = calldata->state;
3105 struct inode *inode = calldata->inode;
3106 bool is_rdonly, is_wronly, is_rdwr;
3107 int call_close = 0;
3109 dprintk("%s: begin!\n", __func__);
3110 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3111 goto out_wait;
3113 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3114 spin_lock(&state->owner->so_lock);
3115 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3116 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3117 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3118 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
3119 /* Calculate the change in open mode */
3120 calldata->arg.fmode = 0;
3121 if (state->n_rdwr == 0) {
3122 if (state->n_rdonly == 0)
3123 call_close |= is_rdonly;
3124 else if (is_rdonly)
3125 calldata->arg.fmode |= FMODE_READ;
3126 if (state->n_wronly == 0)
3127 call_close |= is_wronly;
3128 else if (is_wronly)
3129 calldata->arg.fmode |= FMODE_WRITE;
3130 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3131 call_close |= is_rdwr;
3132 } else if (is_rdwr)
3133 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3135 if (!nfs4_valid_open_stateid(state) ||
3136 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
3137 call_close = 0;
3138 spin_unlock(&state->owner->so_lock);
3140 if (!call_close) {
3141 /* Note: exit _without_ calling nfs4_close_done */
3142 goto out_no_action;
3145 if (nfs4_wait_on_layoutreturn(inode, task)) {
3146 nfs_release_seqid(calldata->arg.seqid);
3147 goto out_wait;
3150 if (calldata->arg.fmode == 0)
3151 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3152 if (calldata->roc)
3153 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
3155 calldata->arg.share_access =
3156 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3157 calldata->arg.fmode, 0);
3159 nfs_fattr_init(calldata->res.fattr);
3160 calldata->timestamp = jiffies;
3161 if (nfs4_setup_sequence(NFS_SERVER(inode),
3162 &calldata->arg.seq_args,
3163 &calldata->res.seq_res,
3164 task) != 0)
3165 nfs_release_seqid(calldata->arg.seqid);
3166 dprintk("%s: done!\n", __func__);
3167 return;
3168 out_no_action:
3169 task->tk_action = NULL;
3170 out_wait:
3171 nfs4_sequence_done(task, &calldata->res.seq_res);
3174 static const struct rpc_call_ops nfs4_close_ops = {
3175 .rpc_call_prepare = nfs4_close_prepare,
3176 .rpc_call_done = nfs4_close_done,
3177 .rpc_release = nfs4_free_closedata,
3180 static bool nfs4_roc(struct inode *inode)
3182 if (!nfs_have_layout(inode))
3183 return false;
3184 return pnfs_roc(inode);
3188 * It is possible for data to be read/written from a mem-mapped file
3189 * after the sys_close call (which hits the vfs layer as a flush).
3190 * This means that we can't safely call nfsv4 close on a file until
3191 * the inode is cleared. This in turn means that we are not good
3192 * NFSv4 citizens - we do not indicate to the server to update the file's
3193 * share state even when we are done with one of the three share
3194 * stateid's in the inode.
3196 * NOTE: Caller must be holding the sp->so_owner semaphore!
3198 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3200 struct nfs_server *server = NFS_SERVER(state->inode);
3201 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3202 struct nfs4_closedata *calldata;
3203 struct nfs4_state_owner *sp = state->owner;
3204 struct rpc_task *task;
3205 struct rpc_message msg = {
3206 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3207 .rpc_cred = state->owner->so_cred,
3209 struct rpc_task_setup task_setup_data = {
3210 .rpc_client = server->client,
3211 .rpc_message = &msg,
3212 .callback_ops = &nfs4_close_ops,
3213 .workqueue = nfsiod_workqueue,
3214 .flags = RPC_TASK_ASYNC,
3216 int status = -ENOMEM;
3218 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3219 &task_setup_data.rpc_client, &msg);
3221 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3222 if (calldata == NULL)
3223 goto out;
3224 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
3225 calldata->inode = state->inode;
3226 calldata->state = state;
3227 calldata->arg.fh = NFS_FH(state->inode);
3228 /* Serialization for the sequence id */
3229 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3230 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3231 if (IS_ERR(calldata->arg.seqid))
3232 goto out_free_calldata;
3233 calldata->arg.fmode = 0;
3234 calldata->arg.bitmask = server->cache_consistency_bitmask;
3235 calldata->res.fattr = &calldata->fattr;
3236 calldata->res.seqid = calldata->arg.seqid;
3237 calldata->res.server = server;
3238 calldata->roc = nfs4_roc(state->inode);
3239 nfs_sb_active(calldata->inode->i_sb);
3241 msg.rpc_argp = &calldata->arg;
3242 msg.rpc_resp = &calldata->res;
3243 task_setup_data.callback_data = calldata;
3244 task = rpc_run_task(&task_setup_data);
3245 if (IS_ERR(task))
3246 return PTR_ERR(task);
3247 status = 0;
3248 if (wait)
3249 status = rpc_wait_for_completion_task(task);
3250 rpc_put_task(task);
3251 return status;
3252 out_free_calldata:
3253 kfree(calldata);
3254 out:
3255 nfs4_put_open_state(state);
3256 nfs4_put_state_owner(sp);
3257 return status;
3260 static struct inode *
3261 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3262 int open_flags, struct iattr *attr, int *opened)
3264 struct nfs4_state *state;
3265 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3267 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3269 /* Protect against concurrent sillydeletes */
3270 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3272 nfs4_label_release_security(label);
3274 if (IS_ERR(state))
3275 return ERR_CAST(state);
3276 return state->inode;
3279 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3281 if (ctx->state == NULL)
3282 return;
3283 if (is_sync)
3284 nfs4_close_sync(ctx->state, ctx->mode);
3285 else
3286 nfs4_close_state(ctx->state, ctx->mode);
3289 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3290 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3291 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
3293 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3295 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3296 struct nfs4_server_caps_arg args = {
3297 .fhandle = fhandle,
3298 .bitmask = bitmask,
3300 struct nfs4_server_caps_res res = {};
3301 struct rpc_message msg = {
3302 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3303 .rpc_argp = &args,
3304 .rpc_resp = &res,
3306 int status;
3308 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3309 FATTR4_WORD0_FH_EXPIRE_TYPE |
3310 FATTR4_WORD0_LINK_SUPPORT |
3311 FATTR4_WORD0_SYMLINK_SUPPORT |
3312 FATTR4_WORD0_ACLSUPPORT;
3313 if (minorversion)
3314 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3316 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3317 if (status == 0) {
3318 /* Sanity check the server answers */
3319 switch (minorversion) {
3320 case 0:
3321 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3322 res.attr_bitmask[2] = 0;
3323 break;
3324 case 1:
3325 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3326 break;
3327 case 2:
3328 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3330 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3331 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3332 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3333 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3334 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3335 NFS_CAP_CTIME|NFS_CAP_MTIME|
3336 NFS_CAP_SECURITY_LABEL);
3337 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3338 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3339 server->caps |= NFS_CAP_ACLS;
3340 if (res.has_links != 0)
3341 server->caps |= NFS_CAP_HARDLINKS;
3342 if (res.has_symlinks != 0)
3343 server->caps |= NFS_CAP_SYMLINKS;
3344 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3345 server->caps |= NFS_CAP_FILEID;
3346 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3347 server->caps |= NFS_CAP_MODE;
3348 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3349 server->caps |= NFS_CAP_NLINK;
3350 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3351 server->caps |= NFS_CAP_OWNER;
3352 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3353 server->caps |= NFS_CAP_OWNER_GROUP;
3354 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3355 server->caps |= NFS_CAP_ATIME;
3356 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3357 server->caps |= NFS_CAP_CTIME;
3358 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3359 server->caps |= NFS_CAP_MTIME;
3360 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3361 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3362 server->caps |= NFS_CAP_SECURITY_LABEL;
3363 #endif
3364 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3365 sizeof(server->attr_bitmask));
3366 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3368 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3369 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3370 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3371 server->cache_consistency_bitmask[2] = 0;
3372 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3373 sizeof(server->exclcreat_bitmask));
3374 server->acl_bitmask = res.acl_bitmask;
3375 server->fh_expire_type = res.fh_expire_type;
3378 return status;
3381 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3383 struct nfs4_exception exception = { };
3384 int err;
3385 do {
3386 err = nfs4_handle_exception(server,
3387 _nfs4_server_capabilities(server, fhandle),
3388 &exception);
3389 } while (exception.retry);
3390 return err;
3393 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3394 struct nfs_fsinfo *info)
3396 u32 bitmask[3];
3397 struct nfs4_lookup_root_arg args = {
3398 .bitmask = bitmask,
3400 struct nfs4_lookup_res res = {
3401 .server = server,
3402 .fattr = info->fattr,
3403 .fh = fhandle,
3405 struct rpc_message msg = {
3406 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3407 .rpc_argp = &args,
3408 .rpc_resp = &res,
3411 bitmask[0] = nfs4_fattr_bitmap[0];
3412 bitmask[1] = nfs4_fattr_bitmap[1];
3414 * Process the label in the upcoming getfattr
3416 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3418 nfs_fattr_init(info->fattr);
3419 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3422 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3423 struct nfs_fsinfo *info)
3425 struct nfs4_exception exception = { };
3426 int err;
3427 do {
3428 err = _nfs4_lookup_root(server, fhandle, info);
3429 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3430 switch (err) {
3431 case 0:
3432 case -NFS4ERR_WRONGSEC:
3433 goto out;
3434 default:
3435 err = nfs4_handle_exception(server, err, &exception);
3437 } while (exception.retry);
3438 out:
3439 return err;
3442 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3443 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3445 struct rpc_auth_create_args auth_args = {
3446 .pseudoflavor = flavor,
3448 struct rpc_auth *auth;
3449 int ret;
3451 auth = rpcauth_create(&auth_args, server->client);
3452 if (IS_ERR(auth)) {
3453 ret = -EACCES;
3454 goto out;
3456 ret = nfs4_lookup_root(server, fhandle, info);
3457 out:
3458 return ret;
3462 * Retry pseudoroot lookup with various security flavors. We do this when:
3464 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3465 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3467 * Returns zero on success, or a negative NFS4ERR value, or a
3468 * negative errno value.
3470 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3471 struct nfs_fsinfo *info)
3473 /* Per 3530bis 15.33.5 */
3474 static const rpc_authflavor_t flav_array[] = {
3475 RPC_AUTH_GSS_KRB5P,
3476 RPC_AUTH_GSS_KRB5I,
3477 RPC_AUTH_GSS_KRB5,
3478 RPC_AUTH_UNIX, /* courtesy */
3479 RPC_AUTH_NULL,
3481 int status = -EPERM;
3482 size_t i;
3484 if (server->auth_info.flavor_len > 0) {
3485 /* try each flavor specified by user */
3486 for (i = 0; i < server->auth_info.flavor_len; i++) {
3487 status = nfs4_lookup_root_sec(server, fhandle, info,
3488 server->auth_info.flavors[i]);
3489 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3490 continue;
3491 break;
3493 } else {
3494 /* no flavors specified by user, try default list */
3495 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3496 status = nfs4_lookup_root_sec(server, fhandle, info,
3497 flav_array[i]);
3498 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3499 continue;
3500 break;
3505 * -EACCESS could mean that the user doesn't have correct permissions
3506 * to access the mount. It could also mean that we tried to mount
3507 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3508 * existing mount programs don't handle -EACCES very well so it should
3509 * be mapped to -EPERM instead.
3511 if (status == -EACCES)
3512 status = -EPERM;
3513 return status;
3517 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3518 * @server: initialized nfs_server handle
3519 * @fhandle: we fill in the pseudo-fs root file handle
3520 * @info: we fill in an FSINFO struct
3521 * @auth_probe: probe the auth flavours
3523 * Returns zero on success, or a negative errno.
3525 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3526 struct nfs_fsinfo *info,
3527 bool auth_probe)
3529 int status = 0;
3531 if (!auth_probe)
3532 status = nfs4_lookup_root(server, fhandle, info);
3534 if (auth_probe || status == NFS4ERR_WRONGSEC)
3535 status = server->nfs_client->cl_mvops->find_root_sec(server,
3536 fhandle, info);
3538 if (status == 0)
3539 status = nfs4_server_capabilities(server, fhandle);
3540 if (status == 0)
3541 status = nfs4_do_fsinfo(server, fhandle, info);
3543 return nfs4_map_errors(status);
3546 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3547 struct nfs_fsinfo *info)
3549 int error;
3550 struct nfs_fattr *fattr = info->fattr;
3551 struct nfs4_label *label = NULL;
3553 error = nfs4_server_capabilities(server, mntfh);
3554 if (error < 0) {
3555 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3556 return error;
3559 label = nfs4_label_alloc(server, GFP_KERNEL);
3560 if (IS_ERR(label))
3561 return PTR_ERR(label);
3563 error = nfs4_proc_getattr(server, mntfh, fattr, label);
3564 if (error < 0) {
3565 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3566 goto err_free_label;
3569 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3570 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3571 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3573 err_free_label:
3574 nfs4_label_free(label);
3576 return error;
3580 * Get locations and (maybe) other attributes of a referral.
3581 * Note that we'll actually follow the referral later when
3582 * we detect fsid mismatch in inode revalidation
3584 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3585 const struct qstr *name, struct nfs_fattr *fattr,
3586 struct nfs_fh *fhandle)
3588 int status = -ENOMEM;
3589 struct page *page = NULL;
3590 struct nfs4_fs_locations *locations = NULL;
3592 page = alloc_page(GFP_KERNEL);
3593 if (page == NULL)
3594 goto out;
3595 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3596 if (locations == NULL)
3597 goto out;
3599 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3600 if (status != 0)
3601 goto out;
3604 * If the fsid didn't change, this is a migration event, not a
3605 * referral. Cause us to drop into the exception handler, which
3606 * will kick off migration recovery.
3608 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3609 dprintk("%s: server did not return a different fsid for"
3610 " a referral at %s\n", __func__, name->name);
3611 status = -NFS4ERR_MOVED;
3612 goto out;
3614 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3615 nfs_fixup_referral_attributes(&locations->fattr);
3617 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3618 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3619 memset(fhandle, 0, sizeof(struct nfs_fh));
3620 out:
3621 if (page)
3622 __free_page(page);
3623 kfree(locations);
3624 return status;
3627 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3628 struct nfs_fattr *fattr, struct nfs4_label *label)
3630 struct nfs4_getattr_arg args = {
3631 .fh = fhandle,
3632 .bitmask = server->attr_bitmask,
3634 struct nfs4_getattr_res res = {
3635 .fattr = fattr,
3636 .label = label,
3637 .server = server,
3639 struct rpc_message msg = {
3640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3641 .rpc_argp = &args,
3642 .rpc_resp = &res,
3645 args.bitmask = nfs4_bitmask(server, label);
3647 nfs_fattr_init(fattr);
3648 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3651 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3652 struct nfs_fattr *fattr, struct nfs4_label *label)
3654 struct nfs4_exception exception = { };
3655 int err;
3656 do {
3657 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3658 trace_nfs4_getattr(server, fhandle, fattr, err);
3659 err = nfs4_handle_exception(server, err,
3660 &exception);
3661 } while (exception.retry);
3662 return err;
3666 * The file is not closed if it is opened due to the a request to change
3667 * the size of the file. The open call will not be needed once the
3668 * VFS layer lookup-intents are implemented.
3670 * Close is called when the inode is destroyed.
3671 * If we haven't opened the file for O_WRONLY, we
3672 * need to in the size_change case to obtain a stateid.
3674 * Got race?
3675 * Because OPEN is always done by name in nfsv4, it is
3676 * possible that we opened a different file by the same
3677 * name. We can recognize this race condition, but we
3678 * can't do anything about it besides returning an error.
3680 * This will be fixed with VFS changes (lookup-intent).
3682 static int
3683 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3684 struct iattr *sattr)
3686 struct inode *inode = d_inode(dentry);
3687 struct rpc_cred *cred = NULL;
3688 struct nfs4_state *state = NULL;
3689 struct nfs4_label *label = NULL;
3690 int status;
3692 if (pnfs_ld_layoutret_on_setattr(inode) &&
3693 sattr->ia_valid & ATTR_SIZE &&
3694 sattr->ia_size < i_size_read(inode))
3695 pnfs_commit_and_return_layout(inode);
3697 nfs_fattr_init(fattr);
3699 /* Deal with open(O_TRUNC) */
3700 if (sattr->ia_valid & ATTR_OPEN)
3701 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3703 /* Optimization: if the end result is no change, don't RPC */
3704 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3705 return 0;
3707 /* Search for an existing open(O_WRITE) file */
3708 if (sattr->ia_valid & ATTR_FILE) {
3709 struct nfs_open_context *ctx;
3711 ctx = nfs_file_open_context(sattr->ia_file);
3712 if (ctx) {
3713 cred = ctx->cred;
3714 state = ctx->state;
3718 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3719 if (IS_ERR(label))
3720 return PTR_ERR(label);
3722 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
3723 if (status == 0) {
3724 nfs_setattr_update_inode(inode, sattr, fattr);
3725 nfs_setsecurity(inode, fattr, label);
3727 nfs4_label_free(label);
3728 return status;
3731 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3732 const struct qstr *name, struct nfs_fh *fhandle,
3733 struct nfs_fattr *fattr, struct nfs4_label *label)
3735 struct nfs_server *server = NFS_SERVER(dir);
3736 int status;
3737 struct nfs4_lookup_arg args = {
3738 .bitmask = server->attr_bitmask,
3739 .dir_fh = NFS_FH(dir),
3740 .name = name,
3742 struct nfs4_lookup_res res = {
3743 .server = server,
3744 .fattr = fattr,
3745 .label = label,
3746 .fh = fhandle,
3748 struct rpc_message msg = {
3749 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3750 .rpc_argp = &args,
3751 .rpc_resp = &res,
3754 args.bitmask = nfs4_bitmask(server, label);
3756 nfs_fattr_init(fattr);
3758 dprintk("NFS call lookup %s\n", name->name);
3759 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
3760 dprintk("NFS reply lookup: %d\n", status);
3761 return status;
3764 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
3766 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
3767 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
3768 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3769 fattr->nlink = 2;
3772 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3773 const struct qstr *name, struct nfs_fh *fhandle,
3774 struct nfs_fattr *fattr, struct nfs4_label *label)
3776 struct nfs4_exception exception = { };
3777 struct rpc_clnt *client = *clnt;
3778 int err;
3779 do {
3780 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
3781 trace_nfs4_lookup(dir, name, err);
3782 switch (err) {
3783 case -NFS4ERR_BADNAME:
3784 err = -ENOENT;
3785 goto out;
3786 case -NFS4ERR_MOVED:
3787 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
3788 if (err == -NFS4ERR_MOVED)
3789 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3790 goto out;
3791 case -NFS4ERR_WRONGSEC:
3792 err = -EPERM;
3793 if (client != *clnt)
3794 goto out;
3795 client = nfs4_negotiate_security(client, dir, name);
3796 if (IS_ERR(client))
3797 return PTR_ERR(client);
3799 exception.retry = 1;
3800 break;
3801 default:
3802 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3804 } while (exception.retry);
3806 out:
3807 if (err == 0)
3808 *clnt = client;
3809 else if (client != *clnt)
3810 rpc_shutdown_client(client);
3812 return err;
3815 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
3816 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3817 struct nfs4_label *label)
3819 int status;
3820 struct rpc_clnt *client = NFS_CLIENT(dir);
3822 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
3823 if (client != NFS_CLIENT(dir)) {
3824 rpc_shutdown_client(client);
3825 nfs_fixup_secinfo_attributes(fattr);
3827 return status;
3830 struct rpc_clnt *
3831 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
3832 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3834 struct rpc_clnt *client = NFS_CLIENT(dir);
3835 int status;
3837 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
3838 if (status < 0)
3839 return ERR_PTR(status);
3840 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
3843 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3845 struct nfs_server *server = NFS_SERVER(inode);
3846 struct nfs4_accessargs args = {
3847 .fh = NFS_FH(inode),
3848 .bitmask = server->cache_consistency_bitmask,
3850 struct nfs4_accessres res = {
3851 .server = server,
3853 struct rpc_message msg = {
3854 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3855 .rpc_argp = &args,
3856 .rpc_resp = &res,
3857 .rpc_cred = entry->cred,
3859 int mode = entry->mask;
3860 int status = 0;
3863 * Determine which access bits we want to ask for...
3865 if (mode & MAY_READ)
3866 args.access |= NFS4_ACCESS_READ;
3867 if (S_ISDIR(inode->i_mode)) {
3868 if (mode & MAY_WRITE)
3869 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3870 if (mode & MAY_EXEC)
3871 args.access |= NFS4_ACCESS_LOOKUP;
3872 } else {
3873 if (mode & MAY_WRITE)
3874 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3875 if (mode & MAY_EXEC)
3876 args.access |= NFS4_ACCESS_EXECUTE;
3879 res.fattr = nfs_alloc_fattr();
3880 if (res.fattr == NULL)
3881 return -ENOMEM;
3883 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3884 if (!status) {
3885 nfs_access_set_mask(entry, res.access);
3886 nfs_refresh_inode(inode, res.fattr);
3888 nfs_free_fattr(res.fattr);
3889 return status;
3892 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3894 struct nfs4_exception exception = { };
3895 int err;
3896 do {
3897 err = _nfs4_proc_access(inode, entry);
3898 trace_nfs4_access(inode, err);
3899 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3900 &exception);
3901 } while (exception.retry);
3902 return err;
3906 * TODO: For the time being, we don't try to get any attributes
3907 * along with any of the zero-copy operations READ, READDIR,
3908 * READLINK, WRITE.
3910 * In the case of the first three, we want to put the GETATTR
3911 * after the read-type operation -- this is because it is hard
3912 * to predict the length of a GETATTR response in v4, and thus
3913 * align the READ data correctly. This means that the GETATTR
3914 * may end up partially falling into the page cache, and we should
3915 * shift it into the 'tail' of the xdr_buf before processing.
3916 * To do this efficiently, we need to know the total length
3917 * of data received, which doesn't seem to be available outside
3918 * of the RPC layer.
3920 * In the case of WRITE, we also want to put the GETATTR after
3921 * the operation -- in this case because we want to make sure
3922 * we get the post-operation mtime and size.
3924 * Both of these changes to the XDR layer would in fact be quite
3925 * minor, but I decided to leave them for a subsequent patch.
3927 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3928 unsigned int pgbase, unsigned int pglen)
3930 struct nfs4_readlink args = {
3931 .fh = NFS_FH(inode),
3932 .pgbase = pgbase,
3933 .pglen = pglen,
3934 .pages = &page,
3936 struct nfs4_readlink_res res;
3937 struct rpc_message msg = {
3938 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3939 .rpc_argp = &args,
3940 .rpc_resp = &res,
3943 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
3946 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3947 unsigned int pgbase, unsigned int pglen)
3949 struct nfs4_exception exception = { };
3950 int err;
3951 do {
3952 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3953 trace_nfs4_readlink(inode, err);
3954 err = nfs4_handle_exception(NFS_SERVER(inode), err,
3955 &exception);
3956 } while (exception.retry);
3957 return err;
3961 * This is just for mknod. open(O_CREAT) will always do ->open_context().
3963 static int
3964 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
3965 int flags)
3967 struct nfs4_label l, *ilabel = NULL;
3968 struct nfs_open_context *ctx;
3969 struct nfs4_state *state;
3970 int status = 0;
3972 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3973 if (IS_ERR(ctx))
3974 return PTR_ERR(ctx);
3976 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3978 sattr->ia_mode &= ~current_umask();
3979 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
3980 if (IS_ERR(state)) {
3981 status = PTR_ERR(state);
3982 goto out;
3984 out:
3985 nfs4_label_release_security(ilabel);
3986 put_nfs_open_context(ctx);
3987 return status;
3990 static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
3992 struct nfs_server *server = NFS_SERVER(dir);
3993 struct nfs_removeargs args = {
3994 .fh = NFS_FH(dir),
3995 .name = *name,
3997 struct nfs_removeres res = {
3998 .server = server,
4000 struct rpc_message msg = {
4001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4002 .rpc_argp = &args,
4003 .rpc_resp = &res,
4005 int status;
4007 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4008 if (status == 0)
4009 update_changeattr(dir, &res.cinfo);
4010 return status;
4013 static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
4015 struct nfs4_exception exception = { };
4016 int err;
4017 do {
4018 err = _nfs4_proc_remove(dir, name);
4019 trace_nfs4_remove(dir, name, err);
4020 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4021 &exception);
4022 } while (exception.retry);
4023 return err;
4026 static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
4028 struct nfs_server *server = NFS_SERVER(dir);
4029 struct nfs_removeargs *args = msg->rpc_argp;
4030 struct nfs_removeres *res = msg->rpc_resp;
4032 res->server = server;
4033 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4034 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
4036 nfs_fattr_init(res->dir_attr);
4039 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4041 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
4042 &data->args.seq_args,
4043 &data->res.seq_res,
4044 task);
4047 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4049 struct nfs_unlinkdata *data = task->tk_calldata;
4050 struct nfs_removeres *res = &data->res;
4052 if (!nfs4_sequence_done(task, &res->seq_res))
4053 return 0;
4054 if (nfs4_async_handle_error(task, res->server, NULL,
4055 &data->timeout) == -EAGAIN)
4056 return 0;
4057 update_changeattr(dir, &res->cinfo);
4058 return 1;
4061 static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4063 struct nfs_server *server = NFS_SERVER(dir);
4064 struct nfs_renameargs *arg = msg->rpc_argp;
4065 struct nfs_renameres *res = msg->rpc_resp;
4067 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4068 res->server = server;
4069 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
4072 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4074 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4075 &data->args.seq_args,
4076 &data->res.seq_res,
4077 task);
4080 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4081 struct inode *new_dir)
4083 struct nfs_renamedata *data = task->tk_calldata;
4084 struct nfs_renameres *res = &data->res;
4086 if (!nfs4_sequence_done(task, &res->seq_res))
4087 return 0;
4088 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4089 return 0;
4091 update_changeattr(old_dir, &res->old_cinfo);
4092 update_changeattr(new_dir, &res->new_cinfo);
4093 return 1;
4096 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4098 struct nfs_server *server = NFS_SERVER(inode);
4099 struct nfs4_link_arg arg = {
4100 .fh = NFS_FH(inode),
4101 .dir_fh = NFS_FH(dir),
4102 .name = name,
4103 .bitmask = server->attr_bitmask,
4105 struct nfs4_link_res res = {
4106 .server = server,
4107 .label = NULL,
4109 struct rpc_message msg = {
4110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4111 .rpc_argp = &arg,
4112 .rpc_resp = &res,
4114 int status = -ENOMEM;
4116 res.fattr = nfs_alloc_fattr();
4117 if (res.fattr == NULL)
4118 goto out;
4120 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4121 if (IS_ERR(res.label)) {
4122 status = PTR_ERR(res.label);
4123 goto out;
4125 arg.bitmask = nfs4_bitmask(server, res.label);
4127 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4128 if (!status) {
4129 update_changeattr(dir, &res.cinfo);
4130 status = nfs_post_op_update_inode(inode, res.fattr);
4131 if (!status)
4132 nfs_setsecurity(inode, res.fattr, res.label);
4136 nfs4_label_free(res.label);
4138 out:
4139 nfs_free_fattr(res.fattr);
4140 return status;
4143 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4145 struct nfs4_exception exception = { };
4146 int err;
4147 do {
4148 err = nfs4_handle_exception(NFS_SERVER(inode),
4149 _nfs4_proc_link(inode, dir, name),
4150 &exception);
4151 } while (exception.retry);
4152 return err;
4155 struct nfs4_createdata {
4156 struct rpc_message msg;
4157 struct nfs4_create_arg arg;
4158 struct nfs4_create_res res;
4159 struct nfs_fh fh;
4160 struct nfs_fattr fattr;
4161 struct nfs4_label *label;
4164 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4165 const struct qstr *name, struct iattr *sattr, u32 ftype)
4167 struct nfs4_createdata *data;
4169 data = kzalloc(sizeof(*data), GFP_KERNEL);
4170 if (data != NULL) {
4171 struct nfs_server *server = NFS_SERVER(dir);
4173 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4174 if (IS_ERR(data->label))
4175 goto out_free;
4177 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4178 data->msg.rpc_argp = &data->arg;
4179 data->msg.rpc_resp = &data->res;
4180 data->arg.dir_fh = NFS_FH(dir);
4181 data->arg.server = server;
4182 data->arg.name = name;
4183 data->arg.attrs = sattr;
4184 data->arg.ftype = ftype;
4185 data->arg.bitmask = nfs4_bitmask(server, data->label);
4186 data->res.server = server;
4187 data->res.fh = &data->fh;
4188 data->res.fattr = &data->fattr;
4189 data->res.label = data->label;
4190 nfs_fattr_init(data->res.fattr);
4192 return data;
4193 out_free:
4194 kfree(data);
4195 return NULL;
4198 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4200 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4201 &data->arg.seq_args, &data->res.seq_res, 1);
4202 if (status == 0) {
4203 update_changeattr(dir, &data->res.dir_cinfo);
4204 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4206 return status;
4209 static void nfs4_free_createdata(struct nfs4_createdata *data)
4211 nfs4_label_free(data->label);
4212 kfree(data);
4215 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4216 struct page *page, unsigned int len, struct iattr *sattr,
4217 struct nfs4_label *label)
4219 struct nfs4_createdata *data;
4220 int status = -ENAMETOOLONG;
4222 if (len > NFS4_MAXPATHLEN)
4223 goto out;
4225 status = -ENOMEM;
4226 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4227 if (data == NULL)
4228 goto out;
4230 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4231 data->arg.u.symlink.pages = &page;
4232 data->arg.u.symlink.len = len;
4233 data->arg.label = label;
4235 status = nfs4_do_create(dir, dentry, data);
4237 nfs4_free_createdata(data);
4238 out:
4239 return status;
4242 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4243 struct page *page, unsigned int len, struct iattr *sattr)
4245 struct nfs4_exception exception = { };
4246 struct nfs4_label l, *label = NULL;
4247 int err;
4249 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4251 do {
4252 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4253 trace_nfs4_symlink(dir, &dentry->d_name, err);
4254 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4255 &exception);
4256 } while (exception.retry);
4258 nfs4_label_release_security(label);
4259 return err;
4262 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4263 struct iattr *sattr, struct nfs4_label *label)
4265 struct nfs4_createdata *data;
4266 int status = -ENOMEM;
4268 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4269 if (data == NULL)
4270 goto out;
4272 data->arg.label = label;
4273 status = nfs4_do_create(dir, dentry, data);
4275 nfs4_free_createdata(data);
4276 out:
4277 return status;
4280 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4281 struct iattr *sattr)
4283 struct nfs4_exception exception = { };
4284 struct nfs4_label l, *label = NULL;
4285 int err;
4287 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4289 sattr->ia_mode &= ~current_umask();
4290 do {
4291 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4292 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4293 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4294 &exception);
4295 } while (exception.retry);
4296 nfs4_label_release_security(label);
4298 return err;
4301 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4302 u64 cookie, struct page **pages, unsigned int count, int plus)
4304 struct inode *dir = d_inode(dentry);
4305 struct nfs4_readdir_arg args = {
4306 .fh = NFS_FH(dir),
4307 .pages = pages,
4308 .pgbase = 0,
4309 .count = count,
4310 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4311 .plus = plus,
4313 struct nfs4_readdir_res res;
4314 struct rpc_message msg = {
4315 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4316 .rpc_argp = &args,
4317 .rpc_resp = &res,
4318 .rpc_cred = cred,
4320 int status;
4322 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4323 dentry,
4324 (unsigned long long)cookie);
4325 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4326 res.pgbase = args.pgbase;
4327 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4328 if (status >= 0) {
4329 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4330 status += args.pgbase;
4333 nfs_invalidate_atime(dir);
4335 dprintk("%s: returns %d\n", __func__, status);
4336 return status;
4339 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4340 u64 cookie, struct page **pages, unsigned int count, int plus)
4342 struct nfs4_exception exception = { };
4343 int err;
4344 do {
4345 err = _nfs4_proc_readdir(dentry, cred, cookie,
4346 pages, count, plus);
4347 trace_nfs4_readdir(d_inode(dentry), err);
4348 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4349 &exception);
4350 } while (exception.retry);
4351 return err;
4354 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4355 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4357 struct nfs4_createdata *data;
4358 int mode = sattr->ia_mode;
4359 int status = -ENOMEM;
4361 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4362 if (data == NULL)
4363 goto out;
4365 if (S_ISFIFO(mode))
4366 data->arg.ftype = NF4FIFO;
4367 else if (S_ISBLK(mode)) {
4368 data->arg.ftype = NF4BLK;
4369 data->arg.u.device.specdata1 = MAJOR(rdev);
4370 data->arg.u.device.specdata2 = MINOR(rdev);
4372 else if (S_ISCHR(mode)) {
4373 data->arg.ftype = NF4CHR;
4374 data->arg.u.device.specdata1 = MAJOR(rdev);
4375 data->arg.u.device.specdata2 = MINOR(rdev);
4376 } else if (!S_ISSOCK(mode)) {
4377 status = -EINVAL;
4378 goto out_free;
4381 data->arg.label = label;
4382 status = nfs4_do_create(dir, dentry, data);
4383 out_free:
4384 nfs4_free_createdata(data);
4385 out:
4386 return status;
4389 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4390 struct iattr *sattr, dev_t rdev)
4392 struct nfs4_exception exception = { };
4393 struct nfs4_label l, *label = NULL;
4394 int err;
4396 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4398 sattr->ia_mode &= ~current_umask();
4399 do {
4400 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4401 trace_nfs4_mknod(dir, &dentry->d_name, err);
4402 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4403 &exception);
4404 } while (exception.retry);
4406 nfs4_label_release_security(label);
4408 return err;
4411 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4412 struct nfs_fsstat *fsstat)
4414 struct nfs4_statfs_arg args = {
4415 .fh = fhandle,
4416 .bitmask = server->attr_bitmask,
4418 struct nfs4_statfs_res res = {
4419 .fsstat = fsstat,
4421 struct rpc_message msg = {
4422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4423 .rpc_argp = &args,
4424 .rpc_resp = &res,
4427 nfs_fattr_init(fsstat->fattr);
4428 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4431 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4433 struct nfs4_exception exception = { };
4434 int err;
4435 do {
4436 err = nfs4_handle_exception(server,
4437 _nfs4_proc_statfs(server, fhandle, fsstat),
4438 &exception);
4439 } while (exception.retry);
4440 return err;
4443 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4444 struct nfs_fsinfo *fsinfo)
4446 struct nfs4_fsinfo_arg args = {
4447 .fh = fhandle,
4448 .bitmask = server->attr_bitmask,
4450 struct nfs4_fsinfo_res res = {
4451 .fsinfo = fsinfo,
4453 struct rpc_message msg = {
4454 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4455 .rpc_argp = &args,
4456 .rpc_resp = &res,
4459 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4462 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4464 struct nfs4_exception exception = { };
4465 unsigned long now = jiffies;
4466 int err;
4468 do {
4469 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4470 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4471 if (err == 0) {
4472 nfs4_set_lease_period(server->nfs_client,
4473 fsinfo->lease_time * HZ,
4474 now);
4475 break;
4477 err = nfs4_handle_exception(server, err, &exception);
4478 } while (exception.retry);
4479 return err;
4482 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4484 int error;
4486 nfs_fattr_init(fsinfo->fattr);
4487 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4488 if (error == 0) {
4489 /* block layout checks this! */
4490 server->pnfs_blksize = fsinfo->blksize;
4491 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4494 return error;
4497 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4498 struct nfs_pathconf *pathconf)
4500 struct nfs4_pathconf_arg args = {
4501 .fh = fhandle,
4502 .bitmask = server->attr_bitmask,
4504 struct nfs4_pathconf_res res = {
4505 .pathconf = pathconf,
4507 struct rpc_message msg = {
4508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4509 .rpc_argp = &args,
4510 .rpc_resp = &res,
4513 /* None of the pathconf attributes are mandatory to implement */
4514 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4515 memset(pathconf, 0, sizeof(*pathconf));
4516 return 0;
4519 nfs_fattr_init(pathconf->fattr);
4520 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4523 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4524 struct nfs_pathconf *pathconf)
4526 struct nfs4_exception exception = { };
4527 int err;
4529 do {
4530 err = nfs4_handle_exception(server,
4531 _nfs4_proc_pathconf(server, fhandle, pathconf),
4532 &exception);
4533 } while (exception.retry);
4534 return err;
4537 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4538 const struct nfs_open_context *ctx,
4539 const struct nfs_lock_context *l_ctx,
4540 fmode_t fmode)
4542 const struct nfs_lockowner *lockowner = NULL;
4544 if (l_ctx != NULL)
4545 lockowner = &l_ctx->lockowner;
4546 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
4548 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4550 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4551 const struct nfs_open_context *ctx,
4552 const struct nfs_lock_context *l_ctx,
4553 fmode_t fmode)
4555 nfs4_stateid current_stateid;
4557 /* If the current stateid represents a lost lock, then exit */
4558 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4559 return true;
4560 return nfs4_stateid_match(stateid, &current_stateid);
4563 static bool nfs4_error_stateid_expired(int err)
4565 switch (err) {
4566 case -NFS4ERR_DELEG_REVOKED:
4567 case -NFS4ERR_ADMIN_REVOKED:
4568 case -NFS4ERR_BAD_STATEID:
4569 case -NFS4ERR_STALE_STATEID:
4570 case -NFS4ERR_OLD_STATEID:
4571 case -NFS4ERR_OPENMODE:
4572 case -NFS4ERR_EXPIRED:
4573 return true;
4575 return false;
4578 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4580 struct nfs_server *server = NFS_SERVER(hdr->inode);
4582 trace_nfs4_read(hdr, task->tk_status);
4583 if (task->tk_status < 0) {
4584 struct nfs4_exception exception = {
4585 .inode = hdr->inode,
4586 .state = hdr->args.context->state,
4587 .stateid = &hdr->args.stateid,
4589 task->tk_status = nfs4_async_handle_exception(task,
4590 server, task->tk_status, &exception);
4591 if (exception.retry) {
4592 rpc_restart_call_prepare(task);
4593 return -EAGAIN;
4597 if (task->tk_status > 0)
4598 renew_lease(server, hdr->timestamp);
4599 return 0;
4602 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4603 struct nfs_pgio_args *args)
4606 if (!nfs4_error_stateid_expired(task->tk_status) ||
4607 nfs4_stateid_is_current(&args->stateid,
4608 args->context,
4609 args->lock_context,
4610 FMODE_READ))
4611 return false;
4612 rpc_restart_call_prepare(task);
4613 return true;
4616 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4619 dprintk("--> %s\n", __func__);
4621 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4622 return -EAGAIN;
4623 if (nfs4_read_stateid_changed(task, &hdr->args))
4624 return -EAGAIN;
4625 if (task->tk_status > 0)
4626 nfs_invalidate_atime(hdr->inode);
4627 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4628 nfs4_read_done_cb(task, hdr);
4631 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4632 struct rpc_message *msg)
4634 hdr->timestamp = jiffies;
4635 if (!hdr->pgio_done_cb)
4636 hdr->pgio_done_cb = nfs4_read_done_cb;
4637 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
4638 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
4641 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4642 struct nfs_pgio_header *hdr)
4644 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4645 &hdr->args.seq_args,
4646 &hdr->res.seq_res,
4647 task))
4648 return 0;
4649 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4650 hdr->args.lock_context,
4651 hdr->rw_ops->rw_mode) == -EIO)
4652 return -EIO;
4653 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
4654 return -EIO;
4655 return 0;
4658 static int nfs4_write_done_cb(struct rpc_task *task,
4659 struct nfs_pgio_header *hdr)
4661 struct inode *inode = hdr->inode;
4663 trace_nfs4_write(hdr, task->tk_status);
4664 if (task->tk_status < 0) {
4665 struct nfs4_exception exception = {
4666 .inode = hdr->inode,
4667 .state = hdr->args.context->state,
4668 .stateid = &hdr->args.stateid,
4670 task->tk_status = nfs4_async_handle_exception(task,
4671 NFS_SERVER(inode), task->tk_status,
4672 &exception);
4673 if (exception.retry) {
4674 rpc_restart_call_prepare(task);
4675 return -EAGAIN;
4678 if (task->tk_status >= 0) {
4679 renew_lease(NFS_SERVER(inode), hdr->timestamp);
4680 nfs_writeback_update_inode(hdr);
4682 return 0;
4685 static bool nfs4_write_stateid_changed(struct rpc_task *task,
4686 struct nfs_pgio_args *args)
4689 if (!nfs4_error_stateid_expired(task->tk_status) ||
4690 nfs4_stateid_is_current(&args->stateid,
4691 args->context,
4692 args->lock_context,
4693 FMODE_WRITE))
4694 return false;
4695 rpc_restart_call_prepare(task);
4696 return true;
4699 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
4701 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
4702 return -EAGAIN;
4703 if (nfs4_write_stateid_changed(task, &hdr->args))
4704 return -EAGAIN;
4705 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4706 nfs4_write_done_cb(task, hdr);
4709 static
4710 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
4712 /* Don't request attributes for pNFS or O_DIRECT writes */
4713 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
4714 return false;
4715 /* Otherwise, request attributes if and only if we don't hold
4716 * a delegation
4718 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
4721 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4722 struct rpc_message *msg)
4724 struct nfs_server *server = NFS_SERVER(hdr->inode);
4726 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4727 hdr->args.bitmask = NULL;
4728 hdr->res.fattr = NULL;
4729 } else
4730 hdr->args.bitmask = server->cache_consistency_bitmask;
4732 if (!hdr->pgio_done_cb)
4733 hdr->pgio_done_cb = nfs4_write_done_cb;
4734 hdr->res.server = server;
4735 hdr->timestamp = jiffies;
4737 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
4738 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
4741 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4743 nfs4_setup_sequence(NFS_SERVER(data->inode),
4744 &data->args.seq_args,
4745 &data->res.seq_res,
4746 task);
4749 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
4751 struct inode *inode = data->inode;
4753 trace_nfs4_commit(data, task->tk_status);
4754 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4755 NULL, NULL) == -EAGAIN) {
4756 rpc_restart_call_prepare(task);
4757 return -EAGAIN;
4759 return 0;
4762 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
4764 if (!nfs4_sequence_done(task, &data->res.seq_res))
4765 return -EAGAIN;
4766 return data->commit_done_cb(task, data);
4769 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
4771 struct nfs_server *server = NFS_SERVER(data->inode);
4773 if (data->commit_done_cb == NULL)
4774 data->commit_done_cb = nfs4_commit_done_cb;
4775 data->res.server = server;
4776 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
4777 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
4780 struct nfs4_renewdata {
4781 struct nfs_client *client;
4782 unsigned long timestamp;
4786 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4787 * standalone procedure for queueing an asynchronous RENEW.
4789 static void nfs4_renew_release(void *calldata)
4791 struct nfs4_renewdata *data = calldata;
4792 struct nfs_client *clp = data->client;
4794 if (atomic_read(&clp->cl_count) > 1)
4795 nfs4_schedule_state_renewal(clp);
4796 nfs_put_client(clp);
4797 kfree(data);
4800 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
4802 struct nfs4_renewdata *data = calldata;
4803 struct nfs_client *clp = data->client;
4804 unsigned long timestamp = data->timestamp;
4806 trace_nfs4_renew_async(clp, task->tk_status);
4807 switch (task->tk_status) {
4808 case 0:
4809 break;
4810 case -NFS4ERR_LEASE_MOVED:
4811 nfs4_schedule_lease_moved_recovery(clp);
4812 break;
4813 default:
4814 /* Unless we're shutting down, schedule state recovery! */
4815 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4816 return;
4817 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
4818 nfs4_schedule_lease_recovery(clp);
4819 return;
4821 nfs4_schedule_path_down_recovery(clp);
4823 do_renew_lease(clp, timestamp);
4826 static const struct rpc_call_ops nfs4_renew_ops = {
4827 .rpc_call_done = nfs4_renew_done,
4828 .rpc_release = nfs4_renew_release,
4831 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
4833 struct rpc_message msg = {
4834 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4835 .rpc_argp = clp,
4836 .rpc_cred = cred,
4838 struct nfs4_renewdata *data;
4840 if (renew_flags == 0)
4841 return 0;
4842 if (!atomic_inc_not_zero(&clp->cl_count))
4843 return -EIO;
4844 data = kmalloc(sizeof(*data), GFP_NOFS);
4845 if (data == NULL)
4846 return -ENOMEM;
4847 data->client = clp;
4848 data->timestamp = jiffies;
4849 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
4850 &nfs4_renew_ops, data);
4853 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
4855 struct rpc_message msg = {
4856 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4857 .rpc_argp = clp,
4858 .rpc_cred = cred,
4860 unsigned long now = jiffies;
4861 int status;
4863 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4864 if (status < 0)
4865 return status;
4866 do_renew_lease(clp, now);
4867 return 0;
4870 static inline int nfs4_server_supports_acls(struct nfs_server *server)
4872 return server->caps & NFS_CAP_ACLS;
4875 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4876 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
4877 * the stack.
4879 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
4881 static int buf_to_pages_noslab(const void *buf, size_t buflen,
4882 struct page **pages)
4884 struct page *newpage, **spages;
4885 int rc = 0;
4886 size_t len;
4887 spages = pages;
4889 do {
4890 len = min_t(size_t, PAGE_SIZE, buflen);
4891 newpage = alloc_page(GFP_KERNEL);
4893 if (newpage == NULL)
4894 goto unwind;
4895 memcpy(page_address(newpage), buf, len);
4896 buf += len;
4897 buflen -= len;
4898 *pages++ = newpage;
4899 rc++;
4900 } while (buflen != 0);
4902 return rc;
4904 unwind:
4905 for(; rc > 0; rc--)
4906 __free_page(spages[rc-1]);
4907 return -ENOMEM;
4910 struct nfs4_cached_acl {
4911 int cached;
4912 size_t len;
4913 char data[0];
4916 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
4918 struct nfs_inode *nfsi = NFS_I(inode);
4920 spin_lock(&inode->i_lock);
4921 kfree(nfsi->nfs4_acl);
4922 nfsi->nfs4_acl = acl;
4923 spin_unlock(&inode->i_lock);
4926 static void nfs4_zap_acl_attr(struct inode *inode)
4928 nfs4_set_cached_acl(inode, NULL);
4931 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4933 struct nfs_inode *nfsi = NFS_I(inode);
4934 struct nfs4_cached_acl *acl;
4935 int ret = -ENOENT;
4937 spin_lock(&inode->i_lock);
4938 acl = nfsi->nfs4_acl;
4939 if (acl == NULL)
4940 goto out;
4941 if (buf == NULL) /* user is just asking for length */
4942 goto out_len;
4943 if (acl->cached == 0)
4944 goto out;
4945 ret = -ERANGE; /* see getxattr(2) man page */
4946 if (acl->len > buflen)
4947 goto out;
4948 memcpy(buf, acl->data, acl->len);
4949 out_len:
4950 ret = acl->len;
4951 out:
4952 spin_unlock(&inode->i_lock);
4953 return ret;
4956 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
4958 struct nfs4_cached_acl *acl;
4959 size_t buflen = sizeof(*acl) + acl_len;
4961 if (buflen <= PAGE_SIZE) {
4962 acl = kmalloc(buflen, GFP_KERNEL);
4963 if (acl == NULL)
4964 goto out;
4965 acl->cached = 1;
4966 _copy_from_pages(acl->data, pages, pgbase, acl_len);
4967 } else {
4968 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4969 if (acl == NULL)
4970 goto out;
4971 acl->cached = 0;
4973 acl->len = acl_len;
4974 out:
4975 nfs4_set_cached_acl(inode, acl);
4979 * The getxattr API returns the required buffer length when called with a
4980 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4981 * the required buf. On a NULL buf, we send a page of data to the server
4982 * guessing that the ACL request can be serviced by a page. If so, we cache
4983 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4984 * the cache. If not so, we throw away the page, and cache the required
4985 * length. The next getxattr call will then produce another round trip to
4986 * the server, this time with the input buf of the required size.
4988 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4990 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
4991 struct nfs_getaclargs args = {
4992 .fh = NFS_FH(inode),
4993 .acl_pages = pages,
4994 .acl_len = buflen,
4996 struct nfs_getaclres res = {
4997 .acl_len = buflen,
4999 struct rpc_message msg = {
5000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5001 .rpc_argp = &args,
5002 .rpc_resp = &res,
5004 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5005 int ret = -ENOMEM, i;
5007 if (npages > ARRAY_SIZE(pages))
5008 return -ERANGE;
5010 for (i = 0; i < npages; i++) {
5011 pages[i] = alloc_page(GFP_KERNEL);
5012 if (!pages[i])
5013 goto out_free;
5016 /* for decoding across pages */
5017 res.acl_scratch = alloc_page(GFP_KERNEL);
5018 if (!res.acl_scratch)
5019 goto out_free;
5021 args.acl_len = npages * PAGE_SIZE;
5023 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5024 __func__, buf, buflen, npages, args.acl_len);
5025 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5026 &msg, &args.seq_args, &res.seq_res, 0);
5027 if (ret)
5028 goto out_free;
5030 /* Handle the case where the passed-in buffer is too short */
5031 if (res.acl_flags & NFS4_ACL_TRUNC) {
5032 /* Did the user only issue a request for the acl length? */
5033 if (buf == NULL)
5034 goto out_ok;
5035 ret = -ERANGE;
5036 goto out_free;
5038 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5039 if (buf) {
5040 if (res.acl_len > buflen) {
5041 ret = -ERANGE;
5042 goto out_free;
5044 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5046 out_ok:
5047 ret = res.acl_len;
5048 out_free:
5049 for (i = 0; i < npages; i++)
5050 if (pages[i])
5051 __free_page(pages[i]);
5052 if (res.acl_scratch)
5053 __free_page(res.acl_scratch);
5054 return ret;
5057 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5059 struct nfs4_exception exception = { };
5060 ssize_t ret;
5061 do {
5062 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5063 trace_nfs4_get_acl(inode, ret);
5064 if (ret >= 0)
5065 break;
5066 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5067 } while (exception.retry);
5068 return ret;
5071 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5073 struct nfs_server *server = NFS_SERVER(inode);
5074 int ret;
5076 if (!nfs4_server_supports_acls(server))
5077 return -EOPNOTSUPP;
5078 ret = nfs_revalidate_inode(server, inode);
5079 if (ret < 0)
5080 return ret;
5081 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5082 nfs_zap_acl_cache(inode);
5083 ret = nfs4_read_cached_acl(inode, buf, buflen);
5084 if (ret != -ENOENT)
5085 /* -ENOENT is returned if there is no ACL or if there is an ACL
5086 * but no cached acl data, just the acl length */
5087 return ret;
5088 return nfs4_get_acl_uncached(inode, buf, buflen);
5091 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5093 struct nfs_server *server = NFS_SERVER(inode);
5094 struct page *pages[NFS4ACL_MAXPAGES];
5095 struct nfs_setaclargs arg = {
5096 .fh = NFS_FH(inode),
5097 .acl_pages = pages,
5098 .acl_len = buflen,
5100 struct nfs_setaclres res;
5101 struct rpc_message msg = {
5102 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5103 .rpc_argp = &arg,
5104 .rpc_resp = &res,
5106 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5107 int ret, i;
5109 if (!nfs4_server_supports_acls(server))
5110 return -EOPNOTSUPP;
5111 if (npages > ARRAY_SIZE(pages))
5112 return -ERANGE;
5113 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5114 if (i < 0)
5115 return i;
5116 nfs4_inode_return_delegation(inode);
5117 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5120 * Free each page after tx, so the only ref left is
5121 * held by the network stack
5123 for (; i > 0; i--)
5124 put_page(pages[i-1]);
5127 * Acl update can result in inode attribute update.
5128 * so mark the attribute cache invalid.
5130 spin_lock(&inode->i_lock);
5131 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5132 spin_unlock(&inode->i_lock);
5133 nfs_access_zap_cache(inode);
5134 nfs_zap_acl_cache(inode);
5135 return ret;
5138 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5140 struct nfs4_exception exception = { };
5141 int err;
5142 do {
5143 err = __nfs4_proc_set_acl(inode, buf, buflen);
5144 trace_nfs4_set_acl(inode, err);
5145 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5146 &exception);
5147 } while (exception.retry);
5148 return err;
5151 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5152 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5153 size_t buflen)
5155 struct nfs_server *server = NFS_SERVER(inode);
5156 struct nfs_fattr fattr;
5157 struct nfs4_label label = {0, 0, buflen, buf};
5159 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5160 struct nfs4_getattr_arg arg = {
5161 .fh = NFS_FH(inode),
5162 .bitmask = bitmask,
5164 struct nfs4_getattr_res res = {
5165 .fattr = &fattr,
5166 .label = &label,
5167 .server = server,
5169 struct rpc_message msg = {
5170 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5171 .rpc_argp = &arg,
5172 .rpc_resp = &res,
5174 int ret;
5176 nfs_fattr_init(&fattr);
5178 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5179 if (ret)
5180 return ret;
5181 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5182 return -ENOENT;
5183 if (buflen < label.len)
5184 return -ERANGE;
5185 return 0;
5188 static int nfs4_get_security_label(struct inode *inode, void *buf,
5189 size_t buflen)
5191 struct nfs4_exception exception = { };
5192 int err;
5194 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5195 return -EOPNOTSUPP;
5197 do {
5198 err = _nfs4_get_security_label(inode, buf, buflen);
5199 trace_nfs4_get_security_label(inode, err);
5200 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5201 &exception);
5202 } while (exception.retry);
5203 return err;
5206 static int _nfs4_do_set_security_label(struct inode *inode,
5207 struct nfs4_label *ilabel,
5208 struct nfs_fattr *fattr,
5209 struct nfs4_label *olabel)
5212 struct iattr sattr = {0};
5213 struct nfs_server *server = NFS_SERVER(inode);
5214 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5215 struct nfs_setattrargs arg = {
5216 .fh = NFS_FH(inode),
5217 .iap = &sattr,
5218 .server = server,
5219 .bitmask = bitmask,
5220 .label = ilabel,
5222 struct nfs_setattrres res = {
5223 .fattr = fattr,
5224 .label = olabel,
5225 .server = server,
5227 struct rpc_message msg = {
5228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5229 .rpc_argp = &arg,
5230 .rpc_resp = &res,
5232 int status;
5234 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5236 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5237 if (status)
5238 dprintk("%s failed: %d\n", __func__, status);
5240 return status;
5243 static int nfs4_do_set_security_label(struct inode *inode,
5244 struct nfs4_label *ilabel,
5245 struct nfs_fattr *fattr,
5246 struct nfs4_label *olabel)
5248 struct nfs4_exception exception = { };
5249 int err;
5251 do {
5252 err = _nfs4_do_set_security_label(inode, ilabel,
5253 fattr, olabel);
5254 trace_nfs4_set_security_label(inode, err);
5255 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5256 &exception);
5257 } while (exception.retry);
5258 return err;
5261 static int
5262 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5264 struct nfs4_label ilabel, *olabel = NULL;
5265 struct nfs_fattr fattr;
5266 struct rpc_cred *cred;
5267 int status;
5269 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5270 return -EOPNOTSUPP;
5272 nfs_fattr_init(&fattr);
5274 ilabel.pi = 0;
5275 ilabel.lfs = 0;
5276 ilabel.label = (char *)buf;
5277 ilabel.len = buflen;
5279 cred = rpc_lookup_cred();
5280 if (IS_ERR(cred))
5281 return PTR_ERR(cred);
5283 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5284 if (IS_ERR(olabel)) {
5285 status = -PTR_ERR(olabel);
5286 goto out;
5289 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5290 if (status == 0)
5291 nfs_setsecurity(inode, &fattr, olabel);
5293 nfs4_label_free(olabel);
5294 out:
5295 put_rpccred(cred);
5296 return status;
5298 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5301 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5302 nfs4_verifier *bootverf)
5304 __be32 verf[2];
5306 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5307 /* An impossible timestamp guarantees this value
5308 * will never match a generated boot time. */
5309 verf[0] = cpu_to_be32(U32_MAX);
5310 verf[1] = cpu_to_be32(U32_MAX);
5311 } else {
5312 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5313 u64 ns = ktime_to_ns(nn->boot_time);
5315 verf[0] = cpu_to_be32(ns >> 32);
5316 verf[1] = cpu_to_be32(ns);
5318 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5321 static int
5322 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5324 size_t len;
5325 char *str;
5327 if (clp->cl_owner_id != NULL)
5328 return 0;
5330 rcu_read_lock();
5331 len = 14 + strlen(clp->cl_ipaddr) + 1 +
5332 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5334 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5336 rcu_read_unlock();
5338 if (len > NFS4_OPAQUE_LIMIT + 1)
5339 return -EINVAL;
5342 * Since this string is allocated at mount time, and held until the
5343 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5344 * about a memory-reclaim deadlock.
5346 str = kmalloc(len, GFP_KERNEL);
5347 if (!str)
5348 return -ENOMEM;
5350 rcu_read_lock();
5351 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
5352 clp->cl_ipaddr,
5353 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5354 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
5355 rcu_read_unlock();
5357 clp->cl_owner_id = str;
5358 return 0;
5361 static int
5362 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5364 size_t len;
5365 char *str;
5367 len = 10 + 10 + 1 + 10 + 1 +
5368 strlen(nfs4_client_id_uniquifier) + 1 +
5369 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5371 if (len > NFS4_OPAQUE_LIMIT + 1)
5372 return -EINVAL;
5375 * Since this string is allocated at mount time, and held until the
5376 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5377 * about a memory-reclaim deadlock.
5379 str = kmalloc(len, GFP_KERNEL);
5380 if (!str)
5381 return -ENOMEM;
5383 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5384 clp->rpc_ops->version, clp->cl_minorversion,
5385 nfs4_client_id_uniquifier,
5386 clp->cl_rpcclient->cl_nodename);
5387 clp->cl_owner_id = str;
5388 return 0;
5391 static int
5392 nfs4_init_uniform_client_string(struct nfs_client *clp)
5394 size_t len;
5395 char *str;
5397 if (clp->cl_owner_id != NULL)
5398 return 0;
5400 if (nfs4_client_id_uniquifier[0] != '\0')
5401 return nfs4_init_uniquifier_client_string(clp);
5403 len = 10 + 10 + 1 + 10 + 1 +
5404 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5406 if (len > NFS4_OPAQUE_LIMIT + 1)
5407 return -EINVAL;
5410 * Since this string is allocated at mount time, and held until the
5411 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5412 * about a memory-reclaim deadlock.
5414 str = kmalloc(len, GFP_KERNEL);
5415 if (!str)
5416 return -ENOMEM;
5418 scnprintf(str, len, "Linux NFSv%u.%u %s",
5419 clp->rpc_ops->version, clp->cl_minorversion,
5420 clp->cl_rpcclient->cl_nodename);
5421 clp->cl_owner_id = str;
5422 return 0;
5426 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5427 * services. Advertise one based on the address family of the
5428 * clientaddr.
5430 static unsigned int
5431 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5433 if (strchr(clp->cl_ipaddr, ':') != NULL)
5434 return scnprintf(buf, len, "tcp6");
5435 else
5436 return scnprintf(buf, len, "tcp");
5439 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5441 struct nfs4_setclientid *sc = calldata;
5443 if (task->tk_status == 0)
5444 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5447 static const struct rpc_call_ops nfs4_setclientid_ops = {
5448 .rpc_call_done = nfs4_setclientid_done,
5452 * nfs4_proc_setclientid - Negotiate client ID
5453 * @clp: state data structure
5454 * @program: RPC program for NFSv4 callback service
5455 * @port: IP port number for NFS4 callback service
5456 * @cred: RPC credential to use for this call
5457 * @res: where to place the result
5459 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5461 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5462 unsigned short port, struct rpc_cred *cred,
5463 struct nfs4_setclientid_res *res)
5465 nfs4_verifier sc_verifier;
5466 struct nfs4_setclientid setclientid = {
5467 .sc_verifier = &sc_verifier,
5468 .sc_prog = program,
5469 .sc_clnt = clp,
5471 struct rpc_message msg = {
5472 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5473 .rpc_argp = &setclientid,
5474 .rpc_resp = res,
5475 .rpc_cred = cred,
5477 struct rpc_task *task;
5478 struct rpc_task_setup task_setup_data = {
5479 .rpc_client = clp->cl_rpcclient,
5480 .rpc_message = &msg,
5481 .callback_ops = &nfs4_setclientid_ops,
5482 .callback_data = &setclientid,
5483 .flags = RPC_TASK_TIMEOUT,
5485 int status;
5487 /* nfs_client_id4 */
5488 nfs4_init_boot_verifier(clp, &sc_verifier);
5490 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5491 status = nfs4_init_uniform_client_string(clp);
5492 else
5493 status = nfs4_init_nonuniform_client_string(clp);
5495 if (status)
5496 goto out;
5498 /* cb_client4 */
5499 setclientid.sc_netid_len =
5500 nfs4_init_callback_netid(clp,
5501 setclientid.sc_netid,
5502 sizeof(setclientid.sc_netid));
5503 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5504 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5505 clp->cl_ipaddr, port >> 8, port & 255);
5507 dprintk("NFS call setclientid auth=%s, '%s'\n",
5508 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5509 clp->cl_owner_id);
5510 task = rpc_run_task(&task_setup_data);
5511 if (IS_ERR(task)) {
5512 status = PTR_ERR(task);
5513 goto out;
5515 status = task->tk_status;
5516 if (setclientid.sc_cred) {
5517 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5518 put_rpccred(setclientid.sc_cred);
5520 rpc_put_task(task);
5521 out:
5522 trace_nfs4_setclientid(clp, status);
5523 dprintk("NFS reply setclientid: %d\n", status);
5524 return status;
5528 * nfs4_proc_setclientid_confirm - Confirm client ID
5529 * @clp: state data structure
5530 * @res: result of a previous SETCLIENTID
5531 * @cred: RPC credential to use for this call
5533 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5535 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5536 struct nfs4_setclientid_res *arg,
5537 struct rpc_cred *cred)
5539 struct rpc_message msg = {
5540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5541 .rpc_argp = arg,
5542 .rpc_cred = cred,
5544 int status;
5546 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5547 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5548 clp->cl_clientid);
5549 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5550 trace_nfs4_setclientid_confirm(clp, status);
5551 dprintk("NFS reply setclientid_confirm: %d\n", status);
5552 return status;
5555 struct nfs4_delegreturndata {
5556 struct nfs4_delegreturnargs args;
5557 struct nfs4_delegreturnres res;
5558 struct nfs_fh fh;
5559 nfs4_stateid stateid;
5560 unsigned long timestamp;
5561 struct nfs_fattr fattr;
5562 int rpc_status;
5563 struct inode *inode;
5564 bool roc;
5565 u32 roc_barrier;
5568 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5570 struct nfs4_delegreturndata *data = calldata;
5572 if (!nfs4_sequence_done(task, &data->res.seq_res))
5573 return;
5575 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
5576 switch (task->tk_status) {
5577 case 0:
5578 renew_lease(data->res.server, data->timestamp);
5579 break;
5580 case -NFS4ERR_ADMIN_REVOKED:
5581 case -NFS4ERR_DELEG_REVOKED:
5582 case -NFS4ERR_EXPIRED:
5583 nfs4_free_revoked_stateid(data->res.server,
5584 data->args.stateid,
5585 task->tk_msg.rpc_cred);
5586 case -NFS4ERR_BAD_STATEID:
5587 case -NFS4ERR_OLD_STATEID:
5588 case -NFS4ERR_STALE_STATEID:
5589 task->tk_status = 0;
5590 break;
5591 default:
5592 if (nfs4_async_handle_error(task, data->res.server,
5593 NULL, NULL) == -EAGAIN) {
5594 rpc_restart_call_prepare(task);
5595 return;
5598 data->rpc_status = task->tk_status;
5599 if (data->roc && data->rpc_status == 0)
5600 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
5603 static void nfs4_delegreturn_release(void *calldata)
5605 struct nfs4_delegreturndata *data = calldata;
5606 struct inode *inode = data->inode;
5608 if (inode) {
5609 if (data->roc)
5610 pnfs_roc_release(inode);
5611 nfs_iput_and_deactive(inode);
5613 kfree(calldata);
5616 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5618 struct nfs4_delegreturndata *d_data;
5620 d_data = (struct nfs4_delegreturndata *)data;
5622 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5623 return;
5625 if (d_data->roc)
5626 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
5628 nfs4_setup_sequence(d_data->res.server,
5629 &d_data->args.seq_args,
5630 &d_data->res.seq_res,
5631 task);
5634 static const struct rpc_call_ops nfs4_delegreturn_ops = {
5635 .rpc_call_prepare = nfs4_delegreturn_prepare,
5636 .rpc_call_done = nfs4_delegreturn_done,
5637 .rpc_release = nfs4_delegreturn_release,
5640 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5642 struct nfs4_delegreturndata *data;
5643 struct nfs_server *server = NFS_SERVER(inode);
5644 struct rpc_task *task;
5645 struct rpc_message msg = {
5646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5647 .rpc_cred = cred,
5649 struct rpc_task_setup task_setup_data = {
5650 .rpc_client = server->client,
5651 .rpc_message = &msg,
5652 .callback_ops = &nfs4_delegreturn_ops,
5653 .flags = RPC_TASK_ASYNC,
5655 int status = 0;
5657 data = kzalloc(sizeof(*data), GFP_NOFS);
5658 if (data == NULL)
5659 return -ENOMEM;
5660 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
5662 nfs4_state_protect(server->nfs_client,
5663 NFS_SP4_MACH_CRED_CLEANUP,
5664 &task_setup_data.rpc_client, &msg);
5666 data->args.fhandle = &data->fh;
5667 data->args.stateid = &data->stateid;
5668 data->args.bitmask = server->cache_consistency_bitmask;
5669 nfs_copy_fh(&data->fh, NFS_FH(inode));
5670 nfs4_stateid_copy(&data->stateid, stateid);
5671 data->res.fattr = &data->fattr;
5672 data->res.server = server;
5673 nfs_fattr_init(data->res.fattr);
5674 data->timestamp = jiffies;
5675 data->rpc_status = 0;
5676 data->inode = nfs_igrab_and_active(inode);
5677 if (data->inode)
5678 data->roc = nfs4_roc(inode);
5680 task_setup_data.callback_data = data;
5681 msg.rpc_argp = &data->args;
5682 msg.rpc_resp = &data->res;
5683 task = rpc_run_task(&task_setup_data);
5684 if (IS_ERR(task))
5685 return PTR_ERR(task);
5686 if (!issync)
5687 goto out;
5688 status = nfs4_wait_for_completion_rpc_task(task);
5689 if (status != 0)
5690 goto out;
5691 status = data->rpc_status;
5692 if (status == 0)
5693 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5694 else
5695 nfs_refresh_inode(inode, &data->fattr);
5696 out:
5697 rpc_put_task(task);
5698 return status;
5701 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
5703 struct nfs_server *server = NFS_SERVER(inode);
5704 struct nfs4_exception exception = { };
5705 int err;
5706 do {
5707 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
5708 trace_nfs4_delegreturn(inode, stateid, err);
5709 switch (err) {
5710 case -NFS4ERR_STALE_STATEID:
5711 case -NFS4ERR_EXPIRED:
5712 case 0:
5713 return 0;
5715 err = nfs4_handle_exception(server, err, &exception);
5716 } while (exception.retry);
5717 return err;
5720 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5722 struct inode *inode = state->inode;
5723 struct nfs_server *server = NFS_SERVER(inode);
5724 struct nfs_client *clp = server->nfs_client;
5725 struct nfs_lockt_args arg = {
5726 .fh = NFS_FH(inode),
5727 .fl = request,
5729 struct nfs_lockt_res res = {
5730 .denied = request,
5732 struct rpc_message msg = {
5733 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5734 .rpc_argp = &arg,
5735 .rpc_resp = &res,
5736 .rpc_cred = state->owner->so_cred,
5738 struct nfs4_lock_state *lsp;
5739 int status;
5741 arg.lock_owner.clientid = clp->cl_clientid;
5742 status = nfs4_set_lock_state(state, request);
5743 if (status != 0)
5744 goto out;
5745 lsp = request->fl_u.nfs4_fl.owner;
5746 arg.lock_owner.id = lsp->ls_seqid.owner_id;
5747 arg.lock_owner.s_dev = server->s_dev;
5748 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5749 switch (status) {
5750 case 0:
5751 request->fl_type = F_UNLCK;
5752 break;
5753 case -NFS4ERR_DENIED:
5754 status = 0;
5756 request->fl_ops->fl_release_private(request);
5757 request->fl_ops = NULL;
5758 out:
5759 return status;
5762 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5764 struct nfs4_exception exception = { };
5765 int err;
5767 do {
5768 err = _nfs4_proc_getlk(state, cmd, request);
5769 trace_nfs4_get_lock(request, state, cmd, err);
5770 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
5771 &exception);
5772 } while (exception.retry);
5773 return err;
5776 struct nfs4_unlockdata {
5777 struct nfs_locku_args arg;
5778 struct nfs_locku_res res;
5779 struct nfs4_lock_state *lsp;
5780 struct nfs_open_context *ctx;
5781 struct file_lock fl;
5782 struct nfs_server *server;
5783 unsigned long timestamp;
5786 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5787 struct nfs_open_context *ctx,
5788 struct nfs4_lock_state *lsp,
5789 struct nfs_seqid *seqid)
5791 struct nfs4_unlockdata *p;
5792 struct inode *inode = lsp->ls_state->inode;
5794 p = kzalloc(sizeof(*p), GFP_NOFS);
5795 if (p == NULL)
5796 return NULL;
5797 p->arg.fh = NFS_FH(inode);
5798 p->arg.fl = &p->fl;
5799 p->arg.seqid = seqid;
5800 p->res.seqid = seqid;
5801 p->lsp = lsp;
5802 atomic_inc(&lsp->ls_count);
5803 /* Ensure we don't close file until we're done freeing locks! */
5804 p->ctx = get_nfs_open_context(ctx);
5805 memcpy(&p->fl, fl, sizeof(p->fl));
5806 p->server = NFS_SERVER(inode);
5807 return p;
5810 static void nfs4_locku_release_calldata(void *data)
5812 struct nfs4_unlockdata *calldata = data;
5813 nfs_free_seqid(calldata->arg.seqid);
5814 nfs4_put_lock_state(calldata->lsp);
5815 put_nfs_open_context(calldata->ctx);
5816 kfree(calldata);
5819 static void nfs4_locku_done(struct rpc_task *task, void *data)
5821 struct nfs4_unlockdata *calldata = data;
5823 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5824 return;
5825 switch (task->tk_status) {
5826 case 0:
5827 renew_lease(calldata->server, calldata->timestamp);
5828 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
5829 if (nfs4_update_lock_stateid(calldata->lsp,
5830 &calldata->res.stateid))
5831 break;
5832 case -NFS4ERR_ADMIN_REVOKED:
5833 case -NFS4ERR_EXPIRED:
5834 nfs4_free_revoked_stateid(calldata->server,
5835 &calldata->arg.stateid,
5836 task->tk_msg.rpc_cred);
5837 case -NFS4ERR_BAD_STATEID:
5838 case -NFS4ERR_OLD_STATEID:
5839 case -NFS4ERR_STALE_STATEID:
5840 if (!nfs4_stateid_match(&calldata->arg.stateid,
5841 &calldata->lsp->ls_stateid))
5842 rpc_restart_call_prepare(task);
5843 break;
5844 default:
5845 if (nfs4_async_handle_error(task, calldata->server,
5846 NULL, NULL) == -EAGAIN)
5847 rpc_restart_call_prepare(task);
5849 nfs_release_seqid(calldata->arg.seqid);
5852 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
5854 struct nfs4_unlockdata *calldata = data;
5856 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
5857 goto out_wait;
5858 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
5859 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
5860 /* Note: exit _without_ running nfs4_locku_done */
5861 goto out_no_action;
5863 calldata->timestamp = jiffies;
5864 if (nfs4_setup_sequence(calldata->server,
5865 &calldata->arg.seq_args,
5866 &calldata->res.seq_res,
5867 task) != 0)
5868 nfs_release_seqid(calldata->arg.seqid);
5869 return;
5870 out_no_action:
5871 task->tk_action = NULL;
5872 out_wait:
5873 nfs4_sequence_done(task, &calldata->res.seq_res);
5876 static const struct rpc_call_ops nfs4_locku_ops = {
5877 .rpc_call_prepare = nfs4_locku_prepare,
5878 .rpc_call_done = nfs4_locku_done,
5879 .rpc_release = nfs4_locku_release_calldata,
5882 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5883 struct nfs_open_context *ctx,
5884 struct nfs4_lock_state *lsp,
5885 struct nfs_seqid *seqid)
5887 struct nfs4_unlockdata *data;
5888 struct rpc_message msg = {
5889 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5890 .rpc_cred = ctx->cred,
5892 struct rpc_task_setup task_setup_data = {
5893 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
5894 .rpc_message = &msg,
5895 .callback_ops = &nfs4_locku_ops,
5896 .workqueue = nfsiod_workqueue,
5897 .flags = RPC_TASK_ASYNC,
5900 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5901 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5903 /* Ensure this is an unlock - when canceling a lock, the
5904 * canceled lock is passed in, and it won't be an unlock.
5906 fl->fl_type = F_UNLCK;
5908 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5909 if (data == NULL) {
5910 nfs_free_seqid(seqid);
5911 return ERR_PTR(-ENOMEM);
5914 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
5915 msg.rpc_argp = &data->arg;
5916 msg.rpc_resp = &data->res;
5917 task_setup_data.callback_data = data;
5918 return rpc_run_task(&task_setup_data);
5921 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5923 struct inode *inode = state->inode;
5924 struct nfs4_state_owner *sp = state->owner;
5925 struct nfs_inode *nfsi = NFS_I(inode);
5926 struct nfs_seqid *seqid;
5927 struct nfs4_lock_state *lsp;
5928 struct rpc_task *task;
5929 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
5930 int status = 0;
5931 unsigned char fl_flags = request->fl_flags;
5933 status = nfs4_set_lock_state(state, request);
5934 /* Unlock _before_ we do the RPC call */
5935 request->fl_flags |= FL_EXISTS;
5936 /* Exclude nfs_delegation_claim_locks() */
5937 mutex_lock(&sp->so_delegreturn_mutex);
5938 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
5939 down_read(&nfsi->rwsem);
5940 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
5941 up_read(&nfsi->rwsem);
5942 mutex_unlock(&sp->so_delegreturn_mutex);
5943 goto out;
5945 up_read(&nfsi->rwsem);
5946 mutex_unlock(&sp->so_delegreturn_mutex);
5947 if (status != 0)
5948 goto out;
5949 /* Is this a delegated lock? */
5950 lsp = request->fl_u.nfs4_fl.owner;
5951 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5952 goto out;
5953 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5954 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
5955 status = -ENOMEM;
5956 if (IS_ERR(seqid))
5957 goto out;
5958 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
5959 status = PTR_ERR(task);
5960 if (IS_ERR(task))
5961 goto out;
5962 status = nfs4_wait_for_completion_rpc_task(task);
5963 rpc_put_task(task);
5964 out:
5965 request->fl_flags = fl_flags;
5966 trace_nfs4_unlock(request, state, F_SETLK, status);
5967 return status;
5970 struct nfs4_lockdata {
5971 struct nfs_lock_args arg;
5972 struct nfs_lock_res res;
5973 struct nfs4_lock_state *lsp;
5974 struct nfs_open_context *ctx;
5975 struct file_lock fl;
5976 unsigned long timestamp;
5977 int rpc_status;
5978 int cancelled;
5979 struct nfs_server *server;
5982 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
5983 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5984 gfp_t gfp_mask)
5986 struct nfs4_lockdata *p;
5987 struct inode *inode = lsp->ls_state->inode;
5988 struct nfs_server *server = NFS_SERVER(inode);
5989 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
5991 p = kzalloc(sizeof(*p), gfp_mask);
5992 if (p == NULL)
5993 return NULL;
5995 p->arg.fh = NFS_FH(inode);
5996 p->arg.fl = &p->fl;
5997 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
5998 if (IS_ERR(p->arg.open_seqid))
5999 goto out_free;
6000 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6001 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6002 if (IS_ERR(p->arg.lock_seqid))
6003 goto out_free_seqid;
6004 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6005 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6006 p->arg.lock_owner.s_dev = server->s_dev;
6007 p->res.lock_seqid = p->arg.lock_seqid;
6008 p->lsp = lsp;
6009 p->server = server;
6010 atomic_inc(&lsp->ls_count);
6011 p->ctx = get_nfs_open_context(ctx);
6012 get_file(fl->fl_file);
6013 memcpy(&p->fl, fl, sizeof(p->fl));
6014 return p;
6015 out_free_seqid:
6016 nfs_free_seqid(p->arg.open_seqid);
6017 out_free:
6018 kfree(p);
6019 return NULL;
6022 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6024 struct nfs4_lockdata *data = calldata;
6025 struct nfs4_state *state = data->lsp->ls_state;
6027 dprintk("%s: begin!\n", __func__);
6028 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6029 goto out_wait;
6030 /* Do we need to do an open_to_lock_owner? */
6031 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6032 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6033 goto out_release_lock_seqid;
6035 nfs4_stateid_copy(&data->arg.open_stateid,
6036 &state->open_stateid);
6037 data->arg.new_lock_owner = 1;
6038 data->res.open_seqid = data->arg.open_seqid;
6039 } else {
6040 data->arg.new_lock_owner = 0;
6041 nfs4_stateid_copy(&data->arg.lock_stateid,
6042 &data->lsp->ls_stateid);
6044 if (!nfs4_valid_open_stateid(state)) {
6045 data->rpc_status = -EBADF;
6046 task->tk_action = NULL;
6047 goto out_release_open_seqid;
6049 data->timestamp = jiffies;
6050 if (nfs4_setup_sequence(data->server,
6051 &data->arg.seq_args,
6052 &data->res.seq_res,
6053 task) == 0)
6054 return;
6055 out_release_open_seqid:
6056 nfs_release_seqid(data->arg.open_seqid);
6057 out_release_lock_seqid:
6058 nfs_release_seqid(data->arg.lock_seqid);
6059 out_wait:
6060 nfs4_sequence_done(task, &data->res.seq_res);
6061 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6064 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6066 struct nfs4_lockdata *data = calldata;
6067 struct nfs4_lock_state *lsp = data->lsp;
6069 dprintk("%s: begin!\n", __func__);
6071 if (!nfs4_sequence_done(task, &data->res.seq_res))
6072 return;
6074 data->rpc_status = task->tk_status;
6075 switch (task->tk_status) {
6076 case 0:
6077 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6078 data->timestamp);
6079 if (data->arg.new_lock) {
6080 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6081 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
6082 rpc_restart_call_prepare(task);
6083 break;
6086 if (data->arg.new_lock_owner != 0) {
6087 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6088 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6089 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6090 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6091 rpc_restart_call_prepare(task);
6092 break;
6093 case -NFS4ERR_BAD_STATEID:
6094 case -NFS4ERR_OLD_STATEID:
6095 case -NFS4ERR_STALE_STATEID:
6096 case -NFS4ERR_EXPIRED:
6097 if (data->arg.new_lock_owner != 0) {
6098 if (!nfs4_stateid_match(&data->arg.open_stateid,
6099 &lsp->ls_state->open_stateid))
6100 rpc_restart_call_prepare(task);
6101 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6102 &lsp->ls_stateid))
6103 rpc_restart_call_prepare(task);
6105 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6108 static void nfs4_lock_release(void *calldata)
6110 struct nfs4_lockdata *data = calldata;
6112 dprintk("%s: begin!\n", __func__);
6113 nfs_free_seqid(data->arg.open_seqid);
6114 if (data->cancelled != 0) {
6115 struct rpc_task *task;
6116 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6117 data->arg.lock_seqid);
6118 if (!IS_ERR(task))
6119 rpc_put_task_async(task);
6120 dprintk("%s: cancelling lock!\n", __func__);
6121 } else
6122 nfs_free_seqid(data->arg.lock_seqid);
6123 nfs4_put_lock_state(data->lsp);
6124 put_nfs_open_context(data->ctx);
6125 fput(data->fl.fl_file);
6126 kfree(data);
6127 dprintk("%s: done!\n", __func__);
6130 static const struct rpc_call_ops nfs4_lock_ops = {
6131 .rpc_call_prepare = nfs4_lock_prepare,
6132 .rpc_call_done = nfs4_lock_done,
6133 .rpc_release = nfs4_lock_release,
6136 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6138 switch (error) {
6139 case -NFS4ERR_ADMIN_REVOKED:
6140 case -NFS4ERR_EXPIRED:
6141 case -NFS4ERR_BAD_STATEID:
6142 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6143 if (new_lock_owner != 0 ||
6144 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6145 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6146 break;
6147 case -NFS4ERR_STALE_STATEID:
6148 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6149 nfs4_schedule_lease_recovery(server->nfs_client);
6153 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6155 struct nfs4_lockdata *data;
6156 struct rpc_task *task;
6157 struct rpc_message msg = {
6158 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6159 .rpc_cred = state->owner->so_cred,
6161 struct rpc_task_setup task_setup_data = {
6162 .rpc_client = NFS_CLIENT(state->inode),
6163 .rpc_message = &msg,
6164 .callback_ops = &nfs4_lock_ops,
6165 .workqueue = nfsiod_workqueue,
6166 .flags = RPC_TASK_ASYNC,
6168 int ret;
6170 dprintk("%s: begin!\n", __func__);
6171 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6172 fl->fl_u.nfs4_fl.owner,
6173 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6174 if (data == NULL)
6175 return -ENOMEM;
6176 if (IS_SETLKW(cmd))
6177 data->arg.block = 1;
6178 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
6179 msg.rpc_argp = &data->arg;
6180 msg.rpc_resp = &data->res;
6181 task_setup_data.callback_data = data;
6182 if (recovery_type > NFS_LOCK_NEW) {
6183 if (recovery_type == NFS_LOCK_RECLAIM)
6184 data->arg.reclaim = NFS_LOCK_RECLAIM;
6185 nfs4_set_sequence_privileged(&data->arg.seq_args);
6186 } else
6187 data->arg.new_lock = 1;
6188 task = rpc_run_task(&task_setup_data);
6189 if (IS_ERR(task))
6190 return PTR_ERR(task);
6191 ret = nfs4_wait_for_completion_rpc_task(task);
6192 if (ret == 0) {
6193 ret = data->rpc_status;
6194 if (ret)
6195 nfs4_handle_setlk_error(data->server, data->lsp,
6196 data->arg.new_lock_owner, ret);
6197 } else
6198 data->cancelled = 1;
6199 rpc_put_task(task);
6200 dprintk("%s: done, ret = %d!\n", __func__, ret);
6201 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6202 return ret;
6205 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6207 struct nfs_server *server = NFS_SERVER(state->inode);
6208 struct nfs4_exception exception = {
6209 .inode = state->inode,
6211 int err;
6213 do {
6214 /* Cache the lock if possible... */
6215 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6216 return 0;
6217 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6218 if (err != -NFS4ERR_DELAY)
6219 break;
6220 nfs4_handle_exception(server, err, &exception);
6221 } while (exception.retry);
6222 return err;
6225 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6227 struct nfs_server *server = NFS_SERVER(state->inode);
6228 struct nfs4_exception exception = {
6229 .inode = state->inode,
6231 int err;
6233 err = nfs4_set_lock_state(state, request);
6234 if (err != 0)
6235 return err;
6236 if (!recover_lost_locks) {
6237 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6238 return 0;
6240 do {
6241 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6242 return 0;
6243 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6244 switch (err) {
6245 default:
6246 goto out;
6247 case -NFS4ERR_GRACE:
6248 case -NFS4ERR_DELAY:
6249 nfs4_handle_exception(server, err, &exception);
6250 err = 0;
6252 } while (exception.retry);
6253 out:
6254 return err;
6257 #if defined(CONFIG_NFS_V4_1)
6258 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6260 struct nfs4_lock_state *lsp;
6261 int status;
6263 status = nfs4_set_lock_state(state, request);
6264 if (status != 0)
6265 return status;
6266 lsp = request->fl_u.nfs4_fl.owner;
6267 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6268 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6269 return 0;
6270 status = nfs4_lock_expired(state, request);
6271 return status;
6273 #endif
6275 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6277 struct nfs_inode *nfsi = NFS_I(state->inode);
6278 struct nfs4_state_owner *sp = state->owner;
6279 unsigned char fl_flags = request->fl_flags;
6280 int status;
6282 request->fl_flags |= FL_ACCESS;
6283 status = locks_lock_inode_wait(state->inode, request);
6284 if (status < 0)
6285 goto out;
6286 mutex_lock(&sp->so_delegreturn_mutex);
6287 down_read(&nfsi->rwsem);
6288 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6289 /* Yes: cache locks! */
6290 /* ...but avoid races with delegation recall... */
6291 request->fl_flags = fl_flags & ~FL_SLEEP;
6292 status = locks_lock_inode_wait(state->inode, request);
6293 up_read(&nfsi->rwsem);
6294 mutex_unlock(&sp->so_delegreturn_mutex);
6295 goto out;
6297 up_read(&nfsi->rwsem);
6298 mutex_unlock(&sp->so_delegreturn_mutex);
6299 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6300 out:
6301 request->fl_flags = fl_flags;
6302 return status;
6305 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6307 struct nfs4_exception exception = {
6308 .state = state,
6309 .inode = state->inode,
6311 int err;
6313 do {
6314 err = _nfs4_proc_setlk(state, cmd, request);
6315 if (err == -NFS4ERR_DENIED)
6316 err = -EAGAIN;
6317 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6318 err, &exception);
6319 } while (exception.retry);
6320 return err;
6323 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6324 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6326 static int
6327 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6328 struct file_lock *request)
6330 int status = -ERESTARTSYS;
6331 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6333 while(!signalled()) {
6334 status = nfs4_proc_setlk(state, cmd, request);
6335 if ((status != -EAGAIN) || IS_SETLK(cmd))
6336 break;
6337 freezable_schedule_timeout_interruptible(timeout);
6338 timeout *= 2;
6339 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6340 status = -ERESTARTSYS;
6342 return status;
6345 #ifdef CONFIG_NFS_V4_1
6346 struct nfs4_lock_waiter {
6347 struct task_struct *task;
6348 struct inode *inode;
6349 struct nfs_lowner *owner;
6350 bool notified;
6353 static int
6354 nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6356 int ret;
6357 struct cb_notify_lock_args *cbnl = key;
6358 struct nfs4_lock_waiter *waiter = wait->private;
6359 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6360 *wowner = waiter->owner;
6362 /* Only wake if the callback was for the same owner */
6363 if (lowner->clientid != wowner->clientid ||
6364 lowner->id != wowner->id ||
6365 lowner->s_dev != wowner->s_dev)
6366 return 0;
6368 /* Make sure it's for the right inode */
6369 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6370 return 0;
6372 waiter->notified = true;
6374 /* override "private" so we can use default_wake_function */
6375 wait->private = waiter->task;
6376 ret = autoremove_wake_function(wait, mode, flags, key);
6377 wait->private = waiter;
6378 return ret;
6381 static int
6382 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6384 int status = -ERESTARTSYS;
6385 unsigned long flags;
6386 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6387 struct nfs_server *server = NFS_SERVER(state->inode);
6388 struct nfs_client *clp = server->nfs_client;
6389 wait_queue_head_t *q = &clp->cl_lock_waitq;
6390 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6391 .id = lsp->ls_seqid.owner_id,
6392 .s_dev = server->s_dev };
6393 struct nfs4_lock_waiter waiter = { .task = current,
6394 .inode = state->inode,
6395 .owner = &owner,
6396 .notified = false };
6397 wait_queue_t wait;
6399 /* Don't bother with waitqueue if we don't expect a callback */
6400 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6401 return nfs4_retry_setlk_simple(state, cmd, request);
6403 init_wait(&wait);
6404 wait.private = &waiter;
6405 wait.func = nfs4_wake_lock_waiter;
6406 add_wait_queue(q, &wait);
6408 while(!signalled()) {
6409 status = nfs4_proc_setlk(state, cmd, request);
6410 if ((status != -EAGAIN) || IS_SETLK(cmd))
6411 break;
6413 status = -ERESTARTSYS;
6414 spin_lock_irqsave(&q->lock, flags);
6415 if (waiter.notified) {
6416 spin_unlock_irqrestore(&q->lock, flags);
6417 continue;
6419 set_current_state(TASK_INTERRUPTIBLE);
6420 spin_unlock_irqrestore(&q->lock, flags);
6422 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
6425 finish_wait(q, &wait);
6426 return status;
6428 #else /* !CONFIG_NFS_V4_1 */
6429 static inline int
6430 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6432 return nfs4_retry_setlk_simple(state, cmd, request);
6434 #endif
6436 static int
6437 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6439 struct nfs_open_context *ctx;
6440 struct nfs4_state *state;
6441 int status;
6443 /* verify open state */
6444 ctx = nfs_file_open_context(filp);
6445 state = ctx->state;
6447 if (request->fl_start < 0 || request->fl_end < 0)
6448 return -EINVAL;
6450 if (IS_GETLK(cmd)) {
6451 if (state != NULL)
6452 return nfs4_proc_getlk(state, F_GETLK, request);
6453 return 0;
6456 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6457 return -EINVAL;
6459 if (request->fl_type == F_UNLCK) {
6460 if (state != NULL)
6461 return nfs4_proc_unlck(state, cmd, request);
6462 return 0;
6465 if (state == NULL)
6466 return -ENOLCK;
6468 if ((request->fl_flags & FL_POSIX) &&
6469 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6470 return -ENOLCK;
6473 * Don't rely on the VFS having checked the file open mode,
6474 * since it won't do this for flock() locks.
6476 switch (request->fl_type) {
6477 case F_RDLCK:
6478 if (!(filp->f_mode & FMODE_READ))
6479 return -EBADF;
6480 break;
6481 case F_WRLCK:
6482 if (!(filp->f_mode & FMODE_WRITE))
6483 return -EBADF;
6486 status = nfs4_set_lock_state(state, request);
6487 if (status != 0)
6488 return status;
6490 return nfs4_retry_setlk(state, cmd, request);
6493 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
6495 struct nfs_server *server = NFS_SERVER(state->inode);
6496 int err;
6498 err = nfs4_set_lock_state(state, fl);
6499 if (err != 0)
6500 return err;
6501 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
6502 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
6505 struct nfs_release_lockowner_data {
6506 struct nfs4_lock_state *lsp;
6507 struct nfs_server *server;
6508 struct nfs_release_lockowner_args args;
6509 struct nfs_release_lockowner_res res;
6510 unsigned long timestamp;
6513 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6515 struct nfs_release_lockowner_data *data = calldata;
6516 struct nfs_server *server = data->server;
6517 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6518 &data->args.seq_args, &data->res.seq_res, task);
6519 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6520 data->timestamp = jiffies;
6523 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6525 struct nfs_release_lockowner_data *data = calldata;
6526 struct nfs_server *server = data->server;
6528 nfs40_sequence_done(task, &data->res.seq_res);
6530 switch (task->tk_status) {
6531 case 0:
6532 renew_lease(server, data->timestamp);
6533 break;
6534 case -NFS4ERR_STALE_CLIENTID:
6535 case -NFS4ERR_EXPIRED:
6536 nfs4_schedule_lease_recovery(server->nfs_client);
6537 break;
6538 case -NFS4ERR_LEASE_MOVED:
6539 case -NFS4ERR_DELAY:
6540 if (nfs4_async_handle_error(task, server,
6541 NULL, NULL) == -EAGAIN)
6542 rpc_restart_call_prepare(task);
6546 static void nfs4_release_lockowner_release(void *calldata)
6548 struct nfs_release_lockowner_data *data = calldata;
6549 nfs4_free_lock_state(data->server, data->lsp);
6550 kfree(calldata);
6553 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
6554 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6555 .rpc_call_done = nfs4_release_lockowner_done,
6556 .rpc_release = nfs4_release_lockowner_release,
6559 static void
6560 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
6562 struct nfs_release_lockowner_data *data;
6563 struct rpc_message msg = {
6564 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6567 if (server->nfs_client->cl_mvops->minor_version != 0)
6568 return;
6570 data = kmalloc(sizeof(*data), GFP_NOFS);
6571 if (!data)
6572 return;
6573 data->lsp = lsp;
6574 data->server = server;
6575 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6576 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6577 data->args.lock_owner.s_dev = server->s_dev;
6579 msg.rpc_argp = &data->args;
6580 msg.rpc_resp = &data->res;
6581 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
6582 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
6585 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6587 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
6588 struct dentry *unused, struct inode *inode,
6589 const char *key, const void *buf,
6590 size_t buflen, int flags)
6592 return nfs4_proc_set_acl(inode, buf, buflen);
6595 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
6596 struct dentry *unused, struct inode *inode,
6597 const char *key, void *buf, size_t buflen)
6599 return nfs4_proc_get_acl(inode, buf, buflen);
6602 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
6604 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
6607 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
6609 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
6610 struct dentry *unused, struct inode *inode,
6611 const char *key, const void *buf,
6612 size_t buflen, int flags)
6614 if (security_ismaclabel(key))
6615 return nfs4_set_security_label(inode, buf, buflen);
6617 return -EOPNOTSUPP;
6620 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
6621 struct dentry *unused, struct inode *inode,
6622 const char *key, void *buf, size_t buflen)
6624 if (security_ismaclabel(key))
6625 return nfs4_get_security_label(inode, buf, buflen);
6626 return -EOPNOTSUPP;
6629 static ssize_t
6630 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6632 int len = 0;
6634 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6635 len = security_inode_listsecurity(inode, list, list_len);
6636 if (list_len && len > list_len)
6637 return -ERANGE;
6639 return len;
6642 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6643 .prefix = XATTR_SECURITY_PREFIX,
6644 .get = nfs4_xattr_get_nfs4_label,
6645 .set = nfs4_xattr_set_nfs4_label,
6648 #else
6650 static ssize_t
6651 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6653 return 0;
6656 #endif
6659 * nfs_fhget will use either the mounted_on_fileid or the fileid
6661 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6663 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6664 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6665 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
6666 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
6667 return;
6669 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
6670 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
6671 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6672 fattr->nlink = 2;
6675 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6676 const struct qstr *name,
6677 struct nfs4_fs_locations *fs_locations,
6678 struct page *page)
6680 struct nfs_server *server = NFS_SERVER(dir);
6681 u32 bitmask[3] = {
6682 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6684 struct nfs4_fs_locations_arg args = {
6685 .dir_fh = NFS_FH(dir),
6686 .name = name,
6687 .page = page,
6688 .bitmask = bitmask,
6690 struct nfs4_fs_locations_res res = {
6691 .fs_locations = fs_locations,
6693 struct rpc_message msg = {
6694 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6695 .rpc_argp = &args,
6696 .rpc_resp = &res,
6698 int status;
6700 dprintk("%s: start\n", __func__);
6702 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6703 * is not supported */
6704 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6705 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6706 else
6707 bitmask[0] |= FATTR4_WORD0_FILEID;
6709 nfs_fattr_init(&fs_locations->fattr);
6710 fs_locations->server = server;
6711 fs_locations->nlocations = 0;
6712 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
6713 dprintk("%s: returned status = %d\n", __func__, status);
6714 return status;
6717 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6718 const struct qstr *name,
6719 struct nfs4_fs_locations *fs_locations,
6720 struct page *page)
6722 struct nfs4_exception exception = { };
6723 int err;
6724 do {
6725 err = _nfs4_proc_fs_locations(client, dir, name,
6726 fs_locations, page);
6727 trace_nfs4_get_fs_locations(dir, name, err);
6728 err = nfs4_handle_exception(NFS_SERVER(dir), err,
6729 &exception);
6730 } while (exception.retry);
6731 return err;
6735 * This operation also signals the server that this client is
6736 * performing migration recovery. The server can stop returning
6737 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6738 * appended to this compound to identify the client ID which is
6739 * performing recovery.
6741 static int _nfs40_proc_get_locations(struct inode *inode,
6742 struct nfs4_fs_locations *locations,
6743 struct page *page, struct rpc_cred *cred)
6745 struct nfs_server *server = NFS_SERVER(inode);
6746 struct rpc_clnt *clnt = server->client;
6747 u32 bitmask[2] = {
6748 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6750 struct nfs4_fs_locations_arg args = {
6751 .clientid = server->nfs_client->cl_clientid,
6752 .fh = NFS_FH(inode),
6753 .page = page,
6754 .bitmask = bitmask,
6755 .migration = 1, /* skip LOOKUP */
6756 .renew = 1, /* append RENEW */
6758 struct nfs4_fs_locations_res res = {
6759 .fs_locations = locations,
6760 .migration = 1,
6761 .renew = 1,
6763 struct rpc_message msg = {
6764 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6765 .rpc_argp = &args,
6766 .rpc_resp = &res,
6767 .rpc_cred = cred,
6769 unsigned long now = jiffies;
6770 int status;
6772 nfs_fattr_init(&locations->fattr);
6773 locations->server = server;
6774 locations->nlocations = 0;
6776 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6777 nfs4_set_sequence_privileged(&args.seq_args);
6778 status = nfs4_call_sync_sequence(clnt, server, &msg,
6779 &args.seq_args, &res.seq_res);
6780 if (status)
6781 return status;
6783 renew_lease(server, now);
6784 return 0;
6787 #ifdef CONFIG_NFS_V4_1
6790 * This operation also signals the server that this client is
6791 * performing migration recovery. The server can stop asserting
6792 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6793 * performing this operation is identified in the SEQUENCE
6794 * operation in this compound.
6796 * When the client supports GETATTR(fs_locations_info), it can
6797 * be plumbed in here.
6799 static int _nfs41_proc_get_locations(struct inode *inode,
6800 struct nfs4_fs_locations *locations,
6801 struct page *page, struct rpc_cred *cred)
6803 struct nfs_server *server = NFS_SERVER(inode);
6804 struct rpc_clnt *clnt = server->client;
6805 u32 bitmask[2] = {
6806 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6808 struct nfs4_fs_locations_arg args = {
6809 .fh = NFS_FH(inode),
6810 .page = page,
6811 .bitmask = bitmask,
6812 .migration = 1, /* skip LOOKUP */
6814 struct nfs4_fs_locations_res res = {
6815 .fs_locations = locations,
6816 .migration = 1,
6818 struct rpc_message msg = {
6819 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6820 .rpc_argp = &args,
6821 .rpc_resp = &res,
6822 .rpc_cred = cred,
6824 int status;
6826 nfs_fattr_init(&locations->fattr);
6827 locations->server = server;
6828 locations->nlocations = 0;
6830 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6831 nfs4_set_sequence_privileged(&args.seq_args);
6832 status = nfs4_call_sync_sequence(clnt, server, &msg,
6833 &args.seq_args, &res.seq_res);
6834 if (status == NFS4_OK &&
6835 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6836 status = -NFS4ERR_LEASE_MOVED;
6837 return status;
6840 #endif /* CONFIG_NFS_V4_1 */
6843 * nfs4_proc_get_locations - discover locations for a migrated FSID
6844 * @inode: inode on FSID that is migrating
6845 * @locations: result of query
6846 * @page: buffer
6847 * @cred: credential to use for this operation
6849 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6850 * operation failed, or a negative errno if a local error occurred.
6852 * On success, "locations" is filled in, but if the server has
6853 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6854 * asserted.
6856 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6857 * from this client that require migration recovery.
6859 int nfs4_proc_get_locations(struct inode *inode,
6860 struct nfs4_fs_locations *locations,
6861 struct page *page, struct rpc_cred *cred)
6863 struct nfs_server *server = NFS_SERVER(inode);
6864 struct nfs_client *clp = server->nfs_client;
6865 const struct nfs4_mig_recovery_ops *ops =
6866 clp->cl_mvops->mig_recovery_ops;
6867 struct nfs4_exception exception = { };
6868 int status;
6870 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6871 (unsigned long long)server->fsid.major,
6872 (unsigned long long)server->fsid.minor,
6873 clp->cl_hostname);
6874 nfs_display_fhandle(NFS_FH(inode), __func__);
6876 do {
6877 status = ops->get_locations(inode, locations, page, cred);
6878 if (status != -NFS4ERR_DELAY)
6879 break;
6880 nfs4_handle_exception(server, status, &exception);
6881 } while (exception.retry);
6882 return status;
6886 * This operation also signals the server that this client is
6887 * performing "lease moved" recovery. The server can stop
6888 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6889 * is appended to this compound to identify the client ID which is
6890 * performing recovery.
6892 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6894 struct nfs_server *server = NFS_SERVER(inode);
6895 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6896 struct rpc_clnt *clnt = server->client;
6897 struct nfs4_fsid_present_arg args = {
6898 .fh = NFS_FH(inode),
6899 .clientid = clp->cl_clientid,
6900 .renew = 1, /* append RENEW */
6902 struct nfs4_fsid_present_res res = {
6903 .renew = 1,
6905 struct rpc_message msg = {
6906 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6907 .rpc_argp = &args,
6908 .rpc_resp = &res,
6909 .rpc_cred = cred,
6911 unsigned long now = jiffies;
6912 int status;
6914 res.fh = nfs_alloc_fhandle();
6915 if (res.fh == NULL)
6916 return -ENOMEM;
6918 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6919 nfs4_set_sequence_privileged(&args.seq_args);
6920 status = nfs4_call_sync_sequence(clnt, server, &msg,
6921 &args.seq_args, &res.seq_res);
6922 nfs_free_fhandle(res.fh);
6923 if (status)
6924 return status;
6926 do_renew_lease(clp, now);
6927 return 0;
6930 #ifdef CONFIG_NFS_V4_1
6933 * This operation also signals the server that this client is
6934 * performing "lease moved" recovery. The server can stop asserting
6935 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6936 * this operation is identified in the SEQUENCE operation in this
6937 * compound.
6939 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6941 struct nfs_server *server = NFS_SERVER(inode);
6942 struct rpc_clnt *clnt = server->client;
6943 struct nfs4_fsid_present_arg args = {
6944 .fh = NFS_FH(inode),
6946 struct nfs4_fsid_present_res res = {
6948 struct rpc_message msg = {
6949 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6950 .rpc_argp = &args,
6951 .rpc_resp = &res,
6952 .rpc_cred = cred,
6954 int status;
6956 res.fh = nfs_alloc_fhandle();
6957 if (res.fh == NULL)
6958 return -ENOMEM;
6960 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6961 nfs4_set_sequence_privileged(&args.seq_args);
6962 status = nfs4_call_sync_sequence(clnt, server, &msg,
6963 &args.seq_args, &res.seq_res);
6964 nfs_free_fhandle(res.fh);
6965 if (status == NFS4_OK &&
6966 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6967 status = -NFS4ERR_LEASE_MOVED;
6968 return status;
6971 #endif /* CONFIG_NFS_V4_1 */
6974 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6975 * @inode: inode on FSID to check
6976 * @cred: credential to use for this operation
6978 * Server indicates whether the FSID is present, moved, or not
6979 * recognized. This operation is necessary to clear a LEASE_MOVED
6980 * condition for this client ID.
6982 * Returns NFS4_OK if the FSID is present on this server,
6983 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6984 * NFS4ERR code if some error occurred on the server, or a
6985 * negative errno if a local failure occurred.
6987 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6989 struct nfs_server *server = NFS_SERVER(inode);
6990 struct nfs_client *clp = server->nfs_client;
6991 const struct nfs4_mig_recovery_ops *ops =
6992 clp->cl_mvops->mig_recovery_ops;
6993 struct nfs4_exception exception = { };
6994 int status;
6996 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6997 (unsigned long long)server->fsid.major,
6998 (unsigned long long)server->fsid.minor,
6999 clp->cl_hostname);
7000 nfs_display_fhandle(NFS_FH(inode), __func__);
7002 do {
7003 status = ops->fsid_present(inode, cred);
7004 if (status != -NFS4ERR_DELAY)
7005 break;
7006 nfs4_handle_exception(server, status, &exception);
7007 } while (exception.retry);
7008 return status;
7012 * If 'use_integrity' is true and the state managment nfs_client
7013 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7014 * and the machine credential as per RFC3530bis and RFC5661 Security
7015 * Considerations sections. Otherwise, just use the user cred with the
7016 * filesystem's rpc_client.
7018 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7020 int status;
7021 struct nfs4_secinfo_arg args = {
7022 .dir_fh = NFS_FH(dir),
7023 .name = name,
7025 struct nfs4_secinfo_res res = {
7026 .flavors = flavors,
7028 struct rpc_message msg = {
7029 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7030 .rpc_argp = &args,
7031 .rpc_resp = &res,
7033 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7034 struct rpc_cred *cred = NULL;
7036 if (use_integrity) {
7037 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7038 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7039 msg.rpc_cred = cred;
7042 dprintk("NFS call secinfo %s\n", name->name);
7044 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7045 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7047 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7048 &res.seq_res, 0);
7049 dprintk("NFS reply secinfo: %d\n", status);
7051 if (cred)
7052 put_rpccred(cred);
7054 return status;
7057 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7058 struct nfs4_secinfo_flavors *flavors)
7060 struct nfs4_exception exception = { };
7061 int err;
7062 do {
7063 err = -NFS4ERR_WRONGSEC;
7065 /* try to use integrity protection with machine cred */
7066 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7067 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7070 * if unable to use integrity protection, or SECINFO with
7071 * integrity protection returns NFS4ERR_WRONGSEC (which is
7072 * disallowed by spec, but exists in deployed servers) use
7073 * the current filesystem's rpc_client and the user cred.
7075 if (err == -NFS4ERR_WRONGSEC)
7076 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7078 trace_nfs4_secinfo(dir, name, err);
7079 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7080 &exception);
7081 } while (exception.retry);
7082 return err;
7085 #ifdef CONFIG_NFS_V4_1
7087 * Check the exchange flags returned by the server for invalid flags, having
7088 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7089 * DS flags set.
7091 static int nfs4_check_cl_exchange_flags(u32 flags)
7093 if (flags & ~EXCHGID4_FLAG_MASK_R)
7094 goto out_inval;
7095 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7096 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7097 goto out_inval;
7098 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7099 goto out_inval;
7100 return NFS_OK;
7101 out_inval:
7102 return -NFS4ERR_INVAL;
7105 static bool
7106 nfs41_same_server_scope(struct nfs41_server_scope *a,
7107 struct nfs41_server_scope *b)
7109 if (a->server_scope_sz == b->server_scope_sz &&
7110 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7111 return true;
7113 return false;
7116 static void
7117 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7121 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7122 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7126 * nfs4_proc_bind_one_conn_to_session()
7128 * The 4.1 client currently uses the same TCP connection for the
7129 * fore and backchannel.
7131 static
7132 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7133 struct rpc_xprt *xprt,
7134 struct nfs_client *clp,
7135 struct rpc_cred *cred)
7137 int status;
7138 struct nfs41_bind_conn_to_session_args args = {
7139 .client = clp,
7140 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7142 struct nfs41_bind_conn_to_session_res res;
7143 struct rpc_message msg = {
7144 .rpc_proc =
7145 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7146 .rpc_argp = &args,
7147 .rpc_resp = &res,
7148 .rpc_cred = cred,
7150 struct rpc_task_setup task_setup_data = {
7151 .rpc_client = clnt,
7152 .rpc_xprt = xprt,
7153 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7154 .rpc_message = &msg,
7155 .flags = RPC_TASK_TIMEOUT,
7157 struct rpc_task *task;
7159 dprintk("--> %s\n", __func__);
7161 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7162 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7163 args.dir = NFS4_CDFC4_FORE;
7165 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7166 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7167 args.dir = NFS4_CDFC4_FORE;
7169 task = rpc_run_task(&task_setup_data);
7170 if (!IS_ERR(task)) {
7171 status = task->tk_status;
7172 rpc_put_task(task);
7173 } else
7174 status = PTR_ERR(task);
7175 trace_nfs4_bind_conn_to_session(clp, status);
7176 if (status == 0) {
7177 if (memcmp(res.sessionid.data,
7178 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7179 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7180 status = -EIO;
7181 goto out;
7183 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7184 dprintk("NFS: %s: Unexpected direction from server\n",
7185 __func__);
7186 status = -EIO;
7187 goto out;
7189 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7190 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7191 __func__);
7192 status = -EIO;
7193 goto out;
7196 out:
7197 dprintk("<-- %s status= %d\n", __func__, status);
7198 return status;
7201 struct rpc_bind_conn_calldata {
7202 struct nfs_client *clp;
7203 struct rpc_cred *cred;
7206 static int
7207 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7208 struct rpc_xprt *xprt,
7209 void *calldata)
7211 struct rpc_bind_conn_calldata *p = calldata;
7213 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7216 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7218 struct rpc_bind_conn_calldata data = {
7219 .clp = clp,
7220 .cred = cred,
7222 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7223 nfs4_proc_bind_conn_to_session_callback, &data);
7227 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7228 * and operations we'd like to see to enable certain features in the allow map
7230 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7231 .how = SP4_MACH_CRED,
7232 .enforce.u.words = {
7233 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7234 1 << (OP_EXCHANGE_ID - 32) |
7235 1 << (OP_CREATE_SESSION - 32) |
7236 1 << (OP_DESTROY_SESSION - 32) |
7237 1 << (OP_DESTROY_CLIENTID - 32)
7239 .allow.u.words = {
7240 [0] = 1 << (OP_CLOSE) |
7241 1 << (OP_OPEN_DOWNGRADE) |
7242 1 << (OP_LOCKU) |
7243 1 << (OP_DELEGRETURN) |
7244 1 << (OP_COMMIT),
7245 [1] = 1 << (OP_SECINFO - 32) |
7246 1 << (OP_SECINFO_NO_NAME - 32) |
7247 1 << (OP_LAYOUTRETURN - 32) |
7248 1 << (OP_TEST_STATEID - 32) |
7249 1 << (OP_FREE_STATEID - 32) |
7250 1 << (OP_WRITE - 32)
7255 * Select the state protection mode for client `clp' given the server results
7256 * from exchange_id in `sp'.
7258 * Returns 0 on success, negative errno otherwise.
7260 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7261 struct nfs41_state_protection *sp)
7263 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7264 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7265 1 << (OP_EXCHANGE_ID - 32) |
7266 1 << (OP_CREATE_SESSION - 32) |
7267 1 << (OP_DESTROY_SESSION - 32) |
7268 1 << (OP_DESTROY_CLIENTID - 32)
7270 unsigned int i;
7272 if (sp->how == SP4_MACH_CRED) {
7273 /* Print state protect result */
7274 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7275 for (i = 0; i <= LAST_NFS4_OP; i++) {
7276 if (test_bit(i, sp->enforce.u.longs))
7277 dfprintk(MOUNT, " enforce op %d\n", i);
7278 if (test_bit(i, sp->allow.u.longs))
7279 dfprintk(MOUNT, " allow op %d\n", i);
7282 /* make sure nothing is on enforce list that isn't supported */
7283 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7284 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7285 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7286 return -EINVAL;
7291 * Minimal mode - state operations are allowed to use machine
7292 * credential. Note this already happens by default, so the
7293 * client doesn't have to do anything more than the negotiation.
7295 * NOTE: we don't care if EXCHANGE_ID is in the list -
7296 * we're already using the machine cred for exchange_id
7297 * and will never use a different cred.
7299 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7300 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7301 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7302 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7303 dfprintk(MOUNT, "sp4_mach_cred:\n");
7304 dfprintk(MOUNT, " minimal mode enabled\n");
7305 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7306 } else {
7307 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7308 return -EINVAL;
7311 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7312 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7313 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7314 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7315 dfprintk(MOUNT, " cleanup mode enabled\n");
7316 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7319 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7320 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7321 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7322 &clp->cl_sp4_flags);
7325 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7326 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7327 dfprintk(MOUNT, " secinfo mode enabled\n");
7328 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7331 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7332 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7333 dfprintk(MOUNT, " stateid mode enabled\n");
7334 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7337 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7338 dfprintk(MOUNT, " write mode enabled\n");
7339 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7342 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7343 dfprintk(MOUNT, " commit mode enabled\n");
7344 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7348 return 0;
7351 struct nfs41_exchange_id_data {
7352 struct nfs41_exchange_id_res res;
7353 struct nfs41_exchange_id_args args;
7354 struct rpc_xprt *xprt;
7355 int rpc_status;
7358 static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
7360 struct nfs41_exchange_id_data *cdata =
7361 (struct nfs41_exchange_id_data *)data;
7362 struct nfs_client *clp = cdata->args.client;
7363 int status = task->tk_status;
7365 trace_nfs4_exchange_id(clp, status);
7367 if (status == 0)
7368 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
7370 if (cdata->xprt && status == 0) {
7371 status = nfs4_detect_session_trunking(clp, &cdata->res,
7372 cdata->xprt);
7373 goto out;
7376 if (status == 0)
7377 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
7379 if (status == 0) {
7380 clp->cl_clientid = cdata->res.clientid;
7381 clp->cl_exchange_flags = cdata->res.flags;
7382 /* Client ID is not confirmed */
7383 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7384 clear_bit(NFS4_SESSION_ESTABLISHED,
7385 &clp->cl_session->session_state);
7386 clp->cl_seqid = cdata->res.seqid;
7389 kfree(clp->cl_serverowner);
7390 clp->cl_serverowner = cdata->res.server_owner;
7391 cdata->res.server_owner = NULL;
7393 /* use the most recent implementation id */
7394 kfree(clp->cl_implid);
7395 clp->cl_implid = cdata->res.impl_id;
7396 cdata->res.impl_id = NULL;
7398 if (clp->cl_serverscope != NULL &&
7399 !nfs41_same_server_scope(clp->cl_serverscope,
7400 cdata->res.server_scope)) {
7401 dprintk("%s: server_scope mismatch detected\n",
7402 __func__);
7403 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7404 kfree(clp->cl_serverscope);
7405 clp->cl_serverscope = NULL;
7408 if (clp->cl_serverscope == NULL) {
7409 clp->cl_serverscope = cdata->res.server_scope;
7410 cdata->res.server_scope = NULL;
7412 /* Save the EXCHANGE_ID verifier session trunk tests */
7413 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7414 sizeof(clp->cl_confirm.data));
7416 out:
7417 cdata->rpc_status = status;
7418 return;
7421 static void nfs4_exchange_id_release(void *data)
7423 struct nfs41_exchange_id_data *cdata =
7424 (struct nfs41_exchange_id_data *)data;
7426 if (cdata->xprt) {
7427 xprt_put(cdata->xprt);
7428 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7430 nfs_put_client(cdata->args.client);
7431 kfree(cdata->res.impl_id);
7432 kfree(cdata->res.server_scope);
7433 kfree(cdata->res.server_owner);
7434 kfree(cdata);
7437 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7438 .rpc_call_done = nfs4_exchange_id_done,
7439 .rpc_release = nfs4_exchange_id_release,
7443 * _nfs4_proc_exchange_id()
7445 * Wrapper for EXCHANGE_ID operation.
7447 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7448 u32 sp4_how, struct rpc_xprt *xprt)
7450 nfs4_verifier verifier;
7451 struct rpc_message msg = {
7452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7453 .rpc_cred = cred,
7455 struct rpc_task_setup task_setup_data = {
7456 .rpc_client = clp->cl_rpcclient,
7457 .callback_ops = &nfs4_exchange_id_call_ops,
7458 .rpc_message = &msg,
7459 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7461 struct nfs41_exchange_id_data *calldata;
7462 struct rpc_task *task;
7463 int status = -EIO;
7465 if (!atomic_inc_not_zero(&clp->cl_count))
7466 goto out;
7468 status = -ENOMEM;
7469 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7470 if (!calldata)
7471 goto out;
7473 if (!xprt)
7474 nfs4_init_boot_verifier(clp, &verifier);
7476 status = nfs4_init_uniform_client_string(clp);
7477 if (status)
7478 goto out_calldata;
7480 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7481 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7482 clp->cl_owner_id);
7484 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7485 GFP_NOFS);
7486 status = -ENOMEM;
7487 if (unlikely(calldata->res.server_owner == NULL))
7488 goto out_calldata;
7490 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7491 GFP_NOFS);
7492 if (unlikely(calldata->res.server_scope == NULL))
7493 goto out_server_owner;
7495 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7496 if (unlikely(calldata->res.impl_id == NULL))
7497 goto out_server_scope;
7499 switch (sp4_how) {
7500 case SP4_NONE:
7501 calldata->args.state_protect.how = SP4_NONE;
7502 break;
7504 case SP4_MACH_CRED:
7505 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
7506 break;
7508 default:
7509 /* unsupported! */
7510 WARN_ON_ONCE(1);
7511 status = -EINVAL;
7512 goto out_impl_id;
7514 if (xprt) {
7515 calldata->xprt = xprt;
7516 task_setup_data.rpc_xprt = xprt;
7517 task_setup_data.flags =
7518 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7519 calldata->args.verifier = &clp->cl_confirm;
7520 } else {
7521 calldata->args.verifier = &verifier;
7523 calldata->args.client = clp;
7524 #ifdef CONFIG_NFS_V4_1_MIGRATION
7525 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7526 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7527 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7528 #else
7529 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7530 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7531 #endif
7532 msg.rpc_argp = &calldata->args;
7533 msg.rpc_resp = &calldata->res;
7534 task_setup_data.callback_data = calldata;
7536 task = rpc_run_task(&task_setup_data);
7537 if (IS_ERR(task))
7538 return PTR_ERR(task);
7540 if (!xprt) {
7541 status = rpc_wait_for_completion_task(task);
7542 if (!status)
7543 status = calldata->rpc_status;
7544 } else /* session trunking test */
7545 status = calldata->rpc_status;
7547 rpc_put_task(task);
7548 out:
7549 if (clp->cl_implid != NULL)
7550 dprintk("NFS reply exchange_id: Server Implementation ID: "
7551 "domain: %s, name: %s, date: %llu,%u\n",
7552 clp->cl_implid->domain, clp->cl_implid->name,
7553 clp->cl_implid->date.seconds,
7554 clp->cl_implid->date.nseconds);
7555 dprintk("NFS reply exchange_id: %d\n", status);
7556 return status;
7558 out_impl_id:
7559 kfree(calldata->res.impl_id);
7560 out_server_scope:
7561 kfree(calldata->res.server_scope);
7562 out_server_owner:
7563 kfree(calldata->res.server_owner);
7564 out_calldata:
7565 kfree(calldata);
7566 nfs_put_client(clp);
7567 goto out;
7571 * nfs4_proc_exchange_id()
7573 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7575 * Since the clientid has expired, all compounds using sessions
7576 * associated with the stale clientid will be returning
7577 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7578 * be in some phase of session reset.
7580 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7582 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7584 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7585 int status;
7587 /* try SP4_MACH_CRED if krb5i/p */
7588 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7589 authflavor == RPC_AUTH_GSS_KRB5P) {
7590 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
7591 if (!status)
7592 return 0;
7595 /* try SP4_NONE */
7596 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
7600 * nfs4_test_session_trunk
7602 * This is an add_xprt_test() test function called from
7603 * rpc_clnt_setup_test_and_add_xprt.
7605 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7606 * and is dereferrenced in nfs4_exchange_id_release
7608 * Upon success, add the new transport to the rpc_clnt
7610 * @clnt: struct rpc_clnt to get new transport
7611 * @xprt: the rpc_xprt to test
7612 * @data: call data for _nfs4_proc_exchange_id.
7614 int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7615 void *data)
7617 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7618 u32 sp4_how;
7620 dprintk("--> %s try %s\n", __func__,
7621 xprt->address_strings[RPC_DISPLAY_ADDR]);
7623 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7625 /* Test connection for session trunking. Async exchange_id call */
7626 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7628 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7630 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7631 struct rpc_cred *cred)
7633 struct rpc_message msg = {
7634 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7635 .rpc_argp = clp,
7636 .rpc_cred = cred,
7638 int status;
7640 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7641 trace_nfs4_destroy_clientid(clp, status);
7642 if (status)
7643 dprintk("NFS: Got error %d from the server %s on "
7644 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7645 return status;
7648 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7649 struct rpc_cred *cred)
7651 unsigned int loop;
7652 int ret;
7654 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7655 ret = _nfs4_proc_destroy_clientid(clp, cred);
7656 switch (ret) {
7657 case -NFS4ERR_DELAY:
7658 case -NFS4ERR_CLIENTID_BUSY:
7659 ssleep(1);
7660 break;
7661 default:
7662 return ret;
7665 return 0;
7668 int nfs4_destroy_clientid(struct nfs_client *clp)
7670 struct rpc_cred *cred;
7671 int ret = 0;
7673 if (clp->cl_mvops->minor_version < 1)
7674 goto out;
7675 if (clp->cl_exchange_flags == 0)
7676 goto out;
7677 if (clp->cl_preserve_clid)
7678 goto out;
7679 cred = nfs4_get_clid_cred(clp);
7680 ret = nfs4_proc_destroy_clientid(clp, cred);
7681 if (cred)
7682 put_rpccred(cred);
7683 switch (ret) {
7684 case 0:
7685 case -NFS4ERR_STALE_CLIENTID:
7686 clp->cl_exchange_flags = 0;
7688 out:
7689 return ret;
7692 struct nfs4_get_lease_time_data {
7693 struct nfs4_get_lease_time_args *args;
7694 struct nfs4_get_lease_time_res *res;
7695 struct nfs_client *clp;
7698 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7699 void *calldata)
7701 struct nfs4_get_lease_time_data *data =
7702 (struct nfs4_get_lease_time_data *)calldata;
7704 dprintk("--> %s\n", __func__);
7705 /* just setup sequence, do not trigger session recovery
7706 since we're invoked within one */
7707 nfs41_setup_sequence(data->clp->cl_session,
7708 &data->args->la_seq_args,
7709 &data->res->lr_seq_res,
7710 task);
7711 dprintk("<-- %s\n", __func__);
7715 * Called from nfs4_state_manager thread for session setup, so don't recover
7716 * from sequence operation or clientid errors.
7718 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7720 struct nfs4_get_lease_time_data *data =
7721 (struct nfs4_get_lease_time_data *)calldata;
7723 dprintk("--> %s\n", __func__);
7724 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7725 return;
7726 switch (task->tk_status) {
7727 case -NFS4ERR_DELAY:
7728 case -NFS4ERR_GRACE:
7729 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7730 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7731 task->tk_status = 0;
7732 /* fall through */
7733 case -NFS4ERR_RETRY_UNCACHED_REP:
7734 rpc_restart_call_prepare(task);
7735 return;
7737 dprintk("<-- %s\n", __func__);
7740 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
7741 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7742 .rpc_call_done = nfs4_get_lease_time_done,
7745 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7747 struct rpc_task *task;
7748 struct nfs4_get_lease_time_args args;
7749 struct nfs4_get_lease_time_res res = {
7750 .lr_fsinfo = fsinfo,
7752 struct nfs4_get_lease_time_data data = {
7753 .args = &args,
7754 .res = &res,
7755 .clp = clp,
7757 struct rpc_message msg = {
7758 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7759 .rpc_argp = &args,
7760 .rpc_resp = &res,
7762 struct rpc_task_setup task_setup = {
7763 .rpc_client = clp->cl_rpcclient,
7764 .rpc_message = &msg,
7765 .callback_ops = &nfs4_get_lease_time_ops,
7766 .callback_data = &data,
7767 .flags = RPC_TASK_TIMEOUT,
7769 int status;
7771 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
7772 nfs4_set_sequence_privileged(&args.la_seq_args);
7773 dprintk("--> %s\n", __func__);
7774 task = rpc_run_task(&task_setup);
7776 if (IS_ERR(task))
7777 status = PTR_ERR(task);
7778 else {
7779 status = task->tk_status;
7780 rpc_put_task(task);
7782 dprintk("<-- %s return %d\n", __func__, status);
7784 return status;
7788 * Initialize the values to be used by the client in CREATE_SESSION
7789 * If nfs4_init_session set the fore channel request and response sizes,
7790 * use them.
7792 * Set the back channel max_resp_sz_cached to zero to force the client to
7793 * always set csa_cachethis to FALSE because the current implementation
7794 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7796 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7797 struct rpc_clnt *clnt)
7799 unsigned int max_rqst_sz, max_resp_sz;
7800 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
7802 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7803 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7805 /* Fore channel attributes */
7806 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7807 args->fc_attrs.max_resp_sz = max_resp_sz;
7808 args->fc_attrs.max_ops = NFS4_MAX_OPS;
7809 args->fc_attrs.max_reqs = max_session_slots;
7811 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
7812 "max_ops=%u max_reqs=%u\n",
7813 __func__,
7814 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
7815 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
7817 /* Back channel attributes */
7818 args->bc_attrs.max_rqst_sz = max_bc_payload;
7819 args->bc_attrs.max_resp_sz = max_bc_payload;
7820 args->bc_attrs.max_resp_sz_cached = 0;
7821 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
7822 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
7824 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7825 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7826 __func__,
7827 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7828 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7829 args->bc_attrs.max_reqs);
7832 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7833 struct nfs41_create_session_res *res)
7835 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7836 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
7838 if (rcvd->max_resp_sz > sent->max_resp_sz)
7839 return -EINVAL;
7841 * Our requested max_ops is the minimum we need; we're not
7842 * prepared to break up compounds into smaller pieces than that.
7843 * So, no point even trying to continue if the server won't
7844 * cooperate:
7846 if (rcvd->max_ops < sent->max_ops)
7847 return -EINVAL;
7848 if (rcvd->max_reqs == 0)
7849 return -EINVAL;
7850 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7851 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
7852 return 0;
7855 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7856 struct nfs41_create_session_res *res)
7858 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7859 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
7861 if (!(res->flags & SESSION4_BACK_CHAN))
7862 goto out;
7863 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7864 return -EINVAL;
7865 if (rcvd->max_resp_sz < sent->max_resp_sz)
7866 return -EINVAL;
7867 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7868 return -EINVAL;
7869 if (rcvd->max_ops > sent->max_ops)
7870 return -EINVAL;
7871 if (rcvd->max_reqs > sent->max_reqs)
7872 return -EINVAL;
7873 out:
7874 return 0;
7877 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7878 struct nfs41_create_session_res *res)
7880 int ret;
7882 ret = nfs4_verify_fore_channel_attrs(args, res);
7883 if (ret)
7884 return ret;
7885 return nfs4_verify_back_channel_attrs(args, res);
7888 static void nfs4_update_session(struct nfs4_session *session,
7889 struct nfs41_create_session_res *res)
7891 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
7892 /* Mark client id and session as being confirmed */
7893 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7894 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
7895 session->flags = res->flags;
7896 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
7897 if (res->flags & SESSION4_BACK_CHAN)
7898 memcpy(&session->bc_attrs, &res->bc_attrs,
7899 sizeof(session->bc_attrs));
7902 static int _nfs4_proc_create_session(struct nfs_client *clp,
7903 struct rpc_cred *cred)
7905 struct nfs4_session *session = clp->cl_session;
7906 struct nfs41_create_session_args args = {
7907 .client = clp,
7908 .clientid = clp->cl_clientid,
7909 .seqid = clp->cl_seqid,
7910 .cb_program = NFS4_CALLBACK,
7912 struct nfs41_create_session_res res;
7914 struct rpc_message msg = {
7915 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7916 .rpc_argp = &args,
7917 .rpc_resp = &res,
7918 .rpc_cred = cred,
7920 int status;
7922 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
7923 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
7925 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
7926 trace_nfs4_create_session(clp, status);
7928 switch (status) {
7929 case -NFS4ERR_STALE_CLIENTID:
7930 case -NFS4ERR_DELAY:
7931 case -ETIMEDOUT:
7932 case -EACCES:
7933 case -EAGAIN:
7934 goto out;
7937 clp->cl_seqid++;
7938 if (!status) {
7939 /* Verify the session's negotiated channel_attrs values */
7940 status = nfs4_verify_channel_attrs(&args, &res);
7941 /* Increment the clientid slot sequence id */
7942 if (status)
7943 goto out;
7944 nfs4_update_session(session, &res);
7946 out:
7947 return status;
7951 * Issues a CREATE_SESSION operation to the server.
7952 * It is the responsibility of the caller to verify the session is
7953 * expired before calling this routine.
7955 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
7957 int status;
7958 unsigned *ptr;
7959 struct nfs4_session *session = clp->cl_session;
7961 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7963 status = _nfs4_proc_create_session(clp, cred);
7964 if (status)
7965 goto out;
7967 /* Init or reset the session slot tables */
7968 status = nfs4_setup_session_slot_tables(session);
7969 dprintk("slot table setup returned %d\n", status);
7970 if (status)
7971 goto out;
7973 ptr = (unsigned *)&session->sess_id.data[0];
7974 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7975 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
7976 out:
7977 dprintk("<-- %s\n", __func__);
7978 return status;
7982 * Issue the over-the-wire RPC DESTROY_SESSION.
7983 * The caller must serialize access to this routine.
7985 int nfs4_proc_destroy_session(struct nfs4_session *session,
7986 struct rpc_cred *cred)
7988 struct rpc_message msg = {
7989 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7990 .rpc_argp = session,
7991 .rpc_cred = cred,
7993 int status = 0;
7995 dprintk("--> nfs4_proc_destroy_session\n");
7997 /* session is still being setup */
7998 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7999 return 0;
8001 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8002 trace_nfs4_destroy_session(session->clp, status);
8004 if (status)
8005 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8006 "Session has been destroyed regardless...\n", status);
8008 dprintk("<-- nfs4_proc_destroy_session\n");
8009 return status;
8013 * Renew the cl_session lease.
8015 struct nfs4_sequence_data {
8016 struct nfs_client *clp;
8017 struct nfs4_sequence_args args;
8018 struct nfs4_sequence_res res;
8021 static void nfs41_sequence_release(void *data)
8023 struct nfs4_sequence_data *calldata = data;
8024 struct nfs_client *clp = calldata->clp;
8026 if (atomic_read(&clp->cl_count) > 1)
8027 nfs4_schedule_state_renewal(clp);
8028 nfs_put_client(clp);
8029 kfree(calldata);
8032 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8034 switch(task->tk_status) {
8035 case -NFS4ERR_DELAY:
8036 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8037 return -EAGAIN;
8038 default:
8039 nfs4_schedule_lease_recovery(clp);
8041 return 0;
8044 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8046 struct nfs4_sequence_data *calldata = data;
8047 struct nfs_client *clp = calldata->clp;
8049 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8050 return;
8052 trace_nfs4_sequence(clp, task->tk_status);
8053 if (task->tk_status < 0) {
8054 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8055 if (atomic_read(&clp->cl_count) == 1)
8056 goto out;
8058 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8059 rpc_restart_call_prepare(task);
8060 return;
8063 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8064 out:
8065 dprintk("<-- %s\n", __func__);
8068 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8070 struct nfs4_sequence_data *calldata = data;
8071 struct nfs_client *clp = calldata->clp;
8072 struct nfs4_sequence_args *args;
8073 struct nfs4_sequence_res *res;
8075 args = task->tk_msg.rpc_argp;
8076 res = task->tk_msg.rpc_resp;
8078 nfs41_setup_sequence(clp->cl_session, args, res, task);
8081 static const struct rpc_call_ops nfs41_sequence_ops = {
8082 .rpc_call_done = nfs41_sequence_call_done,
8083 .rpc_call_prepare = nfs41_sequence_prepare,
8084 .rpc_release = nfs41_sequence_release,
8087 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8088 struct rpc_cred *cred,
8089 bool is_privileged)
8091 struct nfs4_sequence_data *calldata;
8092 struct rpc_message msg = {
8093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8094 .rpc_cred = cred,
8096 struct rpc_task_setup task_setup_data = {
8097 .rpc_client = clp->cl_rpcclient,
8098 .rpc_message = &msg,
8099 .callback_ops = &nfs41_sequence_ops,
8100 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8103 if (!atomic_inc_not_zero(&clp->cl_count))
8104 return ERR_PTR(-EIO);
8105 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8106 if (calldata == NULL) {
8107 nfs_put_client(clp);
8108 return ERR_PTR(-ENOMEM);
8110 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
8111 if (is_privileged)
8112 nfs4_set_sequence_privileged(&calldata->args);
8113 msg.rpc_argp = &calldata->args;
8114 msg.rpc_resp = &calldata->res;
8115 calldata->clp = clp;
8116 task_setup_data.callback_data = calldata;
8118 return rpc_run_task(&task_setup_data);
8121 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
8123 struct rpc_task *task;
8124 int ret = 0;
8126 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8127 return -EAGAIN;
8128 task = _nfs41_proc_sequence(clp, cred, false);
8129 if (IS_ERR(task))
8130 ret = PTR_ERR(task);
8131 else
8132 rpc_put_task_async(task);
8133 dprintk("<-- %s status=%d\n", __func__, ret);
8134 return ret;
8137 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8139 struct rpc_task *task;
8140 int ret;
8142 task = _nfs41_proc_sequence(clp, cred, true);
8143 if (IS_ERR(task)) {
8144 ret = PTR_ERR(task);
8145 goto out;
8147 ret = rpc_wait_for_completion_task(task);
8148 if (!ret)
8149 ret = task->tk_status;
8150 rpc_put_task(task);
8151 out:
8152 dprintk("<-- %s status=%d\n", __func__, ret);
8153 return ret;
8156 struct nfs4_reclaim_complete_data {
8157 struct nfs_client *clp;
8158 struct nfs41_reclaim_complete_args arg;
8159 struct nfs41_reclaim_complete_res res;
8162 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8164 struct nfs4_reclaim_complete_data *calldata = data;
8166 nfs41_setup_sequence(calldata->clp->cl_session,
8167 &calldata->arg.seq_args,
8168 &calldata->res.seq_res,
8169 task);
8172 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8174 switch(task->tk_status) {
8175 case 0:
8176 case -NFS4ERR_COMPLETE_ALREADY:
8177 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8178 break;
8179 case -NFS4ERR_DELAY:
8180 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8181 /* fall through */
8182 case -NFS4ERR_RETRY_UNCACHED_REP:
8183 return -EAGAIN;
8184 default:
8185 nfs4_schedule_lease_recovery(clp);
8187 return 0;
8190 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8192 struct nfs4_reclaim_complete_data *calldata = data;
8193 struct nfs_client *clp = calldata->clp;
8194 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8196 dprintk("--> %s\n", __func__);
8197 if (!nfs41_sequence_done(task, res))
8198 return;
8200 trace_nfs4_reclaim_complete(clp, task->tk_status);
8201 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8202 rpc_restart_call_prepare(task);
8203 return;
8205 dprintk("<-- %s\n", __func__);
8208 static void nfs4_free_reclaim_complete_data(void *data)
8210 struct nfs4_reclaim_complete_data *calldata = data;
8212 kfree(calldata);
8215 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8216 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8217 .rpc_call_done = nfs4_reclaim_complete_done,
8218 .rpc_release = nfs4_free_reclaim_complete_data,
8222 * Issue a global reclaim complete.
8224 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8225 struct rpc_cred *cred)
8227 struct nfs4_reclaim_complete_data *calldata;
8228 struct rpc_task *task;
8229 struct rpc_message msg = {
8230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8231 .rpc_cred = cred,
8233 struct rpc_task_setup task_setup_data = {
8234 .rpc_client = clp->cl_rpcclient,
8235 .rpc_message = &msg,
8236 .callback_ops = &nfs4_reclaim_complete_call_ops,
8237 .flags = RPC_TASK_ASYNC,
8239 int status = -ENOMEM;
8241 dprintk("--> %s\n", __func__);
8242 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8243 if (calldata == NULL)
8244 goto out;
8245 calldata->clp = clp;
8246 calldata->arg.one_fs = 0;
8248 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
8249 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
8250 msg.rpc_argp = &calldata->arg;
8251 msg.rpc_resp = &calldata->res;
8252 task_setup_data.callback_data = calldata;
8253 task = rpc_run_task(&task_setup_data);
8254 if (IS_ERR(task)) {
8255 status = PTR_ERR(task);
8256 goto out;
8258 status = nfs4_wait_for_completion_rpc_task(task);
8259 if (status == 0)
8260 status = task->tk_status;
8261 rpc_put_task(task);
8262 return 0;
8263 out:
8264 dprintk("<-- %s status=%d\n", __func__, status);
8265 return status;
8268 static void
8269 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8271 struct nfs4_layoutget *lgp = calldata;
8272 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8273 struct nfs4_session *session = nfs4_get_session(server);
8275 dprintk("--> %s\n", __func__);
8276 nfs41_setup_sequence(session, &lgp->args.seq_args,
8277 &lgp->res.seq_res, task);
8278 dprintk("<-- %s\n", __func__);
8281 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8283 struct nfs4_layoutget *lgp = calldata;
8285 dprintk("--> %s\n", __func__);
8286 nfs41_sequence_process(task, &lgp->res.seq_res);
8287 dprintk("<-- %s\n", __func__);
8290 static int
8291 nfs4_layoutget_handle_exception(struct rpc_task *task,
8292 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8294 struct inode *inode = lgp->args.inode;
8295 struct nfs_server *server = NFS_SERVER(inode);
8296 struct pnfs_layout_hdr *lo;
8297 int nfs4err = task->tk_status;
8298 int err, status = 0;
8299 LIST_HEAD(head);
8301 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8303 switch (nfs4err) {
8304 case 0:
8305 goto out;
8308 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8309 * on the file. set tk_status to -ENODATA to tell upper layer to
8310 * retry go inband.
8312 case -NFS4ERR_LAYOUTUNAVAILABLE:
8313 status = -ENODATA;
8314 goto out;
8316 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8317 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8319 case -NFS4ERR_BADLAYOUT:
8320 status = -EOVERFLOW;
8321 goto out;
8323 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8324 * (or clients) writing to the same RAID stripe except when
8325 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8327 * Treat it like we would RECALLCONFLICT -- we retry for a little
8328 * while, and then eventually give up.
8330 case -NFS4ERR_LAYOUTTRYLATER:
8331 if (lgp->args.minlength == 0) {
8332 status = -EOVERFLOW;
8333 goto out;
8335 status = -EBUSY;
8336 break;
8337 case -NFS4ERR_RECALLCONFLICT:
8338 status = -ERECALLCONFLICT;
8339 break;
8340 case -NFS4ERR_DELEG_REVOKED:
8341 case -NFS4ERR_ADMIN_REVOKED:
8342 case -NFS4ERR_EXPIRED:
8343 case -NFS4ERR_BAD_STATEID:
8344 exception->timeout = 0;
8345 spin_lock(&inode->i_lock);
8346 lo = NFS_I(inode)->layout;
8347 /* If the open stateid was bad, then recover it. */
8348 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8349 nfs4_stateid_match_other(&lgp->args.stateid,
8350 &lgp->args.ctx->state->stateid)) {
8351 spin_unlock(&inode->i_lock);
8352 exception->state = lgp->args.ctx->state;
8353 exception->stateid = &lgp->args.stateid;
8354 break;
8358 * Mark the bad layout state as invalid, then retry
8360 pnfs_mark_layout_stateid_invalid(lo, &head);
8361 spin_unlock(&inode->i_lock);
8362 pnfs_free_lseg_list(&head);
8363 status = -EAGAIN;
8364 goto out;
8367 nfs4_sequence_free_slot(&lgp->res.seq_res);
8368 err = nfs4_handle_exception(server, nfs4err, exception);
8369 if (!status) {
8370 if (exception->retry)
8371 status = -EAGAIN;
8372 else
8373 status = err;
8375 out:
8376 dprintk("<-- %s\n", __func__);
8377 return status;
8380 static size_t max_response_pages(struct nfs_server *server)
8382 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8383 return nfs_page_array_len(0, max_resp_sz);
8386 static void nfs4_free_pages(struct page **pages, size_t size)
8388 int i;
8390 if (!pages)
8391 return;
8393 for (i = 0; i < size; i++) {
8394 if (!pages[i])
8395 break;
8396 __free_page(pages[i]);
8398 kfree(pages);
8401 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8403 struct page **pages;
8404 int i;
8406 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8407 if (!pages) {
8408 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8409 return NULL;
8412 for (i = 0; i < size; i++) {
8413 pages[i] = alloc_page(gfp_flags);
8414 if (!pages[i]) {
8415 dprintk("%s: failed to allocate page\n", __func__);
8416 nfs4_free_pages(pages, size);
8417 return NULL;
8421 return pages;
8424 static void nfs4_layoutget_release(void *calldata)
8426 struct nfs4_layoutget *lgp = calldata;
8427 struct inode *inode = lgp->args.inode;
8428 struct nfs_server *server = NFS_SERVER(inode);
8429 size_t max_pages = max_response_pages(server);
8431 dprintk("--> %s\n", __func__);
8432 nfs4_sequence_free_slot(&lgp->res.seq_res);
8433 nfs4_free_pages(lgp->args.layout.pages, max_pages);
8434 pnfs_put_layout_hdr(NFS_I(inode)->layout);
8435 put_nfs_open_context(lgp->args.ctx);
8436 kfree(calldata);
8437 dprintk("<-- %s\n", __func__);
8440 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8441 .rpc_call_prepare = nfs4_layoutget_prepare,
8442 .rpc_call_done = nfs4_layoutget_done,
8443 .rpc_release = nfs4_layoutget_release,
8446 struct pnfs_layout_segment *
8447 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
8449 struct inode *inode = lgp->args.inode;
8450 struct nfs_server *server = NFS_SERVER(inode);
8451 size_t max_pages = max_response_pages(server);
8452 struct rpc_task *task;
8453 struct rpc_message msg = {
8454 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8455 .rpc_argp = &lgp->args,
8456 .rpc_resp = &lgp->res,
8457 .rpc_cred = lgp->cred,
8459 struct rpc_task_setup task_setup_data = {
8460 .rpc_client = server->client,
8461 .rpc_message = &msg,
8462 .callback_ops = &nfs4_layoutget_call_ops,
8463 .callback_data = lgp,
8464 .flags = RPC_TASK_ASYNC,
8466 struct pnfs_layout_segment *lseg = NULL;
8467 struct nfs4_exception exception = {
8468 .inode = inode,
8469 .timeout = *timeout,
8471 int status = 0;
8473 dprintk("--> %s\n", __func__);
8475 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8476 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8478 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8479 if (!lgp->args.layout.pages) {
8480 nfs4_layoutget_release(lgp);
8481 return ERR_PTR(-ENOMEM);
8483 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8485 lgp->res.layoutp = &lgp->args.layout;
8486 lgp->res.seq_res.sr_slot = NULL;
8487 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
8489 task = rpc_run_task(&task_setup_data);
8490 if (IS_ERR(task))
8491 return ERR_CAST(task);
8492 status = nfs4_wait_for_completion_rpc_task(task);
8493 if (status == 0) {
8494 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8495 *timeout = exception.timeout;
8498 trace_nfs4_layoutget(lgp->args.ctx,
8499 &lgp->args.range,
8500 &lgp->res.range,
8501 &lgp->res.stateid,
8502 status);
8504 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8505 if (status == 0 && lgp->res.layoutp->len)
8506 lseg = pnfs_layout_process(lgp);
8507 rpc_put_task(task);
8508 dprintk("<-- %s status=%d\n", __func__, status);
8509 if (status)
8510 return ERR_PTR(status);
8511 return lseg;
8514 static void
8515 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8517 struct nfs4_layoutreturn *lrp = calldata;
8519 dprintk("--> %s\n", __func__);
8520 nfs41_setup_sequence(lrp->clp->cl_session,
8521 &lrp->args.seq_args,
8522 &lrp->res.seq_res,
8523 task);
8526 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8528 struct nfs4_layoutreturn *lrp = calldata;
8529 struct nfs_server *server;
8531 dprintk("--> %s\n", __func__);
8533 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
8534 return;
8536 server = NFS_SERVER(lrp->args.inode);
8537 switch (task->tk_status) {
8538 default:
8539 task->tk_status = 0;
8540 case 0:
8541 break;
8542 case -NFS4ERR_DELAY:
8543 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
8544 break;
8545 nfs4_sequence_free_slot(&lrp->res.seq_res);
8546 rpc_restart_call_prepare(task);
8547 return;
8549 dprintk("<-- %s\n", __func__);
8552 static void nfs4_layoutreturn_release(void *calldata)
8554 struct nfs4_layoutreturn *lrp = calldata;
8555 struct pnfs_layout_hdr *lo = lrp->args.layout;
8556 LIST_HEAD(freeme);
8558 dprintk("--> %s\n", __func__);
8559 spin_lock(&lo->plh_inode->i_lock);
8560 if (lrp->res.lrs_present) {
8561 pnfs_mark_matching_lsegs_invalid(lo, &freeme,
8562 &lrp->args.range,
8563 be32_to_cpu(lrp->args.stateid.seqid));
8564 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
8565 } else
8566 pnfs_mark_layout_stateid_invalid(lo, &freeme);
8567 pnfs_clear_layoutreturn_waitbit(lo);
8568 spin_unlock(&lo->plh_inode->i_lock);
8569 nfs4_sequence_free_slot(&lrp->res.seq_res);
8570 pnfs_free_lseg_list(&freeme);
8571 pnfs_put_layout_hdr(lrp->args.layout);
8572 nfs_iput_and_deactive(lrp->inode);
8573 kfree(calldata);
8574 dprintk("<-- %s\n", __func__);
8577 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8578 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8579 .rpc_call_done = nfs4_layoutreturn_done,
8580 .rpc_release = nfs4_layoutreturn_release,
8583 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
8585 struct rpc_task *task;
8586 struct rpc_message msg = {
8587 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8588 .rpc_argp = &lrp->args,
8589 .rpc_resp = &lrp->res,
8590 .rpc_cred = lrp->cred,
8592 struct rpc_task_setup task_setup_data = {
8593 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
8594 .rpc_message = &msg,
8595 .callback_ops = &nfs4_layoutreturn_call_ops,
8596 .callback_data = lrp,
8598 int status = 0;
8600 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8601 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8602 &task_setup_data.rpc_client, &msg);
8604 dprintk("--> %s\n", __func__);
8605 if (!sync) {
8606 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8607 if (!lrp->inode) {
8608 nfs4_layoutreturn_release(lrp);
8609 return -EAGAIN;
8611 task_setup_data.flags |= RPC_TASK_ASYNC;
8613 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
8614 task = rpc_run_task(&task_setup_data);
8615 if (IS_ERR(task))
8616 return PTR_ERR(task);
8617 if (sync)
8618 status = task->tk_status;
8619 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
8620 dprintk("<-- %s status=%d\n", __func__, status);
8621 rpc_put_task(task);
8622 return status;
8625 static int
8626 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
8627 struct pnfs_device *pdev,
8628 struct rpc_cred *cred)
8630 struct nfs4_getdeviceinfo_args args = {
8631 .pdev = pdev,
8632 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8633 NOTIFY_DEVICEID4_DELETE,
8635 struct nfs4_getdeviceinfo_res res = {
8636 .pdev = pdev,
8638 struct rpc_message msg = {
8639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8640 .rpc_argp = &args,
8641 .rpc_resp = &res,
8642 .rpc_cred = cred,
8644 int status;
8646 dprintk("--> %s\n", __func__);
8647 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
8648 if (res.notification & ~args.notify_types)
8649 dprintk("%s: unsupported notification\n", __func__);
8650 if (res.notification != args.notify_types)
8651 pdev->nocache = 1;
8653 dprintk("<-- %s status=%d\n", __func__, status);
8655 return status;
8658 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8659 struct pnfs_device *pdev,
8660 struct rpc_cred *cred)
8662 struct nfs4_exception exception = { };
8663 int err;
8665 do {
8666 err = nfs4_handle_exception(server,
8667 _nfs4_proc_getdeviceinfo(server, pdev, cred),
8668 &exception);
8669 } while (exception.retry);
8670 return err;
8672 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8674 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8676 struct nfs4_layoutcommit_data *data = calldata;
8677 struct nfs_server *server = NFS_SERVER(data->args.inode);
8678 struct nfs4_session *session = nfs4_get_session(server);
8680 nfs41_setup_sequence(session,
8681 &data->args.seq_args,
8682 &data->res.seq_res,
8683 task);
8686 static void
8687 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8689 struct nfs4_layoutcommit_data *data = calldata;
8690 struct nfs_server *server = NFS_SERVER(data->args.inode);
8692 if (!nfs41_sequence_done(task, &data->res.seq_res))
8693 return;
8695 switch (task->tk_status) { /* Just ignore these failures */
8696 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8697 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8698 case -NFS4ERR_BADLAYOUT: /* no layout */
8699 case -NFS4ERR_GRACE: /* loca_recalim always false */
8700 task->tk_status = 0;
8701 case 0:
8702 break;
8703 default:
8704 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
8705 rpc_restart_call_prepare(task);
8706 return;
8711 static void nfs4_layoutcommit_release(void *calldata)
8713 struct nfs4_layoutcommit_data *data = calldata;
8715 pnfs_cleanup_layoutcommit(data);
8716 nfs_post_op_update_inode_force_wcc(data->args.inode,
8717 data->res.fattr);
8718 put_rpccred(data->cred);
8719 nfs_iput_and_deactive(data->inode);
8720 kfree(data);
8723 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8724 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8725 .rpc_call_done = nfs4_layoutcommit_done,
8726 .rpc_release = nfs4_layoutcommit_release,
8730 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
8732 struct rpc_message msg = {
8733 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8734 .rpc_argp = &data->args,
8735 .rpc_resp = &data->res,
8736 .rpc_cred = data->cred,
8738 struct rpc_task_setup task_setup_data = {
8739 .task = &data->task,
8740 .rpc_client = NFS_CLIENT(data->args.inode),
8741 .rpc_message = &msg,
8742 .callback_ops = &nfs4_layoutcommit_ops,
8743 .callback_data = data,
8745 struct rpc_task *task;
8746 int status = 0;
8748 dprintk("NFS: initiating layoutcommit call. sync %d "
8749 "lbw: %llu inode %lu\n", sync,
8750 data->args.lastbytewritten,
8751 data->args.inode->i_ino);
8753 if (!sync) {
8754 data->inode = nfs_igrab_and_active(data->args.inode);
8755 if (data->inode == NULL) {
8756 nfs4_layoutcommit_release(data);
8757 return -EAGAIN;
8759 task_setup_data.flags = RPC_TASK_ASYNC;
8761 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
8762 task = rpc_run_task(&task_setup_data);
8763 if (IS_ERR(task))
8764 return PTR_ERR(task);
8765 if (sync)
8766 status = task->tk_status;
8767 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
8768 dprintk("%s: status %d\n", __func__, status);
8769 rpc_put_task(task);
8770 return status;
8774 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8775 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8777 static int
8778 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8779 struct nfs_fsinfo *info,
8780 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8782 struct nfs41_secinfo_no_name_args args = {
8783 .style = SECINFO_STYLE_CURRENT_FH,
8785 struct nfs4_secinfo_res res = {
8786 .flavors = flavors,
8788 struct rpc_message msg = {
8789 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8790 .rpc_argp = &args,
8791 .rpc_resp = &res,
8793 struct rpc_clnt *clnt = server->client;
8794 struct rpc_cred *cred = NULL;
8795 int status;
8797 if (use_integrity) {
8798 clnt = server->nfs_client->cl_rpcclient;
8799 cred = nfs4_get_clid_cred(server->nfs_client);
8800 msg.rpc_cred = cred;
8803 dprintk("--> %s\n", __func__);
8804 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8805 &res.seq_res, 0);
8806 dprintk("<-- %s status=%d\n", __func__, status);
8808 if (cred)
8809 put_rpccred(cred);
8811 return status;
8814 static int
8815 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8816 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8818 struct nfs4_exception exception = { };
8819 int err;
8820 do {
8821 /* first try using integrity protection */
8822 err = -NFS4ERR_WRONGSEC;
8824 /* try to use integrity protection with machine cred */
8825 if (_nfs4_is_integrity_protected(server->nfs_client))
8826 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8827 flavors, true);
8830 * if unable to use integrity protection, or SECINFO with
8831 * integrity protection returns NFS4ERR_WRONGSEC (which is
8832 * disallowed by spec, but exists in deployed servers) use
8833 * the current filesystem's rpc_client and the user cred.
8835 if (err == -NFS4ERR_WRONGSEC)
8836 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8837 flavors, false);
8839 switch (err) {
8840 case 0:
8841 case -NFS4ERR_WRONGSEC:
8842 case -ENOTSUPP:
8843 goto out;
8844 default:
8845 err = nfs4_handle_exception(server, err, &exception);
8847 } while (exception.retry);
8848 out:
8849 return err;
8852 static int
8853 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8854 struct nfs_fsinfo *info)
8856 int err;
8857 struct page *page;
8858 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
8859 struct nfs4_secinfo_flavors *flavors;
8860 struct nfs4_secinfo4 *secinfo;
8861 int i;
8863 page = alloc_page(GFP_KERNEL);
8864 if (!page) {
8865 err = -ENOMEM;
8866 goto out;
8869 flavors = page_address(page);
8870 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8873 * Fall back on "guess and check" method if
8874 * the server doesn't support SECINFO_NO_NAME
8876 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
8877 err = nfs4_find_root_sec(server, fhandle, info);
8878 goto out_freepage;
8880 if (err)
8881 goto out_freepage;
8883 for (i = 0; i < flavors->num_flavors; i++) {
8884 secinfo = &flavors->flavors[i];
8886 switch (secinfo->flavor) {
8887 case RPC_AUTH_NULL:
8888 case RPC_AUTH_UNIX:
8889 case RPC_AUTH_GSS:
8890 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8891 &secinfo->flavor_info);
8892 break;
8893 default:
8894 flavor = RPC_AUTH_MAXFLAVOR;
8895 break;
8898 if (!nfs_auth_info_match(&server->auth_info, flavor))
8899 flavor = RPC_AUTH_MAXFLAVOR;
8901 if (flavor != RPC_AUTH_MAXFLAVOR) {
8902 err = nfs4_lookup_root_sec(server, fhandle,
8903 info, flavor);
8904 if (!err)
8905 break;
8909 if (flavor == RPC_AUTH_MAXFLAVOR)
8910 err = -EPERM;
8912 out_freepage:
8913 put_page(page);
8914 if (err == -EACCES)
8915 return -EPERM;
8916 out:
8917 return err;
8920 static int _nfs41_test_stateid(struct nfs_server *server,
8921 nfs4_stateid *stateid,
8922 struct rpc_cred *cred)
8924 int status;
8925 struct nfs41_test_stateid_args args = {
8926 .stateid = stateid,
8928 struct nfs41_test_stateid_res res;
8929 struct rpc_message msg = {
8930 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8931 .rpc_argp = &args,
8932 .rpc_resp = &res,
8933 .rpc_cred = cred,
8935 struct rpc_clnt *rpc_client = server->client;
8937 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8938 &rpc_client, &msg);
8940 dprintk("NFS call test_stateid %p\n", stateid);
8941 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
8942 nfs4_set_sequence_privileged(&args.seq_args);
8943 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
8944 &args.seq_args, &res.seq_res);
8945 if (status != NFS_OK) {
8946 dprintk("NFS reply test_stateid: failed, %d\n", status);
8947 return status;
8949 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
8950 return -res.status;
8953 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8954 int err, struct nfs4_exception *exception)
8956 exception->retry = 0;
8957 switch(err) {
8958 case -NFS4ERR_DELAY:
8959 case -NFS4ERR_RETRY_UNCACHED_REP:
8960 nfs4_handle_exception(server, err, exception);
8961 break;
8962 case -NFS4ERR_BADSESSION:
8963 case -NFS4ERR_BADSLOT:
8964 case -NFS4ERR_BAD_HIGH_SLOT:
8965 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8966 case -NFS4ERR_DEADSESSION:
8967 nfs4_do_handle_exception(server, err, exception);
8972 * nfs41_test_stateid - perform a TEST_STATEID operation
8974 * @server: server / transport on which to perform the operation
8975 * @stateid: state ID to test
8976 * @cred: credential
8978 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8979 * Otherwise a negative NFS4ERR value is returned if the operation
8980 * failed or the state ID is not currently valid.
8982 static int nfs41_test_stateid(struct nfs_server *server,
8983 nfs4_stateid *stateid,
8984 struct rpc_cred *cred)
8986 struct nfs4_exception exception = { };
8987 int err;
8988 do {
8989 err = _nfs41_test_stateid(server, stateid, cred);
8990 nfs4_handle_delay_or_session_error(server, err, &exception);
8991 } while (exception.retry);
8992 return err;
8995 struct nfs_free_stateid_data {
8996 struct nfs_server *server;
8997 struct nfs41_free_stateid_args args;
8998 struct nfs41_free_stateid_res res;
9001 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9003 struct nfs_free_stateid_data *data = calldata;
9004 nfs41_setup_sequence(nfs4_get_session(data->server),
9005 &data->args.seq_args,
9006 &data->res.seq_res,
9007 task);
9010 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9012 struct nfs_free_stateid_data *data = calldata;
9014 nfs41_sequence_done(task, &data->res.seq_res);
9016 switch (task->tk_status) {
9017 case -NFS4ERR_DELAY:
9018 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9019 rpc_restart_call_prepare(task);
9023 static void nfs41_free_stateid_release(void *calldata)
9025 kfree(calldata);
9028 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9029 .rpc_call_prepare = nfs41_free_stateid_prepare,
9030 .rpc_call_done = nfs41_free_stateid_done,
9031 .rpc_release = nfs41_free_stateid_release,
9034 static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
9035 const nfs4_stateid *stateid,
9036 struct rpc_cred *cred,
9037 bool privileged)
9039 struct rpc_message msg = {
9040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9041 .rpc_cred = cred,
9043 struct rpc_task_setup task_setup = {
9044 .rpc_client = server->client,
9045 .rpc_message = &msg,
9046 .callback_ops = &nfs41_free_stateid_ops,
9047 .flags = RPC_TASK_ASYNC,
9049 struct nfs_free_stateid_data *data;
9051 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9052 &task_setup.rpc_client, &msg);
9054 dprintk("NFS call free_stateid %p\n", stateid);
9055 data = kmalloc(sizeof(*data), GFP_NOFS);
9056 if (!data)
9057 return ERR_PTR(-ENOMEM);
9058 data->server = server;
9059 nfs4_stateid_copy(&data->args.stateid, stateid);
9061 task_setup.callback_data = data;
9063 msg.rpc_argp = &data->args;
9064 msg.rpc_resp = &data->res;
9065 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
9066 if (privileged)
9067 nfs4_set_sequence_privileged(&data->args.seq_args);
9069 return rpc_run_task(&task_setup);
9073 * nfs41_free_stateid - perform a FREE_STATEID operation
9075 * @server: server / transport on which to perform the operation
9076 * @stateid: state ID to release
9077 * @cred: credential
9078 * @is_recovery: set to true if this call needs to be privileged
9080 * Note: this function is always asynchronous.
9082 static int nfs41_free_stateid(struct nfs_server *server,
9083 const nfs4_stateid *stateid,
9084 struct rpc_cred *cred,
9085 bool is_recovery)
9087 struct rpc_task *task;
9089 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
9090 if (IS_ERR(task))
9091 return PTR_ERR(task);
9092 rpc_put_task(task);
9093 return 0;
9096 static void
9097 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9099 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
9101 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9102 nfs4_free_lock_state(server, lsp);
9105 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9106 const nfs4_stateid *s2)
9108 if (s1->type != s2->type)
9109 return false;
9111 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9112 return false;
9114 if (s1->seqid == s2->seqid)
9115 return true;
9116 if (s1->seqid == 0 || s2->seqid == 0)
9117 return true;
9119 return false;
9122 #endif /* CONFIG_NFS_V4_1 */
9124 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9125 const nfs4_stateid *s2)
9127 return nfs4_stateid_match(s1, s2);
9131 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9132 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9133 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9134 .recover_open = nfs4_open_reclaim,
9135 .recover_lock = nfs4_lock_reclaim,
9136 .establish_clid = nfs4_init_clientid,
9137 .detect_trunking = nfs40_discover_server_trunking,
9140 #if defined(CONFIG_NFS_V4_1)
9141 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9142 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9143 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9144 .recover_open = nfs4_open_reclaim,
9145 .recover_lock = nfs4_lock_reclaim,
9146 .establish_clid = nfs41_init_clientid,
9147 .reclaim_complete = nfs41_proc_reclaim_complete,
9148 .detect_trunking = nfs41_discover_server_trunking,
9150 #endif /* CONFIG_NFS_V4_1 */
9152 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9153 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9154 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9155 .recover_open = nfs40_open_expired,
9156 .recover_lock = nfs4_lock_expired,
9157 .establish_clid = nfs4_init_clientid,
9160 #if defined(CONFIG_NFS_V4_1)
9161 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9162 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9163 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9164 .recover_open = nfs41_open_expired,
9165 .recover_lock = nfs41_lock_expired,
9166 .establish_clid = nfs41_init_clientid,
9168 #endif /* CONFIG_NFS_V4_1 */
9170 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9171 .sched_state_renewal = nfs4_proc_async_renew,
9172 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
9173 .renew_lease = nfs4_proc_renew,
9176 #if defined(CONFIG_NFS_V4_1)
9177 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9178 .sched_state_renewal = nfs41_proc_async_sequence,
9179 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
9180 .renew_lease = nfs4_proc_sequence,
9182 #endif
9184 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9185 .get_locations = _nfs40_proc_get_locations,
9186 .fsid_present = _nfs40_proc_fsid_present,
9189 #if defined(CONFIG_NFS_V4_1)
9190 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9191 .get_locations = _nfs41_proc_get_locations,
9192 .fsid_present = _nfs41_proc_fsid_present,
9194 #endif /* CONFIG_NFS_V4_1 */
9196 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9197 .minor_version = 0,
9198 .init_caps = NFS_CAP_READDIRPLUS
9199 | NFS_CAP_ATOMIC_OPEN
9200 | NFS_CAP_POSIX_LOCK,
9201 .init_client = nfs40_init_client,
9202 .shutdown_client = nfs40_shutdown_client,
9203 .match_stateid = nfs4_match_stateid,
9204 .find_root_sec = nfs4_find_root_sec,
9205 .free_lock_state = nfs4_release_lockowner,
9206 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9207 .alloc_seqid = nfs_alloc_seqid,
9208 .call_sync_ops = &nfs40_call_sync_ops,
9209 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9210 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9211 .state_renewal_ops = &nfs40_state_renewal_ops,
9212 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9215 #if defined(CONFIG_NFS_V4_1)
9216 static struct nfs_seqid *
9217 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9219 return NULL;
9222 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9223 .minor_version = 1,
9224 .init_caps = NFS_CAP_READDIRPLUS
9225 | NFS_CAP_ATOMIC_OPEN
9226 | NFS_CAP_POSIX_LOCK
9227 | NFS_CAP_STATEID_NFSV41
9228 | NFS_CAP_ATOMIC_OPEN_V1,
9229 .init_client = nfs41_init_client,
9230 .shutdown_client = nfs41_shutdown_client,
9231 .match_stateid = nfs41_match_stateid,
9232 .find_root_sec = nfs41_find_root_sec,
9233 .free_lock_state = nfs41_free_lock_state,
9234 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9235 .alloc_seqid = nfs_alloc_no_seqid,
9236 .session_trunk = nfs4_test_session_trunk,
9237 .call_sync_ops = &nfs41_call_sync_ops,
9238 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9239 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9240 .state_renewal_ops = &nfs41_state_renewal_ops,
9241 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9243 #endif
9245 #if defined(CONFIG_NFS_V4_2)
9246 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9247 .minor_version = 2,
9248 .init_caps = NFS_CAP_READDIRPLUS
9249 | NFS_CAP_ATOMIC_OPEN
9250 | NFS_CAP_POSIX_LOCK
9251 | NFS_CAP_STATEID_NFSV41
9252 | NFS_CAP_ATOMIC_OPEN_V1
9253 | NFS_CAP_ALLOCATE
9254 | NFS_CAP_COPY
9255 | NFS_CAP_DEALLOCATE
9256 | NFS_CAP_SEEK
9257 | NFS_CAP_LAYOUTSTATS
9258 | NFS_CAP_CLONE,
9259 .init_client = nfs41_init_client,
9260 .shutdown_client = nfs41_shutdown_client,
9261 .match_stateid = nfs41_match_stateid,
9262 .find_root_sec = nfs41_find_root_sec,
9263 .free_lock_state = nfs41_free_lock_state,
9264 .call_sync_ops = &nfs41_call_sync_ops,
9265 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9266 .alloc_seqid = nfs_alloc_no_seqid,
9267 .session_trunk = nfs4_test_session_trunk,
9268 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9269 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9270 .state_renewal_ops = &nfs41_state_renewal_ops,
9271 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9273 #endif
9275 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9276 [0] = &nfs_v4_0_minor_ops,
9277 #if defined(CONFIG_NFS_V4_1)
9278 [1] = &nfs_v4_1_minor_ops,
9279 #endif
9280 #if defined(CONFIG_NFS_V4_2)
9281 [2] = &nfs_v4_2_minor_ops,
9282 #endif
9285 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9287 ssize_t error, error2;
9289 error = generic_listxattr(dentry, list, size);
9290 if (error < 0)
9291 return error;
9292 if (list) {
9293 list += error;
9294 size -= error;
9297 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9298 if (error2 < 0)
9299 return error2;
9300 return error + error2;
9303 static const struct inode_operations nfs4_dir_inode_operations = {
9304 .create = nfs_create,
9305 .lookup = nfs_lookup,
9306 .atomic_open = nfs_atomic_open,
9307 .link = nfs_link,
9308 .unlink = nfs_unlink,
9309 .symlink = nfs_symlink,
9310 .mkdir = nfs_mkdir,
9311 .rmdir = nfs_rmdir,
9312 .mknod = nfs_mknod,
9313 .rename = nfs_rename,
9314 .permission = nfs_permission,
9315 .getattr = nfs_getattr,
9316 .setattr = nfs_setattr,
9317 .listxattr = nfs4_listxattr,
9320 static const struct inode_operations nfs4_file_inode_operations = {
9321 .permission = nfs_permission,
9322 .getattr = nfs_getattr,
9323 .setattr = nfs_setattr,
9324 .listxattr = nfs4_listxattr,
9327 const struct nfs_rpc_ops nfs_v4_clientops = {
9328 .version = 4, /* protocol version */
9329 .dentry_ops = &nfs4_dentry_operations,
9330 .dir_inode_ops = &nfs4_dir_inode_operations,
9331 .file_inode_ops = &nfs4_file_inode_operations,
9332 .file_ops = &nfs4_file_operations,
9333 .getroot = nfs4_proc_get_root,
9334 .submount = nfs4_submount,
9335 .try_mount = nfs4_try_mount,
9336 .getattr = nfs4_proc_getattr,
9337 .setattr = nfs4_proc_setattr,
9338 .lookup = nfs4_proc_lookup,
9339 .access = nfs4_proc_access,
9340 .readlink = nfs4_proc_readlink,
9341 .create = nfs4_proc_create,
9342 .remove = nfs4_proc_remove,
9343 .unlink_setup = nfs4_proc_unlink_setup,
9344 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9345 .unlink_done = nfs4_proc_unlink_done,
9346 .rename_setup = nfs4_proc_rename_setup,
9347 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9348 .rename_done = nfs4_proc_rename_done,
9349 .link = nfs4_proc_link,
9350 .symlink = nfs4_proc_symlink,
9351 .mkdir = nfs4_proc_mkdir,
9352 .rmdir = nfs4_proc_remove,
9353 .readdir = nfs4_proc_readdir,
9354 .mknod = nfs4_proc_mknod,
9355 .statfs = nfs4_proc_statfs,
9356 .fsinfo = nfs4_proc_fsinfo,
9357 .pathconf = nfs4_proc_pathconf,
9358 .set_capabilities = nfs4_server_capabilities,
9359 .decode_dirent = nfs4_decode_dirent,
9360 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9361 .read_setup = nfs4_proc_read_setup,
9362 .read_done = nfs4_read_done,
9363 .write_setup = nfs4_proc_write_setup,
9364 .write_done = nfs4_write_done,
9365 .commit_setup = nfs4_proc_commit_setup,
9366 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9367 .commit_done = nfs4_commit_done,
9368 .lock = nfs4_proc_lock,
9369 .clear_acl_cache = nfs4_zap_acl_attr,
9370 .close_context = nfs4_close_context,
9371 .open_context = nfs4_atomic_open,
9372 .have_delegation = nfs4_have_delegation,
9373 .return_delegation = nfs4_inode_return_delegation,
9374 .alloc_client = nfs4_alloc_client,
9375 .init_client = nfs4_init_client,
9376 .free_client = nfs4_free_client,
9377 .create_server = nfs4_create_server,
9378 .clone_server = nfs_clone_server,
9381 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9382 .name = XATTR_NAME_NFSV4_ACL,
9383 .list = nfs4_xattr_list_nfs4_acl,
9384 .get = nfs4_xattr_get_nfs4_acl,
9385 .set = nfs4_xattr_set_nfs4_acl,
9388 const struct xattr_handler *nfs4_xattr_handlers[] = {
9389 &nfs4_xattr_nfs4_acl_handler,
9390 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9391 &nfs4_xattr_nfs4_label_handler,
9392 #endif
9393 NULL
9397 * Local variables:
9398 * c-basic-offset: 8
9399 * End: