Linux 4.19.133
[linux/fpc-iii.git] / fs / nfs / nfs4proc.c
blob05cb68ca1ba1a29267fe107ac3cefd00df9dccc3
1 /*
2 * fs/nfs/nfs4proc.c
4 * Client-side procedure declarations for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <linux/mm.h>
39 #include <linux/delay.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/ratelimit.h>
43 #include <linux/printk.h>
44 #include <linux/slab.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/nfs.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfs_fs.h>
49 #include <linux/nfs_page.h>
50 #include <linux/nfs_mount.h>
51 #include <linux/namei.h>
52 #include <linux/mount.h>
53 #include <linux/module.h>
54 #include <linux/xattr.h>
55 #include <linux/utsname.h>
56 #include <linux/freezer.h>
57 #include <linux/iversion.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_BITMASK_SZ 3
76 #define NFS4_POLL_RETRY_MIN (HZ/10)
77 #define NFS4_POLL_RETRY_MAX (15*HZ)
79 /* file attributes which can be mapped to nfs attributes */
80 #define NFS4_VALID_ATTRS (ATTR_MODE \
81 | ATTR_UID \
82 | ATTR_GID \
83 | ATTR_SIZE \
84 | ATTR_ATIME \
85 | ATTR_MTIME \
86 | ATTR_CTIME \
87 | ATTR_ATIME_SET \
88 | ATTR_MTIME_SET)
90 struct nfs4_opendata;
91 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
92 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
93 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
94 static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label, struct inode *inode);
95 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
96 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
97 struct nfs_fattr *fattr, struct iattr *sattr,
98 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
99 struct nfs4_label *olabel);
100 #ifdef CONFIG_NFS_V4_1
101 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
102 struct rpc_cred *cred,
103 struct nfs4_slot *slot,
104 bool is_privileged);
105 static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
106 struct rpc_cred *);
107 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
108 struct rpc_cred *, bool);
109 #endif
111 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
112 static inline struct nfs4_label *
113 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
114 struct iattr *sattr, struct nfs4_label *label)
116 int err;
118 if (label == NULL)
119 return NULL;
121 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
122 return NULL;
124 err = security_dentry_init_security(dentry, sattr->ia_mode,
125 &dentry->d_name, (void **)&label->label, &label->len);
126 if (err == 0)
127 return label;
129 return NULL;
131 static inline void
132 nfs4_label_release_security(struct nfs4_label *label)
134 if (label)
135 security_release_secctx(label->label, label->len);
137 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
139 if (label)
140 return server->attr_bitmask;
142 return server->attr_bitmask_nl;
144 #else
145 static inline struct nfs4_label *
146 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
147 struct iattr *sattr, struct nfs4_label *l)
148 { return NULL; }
149 static inline void
150 nfs4_label_release_security(struct nfs4_label *label)
151 { return; }
152 static inline u32 *
153 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
154 { return server->attr_bitmask; }
155 #endif
157 /* Prevent leaks of NFSv4 errors into userland */
158 static int nfs4_map_errors(int err)
160 if (err >= -1000)
161 return err;
162 switch (err) {
163 case -NFS4ERR_RESOURCE:
164 case -NFS4ERR_LAYOUTTRYLATER:
165 case -NFS4ERR_RECALLCONFLICT:
166 return -EREMOTEIO;
167 case -NFS4ERR_WRONGSEC:
168 case -NFS4ERR_WRONG_CRED:
169 return -EPERM;
170 case -NFS4ERR_BADOWNER:
171 case -NFS4ERR_BADNAME:
172 return -EINVAL;
173 case -NFS4ERR_SHARE_DENIED:
174 return -EACCES;
175 case -NFS4ERR_MINOR_VERS_MISMATCH:
176 return -EPROTONOSUPPORT;
177 case -NFS4ERR_FILE_OPEN:
178 return -EBUSY;
179 default:
180 dprintk("%s could not handle NFSv4 error %d\n",
181 __func__, -err);
182 break;
184 return -EIO;
188 * This is our standard bitmap for GETATTR requests.
190 const u32 nfs4_fattr_bitmap[3] = {
191 FATTR4_WORD0_TYPE
192 | FATTR4_WORD0_CHANGE
193 | FATTR4_WORD0_SIZE
194 | FATTR4_WORD0_FSID
195 | FATTR4_WORD0_FILEID,
196 FATTR4_WORD1_MODE
197 | FATTR4_WORD1_NUMLINKS
198 | FATTR4_WORD1_OWNER
199 | FATTR4_WORD1_OWNER_GROUP
200 | FATTR4_WORD1_RAWDEV
201 | FATTR4_WORD1_SPACE_USED
202 | FATTR4_WORD1_TIME_ACCESS
203 | FATTR4_WORD1_TIME_METADATA
204 | FATTR4_WORD1_TIME_MODIFY
205 | FATTR4_WORD1_MOUNTED_ON_FILEID,
206 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
207 FATTR4_WORD2_SECURITY_LABEL
208 #endif
211 static const u32 nfs4_pnfs_open_bitmap[3] = {
212 FATTR4_WORD0_TYPE
213 | FATTR4_WORD0_CHANGE
214 | FATTR4_WORD0_SIZE
215 | FATTR4_WORD0_FSID
216 | FATTR4_WORD0_FILEID,
217 FATTR4_WORD1_MODE
218 | FATTR4_WORD1_NUMLINKS
219 | FATTR4_WORD1_OWNER
220 | FATTR4_WORD1_OWNER_GROUP
221 | FATTR4_WORD1_RAWDEV
222 | FATTR4_WORD1_SPACE_USED
223 | FATTR4_WORD1_TIME_ACCESS
224 | FATTR4_WORD1_TIME_METADATA
225 | FATTR4_WORD1_TIME_MODIFY,
226 FATTR4_WORD2_MDSTHRESHOLD
227 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
228 | FATTR4_WORD2_SECURITY_LABEL
229 #endif
232 static const u32 nfs4_open_noattr_bitmap[3] = {
233 FATTR4_WORD0_TYPE
234 | FATTR4_WORD0_FILEID,
237 const u32 nfs4_statfs_bitmap[3] = {
238 FATTR4_WORD0_FILES_AVAIL
239 | FATTR4_WORD0_FILES_FREE
240 | FATTR4_WORD0_FILES_TOTAL,
241 FATTR4_WORD1_SPACE_AVAIL
242 | FATTR4_WORD1_SPACE_FREE
243 | FATTR4_WORD1_SPACE_TOTAL
246 const u32 nfs4_pathconf_bitmap[3] = {
247 FATTR4_WORD0_MAXLINK
248 | FATTR4_WORD0_MAXNAME,
252 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
253 | FATTR4_WORD0_MAXREAD
254 | FATTR4_WORD0_MAXWRITE
255 | FATTR4_WORD0_LEASE_TIME,
256 FATTR4_WORD1_TIME_DELTA
257 | FATTR4_WORD1_FS_LAYOUT_TYPES,
258 FATTR4_WORD2_LAYOUT_BLKSIZE
259 | FATTR4_WORD2_CLONE_BLKSIZE
262 const u32 nfs4_fs_locations_bitmap[3] = {
263 FATTR4_WORD0_CHANGE
264 | FATTR4_WORD0_SIZE
265 | FATTR4_WORD0_FSID
266 | FATTR4_WORD0_FILEID
267 | FATTR4_WORD0_FS_LOCATIONS,
268 FATTR4_WORD1_OWNER
269 | FATTR4_WORD1_OWNER_GROUP
270 | FATTR4_WORD1_RAWDEV
271 | FATTR4_WORD1_SPACE_USED
272 | FATTR4_WORD1_TIME_ACCESS
273 | FATTR4_WORD1_TIME_METADATA
274 | FATTR4_WORD1_TIME_MODIFY
275 | FATTR4_WORD1_MOUNTED_ON_FILEID,
278 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
279 struct inode *inode)
281 unsigned long cache_validity;
283 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
284 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
285 return;
287 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
288 if (!(cache_validity & NFS_INO_REVAL_FORCED))
289 cache_validity &= ~(NFS_INO_INVALID_CHANGE
290 | NFS_INO_INVALID_SIZE);
292 if (!(cache_validity & NFS_INO_INVALID_SIZE))
293 dst[0] &= ~FATTR4_WORD0_SIZE;
295 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
296 dst[0] &= ~FATTR4_WORD0_CHANGE;
299 static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
300 const __u32 *src, struct inode *inode)
302 nfs4_bitmap_copy_adjust(dst, src, inode);
305 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
306 struct nfs4_readdir_arg *readdir)
308 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
309 __be32 *start, *p;
311 if (cookie > 2) {
312 readdir->cookie = cookie;
313 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
314 return;
317 readdir->cookie = 0;
318 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
319 if (cookie == 2)
320 return;
323 * NFSv4 servers do not return entries for '.' and '..'
324 * Therefore, we fake these entries here. We let '.'
325 * have cookie 0 and '..' have cookie 1. Note that
326 * when talking to the server, we always send cookie 0
327 * instead of 1 or 2.
329 start = p = kmap_atomic(*readdir->pages);
331 if (cookie == 0) {
332 *p++ = xdr_one; /* next */
333 *p++ = xdr_zero; /* cookie, first word */
334 *p++ = xdr_one; /* cookie, second word */
335 *p++ = xdr_one; /* entry len */
336 memcpy(p, ".\0\0\0", 4); /* entry */
337 p++;
338 *p++ = xdr_one; /* bitmap length */
339 *p++ = htonl(attrs); /* bitmap */
340 *p++ = htonl(12); /* attribute buffer length */
341 *p++ = htonl(NF4DIR);
342 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
345 *p++ = xdr_one; /* next */
346 *p++ = xdr_zero; /* cookie, first word */
347 *p++ = xdr_two; /* cookie, second word */
348 *p++ = xdr_two; /* entry len */
349 memcpy(p, "..\0\0", 4); /* entry */
350 p++;
351 *p++ = xdr_one; /* bitmap length */
352 *p++ = htonl(attrs); /* bitmap */
353 *p++ = htonl(12); /* attribute buffer length */
354 *p++ = htonl(NF4DIR);
355 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
357 readdir->pgbase = (char *)p - (char *)start;
358 readdir->count -= readdir->pgbase;
359 kunmap_atomic(start);
362 static void nfs4_test_and_free_stateid(struct nfs_server *server,
363 nfs4_stateid *stateid,
364 struct rpc_cred *cred)
366 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
368 ops->test_and_free_expired(server, stateid, cred);
371 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
372 nfs4_stateid *stateid,
373 struct rpc_cred *cred)
375 stateid->type = NFS4_REVOKED_STATEID_TYPE;
376 nfs4_test_and_free_stateid(server, stateid, cred);
379 static void nfs4_free_revoked_stateid(struct nfs_server *server,
380 const nfs4_stateid *stateid,
381 struct rpc_cred *cred)
383 nfs4_stateid tmp;
385 nfs4_stateid_copy(&tmp, stateid);
386 __nfs4_free_revoked_stateid(server, &tmp, cred);
389 static long nfs4_update_delay(long *timeout)
391 long ret;
392 if (!timeout)
393 return NFS4_POLL_RETRY_MAX;
394 if (*timeout <= 0)
395 *timeout = NFS4_POLL_RETRY_MIN;
396 if (*timeout > NFS4_POLL_RETRY_MAX)
397 *timeout = NFS4_POLL_RETRY_MAX;
398 ret = *timeout;
399 *timeout <<= 1;
400 return ret;
403 static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
405 int res = 0;
407 might_sleep();
409 freezable_schedule_timeout_killable_unsafe(
410 nfs4_update_delay(timeout));
411 if (fatal_signal_pending(current))
412 res = -ERESTARTSYS;
413 return res;
416 /* This is the error handling routine for processes that are allowed
417 * to sleep.
419 static int nfs4_do_handle_exception(struct nfs_server *server,
420 int errorcode, struct nfs4_exception *exception)
422 struct nfs_client *clp = server->nfs_client;
423 struct nfs4_state *state = exception->state;
424 const nfs4_stateid *stateid = exception->stateid;
425 struct inode *inode = exception->inode;
426 int ret = errorcode;
428 exception->delay = 0;
429 exception->recovering = 0;
430 exception->retry = 0;
432 if (stateid == NULL && state != NULL)
433 stateid = &state->stateid;
435 switch(errorcode) {
436 case 0:
437 return 0;
438 case -NFS4ERR_BADHANDLE:
439 case -ESTALE:
440 if (inode != NULL && S_ISREG(inode->i_mode))
441 pnfs_destroy_layout(NFS_I(inode));
442 break;
443 case -NFS4ERR_DELEG_REVOKED:
444 case -NFS4ERR_ADMIN_REVOKED:
445 case -NFS4ERR_EXPIRED:
446 case -NFS4ERR_BAD_STATEID:
447 if (inode != NULL && stateid != NULL) {
448 nfs_inode_find_state_and_recover(inode,
449 stateid);
450 goto wait_on_recovery;
452 /* Fall through */
453 case -NFS4ERR_OPENMODE:
454 if (inode) {
455 int err;
457 err = nfs_async_inode_return_delegation(inode,
458 stateid);
459 if (err == 0)
460 goto wait_on_recovery;
461 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
462 exception->retry = 1;
463 break;
466 if (state == NULL)
467 break;
468 ret = nfs4_schedule_stateid_recovery(server, state);
469 if (ret < 0)
470 break;
471 goto wait_on_recovery;
472 case -NFS4ERR_STALE_STATEID:
473 case -NFS4ERR_STALE_CLIENTID:
474 nfs4_schedule_lease_recovery(clp);
475 goto wait_on_recovery;
476 case -NFS4ERR_MOVED:
477 ret = nfs4_schedule_migration_recovery(server);
478 if (ret < 0)
479 break;
480 goto wait_on_recovery;
481 case -NFS4ERR_LEASE_MOVED:
482 nfs4_schedule_lease_moved_recovery(clp);
483 goto wait_on_recovery;
484 #if defined(CONFIG_NFS_V4_1)
485 case -NFS4ERR_BADSESSION:
486 case -NFS4ERR_BADSLOT:
487 case -NFS4ERR_BAD_HIGH_SLOT:
488 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
489 case -NFS4ERR_DEADSESSION:
490 case -NFS4ERR_SEQ_FALSE_RETRY:
491 case -NFS4ERR_SEQ_MISORDERED:
492 dprintk("%s ERROR: %d Reset session\n", __func__,
493 errorcode);
494 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
495 goto wait_on_recovery;
496 #endif /* defined(CONFIG_NFS_V4_1) */
497 case -NFS4ERR_FILE_OPEN:
498 if (exception->timeout > HZ) {
499 /* We have retried a decent amount, time to
500 * fail
502 ret = -EBUSY;
503 break;
505 /* Fall through */
506 case -NFS4ERR_DELAY:
507 nfs_inc_server_stats(server, NFSIOS_DELAY);
508 /* Fall through */
509 case -NFS4ERR_GRACE:
510 case -NFS4ERR_LAYOUTTRYLATER:
511 case -NFS4ERR_RECALLCONFLICT:
512 exception->delay = 1;
513 return 0;
515 case -NFS4ERR_RETRY_UNCACHED_REP:
516 case -NFS4ERR_OLD_STATEID:
517 exception->retry = 1;
518 break;
519 case -NFS4ERR_BADOWNER:
520 /* The following works around a Linux server bug! */
521 case -NFS4ERR_BADNAME:
522 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
523 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
524 exception->retry = 1;
525 printk(KERN_WARNING "NFS: v4 server %s "
526 "does not accept raw "
527 "uid/gids. "
528 "Reenabling the idmapper.\n",
529 server->nfs_client->cl_hostname);
532 /* We failed to handle the error */
533 return nfs4_map_errors(ret);
534 wait_on_recovery:
535 exception->recovering = 1;
536 return 0;
539 /* This is the error handling routine for processes that are allowed
540 * to sleep.
542 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
544 struct nfs_client *clp = server->nfs_client;
545 int ret;
547 ret = nfs4_do_handle_exception(server, errorcode, exception);
548 if (exception->delay) {
549 ret = nfs4_delay(server->client, &exception->timeout);
550 goto out_retry;
552 if (exception->recovering) {
553 ret = nfs4_wait_clnt_recover(clp);
554 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
555 return -EIO;
556 goto out_retry;
558 return ret;
559 out_retry:
560 if (ret == 0)
561 exception->retry = 1;
562 return ret;
565 static int
566 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
567 int errorcode, struct nfs4_exception *exception)
569 struct nfs_client *clp = server->nfs_client;
570 int ret;
572 ret = nfs4_do_handle_exception(server, errorcode, exception);
573 if (exception->delay) {
574 rpc_delay(task, nfs4_update_delay(&exception->timeout));
575 goto out_retry;
577 if (exception->recovering) {
578 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
579 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
580 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
581 goto out_retry;
583 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
584 ret = -EIO;
585 return ret;
586 out_retry:
587 if (ret == 0) {
588 exception->retry = 1;
590 * For NFS4ERR_MOVED, the client transport will need to
591 * be recomputed after migration recovery has completed.
593 if (errorcode == -NFS4ERR_MOVED)
594 rpc_task_release_transport(task);
596 return ret;
600 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
601 struct nfs4_state *state, long *timeout)
603 struct nfs4_exception exception = {
604 .state = state,
607 if (task->tk_status >= 0)
608 return 0;
609 if (timeout)
610 exception.timeout = *timeout;
611 task->tk_status = nfs4_async_handle_exception(task, server,
612 task->tk_status,
613 &exception);
614 if (exception.delay && timeout)
615 *timeout = exception.timeout;
616 if (exception.retry)
617 return -EAGAIN;
618 return 0;
622 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
623 * or 'false' otherwise.
625 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
627 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
628 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
631 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
633 spin_lock(&clp->cl_lock);
634 if (time_before(clp->cl_last_renewal,timestamp))
635 clp->cl_last_renewal = timestamp;
636 spin_unlock(&clp->cl_lock);
639 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
641 struct nfs_client *clp = server->nfs_client;
643 if (!nfs4_has_session(clp))
644 do_renew_lease(clp, timestamp);
647 struct nfs4_call_sync_data {
648 const struct nfs_server *seq_server;
649 struct nfs4_sequence_args *seq_args;
650 struct nfs4_sequence_res *seq_res;
653 void nfs4_init_sequence(struct nfs4_sequence_args *args,
654 struct nfs4_sequence_res *res, int cache_reply,
655 int privileged)
657 args->sa_slot = NULL;
658 args->sa_cache_this = cache_reply;
659 args->sa_privileged = privileged;
661 res->sr_slot = NULL;
664 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
666 struct nfs4_slot *slot = res->sr_slot;
667 struct nfs4_slot_table *tbl;
669 tbl = slot->table;
670 spin_lock(&tbl->slot_tbl_lock);
671 if (!nfs41_wake_and_assign_slot(tbl, slot))
672 nfs4_free_slot(tbl, slot);
673 spin_unlock(&tbl->slot_tbl_lock);
675 res->sr_slot = NULL;
678 static int nfs40_sequence_done(struct rpc_task *task,
679 struct nfs4_sequence_res *res)
681 if (res->sr_slot != NULL)
682 nfs40_sequence_free_slot(res);
683 return 1;
686 #if defined(CONFIG_NFS_V4_1)
688 static void nfs41_release_slot(struct nfs4_slot *slot)
690 struct nfs4_session *session;
691 struct nfs4_slot_table *tbl;
692 bool send_new_highest_used_slotid = false;
694 if (!slot)
695 return;
696 tbl = slot->table;
697 session = tbl->session;
699 /* Bump the slot sequence number */
700 if (slot->seq_done)
701 slot->seq_nr++;
702 slot->seq_done = 0;
704 spin_lock(&tbl->slot_tbl_lock);
705 /* Be nice to the server: try to ensure that the last transmitted
706 * value for highest_user_slotid <= target_highest_slotid
708 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
709 send_new_highest_used_slotid = true;
711 if (nfs41_wake_and_assign_slot(tbl, slot)) {
712 send_new_highest_used_slotid = false;
713 goto out_unlock;
715 nfs4_free_slot(tbl, slot);
717 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
718 send_new_highest_used_slotid = false;
719 out_unlock:
720 spin_unlock(&tbl->slot_tbl_lock);
721 if (send_new_highest_used_slotid)
722 nfs41_notify_server(session->clp);
723 if (waitqueue_active(&tbl->slot_waitq))
724 wake_up_all(&tbl->slot_waitq);
727 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
729 nfs41_release_slot(res->sr_slot);
730 res->sr_slot = NULL;
733 static int nfs41_sequence_process(struct rpc_task *task,
734 struct nfs4_sequence_res *res)
736 struct nfs4_session *session;
737 struct nfs4_slot *slot = res->sr_slot;
738 struct nfs_client *clp;
739 bool interrupted = false;
740 int ret = 1;
742 if (slot == NULL)
743 goto out_noaction;
744 /* don't increment the sequence number if the task wasn't sent */
745 if (!RPC_WAS_SENT(task))
746 goto out;
748 session = slot->table->session;
750 if (slot->interrupted) {
751 if (res->sr_status != -NFS4ERR_DELAY)
752 slot->interrupted = 0;
753 interrupted = true;
756 trace_nfs4_sequence_done(session, res);
757 /* Check the SEQUENCE operation status */
758 switch (res->sr_status) {
759 case 0:
760 /* Update the slot's sequence and clientid lease timer */
761 slot->seq_done = 1;
762 clp = session->clp;
763 do_renew_lease(clp, res->sr_timestamp);
764 /* Check sequence flags */
765 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
766 !!slot->privileged);
767 nfs41_update_target_slotid(slot->table, slot, res);
768 break;
769 case 1:
771 * sr_status remains 1 if an RPC level error occurred.
772 * The server may or may not have processed the sequence
773 * operation..
774 * Mark the slot as having hosted an interrupted RPC call.
776 slot->interrupted = 1;
777 goto out;
778 case -NFS4ERR_DELAY:
779 /* The server detected a resend of the RPC call and
780 * returned NFS4ERR_DELAY as per Section 2.10.6.2
781 * of RFC5661.
783 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
784 __func__,
785 slot->slot_nr,
786 slot->seq_nr);
787 goto out_retry;
788 case -NFS4ERR_RETRY_UNCACHED_REP:
789 case -NFS4ERR_SEQ_FALSE_RETRY:
791 * The server thinks we tried to replay a request.
792 * Retry the call after bumping the sequence ID.
794 goto retry_new_seq;
795 case -NFS4ERR_BADSLOT:
797 * The slot id we used was probably retired. Try again
798 * using a different slot id.
800 if (slot->slot_nr < slot->table->target_highest_slotid)
801 goto session_recover;
802 goto retry_nowait;
803 case -NFS4ERR_SEQ_MISORDERED:
805 * Was the last operation on this sequence interrupted?
806 * If so, retry after bumping the sequence number.
808 if (interrupted)
809 goto retry_new_seq;
811 * Could this slot have been previously retired?
812 * If so, then the server may be expecting seq_nr = 1!
814 if (slot->seq_nr != 1) {
815 slot->seq_nr = 1;
816 goto retry_nowait;
818 goto session_recover;
819 default:
820 /* Just update the slot sequence no. */
821 slot->seq_done = 1;
823 out:
824 /* The session may be reset by one of the error handlers. */
825 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
826 out_noaction:
827 return ret;
828 session_recover:
829 nfs4_schedule_session_recovery(session, res->sr_status);
830 goto retry_nowait;
831 retry_new_seq:
832 ++slot->seq_nr;
833 retry_nowait:
834 if (rpc_restart_call_prepare(task)) {
835 nfs41_sequence_free_slot(res);
836 task->tk_status = 0;
837 ret = 0;
839 goto out;
840 out_retry:
841 if (!rpc_restart_call(task))
842 goto out;
843 rpc_delay(task, NFS4_POLL_RETRY_MAX);
844 return 0;
847 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
849 if (!nfs41_sequence_process(task, res))
850 return 0;
851 if (res->sr_slot != NULL)
852 nfs41_sequence_free_slot(res);
853 return 1;
856 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
858 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
860 if (res->sr_slot == NULL)
861 return 1;
862 if (res->sr_slot->table->session != NULL)
863 return nfs41_sequence_process(task, res);
864 return nfs40_sequence_done(task, res);
867 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
869 if (res->sr_slot != NULL) {
870 if (res->sr_slot->table->session != NULL)
871 nfs41_sequence_free_slot(res);
872 else
873 nfs40_sequence_free_slot(res);
877 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
879 if (res->sr_slot == NULL)
880 return 1;
881 if (!res->sr_slot->table->session)
882 return nfs40_sequence_done(task, res);
883 return nfs41_sequence_done(task, res);
885 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
887 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
889 struct nfs4_call_sync_data *data = calldata;
891 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
893 nfs4_setup_sequence(data->seq_server->nfs_client,
894 data->seq_args, data->seq_res, task);
897 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
899 struct nfs4_call_sync_data *data = calldata;
901 nfs41_sequence_done(task, data->seq_res);
904 static const struct rpc_call_ops nfs41_call_sync_ops = {
905 .rpc_call_prepare = nfs41_call_sync_prepare,
906 .rpc_call_done = nfs41_call_sync_done,
909 static void
910 nfs4_sequence_process_interrupted(struct nfs_client *client,
911 struct nfs4_slot *slot, struct rpc_cred *cred)
913 struct rpc_task *task;
915 task = _nfs41_proc_sequence(client, cred, slot, true);
916 if (!IS_ERR(task))
917 rpc_put_task_async(task);
920 #else /* !CONFIG_NFS_V4_1 */
922 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
924 return nfs40_sequence_done(task, res);
927 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
929 if (res->sr_slot != NULL)
930 nfs40_sequence_free_slot(res);
933 int nfs4_sequence_done(struct rpc_task *task,
934 struct nfs4_sequence_res *res)
936 return nfs40_sequence_done(task, res);
938 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
940 static void
941 nfs4_sequence_process_interrupted(struct nfs_client *client,
942 struct nfs4_slot *slot, struct rpc_cred *cred)
944 WARN_ON_ONCE(1);
945 slot->interrupted = 0;
948 #endif /* !CONFIG_NFS_V4_1 */
950 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
952 res->sr_timestamp = jiffies;
953 res->sr_status_flags = 0;
954 res->sr_status = 1;
957 static
958 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
959 struct nfs4_sequence_res *res,
960 struct nfs4_slot *slot)
962 if (!slot)
963 return;
964 slot->privileged = args->sa_privileged ? 1 : 0;
965 args->sa_slot = slot;
967 res->sr_slot = slot;
970 int nfs4_setup_sequence(struct nfs_client *client,
971 struct nfs4_sequence_args *args,
972 struct nfs4_sequence_res *res,
973 struct rpc_task *task)
975 struct nfs4_session *session = nfs4_get_session(client);
976 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
977 struct nfs4_slot *slot;
979 /* slot already allocated? */
980 if (res->sr_slot != NULL)
981 goto out_start;
983 if (session) {
984 tbl = &session->fc_slot_table;
985 task->tk_timeout = 0;
988 for (;;) {
989 spin_lock(&tbl->slot_tbl_lock);
990 /* The state manager will wait until the slot table is empty */
991 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
992 goto out_sleep;
994 slot = nfs4_alloc_slot(tbl);
995 if (IS_ERR(slot)) {
996 /* Try again in 1/4 second */
997 if (slot == ERR_PTR(-ENOMEM))
998 task->tk_timeout = HZ >> 2;
999 goto out_sleep;
1001 spin_unlock(&tbl->slot_tbl_lock);
1003 if (likely(!slot->interrupted))
1004 break;
1005 nfs4_sequence_process_interrupted(client,
1006 slot, task->tk_msg.rpc_cred);
1009 nfs4_sequence_attach_slot(args, res, slot);
1011 trace_nfs4_setup_sequence(session, args);
1012 out_start:
1013 nfs41_sequence_res_init(res);
1014 rpc_call_start(task);
1015 return 0;
1017 out_sleep:
1018 if (args->sa_privileged)
1019 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1020 NULL, RPC_PRIORITY_PRIVILEGED);
1021 else
1022 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1023 spin_unlock(&tbl->slot_tbl_lock);
1024 return -EAGAIN;
1026 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1028 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1030 struct nfs4_call_sync_data *data = calldata;
1031 nfs4_setup_sequence(data->seq_server->nfs_client,
1032 data->seq_args, data->seq_res, task);
1035 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1037 struct nfs4_call_sync_data *data = calldata;
1038 nfs4_sequence_done(task, data->seq_res);
1041 static const struct rpc_call_ops nfs40_call_sync_ops = {
1042 .rpc_call_prepare = nfs40_call_sync_prepare,
1043 .rpc_call_done = nfs40_call_sync_done,
1046 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1047 struct nfs_server *server,
1048 struct rpc_message *msg,
1049 struct nfs4_sequence_args *args,
1050 struct nfs4_sequence_res *res)
1052 int ret;
1053 struct rpc_task *task;
1054 struct nfs_client *clp = server->nfs_client;
1055 struct nfs4_call_sync_data data = {
1056 .seq_server = server,
1057 .seq_args = args,
1058 .seq_res = res,
1060 struct rpc_task_setup task_setup = {
1061 .rpc_client = clnt,
1062 .rpc_message = msg,
1063 .callback_ops = clp->cl_mvops->call_sync_ops,
1064 .callback_data = &data
1067 task = rpc_run_task(&task_setup);
1068 if (IS_ERR(task))
1069 ret = PTR_ERR(task);
1070 else {
1071 ret = task->tk_status;
1072 rpc_put_task(task);
1074 return ret;
1077 int nfs4_call_sync(struct rpc_clnt *clnt,
1078 struct nfs_server *server,
1079 struct rpc_message *msg,
1080 struct nfs4_sequence_args *args,
1081 struct nfs4_sequence_res *res,
1082 int cache_reply)
1084 nfs4_init_sequence(args, res, cache_reply, 0);
1085 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1088 static void
1089 nfs4_inc_nlink_locked(struct inode *inode)
1091 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1092 inc_nlink(inode);
1095 static void
1096 nfs4_dec_nlink_locked(struct inode *inode)
1098 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1099 drop_nlink(inode);
1102 static void
1103 update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
1104 unsigned long timestamp, unsigned long cache_validity)
1106 struct nfs_inode *nfsi = NFS_I(dir);
1108 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1109 | NFS_INO_INVALID_MTIME
1110 | NFS_INO_INVALID_DATA
1111 | cache_validity;
1112 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
1113 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1114 nfsi->attrtimeo_timestamp = jiffies;
1115 } else {
1116 nfs_force_lookup_revalidate(dir);
1117 if (cinfo->before != inode_peek_iversion_raw(dir))
1118 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1119 NFS_INO_INVALID_ACL;
1121 inode_set_iversion_raw(dir, cinfo->after);
1122 nfsi->read_cache_jiffies = timestamp;
1123 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1124 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1125 nfs_fscache_invalidate(dir);
1128 static void
1129 update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1130 unsigned long timestamp, unsigned long cache_validity)
1132 spin_lock(&dir->i_lock);
1133 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1134 spin_unlock(&dir->i_lock);
1137 struct nfs4_open_createattrs {
1138 struct nfs4_label *label;
1139 struct iattr *sattr;
1140 const __u32 verf[2];
1143 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1144 int err, struct nfs4_exception *exception)
1146 if (err != -EINVAL)
1147 return false;
1148 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1149 return false;
1150 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1151 exception->retry = 1;
1152 return true;
1155 static u32
1156 nfs4_map_atomic_open_share(struct nfs_server *server,
1157 fmode_t fmode, int openflags)
1159 u32 res = 0;
1161 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1162 case FMODE_READ:
1163 res = NFS4_SHARE_ACCESS_READ;
1164 break;
1165 case FMODE_WRITE:
1166 res = NFS4_SHARE_ACCESS_WRITE;
1167 break;
1168 case FMODE_READ|FMODE_WRITE:
1169 res = NFS4_SHARE_ACCESS_BOTH;
1171 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1172 goto out;
1173 /* Want no delegation if we're using O_DIRECT */
1174 if (openflags & O_DIRECT)
1175 res |= NFS4_SHARE_WANT_NO_DELEG;
1176 out:
1177 return res;
1180 static enum open_claim_type4
1181 nfs4_map_atomic_open_claim(struct nfs_server *server,
1182 enum open_claim_type4 claim)
1184 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1185 return claim;
1186 switch (claim) {
1187 default:
1188 return claim;
1189 case NFS4_OPEN_CLAIM_FH:
1190 return NFS4_OPEN_CLAIM_NULL;
1191 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1192 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1193 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1194 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1198 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1200 p->o_res.f_attr = &p->f_attr;
1201 p->o_res.f_label = p->f_label;
1202 p->o_res.seqid = p->o_arg.seqid;
1203 p->c_res.seqid = p->c_arg.seqid;
1204 p->o_res.server = p->o_arg.server;
1205 p->o_res.access_request = p->o_arg.access;
1206 nfs_fattr_init(&p->f_attr);
1207 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1210 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1211 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1212 const struct nfs4_open_createattrs *c,
1213 enum open_claim_type4 claim,
1214 gfp_t gfp_mask)
1216 struct dentry *parent = dget_parent(dentry);
1217 struct inode *dir = d_inode(parent);
1218 struct nfs_server *server = NFS_SERVER(dir);
1219 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1220 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1221 struct nfs4_opendata *p;
1223 p = kzalloc(sizeof(*p), gfp_mask);
1224 if (p == NULL)
1225 goto err;
1227 p->f_label = nfs4_label_alloc(server, gfp_mask);
1228 if (IS_ERR(p->f_label))
1229 goto err_free_p;
1231 p->a_label = nfs4_label_alloc(server, gfp_mask);
1232 if (IS_ERR(p->a_label))
1233 goto err_free_f;
1235 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1236 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1237 if (IS_ERR(p->o_arg.seqid))
1238 goto err_free_label;
1239 nfs_sb_active(dentry->d_sb);
1240 p->dentry = dget(dentry);
1241 p->dir = parent;
1242 p->owner = sp;
1243 atomic_inc(&sp->so_count);
1244 p->o_arg.open_flags = flags;
1245 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1246 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1247 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1248 fmode, flags);
1249 if (flags & O_CREAT) {
1250 p->o_arg.umask = current_umask();
1251 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1252 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1253 p->o_arg.u.attrs = &p->attrs;
1254 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1256 memcpy(p->o_arg.u.verifier.data, c->verf,
1257 sizeof(p->o_arg.u.verifier.data));
1260 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1261 * will return permission denied for all bits until close */
1262 if (!(flags & O_EXCL)) {
1263 /* ask server to check for all possible rights as results
1264 * are cached */
1265 switch (p->o_arg.claim) {
1266 default:
1267 break;
1268 case NFS4_OPEN_CLAIM_NULL:
1269 case NFS4_OPEN_CLAIM_FH:
1270 p->o_arg.access = NFS4_ACCESS_READ |
1271 NFS4_ACCESS_MODIFY |
1272 NFS4_ACCESS_EXTEND |
1273 NFS4_ACCESS_EXECUTE;
1276 p->o_arg.clientid = server->nfs_client->cl_clientid;
1277 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1278 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1279 p->o_arg.name = &dentry->d_name;
1280 p->o_arg.server = server;
1281 p->o_arg.bitmask = nfs4_bitmask(server, label);
1282 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1283 switch (p->o_arg.claim) {
1284 case NFS4_OPEN_CLAIM_NULL:
1285 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1286 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1287 p->o_arg.fh = NFS_FH(dir);
1288 break;
1289 case NFS4_OPEN_CLAIM_PREVIOUS:
1290 case NFS4_OPEN_CLAIM_FH:
1291 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1292 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1293 p->o_arg.fh = NFS_FH(d_inode(dentry));
1295 p->c_arg.fh = &p->o_res.fh;
1296 p->c_arg.stateid = &p->o_res.stateid;
1297 p->c_arg.seqid = p->o_arg.seqid;
1298 nfs4_init_opendata_res(p);
1299 kref_init(&p->kref);
1300 return p;
1302 err_free_label:
1303 nfs4_label_free(p->a_label);
1304 err_free_f:
1305 nfs4_label_free(p->f_label);
1306 err_free_p:
1307 kfree(p);
1308 err:
1309 dput(parent);
1310 return NULL;
1313 static void nfs4_opendata_free(struct kref *kref)
1315 struct nfs4_opendata *p = container_of(kref,
1316 struct nfs4_opendata, kref);
1317 struct super_block *sb = p->dentry->d_sb;
1319 nfs4_lgopen_release(p->lgp);
1320 nfs_free_seqid(p->o_arg.seqid);
1321 nfs4_sequence_free_slot(&p->o_res.seq_res);
1322 if (p->state != NULL)
1323 nfs4_put_open_state(p->state);
1324 nfs4_put_state_owner(p->owner);
1326 nfs4_label_free(p->a_label);
1327 nfs4_label_free(p->f_label);
1329 dput(p->dir);
1330 dput(p->dentry);
1331 nfs_sb_deactive(sb);
1332 nfs_fattr_free_names(&p->f_attr);
1333 kfree(p->f_attr.mdsthreshold);
1334 kfree(p);
1337 static void nfs4_opendata_put(struct nfs4_opendata *p)
1339 if (p != NULL)
1340 kref_put(&p->kref, nfs4_opendata_free);
1343 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1344 fmode_t fmode)
1346 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1347 case FMODE_READ|FMODE_WRITE:
1348 return state->n_rdwr != 0;
1349 case FMODE_WRITE:
1350 return state->n_wronly != 0;
1351 case FMODE_READ:
1352 return state->n_rdonly != 0;
1354 WARN_ON_ONCE(1);
1355 return false;
1358 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1359 int open_mode, enum open_claim_type4 claim)
1361 int ret = 0;
1363 if (open_mode & (O_EXCL|O_TRUNC))
1364 goto out;
1365 switch (claim) {
1366 case NFS4_OPEN_CLAIM_NULL:
1367 case NFS4_OPEN_CLAIM_FH:
1368 goto out;
1369 default:
1370 break;
1372 switch (mode & (FMODE_READ|FMODE_WRITE)) {
1373 case FMODE_READ:
1374 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1375 && state->n_rdonly != 0;
1376 break;
1377 case FMODE_WRITE:
1378 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1379 && state->n_wronly != 0;
1380 break;
1381 case FMODE_READ|FMODE_WRITE:
1382 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1383 && state->n_rdwr != 0;
1385 out:
1386 return ret;
1389 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1390 enum open_claim_type4 claim)
1392 if (delegation == NULL)
1393 return 0;
1394 if ((delegation->type & fmode) != fmode)
1395 return 0;
1396 switch (claim) {
1397 case NFS4_OPEN_CLAIM_NULL:
1398 case NFS4_OPEN_CLAIM_FH:
1399 break;
1400 case NFS4_OPEN_CLAIM_PREVIOUS:
1401 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1402 break;
1403 /* Fall through */
1404 default:
1405 return 0;
1407 nfs_mark_delegation_referenced(delegation);
1408 return 1;
1411 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1413 switch (fmode) {
1414 case FMODE_WRITE:
1415 state->n_wronly++;
1416 break;
1417 case FMODE_READ:
1418 state->n_rdonly++;
1419 break;
1420 case FMODE_READ|FMODE_WRITE:
1421 state->n_rdwr++;
1423 nfs4_state_set_mode_locked(state, state->state | fmode);
1426 #ifdef CONFIG_NFS_V4_1
1427 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1429 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1430 return true;
1431 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1432 return true;
1433 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1434 return true;
1435 return false;
1437 #endif /* CONFIG_NFS_V4_1 */
1439 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1441 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1442 wake_up_all(&state->waitq);
1445 static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1446 const nfs4_stateid *stateid)
1448 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1449 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1451 if (stateid_seqid == state_seqid + 1U ||
1452 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1453 nfs_state_log_update_open_stateid(state);
1454 else
1455 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1458 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1460 struct nfs_client *clp = state->owner->so_server->nfs_client;
1461 bool need_recover = false;
1463 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1464 need_recover = true;
1465 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1466 need_recover = true;
1467 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1468 need_recover = true;
1469 if (need_recover)
1470 nfs4_state_mark_reclaim_nograce(clp, state);
1474 * Check for whether or not the caller may update the open stateid
1475 * to the value passed in by stateid.
1477 * Note: This function relies heavily on the server implementing
1478 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1479 * correctly.
1480 * i.e. The stateid seqids have to be initialised to 1, and
1481 * are then incremented on every state transition.
1483 static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1484 const nfs4_stateid *stateid)
1486 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1487 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1488 if (stateid->seqid == cpu_to_be32(1))
1489 nfs_state_log_update_open_stateid(state);
1490 else
1491 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1492 return true;
1495 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1496 nfs_state_log_out_of_order_open_stateid(state, stateid);
1497 return true;
1499 return false;
1502 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1504 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1505 return;
1506 if (state->n_wronly)
1507 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1508 if (state->n_rdonly)
1509 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1510 if (state->n_rdwr)
1511 set_bit(NFS_O_RDWR_STATE, &state->flags);
1512 set_bit(NFS_OPEN_STATE, &state->flags);
1515 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1516 nfs4_stateid *stateid, fmode_t fmode)
1518 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1519 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1520 case FMODE_WRITE:
1521 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1522 break;
1523 case FMODE_READ:
1524 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1525 break;
1526 case 0:
1527 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1528 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1529 clear_bit(NFS_OPEN_STATE, &state->flags);
1531 if (stateid == NULL)
1532 return;
1533 /* Handle OPEN+OPEN_DOWNGRADE races */
1534 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1535 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1536 nfs_resync_open_stateid_locked(state);
1537 goto out;
1539 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1540 nfs4_stateid_copy(&state->stateid, stateid);
1541 nfs4_stateid_copy(&state->open_stateid, stateid);
1542 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1543 out:
1544 nfs_state_log_update_open_stateid(state);
1547 static void nfs_clear_open_stateid(struct nfs4_state *state,
1548 nfs4_stateid *arg_stateid,
1549 nfs4_stateid *stateid, fmode_t fmode)
1551 write_seqlock(&state->seqlock);
1552 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1553 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1554 nfs_clear_open_stateid_locked(state, stateid, fmode);
1555 write_sequnlock(&state->seqlock);
1556 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1557 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1560 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1561 const nfs4_stateid *stateid, nfs4_stateid *freeme)
1563 DEFINE_WAIT(wait);
1564 int status = 0;
1565 for (;;) {
1567 if (!nfs_need_update_open_stateid(state, stateid))
1568 return;
1569 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1570 break;
1571 if (status)
1572 break;
1573 /* Rely on seqids for serialisation with NFSv4.0 */
1574 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1575 break;
1577 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1579 * Ensure we process the state changes in the same order
1580 * in which the server processed them by delaying the
1581 * update of the stateid until we are in sequence.
1583 write_sequnlock(&state->seqlock);
1584 spin_unlock(&state->owner->so_lock);
1585 rcu_read_unlock();
1586 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1587 if (!signal_pending(current)) {
1588 if (schedule_timeout(5*HZ) == 0)
1589 status = -EAGAIN;
1590 else
1591 status = 0;
1592 } else
1593 status = -EINTR;
1594 finish_wait(&state->waitq, &wait);
1595 rcu_read_lock();
1596 spin_lock(&state->owner->so_lock);
1597 write_seqlock(&state->seqlock);
1600 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1601 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1602 nfs4_stateid_copy(freeme, &state->open_stateid);
1603 nfs_test_and_clear_all_open_stateid(state);
1606 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1607 nfs4_stateid_copy(&state->stateid, stateid);
1608 nfs4_stateid_copy(&state->open_stateid, stateid);
1609 trace_nfs4_open_stateid_update(state->inode, stateid, status);
1610 nfs_state_log_update_open_stateid(state);
1613 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1614 const nfs4_stateid *open_stateid,
1615 fmode_t fmode,
1616 nfs4_stateid *freeme)
1619 * Protect the call to nfs4_state_set_mode_locked and
1620 * serialise the stateid update
1622 write_seqlock(&state->seqlock);
1623 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1624 switch (fmode) {
1625 case FMODE_READ:
1626 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1627 break;
1628 case FMODE_WRITE:
1629 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1630 break;
1631 case FMODE_READ|FMODE_WRITE:
1632 set_bit(NFS_O_RDWR_STATE, &state->flags);
1634 set_bit(NFS_OPEN_STATE, &state->flags);
1635 write_sequnlock(&state->seqlock);
1638 static void nfs_state_set_delegation(struct nfs4_state *state,
1639 const nfs4_stateid *deleg_stateid,
1640 fmode_t fmode)
1643 * Protect the call to nfs4_state_set_mode_locked and
1644 * serialise the stateid update
1646 write_seqlock(&state->seqlock);
1647 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1648 set_bit(NFS_DELEGATED_STATE, &state->flags);
1649 write_sequnlock(&state->seqlock);
1652 static void nfs_state_clear_delegation(struct nfs4_state *state)
1654 write_seqlock(&state->seqlock);
1655 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1656 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1657 write_sequnlock(&state->seqlock);
1660 static int update_open_stateid(struct nfs4_state *state,
1661 const nfs4_stateid *open_stateid,
1662 const nfs4_stateid *delegation,
1663 fmode_t fmode)
1665 struct nfs_server *server = NFS_SERVER(state->inode);
1666 struct nfs_client *clp = server->nfs_client;
1667 struct nfs_inode *nfsi = NFS_I(state->inode);
1668 struct nfs_delegation *deleg_cur;
1669 nfs4_stateid freeme = { };
1670 int ret = 0;
1672 fmode &= (FMODE_READ|FMODE_WRITE);
1674 rcu_read_lock();
1675 spin_lock(&state->owner->so_lock);
1676 if (open_stateid != NULL) {
1677 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1678 ret = 1;
1681 deleg_cur = rcu_dereference(nfsi->delegation);
1682 if (deleg_cur == NULL)
1683 goto no_delegation;
1685 spin_lock(&deleg_cur->lock);
1686 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1687 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1688 (deleg_cur->type & fmode) != fmode)
1689 goto no_delegation_unlock;
1691 if (delegation == NULL)
1692 delegation = &deleg_cur->stateid;
1693 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
1694 goto no_delegation_unlock;
1696 nfs_mark_delegation_referenced(deleg_cur);
1697 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1698 ret = 1;
1699 no_delegation_unlock:
1700 spin_unlock(&deleg_cur->lock);
1701 no_delegation:
1702 if (ret)
1703 update_open_stateflags(state, fmode);
1704 spin_unlock(&state->owner->so_lock);
1705 rcu_read_unlock();
1707 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1708 nfs4_schedule_state_manager(clp);
1709 if (freeme.type != 0)
1710 nfs4_test_and_free_stateid(server, &freeme,
1711 state->owner->so_cred);
1713 return ret;
1716 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1717 const nfs4_stateid *stateid)
1719 struct nfs4_state *state = lsp->ls_state;
1720 bool ret = false;
1722 spin_lock(&state->state_lock);
1723 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1724 goto out_noupdate;
1725 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1726 goto out_noupdate;
1727 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1728 ret = true;
1729 out_noupdate:
1730 spin_unlock(&state->state_lock);
1731 return ret;
1734 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1736 struct nfs_delegation *delegation;
1738 fmode &= FMODE_READ|FMODE_WRITE;
1739 rcu_read_lock();
1740 delegation = rcu_dereference(NFS_I(inode)->delegation);
1741 if (delegation == NULL || (delegation->type & fmode) == fmode) {
1742 rcu_read_unlock();
1743 return;
1745 rcu_read_unlock();
1746 nfs4_inode_return_delegation(inode);
1749 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1751 struct nfs4_state *state = opendata->state;
1752 struct nfs_delegation *delegation;
1753 int open_mode = opendata->o_arg.open_flags;
1754 fmode_t fmode = opendata->o_arg.fmode;
1755 enum open_claim_type4 claim = opendata->o_arg.claim;
1756 nfs4_stateid stateid;
1757 int ret = -EAGAIN;
1759 for (;;) {
1760 spin_lock(&state->owner->so_lock);
1761 if (can_open_cached(state, fmode, open_mode, claim)) {
1762 update_open_stateflags(state, fmode);
1763 spin_unlock(&state->owner->so_lock);
1764 goto out_return_state;
1766 spin_unlock(&state->owner->so_lock);
1767 rcu_read_lock();
1768 delegation = nfs4_get_valid_delegation(state->inode);
1769 if (!can_open_delegated(delegation, fmode, claim)) {
1770 rcu_read_unlock();
1771 break;
1773 /* Save the delegation */
1774 nfs4_stateid_copy(&stateid, &delegation->stateid);
1775 rcu_read_unlock();
1776 nfs_release_seqid(opendata->o_arg.seqid);
1777 if (!opendata->is_recover) {
1778 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1779 if (ret != 0)
1780 goto out;
1782 ret = -EAGAIN;
1784 /* Try to update the stateid using the delegation */
1785 if (update_open_stateid(state, NULL, &stateid, fmode))
1786 goto out_return_state;
1788 out:
1789 return ERR_PTR(ret);
1790 out_return_state:
1791 atomic_inc(&state->count);
1792 return state;
1795 static void
1796 nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1798 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1799 struct nfs_delegation *delegation;
1800 int delegation_flags = 0;
1802 rcu_read_lock();
1803 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1804 if (delegation)
1805 delegation_flags = delegation->flags;
1806 rcu_read_unlock();
1807 switch (data->o_arg.claim) {
1808 default:
1809 break;
1810 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1811 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1812 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1813 "returning a delegation for "
1814 "OPEN(CLAIM_DELEGATE_CUR)\n",
1815 clp->cl_hostname);
1816 return;
1818 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1819 nfs_inode_set_delegation(state->inode,
1820 data->owner->so_cred,
1821 data->o_res.delegation_type,
1822 &data->o_res.delegation,
1823 data->o_res.pagemod_limit);
1824 else
1825 nfs_inode_reclaim_delegation(state->inode,
1826 data->owner->so_cred,
1827 data->o_res.delegation_type,
1828 &data->o_res.delegation,
1829 data->o_res.pagemod_limit);
1831 if (data->o_res.do_recall)
1832 nfs_async_inode_return_delegation(state->inode,
1833 &data->o_res.delegation);
1837 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1838 * and update the nfs4_state.
1840 static struct nfs4_state *
1841 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1843 struct inode *inode = data->state->inode;
1844 struct nfs4_state *state = data->state;
1845 int ret;
1847 if (!data->rpc_done) {
1848 if (data->rpc_status)
1849 return ERR_PTR(data->rpc_status);
1850 /* cached opens have already been processed */
1851 goto update;
1854 ret = nfs_refresh_inode(inode, &data->f_attr);
1855 if (ret)
1856 return ERR_PTR(ret);
1858 if (data->o_res.delegation_type != 0)
1859 nfs4_opendata_check_deleg(data, state);
1860 update:
1861 update_open_stateid(state, &data->o_res.stateid, NULL,
1862 data->o_arg.fmode);
1863 atomic_inc(&state->count);
1865 return state;
1868 static struct inode *
1869 nfs4_opendata_get_inode(struct nfs4_opendata *data)
1871 struct inode *inode;
1873 switch (data->o_arg.claim) {
1874 case NFS4_OPEN_CLAIM_NULL:
1875 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1876 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1877 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1878 return ERR_PTR(-EAGAIN);
1879 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1880 &data->f_attr, data->f_label);
1881 break;
1882 default:
1883 inode = d_inode(data->dentry);
1884 ihold(inode);
1885 nfs_refresh_inode(inode, &data->f_attr);
1887 return inode;
1890 static struct nfs4_state *
1891 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1893 struct nfs4_state *state;
1894 struct inode *inode;
1896 inode = nfs4_opendata_get_inode(data);
1897 if (IS_ERR(inode))
1898 return ERR_CAST(inode);
1899 if (data->state != NULL && data->state->inode == inode) {
1900 state = data->state;
1901 atomic_inc(&state->count);
1902 } else
1903 state = nfs4_get_open_state(inode, data->owner);
1904 iput(inode);
1905 if (state == NULL)
1906 state = ERR_PTR(-ENOMEM);
1907 return state;
1910 static struct nfs4_state *
1911 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1913 struct nfs4_state *state;
1915 if (!data->rpc_done) {
1916 state = nfs4_try_open_cached(data);
1917 trace_nfs4_cached_open(data->state);
1918 goto out;
1921 state = nfs4_opendata_find_nfs4_state(data);
1922 if (IS_ERR(state))
1923 goto out;
1925 if (data->o_res.delegation_type != 0)
1926 nfs4_opendata_check_deleg(data, state);
1927 update_open_stateid(state, &data->o_res.stateid, NULL,
1928 data->o_arg.fmode);
1929 out:
1930 nfs_release_seqid(data->o_arg.seqid);
1931 return state;
1934 static struct nfs4_state *
1935 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1937 struct nfs4_state *ret;
1939 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1940 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1941 else
1942 ret = _nfs4_opendata_to_nfs4_state(data);
1943 nfs4_sequence_free_slot(&data->o_res.seq_res);
1944 return ret;
1947 static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1949 struct nfs_inode *nfsi = NFS_I(state->inode);
1950 struct nfs_open_context *ctx;
1952 spin_lock(&state->inode->i_lock);
1953 list_for_each_entry(ctx, &nfsi->open_files, list) {
1954 if (ctx->state != state)
1955 continue;
1956 get_nfs_open_context(ctx);
1957 spin_unlock(&state->inode->i_lock);
1958 return ctx;
1960 spin_unlock(&state->inode->i_lock);
1961 return ERR_PTR(-ENOENT);
1964 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1965 struct nfs4_state *state, enum open_claim_type4 claim)
1967 struct nfs4_opendata *opendata;
1969 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
1970 NULL, claim, GFP_NOFS);
1971 if (opendata == NULL)
1972 return ERR_PTR(-ENOMEM);
1973 opendata->state = state;
1974 atomic_inc(&state->count);
1975 return opendata;
1978 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1979 fmode_t fmode)
1981 struct nfs4_state *newstate;
1982 int ret;
1984 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
1985 return 0;
1986 opendata->o_arg.open_flags = 0;
1987 opendata->o_arg.fmode = fmode;
1988 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1989 NFS_SB(opendata->dentry->d_sb),
1990 fmode, 0);
1991 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1992 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1993 nfs4_init_opendata_res(opendata);
1994 ret = _nfs4_recover_proc_open(opendata);
1995 if (ret != 0)
1996 return ret;
1997 newstate = nfs4_opendata_to_nfs4_state(opendata);
1998 if (IS_ERR(newstate))
1999 return PTR_ERR(newstate);
2000 if (newstate != opendata->state)
2001 ret = -ESTALE;
2002 nfs4_close_state(newstate, fmode);
2003 return ret;
2006 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2008 int ret;
2010 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
2011 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2012 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2013 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2014 /* memory barrier prior to reading state->n_* */
2015 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2016 clear_bit(NFS_OPEN_STATE, &state->flags);
2017 smp_rmb();
2018 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2019 if (ret != 0)
2020 return ret;
2021 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2022 if (ret != 0)
2023 return ret;
2024 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2025 if (ret != 0)
2026 return ret;
2028 * We may have performed cached opens for all three recoveries.
2029 * Check if we need to update the current stateid.
2031 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2032 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2033 write_seqlock(&state->seqlock);
2034 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2035 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2036 write_sequnlock(&state->seqlock);
2038 return 0;
2042 * OPEN_RECLAIM:
2043 * reclaim state on the server after a reboot.
2045 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2047 struct nfs_delegation *delegation;
2048 struct nfs4_opendata *opendata;
2049 fmode_t delegation_type = 0;
2050 int status;
2052 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2053 NFS4_OPEN_CLAIM_PREVIOUS);
2054 if (IS_ERR(opendata))
2055 return PTR_ERR(opendata);
2056 rcu_read_lock();
2057 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2058 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
2059 delegation_type = delegation->type;
2060 rcu_read_unlock();
2061 opendata->o_arg.u.delegation_type = delegation_type;
2062 status = nfs4_open_recover(opendata, state);
2063 nfs4_opendata_put(opendata);
2064 return status;
2067 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2069 struct nfs_server *server = NFS_SERVER(state->inode);
2070 struct nfs4_exception exception = { };
2071 int err;
2072 do {
2073 err = _nfs4_do_open_reclaim(ctx, state);
2074 trace_nfs4_open_reclaim(ctx, 0, err);
2075 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2076 continue;
2077 if (err != -NFS4ERR_DELAY)
2078 break;
2079 nfs4_handle_exception(server, err, &exception);
2080 } while (exception.retry);
2081 return err;
2084 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2086 struct nfs_open_context *ctx;
2087 int ret;
2089 ctx = nfs4_state_find_open_context(state);
2090 if (IS_ERR(ctx))
2091 return -EAGAIN;
2092 ret = nfs4_do_open_reclaim(ctx, state);
2093 put_nfs_open_context(ctx);
2094 return ret;
2097 static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
2099 switch (err) {
2100 default:
2101 printk(KERN_ERR "NFS: %s: unhandled error "
2102 "%d.\n", __func__, err);
2103 case 0:
2104 case -ENOENT:
2105 case -EAGAIN:
2106 case -ESTALE:
2107 break;
2108 case -NFS4ERR_BADSESSION:
2109 case -NFS4ERR_BADSLOT:
2110 case -NFS4ERR_BAD_HIGH_SLOT:
2111 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2112 case -NFS4ERR_DEADSESSION:
2113 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2114 return -EAGAIN;
2115 case -NFS4ERR_STALE_CLIENTID:
2116 case -NFS4ERR_STALE_STATEID:
2117 /* Don't recall a delegation if it was lost */
2118 nfs4_schedule_lease_recovery(server->nfs_client);
2119 return -EAGAIN;
2120 case -NFS4ERR_MOVED:
2121 nfs4_schedule_migration_recovery(server);
2122 return -EAGAIN;
2123 case -NFS4ERR_LEASE_MOVED:
2124 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2125 return -EAGAIN;
2126 case -NFS4ERR_DELEG_REVOKED:
2127 case -NFS4ERR_ADMIN_REVOKED:
2128 case -NFS4ERR_EXPIRED:
2129 case -NFS4ERR_BAD_STATEID:
2130 case -NFS4ERR_OPENMODE:
2131 nfs_inode_find_state_and_recover(state->inode,
2132 stateid);
2133 nfs4_schedule_stateid_recovery(server, state);
2134 return -EAGAIN;
2135 case -NFS4ERR_DELAY:
2136 case -NFS4ERR_GRACE:
2137 ssleep(1);
2138 return -EAGAIN;
2139 case -ENOMEM:
2140 case -NFS4ERR_DENIED:
2141 if (fl) {
2142 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2143 if (lsp)
2144 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2146 return 0;
2148 return err;
2151 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2152 struct nfs4_state *state, const nfs4_stateid *stateid)
2154 struct nfs_server *server = NFS_SERVER(state->inode);
2155 struct nfs4_opendata *opendata;
2156 int err = 0;
2158 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2159 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2160 if (IS_ERR(opendata))
2161 return PTR_ERR(opendata);
2162 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2163 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2164 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2165 if (err)
2166 goto out;
2168 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2169 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2170 if (err)
2171 goto out;
2173 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2174 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2175 if (err)
2176 goto out;
2178 nfs_state_clear_delegation(state);
2179 out:
2180 nfs4_opendata_put(opendata);
2181 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2184 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2186 struct nfs4_opendata *data = calldata;
2188 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2189 &data->c_arg.seq_args, &data->c_res.seq_res, task);
2192 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2194 struct nfs4_opendata *data = calldata;
2196 nfs40_sequence_done(task, &data->c_res.seq_res);
2198 data->rpc_status = task->tk_status;
2199 if (data->rpc_status == 0) {
2200 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2201 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2202 renew_lease(data->o_res.server, data->timestamp);
2203 data->rpc_done = true;
2207 static void nfs4_open_confirm_release(void *calldata)
2209 struct nfs4_opendata *data = calldata;
2210 struct nfs4_state *state = NULL;
2212 /* If this request hasn't been cancelled, do nothing */
2213 if (!data->cancelled)
2214 goto out_free;
2215 /* In case of error, no cleanup! */
2216 if (!data->rpc_done)
2217 goto out_free;
2218 state = nfs4_opendata_to_nfs4_state(data);
2219 if (!IS_ERR(state))
2220 nfs4_close_state(state, data->o_arg.fmode);
2221 out_free:
2222 nfs4_opendata_put(data);
2225 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2226 .rpc_call_prepare = nfs4_open_confirm_prepare,
2227 .rpc_call_done = nfs4_open_confirm_done,
2228 .rpc_release = nfs4_open_confirm_release,
2232 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2234 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2236 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2237 struct rpc_task *task;
2238 struct rpc_message msg = {
2239 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2240 .rpc_argp = &data->c_arg,
2241 .rpc_resp = &data->c_res,
2242 .rpc_cred = data->owner->so_cred,
2244 struct rpc_task_setup task_setup_data = {
2245 .rpc_client = server->client,
2246 .rpc_message = &msg,
2247 .callback_ops = &nfs4_open_confirm_ops,
2248 .callback_data = data,
2249 .workqueue = nfsiod_workqueue,
2250 .flags = RPC_TASK_ASYNC,
2252 int status;
2254 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2255 data->is_recover);
2256 kref_get(&data->kref);
2257 data->rpc_done = false;
2258 data->rpc_status = 0;
2259 data->timestamp = jiffies;
2260 task = rpc_run_task(&task_setup_data);
2261 if (IS_ERR(task))
2262 return PTR_ERR(task);
2263 status = rpc_wait_for_completion_task(task);
2264 if (status != 0) {
2265 data->cancelled = true;
2266 smp_wmb();
2267 } else
2268 status = data->rpc_status;
2269 rpc_put_task(task);
2270 return status;
2273 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2275 struct nfs4_opendata *data = calldata;
2276 struct nfs4_state_owner *sp = data->owner;
2277 struct nfs_client *clp = sp->so_server->nfs_client;
2278 enum open_claim_type4 claim = data->o_arg.claim;
2280 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2281 goto out_wait;
2283 * Check if we still need to send an OPEN call, or if we can use
2284 * a delegation instead.
2286 if (data->state != NULL) {
2287 struct nfs_delegation *delegation;
2289 if (can_open_cached(data->state, data->o_arg.fmode,
2290 data->o_arg.open_flags, claim))
2291 goto out_no_action;
2292 rcu_read_lock();
2293 delegation = nfs4_get_valid_delegation(data->state->inode);
2294 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2295 goto unlock_no_action;
2296 rcu_read_unlock();
2298 /* Update client id. */
2299 data->o_arg.clientid = clp->cl_clientid;
2300 switch (claim) {
2301 default:
2302 break;
2303 case NFS4_OPEN_CLAIM_PREVIOUS:
2304 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2305 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2306 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2307 /* Fall through */
2308 case NFS4_OPEN_CLAIM_FH:
2309 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2311 data->timestamp = jiffies;
2312 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2313 &data->o_arg.seq_args,
2314 &data->o_res.seq_res,
2315 task) != 0)
2316 nfs_release_seqid(data->o_arg.seqid);
2318 /* Set the create mode (note dependency on the session type) */
2319 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2320 if (data->o_arg.open_flags & O_EXCL) {
2321 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2322 if (nfs4_has_persistent_session(clp))
2323 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2324 else if (clp->cl_mvops->minor_version > 0)
2325 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2327 return;
2328 unlock_no_action:
2329 trace_nfs4_cached_open(data->state);
2330 rcu_read_unlock();
2331 out_no_action:
2332 task->tk_action = NULL;
2333 out_wait:
2334 nfs4_sequence_done(task, &data->o_res.seq_res);
2337 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2339 struct nfs4_opendata *data = calldata;
2341 data->rpc_status = task->tk_status;
2343 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2344 return;
2346 if (task->tk_status == 0) {
2347 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2348 switch (data->o_res.f_attr->mode & S_IFMT) {
2349 case S_IFREG:
2350 break;
2351 case S_IFLNK:
2352 data->rpc_status = -ELOOP;
2353 break;
2354 case S_IFDIR:
2355 data->rpc_status = -EISDIR;
2356 break;
2357 default:
2358 data->rpc_status = -ENOTDIR;
2361 renew_lease(data->o_res.server, data->timestamp);
2362 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2363 nfs_confirm_seqid(&data->owner->so_seqid, 0);
2365 data->rpc_done = true;
2368 static void nfs4_open_release(void *calldata)
2370 struct nfs4_opendata *data = calldata;
2371 struct nfs4_state *state = NULL;
2373 /* If this request hasn't been cancelled, do nothing */
2374 if (!data->cancelled)
2375 goto out_free;
2376 /* In case of error, no cleanup! */
2377 if (data->rpc_status != 0 || !data->rpc_done)
2378 goto out_free;
2379 /* In case we need an open_confirm, no cleanup! */
2380 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2381 goto out_free;
2382 state = nfs4_opendata_to_nfs4_state(data);
2383 if (!IS_ERR(state))
2384 nfs4_close_state(state, data->o_arg.fmode);
2385 out_free:
2386 nfs4_opendata_put(data);
2389 static const struct rpc_call_ops nfs4_open_ops = {
2390 .rpc_call_prepare = nfs4_open_prepare,
2391 .rpc_call_done = nfs4_open_done,
2392 .rpc_release = nfs4_open_release,
2395 static int nfs4_run_open_task(struct nfs4_opendata *data,
2396 struct nfs_open_context *ctx)
2398 struct inode *dir = d_inode(data->dir);
2399 struct nfs_server *server = NFS_SERVER(dir);
2400 struct nfs_openargs *o_arg = &data->o_arg;
2401 struct nfs_openres *o_res = &data->o_res;
2402 struct rpc_task *task;
2403 struct rpc_message msg = {
2404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2405 .rpc_argp = o_arg,
2406 .rpc_resp = o_res,
2407 .rpc_cred = data->owner->so_cred,
2409 struct rpc_task_setup task_setup_data = {
2410 .rpc_client = server->client,
2411 .rpc_message = &msg,
2412 .callback_ops = &nfs4_open_ops,
2413 .callback_data = data,
2414 .workqueue = nfsiod_workqueue,
2415 .flags = RPC_TASK_ASYNC,
2417 int status;
2419 kref_get(&data->kref);
2420 data->rpc_done = false;
2421 data->rpc_status = 0;
2422 data->cancelled = false;
2423 data->is_recover = false;
2424 if (!ctx) {
2425 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2426 data->is_recover = true;
2427 } else {
2428 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2429 pnfs_lgopen_prepare(data, ctx);
2431 task = rpc_run_task(&task_setup_data);
2432 if (IS_ERR(task))
2433 return PTR_ERR(task);
2434 status = rpc_wait_for_completion_task(task);
2435 if (status != 0) {
2436 data->cancelled = true;
2437 smp_wmb();
2438 } else
2439 status = data->rpc_status;
2440 rpc_put_task(task);
2442 return status;
2445 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2447 struct inode *dir = d_inode(data->dir);
2448 struct nfs_openres *o_res = &data->o_res;
2449 int status;
2451 status = nfs4_run_open_task(data, NULL);
2452 if (status != 0 || !data->rpc_done)
2453 return status;
2455 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2457 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2458 status = _nfs4_proc_open_confirm(data);
2460 return status;
2464 * Additional permission checks in order to distinguish between an
2465 * open for read, and an open for execute. This works around the
2466 * fact that NFSv4 OPEN treats read and execute permissions as being
2467 * the same.
2468 * Note that in the non-execute case, we want to turn off permission
2469 * checking if we just created a new file (POSIX open() semantics).
2471 static int nfs4_opendata_access(struct rpc_cred *cred,
2472 struct nfs4_opendata *opendata,
2473 struct nfs4_state *state, fmode_t fmode,
2474 int openflags)
2476 struct nfs_access_entry cache;
2477 u32 mask, flags;
2479 /* access call failed or for some reason the server doesn't
2480 * support any access modes -- defer access call until later */
2481 if (opendata->o_res.access_supported == 0)
2482 return 0;
2484 mask = 0;
2486 * Use openflags to check for exec, because fmode won't
2487 * always have FMODE_EXEC set when file open for exec.
2489 if (openflags & __FMODE_EXEC) {
2490 /* ONLY check for exec rights */
2491 if (S_ISDIR(state->inode->i_mode))
2492 mask = NFS4_ACCESS_LOOKUP;
2493 else
2494 mask = NFS4_ACCESS_EXECUTE;
2495 } else if ((fmode & FMODE_READ) && !opendata->file_created)
2496 mask = NFS4_ACCESS_READ;
2498 cache.cred = cred;
2499 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2500 nfs_access_add_cache(state->inode, &cache);
2502 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2503 if ((mask & ~cache.mask & flags) == 0)
2504 return 0;
2506 return -EACCES;
2510 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2512 static int _nfs4_proc_open(struct nfs4_opendata *data,
2513 struct nfs_open_context *ctx)
2515 struct inode *dir = d_inode(data->dir);
2516 struct nfs_server *server = NFS_SERVER(dir);
2517 struct nfs_openargs *o_arg = &data->o_arg;
2518 struct nfs_openres *o_res = &data->o_res;
2519 int status;
2521 status = nfs4_run_open_task(data, ctx);
2522 if (!data->rpc_done)
2523 return status;
2524 if (status != 0) {
2525 if (status == -NFS4ERR_BADNAME &&
2526 !(o_arg->open_flags & O_CREAT))
2527 return -ENOENT;
2528 return status;
2531 nfs_fattr_map_and_free_names(server, &data->f_attr);
2533 if (o_arg->open_flags & O_CREAT) {
2534 if (o_arg->open_flags & O_EXCL)
2535 data->file_created = true;
2536 else if (o_res->cinfo.before != o_res->cinfo.after)
2537 data->file_created = true;
2538 if (data->file_created ||
2539 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2540 update_changeattr(dir, &o_res->cinfo,
2541 o_res->f_attr->time_start, 0);
2543 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2544 server->caps &= ~NFS_CAP_POSIX_LOCK;
2545 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2546 status = _nfs4_proc_open_confirm(data);
2547 if (status != 0)
2548 return status;
2550 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2551 nfs4_sequence_free_slot(&o_res->seq_res);
2552 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2553 o_res->f_label, NULL);
2555 return 0;
2559 * OPEN_EXPIRED:
2560 * reclaim state on the server after a network partition.
2561 * Assumes caller holds the appropriate lock
2563 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2565 struct nfs4_opendata *opendata;
2566 int ret;
2568 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2569 NFS4_OPEN_CLAIM_FH);
2570 if (IS_ERR(opendata))
2571 return PTR_ERR(opendata);
2572 ret = nfs4_open_recover(opendata, state);
2573 if (ret == -ESTALE)
2574 d_drop(ctx->dentry);
2575 nfs4_opendata_put(opendata);
2576 return ret;
2579 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2581 struct nfs_server *server = NFS_SERVER(state->inode);
2582 struct nfs4_exception exception = { };
2583 int err;
2585 do {
2586 err = _nfs4_open_expired(ctx, state);
2587 trace_nfs4_open_expired(ctx, 0, err);
2588 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2589 continue;
2590 switch (err) {
2591 default:
2592 goto out;
2593 case -NFS4ERR_GRACE:
2594 case -NFS4ERR_DELAY:
2595 nfs4_handle_exception(server, err, &exception);
2596 err = 0;
2598 } while (exception.retry);
2599 out:
2600 return err;
2603 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2605 struct nfs_open_context *ctx;
2606 int ret;
2608 ctx = nfs4_state_find_open_context(state);
2609 if (IS_ERR(ctx))
2610 return -EAGAIN;
2611 ret = nfs4_do_open_expired(ctx, state);
2612 put_nfs_open_context(ctx);
2613 return ret;
2616 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2617 const nfs4_stateid *stateid)
2619 nfs_remove_bad_delegation(state->inode, stateid);
2620 nfs_state_clear_delegation(state);
2623 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2625 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2626 nfs_finish_clear_delegation_stateid(state, NULL);
2629 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2631 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2632 nfs40_clear_delegation_stateid(state);
2633 return nfs4_open_expired(sp, state);
2636 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2637 nfs4_stateid *stateid,
2638 struct rpc_cred *cred)
2640 return -NFS4ERR_BAD_STATEID;
2643 #if defined(CONFIG_NFS_V4_1)
2644 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2645 nfs4_stateid *stateid,
2646 struct rpc_cred *cred)
2648 int status;
2650 switch (stateid->type) {
2651 default:
2652 break;
2653 case NFS4_INVALID_STATEID_TYPE:
2654 case NFS4_SPECIAL_STATEID_TYPE:
2655 return -NFS4ERR_BAD_STATEID;
2656 case NFS4_REVOKED_STATEID_TYPE:
2657 goto out_free;
2660 status = nfs41_test_stateid(server, stateid, cred);
2661 switch (status) {
2662 case -NFS4ERR_EXPIRED:
2663 case -NFS4ERR_ADMIN_REVOKED:
2664 case -NFS4ERR_DELEG_REVOKED:
2665 break;
2666 default:
2667 return status;
2669 out_free:
2670 /* Ack the revoked state to the server */
2671 nfs41_free_stateid(server, stateid, cred, true);
2672 return -NFS4ERR_EXPIRED;
2675 static void nfs41_check_delegation_stateid(struct nfs4_state *state)
2677 struct nfs_server *server = NFS_SERVER(state->inode);
2678 nfs4_stateid stateid;
2679 struct nfs_delegation *delegation;
2680 struct rpc_cred *cred;
2681 int status;
2683 /* Get the delegation credential for use by test/free_stateid */
2684 rcu_read_lock();
2685 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2686 if (delegation == NULL) {
2687 rcu_read_unlock();
2688 nfs_state_clear_delegation(state);
2689 return;
2692 nfs4_stateid_copy(&stateid, &delegation->stateid);
2693 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2694 rcu_read_unlock();
2695 nfs_state_clear_delegation(state);
2696 return;
2699 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2700 &delegation->flags)) {
2701 rcu_read_unlock();
2702 return;
2705 cred = get_rpccred(delegation->cred);
2706 rcu_read_unlock();
2707 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2708 trace_nfs4_test_delegation_stateid(state, NULL, status);
2709 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2710 nfs_finish_clear_delegation_stateid(state, &stateid);
2712 put_rpccred(cred);
2716 * nfs41_check_expired_locks - possibly free a lock stateid
2718 * @state: NFSv4 state for an inode
2720 * Returns NFS_OK if recovery for this stateid is now finished.
2721 * Otherwise a negative NFS4ERR value is returned.
2723 static int nfs41_check_expired_locks(struct nfs4_state *state)
2725 int status, ret = NFS_OK;
2726 struct nfs4_lock_state *lsp, *prev = NULL;
2727 struct nfs_server *server = NFS_SERVER(state->inode);
2729 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2730 goto out;
2732 spin_lock(&state->state_lock);
2733 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2734 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2735 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2737 refcount_inc(&lsp->ls_count);
2738 spin_unlock(&state->state_lock);
2740 nfs4_put_lock_state(prev);
2741 prev = lsp;
2743 status = nfs41_test_and_free_expired_stateid(server,
2744 &lsp->ls_stateid,
2745 cred);
2746 trace_nfs4_test_lock_stateid(state, lsp, status);
2747 if (status == -NFS4ERR_EXPIRED ||
2748 status == -NFS4ERR_BAD_STATEID) {
2749 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
2750 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
2751 if (!recover_lost_locks)
2752 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2753 } else if (status != NFS_OK) {
2754 ret = status;
2755 nfs4_put_lock_state(prev);
2756 goto out;
2758 spin_lock(&state->state_lock);
2761 spin_unlock(&state->state_lock);
2762 nfs4_put_lock_state(prev);
2763 out:
2764 return ret;
2768 * nfs41_check_open_stateid - possibly free an open stateid
2770 * @state: NFSv4 state for an inode
2772 * Returns NFS_OK if recovery for this stateid is now finished.
2773 * Otherwise a negative NFS4ERR value is returned.
2775 static int nfs41_check_open_stateid(struct nfs4_state *state)
2777 struct nfs_server *server = NFS_SERVER(state->inode);
2778 nfs4_stateid *stateid = &state->open_stateid;
2779 struct rpc_cred *cred = state->owner->so_cred;
2780 int status;
2782 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
2783 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2784 if (nfs4_have_delegation(state->inode, state->state))
2785 return NFS_OK;
2786 return -NFS4ERR_OPENMODE;
2788 return -NFS4ERR_BAD_STATEID;
2790 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
2791 trace_nfs4_test_open_stateid(state, NULL, status);
2792 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
2793 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2794 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2795 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2796 clear_bit(NFS_OPEN_STATE, &state->flags);
2797 stateid->type = NFS4_INVALID_STATEID_TYPE;
2798 return status;
2800 if (nfs_open_stateid_recover_openmode(state))
2801 return -NFS4ERR_OPENMODE;
2802 return NFS_OK;
2805 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2807 int status;
2809 nfs41_check_delegation_stateid(state);
2810 status = nfs41_check_expired_locks(state);
2811 if (status != NFS_OK)
2812 return status;
2813 status = nfs41_check_open_stateid(state);
2814 if (status != NFS_OK)
2815 status = nfs4_open_expired(sp, state);
2816 return status;
2818 #endif
2821 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2822 * fields corresponding to attributes that were used to store the verifier.
2823 * Make sure we clobber those fields in the later setattr call
2825 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2826 struct iattr *sattr, struct nfs4_label **label)
2828 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2829 __u32 attrset[3];
2830 unsigned ret;
2831 unsigned i;
2833 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2834 attrset[i] = opendata->o_res.attrset[i];
2835 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2836 attrset[i] &= ~bitmask[i];
2839 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2840 sattr->ia_valid : 0;
2842 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2843 if (sattr->ia_valid & ATTR_ATIME_SET)
2844 ret |= ATTR_ATIME_SET;
2845 else
2846 ret |= ATTR_ATIME;
2849 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2850 if (sattr->ia_valid & ATTR_MTIME_SET)
2851 ret |= ATTR_MTIME_SET;
2852 else
2853 ret |= ATTR_MTIME;
2856 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
2857 *label = NULL;
2858 return ret;
2861 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2862 fmode_t fmode,
2863 int flags,
2864 struct nfs_open_context *ctx)
2866 struct nfs4_state_owner *sp = opendata->owner;
2867 struct nfs_server *server = sp->so_server;
2868 struct dentry *dentry;
2869 struct nfs4_state *state;
2870 unsigned int seq;
2871 int ret;
2873 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2875 ret = _nfs4_proc_open(opendata, ctx);
2876 if (ret != 0)
2877 goto out;
2879 state = _nfs4_opendata_to_nfs4_state(opendata);
2880 ret = PTR_ERR(state);
2881 if (IS_ERR(state))
2882 goto out;
2883 ctx->state = state;
2884 if (server->caps & NFS_CAP_POSIX_LOCK)
2885 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2886 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2887 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
2889 dentry = opendata->dentry;
2890 if (d_really_is_negative(dentry)) {
2891 struct dentry *alias;
2892 d_drop(dentry);
2893 alias = d_exact_alias(dentry, state->inode);
2894 if (!alias)
2895 alias = d_splice_alias(igrab(state->inode), dentry);
2896 /* d_splice_alias() can't fail here - it's a non-directory */
2897 if (alias) {
2898 dput(ctx->dentry);
2899 ctx->dentry = dentry = alias;
2901 nfs_set_verifier(dentry,
2902 nfs_save_change_attribute(d_inode(opendata->dir)));
2905 /* Parse layoutget results before we check for access */
2906 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
2908 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2909 if (ret != 0)
2910 goto out;
2912 if (d_inode(dentry) == state->inode) {
2913 nfs_inode_attach_open_context(ctx);
2914 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2915 nfs4_schedule_stateid_recovery(server, state);
2918 out:
2919 if (!opendata->cancelled)
2920 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
2921 return ret;
2925 * Returns a referenced nfs4_state
2927 static int _nfs4_do_open(struct inode *dir,
2928 struct nfs_open_context *ctx,
2929 int flags,
2930 const struct nfs4_open_createattrs *c,
2931 int *opened)
2933 struct nfs4_state_owner *sp;
2934 struct nfs4_state *state = NULL;
2935 struct nfs_server *server = NFS_SERVER(dir);
2936 struct nfs4_opendata *opendata;
2937 struct dentry *dentry = ctx->dentry;
2938 struct rpc_cred *cred = ctx->cred;
2939 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2940 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
2941 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
2942 struct iattr *sattr = c->sattr;
2943 struct nfs4_label *label = c->label;
2944 struct nfs4_label *olabel = NULL;
2945 int status;
2947 /* Protect against reboot recovery conflicts */
2948 status = -ENOMEM;
2949 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2950 if (sp == NULL) {
2951 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2952 goto out_err;
2954 status = nfs4_client_recover_expired_lease(server->nfs_client);
2955 if (status != 0)
2956 goto err_put_state_owner;
2957 if (d_really_is_positive(dentry))
2958 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
2959 status = -ENOMEM;
2960 if (d_really_is_positive(dentry))
2961 claim = NFS4_OPEN_CLAIM_FH;
2962 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2963 c, claim, GFP_KERNEL);
2964 if (opendata == NULL)
2965 goto err_put_state_owner;
2967 if (label) {
2968 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2969 if (IS_ERR(olabel)) {
2970 status = PTR_ERR(olabel);
2971 goto err_opendata_put;
2975 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2976 if (!opendata->f_attr.mdsthreshold) {
2977 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2978 if (!opendata->f_attr.mdsthreshold)
2979 goto err_free_label;
2981 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
2983 if (d_really_is_positive(dentry))
2984 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
2986 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
2987 if (status != 0)
2988 goto err_free_label;
2989 state = ctx->state;
2991 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
2992 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
2993 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
2995 * send create attributes which was not set by open
2996 * with an extra setattr.
2998 if (attrs || label) {
2999 unsigned ia_old = sattr->ia_valid;
3001 sattr->ia_valid = attrs;
3002 nfs_fattr_init(opendata->o_res.f_attr);
3003 status = nfs4_do_setattr(state->inode, cred,
3004 opendata->o_res.f_attr, sattr,
3005 ctx, label, olabel);
3006 if (status == 0) {
3007 nfs_setattr_update_inode(state->inode, sattr,
3008 opendata->o_res.f_attr);
3009 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3011 sattr->ia_valid = ia_old;
3014 if (opened && opendata->file_created)
3015 *opened = 1;
3017 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3018 *ctx_th = opendata->f_attr.mdsthreshold;
3019 opendata->f_attr.mdsthreshold = NULL;
3022 nfs4_label_free(olabel);
3024 nfs4_opendata_put(opendata);
3025 nfs4_put_state_owner(sp);
3026 return 0;
3027 err_free_label:
3028 nfs4_label_free(olabel);
3029 err_opendata_put:
3030 nfs4_opendata_put(opendata);
3031 err_put_state_owner:
3032 nfs4_put_state_owner(sp);
3033 out_err:
3034 return status;
3038 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3039 struct nfs_open_context *ctx,
3040 int flags,
3041 struct iattr *sattr,
3042 struct nfs4_label *label,
3043 int *opened)
3045 struct nfs_server *server = NFS_SERVER(dir);
3046 struct nfs4_exception exception = { };
3047 struct nfs4_state *res;
3048 struct nfs4_open_createattrs c = {
3049 .label = label,
3050 .sattr = sattr,
3051 .verf = {
3052 [0] = (__u32)jiffies,
3053 [1] = (__u32)current->pid,
3056 int status;
3058 do {
3059 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3060 res = ctx->state;
3061 trace_nfs4_open_file(ctx, flags, status);
3062 if (status == 0)
3063 break;
3064 /* NOTE: BAD_SEQID means the server and client disagree about the
3065 * book-keeping w.r.t. state-changing operations
3066 * (OPEN/CLOSE/LOCK/LOCKU...)
3067 * It is actually a sign of a bug on the client or on the server.
3069 * If we receive a BAD_SEQID error in the particular case of
3070 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3071 * have unhashed the old state_owner for us, and that we can
3072 * therefore safely retry using a new one. We should still warn
3073 * the user though...
3075 if (status == -NFS4ERR_BAD_SEQID) {
3076 pr_warn_ratelimited("NFS: v4 server %s "
3077 " returned a bad sequence-id error!\n",
3078 NFS_SERVER(dir)->nfs_client->cl_hostname);
3079 exception.retry = 1;
3080 continue;
3083 * BAD_STATEID on OPEN means that the server cancelled our
3084 * state before it received the OPEN_CONFIRM.
3085 * Recover by retrying the request as per the discussion
3086 * on Page 181 of RFC3530.
3088 if (status == -NFS4ERR_BAD_STATEID) {
3089 exception.retry = 1;
3090 continue;
3092 if (status == -NFS4ERR_EXPIRED) {
3093 nfs4_schedule_lease_recovery(server->nfs_client);
3094 exception.retry = 1;
3095 continue;
3097 if (status == -EAGAIN) {
3098 /* We must have found a delegation */
3099 exception.retry = 1;
3100 continue;
3102 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3103 continue;
3104 res = ERR_PTR(nfs4_handle_exception(server,
3105 status, &exception));
3106 } while (exception.retry);
3107 return res;
3110 static int _nfs4_do_setattr(struct inode *inode,
3111 struct nfs_setattrargs *arg,
3112 struct nfs_setattrres *res,
3113 struct rpc_cred *cred,
3114 struct nfs_open_context *ctx)
3116 struct nfs_server *server = NFS_SERVER(inode);
3117 struct rpc_message msg = {
3118 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3119 .rpc_argp = arg,
3120 .rpc_resp = res,
3121 .rpc_cred = cred,
3123 struct rpc_cred *delegation_cred = NULL;
3124 unsigned long timestamp = jiffies;
3125 bool truncate;
3126 int status;
3128 nfs_fattr_init(res->fattr);
3130 /* Servers should only apply open mode checks for file size changes */
3131 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3132 if (!truncate)
3133 goto zero_stateid;
3135 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3136 /* Use that stateid */
3137 } else if (ctx != NULL && ctx->state) {
3138 struct nfs_lock_context *l_ctx;
3139 if (!nfs4_valid_open_stateid(ctx->state))
3140 return -EBADF;
3141 l_ctx = nfs_get_lock_context(ctx);
3142 if (IS_ERR(l_ctx))
3143 return PTR_ERR(l_ctx);
3144 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3145 &arg->stateid, &delegation_cred);
3146 nfs_put_lock_context(l_ctx);
3147 if (status == -EIO)
3148 return -EBADF;
3149 } else {
3150 zero_stateid:
3151 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3153 if (delegation_cred)
3154 msg.rpc_cred = delegation_cred;
3156 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3158 put_rpccred(delegation_cred);
3159 if (status == 0 && ctx != NULL)
3160 renew_lease(server, timestamp);
3161 trace_nfs4_setattr(inode, &arg->stateid, status);
3162 return status;
3165 static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3166 struct nfs_fattr *fattr, struct iattr *sattr,
3167 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
3168 struct nfs4_label *olabel)
3170 struct nfs_server *server = NFS_SERVER(inode);
3171 __u32 bitmask[NFS4_BITMASK_SZ];
3172 struct nfs4_state *state = ctx ? ctx->state : NULL;
3173 struct nfs_setattrargs arg = {
3174 .fh = NFS_FH(inode),
3175 .iap = sattr,
3176 .server = server,
3177 .bitmask = bitmask,
3178 .label = ilabel,
3180 struct nfs_setattrres res = {
3181 .fattr = fattr,
3182 .label = olabel,
3183 .server = server,
3185 struct nfs4_exception exception = {
3186 .state = state,
3187 .inode = inode,
3188 .stateid = &arg.stateid,
3190 int err;
3192 do {
3193 nfs4_bitmap_copy_adjust_setattr(bitmask,
3194 nfs4_bitmask(server, olabel),
3195 inode);
3197 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3198 switch (err) {
3199 case -NFS4ERR_OPENMODE:
3200 if (!(sattr->ia_valid & ATTR_SIZE)) {
3201 pr_warn_once("NFSv4: server %s is incorrectly "
3202 "applying open mode checks to "
3203 "a SETATTR that is not "
3204 "changing file size.\n",
3205 server->nfs_client->cl_hostname);
3207 if (state && !(state->state & FMODE_WRITE)) {
3208 err = -EBADF;
3209 if (sattr->ia_valid & ATTR_OPEN)
3210 err = -EACCES;
3211 goto out;
3214 err = nfs4_handle_exception(server, err, &exception);
3215 } while (exception.retry);
3216 out:
3217 return err;
3220 static bool
3221 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3223 if (inode == NULL || !nfs_have_layout(inode))
3224 return false;
3226 return pnfs_wait_on_layoutreturn(inode, task);
3229 struct nfs4_closedata {
3230 struct inode *inode;
3231 struct nfs4_state *state;
3232 struct nfs_closeargs arg;
3233 struct nfs_closeres res;
3234 struct {
3235 struct nfs4_layoutreturn_args arg;
3236 struct nfs4_layoutreturn_res res;
3237 struct nfs4_xdr_opaque_data ld_private;
3238 u32 roc_barrier;
3239 bool roc;
3240 } lr;
3241 struct nfs_fattr fattr;
3242 unsigned long timestamp;
3245 static void nfs4_free_closedata(void *data)
3247 struct nfs4_closedata *calldata = data;
3248 struct nfs4_state_owner *sp = calldata->state->owner;
3249 struct super_block *sb = calldata->state->inode->i_sb;
3251 if (calldata->lr.roc)
3252 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3253 calldata->res.lr_ret);
3254 nfs4_put_open_state(calldata->state);
3255 nfs_free_seqid(calldata->arg.seqid);
3256 nfs4_put_state_owner(sp);
3257 nfs_sb_deactive(sb);
3258 kfree(calldata);
3261 static void nfs4_close_done(struct rpc_task *task, void *data)
3263 struct nfs4_closedata *calldata = data;
3264 struct nfs4_state *state = calldata->state;
3265 struct nfs_server *server = NFS_SERVER(calldata->inode);
3266 nfs4_stateid *res_stateid = NULL;
3267 struct nfs4_exception exception = {
3268 .state = state,
3269 .inode = calldata->inode,
3270 .stateid = &calldata->arg.stateid,
3273 dprintk("%s: begin!\n", __func__);
3274 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3275 return;
3276 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3278 /* Handle Layoutreturn errors */
3279 if (calldata->arg.lr_args && task->tk_status != 0) {
3280 switch (calldata->res.lr_ret) {
3281 default:
3282 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3283 break;
3284 case 0:
3285 calldata->arg.lr_args = NULL;
3286 calldata->res.lr_res = NULL;
3287 break;
3288 case -NFS4ERR_OLD_STATEID:
3289 if (nfs4_layoutreturn_refresh_stateid(&calldata->arg.lr_args->stateid,
3290 &calldata->arg.lr_args->range,
3291 calldata->inode))
3292 goto lr_restart;
3293 /* Fallthrough */
3294 case -NFS4ERR_ADMIN_REVOKED:
3295 case -NFS4ERR_DELEG_REVOKED:
3296 case -NFS4ERR_EXPIRED:
3297 case -NFS4ERR_BAD_STATEID:
3298 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3299 case -NFS4ERR_WRONG_CRED:
3300 calldata->arg.lr_args = NULL;
3301 calldata->res.lr_res = NULL;
3302 goto lr_restart;
3306 /* hmm. we are done with the inode, and in the process of freeing
3307 * the state_owner. we keep this around to process errors
3309 switch (task->tk_status) {
3310 case 0:
3311 res_stateid = &calldata->res.stateid;
3312 renew_lease(server, calldata->timestamp);
3313 break;
3314 case -NFS4ERR_ACCESS:
3315 if (calldata->arg.bitmask != NULL) {
3316 calldata->arg.bitmask = NULL;
3317 calldata->res.fattr = NULL;
3318 goto out_restart;
3321 break;
3322 case -NFS4ERR_OLD_STATEID:
3323 /* Did we race with OPEN? */
3324 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
3325 state))
3326 goto out_restart;
3327 goto out_release;
3328 case -NFS4ERR_ADMIN_REVOKED:
3329 case -NFS4ERR_STALE_STATEID:
3330 case -NFS4ERR_EXPIRED:
3331 nfs4_free_revoked_stateid(server,
3332 &calldata->arg.stateid,
3333 task->tk_msg.rpc_cred);
3334 /* Fallthrough */
3335 case -NFS4ERR_BAD_STATEID:
3336 break;
3337 default:
3338 task->tk_status = nfs4_async_handle_exception(task,
3339 server, task->tk_status, &exception);
3340 if (exception.retry)
3341 goto out_restart;
3343 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3344 res_stateid, calldata->arg.fmode);
3345 out_release:
3346 task->tk_status = 0;
3347 nfs_release_seqid(calldata->arg.seqid);
3348 nfs_refresh_inode(calldata->inode, &calldata->fattr);
3349 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
3350 return;
3351 lr_restart:
3352 calldata->res.lr_ret = 0;
3353 out_restart:
3354 task->tk_status = 0;
3355 rpc_restart_call_prepare(task);
3356 goto out_release;
3359 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3361 struct nfs4_closedata *calldata = data;
3362 struct nfs4_state *state = calldata->state;
3363 struct inode *inode = calldata->inode;
3364 struct pnfs_layout_hdr *lo;
3365 bool is_rdonly, is_wronly, is_rdwr;
3366 int call_close = 0;
3368 dprintk("%s: begin!\n", __func__);
3369 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3370 goto out_wait;
3372 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3373 spin_lock(&state->owner->so_lock);
3374 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3375 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3376 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3377 /* Calculate the change in open mode */
3378 calldata->arg.fmode = 0;
3379 if (state->n_rdwr == 0) {
3380 if (state->n_rdonly == 0)
3381 call_close |= is_rdonly;
3382 else if (is_rdonly)
3383 calldata->arg.fmode |= FMODE_READ;
3384 if (state->n_wronly == 0)
3385 call_close |= is_wronly;
3386 else if (is_wronly)
3387 calldata->arg.fmode |= FMODE_WRITE;
3388 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3389 call_close |= is_rdwr;
3390 } else if (is_rdwr)
3391 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3393 if (!nfs4_valid_open_stateid(state) ||
3394 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
3395 call_close = 0;
3396 spin_unlock(&state->owner->so_lock);
3398 if (!call_close) {
3399 /* Note: exit _without_ calling nfs4_close_done */
3400 goto out_no_action;
3403 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3404 nfs_release_seqid(calldata->arg.seqid);
3405 goto out_wait;
3408 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3409 if (lo && !pnfs_layout_is_valid(lo)) {
3410 calldata->arg.lr_args = NULL;
3411 calldata->res.lr_res = NULL;
3414 if (calldata->arg.fmode == 0)
3415 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3417 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3418 /* Close-to-open cache consistency revalidation */
3419 if (!nfs4_have_delegation(inode, FMODE_READ))
3420 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3421 else
3422 calldata->arg.bitmask = NULL;
3425 calldata->arg.share_access =
3426 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3427 calldata->arg.fmode, 0);
3429 if (calldata->res.fattr == NULL)
3430 calldata->arg.bitmask = NULL;
3431 else if (calldata->arg.bitmask == NULL)
3432 calldata->res.fattr = NULL;
3433 calldata->timestamp = jiffies;
3434 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3435 &calldata->arg.seq_args,
3436 &calldata->res.seq_res,
3437 task) != 0)
3438 nfs_release_seqid(calldata->arg.seqid);
3439 dprintk("%s: done!\n", __func__);
3440 return;
3441 out_no_action:
3442 task->tk_action = NULL;
3443 out_wait:
3444 nfs4_sequence_done(task, &calldata->res.seq_res);
3447 static const struct rpc_call_ops nfs4_close_ops = {
3448 .rpc_call_prepare = nfs4_close_prepare,
3449 .rpc_call_done = nfs4_close_done,
3450 .rpc_release = nfs4_free_closedata,
3454 * It is possible for data to be read/written from a mem-mapped file
3455 * after the sys_close call (which hits the vfs layer as a flush).
3456 * This means that we can't safely call nfsv4 close on a file until
3457 * the inode is cleared. This in turn means that we are not good
3458 * NFSv4 citizens - we do not indicate to the server to update the file's
3459 * share state even when we are done with one of the three share
3460 * stateid's in the inode.
3462 * NOTE: Caller must be holding the sp->so_owner semaphore!
3464 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3466 struct nfs_server *server = NFS_SERVER(state->inode);
3467 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3468 struct nfs4_closedata *calldata;
3469 struct nfs4_state_owner *sp = state->owner;
3470 struct rpc_task *task;
3471 struct rpc_message msg = {
3472 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3473 .rpc_cred = state->owner->so_cred,
3475 struct rpc_task_setup task_setup_data = {
3476 .rpc_client = server->client,
3477 .rpc_message = &msg,
3478 .callback_ops = &nfs4_close_ops,
3479 .workqueue = nfsiod_workqueue,
3480 .flags = RPC_TASK_ASYNC,
3482 int status = -ENOMEM;
3484 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3485 &task_setup_data.rpc_client, &msg);
3487 calldata = kzalloc(sizeof(*calldata), gfp_mask);
3488 if (calldata == NULL)
3489 goto out;
3490 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3491 calldata->inode = state->inode;
3492 calldata->state = state;
3493 calldata->arg.fh = NFS_FH(state->inode);
3494 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3495 goto out_free_calldata;
3496 /* Serialization for the sequence id */
3497 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3498 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3499 if (IS_ERR(calldata->arg.seqid))
3500 goto out_free_calldata;
3501 nfs_fattr_init(&calldata->fattr);
3502 calldata->arg.fmode = 0;
3503 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3504 calldata->res.fattr = &calldata->fattr;
3505 calldata->res.seqid = calldata->arg.seqid;
3506 calldata->res.server = server;
3507 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3508 calldata->lr.roc = pnfs_roc(state->inode,
3509 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3510 if (calldata->lr.roc) {
3511 calldata->arg.lr_args = &calldata->lr.arg;
3512 calldata->res.lr_res = &calldata->lr.res;
3514 nfs_sb_active(calldata->inode->i_sb);
3516 msg.rpc_argp = &calldata->arg;
3517 msg.rpc_resp = &calldata->res;
3518 task_setup_data.callback_data = calldata;
3519 task = rpc_run_task(&task_setup_data);
3520 if (IS_ERR(task))
3521 return PTR_ERR(task);
3522 status = 0;
3523 if (wait)
3524 status = rpc_wait_for_completion_task(task);
3525 rpc_put_task(task);
3526 return status;
3527 out_free_calldata:
3528 kfree(calldata);
3529 out:
3530 nfs4_put_open_state(state);
3531 nfs4_put_state_owner(sp);
3532 return status;
3535 static struct inode *
3536 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3537 int open_flags, struct iattr *attr, int *opened)
3539 struct nfs4_state *state;
3540 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3542 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3544 /* Protect against concurrent sillydeletes */
3545 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3547 nfs4_label_release_security(label);
3549 if (IS_ERR(state))
3550 return ERR_CAST(state);
3551 return state->inode;
3554 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3556 if (ctx->state == NULL)
3557 return;
3558 if (is_sync)
3559 nfs4_close_sync(ctx->state, ctx->mode);
3560 else
3561 nfs4_close_state(ctx->state, ctx->mode);
3564 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3565 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3566 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
3568 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3570 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
3571 struct nfs4_server_caps_arg args = {
3572 .fhandle = fhandle,
3573 .bitmask = bitmask,
3575 struct nfs4_server_caps_res res = {};
3576 struct rpc_message msg = {
3577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3578 .rpc_argp = &args,
3579 .rpc_resp = &res,
3581 int status;
3582 int i;
3584 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3585 FATTR4_WORD0_FH_EXPIRE_TYPE |
3586 FATTR4_WORD0_LINK_SUPPORT |
3587 FATTR4_WORD0_SYMLINK_SUPPORT |
3588 FATTR4_WORD0_ACLSUPPORT;
3589 if (minorversion)
3590 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3592 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3593 if (status == 0) {
3594 /* Sanity check the server answers */
3595 switch (minorversion) {
3596 case 0:
3597 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3598 res.attr_bitmask[2] = 0;
3599 break;
3600 case 1:
3601 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3602 break;
3603 case 2:
3604 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3606 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3607 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3608 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3609 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3610 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
3611 NFS_CAP_CTIME|NFS_CAP_MTIME|
3612 NFS_CAP_SECURITY_LABEL);
3613 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3614 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3615 server->caps |= NFS_CAP_ACLS;
3616 if (res.has_links != 0)
3617 server->caps |= NFS_CAP_HARDLINKS;
3618 if (res.has_symlinks != 0)
3619 server->caps |= NFS_CAP_SYMLINKS;
3620 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3621 server->caps |= NFS_CAP_FILEID;
3622 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3623 server->caps |= NFS_CAP_MODE;
3624 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3625 server->caps |= NFS_CAP_NLINK;
3626 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3627 server->caps |= NFS_CAP_OWNER;
3628 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3629 server->caps |= NFS_CAP_OWNER_GROUP;
3630 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3631 server->caps |= NFS_CAP_ATIME;
3632 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3633 server->caps |= NFS_CAP_CTIME;
3634 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3635 server->caps |= NFS_CAP_MTIME;
3636 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3637 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3638 server->caps |= NFS_CAP_SECURITY_LABEL;
3639 #endif
3640 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3641 sizeof(server->attr_bitmask));
3642 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
3644 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3645 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3646 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
3647 server->cache_consistency_bitmask[2] = 0;
3649 /* Avoid a regression due to buggy server */
3650 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3651 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
3652 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3653 sizeof(server->exclcreat_bitmask));
3655 server->acl_bitmask = res.acl_bitmask;
3656 server->fh_expire_type = res.fh_expire_type;
3659 return status;
3662 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3664 struct nfs4_exception exception = { };
3665 int err;
3666 do {
3667 err = nfs4_handle_exception(server,
3668 _nfs4_server_capabilities(server, fhandle),
3669 &exception);
3670 } while (exception.retry);
3671 return err;
3674 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3675 struct nfs_fsinfo *info)
3677 u32 bitmask[3];
3678 struct nfs4_lookup_root_arg args = {
3679 .bitmask = bitmask,
3681 struct nfs4_lookup_res res = {
3682 .server = server,
3683 .fattr = info->fattr,
3684 .fh = fhandle,
3686 struct rpc_message msg = {
3687 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3688 .rpc_argp = &args,
3689 .rpc_resp = &res,
3692 bitmask[0] = nfs4_fattr_bitmap[0];
3693 bitmask[1] = nfs4_fattr_bitmap[1];
3695 * Process the label in the upcoming getfattr
3697 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3699 nfs_fattr_init(info->fattr);
3700 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3703 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3704 struct nfs_fsinfo *info)
3706 struct nfs4_exception exception = { };
3707 int err;
3708 do {
3709 err = _nfs4_lookup_root(server, fhandle, info);
3710 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
3711 switch (err) {
3712 case 0:
3713 case -NFS4ERR_WRONGSEC:
3714 goto out;
3715 default:
3716 err = nfs4_handle_exception(server, err, &exception);
3718 } while (exception.retry);
3719 out:
3720 return err;
3723 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3724 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3726 struct rpc_auth_create_args auth_args = {
3727 .pseudoflavor = flavor,
3729 struct rpc_auth *auth;
3731 auth = rpcauth_create(&auth_args, server->client);
3732 if (IS_ERR(auth))
3733 return -EACCES;
3734 return nfs4_lookup_root(server, fhandle, info);
3738 * Retry pseudoroot lookup with various security flavors. We do this when:
3740 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3741 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3743 * Returns zero on success, or a negative NFS4ERR value, or a
3744 * negative errno value.
3746 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3747 struct nfs_fsinfo *info)
3749 /* Per 3530bis 15.33.5 */
3750 static const rpc_authflavor_t flav_array[] = {
3751 RPC_AUTH_GSS_KRB5P,
3752 RPC_AUTH_GSS_KRB5I,
3753 RPC_AUTH_GSS_KRB5,
3754 RPC_AUTH_UNIX, /* courtesy */
3755 RPC_AUTH_NULL,
3757 int status = -EPERM;
3758 size_t i;
3760 if (server->auth_info.flavor_len > 0) {
3761 /* try each flavor specified by user */
3762 for (i = 0; i < server->auth_info.flavor_len; i++) {
3763 status = nfs4_lookup_root_sec(server, fhandle, info,
3764 server->auth_info.flavors[i]);
3765 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3766 continue;
3767 break;
3769 } else {
3770 /* no flavors specified by user, try default list */
3771 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3772 status = nfs4_lookup_root_sec(server, fhandle, info,
3773 flav_array[i]);
3774 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3775 continue;
3776 break;
3781 * -EACCESS could mean that the user doesn't have correct permissions
3782 * to access the mount. It could also mean that we tried to mount
3783 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3784 * existing mount programs don't handle -EACCES very well so it should
3785 * be mapped to -EPERM instead.
3787 if (status == -EACCES)
3788 status = -EPERM;
3789 return status;
3793 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3794 * @server: initialized nfs_server handle
3795 * @fhandle: we fill in the pseudo-fs root file handle
3796 * @info: we fill in an FSINFO struct
3797 * @auth_probe: probe the auth flavours
3799 * Returns zero on success, or a negative errno.
3801 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
3802 struct nfs_fsinfo *info,
3803 bool auth_probe)
3805 int status = 0;
3807 if (!auth_probe)
3808 status = nfs4_lookup_root(server, fhandle, info);
3810 if (auth_probe || status == NFS4ERR_WRONGSEC)
3811 status = server->nfs_client->cl_mvops->find_root_sec(server,
3812 fhandle, info);
3814 if (status == 0)
3815 status = nfs4_server_capabilities(server, fhandle);
3816 if (status == 0)
3817 status = nfs4_do_fsinfo(server, fhandle, info);
3819 return nfs4_map_errors(status);
3822 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3823 struct nfs_fsinfo *info)
3825 int error;
3826 struct nfs_fattr *fattr = info->fattr;
3827 struct nfs4_label *label = NULL;
3829 error = nfs4_server_capabilities(server, mntfh);
3830 if (error < 0) {
3831 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3832 return error;
3835 label = nfs4_label_alloc(server, GFP_KERNEL);
3836 if (IS_ERR(label))
3837 return PTR_ERR(label);
3839 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
3840 if (error < 0) {
3841 dprintk("nfs4_get_root: getattr error = %d\n", -error);
3842 goto err_free_label;
3845 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3846 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3847 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3849 err_free_label:
3850 nfs4_label_free(label);
3852 return error;
3856 * Get locations and (maybe) other attributes of a referral.
3857 * Note that we'll actually follow the referral later when
3858 * we detect fsid mismatch in inode revalidation
3860 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3861 const struct qstr *name, struct nfs_fattr *fattr,
3862 struct nfs_fh *fhandle)
3864 int status = -ENOMEM;
3865 struct page *page = NULL;
3866 struct nfs4_fs_locations *locations = NULL;
3868 page = alloc_page(GFP_KERNEL);
3869 if (page == NULL)
3870 goto out;
3871 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3872 if (locations == NULL)
3873 goto out;
3875 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
3876 if (status != 0)
3877 goto out;
3880 * If the fsid didn't change, this is a migration event, not a
3881 * referral. Cause us to drop into the exception handler, which
3882 * will kick off migration recovery.
3884 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
3885 dprintk("%s: server did not return a different fsid for"
3886 " a referral at %s\n", __func__, name->name);
3887 status = -NFS4ERR_MOVED;
3888 goto out;
3890 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3891 nfs_fixup_referral_attributes(&locations->fattr);
3893 /* replace the lookup nfs_fattr with the locations nfs_fattr */
3894 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
3895 memset(fhandle, 0, sizeof(struct nfs_fh));
3896 out:
3897 if (page)
3898 __free_page(page);
3899 kfree(locations);
3900 return status;
3903 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3904 struct nfs_fattr *fattr, struct nfs4_label *label,
3905 struct inode *inode)
3907 __u32 bitmask[NFS4_BITMASK_SZ];
3908 struct nfs4_getattr_arg args = {
3909 .fh = fhandle,
3910 .bitmask = bitmask,
3912 struct nfs4_getattr_res res = {
3913 .fattr = fattr,
3914 .label = label,
3915 .server = server,
3917 struct rpc_message msg = {
3918 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3919 .rpc_argp = &args,
3920 .rpc_resp = &res,
3923 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
3925 nfs_fattr_init(fattr);
3926 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3929 static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3930 struct nfs_fattr *fattr, struct nfs4_label *label,
3931 struct inode *inode)
3933 struct nfs4_exception exception = { };
3934 int err;
3935 do {
3936 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
3937 trace_nfs4_getattr(server, fhandle, fattr, err);
3938 err = nfs4_handle_exception(server, err,
3939 &exception);
3940 } while (exception.retry);
3941 return err;
3945 * The file is not closed if it is opened due to the a request to change
3946 * the size of the file. The open call will not be needed once the
3947 * VFS layer lookup-intents are implemented.
3949 * Close is called when the inode is destroyed.
3950 * If we haven't opened the file for O_WRONLY, we
3951 * need to in the size_change case to obtain a stateid.
3953 * Got race?
3954 * Because OPEN is always done by name in nfsv4, it is
3955 * possible that we opened a different file by the same
3956 * name. We can recognize this race condition, but we
3957 * can't do anything about it besides returning an error.
3959 * This will be fixed with VFS changes (lookup-intent).
3961 static int
3962 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3963 struct iattr *sattr)
3965 struct inode *inode = d_inode(dentry);
3966 struct rpc_cred *cred = NULL;
3967 struct nfs_open_context *ctx = NULL;
3968 struct nfs4_label *label = NULL;
3969 int status;
3971 if (pnfs_ld_layoutret_on_setattr(inode) &&
3972 sattr->ia_valid & ATTR_SIZE &&
3973 sattr->ia_size < i_size_read(inode))
3974 pnfs_commit_and_return_layout(inode);
3976 nfs_fattr_init(fattr);
3978 /* Deal with open(O_TRUNC) */
3979 if (sattr->ia_valid & ATTR_OPEN)
3980 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
3982 /* Optimization: if the end result is no change, don't RPC */
3983 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
3984 return 0;
3986 /* Search for an existing open(O_WRITE) file */
3987 if (sattr->ia_valid & ATTR_FILE) {
3989 ctx = nfs_file_open_context(sattr->ia_file);
3990 if (ctx)
3991 cred = ctx->cred;
3994 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3995 if (IS_ERR(label))
3996 return PTR_ERR(label);
3998 /* Return any delegations if we're going to change ACLs */
3999 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4000 nfs4_inode_make_writeable(inode);
4002 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
4003 if (status == 0) {
4004 nfs_setattr_update_inode(inode, sattr, fattr);
4005 nfs_setsecurity(inode, fattr, label);
4007 nfs4_label_free(label);
4008 return status;
4011 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4012 const struct qstr *name, struct nfs_fh *fhandle,
4013 struct nfs_fattr *fattr, struct nfs4_label *label)
4015 struct nfs_server *server = NFS_SERVER(dir);
4016 int status;
4017 struct nfs4_lookup_arg args = {
4018 .bitmask = server->attr_bitmask,
4019 .dir_fh = NFS_FH(dir),
4020 .name = name,
4022 struct nfs4_lookup_res res = {
4023 .server = server,
4024 .fattr = fattr,
4025 .label = label,
4026 .fh = fhandle,
4028 struct rpc_message msg = {
4029 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4030 .rpc_argp = &args,
4031 .rpc_resp = &res,
4034 args.bitmask = nfs4_bitmask(server, label);
4036 nfs_fattr_init(fattr);
4038 dprintk("NFS call lookup %s\n", name->name);
4039 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
4040 dprintk("NFS reply lookup: %d\n", status);
4041 return status;
4044 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4046 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4047 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4048 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4049 fattr->nlink = 2;
4052 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4053 const struct qstr *name, struct nfs_fh *fhandle,
4054 struct nfs_fattr *fattr, struct nfs4_label *label)
4056 struct nfs4_exception exception = { };
4057 struct rpc_clnt *client = *clnt;
4058 int err;
4059 do {
4060 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
4061 trace_nfs4_lookup(dir, name, err);
4062 switch (err) {
4063 case -NFS4ERR_BADNAME:
4064 err = -ENOENT;
4065 goto out;
4066 case -NFS4ERR_MOVED:
4067 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4068 if (err == -NFS4ERR_MOVED)
4069 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4070 goto out;
4071 case -NFS4ERR_WRONGSEC:
4072 err = -EPERM;
4073 if (client != *clnt)
4074 goto out;
4075 client = nfs4_negotiate_security(client, dir, name);
4076 if (IS_ERR(client))
4077 return PTR_ERR(client);
4079 exception.retry = 1;
4080 break;
4081 default:
4082 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4084 } while (exception.retry);
4086 out:
4087 if (err == 0)
4088 *clnt = client;
4089 else if (client != *clnt)
4090 rpc_shutdown_client(client);
4092 return err;
4095 static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
4096 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4097 struct nfs4_label *label)
4099 int status;
4100 struct rpc_clnt *client = NFS_CLIENT(dir);
4102 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
4103 if (client != NFS_CLIENT(dir)) {
4104 rpc_shutdown_client(client);
4105 nfs_fixup_secinfo_attributes(fattr);
4107 return status;
4110 struct rpc_clnt *
4111 nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
4112 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4114 struct rpc_clnt *client = NFS_CLIENT(dir);
4115 int status;
4117 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
4118 if (status < 0)
4119 return ERR_PTR(status);
4120 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4123 static int _nfs4_proc_lookupp(struct inode *inode,
4124 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4125 struct nfs4_label *label)
4127 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4128 struct nfs_server *server = NFS_SERVER(inode);
4129 int status;
4130 struct nfs4_lookupp_arg args = {
4131 .bitmask = server->attr_bitmask,
4132 .fh = NFS_FH(inode),
4134 struct nfs4_lookupp_res res = {
4135 .server = server,
4136 .fattr = fattr,
4137 .label = label,
4138 .fh = fhandle,
4140 struct rpc_message msg = {
4141 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4142 .rpc_argp = &args,
4143 .rpc_resp = &res,
4146 args.bitmask = nfs4_bitmask(server, label);
4148 nfs_fattr_init(fattr);
4150 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4151 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4152 &res.seq_res, 0);
4153 dprintk("NFS reply lookupp: %d\n", status);
4154 return status;
4157 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4158 struct nfs_fattr *fattr, struct nfs4_label *label)
4160 struct nfs4_exception exception = { };
4161 int err;
4162 do {
4163 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4164 trace_nfs4_lookupp(inode, err);
4165 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4166 &exception);
4167 } while (exception.retry);
4168 return err;
4171 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4173 struct nfs_server *server = NFS_SERVER(inode);
4174 struct nfs4_accessargs args = {
4175 .fh = NFS_FH(inode),
4176 .access = entry->mask,
4178 struct nfs4_accessres res = {
4179 .server = server,
4181 struct rpc_message msg = {
4182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4183 .rpc_argp = &args,
4184 .rpc_resp = &res,
4185 .rpc_cred = entry->cred,
4187 int status = 0;
4189 if (!nfs4_have_delegation(inode, FMODE_READ)) {
4190 res.fattr = nfs_alloc_fattr();
4191 if (res.fattr == NULL)
4192 return -ENOMEM;
4193 args.bitmask = server->cache_consistency_bitmask;
4196 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4197 if (!status) {
4198 nfs_access_set_mask(entry, res.access);
4199 if (res.fattr)
4200 nfs_refresh_inode(inode, res.fattr);
4202 nfs_free_fattr(res.fattr);
4203 return status;
4206 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4208 struct nfs4_exception exception = { };
4209 int err;
4210 do {
4211 err = _nfs4_proc_access(inode, entry);
4212 trace_nfs4_access(inode, err);
4213 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4214 &exception);
4215 } while (exception.retry);
4216 return err;
4220 * TODO: For the time being, we don't try to get any attributes
4221 * along with any of the zero-copy operations READ, READDIR,
4222 * READLINK, WRITE.
4224 * In the case of the first three, we want to put the GETATTR
4225 * after the read-type operation -- this is because it is hard
4226 * to predict the length of a GETATTR response in v4, and thus
4227 * align the READ data correctly. This means that the GETATTR
4228 * may end up partially falling into the page cache, and we should
4229 * shift it into the 'tail' of the xdr_buf before processing.
4230 * To do this efficiently, we need to know the total length
4231 * of data received, which doesn't seem to be available outside
4232 * of the RPC layer.
4234 * In the case of WRITE, we also want to put the GETATTR after
4235 * the operation -- in this case because we want to make sure
4236 * we get the post-operation mtime and size.
4238 * Both of these changes to the XDR layer would in fact be quite
4239 * minor, but I decided to leave them for a subsequent patch.
4241 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4242 unsigned int pgbase, unsigned int pglen)
4244 struct nfs4_readlink args = {
4245 .fh = NFS_FH(inode),
4246 .pgbase = pgbase,
4247 .pglen = pglen,
4248 .pages = &page,
4250 struct nfs4_readlink_res res;
4251 struct rpc_message msg = {
4252 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4253 .rpc_argp = &args,
4254 .rpc_resp = &res,
4257 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4260 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4261 unsigned int pgbase, unsigned int pglen)
4263 struct nfs4_exception exception = { };
4264 int err;
4265 do {
4266 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4267 trace_nfs4_readlink(inode, err);
4268 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4269 &exception);
4270 } while (exception.retry);
4271 return err;
4275 * This is just for mknod. open(O_CREAT) will always do ->open_context().
4277 static int
4278 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4279 int flags)
4281 struct nfs_server *server = NFS_SERVER(dir);
4282 struct nfs4_label l, *ilabel = NULL;
4283 struct nfs_open_context *ctx;
4284 struct nfs4_state *state;
4285 int status = 0;
4287 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4288 if (IS_ERR(ctx))
4289 return PTR_ERR(ctx);
4291 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4293 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4294 sattr->ia_mode &= ~current_umask();
4295 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4296 if (IS_ERR(state)) {
4297 status = PTR_ERR(state);
4298 goto out;
4300 out:
4301 nfs4_label_release_security(ilabel);
4302 put_nfs_open_context(ctx);
4303 return status;
4306 static int
4307 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4309 struct nfs_server *server = NFS_SERVER(dir);
4310 struct nfs_removeargs args = {
4311 .fh = NFS_FH(dir),
4312 .name = *name,
4314 struct nfs_removeres res = {
4315 .server = server,
4317 struct rpc_message msg = {
4318 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4319 .rpc_argp = &args,
4320 .rpc_resp = &res,
4322 unsigned long timestamp = jiffies;
4323 int status;
4325 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4326 if (status == 0) {
4327 spin_lock(&dir->i_lock);
4328 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
4329 /* Removing a directory decrements nlink in the parent */
4330 if (ftype == NF4DIR && dir->i_nlink > 2)
4331 nfs4_dec_nlink_locked(dir);
4332 spin_unlock(&dir->i_lock);
4334 return status;
4337 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4339 struct nfs4_exception exception = { };
4340 struct inode *inode = d_inode(dentry);
4341 int err;
4343 if (inode) {
4344 if (inode->i_nlink == 1)
4345 nfs4_inode_return_delegation(inode);
4346 else
4347 nfs4_inode_make_writeable(inode);
4349 do {
4350 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4351 trace_nfs4_remove(dir, &dentry->d_name, err);
4352 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4353 &exception);
4354 } while (exception.retry);
4355 return err;
4358 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4360 struct nfs4_exception exception = { };
4361 int err;
4363 do {
4364 err = _nfs4_proc_remove(dir, name, NF4DIR);
4365 trace_nfs4_remove(dir, name, err);
4366 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4367 &exception);
4368 } while (exception.retry);
4369 return err;
4372 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4373 struct dentry *dentry,
4374 struct inode *inode)
4376 struct nfs_removeargs *args = msg->rpc_argp;
4377 struct nfs_removeres *res = msg->rpc_resp;
4379 res->server = NFS_SB(dentry->d_sb);
4380 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4381 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4383 nfs_fattr_init(res->dir_attr);
4385 if (inode)
4386 nfs4_inode_return_delegation(inode);
4389 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4391 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4392 &data->args.seq_args,
4393 &data->res.seq_res,
4394 task);
4397 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4399 struct nfs_unlinkdata *data = task->tk_calldata;
4400 struct nfs_removeres *res = &data->res;
4402 if (!nfs4_sequence_done(task, &res->seq_res))
4403 return 0;
4404 if (nfs4_async_handle_error(task, res->server, NULL,
4405 &data->timeout) == -EAGAIN)
4406 return 0;
4407 if (task->tk_status == 0)
4408 update_changeattr(dir, &res->cinfo,
4409 res->dir_attr->time_start, 0);
4410 return 1;
4413 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4414 struct dentry *old_dentry,
4415 struct dentry *new_dentry)
4417 struct nfs_renameargs *arg = msg->rpc_argp;
4418 struct nfs_renameres *res = msg->rpc_resp;
4419 struct inode *old_inode = d_inode(old_dentry);
4420 struct inode *new_inode = d_inode(new_dentry);
4422 if (old_inode)
4423 nfs4_inode_make_writeable(old_inode);
4424 if (new_inode)
4425 nfs4_inode_return_delegation(new_inode);
4426 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4427 res->server = NFS_SB(old_dentry->d_sb);
4428 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4431 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4433 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4434 &data->args.seq_args,
4435 &data->res.seq_res,
4436 task);
4439 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4440 struct inode *new_dir)
4442 struct nfs_renamedata *data = task->tk_calldata;
4443 struct nfs_renameres *res = &data->res;
4445 if (!nfs4_sequence_done(task, &res->seq_res))
4446 return 0;
4447 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4448 return 0;
4450 if (task->tk_status == 0) {
4451 if (new_dir != old_dir) {
4452 /* Note: If we moved a directory, nlink will change */
4453 update_changeattr(old_dir, &res->old_cinfo,
4454 res->old_fattr->time_start,
4455 NFS_INO_INVALID_OTHER);
4456 update_changeattr(new_dir, &res->new_cinfo,
4457 res->new_fattr->time_start,
4458 NFS_INO_INVALID_OTHER);
4459 } else
4460 update_changeattr(old_dir, &res->old_cinfo,
4461 res->old_fattr->time_start,
4464 return 1;
4467 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4469 struct nfs_server *server = NFS_SERVER(inode);
4470 __u32 bitmask[NFS4_BITMASK_SZ];
4471 struct nfs4_link_arg arg = {
4472 .fh = NFS_FH(inode),
4473 .dir_fh = NFS_FH(dir),
4474 .name = name,
4475 .bitmask = bitmask,
4477 struct nfs4_link_res res = {
4478 .server = server,
4479 .label = NULL,
4481 struct rpc_message msg = {
4482 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4483 .rpc_argp = &arg,
4484 .rpc_resp = &res,
4486 int status = -ENOMEM;
4488 res.fattr = nfs_alloc_fattr();
4489 if (res.fattr == NULL)
4490 goto out;
4492 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4493 if (IS_ERR(res.label)) {
4494 status = PTR_ERR(res.label);
4495 goto out;
4498 nfs4_inode_make_writeable(inode);
4499 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
4501 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
4502 if (!status) {
4503 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
4504 status = nfs_post_op_update_inode(inode, res.fattr);
4505 if (!status)
4506 nfs_setsecurity(inode, res.fattr, res.label);
4510 nfs4_label_free(res.label);
4512 out:
4513 nfs_free_fattr(res.fattr);
4514 return status;
4517 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
4519 struct nfs4_exception exception = { };
4520 int err;
4521 do {
4522 err = nfs4_handle_exception(NFS_SERVER(inode),
4523 _nfs4_proc_link(inode, dir, name),
4524 &exception);
4525 } while (exception.retry);
4526 return err;
4529 struct nfs4_createdata {
4530 struct rpc_message msg;
4531 struct nfs4_create_arg arg;
4532 struct nfs4_create_res res;
4533 struct nfs_fh fh;
4534 struct nfs_fattr fattr;
4535 struct nfs4_label *label;
4538 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
4539 const struct qstr *name, struct iattr *sattr, u32 ftype)
4541 struct nfs4_createdata *data;
4543 data = kzalloc(sizeof(*data), GFP_KERNEL);
4544 if (data != NULL) {
4545 struct nfs_server *server = NFS_SERVER(dir);
4547 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4548 if (IS_ERR(data->label))
4549 goto out_free;
4551 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4552 data->msg.rpc_argp = &data->arg;
4553 data->msg.rpc_resp = &data->res;
4554 data->arg.dir_fh = NFS_FH(dir);
4555 data->arg.server = server;
4556 data->arg.name = name;
4557 data->arg.attrs = sattr;
4558 data->arg.ftype = ftype;
4559 data->arg.bitmask = nfs4_bitmask(server, data->label);
4560 data->arg.umask = current_umask();
4561 data->res.server = server;
4562 data->res.fh = &data->fh;
4563 data->res.fattr = &data->fattr;
4564 data->res.label = data->label;
4565 nfs_fattr_init(data->res.fattr);
4567 return data;
4568 out_free:
4569 kfree(data);
4570 return NULL;
4573 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4575 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
4576 &data->arg.seq_args, &data->res.seq_res, 1);
4577 if (status == 0) {
4578 spin_lock(&dir->i_lock);
4579 update_changeattr_locked(dir, &data->res.dir_cinfo,
4580 data->res.fattr->time_start, 0);
4581 /* Creating a directory bumps nlink in the parent */
4582 if (data->arg.ftype == NF4DIR)
4583 nfs4_inc_nlink_locked(dir);
4584 spin_unlock(&dir->i_lock);
4585 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
4587 return status;
4590 static void nfs4_free_createdata(struct nfs4_createdata *data)
4592 nfs4_label_free(data->label);
4593 kfree(data);
4596 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4597 struct page *page, unsigned int len, struct iattr *sattr,
4598 struct nfs4_label *label)
4600 struct nfs4_createdata *data;
4601 int status = -ENAMETOOLONG;
4603 if (len > NFS4_MAXPATHLEN)
4604 goto out;
4606 status = -ENOMEM;
4607 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4608 if (data == NULL)
4609 goto out;
4611 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4612 data->arg.u.symlink.pages = &page;
4613 data->arg.u.symlink.len = len;
4614 data->arg.label = label;
4616 status = nfs4_do_create(dir, dentry, data);
4618 nfs4_free_createdata(data);
4619 out:
4620 return status;
4623 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
4624 struct page *page, unsigned int len, struct iattr *sattr)
4626 struct nfs4_exception exception = { };
4627 struct nfs4_label l, *label = NULL;
4628 int err;
4630 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4632 do {
4633 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4634 trace_nfs4_symlink(dir, &dentry->d_name, err);
4635 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4636 &exception);
4637 } while (exception.retry);
4639 nfs4_label_release_security(label);
4640 return err;
4643 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4644 struct iattr *sattr, struct nfs4_label *label)
4646 struct nfs4_createdata *data;
4647 int status = -ENOMEM;
4649 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4650 if (data == NULL)
4651 goto out;
4653 data->arg.label = label;
4654 status = nfs4_do_create(dir, dentry, data);
4656 nfs4_free_createdata(data);
4657 out:
4658 return status;
4661 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4662 struct iattr *sattr)
4664 struct nfs_server *server = NFS_SERVER(dir);
4665 struct nfs4_exception exception = { };
4666 struct nfs4_label l, *label = NULL;
4667 int err;
4669 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4671 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4672 sattr->ia_mode &= ~current_umask();
4673 do {
4674 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4675 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4676 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4677 &exception);
4678 } while (exception.retry);
4679 nfs4_label_release_security(label);
4681 return err;
4684 static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4685 u64 cookie, struct page **pages, unsigned int count, bool plus)
4687 struct inode *dir = d_inode(dentry);
4688 struct nfs4_readdir_arg args = {
4689 .fh = NFS_FH(dir),
4690 .pages = pages,
4691 .pgbase = 0,
4692 .count = count,
4693 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
4694 .plus = plus,
4696 struct nfs4_readdir_res res;
4697 struct rpc_message msg = {
4698 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4699 .rpc_argp = &args,
4700 .rpc_resp = &res,
4701 .rpc_cred = cred,
4703 int status;
4705 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4706 dentry,
4707 (unsigned long long)cookie);
4708 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
4709 res.pgbase = args.pgbase;
4710 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4711 if (status >= 0) {
4712 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
4713 status += args.pgbase;
4716 nfs_invalidate_atime(dir);
4718 dprintk("%s: returns %d\n", __func__, status);
4719 return status;
4722 static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
4723 u64 cookie, struct page **pages, unsigned int count, bool plus)
4725 struct nfs4_exception exception = { };
4726 int err;
4727 do {
4728 err = _nfs4_proc_readdir(dentry, cred, cookie,
4729 pages, count, plus);
4730 trace_nfs4_readdir(d_inode(dentry), err);
4731 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
4732 &exception);
4733 } while (exception.retry);
4734 return err;
4737 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4738 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
4740 struct nfs4_createdata *data;
4741 int mode = sattr->ia_mode;
4742 int status = -ENOMEM;
4744 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4745 if (data == NULL)
4746 goto out;
4748 if (S_ISFIFO(mode))
4749 data->arg.ftype = NF4FIFO;
4750 else if (S_ISBLK(mode)) {
4751 data->arg.ftype = NF4BLK;
4752 data->arg.u.device.specdata1 = MAJOR(rdev);
4753 data->arg.u.device.specdata2 = MINOR(rdev);
4755 else if (S_ISCHR(mode)) {
4756 data->arg.ftype = NF4CHR;
4757 data->arg.u.device.specdata1 = MAJOR(rdev);
4758 data->arg.u.device.specdata2 = MINOR(rdev);
4759 } else if (!S_ISSOCK(mode)) {
4760 status = -EINVAL;
4761 goto out_free;
4764 data->arg.label = label;
4765 status = nfs4_do_create(dir, dentry, data);
4766 out_free:
4767 nfs4_free_createdata(data);
4768 out:
4769 return status;
4772 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4773 struct iattr *sattr, dev_t rdev)
4775 struct nfs_server *server = NFS_SERVER(dir);
4776 struct nfs4_exception exception = { };
4777 struct nfs4_label l, *label = NULL;
4778 int err;
4780 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4782 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4783 sattr->ia_mode &= ~current_umask();
4784 do {
4785 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4786 trace_nfs4_mknod(dir, &dentry->d_name, err);
4787 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4788 &exception);
4789 } while (exception.retry);
4791 nfs4_label_release_security(label);
4793 return err;
4796 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4797 struct nfs_fsstat *fsstat)
4799 struct nfs4_statfs_arg args = {
4800 .fh = fhandle,
4801 .bitmask = server->attr_bitmask,
4803 struct nfs4_statfs_res res = {
4804 .fsstat = fsstat,
4806 struct rpc_message msg = {
4807 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4808 .rpc_argp = &args,
4809 .rpc_resp = &res,
4812 nfs_fattr_init(fsstat->fattr);
4813 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4816 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4818 struct nfs4_exception exception = { };
4819 int err;
4820 do {
4821 err = nfs4_handle_exception(server,
4822 _nfs4_proc_statfs(server, fhandle, fsstat),
4823 &exception);
4824 } while (exception.retry);
4825 return err;
4828 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4829 struct nfs_fsinfo *fsinfo)
4831 struct nfs4_fsinfo_arg args = {
4832 .fh = fhandle,
4833 .bitmask = server->attr_bitmask,
4835 struct nfs4_fsinfo_res res = {
4836 .fsinfo = fsinfo,
4838 struct rpc_message msg = {
4839 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4840 .rpc_argp = &args,
4841 .rpc_resp = &res,
4844 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4847 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4849 struct nfs4_exception exception = { };
4850 unsigned long now = jiffies;
4851 int err;
4853 do {
4854 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
4855 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
4856 if (err == 0) {
4857 nfs4_set_lease_period(server->nfs_client,
4858 fsinfo->lease_time * HZ,
4859 now);
4860 break;
4862 err = nfs4_handle_exception(server, err, &exception);
4863 } while (exception.retry);
4864 return err;
4867 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4869 int error;
4871 nfs_fattr_init(fsinfo->fattr);
4872 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
4873 if (error == 0) {
4874 /* block layout checks this! */
4875 server->pnfs_blksize = fsinfo->blksize;
4876 set_pnfs_layoutdriver(server, fhandle, fsinfo);
4879 return error;
4882 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4883 struct nfs_pathconf *pathconf)
4885 struct nfs4_pathconf_arg args = {
4886 .fh = fhandle,
4887 .bitmask = server->attr_bitmask,
4889 struct nfs4_pathconf_res res = {
4890 .pathconf = pathconf,
4892 struct rpc_message msg = {
4893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4894 .rpc_argp = &args,
4895 .rpc_resp = &res,
4898 /* None of the pathconf attributes are mandatory to implement */
4899 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4900 memset(pathconf, 0, sizeof(*pathconf));
4901 return 0;
4904 nfs_fattr_init(pathconf->fattr);
4905 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4908 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4909 struct nfs_pathconf *pathconf)
4911 struct nfs4_exception exception = { };
4912 int err;
4914 do {
4915 err = nfs4_handle_exception(server,
4916 _nfs4_proc_pathconf(server, fhandle, pathconf),
4917 &exception);
4918 } while (exception.retry);
4919 return err;
4922 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
4923 const struct nfs_open_context *ctx,
4924 const struct nfs_lock_context *l_ctx,
4925 fmode_t fmode)
4927 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
4929 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4931 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4932 const struct nfs_open_context *ctx,
4933 const struct nfs_lock_context *l_ctx,
4934 fmode_t fmode)
4936 nfs4_stateid current_stateid;
4938 /* If the current stateid represents a lost lock, then exit */
4939 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4940 return true;
4941 return nfs4_stateid_match(stateid, &current_stateid);
4944 static bool nfs4_error_stateid_expired(int err)
4946 switch (err) {
4947 case -NFS4ERR_DELEG_REVOKED:
4948 case -NFS4ERR_ADMIN_REVOKED:
4949 case -NFS4ERR_BAD_STATEID:
4950 case -NFS4ERR_STALE_STATEID:
4951 case -NFS4ERR_OLD_STATEID:
4952 case -NFS4ERR_OPENMODE:
4953 case -NFS4ERR_EXPIRED:
4954 return true;
4956 return false;
4959 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
4961 struct nfs_server *server = NFS_SERVER(hdr->inode);
4963 trace_nfs4_read(hdr, task->tk_status);
4964 if (task->tk_status < 0) {
4965 struct nfs4_exception exception = {
4966 .inode = hdr->inode,
4967 .state = hdr->args.context->state,
4968 .stateid = &hdr->args.stateid,
4970 task->tk_status = nfs4_async_handle_exception(task,
4971 server, task->tk_status, &exception);
4972 if (exception.retry) {
4973 rpc_restart_call_prepare(task);
4974 return -EAGAIN;
4978 if (task->tk_status > 0)
4979 renew_lease(server, hdr->timestamp);
4980 return 0;
4983 static bool nfs4_read_stateid_changed(struct rpc_task *task,
4984 struct nfs_pgio_args *args)
4987 if (!nfs4_error_stateid_expired(task->tk_status) ||
4988 nfs4_stateid_is_current(&args->stateid,
4989 args->context,
4990 args->lock_context,
4991 FMODE_READ))
4992 return false;
4993 rpc_restart_call_prepare(task);
4994 return true;
4997 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5000 dprintk("--> %s\n", __func__);
5002 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5003 return -EAGAIN;
5004 if (nfs4_read_stateid_changed(task, &hdr->args))
5005 return -EAGAIN;
5006 if (task->tk_status > 0)
5007 nfs_invalidate_atime(hdr->inode);
5008 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5009 nfs4_read_done_cb(task, hdr);
5012 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5013 struct rpc_message *msg)
5015 hdr->timestamp = jiffies;
5016 if (!hdr->pgio_done_cb)
5017 hdr->pgio_done_cb = nfs4_read_done_cb;
5018 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5019 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5022 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5023 struct nfs_pgio_header *hdr)
5025 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5026 &hdr->args.seq_args,
5027 &hdr->res.seq_res,
5028 task))
5029 return 0;
5030 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5031 hdr->args.lock_context,
5032 hdr->rw_mode) == -EIO)
5033 return -EIO;
5034 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5035 return -EIO;
5036 return 0;
5039 static int nfs4_write_done_cb(struct rpc_task *task,
5040 struct nfs_pgio_header *hdr)
5042 struct inode *inode = hdr->inode;
5044 trace_nfs4_write(hdr, task->tk_status);
5045 if (task->tk_status < 0) {
5046 struct nfs4_exception exception = {
5047 .inode = hdr->inode,
5048 .state = hdr->args.context->state,
5049 .stateid = &hdr->args.stateid,
5051 task->tk_status = nfs4_async_handle_exception(task,
5052 NFS_SERVER(inode), task->tk_status,
5053 &exception);
5054 if (exception.retry) {
5055 rpc_restart_call_prepare(task);
5056 return -EAGAIN;
5059 if (task->tk_status >= 0) {
5060 renew_lease(NFS_SERVER(inode), hdr->timestamp);
5061 nfs_writeback_update_inode(hdr);
5063 return 0;
5066 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5067 struct nfs_pgio_args *args)
5070 if (!nfs4_error_stateid_expired(task->tk_status) ||
5071 nfs4_stateid_is_current(&args->stateid,
5072 args->context,
5073 args->lock_context,
5074 FMODE_WRITE))
5075 return false;
5076 rpc_restart_call_prepare(task);
5077 return true;
5080 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5082 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5083 return -EAGAIN;
5084 if (nfs4_write_stateid_changed(task, &hdr->args))
5085 return -EAGAIN;
5086 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5087 nfs4_write_done_cb(task, hdr);
5090 static
5091 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5093 /* Don't request attributes for pNFS or O_DIRECT writes */
5094 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5095 return false;
5096 /* Otherwise, request attributes if and only if we don't hold
5097 * a delegation
5099 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
5102 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5103 struct rpc_message *msg,
5104 struct rpc_clnt **clnt)
5106 struct nfs_server *server = NFS_SERVER(hdr->inode);
5108 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5109 hdr->args.bitmask = NULL;
5110 hdr->res.fattr = NULL;
5111 } else
5112 hdr->args.bitmask = server->cache_consistency_bitmask;
5114 if (!hdr->pgio_done_cb)
5115 hdr->pgio_done_cb = nfs4_write_done_cb;
5116 hdr->res.server = server;
5117 hdr->timestamp = jiffies;
5119 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5120 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5121 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
5124 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5126 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5127 &data->args.seq_args,
5128 &data->res.seq_res,
5129 task);
5132 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5134 struct inode *inode = data->inode;
5136 trace_nfs4_commit(data, task->tk_status);
5137 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5138 NULL, NULL) == -EAGAIN) {
5139 rpc_restart_call_prepare(task);
5140 return -EAGAIN;
5142 return 0;
5145 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5147 if (!nfs4_sequence_done(task, &data->res.seq_res))
5148 return -EAGAIN;
5149 return data->commit_done_cb(task, data);
5152 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5153 struct rpc_clnt **clnt)
5155 struct nfs_server *server = NFS_SERVER(data->inode);
5157 if (data->commit_done_cb == NULL)
5158 data->commit_done_cb = nfs4_commit_done_cb;
5159 data->res.server = server;
5160 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5161 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5162 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5165 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5166 struct nfs_commitres *res)
5168 struct inode *dst_inode = file_inode(dst);
5169 struct nfs_server *server = NFS_SERVER(dst_inode);
5170 struct rpc_message msg = {
5171 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5172 .rpc_argp = args,
5173 .rpc_resp = res,
5176 args->fh = NFS_FH(dst_inode);
5177 return nfs4_call_sync(server->client, server, &msg,
5178 &args->seq_args, &res->seq_res, 1);
5181 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5183 struct nfs_commitargs args = {
5184 .offset = offset,
5185 .count = count,
5187 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5188 struct nfs4_exception exception = { };
5189 int status;
5191 do {
5192 status = _nfs4_proc_commit(dst, &args, res);
5193 status = nfs4_handle_exception(dst_server, status, &exception);
5194 } while (exception.retry);
5196 return status;
5199 struct nfs4_renewdata {
5200 struct nfs_client *client;
5201 unsigned long timestamp;
5205 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5206 * standalone procedure for queueing an asynchronous RENEW.
5208 static void nfs4_renew_release(void *calldata)
5210 struct nfs4_renewdata *data = calldata;
5211 struct nfs_client *clp = data->client;
5213 if (refcount_read(&clp->cl_count) > 1)
5214 nfs4_schedule_state_renewal(clp);
5215 nfs_put_client(clp);
5216 kfree(data);
5219 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5221 struct nfs4_renewdata *data = calldata;
5222 struct nfs_client *clp = data->client;
5223 unsigned long timestamp = data->timestamp;
5225 trace_nfs4_renew_async(clp, task->tk_status);
5226 switch (task->tk_status) {
5227 case 0:
5228 break;
5229 case -NFS4ERR_LEASE_MOVED:
5230 nfs4_schedule_lease_moved_recovery(clp);
5231 break;
5232 default:
5233 /* Unless we're shutting down, schedule state recovery! */
5234 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5235 return;
5236 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5237 nfs4_schedule_lease_recovery(clp);
5238 return;
5240 nfs4_schedule_path_down_recovery(clp);
5242 do_renew_lease(clp, timestamp);
5245 static const struct rpc_call_ops nfs4_renew_ops = {
5246 .rpc_call_done = nfs4_renew_done,
5247 .rpc_release = nfs4_renew_release,
5250 static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
5252 struct rpc_message msg = {
5253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5254 .rpc_argp = clp,
5255 .rpc_cred = cred,
5257 struct nfs4_renewdata *data;
5259 if (renew_flags == 0)
5260 return 0;
5261 if (!refcount_inc_not_zero(&clp->cl_count))
5262 return -EIO;
5263 data = kmalloc(sizeof(*data), GFP_NOFS);
5264 if (data == NULL) {
5265 nfs_put_client(clp);
5266 return -ENOMEM;
5268 data->client = clp;
5269 data->timestamp = jiffies;
5270 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5271 &nfs4_renew_ops, data);
5274 static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
5276 struct rpc_message msg = {
5277 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5278 .rpc_argp = clp,
5279 .rpc_cred = cred,
5281 unsigned long now = jiffies;
5282 int status;
5284 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5285 if (status < 0)
5286 return status;
5287 do_renew_lease(clp, now);
5288 return 0;
5291 static inline int nfs4_server_supports_acls(struct nfs_server *server)
5293 return server->caps & NFS_CAP_ACLS;
5296 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5297 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5298 * the stack.
5300 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5302 static int buf_to_pages_noslab(const void *buf, size_t buflen,
5303 struct page **pages)
5305 struct page *newpage, **spages;
5306 int rc = 0;
5307 size_t len;
5308 spages = pages;
5310 do {
5311 len = min_t(size_t, PAGE_SIZE, buflen);
5312 newpage = alloc_page(GFP_KERNEL);
5314 if (newpage == NULL)
5315 goto unwind;
5316 memcpy(page_address(newpage), buf, len);
5317 buf += len;
5318 buflen -= len;
5319 *pages++ = newpage;
5320 rc++;
5321 } while (buflen != 0);
5323 return rc;
5325 unwind:
5326 for(; rc > 0; rc--)
5327 __free_page(spages[rc-1]);
5328 return -ENOMEM;
5331 struct nfs4_cached_acl {
5332 int cached;
5333 size_t len;
5334 char data[0];
5337 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5339 struct nfs_inode *nfsi = NFS_I(inode);
5341 spin_lock(&inode->i_lock);
5342 kfree(nfsi->nfs4_acl);
5343 nfsi->nfs4_acl = acl;
5344 spin_unlock(&inode->i_lock);
5347 static void nfs4_zap_acl_attr(struct inode *inode)
5349 nfs4_set_cached_acl(inode, NULL);
5352 static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5354 struct nfs_inode *nfsi = NFS_I(inode);
5355 struct nfs4_cached_acl *acl;
5356 int ret = -ENOENT;
5358 spin_lock(&inode->i_lock);
5359 acl = nfsi->nfs4_acl;
5360 if (acl == NULL)
5361 goto out;
5362 if (buf == NULL) /* user is just asking for length */
5363 goto out_len;
5364 if (acl->cached == 0)
5365 goto out;
5366 ret = -ERANGE; /* see getxattr(2) man page */
5367 if (acl->len > buflen)
5368 goto out;
5369 memcpy(buf, acl->data, acl->len);
5370 out_len:
5371 ret = acl->len;
5372 out:
5373 spin_unlock(&inode->i_lock);
5374 return ret;
5377 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len)
5379 struct nfs4_cached_acl *acl;
5380 size_t buflen = sizeof(*acl) + acl_len;
5382 if (buflen <= PAGE_SIZE) {
5383 acl = kmalloc(buflen, GFP_KERNEL);
5384 if (acl == NULL)
5385 goto out;
5386 acl->cached = 1;
5387 _copy_from_pages(acl->data, pages, pgbase, acl_len);
5388 } else {
5389 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5390 if (acl == NULL)
5391 goto out;
5392 acl->cached = 0;
5394 acl->len = acl_len;
5395 out:
5396 nfs4_set_cached_acl(inode, acl);
5400 * The getxattr API returns the required buffer length when called with a
5401 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5402 * the required buf. On a NULL buf, we send a page of data to the server
5403 * guessing that the ACL request can be serviced by a page. If so, we cache
5404 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5405 * the cache. If not so, we throw away the page, and cache the required
5406 * length. The next getxattr call will then produce another round trip to
5407 * the server, this time with the input buf of the required size.
5409 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5411 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
5412 struct nfs_getaclargs args = {
5413 .fh = NFS_FH(inode),
5414 .acl_pages = pages,
5415 .acl_len = buflen,
5417 struct nfs_getaclres res = {
5418 .acl_len = buflen,
5420 struct rpc_message msg = {
5421 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5422 .rpc_argp = &args,
5423 .rpc_resp = &res,
5425 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5426 int ret = -ENOMEM, i;
5428 if (npages > ARRAY_SIZE(pages))
5429 return -ERANGE;
5431 for (i = 0; i < npages; i++) {
5432 pages[i] = alloc_page(GFP_KERNEL);
5433 if (!pages[i])
5434 goto out_free;
5437 /* for decoding across pages */
5438 res.acl_scratch = alloc_page(GFP_KERNEL);
5439 if (!res.acl_scratch)
5440 goto out_free;
5442 args.acl_len = npages * PAGE_SIZE;
5444 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
5445 __func__, buf, buflen, npages, args.acl_len);
5446 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5447 &msg, &args.seq_args, &res.seq_res, 0);
5448 if (ret)
5449 goto out_free;
5451 /* Handle the case where the passed-in buffer is too short */
5452 if (res.acl_flags & NFS4_ACL_TRUNC) {
5453 /* Did the user only issue a request for the acl length? */
5454 if (buf == NULL)
5455 goto out_ok;
5456 ret = -ERANGE;
5457 goto out_free;
5459 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
5460 if (buf) {
5461 if (res.acl_len > buflen) {
5462 ret = -ERANGE;
5463 goto out_free;
5465 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
5467 out_ok:
5468 ret = res.acl_len;
5469 out_free:
5470 for (i = 0; i < npages; i++)
5471 if (pages[i])
5472 __free_page(pages[i]);
5473 if (res.acl_scratch)
5474 __free_page(res.acl_scratch);
5475 return ret;
5478 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5480 struct nfs4_exception exception = { };
5481 ssize_t ret;
5482 do {
5483 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
5484 trace_nfs4_get_acl(inode, ret);
5485 if (ret >= 0)
5486 break;
5487 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5488 } while (exception.retry);
5489 return ret;
5492 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5494 struct nfs_server *server = NFS_SERVER(inode);
5495 int ret;
5497 if (!nfs4_server_supports_acls(server))
5498 return -EOPNOTSUPP;
5499 ret = nfs_revalidate_inode(server, inode);
5500 if (ret < 0)
5501 return ret;
5502 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5503 nfs_zap_acl_cache(inode);
5504 ret = nfs4_read_cached_acl(inode, buf, buflen);
5505 if (ret != -ENOENT)
5506 /* -ENOENT is returned if there is no ACL or if there is an ACL
5507 * but no cached acl data, just the acl length */
5508 return ret;
5509 return nfs4_get_acl_uncached(inode, buf, buflen);
5512 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5514 struct nfs_server *server = NFS_SERVER(inode);
5515 struct page *pages[NFS4ACL_MAXPAGES];
5516 struct nfs_setaclargs arg = {
5517 .fh = NFS_FH(inode),
5518 .acl_pages = pages,
5519 .acl_len = buflen,
5521 struct nfs_setaclres res;
5522 struct rpc_message msg = {
5523 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5524 .rpc_argp = &arg,
5525 .rpc_resp = &res,
5527 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5528 int ret, i;
5530 if (!nfs4_server_supports_acls(server))
5531 return -EOPNOTSUPP;
5532 if (npages > ARRAY_SIZE(pages))
5533 return -ERANGE;
5534 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
5535 if (i < 0)
5536 return i;
5537 nfs4_inode_make_writeable(inode);
5538 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5541 * Free each page after tx, so the only ref left is
5542 * held by the network stack
5544 for (; i > 0; i--)
5545 put_page(pages[i-1]);
5548 * Acl update can result in inode attribute update.
5549 * so mark the attribute cache invalid.
5551 spin_lock(&inode->i_lock);
5552 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5553 | NFS_INO_INVALID_CTIME
5554 | NFS_INO_REVAL_FORCED;
5555 spin_unlock(&inode->i_lock);
5556 nfs_access_zap_cache(inode);
5557 nfs_zap_acl_cache(inode);
5558 return ret;
5561 static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5563 struct nfs4_exception exception = { };
5564 int err;
5565 do {
5566 err = __nfs4_proc_set_acl(inode, buf, buflen);
5567 trace_nfs4_set_acl(inode, err);
5568 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5569 &exception);
5570 } while (exception.retry);
5571 return err;
5574 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
5575 static int _nfs4_get_security_label(struct inode *inode, void *buf,
5576 size_t buflen)
5578 struct nfs_server *server = NFS_SERVER(inode);
5579 struct nfs_fattr fattr;
5580 struct nfs4_label label = {0, 0, buflen, buf};
5582 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5583 struct nfs4_getattr_arg arg = {
5584 .fh = NFS_FH(inode),
5585 .bitmask = bitmask,
5587 struct nfs4_getattr_res res = {
5588 .fattr = &fattr,
5589 .label = &label,
5590 .server = server,
5592 struct rpc_message msg = {
5593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
5594 .rpc_argp = &arg,
5595 .rpc_resp = &res,
5597 int ret;
5599 nfs_fattr_init(&fattr);
5601 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
5602 if (ret)
5603 return ret;
5604 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5605 return -ENOENT;
5606 if (buflen < label.len)
5607 return -ERANGE;
5608 return 0;
5611 static int nfs4_get_security_label(struct inode *inode, void *buf,
5612 size_t buflen)
5614 struct nfs4_exception exception = { };
5615 int err;
5617 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5618 return -EOPNOTSUPP;
5620 do {
5621 err = _nfs4_get_security_label(inode, buf, buflen);
5622 trace_nfs4_get_security_label(inode, err);
5623 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5624 &exception);
5625 } while (exception.retry);
5626 return err;
5629 static int _nfs4_do_set_security_label(struct inode *inode,
5630 struct nfs4_label *ilabel,
5631 struct nfs_fattr *fattr,
5632 struct nfs4_label *olabel)
5635 struct iattr sattr = {0};
5636 struct nfs_server *server = NFS_SERVER(inode);
5637 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
5638 struct nfs_setattrargs arg = {
5639 .fh = NFS_FH(inode),
5640 .iap = &sattr,
5641 .server = server,
5642 .bitmask = bitmask,
5643 .label = ilabel,
5645 struct nfs_setattrres res = {
5646 .fattr = fattr,
5647 .label = olabel,
5648 .server = server,
5650 struct rpc_message msg = {
5651 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5652 .rpc_argp = &arg,
5653 .rpc_resp = &res,
5655 int status;
5657 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
5659 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5660 if (status)
5661 dprintk("%s failed: %d\n", __func__, status);
5663 return status;
5666 static int nfs4_do_set_security_label(struct inode *inode,
5667 struct nfs4_label *ilabel,
5668 struct nfs_fattr *fattr,
5669 struct nfs4_label *olabel)
5671 struct nfs4_exception exception = { };
5672 int err;
5674 do {
5675 err = _nfs4_do_set_security_label(inode, ilabel,
5676 fattr, olabel);
5677 trace_nfs4_set_security_label(inode, err);
5678 err = nfs4_handle_exception(NFS_SERVER(inode), err,
5679 &exception);
5680 } while (exception.retry);
5681 return err;
5684 static int
5685 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
5687 struct nfs4_label ilabel, *olabel = NULL;
5688 struct nfs_fattr fattr;
5689 struct rpc_cred *cred;
5690 int status;
5692 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5693 return -EOPNOTSUPP;
5695 nfs_fattr_init(&fattr);
5697 ilabel.pi = 0;
5698 ilabel.lfs = 0;
5699 ilabel.label = (char *)buf;
5700 ilabel.len = buflen;
5702 cred = rpc_lookup_cred();
5703 if (IS_ERR(cred))
5704 return PTR_ERR(cred);
5706 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5707 if (IS_ERR(olabel)) {
5708 status = -PTR_ERR(olabel);
5709 goto out;
5712 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5713 if (status == 0)
5714 nfs_setsecurity(inode, &fattr, olabel);
5716 nfs4_label_free(olabel);
5717 out:
5718 put_rpccred(cred);
5719 return status;
5721 #endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5724 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5725 nfs4_verifier *bootverf)
5727 __be32 verf[2];
5729 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5730 /* An impossible timestamp guarantees this value
5731 * will never match a generated boot time. */
5732 verf[0] = cpu_to_be32(U32_MAX);
5733 verf[1] = cpu_to_be32(U32_MAX);
5734 } else {
5735 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
5736 u64 ns = ktime_to_ns(nn->boot_time);
5738 verf[0] = cpu_to_be32(ns >> 32);
5739 verf[1] = cpu_to_be32(ns);
5741 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5744 static int
5745 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
5747 size_t len;
5748 char *str;
5750 if (clp->cl_owner_id != NULL)
5751 return 0;
5753 rcu_read_lock();
5754 len = 14 +
5755 strlen(clp->cl_rpcclient->cl_nodename) +
5757 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5759 rcu_read_unlock();
5760 if (nfs4_client_id_uniquifier[0] != '\0')
5761 len += strlen(nfs4_client_id_uniquifier) + 1;
5762 if (len > NFS4_OPAQUE_LIMIT + 1)
5763 return -EINVAL;
5766 * Since this string is allocated at mount time, and held until the
5767 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5768 * about a memory-reclaim deadlock.
5770 str = kmalloc(len, GFP_KERNEL);
5771 if (!str)
5772 return -ENOMEM;
5774 rcu_read_lock();
5775 if (nfs4_client_id_uniquifier[0] != '\0')
5776 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
5777 clp->cl_rpcclient->cl_nodename,
5778 nfs4_client_id_uniquifier,
5779 rpc_peeraddr2str(clp->cl_rpcclient,
5780 RPC_DISPLAY_ADDR));
5781 else
5782 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
5783 clp->cl_rpcclient->cl_nodename,
5784 rpc_peeraddr2str(clp->cl_rpcclient,
5785 RPC_DISPLAY_ADDR));
5786 rcu_read_unlock();
5788 clp->cl_owner_id = str;
5789 return 0;
5792 static int
5793 nfs4_init_uniquifier_client_string(struct nfs_client *clp)
5795 size_t len;
5796 char *str;
5798 len = 10 + 10 + 1 + 10 + 1 +
5799 strlen(nfs4_client_id_uniquifier) + 1 +
5800 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5802 if (len > NFS4_OPAQUE_LIMIT + 1)
5803 return -EINVAL;
5806 * Since this string is allocated at mount time, and held until the
5807 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5808 * about a memory-reclaim deadlock.
5810 str = kmalloc(len, GFP_KERNEL);
5811 if (!str)
5812 return -ENOMEM;
5814 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
5815 clp->rpc_ops->version, clp->cl_minorversion,
5816 nfs4_client_id_uniquifier,
5817 clp->cl_rpcclient->cl_nodename);
5818 clp->cl_owner_id = str;
5819 return 0;
5822 static int
5823 nfs4_init_uniform_client_string(struct nfs_client *clp)
5825 size_t len;
5826 char *str;
5828 if (clp->cl_owner_id != NULL)
5829 return 0;
5831 if (nfs4_client_id_uniquifier[0] != '\0')
5832 return nfs4_init_uniquifier_client_string(clp);
5834 len = 10 + 10 + 1 + 10 + 1 +
5835 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5837 if (len > NFS4_OPAQUE_LIMIT + 1)
5838 return -EINVAL;
5841 * Since this string is allocated at mount time, and held until the
5842 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5843 * about a memory-reclaim deadlock.
5845 str = kmalloc(len, GFP_KERNEL);
5846 if (!str)
5847 return -ENOMEM;
5849 scnprintf(str, len, "Linux NFSv%u.%u %s",
5850 clp->rpc_ops->version, clp->cl_minorversion,
5851 clp->cl_rpcclient->cl_nodename);
5852 clp->cl_owner_id = str;
5853 return 0;
5857 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5858 * services. Advertise one based on the address family of the
5859 * clientaddr.
5861 static unsigned int
5862 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5864 if (strchr(clp->cl_ipaddr, ':') != NULL)
5865 return scnprintf(buf, len, "tcp6");
5866 else
5867 return scnprintf(buf, len, "tcp");
5870 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5872 struct nfs4_setclientid *sc = calldata;
5874 if (task->tk_status == 0)
5875 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5878 static const struct rpc_call_ops nfs4_setclientid_ops = {
5879 .rpc_call_done = nfs4_setclientid_done,
5883 * nfs4_proc_setclientid - Negotiate client ID
5884 * @clp: state data structure
5885 * @program: RPC program for NFSv4 callback service
5886 * @port: IP port number for NFS4 callback service
5887 * @cred: RPC credential to use for this call
5888 * @res: where to place the result
5890 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5892 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5893 unsigned short port, struct rpc_cred *cred,
5894 struct nfs4_setclientid_res *res)
5896 nfs4_verifier sc_verifier;
5897 struct nfs4_setclientid setclientid = {
5898 .sc_verifier = &sc_verifier,
5899 .sc_prog = program,
5900 .sc_clnt = clp,
5902 struct rpc_message msg = {
5903 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5904 .rpc_argp = &setclientid,
5905 .rpc_resp = res,
5906 .rpc_cred = cred,
5908 struct rpc_task *task;
5909 struct rpc_task_setup task_setup_data = {
5910 .rpc_client = clp->cl_rpcclient,
5911 .rpc_message = &msg,
5912 .callback_ops = &nfs4_setclientid_ops,
5913 .callback_data = &setclientid,
5914 .flags = RPC_TASK_TIMEOUT,
5916 int status;
5918 /* nfs_client_id4 */
5919 nfs4_init_boot_verifier(clp, &sc_verifier);
5921 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5922 status = nfs4_init_uniform_client_string(clp);
5923 else
5924 status = nfs4_init_nonuniform_client_string(clp);
5926 if (status)
5927 goto out;
5929 /* cb_client4 */
5930 setclientid.sc_netid_len =
5931 nfs4_init_callback_netid(clp,
5932 setclientid.sc_netid,
5933 sizeof(setclientid.sc_netid));
5934 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
5935 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
5936 clp->cl_ipaddr, port >> 8, port & 255);
5938 dprintk("NFS call setclientid auth=%s, '%s'\n",
5939 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5940 clp->cl_owner_id);
5941 task = rpc_run_task(&task_setup_data);
5942 if (IS_ERR(task)) {
5943 status = PTR_ERR(task);
5944 goto out;
5946 status = task->tk_status;
5947 if (setclientid.sc_cred) {
5948 kfree(clp->cl_acceptor);
5949 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5950 put_rpccred(setclientid.sc_cred);
5952 rpc_put_task(task);
5953 out:
5954 trace_nfs4_setclientid(clp, status);
5955 dprintk("NFS reply setclientid: %d\n", status);
5956 return status;
5960 * nfs4_proc_setclientid_confirm - Confirm client ID
5961 * @clp: state data structure
5962 * @res: result of a previous SETCLIENTID
5963 * @cred: RPC credential to use for this call
5965 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5967 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
5968 struct nfs4_setclientid_res *arg,
5969 struct rpc_cred *cred)
5971 struct rpc_message msg = {
5972 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
5973 .rpc_argp = arg,
5974 .rpc_cred = cred,
5976 int status;
5978 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5979 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5980 clp->cl_clientid);
5981 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5982 trace_nfs4_setclientid_confirm(clp, status);
5983 dprintk("NFS reply setclientid_confirm: %d\n", status);
5984 return status;
5987 struct nfs4_delegreturndata {
5988 struct nfs4_delegreturnargs args;
5989 struct nfs4_delegreturnres res;
5990 struct nfs_fh fh;
5991 nfs4_stateid stateid;
5992 unsigned long timestamp;
5993 struct {
5994 struct nfs4_layoutreturn_args arg;
5995 struct nfs4_layoutreturn_res res;
5996 struct nfs4_xdr_opaque_data ld_private;
5997 u32 roc_barrier;
5998 bool roc;
5999 } lr;
6000 struct nfs_fattr fattr;
6001 int rpc_status;
6002 struct inode *inode;
6005 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6007 struct nfs4_delegreturndata *data = calldata;
6008 struct nfs4_exception exception = {
6009 .inode = data->inode,
6010 .stateid = &data->stateid,
6013 if (!nfs4_sequence_done(task, &data->res.seq_res))
6014 return;
6016 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6018 /* Handle Layoutreturn errors */
6019 if (data->args.lr_args && task->tk_status != 0) {
6020 switch(data->res.lr_ret) {
6021 default:
6022 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6023 break;
6024 case 0:
6025 data->args.lr_args = NULL;
6026 data->res.lr_res = NULL;
6027 break;
6028 case -NFS4ERR_OLD_STATEID:
6029 if (nfs4_layoutreturn_refresh_stateid(&data->args.lr_args->stateid,
6030 &data->args.lr_args->range,
6031 data->inode))
6032 goto lr_restart;
6033 /* Fallthrough */
6034 case -NFS4ERR_ADMIN_REVOKED:
6035 case -NFS4ERR_DELEG_REVOKED:
6036 case -NFS4ERR_EXPIRED:
6037 case -NFS4ERR_BAD_STATEID:
6038 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
6039 case -NFS4ERR_WRONG_CRED:
6040 data->args.lr_args = NULL;
6041 data->res.lr_res = NULL;
6042 goto lr_restart;
6046 switch (task->tk_status) {
6047 case 0:
6048 renew_lease(data->res.server, data->timestamp);
6049 break;
6050 case -NFS4ERR_ADMIN_REVOKED:
6051 case -NFS4ERR_DELEG_REVOKED:
6052 case -NFS4ERR_EXPIRED:
6053 nfs4_free_revoked_stateid(data->res.server,
6054 data->args.stateid,
6055 task->tk_msg.rpc_cred);
6056 /* Fallthrough */
6057 case -NFS4ERR_BAD_STATEID:
6058 case -NFS4ERR_STALE_STATEID:
6059 task->tk_status = 0;
6060 break;
6061 case -NFS4ERR_OLD_STATEID:
6062 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6063 goto out_restart;
6064 task->tk_status = 0;
6065 break;
6066 case -NFS4ERR_ACCESS:
6067 if (data->args.bitmask) {
6068 data->args.bitmask = NULL;
6069 data->res.fattr = NULL;
6070 goto out_restart;
6072 /* Fallthrough */
6073 default:
6074 task->tk_status = nfs4_async_handle_exception(task,
6075 data->res.server, task->tk_status,
6076 &exception);
6077 if (exception.retry)
6078 goto out_restart;
6080 data->rpc_status = task->tk_status;
6081 return;
6082 lr_restart:
6083 data->res.lr_ret = 0;
6084 out_restart:
6085 task->tk_status = 0;
6086 rpc_restart_call_prepare(task);
6089 static void nfs4_delegreturn_release(void *calldata)
6091 struct nfs4_delegreturndata *data = calldata;
6092 struct inode *inode = data->inode;
6094 if (inode) {
6095 if (data->lr.roc)
6096 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6097 data->res.lr_ret);
6098 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
6099 nfs_iput_and_deactive(inode);
6101 kfree(calldata);
6104 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6106 struct nfs4_delegreturndata *d_data;
6107 struct pnfs_layout_hdr *lo;
6109 d_data = (struct nfs4_delegreturndata *)data;
6111 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6112 nfs4_sequence_done(task, &d_data->res.seq_res);
6113 return;
6116 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6117 if (lo && !pnfs_layout_is_valid(lo)) {
6118 d_data->args.lr_args = NULL;
6119 d_data->res.lr_res = NULL;
6122 nfs4_setup_sequence(d_data->res.server->nfs_client,
6123 &d_data->args.seq_args,
6124 &d_data->res.seq_res,
6125 task);
6128 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6129 .rpc_call_prepare = nfs4_delegreturn_prepare,
6130 .rpc_call_done = nfs4_delegreturn_done,
6131 .rpc_release = nfs4_delegreturn_release,
6134 static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
6136 struct nfs4_delegreturndata *data;
6137 struct nfs_server *server = NFS_SERVER(inode);
6138 struct rpc_task *task;
6139 struct rpc_message msg = {
6140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6141 .rpc_cred = cred,
6143 struct rpc_task_setup task_setup_data = {
6144 .rpc_client = server->client,
6145 .rpc_message = &msg,
6146 .callback_ops = &nfs4_delegreturn_ops,
6147 .flags = RPC_TASK_ASYNC,
6149 int status = 0;
6151 data = kzalloc(sizeof(*data), GFP_NOFS);
6152 if (data == NULL)
6153 return -ENOMEM;
6154 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
6156 nfs4_state_protect(server->nfs_client,
6157 NFS_SP4_MACH_CRED_CLEANUP,
6158 &task_setup_data.rpc_client, &msg);
6160 data->args.fhandle = &data->fh;
6161 data->args.stateid = &data->stateid;
6162 data->args.bitmask = server->cache_consistency_bitmask;
6163 nfs_copy_fh(&data->fh, NFS_FH(inode));
6164 nfs4_stateid_copy(&data->stateid, stateid);
6165 data->res.fattr = &data->fattr;
6166 data->res.server = server;
6167 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6168 data->lr.arg.ld_private = &data->lr.ld_private;
6169 nfs_fattr_init(data->res.fattr);
6170 data->timestamp = jiffies;
6171 data->rpc_status = 0;
6172 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
6173 data->inode = nfs_igrab_and_active(inode);
6174 if (data->inode) {
6175 if (data->lr.roc) {
6176 data->args.lr_args = &data->lr.arg;
6177 data->res.lr_res = &data->lr.res;
6179 } else if (data->lr.roc) {
6180 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6181 data->lr.roc = false;
6184 task_setup_data.callback_data = data;
6185 msg.rpc_argp = &data->args;
6186 msg.rpc_resp = &data->res;
6187 task = rpc_run_task(&task_setup_data);
6188 if (IS_ERR(task))
6189 return PTR_ERR(task);
6190 if (!issync)
6191 goto out;
6192 status = rpc_wait_for_completion_task(task);
6193 if (status != 0)
6194 goto out;
6195 status = data->rpc_status;
6196 out:
6197 rpc_put_task(task);
6198 return status;
6201 int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
6203 struct nfs_server *server = NFS_SERVER(inode);
6204 struct nfs4_exception exception = { };
6205 int err;
6206 do {
6207 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
6208 trace_nfs4_delegreturn(inode, stateid, err);
6209 switch (err) {
6210 case -NFS4ERR_STALE_STATEID:
6211 case -NFS4ERR_EXPIRED:
6212 case 0:
6213 return 0;
6215 err = nfs4_handle_exception(server, err, &exception);
6216 } while (exception.retry);
6217 return err;
6220 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6222 struct inode *inode = state->inode;
6223 struct nfs_server *server = NFS_SERVER(inode);
6224 struct nfs_client *clp = server->nfs_client;
6225 struct nfs_lockt_args arg = {
6226 .fh = NFS_FH(inode),
6227 .fl = request,
6229 struct nfs_lockt_res res = {
6230 .denied = request,
6232 struct rpc_message msg = {
6233 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6234 .rpc_argp = &arg,
6235 .rpc_resp = &res,
6236 .rpc_cred = state->owner->so_cred,
6238 struct nfs4_lock_state *lsp;
6239 int status;
6241 arg.lock_owner.clientid = clp->cl_clientid;
6242 status = nfs4_set_lock_state(state, request);
6243 if (status != 0)
6244 goto out;
6245 lsp = request->fl_u.nfs4_fl.owner;
6246 arg.lock_owner.id = lsp->ls_seqid.owner_id;
6247 arg.lock_owner.s_dev = server->s_dev;
6248 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6249 switch (status) {
6250 case 0:
6251 request->fl_type = F_UNLCK;
6252 break;
6253 case -NFS4ERR_DENIED:
6254 status = 0;
6256 request->fl_ops->fl_release_private(request);
6257 request->fl_ops = NULL;
6258 out:
6259 return status;
6262 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6264 struct nfs4_exception exception = { };
6265 int err;
6267 do {
6268 err = _nfs4_proc_getlk(state, cmd, request);
6269 trace_nfs4_get_lock(request, state, cmd, err);
6270 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6271 &exception);
6272 } while (exception.retry);
6273 return err;
6276 struct nfs4_unlockdata {
6277 struct nfs_locku_args arg;
6278 struct nfs_locku_res res;
6279 struct nfs4_lock_state *lsp;
6280 struct nfs_open_context *ctx;
6281 struct nfs_lock_context *l_ctx;
6282 struct file_lock fl;
6283 struct nfs_server *server;
6284 unsigned long timestamp;
6287 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6288 struct nfs_open_context *ctx,
6289 struct nfs4_lock_state *lsp,
6290 struct nfs_seqid *seqid)
6292 struct nfs4_unlockdata *p;
6293 struct inode *inode = lsp->ls_state->inode;
6295 p = kzalloc(sizeof(*p), GFP_NOFS);
6296 if (p == NULL)
6297 return NULL;
6298 p->arg.fh = NFS_FH(inode);
6299 p->arg.fl = &p->fl;
6300 p->arg.seqid = seqid;
6301 p->res.seqid = seqid;
6302 p->lsp = lsp;
6303 refcount_inc(&lsp->ls_count);
6304 /* Ensure we don't close file until we're done freeing locks! */
6305 p->ctx = get_nfs_open_context(ctx);
6306 p->l_ctx = nfs_get_lock_context(ctx);
6307 memcpy(&p->fl, fl, sizeof(p->fl));
6308 p->server = NFS_SERVER(inode);
6309 return p;
6312 static void nfs4_locku_release_calldata(void *data)
6314 struct nfs4_unlockdata *calldata = data;
6315 nfs_free_seqid(calldata->arg.seqid);
6316 nfs4_put_lock_state(calldata->lsp);
6317 nfs_put_lock_context(calldata->l_ctx);
6318 put_nfs_open_context(calldata->ctx);
6319 kfree(calldata);
6322 static void nfs4_locku_done(struct rpc_task *task, void *data)
6324 struct nfs4_unlockdata *calldata = data;
6325 struct nfs4_exception exception = {
6326 .inode = calldata->lsp->ls_state->inode,
6327 .stateid = &calldata->arg.stateid,
6330 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6331 return;
6332 switch (task->tk_status) {
6333 case 0:
6334 renew_lease(calldata->server, calldata->timestamp);
6335 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
6336 if (nfs4_update_lock_stateid(calldata->lsp,
6337 &calldata->res.stateid))
6338 break;
6339 /* Fall through */
6340 case -NFS4ERR_ADMIN_REVOKED:
6341 case -NFS4ERR_EXPIRED:
6342 nfs4_free_revoked_stateid(calldata->server,
6343 &calldata->arg.stateid,
6344 task->tk_msg.rpc_cred);
6345 /* Fall through */
6346 case -NFS4ERR_BAD_STATEID:
6347 case -NFS4ERR_OLD_STATEID:
6348 case -NFS4ERR_STALE_STATEID:
6349 if (!nfs4_stateid_match(&calldata->arg.stateid,
6350 &calldata->lsp->ls_stateid))
6351 rpc_restart_call_prepare(task);
6352 break;
6353 default:
6354 task->tk_status = nfs4_async_handle_exception(task,
6355 calldata->server, task->tk_status,
6356 &exception);
6357 if (exception.retry)
6358 rpc_restart_call_prepare(task);
6360 nfs_release_seqid(calldata->arg.seqid);
6363 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
6365 struct nfs4_unlockdata *calldata = data;
6367 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6368 nfs_async_iocounter_wait(task, calldata->l_ctx))
6369 return;
6371 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
6372 goto out_wait;
6373 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
6374 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
6375 /* Note: exit _without_ running nfs4_locku_done */
6376 goto out_no_action;
6378 calldata->timestamp = jiffies;
6379 if (nfs4_setup_sequence(calldata->server->nfs_client,
6380 &calldata->arg.seq_args,
6381 &calldata->res.seq_res,
6382 task) != 0)
6383 nfs_release_seqid(calldata->arg.seqid);
6384 return;
6385 out_no_action:
6386 task->tk_action = NULL;
6387 out_wait:
6388 nfs4_sequence_done(task, &calldata->res.seq_res);
6391 static const struct rpc_call_ops nfs4_locku_ops = {
6392 .rpc_call_prepare = nfs4_locku_prepare,
6393 .rpc_call_done = nfs4_locku_done,
6394 .rpc_release = nfs4_locku_release_calldata,
6397 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6398 struct nfs_open_context *ctx,
6399 struct nfs4_lock_state *lsp,
6400 struct nfs_seqid *seqid)
6402 struct nfs4_unlockdata *data;
6403 struct rpc_message msg = {
6404 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6405 .rpc_cred = ctx->cred,
6407 struct rpc_task_setup task_setup_data = {
6408 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
6409 .rpc_message = &msg,
6410 .callback_ops = &nfs4_locku_ops,
6411 .workqueue = nfsiod_workqueue,
6412 .flags = RPC_TASK_ASYNC,
6415 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6416 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6418 /* Ensure this is an unlock - when canceling a lock, the
6419 * canceled lock is passed in, and it won't be an unlock.
6421 fl->fl_type = F_UNLCK;
6422 if (fl->fl_flags & FL_CLOSE)
6423 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
6425 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6426 if (data == NULL) {
6427 nfs_free_seqid(seqid);
6428 return ERR_PTR(-ENOMEM);
6431 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
6432 msg.rpc_argp = &data->arg;
6433 msg.rpc_resp = &data->res;
6434 task_setup_data.callback_data = data;
6435 return rpc_run_task(&task_setup_data);
6438 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6440 struct inode *inode = state->inode;
6441 struct nfs4_state_owner *sp = state->owner;
6442 struct nfs_inode *nfsi = NFS_I(inode);
6443 struct nfs_seqid *seqid;
6444 struct nfs4_lock_state *lsp;
6445 struct rpc_task *task;
6446 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6447 int status = 0;
6448 unsigned char fl_flags = request->fl_flags;
6450 status = nfs4_set_lock_state(state, request);
6451 /* Unlock _before_ we do the RPC call */
6452 request->fl_flags |= FL_EXISTS;
6453 /* Exclude nfs_delegation_claim_locks() */
6454 mutex_lock(&sp->so_delegreturn_mutex);
6455 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
6456 down_read(&nfsi->rwsem);
6457 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
6458 up_read(&nfsi->rwsem);
6459 mutex_unlock(&sp->so_delegreturn_mutex);
6460 goto out;
6462 up_read(&nfsi->rwsem);
6463 mutex_unlock(&sp->so_delegreturn_mutex);
6464 if (status != 0)
6465 goto out;
6466 /* Is this a delegated lock? */
6467 lsp = request->fl_u.nfs4_fl.owner;
6468 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6469 goto out;
6470 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6471 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
6472 status = -ENOMEM;
6473 if (IS_ERR(seqid))
6474 goto out;
6475 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
6476 status = PTR_ERR(task);
6477 if (IS_ERR(task))
6478 goto out;
6479 status = rpc_wait_for_completion_task(task);
6480 rpc_put_task(task);
6481 out:
6482 request->fl_flags = fl_flags;
6483 trace_nfs4_unlock(request, state, F_SETLK, status);
6484 return status;
6487 struct nfs4_lockdata {
6488 struct nfs_lock_args arg;
6489 struct nfs_lock_res res;
6490 struct nfs4_lock_state *lsp;
6491 struct nfs_open_context *ctx;
6492 struct file_lock fl;
6493 unsigned long timestamp;
6494 int rpc_status;
6495 int cancelled;
6496 struct nfs_server *server;
6499 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
6500 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6501 gfp_t gfp_mask)
6503 struct nfs4_lockdata *p;
6504 struct inode *inode = lsp->ls_state->inode;
6505 struct nfs_server *server = NFS_SERVER(inode);
6506 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
6508 p = kzalloc(sizeof(*p), gfp_mask);
6509 if (p == NULL)
6510 return NULL;
6512 p->arg.fh = NFS_FH(inode);
6513 p->arg.fl = &p->fl;
6514 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
6515 if (IS_ERR(p->arg.open_seqid))
6516 goto out_free;
6517 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6518 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
6519 if (IS_ERR(p->arg.lock_seqid))
6520 goto out_free_seqid;
6521 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
6522 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
6523 p->arg.lock_owner.s_dev = server->s_dev;
6524 p->res.lock_seqid = p->arg.lock_seqid;
6525 p->lsp = lsp;
6526 p->server = server;
6527 refcount_inc(&lsp->ls_count);
6528 p->ctx = get_nfs_open_context(ctx);
6529 memcpy(&p->fl, fl, sizeof(p->fl));
6530 return p;
6531 out_free_seqid:
6532 nfs_free_seqid(p->arg.open_seqid);
6533 out_free:
6534 kfree(p);
6535 return NULL;
6538 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6540 struct nfs4_lockdata *data = calldata;
6541 struct nfs4_state *state = data->lsp->ls_state;
6543 dprintk("%s: begin!\n", __func__);
6544 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
6545 goto out_wait;
6546 /* Do we need to do an open_to_lock_owner? */
6547 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
6548 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
6549 goto out_release_lock_seqid;
6551 nfs4_stateid_copy(&data->arg.open_stateid,
6552 &state->open_stateid);
6553 data->arg.new_lock_owner = 1;
6554 data->res.open_seqid = data->arg.open_seqid;
6555 } else {
6556 data->arg.new_lock_owner = 0;
6557 nfs4_stateid_copy(&data->arg.lock_stateid,
6558 &data->lsp->ls_stateid);
6560 if (!nfs4_valid_open_stateid(state)) {
6561 data->rpc_status = -EBADF;
6562 task->tk_action = NULL;
6563 goto out_release_open_seqid;
6565 data->timestamp = jiffies;
6566 if (nfs4_setup_sequence(data->server->nfs_client,
6567 &data->arg.seq_args,
6568 &data->res.seq_res,
6569 task) == 0)
6570 return;
6571 out_release_open_seqid:
6572 nfs_release_seqid(data->arg.open_seqid);
6573 out_release_lock_seqid:
6574 nfs_release_seqid(data->arg.lock_seqid);
6575 out_wait:
6576 nfs4_sequence_done(task, &data->res.seq_res);
6577 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
6580 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6582 struct nfs4_lockdata *data = calldata;
6583 struct nfs4_lock_state *lsp = data->lsp;
6585 dprintk("%s: begin!\n", __func__);
6587 if (!nfs4_sequence_done(task, &data->res.seq_res))
6588 return;
6590 data->rpc_status = task->tk_status;
6591 switch (task->tk_status) {
6592 case 0:
6593 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
6594 data->timestamp);
6595 if (data->arg.new_lock && !data->cancelled) {
6596 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
6597 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
6598 goto out_restart;
6600 if (data->arg.new_lock_owner != 0) {
6601 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6602 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6603 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6604 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6605 goto out_restart;
6606 break;
6607 case -NFS4ERR_BAD_STATEID:
6608 case -NFS4ERR_OLD_STATEID:
6609 case -NFS4ERR_STALE_STATEID:
6610 case -NFS4ERR_EXPIRED:
6611 if (data->arg.new_lock_owner != 0) {
6612 if (!nfs4_stateid_match(&data->arg.open_stateid,
6613 &lsp->ls_state->open_stateid))
6614 goto out_restart;
6615 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6616 &lsp->ls_stateid))
6617 goto out_restart;
6619 out_done:
6620 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
6621 return;
6622 out_restart:
6623 if (!data->cancelled)
6624 rpc_restart_call_prepare(task);
6625 goto out_done;
6628 static void nfs4_lock_release(void *calldata)
6630 struct nfs4_lockdata *data = calldata;
6632 dprintk("%s: begin!\n", __func__);
6633 nfs_free_seqid(data->arg.open_seqid);
6634 if (data->cancelled && data->rpc_status == 0) {
6635 struct rpc_task *task;
6636 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6637 data->arg.lock_seqid);
6638 if (!IS_ERR(task))
6639 rpc_put_task_async(task);
6640 dprintk("%s: cancelling lock!\n", __func__);
6641 } else
6642 nfs_free_seqid(data->arg.lock_seqid);
6643 nfs4_put_lock_state(data->lsp);
6644 put_nfs_open_context(data->ctx);
6645 kfree(data);
6646 dprintk("%s: done!\n", __func__);
6649 static const struct rpc_call_ops nfs4_lock_ops = {
6650 .rpc_call_prepare = nfs4_lock_prepare,
6651 .rpc_call_done = nfs4_lock_done,
6652 .rpc_release = nfs4_lock_release,
6655 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6657 switch (error) {
6658 case -NFS4ERR_ADMIN_REVOKED:
6659 case -NFS4ERR_EXPIRED:
6660 case -NFS4ERR_BAD_STATEID:
6661 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6662 if (new_lock_owner != 0 ||
6663 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
6664 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
6665 break;
6666 case -NFS4ERR_STALE_STATEID:
6667 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
6668 nfs4_schedule_lease_recovery(server->nfs_client);
6672 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
6674 struct nfs4_lockdata *data;
6675 struct rpc_task *task;
6676 struct rpc_message msg = {
6677 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6678 .rpc_cred = state->owner->so_cred,
6680 struct rpc_task_setup task_setup_data = {
6681 .rpc_client = NFS_CLIENT(state->inode),
6682 .rpc_message = &msg,
6683 .callback_ops = &nfs4_lock_ops,
6684 .workqueue = nfsiod_workqueue,
6685 .flags = RPC_TASK_ASYNC,
6687 int ret;
6689 dprintk("%s: begin!\n", __func__);
6690 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
6691 fl->fl_u.nfs4_fl.owner,
6692 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
6693 if (data == NULL)
6694 return -ENOMEM;
6695 if (IS_SETLKW(cmd))
6696 data->arg.block = 1;
6697 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6698 recovery_type > NFS_LOCK_NEW);
6699 msg.rpc_argp = &data->arg;
6700 msg.rpc_resp = &data->res;
6701 task_setup_data.callback_data = data;
6702 if (recovery_type > NFS_LOCK_NEW) {
6703 if (recovery_type == NFS_LOCK_RECLAIM)
6704 data->arg.reclaim = NFS_LOCK_RECLAIM;
6705 } else
6706 data->arg.new_lock = 1;
6707 task = rpc_run_task(&task_setup_data);
6708 if (IS_ERR(task))
6709 return PTR_ERR(task);
6710 ret = rpc_wait_for_completion_task(task);
6711 if (ret == 0) {
6712 ret = data->rpc_status;
6713 if (ret)
6714 nfs4_handle_setlk_error(data->server, data->lsp,
6715 data->arg.new_lock_owner, ret);
6716 } else
6717 data->cancelled = true;
6718 rpc_put_task(task);
6719 dprintk("%s: done, ret = %d!\n", __func__, ret);
6720 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
6721 return ret;
6724 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6726 struct nfs_server *server = NFS_SERVER(state->inode);
6727 struct nfs4_exception exception = {
6728 .inode = state->inode,
6730 int err;
6732 do {
6733 /* Cache the lock if possible... */
6734 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6735 return 0;
6736 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
6737 if (err != -NFS4ERR_DELAY)
6738 break;
6739 nfs4_handle_exception(server, err, &exception);
6740 } while (exception.retry);
6741 return err;
6744 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6746 struct nfs_server *server = NFS_SERVER(state->inode);
6747 struct nfs4_exception exception = {
6748 .inode = state->inode,
6750 int err;
6752 err = nfs4_set_lock_state(state, request);
6753 if (err != 0)
6754 return err;
6755 if (!recover_lost_locks) {
6756 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6757 return 0;
6759 do {
6760 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6761 return 0;
6762 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
6763 switch (err) {
6764 default:
6765 goto out;
6766 case -NFS4ERR_GRACE:
6767 case -NFS4ERR_DELAY:
6768 nfs4_handle_exception(server, err, &exception);
6769 err = 0;
6771 } while (exception.retry);
6772 out:
6773 return err;
6776 #if defined(CONFIG_NFS_V4_1)
6777 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6779 struct nfs4_lock_state *lsp;
6780 int status;
6782 status = nfs4_set_lock_state(state, request);
6783 if (status != 0)
6784 return status;
6785 lsp = request->fl_u.nfs4_fl.owner;
6786 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6787 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6788 return 0;
6789 return nfs4_lock_expired(state, request);
6791 #endif
6793 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6795 struct nfs_inode *nfsi = NFS_I(state->inode);
6796 struct nfs4_state_owner *sp = state->owner;
6797 unsigned char fl_flags = request->fl_flags;
6798 int status;
6800 request->fl_flags |= FL_ACCESS;
6801 status = locks_lock_inode_wait(state->inode, request);
6802 if (status < 0)
6803 goto out;
6804 mutex_lock(&sp->so_delegreturn_mutex);
6805 down_read(&nfsi->rwsem);
6806 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
6807 /* Yes: cache locks! */
6808 /* ...but avoid races with delegation recall... */
6809 request->fl_flags = fl_flags & ~FL_SLEEP;
6810 status = locks_lock_inode_wait(state->inode, request);
6811 up_read(&nfsi->rwsem);
6812 mutex_unlock(&sp->so_delegreturn_mutex);
6813 goto out;
6815 up_read(&nfsi->rwsem);
6816 mutex_unlock(&sp->so_delegreturn_mutex);
6817 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
6818 out:
6819 request->fl_flags = fl_flags;
6820 return status;
6823 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6825 struct nfs4_exception exception = {
6826 .state = state,
6827 .inode = state->inode,
6829 int err;
6831 do {
6832 err = _nfs4_proc_setlk(state, cmd, request);
6833 if (err == -NFS4ERR_DENIED)
6834 err = -EAGAIN;
6835 err = nfs4_handle_exception(NFS_SERVER(state->inode),
6836 err, &exception);
6837 } while (exception.retry);
6838 return err;
6841 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6842 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6844 static int
6845 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6846 struct file_lock *request)
6848 int status = -ERESTARTSYS;
6849 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6851 while(!signalled()) {
6852 status = nfs4_proc_setlk(state, cmd, request);
6853 if ((status != -EAGAIN) || IS_SETLK(cmd))
6854 break;
6855 freezable_schedule_timeout_interruptible(timeout);
6856 timeout *= 2;
6857 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6858 status = -ERESTARTSYS;
6860 return status;
6863 #ifdef CONFIG_NFS_V4_1
6864 struct nfs4_lock_waiter {
6865 struct task_struct *task;
6866 struct inode *inode;
6867 struct nfs_lowner *owner;
6870 static int
6871 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
6873 int ret;
6874 struct nfs4_lock_waiter *waiter = wait->private;
6876 /* NULL key means to wake up everyone */
6877 if (key) {
6878 struct cb_notify_lock_args *cbnl = key;
6879 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6880 *wowner = waiter->owner;
6882 /* Only wake if the callback was for the same owner. */
6883 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6884 return 0;
6886 /* Make sure it's for the right inode */
6887 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6888 return 0;
6891 /* override "private" so we can use default_wake_function */
6892 wait->private = waiter->task;
6893 ret = woken_wake_function(wait, mode, flags, key);
6894 if (ret)
6895 list_del_init(&wait->entry);
6896 wait->private = waiter;
6897 return ret;
6900 static int
6901 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6903 int status = -ERESTARTSYS;
6904 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6905 struct nfs_server *server = NFS_SERVER(state->inode);
6906 struct nfs_client *clp = server->nfs_client;
6907 wait_queue_head_t *q = &clp->cl_lock_waitq;
6908 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6909 .id = lsp->ls_seqid.owner_id,
6910 .s_dev = server->s_dev };
6911 struct nfs4_lock_waiter waiter = { .task = current,
6912 .inode = state->inode,
6913 .owner = &owner};
6914 wait_queue_entry_t wait;
6916 /* Don't bother with waitqueue if we don't expect a callback */
6917 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6918 return nfs4_retry_setlk_simple(state, cmd, request);
6920 init_wait(&wait);
6921 wait.private = &waiter;
6922 wait.func = nfs4_wake_lock_waiter;
6924 while(!signalled()) {
6925 add_wait_queue(q, &wait);
6926 status = nfs4_proc_setlk(state, cmd, request);
6927 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
6928 finish_wait(q, &wait);
6929 break;
6932 status = -ERESTARTSYS;
6933 freezer_do_not_count();
6934 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
6935 freezer_count();
6936 finish_wait(q, &wait);
6939 return status;
6941 #else /* !CONFIG_NFS_V4_1 */
6942 static inline int
6943 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6945 return nfs4_retry_setlk_simple(state, cmd, request);
6947 #endif
6949 static int
6950 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6952 struct nfs_open_context *ctx;
6953 struct nfs4_state *state;
6954 int status;
6956 /* verify open state */
6957 ctx = nfs_file_open_context(filp);
6958 state = ctx->state;
6960 if (IS_GETLK(cmd)) {
6961 if (state != NULL)
6962 return nfs4_proc_getlk(state, F_GETLK, request);
6963 return 0;
6966 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6967 return -EINVAL;
6969 if (request->fl_type == F_UNLCK) {
6970 if (state != NULL)
6971 return nfs4_proc_unlck(state, cmd, request);
6972 return 0;
6975 if (state == NULL)
6976 return -ENOLCK;
6978 if ((request->fl_flags & FL_POSIX) &&
6979 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6980 return -ENOLCK;
6983 * Don't rely on the VFS having checked the file open mode,
6984 * since it won't do this for flock() locks.
6986 switch (request->fl_type) {
6987 case F_RDLCK:
6988 if (!(filp->f_mode & FMODE_READ))
6989 return -EBADF;
6990 break;
6991 case F_WRLCK:
6992 if (!(filp->f_mode & FMODE_WRITE))
6993 return -EBADF;
6996 status = nfs4_set_lock_state(state, request);
6997 if (status != 0)
6998 return status;
7000 return nfs4_retry_setlk(state, cmd, request);
7003 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7005 struct nfs_server *server = NFS_SERVER(state->inode);
7006 int err;
7008 err = nfs4_set_lock_state(state, fl);
7009 if (err != 0)
7010 return err;
7011 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7012 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7015 struct nfs_release_lockowner_data {
7016 struct nfs4_lock_state *lsp;
7017 struct nfs_server *server;
7018 struct nfs_release_lockowner_args args;
7019 struct nfs_release_lockowner_res res;
7020 unsigned long timestamp;
7023 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7025 struct nfs_release_lockowner_data *data = calldata;
7026 struct nfs_server *server = data->server;
7027 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7028 &data->res.seq_res, task);
7029 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7030 data->timestamp = jiffies;
7033 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7035 struct nfs_release_lockowner_data *data = calldata;
7036 struct nfs_server *server = data->server;
7038 nfs40_sequence_done(task, &data->res.seq_res);
7040 switch (task->tk_status) {
7041 case 0:
7042 renew_lease(server, data->timestamp);
7043 break;
7044 case -NFS4ERR_STALE_CLIENTID:
7045 case -NFS4ERR_EXPIRED:
7046 nfs4_schedule_lease_recovery(server->nfs_client);
7047 break;
7048 case -NFS4ERR_LEASE_MOVED:
7049 case -NFS4ERR_DELAY:
7050 if (nfs4_async_handle_error(task, server,
7051 NULL, NULL) == -EAGAIN)
7052 rpc_restart_call_prepare(task);
7056 static void nfs4_release_lockowner_release(void *calldata)
7058 struct nfs_release_lockowner_data *data = calldata;
7059 nfs4_free_lock_state(data->server, data->lsp);
7060 kfree(calldata);
7063 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7064 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7065 .rpc_call_done = nfs4_release_lockowner_done,
7066 .rpc_release = nfs4_release_lockowner_release,
7069 static void
7070 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7072 struct nfs_release_lockowner_data *data;
7073 struct rpc_message msg = {
7074 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7077 if (server->nfs_client->cl_mvops->minor_version != 0)
7078 return;
7080 data = kmalloc(sizeof(*data), GFP_NOFS);
7081 if (!data)
7082 return;
7083 data->lsp = lsp;
7084 data->server = server;
7085 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7086 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7087 data->args.lock_owner.s_dev = server->s_dev;
7089 msg.rpc_argp = &data->args;
7090 msg.rpc_resp = &data->res;
7091 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7092 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7095 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7097 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7098 struct dentry *unused, struct inode *inode,
7099 const char *key, const void *buf,
7100 size_t buflen, int flags)
7102 return nfs4_proc_set_acl(inode, buf, buflen);
7105 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7106 struct dentry *unused, struct inode *inode,
7107 const char *key, void *buf, size_t buflen)
7109 return nfs4_proc_get_acl(inode, buf, buflen);
7112 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7114 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
7117 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7119 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7120 struct dentry *unused, struct inode *inode,
7121 const char *key, const void *buf,
7122 size_t buflen, int flags)
7124 if (security_ismaclabel(key))
7125 return nfs4_set_security_label(inode, buf, buflen);
7127 return -EOPNOTSUPP;
7130 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7131 struct dentry *unused, struct inode *inode,
7132 const char *key, void *buf, size_t buflen)
7134 if (security_ismaclabel(key))
7135 return nfs4_get_security_label(inode, buf, buflen);
7136 return -EOPNOTSUPP;
7139 static ssize_t
7140 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7142 int len = 0;
7144 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7145 len = security_inode_listsecurity(inode, list, list_len);
7146 if (list_len && len > list_len)
7147 return -ERANGE;
7149 return len;
7152 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7153 .prefix = XATTR_SECURITY_PREFIX,
7154 .get = nfs4_xattr_get_nfs4_label,
7155 .set = nfs4_xattr_set_nfs4_label,
7158 #else
7160 static ssize_t
7161 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7163 return 0;
7166 #endif
7169 * nfs_fhget will use either the mounted_on_fileid or the fileid
7171 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7173 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7174 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7175 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
7176 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
7177 return;
7179 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
7180 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
7181 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7182 fattr->nlink = 2;
7185 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7186 const struct qstr *name,
7187 struct nfs4_fs_locations *fs_locations,
7188 struct page *page)
7190 struct nfs_server *server = NFS_SERVER(dir);
7191 u32 bitmask[3];
7192 struct nfs4_fs_locations_arg args = {
7193 .dir_fh = NFS_FH(dir),
7194 .name = name,
7195 .page = page,
7196 .bitmask = bitmask,
7198 struct nfs4_fs_locations_res res = {
7199 .fs_locations = fs_locations,
7201 struct rpc_message msg = {
7202 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7203 .rpc_argp = &args,
7204 .rpc_resp = &res,
7206 int status;
7208 dprintk("%s: start\n", __func__);
7210 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7211 bitmask[1] = nfs4_fattr_bitmap[1];
7213 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7214 * is not supported */
7215 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
7216 bitmask[0] &= ~FATTR4_WORD0_FILEID;
7217 else
7218 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
7220 nfs_fattr_init(&fs_locations->fattr);
7221 fs_locations->server = server;
7222 fs_locations->nlocations = 0;
7223 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
7224 dprintk("%s: returned status = %d\n", __func__, status);
7225 return status;
7228 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7229 const struct qstr *name,
7230 struct nfs4_fs_locations *fs_locations,
7231 struct page *page)
7233 struct nfs4_exception exception = { };
7234 int err;
7235 do {
7236 err = _nfs4_proc_fs_locations(client, dir, name,
7237 fs_locations, page);
7238 trace_nfs4_get_fs_locations(dir, name, err);
7239 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7240 &exception);
7241 } while (exception.retry);
7242 return err;
7246 * This operation also signals the server that this client is
7247 * performing migration recovery. The server can stop returning
7248 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7249 * appended to this compound to identify the client ID which is
7250 * performing recovery.
7252 static int _nfs40_proc_get_locations(struct inode *inode,
7253 struct nfs4_fs_locations *locations,
7254 struct page *page, struct rpc_cred *cred)
7256 struct nfs_server *server = NFS_SERVER(inode);
7257 struct rpc_clnt *clnt = server->client;
7258 u32 bitmask[2] = {
7259 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7261 struct nfs4_fs_locations_arg args = {
7262 .clientid = server->nfs_client->cl_clientid,
7263 .fh = NFS_FH(inode),
7264 .page = page,
7265 .bitmask = bitmask,
7266 .migration = 1, /* skip LOOKUP */
7267 .renew = 1, /* append RENEW */
7269 struct nfs4_fs_locations_res res = {
7270 .fs_locations = locations,
7271 .migration = 1,
7272 .renew = 1,
7274 struct rpc_message msg = {
7275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7276 .rpc_argp = &args,
7277 .rpc_resp = &res,
7278 .rpc_cred = cred,
7280 unsigned long now = jiffies;
7281 int status;
7283 nfs_fattr_init(&locations->fattr);
7284 locations->server = server;
7285 locations->nlocations = 0;
7287 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7288 status = nfs4_call_sync_sequence(clnt, server, &msg,
7289 &args.seq_args, &res.seq_res);
7290 if (status)
7291 return status;
7293 renew_lease(server, now);
7294 return 0;
7297 #ifdef CONFIG_NFS_V4_1
7300 * This operation also signals the server that this client is
7301 * performing migration recovery. The server can stop asserting
7302 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7303 * performing this operation is identified in the SEQUENCE
7304 * operation in this compound.
7306 * When the client supports GETATTR(fs_locations_info), it can
7307 * be plumbed in here.
7309 static int _nfs41_proc_get_locations(struct inode *inode,
7310 struct nfs4_fs_locations *locations,
7311 struct page *page, struct rpc_cred *cred)
7313 struct nfs_server *server = NFS_SERVER(inode);
7314 struct rpc_clnt *clnt = server->client;
7315 u32 bitmask[2] = {
7316 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7318 struct nfs4_fs_locations_arg args = {
7319 .fh = NFS_FH(inode),
7320 .page = page,
7321 .bitmask = bitmask,
7322 .migration = 1, /* skip LOOKUP */
7324 struct nfs4_fs_locations_res res = {
7325 .fs_locations = locations,
7326 .migration = 1,
7328 struct rpc_message msg = {
7329 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7330 .rpc_argp = &args,
7331 .rpc_resp = &res,
7332 .rpc_cred = cred,
7334 int status;
7336 nfs_fattr_init(&locations->fattr);
7337 locations->server = server;
7338 locations->nlocations = 0;
7340 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7341 status = nfs4_call_sync_sequence(clnt, server, &msg,
7342 &args.seq_args, &res.seq_res);
7343 if (status == NFS4_OK &&
7344 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7345 status = -NFS4ERR_LEASE_MOVED;
7346 return status;
7349 #endif /* CONFIG_NFS_V4_1 */
7352 * nfs4_proc_get_locations - discover locations for a migrated FSID
7353 * @inode: inode on FSID that is migrating
7354 * @locations: result of query
7355 * @page: buffer
7356 * @cred: credential to use for this operation
7358 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7359 * operation failed, or a negative errno if a local error occurred.
7361 * On success, "locations" is filled in, but if the server has
7362 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7363 * asserted.
7365 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7366 * from this client that require migration recovery.
7368 int nfs4_proc_get_locations(struct inode *inode,
7369 struct nfs4_fs_locations *locations,
7370 struct page *page, struct rpc_cred *cred)
7372 struct nfs_server *server = NFS_SERVER(inode);
7373 struct nfs_client *clp = server->nfs_client;
7374 const struct nfs4_mig_recovery_ops *ops =
7375 clp->cl_mvops->mig_recovery_ops;
7376 struct nfs4_exception exception = { };
7377 int status;
7379 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7380 (unsigned long long)server->fsid.major,
7381 (unsigned long long)server->fsid.minor,
7382 clp->cl_hostname);
7383 nfs_display_fhandle(NFS_FH(inode), __func__);
7385 do {
7386 status = ops->get_locations(inode, locations, page, cred);
7387 if (status != -NFS4ERR_DELAY)
7388 break;
7389 nfs4_handle_exception(server, status, &exception);
7390 } while (exception.retry);
7391 return status;
7395 * This operation also signals the server that this client is
7396 * performing "lease moved" recovery. The server can stop
7397 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7398 * is appended to this compound to identify the client ID which is
7399 * performing recovery.
7401 static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7403 struct nfs_server *server = NFS_SERVER(inode);
7404 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7405 struct rpc_clnt *clnt = server->client;
7406 struct nfs4_fsid_present_arg args = {
7407 .fh = NFS_FH(inode),
7408 .clientid = clp->cl_clientid,
7409 .renew = 1, /* append RENEW */
7411 struct nfs4_fsid_present_res res = {
7412 .renew = 1,
7414 struct rpc_message msg = {
7415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7416 .rpc_argp = &args,
7417 .rpc_resp = &res,
7418 .rpc_cred = cred,
7420 unsigned long now = jiffies;
7421 int status;
7423 res.fh = nfs_alloc_fhandle();
7424 if (res.fh == NULL)
7425 return -ENOMEM;
7427 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7428 status = nfs4_call_sync_sequence(clnt, server, &msg,
7429 &args.seq_args, &res.seq_res);
7430 nfs_free_fhandle(res.fh);
7431 if (status)
7432 return status;
7434 do_renew_lease(clp, now);
7435 return 0;
7438 #ifdef CONFIG_NFS_V4_1
7441 * This operation also signals the server that this client is
7442 * performing "lease moved" recovery. The server can stop asserting
7443 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7444 * this operation is identified in the SEQUENCE operation in this
7445 * compound.
7447 static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7449 struct nfs_server *server = NFS_SERVER(inode);
7450 struct rpc_clnt *clnt = server->client;
7451 struct nfs4_fsid_present_arg args = {
7452 .fh = NFS_FH(inode),
7454 struct nfs4_fsid_present_res res = {
7456 struct rpc_message msg = {
7457 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7458 .rpc_argp = &args,
7459 .rpc_resp = &res,
7460 .rpc_cred = cred,
7462 int status;
7464 res.fh = nfs_alloc_fhandle();
7465 if (res.fh == NULL)
7466 return -ENOMEM;
7468 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
7469 status = nfs4_call_sync_sequence(clnt, server, &msg,
7470 &args.seq_args, &res.seq_res);
7471 nfs_free_fhandle(res.fh);
7472 if (status == NFS4_OK &&
7473 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7474 status = -NFS4ERR_LEASE_MOVED;
7475 return status;
7478 #endif /* CONFIG_NFS_V4_1 */
7481 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7482 * @inode: inode on FSID to check
7483 * @cred: credential to use for this operation
7485 * Server indicates whether the FSID is present, moved, or not
7486 * recognized. This operation is necessary to clear a LEASE_MOVED
7487 * condition for this client ID.
7489 * Returns NFS4_OK if the FSID is present on this server,
7490 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7491 * NFS4ERR code if some error occurred on the server, or a
7492 * negative errno if a local failure occurred.
7494 int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7496 struct nfs_server *server = NFS_SERVER(inode);
7497 struct nfs_client *clp = server->nfs_client;
7498 const struct nfs4_mig_recovery_ops *ops =
7499 clp->cl_mvops->mig_recovery_ops;
7500 struct nfs4_exception exception = { };
7501 int status;
7503 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7504 (unsigned long long)server->fsid.major,
7505 (unsigned long long)server->fsid.minor,
7506 clp->cl_hostname);
7507 nfs_display_fhandle(NFS_FH(inode), __func__);
7509 do {
7510 status = ops->fsid_present(inode, cred);
7511 if (status != -NFS4ERR_DELAY)
7512 break;
7513 nfs4_handle_exception(server, status, &exception);
7514 } while (exception.retry);
7515 return status;
7519 * If 'use_integrity' is true and the state managment nfs_client
7520 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7521 * and the machine credential as per RFC3530bis and RFC5661 Security
7522 * Considerations sections. Otherwise, just use the user cred with the
7523 * filesystem's rpc_client.
7525 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
7527 int status;
7528 struct nfs4_secinfo_arg args = {
7529 .dir_fh = NFS_FH(dir),
7530 .name = name,
7532 struct nfs4_secinfo_res res = {
7533 .flavors = flavors,
7535 struct rpc_message msg = {
7536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7537 .rpc_argp = &args,
7538 .rpc_resp = &res,
7540 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7541 struct rpc_cred *cred = NULL;
7543 if (use_integrity) {
7544 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
7545 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7546 msg.rpc_cred = cred;
7549 dprintk("NFS call secinfo %s\n", name->name);
7551 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7552 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7554 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7555 &res.seq_res, 0);
7556 dprintk("NFS reply secinfo: %d\n", status);
7558 if (cred)
7559 put_rpccred(cred);
7561 return status;
7564 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7565 struct nfs4_secinfo_flavors *flavors)
7567 struct nfs4_exception exception = { };
7568 int err;
7569 do {
7570 err = -NFS4ERR_WRONGSEC;
7572 /* try to use integrity protection with machine cred */
7573 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7574 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7577 * if unable to use integrity protection, or SECINFO with
7578 * integrity protection returns NFS4ERR_WRONGSEC (which is
7579 * disallowed by spec, but exists in deployed servers) use
7580 * the current filesystem's rpc_client and the user cred.
7582 if (err == -NFS4ERR_WRONGSEC)
7583 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7585 trace_nfs4_secinfo(dir, name, err);
7586 err = nfs4_handle_exception(NFS_SERVER(dir), err,
7587 &exception);
7588 } while (exception.retry);
7589 return err;
7592 #ifdef CONFIG_NFS_V4_1
7594 * Check the exchange flags returned by the server for invalid flags, having
7595 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7596 * DS flags set.
7598 static int nfs4_check_cl_exchange_flags(u32 flags)
7600 if (flags & ~EXCHGID4_FLAG_MASK_R)
7601 goto out_inval;
7602 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7603 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7604 goto out_inval;
7605 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7606 goto out_inval;
7607 return NFS_OK;
7608 out_inval:
7609 return -NFS4ERR_INVAL;
7612 static bool
7613 nfs41_same_server_scope(struct nfs41_server_scope *a,
7614 struct nfs41_server_scope *b)
7616 if (a->server_scope_sz != b->server_scope_sz)
7617 return false;
7618 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
7621 static void
7622 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7626 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7627 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
7631 * nfs4_proc_bind_one_conn_to_session()
7633 * The 4.1 client currently uses the same TCP connection for the
7634 * fore and backchannel.
7636 static
7637 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7638 struct rpc_xprt *xprt,
7639 struct nfs_client *clp,
7640 struct rpc_cred *cred)
7642 int status;
7643 struct nfs41_bind_conn_to_session_args args = {
7644 .client = clp,
7645 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7647 struct nfs41_bind_conn_to_session_res res;
7648 struct rpc_message msg = {
7649 .rpc_proc =
7650 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
7651 .rpc_argp = &args,
7652 .rpc_resp = &res,
7653 .rpc_cred = cred,
7655 struct rpc_task_setup task_setup_data = {
7656 .rpc_client = clnt,
7657 .rpc_xprt = xprt,
7658 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
7659 .rpc_message = &msg,
7660 .flags = RPC_TASK_TIMEOUT,
7662 struct rpc_task *task;
7664 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7665 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7666 args.dir = NFS4_CDFC4_FORE;
7668 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7669 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7670 args.dir = NFS4_CDFC4_FORE;
7672 task = rpc_run_task(&task_setup_data);
7673 if (!IS_ERR(task)) {
7674 status = task->tk_status;
7675 rpc_put_task(task);
7676 } else
7677 status = PTR_ERR(task);
7678 trace_nfs4_bind_conn_to_session(clp, status);
7679 if (status == 0) {
7680 if (memcmp(res.sessionid.data,
7681 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7682 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7683 return -EIO;
7685 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
7686 dprintk("NFS: %s: Unexpected direction from server\n",
7687 __func__);
7688 return -EIO;
7690 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
7691 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7692 __func__);
7693 return -EIO;
7697 return status;
7700 struct rpc_bind_conn_calldata {
7701 struct nfs_client *clp;
7702 struct rpc_cred *cred;
7705 static int
7706 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7707 struct rpc_xprt *xprt,
7708 void *calldata)
7710 struct rpc_bind_conn_calldata *p = calldata;
7712 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7715 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7717 struct rpc_bind_conn_calldata data = {
7718 .clp = clp,
7719 .cred = cred,
7721 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7722 nfs4_proc_bind_conn_to_session_callback, &data);
7726 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7727 * and operations we'd like to see to enable certain features in the allow map
7729 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7730 .how = SP4_MACH_CRED,
7731 .enforce.u.words = {
7732 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7733 1 << (OP_EXCHANGE_ID - 32) |
7734 1 << (OP_CREATE_SESSION - 32) |
7735 1 << (OP_DESTROY_SESSION - 32) |
7736 1 << (OP_DESTROY_CLIENTID - 32)
7738 .allow.u.words = {
7739 [0] = 1 << (OP_CLOSE) |
7740 1 << (OP_OPEN_DOWNGRADE) |
7741 1 << (OP_LOCKU) |
7742 1 << (OP_DELEGRETURN) |
7743 1 << (OP_COMMIT),
7744 [1] = 1 << (OP_SECINFO - 32) |
7745 1 << (OP_SECINFO_NO_NAME - 32) |
7746 1 << (OP_LAYOUTRETURN - 32) |
7747 1 << (OP_TEST_STATEID - 32) |
7748 1 << (OP_FREE_STATEID - 32) |
7749 1 << (OP_WRITE - 32)
7754 * Select the state protection mode for client `clp' given the server results
7755 * from exchange_id in `sp'.
7757 * Returns 0 on success, negative errno otherwise.
7759 static int nfs4_sp4_select_mode(struct nfs_client *clp,
7760 struct nfs41_state_protection *sp)
7762 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7763 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7764 1 << (OP_EXCHANGE_ID - 32) |
7765 1 << (OP_CREATE_SESSION - 32) |
7766 1 << (OP_DESTROY_SESSION - 32) |
7767 1 << (OP_DESTROY_CLIENTID - 32)
7769 unsigned long flags = 0;
7770 unsigned int i;
7771 int ret = 0;
7773 if (sp->how == SP4_MACH_CRED) {
7774 /* Print state protect result */
7775 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7776 for (i = 0; i <= LAST_NFS4_OP; i++) {
7777 if (test_bit(i, sp->enforce.u.longs))
7778 dfprintk(MOUNT, " enforce op %d\n", i);
7779 if (test_bit(i, sp->allow.u.longs))
7780 dfprintk(MOUNT, " allow op %d\n", i);
7783 /* make sure nothing is on enforce list that isn't supported */
7784 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7785 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7786 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7787 ret = -EINVAL;
7788 goto out;
7793 * Minimal mode - state operations are allowed to use machine
7794 * credential. Note this already happens by default, so the
7795 * client doesn't have to do anything more than the negotiation.
7797 * NOTE: we don't care if EXCHANGE_ID is in the list -
7798 * we're already using the machine cred for exchange_id
7799 * and will never use a different cred.
7801 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7802 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7803 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7804 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7805 dfprintk(MOUNT, "sp4_mach_cred:\n");
7806 dfprintk(MOUNT, " minimal mode enabled\n");
7807 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
7808 } else {
7809 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7810 ret = -EINVAL;
7811 goto out;
7814 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
7815 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7816 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
7817 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7818 dfprintk(MOUNT, " cleanup mode enabled\n");
7819 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
7822 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7823 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7824 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
7827 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7828 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7829 dfprintk(MOUNT, " secinfo mode enabled\n");
7830 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
7833 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7834 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7835 dfprintk(MOUNT, " stateid mode enabled\n");
7836 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
7839 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7840 dfprintk(MOUNT, " write mode enabled\n");
7841 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
7844 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7845 dfprintk(MOUNT, " commit mode enabled\n");
7846 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
7849 out:
7850 clp->cl_sp4_flags = flags;
7851 return ret;
7854 struct nfs41_exchange_id_data {
7855 struct nfs41_exchange_id_res res;
7856 struct nfs41_exchange_id_args args;
7859 static void nfs4_exchange_id_release(void *data)
7861 struct nfs41_exchange_id_data *cdata =
7862 (struct nfs41_exchange_id_data *)data;
7864 nfs_put_client(cdata->args.client);
7865 kfree(cdata->res.impl_id);
7866 kfree(cdata->res.server_scope);
7867 kfree(cdata->res.server_owner);
7868 kfree(cdata);
7871 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7872 .rpc_release = nfs4_exchange_id_release,
7876 * _nfs4_proc_exchange_id()
7878 * Wrapper for EXCHANGE_ID operation.
7880 static struct rpc_task *
7881 nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7882 u32 sp4_how, struct rpc_xprt *xprt)
7884 struct rpc_message msg = {
7885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7886 .rpc_cred = cred,
7888 struct rpc_task_setup task_setup_data = {
7889 .rpc_client = clp->cl_rpcclient,
7890 .callback_ops = &nfs4_exchange_id_call_ops,
7891 .rpc_message = &msg,
7892 .flags = RPC_TASK_TIMEOUT,
7894 struct nfs41_exchange_id_data *calldata;
7895 int status;
7897 if (!refcount_inc_not_zero(&clp->cl_count))
7898 return ERR_PTR(-EIO);
7900 status = -ENOMEM;
7901 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7902 if (!calldata)
7903 goto out;
7905 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
7907 status = nfs4_init_uniform_client_string(clp);
7908 if (status)
7909 goto out_calldata;
7911 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7912 GFP_NOFS);
7913 status = -ENOMEM;
7914 if (unlikely(calldata->res.server_owner == NULL))
7915 goto out_calldata;
7917 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
7918 GFP_NOFS);
7919 if (unlikely(calldata->res.server_scope == NULL))
7920 goto out_server_owner;
7922 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7923 if (unlikely(calldata->res.impl_id == NULL))
7924 goto out_server_scope;
7926 switch (sp4_how) {
7927 case SP4_NONE:
7928 calldata->args.state_protect.how = SP4_NONE;
7929 break;
7931 case SP4_MACH_CRED:
7932 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
7933 break;
7935 default:
7936 /* unsupported! */
7937 WARN_ON_ONCE(1);
7938 status = -EINVAL;
7939 goto out_impl_id;
7941 if (xprt) {
7942 task_setup_data.rpc_xprt = xprt;
7943 task_setup_data.flags |= RPC_TASK_SOFTCONN;
7944 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7945 sizeof(calldata->args.verifier.data));
7947 calldata->args.client = clp;
7948 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7949 EXCHGID4_FLAG_BIND_PRINC_STATEID;
7950 #ifdef CONFIG_NFS_V4_1_MIGRATION
7951 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
7952 #endif
7953 msg.rpc_argp = &calldata->args;
7954 msg.rpc_resp = &calldata->res;
7955 task_setup_data.callback_data = calldata;
7957 return rpc_run_task(&task_setup_data);
7959 out_impl_id:
7960 kfree(calldata->res.impl_id);
7961 out_server_scope:
7962 kfree(calldata->res.server_scope);
7963 out_server_owner:
7964 kfree(calldata->res.server_owner);
7965 out_calldata:
7966 kfree(calldata);
7967 out:
7968 nfs_put_client(clp);
7969 return ERR_PTR(status);
7973 * _nfs4_proc_exchange_id()
7975 * Wrapper for EXCHANGE_ID operation.
7977 static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7978 u32 sp4_how)
7980 struct rpc_task *task;
7981 struct nfs41_exchange_id_args *argp;
7982 struct nfs41_exchange_id_res *resp;
7983 int status;
7985 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7986 if (IS_ERR(task))
7987 return PTR_ERR(task);
7989 argp = task->tk_msg.rpc_argp;
7990 resp = task->tk_msg.rpc_resp;
7991 status = task->tk_status;
7992 if (status != 0)
7993 goto out;
7995 status = nfs4_check_cl_exchange_flags(resp->flags);
7996 if (status != 0)
7997 goto out;
7999 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8000 if (status != 0)
8001 goto out;
8003 clp->cl_clientid = resp->clientid;
8004 clp->cl_exchange_flags = resp->flags;
8005 clp->cl_seqid = resp->seqid;
8006 /* Client ID is not confirmed */
8007 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8008 clear_bit(NFS4_SESSION_ESTABLISHED,
8009 &clp->cl_session->session_state);
8011 if (clp->cl_serverscope != NULL &&
8012 !nfs41_same_server_scope(clp->cl_serverscope,
8013 resp->server_scope)) {
8014 dprintk("%s: server_scope mismatch detected\n",
8015 __func__);
8016 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8019 swap(clp->cl_serverowner, resp->server_owner);
8020 swap(clp->cl_serverscope, resp->server_scope);
8021 swap(clp->cl_implid, resp->impl_id);
8023 /* Save the EXCHANGE_ID verifier session trunk tests */
8024 memcpy(clp->cl_confirm.data, argp->verifier.data,
8025 sizeof(clp->cl_confirm.data));
8026 out:
8027 trace_nfs4_exchange_id(clp, status);
8028 rpc_put_task(task);
8029 return status;
8033 * nfs4_proc_exchange_id()
8035 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8037 * Since the clientid has expired, all compounds using sessions
8038 * associated with the stale clientid will be returning
8039 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8040 * be in some phase of session reset.
8042 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8044 int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
8046 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8047 int status;
8049 /* try SP4_MACH_CRED if krb5i/p */
8050 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8051 authflavor == RPC_AUTH_GSS_KRB5P) {
8052 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
8053 if (!status)
8054 return 0;
8057 /* try SP4_NONE */
8058 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
8062 * nfs4_test_session_trunk
8064 * This is an add_xprt_test() test function called from
8065 * rpc_clnt_setup_test_and_add_xprt.
8067 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8068 * and is dereferrenced in nfs4_exchange_id_release
8070 * Upon success, add the new transport to the rpc_clnt
8072 * @clnt: struct rpc_clnt to get new transport
8073 * @xprt: the rpc_xprt to test
8074 * @data: call data for _nfs4_proc_exchange_id.
8076 int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8077 void *data)
8079 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
8080 struct rpc_task *task;
8081 int status;
8083 u32 sp4_how;
8085 dprintk("--> %s try %s\n", __func__,
8086 xprt->address_strings[RPC_DISPLAY_ADDR]);
8088 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8090 /* Test connection for session trunking. Async exchange_id call */
8091 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8092 if (IS_ERR(task))
8093 return PTR_ERR(task);
8095 status = task->tk_status;
8096 if (status == 0)
8097 status = nfs4_detect_session_trunking(adata->clp,
8098 task->tk_msg.rpc_resp, xprt);
8100 rpc_put_task(task);
8101 return status;
8103 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8105 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8106 struct rpc_cred *cred)
8108 struct rpc_message msg = {
8109 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8110 .rpc_argp = clp,
8111 .rpc_cred = cred,
8113 int status;
8115 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8116 trace_nfs4_destroy_clientid(clp, status);
8117 if (status)
8118 dprintk("NFS: Got error %d from the server %s on "
8119 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8120 return status;
8123 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8124 struct rpc_cred *cred)
8126 unsigned int loop;
8127 int ret;
8129 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8130 ret = _nfs4_proc_destroy_clientid(clp, cred);
8131 switch (ret) {
8132 case -NFS4ERR_DELAY:
8133 case -NFS4ERR_CLIENTID_BUSY:
8134 ssleep(1);
8135 break;
8136 default:
8137 return ret;
8140 return 0;
8143 int nfs4_destroy_clientid(struct nfs_client *clp)
8145 struct rpc_cred *cred;
8146 int ret = 0;
8148 if (clp->cl_mvops->minor_version < 1)
8149 goto out;
8150 if (clp->cl_exchange_flags == 0)
8151 goto out;
8152 if (clp->cl_preserve_clid)
8153 goto out;
8154 cred = nfs4_get_clid_cred(clp);
8155 ret = nfs4_proc_destroy_clientid(clp, cred);
8156 if (cred)
8157 put_rpccred(cred);
8158 switch (ret) {
8159 case 0:
8160 case -NFS4ERR_STALE_CLIENTID:
8161 clp->cl_exchange_flags = 0;
8163 out:
8164 return ret;
8167 struct nfs4_get_lease_time_data {
8168 struct nfs4_get_lease_time_args *args;
8169 struct nfs4_get_lease_time_res *res;
8170 struct nfs_client *clp;
8173 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8174 void *calldata)
8176 struct nfs4_get_lease_time_data *data =
8177 (struct nfs4_get_lease_time_data *)calldata;
8179 dprintk("--> %s\n", __func__);
8180 /* just setup sequence, do not trigger session recovery
8181 since we're invoked within one */
8182 nfs4_setup_sequence(data->clp,
8183 &data->args->la_seq_args,
8184 &data->res->lr_seq_res,
8185 task);
8186 dprintk("<-- %s\n", __func__);
8190 * Called from nfs4_state_manager thread for session setup, so don't recover
8191 * from sequence operation or clientid errors.
8193 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8195 struct nfs4_get_lease_time_data *data =
8196 (struct nfs4_get_lease_time_data *)calldata;
8198 dprintk("--> %s\n", __func__);
8199 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
8200 return;
8201 switch (task->tk_status) {
8202 case -NFS4ERR_DELAY:
8203 case -NFS4ERR_GRACE:
8204 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8205 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8206 task->tk_status = 0;
8207 /* fall through */
8208 case -NFS4ERR_RETRY_UNCACHED_REP:
8209 rpc_restart_call_prepare(task);
8210 return;
8212 dprintk("<-- %s\n", __func__);
8215 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
8216 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8217 .rpc_call_done = nfs4_get_lease_time_done,
8220 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8222 struct rpc_task *task;
8223 struct nfs4_get_lease_time_args args;
8224 struct nfs4_get_lease_time_res res = {
8225 .lr_fsinfo = fsinfo,
8227 struct nfs4_get_lease_time_data data = {
8228 .args = &args,
8229 .res = &res,
8230 .clp = clp,
8232 struct rpc_message msg = {
8233 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8234 .rpc_argp = &args,
8235 .rpc_resp = &res,
8237 struct rpc_task_setup task_setup = {
8238 .rpc_client = clp->cl_rpcclient,
8239 .rpc_message = &msg,
8240 .callback_ops = &nfs4_get_lease_time_ops,
8241 .callback_data = &data,
8242 .flags = RPC_TASK_TIMEOUT,
8244 int status;
8246 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
8247 task = rpc_run_task(&task_setup);
8249 if (IS_ERR(task))
8250 return PTR_ERR(task);
8252 status = task->tk_status;
8253 rpc_put_task(task);
8254 return status;
8258 * Initialize the values to be used by the client in CREATE_SESSION
8259 * If nfs4_init_session set the fore channel request and response sizes,
8260 * use them.
8262 * Set the back channel max_resp_sz_cached to zero to force the client to
8263 * always set csa_cachethis to FALSE because the current implementation
8264 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8266 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8267 struct rpc_clnt *clnt)
8269 unsigned int max_rqst_sz, max_resp_sz;
8270 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
8272 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8273 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8275 /* Fore channel attributes */
8276 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8277 args->fc_attrs.max_resp_sz = max_resp_sz;
8278 args->fc_attrs.max_ops = NFS4_MAX_OPS;
8279 args->fc_attrs.max_reqs = max_session_slots;
8281 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
8282 "max_ops=%u max_reqs=%u\n",
8283 __func__,
8284 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
8285 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
8287 /* Back channel attributes */
8288 args->bc_attrs.max_rqst_sz = max_bc_payload;
8289 args->bc_attrs.max_resp_sz = max_bc_payload;
8290 args->bc_attrs.max_resp_sz_cached = 0;
8291 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
8292 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
8294 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8295 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8296 __func__,
8297 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8298 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8299 args->bc_attrs.max_reqs);
8302 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8303 struct nfs41_create_session_res *res)
8305 struct nfs4_channel_attrs *sent = &args->fc_attrs;
8306 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
8308 if (rcvd->max_resp_sz > sent->max_resp_sz)
8309 return -EINVAL;
8311 * Our requested max_ops is the minimum we need; we're not
8312 * prepared to break up compounds into smaller pieces than that.
8313 * So, no point even trying to continue if the server won't
8314 * cooperate:
8316 if (rcvd->max_ops < sent->max_ops)
8317 return -EINVAL;
8318 if (rcvd->max_reqs == 0)
8319 return -EINVAL;
8320 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8321 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
8322 return 0;
8325 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8326 struct nfs41_create_session_res *res)
8328 struct nfs4_channel_attrs *sent = &args->bc_attrs;
8329 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
8331 if (!(res->flags & SESSION4_BACK_CHAN))
8332 goto out;
8333 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8334 return -EINVAL;
8335 if (rcvd->max_resp_sz < sent->max_resp_sz)
8336 return -EINVAL;
8337 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8338 return -EINVAL;
8339 if (rcvd->max_ops > sent->max_ops)
8340 return -EINVAL;
8341 if (rcvd->max_reqs > sent->max_reqs)
8342 return -EINVAL;
8343 out:
8344 return 0;
8347 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
8348 struct nfs41_create_session_res *res)
8350 int ret;
8352 ret = nfs4_verify_fore_channel_attrs(args, res);
8353 if (ret)
8354 return ret;
8355 return nfs4_verify_back_channel_attrs(args, res);
8358 static void nfs4_update_session(struct nfs4_session *session,
8359 struct nfs41_create_session_res *res)
8361 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
8362 /* Mark client id and session as being confirmed */
8363 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8364 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
8365 session->flags = res->flags;
8366 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
8367 if (res->flags & SESSION4_BACK_CHAN)
8368 memcpy(&session->bc_attrs, &res->bc_attrs,
8369 sizeof(session->bc_attrs));
8372 static int _nfs4_proc_create_session(struct nfs_client *clp,
8373 struct rpc_cred *cred)
8375 struct nfs4_session *session = clp->cl_session;
8376 struct nfs41_create_session_args args = {
8377 .client = clp,
8378 .clientid = clp->cl_clientid,
8379 .seqid = clp->cl_seqid,
8380 .cb_program = NFS4_CALLBACK,
8382 struct nfs41_create_session_res res;
8384 struct rpc_message msg = {
8385 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8386 .rpc_argp = &args,
8387 .rpc_resp = &res,
8388 .rpc_cred = cred,
8390 int status;
8392 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
8393 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
8395 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8396 trace_nfs4_create_session(clp, status);
8398 switch (status) {
8399 case -NFS4ERR_STALE_CLIENTID:
8400 case -NFS4ERR_DELAY:
8401 case -ETIMEDOUT:
8402 case -EACCES:
8403 case -EAGAIN:
8404 goto out;
8407 clp->cl_seqid++;
8408 if (!status) {
8409 /* Verify the session's negotiated channel_attrs values */
8410 status = nfs4_verify_channel_attrs(&args, &res);
8411 /* Increment the clientid slot sequence id */
8412 if (status)
8413 goto out;
8414 nfs4_update_session(session, &res);
8416 out:
8417 return status;
8421 * Issues a CREATE_SESSION operation to the server.
8422 * It is the responsibility of the caller to verify the session is
8423 * expired before calling this routine.
8425 int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
8427 int status;
8428 unsigned *ptr;
8429 struct nfs4_session *session = clp->cl_session;
8431 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8433 status = _nfs4_proc_create_session(clp, cred);
8434 if (status)
8435 goto out;
8437 /* Init or reset the session slot tables */
8438 status = nfs4_setup_session_slot_tables(session);
8439 dprintk("slot table setup returned %d\n", status);
8440 if (status)
8441 goto out;
8443 ptr = (unsigned *)&session->sess_id.data[0];
8444 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8445 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
8446 out:
8447 dprintk("<-- %s\n", __func__);
8448 return status;
8452 * Issue the over-the-wire RPC DESTROY_SESSION.
8453 * The caller must serialize access to this routine.
8455 int nfs4_proc_destroy_session(struct nfs4_session *session,
8456 struct rpc_cred *cred)
8458 struct rpc_message msg = {
8459 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8460 .rpc_argp = session,
8461 .rpc_cred = cred,
8463 int status = 0;
8465 dprintk("--> nfs4_proc_destroy_session\n");
8467 /* session is still being setup */
8468 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8469 return 0;
8471 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
8472 trace_nfs4_destroy_session(session->clp, status);
8474 if (status)
8475 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
8476 "Session has been destroyed regardless...\n", status);
8478 dprintk("<-- nfs4_proc_destroy_session\n");
8479 return status;
8483 * Renew the cl_session lease.
8485 struct nfs4_sequence_data {
8486 struct nfs_client *clp;
8487 struct nfs4_sequence_args args;
8488 struct nfs4_sequence_res res;
8491 static void nfs41_sequence_release(void *data)
8493 struct nfs4_sequence_data *calldata = data;
8494 struct nfs_client *clp = calldata->clp;
8496 if (refcount_read(&clp->cl_count) > 1)
8497 nfs4_schedule_state_renewal(clp);
8498 nfs_put_client(clp);
8499 kfree(calldata);
8502 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8504 switch(task->tk_status) {
8505 case -NFS4ERR_DELAY:
8506 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8507 return -EAGAIN;
8508 default:
8509 nfs4_schedule_lease_recovery(clp);
8511 return 0;
8514 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
8516 struct nfs4_sequence_data *calldata = data;
8517 struct nfs_client *clp = calldata->clp;
8519 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8520 return;
8522 trace_nfs4_sequence(clp, task->tk_status);
8523 if (task->tk_status < 0) {
8524 dprintk("%s ERROR %d\n", __func__, task->tk_status);
8525 if (refcount_read(&clp->cl_count) == 1)
8526 goto out;
8528 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8529 rpc_restart_call_prepare(task);
8530 return;
8533 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
8534 out:
8535 dprintk("<-- %s\n", __func__);
8538 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8540 struct nfs4_sequence_data *calldata = data;
8541 struct nfs_client *clp = calldata->clp;
8542 struct nfs4_sequence_args *args;
8543 struct nfs4_sequence_res *res;
8545 args = task->tk_msg.rpc_argp;
8546 res = task->tk_msg.rpc_resp;
8548 nfs4_setup_sequence(clp, args, res, task);
8551 static const struct rpc_call_ops nfs41_sequence_ops = {
8552 .rpc_call_done = nfs41_sequence_call_done,
8553 .rpc_call_prepare = nfs41_sequence_prepare,
8554 .rpc_release = nfs41_sequence_release,
8557 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8558 struct rpc_cred *cred,
8559 struct nfs4_slot *slot,
8560 bool is_privileged)
8562 struct nfs4_sequence_data *calldata;
8563 struct rpc_message msg = {
8564 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8565 .rpc_cred = cred,
8567 struct rpc_task_setup task_setup_data = {
8568 .rpc_client = clp->cl_rpcclient,
8569 .rpc_message = &msg,
8570 .callback_ops = &nfs41_sequence_ops,
8571 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
8573 struct rpc_task *ret;
8575 ret = ERR_PTR(-EIO);
8576 if (!refcount_inc_not_zero(&clp->cl_count))
8577 goto out_err;
8579 ret = ERR_PTR(-ENOMEM);
8580 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8581 if (calldata == NULL)
8582 goto out_put_clp;
8583 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
8584 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
8585 msg.rpc_argp = &calldata->args;
8586 msg.rpc_resp = &calldata->res;
8587 calldata->clp = clp;
8588 task_setup_data.callback_data = calldata;
8590 ret = rpc_run_task(&task_setup_data);
8591 if (IS_ERR(ret))
8592 goto out_err;
8593 return ret;
8594 out_put_clp:
8595 nfs_put_client(clp);
8596 out_err:
8597 nfs41_release_slot(slot);
8598 return ret;
8601 static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
8603 struct rpc_task *task;
8604 int ret = 0;
8606 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
8607 return -EAGAIN;
8608 task = _nfs41_proc_sequence(clp, cred, NULL, false);
8609 if (IS_ERR(task))
8610 ret = PTR_ERR(task);
8611 else
8612 rpc_put_task_async(task);
8613 dprintk("<-- %s status=%d\n", __func__, ret);
8614 return ret;
8617 static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8619 struct rpc_task *task;
8620 int ret;
8622 task = _nfs41_proc_sequence(clp, cred, NULL, true);
8623 if (IS_ERR(task)) {
8624 ret = PTR_ERR(task);
8625 goto out;
8627 ret = rpc_wait_for_completion_task(task);
8628 if (!ret)
8629 ret = task->tk_status;
8630 rpc_put_task(task);
8631 out:
8632 dprintk("<-- %s status=%d\n", __func__, ret);
8633 return ret;
8636 struct nfs4_reclaim_complete_data {
8637 struct nfs_client *clp;
8638 struct nfs41_reclaim_complete_args arg;
8639 struct nfs41_reclaim_complete_res res;
8642 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8644 struct nfs4_reclaim_complete_data *calldata = data;
8646 nfs4_setup_sequence(calldata->clp,
8647 &calldata->arg.seq_args,
8648 &calldata->res.seq_res,
8649 task);
8652 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8654 switch(task->tk_status) {
8655 case 0:
8656 wake_up_all(&clp->cl_lock_waitq);
8657 /* Fallthrough */
8658 case -NFS4ERR_COMPLETE_ALREADY:
8659 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8660 break;
8661 case -NFS4ERR_DELAY:
8662 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8663 /* fall through */
8664 case -NFS4ERR_RETRY_UNCACHED_REP:
8665 return -EAGAIN;
8666 case -NFS4ERR_BADSESSION:
8667 case -NFS4ERR_DEADSESSION:
8668 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8669 nfs4_schedule_session_recovery(clp->cl_session,
8670 task->tk_status);
8671 break;
8672 default:
8673 nfs4_schedule_lease_recovery(clp);
8675 return 0;
8678 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8680 struct nfs4_reclaim_complete_data *calldata = data;
8681 struct nfs_client *clp = calldata->clp;
8682 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8684 dprintk("--> %s\n", __func__);
8685 if (!nfs41_sequence_done(task, res))
8686 return;
8688 trace_nfs4_reclaim_complete(clp, task->tk_status);
8689 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8690 rpc_restart_call_prepare(task);
8691 return;
8693 dprintk("<-- %s\n", __func__);
8696 static void nfs4_free_reclaim_complete_data(void *data)
8698 struct nfs4_reclaim_complete_data *calldata = data;
8700 kfree(calldata);
8703 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8704 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8705 .rpc_call_done = nfs4_reclaim_complete_done,
8706 .rpc_release = nfs4_free_reclaim_complete_data,
8710 * Issue a global reclaim complete.
8712 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8713 struct rpc_cred *cred)
8715 struct nfs4_reclaim_complete_data *calldata;
8716 struct rpc_task *task;
8717 struct rpc_message msg = {
8718 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
8719 .rpc_cred = cred,
8721 struct rpc_task_setup task_setup_data = {
8722 .rpc_client = clp->cl_rpcclient,
8723 .rpc_message = &msg,
8724 .callback_ops = &nfs4_reclaim_complete_call_ops,
8725 .flags = RPC_TASK_ASYNC,
8727 int status = -ENOMEM;
8729 dprintk("--> %s\n", __func__);
8730 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8731 if (calldata == NULL)
8732 goto out;
8733 calldata->clp = clp;
8734 calldata->arg.one_fs = 0;
8736 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
8737 msg.rpc_argp = &calldata->arg;
8738 msg.rpc_resp = &calldata->res;
8739 task_setup_data.callback_data = calldata;
8740 task = rpc_run_task(&task_setup_data);
8741 if (IS_ERR(task)) {
8742 status = PTR_ERR(task);
8743 goto out;
8745 status = rpc_wait_for_completion_task(task);
8746 if (status == 0)
8747 status = task->tk_status;
8748 rpc_put_task(task);
8749 out:
8750 dprintk("<-- %s status=%d\n", __func__, status);
8751 return status;
8754 static void
8755 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8757 struct nfs4_layoutget *lgp = calldata;
8758 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
8760 dprintk("--> %s\n", __func__);
8761 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
8762 &lgp->res.seq_res, task);
8763 dprintk("<-- %s\n", __func__);
8766 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8768 struct nfs4_layoutget *lgp = calldata;
8770 dprintk("--> %s\n", __func__);
8771 nfs41_sequence_process(task, &lgp->res.seq_res);
8772 dprintk("<-- %s\n", __func__);
8775 static int
8776 nfs4_layoutget_handle_exception(struct rpc_task *task,
8777 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8779 struct inode *inode = lgp->args.inode;
8780 struct nfs_server *server = NFS_SERVER(inode);
8781 struct pnfs_layout_hdr *lo;
8782 int nfs4err = task->tk_status;
8783 int err, status = 0;
8784 LIST_HEAD(head);
8786 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
8788 nfs4_sequence_free_slot(&lgp->res.seq_res);
8790 switch (nfs4err) {
8791 case 0:
8792 goto out;
8795 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8796 * on the file. set tk_status to -ENODATA to tell upper layer to
8797 * retry go inband.
8799 case -NFS4ERR_LAYOUTUNAVAILABLE:
8800 status = -ENODATA;
8801 goto out;
8803 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8804 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8806 case -NFS4ERR_BADLAYOUT:
8807 status = -EOVERFLOW;
8808 goto out;
8810 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
8811 * (or clients) writing to the same RAID stripe except when
8812 * the minlength argument is 0 (see RFC5661 section 18.43.3).
8814 * Treat it like we would RECALLCONFLICT -- we retry for a little
8815 * while, and then eventually give up.
8817 case -NFS4ERR_LAYOUTTRYLATER:
8818 if (lgp->args.minlength == 0) {
8819 status = -EOVERFLOW;
8820 goto out;
8822 status = -EBUSY;
8823 break;
8824 case -NFS4ERR_RECALLCONFLICT:
8825 status = -ERECALLCONFLICT;
8826 break;
8827 case -NFS4ERR_DELEG_REVOKED:
8828 case -NFS4ERR_ADMIN_REVOKED:
8829 case -NFS4ERR_EXPIRED:
8830 case -NFS4ERR_BAD_STATEID:
8831 exception->timeout = 0;
8832 spin_lock(&inode->i_lock);
8833 lo = NFS_I(inode)->layout;
8834 /* If the open stateid was bad, then recover it. */
8835 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8836 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
8837 spin_unlock(&inode->i_lock);
8838 exception->state = lgp->args.ctx->state;
8839 exception->stateid = &lgp->args.stateid;
8840 break;
8844 * Mark the bad layout state as invalid, then retry
8846 pnfs_mark_layout_stateid_invalid(lo, &head);
8847 spin_unlock(&inode->i_lock);
8848 nfs_commit_inode(inode, 0);
8849 pnfs_free_lseg_list(&head);
8850 status = -EAGAIN;
8851 goto out;
8854 err = nfs4_handle_exception(server, nfs4err, exception);
8855 if (!status) {
8856 if (exception->retry)
8857 status = -EAGAIN;
8858 else
8859 status = err;
8861 out:
8862 dprintk("<-- %s\n", __func__);
8863 return status;
8866 size_t max_response_pages(struct nfs_server *server)
8868 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8869 return nfs_page_array_len(0, max_resp_sz);
8872 static void nfs4_layoutget_release(void *calldata)
8874 struct nfs4_layoutget *lgp = calldata;
8876 dprintk("--> %s\n", __func__);
8877 nfs4_sequence_free_slot(&lgp->res.seq_res);
8878 pnfs_layoutget_free(lgp);
8879 dprintk("<-- %s\n", __func__);
8882 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8883 .rpc_call_prepare = nfs4_layoutget_prepare,
8884 .rpc_call_done = nfs4_layoutget_done,
8885 .rpc_release = nfs4_layoutget_release,
8888 struct pnfs_layout_segment *
8889 nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
8891 struct inode *inode = lgp->args.inode;
8892 struct nfs_server *server = NFS_SERVER(inode);
8893 struct rpc_task *task;
8894 struct rpc_message msg = {
8895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8896 .rpc_argp = &lgp->args,
8897 .rpc_resp = &lgp->res,
8898 .rpc_cred = lgp->cred,
8900 struct rpc_task_setup task_setup_data = {
8901 .rpc_client = server->client,
8902 .rpc_message = &msg,
8903 .callback_ops = &nfs4_layoutget_call_ops,
8904 .callback_data = lgp,
8905 .flags = RPC_TASK_ASYNC,
8907 struct pnfs_layout_segment *lseg = NULL;
8908 struct nfs4_exception exception = {
8909 .inode = inode,
8910 .timeout = *timeout,
8912 int status = 0;
8914 dprintk("--> %s\n", __func__);
8916 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8917 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8919 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
8921 task = rpc_run_task(&task_setup_data);
8922 if (IS_ERR(task))
8923 return ERR_CAST(task);
8924 status = rpc_wait_for_completion_task(task);
8925 if (status != 0)
8926 goto out;
8928 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8929 if (task->tk_status < 0 || lgp->res.layoutp->len == 0) {
8930 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8931 *timeout = exception.timeout;
8932 } else
8933 lseg = pnfs_layout_process(lgp);
8934 out:
8935 trace_nfs4_layoutget(lgp->args.ctx,
8936 &lgp->args.range,
8937 &lgp->res.range,
8938 &lgp->res.stateid,
8939 status);
8941 rpc_put_task(task);
8942 dprintk("<-- %s status=%d\n", __func__, status);
8943 if (status)
8944 return ERR_PTR(status);
8945 return lseg;
8948 static void
8949 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8951 struct nfs4_layoutreturn *lrp = calldata;
8953 dprintk("--> %s\n", __func__);
8954 nfs4_setup_sequence(lrp->clp,
8955 &lrp->args.seq_args,
8956 &lrp->res.seq_res,
8957 task);
8958 if (!pnfs_layout_is_valid(lrp->args.layout))
8959 rpc_exit(task, 0);
8962 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8964 struct nfs4_layoutreturn *lrp = calldata;
8965 struct nfs_server *server;
8967 dprintk("--> %s\n", __func__);
8969 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
8970 return;
8972 server = NFS_SERVER(lrp->args.inode);
8973 switch (task->tk_status) {
8974 case -NFS4ERR_OLD_STATEID:
8975 if (nfs4_layoutreturn_refresh_stateid(&lrp->args.stateid,
8976 &lrp->args.range,
8977 lrp->args.inode))
8978 goto out_restart;
8979 /* Fallthrough */
8980 default:
8981 task->tk_status = 0;
8982 /* Fallthrough */
8983 case 0:
8984 break;
8985 case -NFS4ERR_DELAY:
8986 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
8987 break;
8988 goto out_restart;
8990 dprintk("<-- %s\n", __func__);
8991 return;
8992 out_restart:
8993 task->tk_status = 0;
8994 nfs4_sequence_free_slot(&lrp->res.seq_res);
8995 rpc_restart_call_prepare(task);
8998 static void nfs4_layoutreturn_release(void *calldata)
9000 struct nfs4_layoutreturn *lrp = calldata;
9001 struct pnfs_layout_hdr *lo = lrp->args.layout;
9003 dprintk("--> %s\n", __func__);
9004 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
9005 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
9006 nfs4_sequence_free_slot(&lrp->res.seq_res);
9007 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9008 lrp->ld_private.ops->free(&lrp->ld_private);
9009 pnfs_put_layout_hdr(lrp->args.layout);
9010 nfs_iput_and_deactive(lrp->inode);
9011 kfree(calldata);
9012 dprintk("<-- %s\n", __func__);
9015 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9016 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9017 .rpc_call_done = nfs4_layoutreturn_done,
9018 .rpc_release = nfs4_layoutreturn_release,
9021 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
9023 struct rpc_task *task;
9024 struct rpc_message msg = {
9025 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9026 .rpc_argp = &lrp->args,
9027 .rpc_resp = &lrp->res,
9028 .rpc_cred = lrp->cred,
9030 struct rpc_task_setup task_setup_data = {
9031 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
9032 .rpc_message = &msg,
9033 .callback_ops = &nfs4_layoutreturn_call_ops,
9034 .callback_data = lrp,
9036 int status = 0;
9038 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9039 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9040 &task_setup_data.rpc_client, &msg);
9042 dprintk("--> %s\n", __func__);
9043 if (!sync) {
9044 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9045 if (!lrp->inode) {
9046 nfs4_layoutreturn_release(lrp);
9047 return -EAGAIN;
9049 task_setup_data.flags |= RPC_TASK_ASYNC;
9051 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
9052 task = rpc_run_task(&task_setup_data);
9053 if (IS_ERR(task))
9054 return PTR_ERR(task);
9055 if (sync)
9056 status = task->tk_status;
9057 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
9058 dprintk("<-- %s status=%d\n", __func__, status);
9059 rpc_put_task(task);
9060 return status;
9063 static int
9064 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
9065 struct pnfs_device *pdev,
9066 struct rpc_cred *cred)
9068 struct nfs4_getdeviceinfo_args args = {
9069 .pdev = pdev,
9070 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9071 NOTIFY_DEVICEID4_DELETE,
9073 struct nfs4_getdeviceinfo_res res = {
9074 .pdev = pdev,
9076 struct rpc_message msg = {
9077 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9078 .rpc_argp = &args,
9079 .rpc_resp = &res,
9080 .rpc_cred = cred,
9082 int status;
9084 dprintk("--> %s\n", __func__);
9085 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
9086 if (res.notification & ~args.notify_types)
9087 dprintk("%s: unsupported notification\n", __func__);
9088 if (res.notification != args.notify_types)
9089 pdev->nocache = 1;
9091 dprintk("<-- %s status=%d\n", __func__, status);
9093 return status;
9096 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9097 struct pnfs_device *pdev,
9098 struct rpc_cred *cred)
9100 struct nfs4_exception exception = { };
9101 int err;
9103 do {
9104 err = nfs4_handle_exception(server,
9105 _nfs4_proc_getdeviceinfo(server, pdev, cred),
9106 &exception);
9107 } while (exception.retry);
9108 return err;
9110 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9112 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9114 struct nfs4_layoutcommit_data *data = calldata;
9115 struct nfs_server *server = NFS_SERVER(data->args.inode);
9117 nfs4_setup_sequence(server->nfs_client,
9118 &data->args.seq_args,
9119 &data->res.seq_res,
9120 task);
9123 static void
9124 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9126 struct nfs4_layoutcommit_data *data = calldata;
9127 struct nfs_server *server = NFS_SERVER(data->args.inode);
9129 if (!nfs41_sequence_done(task, &data->res.seq_res))
9130 return;
9132 switch (task->tk_status) { /* Just ignore these failures */
9133 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9134 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9135 case -NFS4ERR_BADLAYOUT: /* no layout */
9136 case -NFS4ERR_GRACE: /* loca_recalim always false */
9137 task->tk_status = 0;
9138 case 0:
9139 break;
9140 default:
9141 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
9142 rpc_restart_call_prepare(task);
9143 return;
9148 static void nfs4_layoutcommit_release(void *calldata)
9150 struct nfs4_layoutcommit_data *data = calldata;
9152 pnfs_cleanup_layoutcommit(data);
9153 nfs_post_op_update_inode_force_wcc(data->args.inode,
9154 data->res.fattr);
9155 put_rpccred(data->cred);
9156 nfs_iput_and_deactive(data->inode);
9157 kfree(data);
9160 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9161 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9162 .rpc_call_done = nfs4_layoutcommit_done,
9163 .rpc_release = nfs4_layoutcommit_release,
9167 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
9169 struct rpc_message msg = {
9170 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9171 .rpc_argp = &data->args,
9172 .rpc_resp = &data->res,
9173 .rpc_cred = data->cred,
9175 struct rpc_task_setup task_setup_data = {
9176 .task = &data->task,
9177 .rpc_client = NFS_CLIENT(data->args.inode),
9178 .rpc_message = &msg,
9179 .callback_ops = &nfs4_layoutcommit_ops,
9180 .callback_data = data,
9182 struct rpc_task *task;
9183 int status = 0;
9185 dprintk("NFS: initiating layoutcommit call. sync %d "
9186 "lbw: %llu inode %lu\n", sync,
9187 data->args.lastbytewritten,
9188 data->args.inode->i_ino);
9190 if (!sync) {
9191 data->inode = nfs_igrab_and_active(data->args.inode);
9192 if (data->inode == NULL) {
9193 nfs4_layoutcommit_release(data);
9194 return -EAGAIN;
9196 task_setup_data.flags = RPC_TASK_ASYNC;
9198 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
9199 task = rpc_run_task(&task_setup_data);
9200 if (IS_ERR(task))
9201 return PTR_ERR(task);
9202 if (sync)
9203 status = task->tk_status;
9204 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
9205 dprintk("%s: status %d\n", __func__, status);
9206 rpc_put_task(task);
9207 return status;
9211 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9212 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9214 static int
9215 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9216 struct nfs_fsinfo *info,
9217 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
9219 struct nfs41_secinfo_no_name_args args = {
9220 .style = SECINFO_STYLE_CURRENT_FH,
9222 struct nfs4_secinfo_res res = {
9223 .flavors = flavors,
9225 struct rpc_message msg = {
9226 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9227 .rpc_argp = &args,
9228 .rpc_resp = &res,
9230 struct rpc_clnt *clnt = server->client;
9231 struct rpc_cred *cred = NULL;
9232 int status;
9234 if (use_integrity) {
9235 clnt = server->nfs_client->cl_rpcclient;
9236 cred = nfs4_get_clid_cred(server->nfs_client);
9237 msg.rpc_cred = cred;
9240 dprintk("--> %s\n", __func__);
9241 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9242 &res.seq_res, 0);
9243 dprintk("<-- %s status=%d\n", __func__, status);
9245 if (cred)
9246 put_rpccred(cred);
9248 return status;
9251 static int
9252 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9253 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9255 struct nfs4_exception exception = { };
9256 int err;
9257 do {
9258 /* first try using integrity protection */
9259 err = -NFS4ERR_WRONGSEC;
9261 /* try to use integrity protection with machine cred */
9262 if (_nfs4_is_integrity_protected(server->nfs_client))
9263 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9264 flavors, true);
9267 * if unable to use integrity protection, or SECINFO with
9268 * integrity protection returns NFS4ERR_WRONGSEC (which is
9269 * disallowed by spec, but exists in deployed servers) use
9270 * the current filesystem's rpc_client and the user cred.
9272 if (err == -NFS4ERR_WRONGSEC)
9273 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9274 flavors, false);
9276 switch (err) {
9277 case 0:
9278 case -NFS4ERR_WRONGSEC:
9279 case -ENOTSUPP:
9280 goto out;
9281 default:
9282 err = nfs4_handle_exception(server, err, &exception);
9284 } while (exception.retry);
9285 out:
9286 return err;
9289 static int
9290 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9291 struct nfs_fsinfo *info)
9293 int err;
9294 struct page *page;
9295 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
9296 struct nfs4_secinfo_flavors *flavors;
9297 struct nfs4_secinfo4 *secinfo;
9298 int i;
9300 page = alloc_page(GFP_KERNEL);
9301 if (!page) {
9302 err = -ENOMEM;
9303 goto out;
9306 flavors = page_address(page);
9307 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9310 * Fall back on "guess and check" method if
9311 * the server doesn't support SECINFO_NO_NAME
9313 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
9314 err = nfs4_find_root_sec(server, fhandle, info);
9315 goto out_freepage;
9317 if (err)
9318 goto out_freepage;
9320 for (i = 0; i < flavors->num_flavors; i++) {
9321 secinfo = &flavors->flavors[i];
9323 switch (secinfo->flavor) {
9324 case RPC_AUTH_NULL:
9325 case RPC_AUTH_UNIX:
9326 case RPC_AUTH_GSS:
9327 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9328 &secinfo->flavor_info);
9329 break;
9330 default:
9331 flavor = RPC_AUTH_MAXFLAVOR;
9332 break;
9335 if (!nfs_auth_info_match(&server->auth_info, flavor))
9336 flavor = RPC_AUTH_MAXFLAVOR;
9338 if (flavor != RPC_AUTH_MAXFLAVOR) {
9339 err = nfs4_lookup_root_sec(server, fhandle,
9340 info, flavor);
9341 if (!err)
9342 break;
9346 if (flavor == RPC_AUTH_MAXFLAVOR)
9347 err = -EPERM;
9349 out_freepage:
9350 put_page(page);
9351 if (err == -EACCES)
9352 return -EPERM;
9353 out:
9354 return err;
9357 static int _nfs41_test_stateid(struct nfs_server *server,
9358 nfs4_stateid *stateid,
9359 struct rpc_cred *cred)
9361 int status;
9362 struct nfs41_test_stateid_args args = {
9363 .stateid = stateid,
9365 struct nfs41_test_stateid_res res;
9366 struct rpc_message msg = {
9367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9368 .rpc_argp = &args,
9369 .rpc_resp = &res,
9370 .rpc_cred = cred,
9372 struct rpc_clnt *rpc_client = server->client;
9374 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9375 &rpc_client, &msg);
9377 dprintk("NFS call test_stateid %p\n", stateid);
9378 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
9379 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
9380 &args.seq_args, &res.seq_res);
9381 if (status != NFS_OK) {
9382 dprintk("NFS reply test_stateid: failed, %d\n", status);
9383 return status;
9385 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
9386 return -res.status;
9389 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9390 int err, struct nfs4_exception *exception)
9392 exception->retry = 0;
9393 switch(err) {
9394 case -NFS4ERR_DELAY:
9395 case -NFS4ERR_RETRY_UNCACHED_REP:
9396 nfs4_handle_exception(server, err, exception);
9397 break;
9398 case -NFS4ERR_BADSESSION:
9399 case -NFS4ERR_BADSLOT:
9400 case -NFS4ERR_BAD_HIGH_SLOT:
9401 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9402 case -NFS4ERR_DEADSESSION:
9403 nfs4_do_handle_exception(server, err, exception);
9408 * nfs41_test_stateid - perform a TEST_STATEID operation
9410 * @server: server / transport on which to perform the operation
9411 * @stateid: state ID to test
9412 * @cred: credential
9414 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9415 * Otherwise a negative NFS4ERR value is returned if the operation
9416 * failed or the state ID is not currently valid.
9418 static int nfs41_test_stateid(struct nfs_server *server,
9419 nfs4_stateid *stateid,
9420 struct rpc_cred *cred)
9422 struct nfs4_exception exception = { };
9423 int err;
9424 do {
9425 err = _nfs41_test_stateid(server, stateid, cred);
9426 nfs4_handle_delay_or_session_error(server, err, &exception);
9427 } while (exception.retry);
9428 return err;
9431 struct nfs_free_stateid_data {
9432 struct nfs_server *server;
9433 struct nfs41_free_stateid_args args;
9434 struct nfs41_free_stateid_res res;
9437 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9439 struct nfs_free_stateid_data *data = calldata;
9440 nfs4_setup_sequence(data->server->nfs_client,
9441 &data->args.seq_args,
9442 &data->res.seq_res,
9443 task);
9446 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9448 struct nfs_free_stateid_data *data = calldata;
9450 nfs41_sequence_done(task, &data->res.seq_res);
9452 switch (task->tk_status) {
9453 case -NFS4ERR_DELAY:
9454 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
9455 rpc_restart_call_prepare(task);
9459 static void nfs41_free_stateid_release(void *calldata)
9461 kfree(calldata);
9464 static const struct rpc_call_ops nfs41_free_stateid_ops = {
9465 .rpc_call_prepare = nfs41_free_stateid_prepare,
9466 .rpc_call_done = nfs41_free_stateid_done,
9467 .rpc_release = nfs41_free_stateid_release,
9471 * nfs41_free_stateid - perform a FREE_STATEID operation
9473 * @server: server / transport on which to perform the operation
9474 * @stateid: state ID to release
9475 * @cred: credential
9476 * @is_recovery: set to true if this call needs to be privileged
9478 * Note: this function is always asynchronous.
9480 static int nfs41_free_stateid(struct nfs_server *server,
9481 const nfs4_stateid *stateid,
9482 struct rpc_cred *cred,
9483 bool privileged)
9485 struct rpc_message msg = {
9486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
9487 .rpc_cred = cred,
9489 struct rpc_task_setup task_setup = {
9490 .rpc_client = server->client,
9491 .rpc_message = &msg,
9492 .callback_ops = &nfs41_free_stateid_ops,
9493 .flags = RPC_TASK_ASYNC,
9495 struct nfs_free_stateid_data *data;
9496 struct rpc_task *task;
9498 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9499 &task_setup.rpc_client, &msg);
9501 dprintk("NFS call free_stateid %p\n", stateid);
9502 data = kmalloc(sizeof(*data), GFP_NOFS);
9503 if (!data)
9504 return -ENOMEM;
9505 data->server = server;
9506 nfs4_stateid_copy(&data->args.stateid, stateid);
9508 task_setup.callback_data = data;
9510 msg.rpc_argp = &data->args;
9511 msg.rpc_resp = &data->res;
9512 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
9513 task = rpc_run_task(&task_setup);
9514 if (IS_ERR(task))
9515 return PTR_ERR(task);
9516 rpc_put_task(task);
9517 return 0;
9520 static void
9521 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
9523 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
9525 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
9526 nfs4_free_lock_state(server, lsp);
9529 static bool nfs41_match_stateid(const nfs4_stateid *s1,
9530 const nfs4_stateid *s2)
9532 if (s1->type != s2->type)
9533 return false;
9535 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
9536 return false;
9538 if (s1->seqid == s2->seqid)
9539 return true;
9541 return s1->seqid == 0 || s2->seqid == 0;
9544 #endif /* CONFIG_NFS_V4_1 */
9546 static bool nfs4_match_stateid(const nfs4_stateid *s1,
9547 const nfs4_stateid *s2)
9549 return nfs4_stateid_match(s1, s2);
9553 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
9554 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9555 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9556 .recover_open = nfs4_open_reclaim,
9557 .recover_lock = nfs4_lock_reclaim,
9558 .establish_clid = nfs4_init_clientid,
9559 .detect_trunking = nfs40_discover_server_trunking,
9562 #if defined(CONFIG_NFS_V4_1)
9563 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
9564 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9565 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9566 .recover_open = nfs4_open_reclaim,
9567 .recover_lock = nfs4_lock_reclaim,
9568 .establish_clid = nfs41_init_clientid,
9569 .reclaim_complete = nfs41_proc_reclaim_complete,
9570 .detect_trunking = nfs41_discover_server_trunking,
9572 #endif /* CONFIG_NFS_V4_1 */
9574 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
9575 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9576 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9577 .recover_open = nfs40_open_expired,
9578 .recover_lock = nfs4_lock_expired,
9579 .establish_clid = nfs4_init_clientid,
9582 #if defined(CONFIG_NFS_V4_1)
9583 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
9584 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9585 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
9586 .recover_open = nfs41_open_expired,
9587 .recover_lock = nfs41_lock_expired,
9588 .establish_clid = nfs41_init_clientid,
9590 #endif /* CONFIG_NFS_V4_1 */
9592 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
9593 .sched_state_renewal = nfs4_proc_async_renew,
9594 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
9595 .renew_lease = nfs4_proc_renew,
9598 #if defined(CONFIG_NFS_V4_1)
9599 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
9600 .sched_state_renewal = nfs41_proc_async_sequence,
9601 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
9602 .renew_lease = nfs4_proc_sequence,
9604 #endif
9606 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
9607 .get_locations = _nfs40_proc_get_locations,
9608 .fsid_present = _nfs40_proc_fsid_present,
9611 #if defined(CONFIG_NFS_V4_1)
9612 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
9613 .get_locations = _nfs41_proc_get_locations,
9614 .fsid_present = _nfs41_proc_fsid_present,
9616 #endif /* CONFIG_NFS_V4_1 */
9618 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9619 .minor_version = 0,
9620 .init_caps = NFS_CAP_READDIRPLUS
9621 | NFS_CAP_ATOMIC_OPEN
9622 | NFS_CAP_POSIX_LOCK,
9623 .init_client = nfs40_init_client,
9624 .shutdown_client = nfs40_shutdown_client,
9625 .match_stateid = nfs4_match_stateid,
9626 .find_root_sec = nfs4_find_root_sec,
9627 .free_lock_state = nfs4_release_lockowner,
9628 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
9629 .alloc_seqid = nfs_alloc_seqid,
9630 .call_sync_ops = &nfs40_call_sync_ops,
9631 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9632 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9633 .state_renewal_ops = &nfs40_state_renewal_ops,
9634 .mig_recovery_ops = &nfs40_mig_recovery_ops,
9637 #if defined(CONFIG_NFS_V4_1)
9638 static struct nfs_seqid *
9639 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9641 return NULL;
9644 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9645 .minor_version = 1,
9646 .init_caps = NFS_CAP_READDIRPLUS
9647 | NFS_CAP_ATOMIC_OPEN
9648 | NFS_CAP_POSIX_LOCK
9649 | NFS_CAP_STATEID_NFSV41
9650 | NFS_CAP_ATOMIC_OPEN_V1
9651 | NFS_CAP_LGOPEN,
9652 .init_client = nfs41_init_client,
9653 .shutdown_client = nfs41_shutdown_client,
9654 .match_stateid = nfs41_match_stateid,
9655 .find_root_sec = nfs41_find_root_sec,
9656 .free_lock_state = nfs41_free_lock_state,
9657 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9658 .alloc_seqid = nfs_alloc_no_seqid,
9659 .session_trunk = nfs4_test_session_trunk,
9660 .call_sync_ops = &nfs41_call_sync_ops,
9661 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9662 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9663 .state_renewal_ops = &nfs41_state_renewal_ops,
9664 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9666 #endif
9668 #if defined(CONFIG_NFS_V4_2)
9669 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9670 .minor_version = 2,
9671 .init_caps = NFS_CAP_READDIRPLUS
9672 | NFS_CAP_ATOMIC_OPEN
9673 | NFS_CAP_POSIX_LOCK
9674 | NFS_CAP_STATEID_NFSV41
9675 | NFS_CAP_ATOMIC_OPEN_V1
9676 | NFS_CAP_LGOPEN
9677 | NFS_CAP_ALLOCATE
9678 | NFS_CAP_COPY
9679 | NFS_CAP_OFFLOAD_CANCEL
9680 | NFS_CAP_DEALLOCATE
9681 | NFS_CAP_SEEK
9682 | NFS_CAP_LAYOUTSTATS
9683 | NFS_CAP_CLONE,
9684 .init_client = nfs41_init_client,
9685 .shutdown_client = nfs41_shutdown_client,
9686 .match_stateid = nfs41_match_stateid,
9687 .find_root_sec = nfs41_find_root_sec,
9688 .free_lock_state = nfs41_free_lock_state,
9689 .call_sync_ops = &nfs41_call_sync_ops,
9690 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
9691 .alloc_seqid = nfs_alloc_no_seqid,
9692 .session_trunk = nfs4_test_session_trunk,
9693 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9694 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9695 .state_renewal_ops = &nfs41_state_renewal_ops,
9696 .mig_recovery_ops = &nfs41_mig_recovery_ops,
9698 #endif
9700 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9701 [0] = &nfs_v4_0_minor_ops,
9702 #if defined(CONFIG_NFS_V4_1)
9703 [1] = &nfs_v4_1_minor_ops,
9704 #endif
9705 #if defined(CONFIG_NFS_V4_2)
9706 [2] = &nfs_v4_2_minor_ops,
9707 #endif
9710 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
9712 ssize_t error, error2;
9714 error = generic_listxattr(dentry, list, size);
9715 if (error < 0)
9716 return error;
9717 if (list) {
9718 list += error;
9719 size -= error;
9722 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9723 if (error2 < 0)
9724 return error2;
9725 return error + error2;
9728 static const struct inode_operations nfs4_dir_inode_operations = {
9729 .create = nfs_create,
9730 .lookup = nfs_lookup,
9731 .atomic_open = nfs_atomic_open,
9732 .link = nfs_link,
9733 .unlink = nfs_unlink,
9734 .symlink = nfs_symlink,
9735 .mkdir = nfs_mkdir,
9736 .rmdir = nfs_rmdir,
9737 .mknod = nfs_mknod,
9738 .rename = nfs_rename,
9739 .permission = nfs_permission,
9740 .getattr = nfs_getattr,
9741 .setattr = nfs_setattr,
9742 .listxattr = nfs4_listxattr,
9745 static const struct inode_operations nfs4_file_inode_operations = {
9746 .permission = nfs_permission,
9747 .getattr = nfs_getattr,
9748 .setattr = nfs_setattr,
9749 .listxattr = nfs4_listxattr,
9752 const struct nfs_rpc_ops nfs_v4_clientops = {
9753 .version = 4, /* protocol version */
9754 .dentry_ops = &nfs4_dentry_operations,
9755 .dir_inode_ops = &nfs4_dir_inode_operations,
9756 .file_inode_ops = &nfs4_file_inode_operations,
9757 .file_ops = &nfs4_file_operations,
9758 .getroot = nfs4_proc_get_root,
9759 .submount = nfs4_submount,
9760 .try_mount = nfs4_try_mount,
9761 .getattr = nfs4_proc_getattr,
9762 .setattr = nfs4_proc_setattr,
9763 .lookup = nfs4_proc_lookup,
9764 .lookupp = nfs4_proc_lookupp,
9765 .access = nfs4_proc_access,
9766 .readlink = nfs4_proc_readlink,
9767 .create = nfs4_proc_create,
9768 .remove = nfs4_proc_remove,
9769 .unlink_setup = nfs4_proc_unlink_setup,
9770 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
9771 .unlink_done = nfs4_proc_unlink_done,
9772 .rename_setup = nfs4_proc_rename_setup,
9773 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
9774 .rename_done = nfs4_proc_rename_done,
9775 .link = nfs4_proc_link,
9776 .symlink = nfs4_proc_symlink,
9777 .mkdir = nfs4_proc_mkdir,
9778 .rmdir = nfs4_proc_rmdir,
9779 .readdir = nfs4_proc_readdir,
9780 .mknod = nfs4_proc_mknod,
9781 .statfs = nfs4_proc_statfs,
9782 .fsinfo = nfs4_proc_fsinfo,
9783 .pathconf = nfs4_proc_pathconf,
9784 .set_capabilities = nfs4_server_capabilities,
9785 .decode_dirent = nfs4_decode_dirent,
9786 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
9787 .read_setup = nfs4_proc_read_setup,
9788 .read_done = nfs4_read_done,
9789 .write_setup = nfs4_proc_write_setup,
9790 .write_done = nfs4_write_done,
9791 .commit_setup = nfs4_proc_commit_setup,
9792 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
9793 .commit_done = nfs4_commit_done,
9794 .lock = nfs4_proc_lock,
9795 .clear_acl_cache = nfs4_zap_acl_attr,
9796 .close_context = nfs4_close_context,
9797 .open_context = nfs4_atomic_open,
9798 .have_delegation = nfs4_have_delegation,
9799 .alloc_client = nfs4_alloc_client,
9800 .init_client = nfs4_init_client,
9801 .free_client = nfs4_free_client,
9802 .create_server = nfs4_create_server,
9803 .clone_server = nfs_clone_server,
9806 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
9807 .name = XATTR_NAME_NFSV4_ACL,
9808 .list = nfs4_xattr_list_nfs4_acl,
9809 .get = nfs4_xattr_get_nfs4_acl,
9810 .set = nfs4_xattr_set_nfs4_acl,
9813 const struct xattr_handler *nfs4_xattr_handlers[] = {
9814 &nfs4_xattr_nfs4_acl_handler,
9815 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
9816 &nfs4_xattr_nfs4_label_handler,
9817 #endif
9818 NULL
9822 * Local variables:
9823 * c-basic-offset: 8
9824 * End: