xref: /linux/fs/nfs/nfs4proc.c (revision 42b16d3ac371a2fac9b6f08fd75f23f34ba3955a)
1 /*
2  *  fs/nfs/nfs4proc.c
3  *
4  *  Client-side procedure declarations for NFSv4.
5  *
6  *  Copyright (c) 2002 The Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  Kendrick Smith <kmsmith@umich.edu>
10  *  Andy Adamson   <andros@umich.edu>
11  *
12  *  Redistribution and use in source and binary forms, with or without
13  *  modification, are permitted provided that the following conditions
14  *  are met:
15  *
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.
24  *
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.
36  */
37 
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>
58 
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 "sysfs.h"
67 #include "nfs4idmap.h"
68 #include "nfs4session.h"
69 #include "fscache.h"
70 #include "nfs42.h"
71 
72 #include "nfs4trace.h"
73 
74 #define NFSDBG_FACILITY		NFSDBG_PROC
75 
76 #define NFS4_BITMASK_SZ		3
77 
78 #define NFS4_POLL_RETRY_MIN	(HZ/10)
79 #define NFS4_POLL_RETRY_MAX	(15*HZ)
80 
81 /* file attributes which can be mapped to nfs attributes */
82 #define NFS4_VALID_ATTRS (ATTR_MODE \
83 	| ATTR_UID \
84 	| ATTR_GID \
85 	| ATTR_SIZE \
86 	| ATTR_ATIME \
87 	| ATTR_MTIME \
88 	| ATTR_CTIME \
89 	| ATTR_ATIME_SET \
90 	| ATTR_MTIME_SET)
91 
92 struct nfs4_opendata;
93 static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
94 static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
95 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
96 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
97 			      struct nfs_fattr *fattr, struct inode *inode);
98 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
99 			    struct nfs_fattr *fattr, struct iattr *sattr,
100 			    struct nfs_open_context *ctx, struct nfs4_label *ilabel);
101 #ifdef CONFIG_NFS_V4_1
102 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
103 		const struct cred *cred,
104 		struct nfs4_slot *slot,
105 		bool is_privileged);
106 static int nfs41_test_stateid(struct nfs_server *, const nfs4_stateid *,
107 			      const struct cred *);
108 static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
109 			      const struct cred *, bool);
110 #endif
111 
112 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
113 static inline struct nfs4_label *
nfs4_label_init_security(struct inode * dir,struct dentry * dentry,struct iattr * sattr,struct nfs4_label * label)114 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
115 	struct iattr *sattr, struct nfs4_label *label)
116 {
117 	int err;
118 
119 	if (label == NULL)
120 		return NULL;
121 
122 	if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
123 		return NULL;
124 
125 	label->lfs = 0;
126 	label->pi = 0;
127 	label->len = 0;
128 	label->label = NULL;
129 
130 	err = security_dentry_init_security(dentry, sattr->ia_mode,
131 				&dentry->d_name, NULL,
132 				(void **)&label->label, &label->len);
133 	if (err == 0)
134 		return label;
135 
136 	return NULL;
137 }
138 static inline void
nfs4_label_release_security(struct nfs4_label * label)139 nfs4_label_release_security(struct nfs4_label *label)
140 {
141 	if (label)
142 		security_release_secctx(label->label, label->len);
143 }
nfs4_bitmask(struct nfs_server * server,struct nfs4_label * label)144 static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
145 {
146 	if (label)
147 		return server->attr_bitmask;
148 
149 	return server->attr_bitmask_nl;
150 }
151 #else
152 static inline struct nfs4_label *
nfs4_label_init_security(struct inode * dir,struct dentry * dentry,struct iattr * sattr,struct nfs4_label * l)153 nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
154 	struct iattr *sattr, struct nfs4_label *l)
155 { return NULL; }
156 static inline void
nfs4_label_release_security(struct nfs4_label * label)157 nfs4_label_release_security(struct nfs4_label *label)
158 { return; }
159 static inline u32 *
nfs4_bitmask(struct nfs_server * server,struct nfs4_label * label)160 nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
161 { return server->attr_bitmask; }
162 #endif
163 
164 /* Prevent leaks of NFSv4 errors into userland */
nfs4_map_errors(int err)165 static int nfs4_map_errors(int err)
166 {
167 	if (err >= -1000)
168 		return err;
169 	switch (err) {
170 	case -NFS4ERR_RESOURCE:
171 	case -NFS4ERR_LAYOUTTRYLATER:
172 	case -NFS4ERR_RECALLCONFLICT:
173 	case -NFS4ERR_RETURNCONFLICT:
174 		return -EREMOTEIO;
175 	case -NFS4ERR_WRONGSEC:
176 	case -NFS4ERR_WRONG_CRED:
177 		return -EPERM;
178 	case -NFS4ERR_BADOWNER:
179 	case -NFS4ERR_BADNAME:
180 		return -EINVAL;
181 	case -NFS4ERR_SHARE_DENIED:
182 		return -EACCES;
183 	case -NFS4ERR_MINOR_VERS_MISMATCH:
184 		return -EPROTONOSUPPORT;
185 	case -NFS4ERR_FILE_OPEN:
186 		return -EBUSY;
187 	case -NFS4ERR_NOT_SAME:
188 		return -ENOTSYNC;
189 	default:
190 		dprintk("%s could not handle NFSv4 error %d\n",
191 				__func__, -err);
192 		break;
193 	}
194 	return -EIO;
195 }
196 
197 /*
198  * This is our standard bitmap for GETATTR requests.
199  */
200 const u32 nfs4_fattr_bitmap[3] = {
201 	FATTR4_WORD0_TYPE
202 	| FATTR4_WORD0_CHANGE
203 	| FATTR4_WORD0_SIZE
204 	| FATTR4_WORD0_FSID
205 	| FATTR4_WORD0_FILEID,
206 	FATTR4_WORD1_MODE
207 	| FATTR4_WORD1_NUMLINKS
208 	| FATTR4_WORD1_OWNER
209 	| FATTR4_WORD1_OWNER_GROUP
210 	| FATTR4_WORD1_RAWDEV
211 	| FATTR4_WORD1_SPACE_USED
212 	| FATTR4_WORD1_TIME_ACCESS
213 	| FATTR4_WORD1_TIME_METADATA
214 	| FATTR4_WORD1_TIME_MODIFY
215 	| FATTR4_WORD1_MOUNTED_ON_FILEID,
216 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
217 	FATTR4_WORD2_SECURITY_LABEL
218 #endif
219 };
220 
221 static const u32 nfs4_pnfs_open_bitmap[3] = {
222 	FATTR4_WORD0_TYPE
223 	| FATTR4_WORD0_CHANGE
224 	| FATTR4_WORD0_SIZE
225 	| FATTR4_WORD0_FSID
226 	| FATTR4_WORD0_FILEID,
227 	FATTR4_WORD1_MODE
228 	| FATTR4_WORD1_NUMLINKS
229 	| FATTR4_WORD1_OWNER
230 	| FATTR4_WORD1_OWNER_GROUP
231 	| FATTR4_WORD1_RAWDEV
232 	| FATTR4_WORD1_SPACE_USED
233 	| FATTR4_WORD1_TIME_ACCESS
234 	| FATTR4_WORD1_TIME_METADATA
235 	| FATTR4_WORD1_TIME_MODIFY,
236 	FATTR4_WORD2_MDSTHRESHOLD
237 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
238 	| FATTR4_WORD2_SECURITY_LABEL
239 #endif
240 };
241 
242 static const u32 nfs4_open_noattr_bitmap[3] = {
243 	FATTR4_WORD0_TYPE
244 	| FATTR4_WORD0_FILEID,
245 };
246 
247 const u32 nfs4_statfs_bitmap[3] = {
248 	FATTR4_WORD0_FILES_AVAIL
249 	| FATTR4_WORD0_FILES_FREE
250 	| FATTR4_WORD0_FILES_TOTAL,
251 	FATTR4_WORD1_SPACE_AVAIL
252 	| FATTR4_WORD1_SPACE_FREE
253 	| FATTR4_WORD1_SPACE_TOTAL
254 };
255 
256 const u32 nfs4_pathconf_bitmap[3] = {
257 	FATTR4_WORD0_MAXLINK
258 	| FATTR4_WORD0_MAXNAME,
259 	0
260 };
261 
262 const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
263 			| FATTR4_WORD0_MAXREAD
264 			| FATTR4_WORD0_MAXWRITE
265 			| FATTR4_WORD0_LEASE_TIME,
266 			FATTR4_WORD1_TIME_DELTA
267 			| FATTR4_WORD1_FS_LAYOUT_TYPES,
268 			FATTR4_WORD2_LAYOUT_BLKSIZE
269 			| FATTR4_WORD2_CLONE_BLKSIZE
270 			| FATTR4_WORD2_CHANGE_ATTR_TYPE
271 			| FATTR4_WORD2_XATTR_SUPPORT
272 };
273 
274 const u32 nfs4_fs_locations_bitmap[3] = {
275 	FATTR4_WORD0_CHANGE
276 	| FATTR4_WORD0_SIZE
277 	| FATTR4_WORD0_FSID
278 	| FATTR4_WORD0_FILEID
279 	| FATTR4_WORD0_FS_LOCATIONS,
280 	FATTR4_WORD1_OWNER
281 	| FATTR4_WORD1_OWNER_GROUP
282 	| FATTR4_WORD1_RAWDEV
283 	| FATTR4_WORD1_SPACE_USED
284 	| FATTR4_WORD1_TIME_ACCESS
285 	| FATTR4_WORD1_TIME_METADATA
286 	| FATTR4_WORD1_TIME_MODIFY
287 	| FATTR4_WORD1_MOUNTED_ON_FILEID,
288 };
289 
nfs4_bitmap_copy_adjust(__u32 * dst,const __u32 * src,struct inode * inode,unsigned long flags)290 static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
291 				    struct inode *inode, unsigned long flags)
292 {
293 	unsigned long cache_validity;
294 
295 	memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
296 	if (!inode || !nfs_have_read_or_write_delegation(inode))
297 		return;
298 
299 	cache_validity = READ_ONCE(NFS_I(inode)->cache_validity) | flags;
300 
301 	/* Remove the attributes over which we have full control */
302 	dst[1] &= ~FATTR4_WORD1_RAWDEV;
303 	if (!(cache_validity & NFS_INO_INVALID_SIZE))
304 		dst[0] &= ~FATTR4_WORD0_SIZE;
305 
306 	if (!(cache_validity & NFS_INO_INVALID_CHANGE))
307 		dst[0] &= ~FATTR4_WORD0_CHANGE;
308 
309 	if (!(cache_validity & NFS_INO_INVALID_MODE))
310 		dst[1] &= ~FATTR4_WORD1_MODE;
311 	if (!(cache_validity & NFS_INO_INVALID_OTHER))
312 		dst[1] &= ~(FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP);
313 
314 	if (nfs_have_delegated_mtime(inode)) {
315 		if (!(cache_validity & NFS_INO_INVALID_ATIME))
316 			dst[1] &= ~FATTR4_WORD1_TIME_ACCESS;
317 		if (!(cache_validity & NFS_INO_INVALID_MTIME))
318 			dst[1] &= ~FATTR4_WORD1_TIME_MODIFY;
319 		if (!(cache_validity & NFS_INO_INVALID_CTIME))
320 			dst[1] &= ~FATTR4_WORD1_TIME_METADATA;
321 	} else if (nfs_have_delegated_atime(inode)) {
322 		if (!(cache_validity & NFS_INO_INVALID_ATIME))
323 			dst[1] &= ~FATTR4_WORD1_TIME_ACCESS;
324 	}
325 }
326 
nfs4_setup_readdir(u64 cookie,__be32 * verifier,struct dentry * dentry,struct nfs4_readdir_arg * readdir)327 static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
328 		struct nfs4_readdir_arg *readdir)
329 {
330 	unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
331 	__be32 *start, *p;
332 
333 	if (cookie > 2) {
334 		readdir->cookie = cookie;
335 		memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
336 		return;
337 	}
338 
339 	readdir->cookie = 0;
340 	memset(&readdir->verifier, 0, sizeof(readdir->verifier));
341 	if (cookie == 2)
342 		return;
343 
344 	/*
345 	 * NFSv4 servers do not return entries for '.' and '..'
346 	 * Therefore, we fake these entries here.  We let '.'
347 	 * have cookie 0 and '..' have cookie 1.  Note that
348 	 * when talking to the server, we always send cookie 0
349 	 * instead of 1 or 2.
350 	 */
351 	start = p = kmap_atomic(*readdir->pages);
352 
353 	if (cookie == 0) {
354 		*p++ = xdr_one;                                  /* next */
355 		*p++ = xdr_zero;                   /* cookie, first word */
356 		*p++ = xdr_one;                   /* cookie, second word */
357 		*p++ = xdr_one;                             /* entry len */
358 		memcpy(p, ".\0\0\0", 4);                        /* entry */
359 		p++;
360 		*p++ = xdr_one;                         /* bitmap length */
361 		*p++ = htonl(attrs);                           /* bitmap */
362 		*p++ = htonl(12);             /* attribute buffer length */
363 		*p++ = htonl(NF4DIR);
364 		p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
365 	}
366 
367 	*p++ = xdr_one;                                  /* next */
368 	*p++ = xdr_zero;                   /* cookie, first word */
369 	*p++ = xdr_two;                   /* cookie, second word */
370 	*p++ = xdr_two;                             /* entry len */
371 	memcpy(p, "..\0\0", 4);                         /* entry */
372 	p++;
373 	*p++ = xdr_one;                         /* bitmap length */
374 	*p++ = htonl(attrs);                           /* bitmap */
375 	*p++ = htonl(12);             /* attribute buffer length */
376 	*p++ = htonl(NF4DIR);
377 	p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
378 
379 	readdir->pgbase = (char *)p - (char *)start;
380 	readdir->count -= readdir->pgbase;
381 	kunmap_atomic(start);
382 }
383 
nfs4_fattr_set_prechange(struct nfs_fattr * fattr,u64 version)384 static void nfs4_fattr_set_prechange(struct nfs_fattr *fattr, u64 version)
385 {
386 	if (!(fattr->valid & NFS_ATTR_FATTR_PRECHANGE)) {
387 		fattr->pre_change_attr = version;
388 		fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
389 	}
390 }
391 
nfs4_test_and_free_stateid(struct nfs_server * server,nfs4_stateid * stateid,const struct cred * cred)392 static void nfs4_test_and_free_stateid(struct nfs_server *server,
393 		nfs4_stateid *stateid,
394 		const struct cred *cred)
395 {
396 	const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
397 
398 	ops->test_and_free_expired(server, stateid, cred);
399 }
400 
__nfs4_free_revoked_stateid(struct nfs_server * server,nfs4_stateid * stateid,const struct cred * cred)401 static void __nfs4_free_revoked_stateid(struct nfs_server *server,
402 		nfs4_stateid *stateid,
403 		const struct cred *cred)
404 {
405 	stateid->type = NFS4_REVOKED_STATEID_TYPE;
406 	nfs4_test_and_free_stateid(server, stateid, cred);
407 }
408 
nfs4_free_revoked_stateid(struct nfs_server * server,const nfs4_stateid * stateid,const struct cred * cred)409 static void nfs4_free_revoked_stateid(struct nfs_server *server,
410 		const nfs4_stateid *stateid,
411 		const struct cred *cred)
412 {
413 	nfs4_stateid tmp;
414 
415 	nfs4_stateid_copy(&tmp, stateid);
416 	__nfs4_free_revoked_stateid(server, &tmp, cred);
417 }
418 
nfs4_update_delay(long * timeout)419 static long nfs4_update_delay(long *timeout)
420 {
421 	long ret;
422 	if (!timeout)
423 		return NFS4_POLL_RETRY_MAX;
424 	if (*timeout <= 0)
425 		*timeout = NFS4_POLL_RETRY_MIN;
426 	if (*timeout > NFS4_POLL_RETRY_MAX)
427 		*timeout = NFS4_POLL_RETRY_MAX;
428 	ret = *timeout;
429 	*timeout <<= 1;
430 	return ret;
431 }
432 
nfs4_delay_killable(long * timeout)433 static int nfs4_delay_killable(long *timeout)
434 {
435 	might_sleep();
436 
437 	__set_current_state(TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
438 	schedule_timeout(nfs4_update_delay(timeout));
439 	if (!__fatal_signal_pending(current))
440 		return 0;
441 	return -EINTR;
442 }
443 
nfs4_delay_interruptible(long * timeout)444 static int nfs4_delay_interruptible(long *timeout)
445 {
446 	might_sleep();
447 
448 	__set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE_UNSAFE);
449 	schedule_timeout(nfs4_update_delay(timeout));
450 	if (!signal_pending(current))
451 		return 0;
452 	return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
453 }
454 
nfs4_delay(long * timeout,bool interruptible)455 static int nfs4_delay(long *timeout, bool interruptible)
456 {
457 	if (interruptible)
458 		return nfs4_delay_interruptible(timeout);
459 	return nfs4_delay_killable(timeout);
460 }
461 
462 static const nfs4_stateid *
nfs4_recoverable_stateid(const nfs4_stateid * stateid)463 nfs4_recoverable_stateid(const nfs4_stateid *stateid)
464 {
465 	if (!stateid)
466 		return NULL;
467 	switch (stateid->type) {
468 	case NFS4_OPEN_STATEID_TYPE:
469 	case NFS4_LOCK_STATEID_TYPE:
470 	case NFS4_DELEGATION_STATEID_TYPE:
471 		return stateid;
472 	default:
473 		break;
474 	}
475 	return NULL;
476 }
477 
478 /* This is the error handling routine for processes that are allowed
479  * to sleep.
480  */
nfs4_do_handle_exception(struct nfs_server * server,int errorcode,struct nfs4_exception * exception)481 static int nfs4_do_handle_exception(struct nfs_server *server,
482 		int errorcode, struct nfs4_exception *exception)
483 {
484 	struct nfs_client *clp = server->nfs_client;
485 	struct nfs4_state *state = exception->state;
486 	const nfs4_stateid *stateid;
487 	struct inode *inode = exception->inode;
488 	int ret = errorcode;
489 
490 	exception->delay = 0;
491 	exception->recovering = 0;
492 	exception->retry = 0;
493 
494 	stateid = nfs4_recoverable_stateid(exception->stateid);
495 	if (stateid == NULL && state != NULL)
496 		stateid = nfs4_recoverable_stateid(&state->stateid);
497 
498 	switch(errorcode) {
499 		case 0:
500 			return 0;
501 		case -NFS4ERR_BADHANDLE:
502 		case -ESTALE:
503 			if (inode != NULL && S_ISREG(inode->i_mode))
504 				pnfs_destroy_layout(NFS_I(inode));
505 			break;
506 		case -NFS4ERR_DELEG_REVOKED:
507 		case -NFS4ERR_ADMIN_REVOKED:
508 		case -NFS4ERR_EXPIRED:
509 		case -NFS4ERR_BAD_STATEID:
510 		case -NFS4ERR_PARTNER_NO_AUTH:
511 			if (inode != NULL && stateid != NULL) {
512 				nfs_inode_find_state_and_recover(inode,
513 						stateid);
514 				goto wait_on_recovery;
515 			}
516 			fallthrough;
517 		case -NFS4ERR_OPENMODE:
518 			if (inode) {
519 				int err;
520 
521 				err = nfs_async_inode_return_delegation(inode,
522 						stateid);
523 				if (err == 0)
524 					goto wait_on_recovery;
525 				if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
526 					exception->retry = 1;
527 					break;
528 				}
529 			}
530 			if (state == NULL)
531 				break;
532 			ret = nfs4_schedule_stateid_recovery(server, state);
533 			if (ret < 0)
534 				break;
535 			goto wait_on_recovery;
536 		case -NFS4ERR_STALE_STATEID:
537 		case -NFS4ERR_STALE_CLIENTID:
538 			nfs4_schedule_lease_recovery(clp);
539 			goto wait_on_recovery;
540 		case -NFS4ERR_MOVED:
541 			ret = nfs4_schedule_migration_recovery(server);
542 			if (ret < 0)
543 				break;
544 			goto wait_on_recovery;
545 		case -NFS4ERR_LEASE_MOVED:
546 			nfs4_schedule_lease_moved_recovery(clp);
547 			goto wait_on_recovery;
548 #if defined(CONFIG_NFS_V4_1)
549 		case -NFS4ERR_BADSESSION:
550 		case -NFS4ERR_BADSLOT:
551 		case -NFS4ERR_BAD_HIGH_SLOT:
552 		case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
553 		case -NFS4ERR_DEADSESSION:
554 		case -NFS4ERR_SEQ_FALSE_RETRY:
555 		case -NFS4ERR_SEQ_MISORDERED:
556 			/* Handled in nfs41_sequence_process() */
557 			goto wait_on_recovery;
558 #endif /* defined(CONFIG_NFS_V4_1) */
559 		case -NFS4ERR_FILE_OPEN:
560 			if (exception->timeout > HZ) {
561 				/* We have retried a decent amount, time to
562 				 * fail
563 				 */
564 				ret = -EBUSY;
565 				break;
566 			}
567 			fallthrough;
568 		case -NFS4ERR_DELAY:
569 			nfs_inc_server_stats(server, NFSIOS_DELAY);
570 			fallthrough;
571 		case -NFS4ERR_GRACE:
572 		case -NFS4ERR_LAYOUTTRYLATER:
573 		case -NFS4ERR_RECALLCONFLICT:
574 		case -NFS4ERR_RETURNCONFLICT:
575 			exception->delay = 1;
576 			return 0;
577 
578 		case -NFS4ERR_RETRY_UNCACHED_REP:
579 		case -NFS4ERR_OLD_STATEID:
580 			exception->retry = 1;
581 			break;
582 		case -NFS4ERR_BADOWNER:
583 			/* The following works around a Linux server bug! */
584 		case -NFS4ERR_BADNAME:
585 			if (server->caps & NFS_CAP_UIDGID_NOMAP) {
586 				server->caps &= ~NFS_CAP_UIDGID_NOMAP;
587 				exception->retry = 1;
588 				printk(KERN_WARNING "NFS: v4 server %s "
589 						"does not accept raw "
590 						"uid/gids. "
591 						"Reenabling the idmapper.\n",
592 						server->nfs_client->cl_hostname);
593 			}
594 	}
595 	/* We failed to handle the error */
596 	return nfs4_map_errors(ret);
597 wait_on_recovery:
598 	exception->recovering = 1;
599 	return 0;
600 }
601 
602 /*
603  * Track the number of NFS4ERR_DELAY related retransmissions and return
604  * EAGAIN if the 'softerr' mount option is set, and we've exceeded the limit
605  * set by 'nfs_delay_retrans'.
606  */
nfs4_exception_should_retrans(const struct nfs_server * server,struct nfs4_exception * exception)607 static int nfs4_exception_should_retrans(const struct nfs_server *server,
608 					 struct nfs4_exception *exception)
609 {
610 	if (server->flags & NFS_MOUNT_SOFTERR && nfs_delay_retrans >= 0) {
611 		if (exception->retrans++ >= (unsigned short)nfs_delay_retrans)
612 			return -EAGAIN;
613 	}
614 	return 0;
615 }
616 
617 /* This is the error handling routine for processes that are allowed
618  * to sleep.
619  */
nfs4_handle_exception(struct nfs_server * server,int errorcode,struct nfs4_exception * exception)620 int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
621 {
622 	struct nfs_client *clp = server->nfs_client;
623 	int ret;
624 
625 	ret = nfs4_do_handle_exception(server, errorcode, exception);
626 	if (exception->delay) {
627 		int ret2 = nfs4_exception_should_retrans(server, exception);
628 		if (ret2 < 0) {
629 			exception->retry = 0;
630 			return ret2;
631 		}
632 		ret = nfs4_delay(&exception->timeout,
633 				exception->interruptible);
634 		goto out_retry;
635 	}
636 	if (exception->recovering) {
637 		if (exception->task_is_privileged)
638 			return -EDEADLOCK;
639 		ret = nfs4_wait_clnt_recover(clp);
640 		if (test_bit(NFS_MIG_FAILED, &server->mig_status))
641 			return -EIO;
642 		goto out_retry;
643 	}
644 	return ret;
645 out_retry:
646 	if (ret == 0)
647 		exception->retry = 1;
648 	return ret;
649 }
650 
651 static int
nfs4_async_handle_exception(struct rpc_task * task,struct nfs_server * server,int errorcode,struct nfs4_exception * exception)652 nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
653 		int errorcode, struct nfs4_exception *exception)
654 {
655 	struct nfs_client *clp = server->nfs_client;
656 	int ret;
657 
658 	ret = nfs4_do_handle_exception(server, errorcode, exception);
659 	if (exception->delay) {
660 		int ret2 = nfs4_exception_should_retrans(server, exception);
661 		if (ret2 < 0) {
662 			exception->retry = 0;
663 			return ret2;
664 		}
665 		rpc_delay(task, nfs4_update_delay(&exception->timeout));
666 		goto out_retry;
667 	}
668 	if (exception->recovering) {
669 		if (exception->task_is_privileged)
670 			return -EDEADLOCK;
671 		rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
672 		if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
673 			rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
674 		goto out_retry;
675 	}
676 	if (test_bit(NFS_MIG_FAILED, &server->mig_status))
677 		ret = -EIO;
678 	return ret;
679 out_retry:
680 	if (ret == 0) {
681 		exception->retry = 1;
682 		/*
683 		 * For NFS4ERR_MOVED, the client transport will need to
684 		 * be recomputed after migration recovery has completed.
685 		 */
686 		if (errorcode == -NFS4ERR_MOVED)
687 			rpc_task_release_transport(task);
688 	}
689 	return ret;
690 }
691 
692 int
nfs4_async_handle_error(struct rpc_task * task,struct nfs_server * server,struct nfs4_state * state,long * timeout)693 nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
694 			struct nfs4_state *state, long *timeout)
695 {
696 	struct nfs4_exception exception = {
697 		.state = state,
698 	};
699 
700 	if (task->tk_status >= 0)
701 		return 0;
702 	if (timeout)
703 		exception.timeout = *timeout;
704 	task->tk_status = nfs4_async_handle_exception(task, server,
705 			task->tk_status,
706 			&exception);
707 	if (exception.delay && timeout)
708 		*timeout = exception.timeout;
709 	if (exception.retry)
710 		return -EAGAIN;
711 	return 0;
712 }
713 
714 /*
715  * Return 'true' if 'clp' is using an rpc_client that is integrity protected
716  * or 'false' otherwise.
717  */
_nfs4_is_integrity_protected(struct nfs_client * clp)718 static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
719 {
720 	rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
721 	return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
722 }
723 
do_renew_lease(struct nfs_client * clp,unsigned long timestamp)724 static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
725 {
726 	spin_lock(&clp->cl_lock);
727 	if (time_before(clp->cl_last_renewal,timestamp))
728 		clp->cl_last_renewal = timestamp;
729 	spin_unlock(&clp->cl_lock);
730 }
731 
renew_lease(const struct nfs_server * server,unsigned long timestamp)732 static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
733 {
734 	struct nfs_client *clp = server->nfs_client;
735 
736 	if (!nfs4_has_session(clp))
737 		do_renew_lease(clp, timestamp);
738 }
739 
740 struct nfs4_call_sync_data {
741 	const struct nfs_server *seq_server;
742 	struct nfs4_sequence_args *seq_args;
743 	struct nfs4_sequence_res *seq_res;
744 };
745 
nfs4_init_sequence(struct nfs4_sequence_args * args,struct nfs4_sequence_res * res,int cache_reply,int privileged)746 void nfs4_init_sequence(struct nfs4_sequence_args *args,
747 			struct nfs4_sequence_res *res, int cache_reply,
748 			int privileged)
749 {
750 	args->sa_slot = NULL;
751 	args->sa_cache_this = cache_reply;
752 	args->sa_privileged = privileged;
753 
754 	res->sr_slot = NULL;
755 }
756 
nfs40_sequence_free_slot(struct nfs4_sequence_res * res)757 static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
758 {
759 	struct nfs4_slot *slot = res->sr_slot;
760 	struct nfs4_slot_table *tbl;
761 
762 	tbl = slot->table;
763 	spin_lock(&tbl->slot_tbl_lock);
764 	if (!nfs41_wake_and_assign_slot(tbl, slot))
765 		nfs4_free_slot(tbl, slot);
766 	spin_unlock(&tbl->slot_tbl_lock);
767 
768 	res->sr_slot = NULL;
769 }
770 
nfs40_sequence_done(struct rpc_task * task,struct nfs4_sequence_res * res)771 static int nfs40_sequence_done(struct rpc_task *task,
772 			       struct nfs4_sequence_res *res)
773 {
774 	if (res->sr_slot != NULL)
775 		nfs40_sequence_free_slot(res);
776 	return 1;
777 }
778 
779 #if defined(CONFIG_NFS_V4_1)
780 
nfs41_release_slot(struct nfs4_slot * slot)781 static void nfs41_release_slot(struct nfs4_slot *slot)
782 {
783 	struct nfs4_session *session;
784 	struct nfs4_slot_table *tbl;
785 	bool send_new_highest_used_slotid = false;
786 
787 	if (!slot)
788 		return;
789 	tbl = slot->table;
790 	session = tbl->session;
791 
792 	/* Bump the slot sequence number */
793 	if (slot->seq_done)
794 		slot->seq_nr++;
795 	slot->seq_done = 0;
796 
797 	spin_lock(&tbl->slot_tbl_lock);
798 	/* Be nice to the server: try to ensure that the last transmitted
799 	 * value for highest_user_slotid <= target_highest_slotid
800 	 */
801 	if (tbl->highest_used_slotid > tbl->target_highest_slotid)
802 		send_new_highest_used_slotid = true;
803 
804 	if (nfs41_wake_and_assign_slot(tbl, slot)) {
805 		send_new_highest_used_slotid = false;
806 		goto out_unlock;
807 	}
808 	nfs4_free_slot(tbl, slot);
809 
810 	if (tbl->highest_used_slotid != NFS4_NO_SLOT)
811 		send_new_highest_used_slotid = false;
812 out_unlock:
813 	spin_unlock(&tbl->slot_tbl_lock);
814 	if (send_new_highest_used_slotid)
815 		nfs41_notify_server(session->clp);
816 	if (waitqueue_active(&tbl->slot_waitq))
817 		wake_up_all(&tbl->slot_waitq);
818 }
819 
nfs41_sequence_free_slot(struct nfs4_sequence_res * res)820 static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
821 {
822 	nfs41_release_slot(res->sr_slot);
823 	res->sr_slot = NULL;
824 }
825 
nfs4_slot_sequence_record_sent(struct nfs4_slot * slot,u32 seqnr)826 static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
827 		u32 seqnr)
828 {
829 	if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
830 		slot->seq_nr_highest_sent = seqnr;
831 }
nfs4_slot_sequence_acked(struct nfs4_slot * slot,u32 seqnr)832 static void nfs4_slot_sequence_acked(struct nfs4_slot *slot, u32 seqnr)
833 {
834 	nfs4_slot_sequence_record_sent(slot, seqnr);
835 	slot->seq_nr_last_acked = seqnr;
836 }
837 
nfs4_probe_sequence(struct nfs_client * client,const struct cred * cred,struct nfs4_slot * slot)838 static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
839 				struct nfs4_slot *slot)
840 {
841 	struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
842 	if (!IS_ERR(task))
843 		rpc_put_task_async(task);
844 }
845 
nfs41_sequence_process(struct rpc_task * task,struct nfs4_sequence_res * res)846 static int nfs41_sequence_process(struct rpc_task *task,
847 		struct nfs4_sequence_res *res)
848 {
849 	struct nfs4_session *session;
850 	struct nfs4_slot *slot = res->sr_slot;
851 	struct nfs_client *clp;
852 	int status;
853 	int ret = 1;
854 
855 	if (slot == NULL)
856 		goto out_noaction;
857 	/* don't increment the sequence number if the task wasn't sent */
858 	if (!RPC_WAS_SENT(task) || slot->seq_done)
859 		goto out;
860 
861 	session = slot->table->session;
862 	clp = session->clp;
863 
864 	trace_nfs4_sequence_done(session, res);
865 
866 	status = res->sr_status;
867 	if (task->tk_status == -NFS4ERR_DEADSESSION)
868 		status = -NFS4ERR_DEADSESSION;
869 
870 	/* Check the SEQUENCE operation status */
871 	switch (status) {
872 	case 0:
873 		/* Mark this sequence number as having been acked */
874 		nfs4_slot_sequence_acked(slot, slot->seq_nr);
875 		/* Update the slot's sequence and clientid lease timer */
876 		slot->seq_done = 1;
877 		do_renew_lease(clp, res->sr_timestamp);
878 		/* Check sequence flags */
879 		nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
880 				!!slot->privileged);
881 		nfs41_update_target_slotid(slot->table, slot, res);
882 		break;
883 	case 1:
884 		/*
885 		 * sr_status remains 1 if an RPC level error occurred.
886 		 * The server may or may not have processed the sequence
887 		 * operation..
888 		 */
889 		nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
890 		slot->seq_done = 1;
891 		goto out;
892 	case -NFS4ERR_DELAY:
893 		/* The server detected a resend of the RPC call and
894 		 * returned NFS4ERR_DELAY as per Section 2.10.6.2
895 		 * of RFC5661.
896 		 */
897 		dprintk("%s: slot=%u seq=%u: Operation in progress\n",
898 			__func__,
899 			slot->slot_nr,
900 			slot->seq_nr);
901 		goto out_retry;
902 	case -NFS4ERR_RETRY_UNCACHED_REP:
903 	case -NFS4ERR_SEQ_FALSE_RETRY:
904 		/*
905 		 * The server thinks we tried to replay a request.
906 		 * Retry the call after bumping the sequence ID.
907 		 */
908 		nfs4_slot_sequence_acked(slot, slot->seq_nr);
909 		goto retry_new_seq;
910 	case -NFS4ERR_BADSLOT:
911 		/*
912 		 * The slot id we used was probably retired. Try again
913 		 * using a different slot id.
914 		 */
915 		if (slot->slot_nr < slot->table->target_highest_slotid)
916 			goto session_recover;
917 		goto retry_nowait;
918 	case -NFS4ERR_SEQ_MISORDERED:
919 		nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
920 		/*
921 		 * Were one or more calls using this slot interrupted?
922 		 * If the server never received the request, then our
923 		 * transmitted slot sequence number may be too high. However,
924 		 * if the server did receive the request then it might
925 		 * accidentally give us a reply with a mismatched operation.
926 		 * We can sort this out by sending a lone sequence operation
927 		 * to the server on the same slot.
928 		 */
929 		if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
930 			slot->seq_nr--;
931 			if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
932 				nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
933 				res->sr_slot = NULL;
934 			}
935 			goto retry_nowait;
936 		}
937 		/*
938 		 * RFC5661:
939 		 * A retry might be sent while the original request is
940 		 * still in progress on the replier. The replier SHOULD
941 		 * deal with the issue by returning NFS4ERR_DELAY as the
942 		 * reply to SEQUENCE or CB_SEQUENCE operation, but
943 		 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
944 		 *
945 		 * Restart the search after a delay.
946 		 */
947 		slot->seq_nr = slot->seq_nr_highest_sent;
948 		goto out_retry;
949 	case -NFS4ERR_BADSESSION:
950 	case -NFS4ERR_DEADSESSION:
951 	case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
952 		goto session_recover;
953 	default:
954 		/* Just update the slot sequence no. */
955 		slot->seq_done = 1;
956 	}
957 out:
958 	/* The session may be reset by one of the error handlers. */
959 	dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
960 out_noaction:
961 	return ret;
962 session_recover:
963 	set_bit(NFS4_SLOT_TBL_DRAINING, &session->fc_slot_table.slot_tbl_state);
964 	nfs4_schedule_session_recovery(session, status);
965 	dprintk("%s ERROR: %d Reset session\n", __func__, status);
966 	nfs41_sequence_free_slot(res);
967 	goto out;
968 retry_new_seq:
969 	++slot->seq_nr;
970 retry_nowait:
971 	if (rpc_restart_call_prepare(task)) {
972 		nfs41_sequence_free_slot(res);
973 		task->tk_status = 0;
974 		ret = 0;
975 	}
976 	goto out;
977 out_retry:
978 	if (!rpc_restart_call(task))
979 		goto out;
980 	rpc_delay(task, NFS4_POLL_RETRY_MAX);
981 	return 0;
982 }
983 
nfs41_sequence_done(struct rpc_task * task,struct nfs4_sequence_res * res)984 int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
985 {
986 	if (!nfs41_sequence_process(task, res))
987 		return 0;
988 	if (res->sr_slot != NULL)
989 		nfs41_sequence_free_slot(res);
990 	return 1;
991 
992 }
993 EXPORT_SYMBOL_GPL(nfs41_sequence_done);
994 
nfs4_sequence_process(struct rpc_task * task,struct nfs4_sequence_res * res)995 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
996 {
997 	if (res->sr_slot == NULL)
998 		return 1;
999 	if (res->sr_slot->table->session != NULL)
1000 		return nfs41_sequence_process(task, res);
1001 	return nfs40_sequence_done(task, res);
1002 }
1003 
nfs4_sequence_free_slot(struct nfs4_sequence_res * res)1004 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1005 {
1006 	if (res->sr_slot != NULL) {
1007 		if (res->sr_slot->table->session != NULL)
1008 			nfs41_sequence_free_slot(res);
1009 		else
1010 			nfs40_sequence_free_slot(res);
1011 	}
1012 }
1013 
nfs4_sequence_done(struct rpc_task * task,struct nfs4_sequence_res * res)1014 int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
1015 {
1016 	if (res->sr_slot == NULL)
1017 		return 1;
1018 	if (!res->sr_slot->table->session)
1019 		return nfs40_sequence_done(task, res);
1020 	return nfs41_sequence_done(task, res);
1021 }
1022 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1023 
nfs41_call_sync_prepare(struct rpc_task * task,void * calldata)1024 static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
1025 {
1026 	struct nfs4_call_sync_data *data = calldata;
1027 
1028 	dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
1029 
1030 	nfs4_setup_sequence(data->seq_server->nfs_client,
1031 			    data->seq_args, data->seq_res, task);
1032 }
1033 
nfs41_call_sync_done(struct rpc_task * task,void * calldata)1034 static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
1035 {
1036 	struct nfs4_call_sync_data *data = calldata;
1037 
1038 	nfs41_sequence_done(task, data->seq_res);
1039 }
1040 
1041 static const struct rpc_call_ops nfs41_call_sync_ops = {
1042 	.rpc_call_prepare = nfs41_call_sync_prepare,
1043 	.rpc_call_done = nfs41_call_sync_done,
1044 };
1045 
1046 #else	/* !CONFIG_NFS_V4_1 */
1047 
nfs4_sequence_process(struct rpc_task * task,struct nfs4_sequence_res * res)1048 static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1049 {
1050 	return nfs40_sequence_done(task, res);
1051 }
1052 
nfs4_sequence_free_slot(struct nfs4_sequence_res * res)1053 static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1054 {
1055 	if (res->sr_slot != NULL)
1056 		nfs40_sequence_free_slot(res);
1057 }
1058 
nfs4_sequence_done(struct rpc_task * task,struct nfs4_sequence_res * res)1059 int nfs4_sequence_done(struct rpc_task *task,
1060 		       struct nfs4_sequence_res *res)
1061 {
1062 	return nfs40_sequence_done(task, res);
1063 }
1064 EXPORT_SYMBOL_GPL(nfs4_sequence_done);
1065 
1066 #endif	/* !CONFIG_NFS_V4_1 */
1067 
nfs41_sequence_res_init(struct nfs4_sequence_res * res)1068 static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1069 {
1070 	res->sr_timestamp = jiffies;
1071 	res->sr_status_flags = 0;
1072 	res->sr_status = 1;
1073 }
1074 
1075 static
nfs4_sequence_attach_slot(struct nfs4_sequence_args * args,struct nfs4_sequence_res * res,struct nfs4_slot * slot)1076 void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1077 		struct nfs4_sequence_res *res,
1078 		struct nfs4_slot *slot)
1079 {
1080 	if (!slot)
1081 		return;
1082 	slot->privileged = args->sa_privileged ? 1 : 0;
1083 	args->sa_slot = slot;
1084 
1085 	res->sr_slot = slot;
1086 }
1087 
nfs4_setup_sequence(struct nfs_client * client,struct nfs4_sequence_args * args,struct nfs4_sequence_res * res,struct rpc_task * task)1088 int nfs4_setup_sequence(struct nfs_client *client,
1089 			struct nfs4_sequence_args *args,
1090 			struct nfs4_sequence_res *res,
1091 			struct rpc_task *task)
1092 {
1093 	struct nfs4_session *session = nfs4_get_session(client);
1094 	struct nfs4_slot_table *tbl  = client->cl_slot_tbl;
1095 	struct nfs4_slot *slot;
1096 
1097 	/* slot already allocated? */
1098 	if (res->sr_slot != NULL)
1099 		goto out_start;
1100 
1101 	if (session)
1102 		tbl = &session->fc_slot_table;
1103 
1104 	spin_lock(&tbl->slot_tbl_lock);
1105 	/* The state manager will wait until the slot table is empty */
1106 	if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1107 		goto out_sleep;
1108 
1109 	slot = nfs4_alloc_slot(tbl);
1110 	if (IS_ERR(slot)) {
1111 		if (slot == ERR_PTR(-ENOMEM))
1112 			goto out_sleep_timeout;
1113 		goto out_sleep;
1114 	}
1115 	spin_unlock(&tbl->slot_tbl_lock);
1116 
1117 	nfs4_sequence_attach_slot(args, res, slot);
1118 
1119 	trace_nfs4_setup_sequence(session, args);
1120 out_start:
1121 	nfs41_sequence_res_init(res);
1122 	rpc_call_start(task);
1123 	return 0;
1124 out_sleep_timeout:
1125 	/* Try again in 1/4 second */
1126 	if (args->sa_privileged)
1127 		rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1128 				jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1129 	else
1130 		rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1131 				NULL, jiffies + (HZ >> 2));
1132 	spin_unlock(&tbl->slot_tbl_lock);
1133 	return -EAGAIN;
1134 out_sleep:
1135 	if (args->sa_privileged)
1136 		rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1137 				RPC_PRIORITY_PRIVILEGED);
1138 	else
1139 		rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1140 	spin_unlock(&tbl->slot_tbl_lock);
1141 	return -EAGAIN;
1142 }
1143 EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1144 
nfs40_call_sync_prepare(struct rpc_task * task,void * calldata)1145 static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1146 {
1147 	struct nfs4_call_sync_data *data = calldata;
1148 	nfs4_setup_sequence(data->seq_server->nfs_client,
1149 				data->seq_args, data->seq_res, task);
1150 }
1151 
nfs40_call_sync_done(struct rpc_task * task,void * calldata)1152 static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1153 {
1154 	struct nfs4_call_sync_data *data = calldata;
1155 	nfs4_sequence_done(task, data->seq_res);
1156 }
1157 
1158 static const struct rpc_call_ops nfs40_call_sync_ops = {
1159 	.rpc_call_prepare = nfs40_call_sync_prepare,
1160 	.rpc_call_done = nfs40_call_sync_done,
1161 };
1162 
nfs4_call_sync_custom(struct rpc_task_setup * task_setup)1163 static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1164 {
1165 	int ret;
1166 	struct rpc_task *task;
1167 
1168 	task = rpc_run_task(task_setup);
1169 	if (IS_ERR(task))
1170 		return PTR_ERR(task);
1171 
1172 	ret = task->tk_status;
1173 	rpc_put_task(task);
1174 	return ret;
1175 }
1176 
nfs4_do_call_sync(struct rpc_clnt * clnt,struct nfs_server * server,struct rpc_message * msg,struct nfs4_sequence_args * args,struct nfs4_sequence_res * res,unsigned short task_flags)1177 static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1178 			     struct nfs_server *server,
1179 			     struct rpc_message *msg,
1180 			     struct nfs4_sequence_args *args,
1181 			     struct nfs4_sequence_res *res,
1182 			     unsigned short task_flags)
1183 {
1184 	struct nfs_client *clp = server->nfs_client;
1185 	struct nfs4_call_sync_data data = {
1186 		.seq_server = server,
1187 		.seq_args = args,
1188 		.seq_res = res,
1189 	};
1190 	struct rpc_task_setup task_setup = {
1191 		.rpc_client = clnt,
1192 		.rpc_message = msg,
1193 		.callback_ops = clp->cl_mvops->call_sync_ops,
1194 		.callback_data = &data,
1195 		.flags = task_flags,
1196 	};
1197 
1198 	return nfs4_call_sync_custom(&task_setup);
1199 }
1200 
nfs4_call_sync_sequence(struct rpc_clnt * clnt,struct nfs_server * server,struct rpc_message * msg,struct nfs4_sequence_args * args,struct nfs4_sequence_res * res)1201 static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1202 				   struct nfs_server *server,
1203 				   struct rpc_message *msg,
1204 				   struct nfs4_sequence_args *args,
1205 				   struct nfs4_sequence_res *res)
1206 {
1207 	unsigned short task_flags = 0;
1208 
1209 	if (server->caps & NFS_CAP_MOVEABLE)
1210 		task_flags = RPC_TASK_MOVEABLE;
1211 	return nfs4_do_call_sync(clnt, server, msg, args, res, task_flags);
1212 }
1213 
1214 
nfs4_call_sync(struct rpc_clnt * clnt,struct nfs_server * server,struct rpc_message * msg,struct nfs4_sequence_args * args,struct nfs4_sequence_res * res,int cache_reply)1215 int nfs4_call_sync(struct rpc_clnt *clnt,
1216 		   struct nfs_server *server,
1217 		   struct rpc_message *msg,
1218 		   struct nfs4_sequence_args *args,
1219 		   struct nfs4_sequence_res *res,
1220 		   int cache_reply)
1221 {
1222 	nfs4_init_sequence(args, res, cache_reply, 0);
1223 	return nfs4_call_sync_sequence(clnt, server, msg, args, res);
1224 }
1225 
1226 static void
nfs4_inc_nlink_locked(struct inode * inode)1227 nfs4_inc_nlink_locked(struct inode *inode)
1228 {
1229 	nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1230 					     NFS_INO_INVALID_CTIME |
1231 					     NFS_INO_INVALID_NLINK);
1232 	inc_nlink(inode);
1233 }
1234 
1235 static void
nfs4_inc_nlink(struct inode * inode)1236 nfs4_inc_nlink(struct inode *inode)
1237 {
1238 	spin_lock(&inode->i_lock);
1239 	nfs4_inc_nlink_locked(inode);
1240 	spin_unlock(&inode->i_lock);
1241 }
1242 
1243 static void
nfs4_dec_nlink_locked(struct inode * inode)1244 nfs4_dec_nlink_locked(struct inode *inode)
1245 {
1246 	nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
1247 					     NFS_INO_INVALID_CTIME |
1248 					     NFS_INO_INVALID_NLINK);
1249 	drop_nlink(inode);
1250 }
1251 
1252 static void
nfs4_update_changeattr_locked(struct inode * inode,struct nfs4_change_info * cinfo,unsigned long timestamp,unsigned long cache_validity)1253 nfs4_update_changeattr_locked(struct inode *inode,
1254 		struct nfs4_change_info *cinfo,
1255 		unsigned long timestamp, unsigned long cache_validity)
1256 {
1257 	struct nfs_inode *nfsi = NFS_I(inode);
1258 	u64 change_attr = inode_peek_iversion_raw(inode);
1259 
1260 	if (!nfs_have_delegated_mtime(inode))
1261 		cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME;
1262 	if (S_ISDIR(inode->i_mode))
1263 		cache_validity |= NFS_INO_INVALID_DATA;
1264 
1265 	switch (NFS_SERVER(inode)->change_attr_type) {
1266 	case NFS4_CHANGE_TYPE_IS_UNDEFINED:
1267 		if (cinfo->after == change_attr)
1268 			goto out;
1269 		break;
1270 	default:
1271 		if ((s64)(change_attr - cinfo->after) >= 0)
1272 			goto out;
1273 	}
1274 
1275 	inode_set_iversion_raw(inode, cinfo->after);
1276 	if (!cinfo->atomic || cinfo->before != change_attr) {
1277 		if (S_ISDIR(inode->i_mode))
1278 			nfs_force_lookup_revalidate(inode);
1279 
1280 		if (!nfs_have_delegated_attributes(inode))
1281 			cache_validity |=
1282 				NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL |
1283 				NFS_INO_INVALID_SIZE | NFS_INO_INVALID_OTHER |
1284 				NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_NLINK |
1285 				NFS_INO_INVALID_MODE | NFS_INO_INVALID_XATTR;
1286 		nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
1287 	}
1288 	nfsi->attrtimeo_timestamp = jiffies;
1289 	nfsi->read_cache_jiffies = timestamp;
1290 	nfsi->attr_gencount = nfs_inc_attr_generation_counter();
1291 	nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
1292 out:
1293 	nfs_set_cache_invalid(inode, cache_validity);
1294 }
1295 
1296 void
nfs4_update_changeattr(struct inode * dir,struct nfs4_change_info * cinfo,unsigned long timestamp,unsigned long cache_validity)1297 nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1298 		unsigned long timestamp, unsigned long cache_validity)
1299 {
1300 	spin_lock(&dir->i_lock);
1301 	nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
1302 	spin_unlock(&dir->i_lock);
1303 }
1304 
1305 struct nfs4_open_createattrs {
1306 	struct nfs4_label *label;
1307 	struct iattr *sattr;
1308 	const __u32 verf[2];
1309 };
1310 
nfs4_clear_cap_atomic_open_v1(struct nfs_server * server,int err,struct nfs4_exception * exception)1311 static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1312 		int err, struct nfs4_exception *exception)
1313 {
1314 	if (err != -EINVAL)
1315 		return false;
1316 	if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1317 		return false;
1318 	server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1319 	exception->retry = 1;
1320 	return true;
1321 }
1322 
_nfs4_ctx_to_accessmode(const struct nfs_open_context * ctx)1323 static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1324 {
1325 	 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1326 }
1327 
_nfs4_ctx_to_openmode(const struct nfs_open_context * ctx)1328 static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1329 {
1330 	fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1331 
1332 	return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1333 }
1334 
1335 static u32
nfs4_fmode_to_share_access(fmode_t fmode)1336 nfs4_fmode_to_share_access(fmode_t fmode)
1337 {
1338 	u32 res = 0;
1339 
1340 	switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1341 	case FMODE_READ:
1342 		res = NFS4_SHARE_ACCESS_READ;
1343 		break;
1344 	case FMODE_WRITE:
1345 		res = NFS4_SHARE_ACCESS_WRITE;
1346 		break;
1347 	case FMODE_READ|FMODE_WRITE:
1348 		res = NFS4_SHARE_ACCESS_BOTH;
1349 	}
1350 	return res;
1351 }
1352 
1353 static u32
nfs4_map_atomic_open_share(struct nfs_server * server,fmode_t fmode,int openflags)1354 nfs4_map_atomic_open_share(struct nfs_server *server,
1355 		fmode_t fmode, int openflags)
1356 {
1357 	u32 res = nfs4_fmode_to_share_access(fmode);
1358 
1359 	if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1360 		goto out;
1361 	/* Want no delegation if we're using O_DIRECT */
1362 	if (openflags & O_DIRECT) {
1363 		res |= NFS4_SHARE_WANT_NO_DELEG;
1364 		goto out;
1365 	}
1366 	/* res |= NFS4_SHARE_WANT_NO_PREFERENCE; */
1367 	if (server->caps & NFS_CAP_DELEGTIME)
1368 		res |= NFS4_SHARE_WANT_DELEG_TIMESTAMPS;
1369 	if (server->caps & NFS_CAP_OPEN_XOR)
1370 		res |= NFS4_SHARE_WANT_OPEN_XOR_DELEGATION;
1371 out:
1372 	return res;
1373 }
1374 
1375 static enum open_claim_type4
nfs4_map_atomic_open_claim(struct nfs_server * server,enum open_claim_type4 claim)1376 nfs4_map_atomic_open_claim(struct nfs_server *server,
1377 		enum open_claim_type4 claim)
1378 {
1379 	if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1380 		return claim;
1381 	switch (claim) {
1382 	default:
1383 		return claim;
1384 	case NFS4_OPEN_CLAIM_FH:
1385 		return NFS4_OPEN_CLAIM_NULL;
1386 	case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1387 		return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1388 	case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1389 		return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1390 	}
1391 }
1392 
nfs4_init_opendata_res(struct nfs4_opendata * p)1393 static void nfs4_init_opendata_res(struct nfs4_opendata *p)
1394 {
1395 	p->o_res.f_attr = &p->f_attr;
1396 	p->o_res.seqid = p->o_arg.seqid;
1397 	p->c_res.seqid = p->c_arg.seqid;
1398 	p->o_res.server = p->o_arg.server;
1399 	p->o_res.access_request = p->o_arg.access;
1400 	nfs_fattr_init(&p->f_attr);
1401 	nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
1402 }
1403 
nfs4_opendata_alloc(struct dentry * dentry,struct nfs4_state_owner * sp,fmode_t fmode,int flags,const struct nfs4_open_createattrs * c,enum open_claim_type4 claim,gfp_t gfp_mask)1404 static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
1405 		struct nfs4_state_owner *sp, fmode_t fmode, int flags,
1406 		const struct nfs4_open_createattrs *c,
1407 		enum open_claim_type4 claim,
1408 		gfp_t gfp_mask)
1409 {
1410 	struct dentry *parent = dget_parent(dentry);
1411 	struct inode *dir = d_inode(parent);
1412 	struct nfs_server *server = NFS_SERVER(dir);
1413 	struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
1414 	struct nfs4_label *label = (c != NULL) ? c->label : NULL;
1415 	struct nfs4_opendata *p;
1416 
1417 	p = kzalloc(sizeof(*p), gfp_mask);
1418 	if (p == NULL)
1419 		goto err;
1420 
1421 	p->f_attr.label = nfs4_label_alloc(server, gfp_mask);
1422 	if (IS_ERR(p->f_attr.label))
1423 		goto err_free_p;
1424 
1425 	p->a_label = nfs4_label_alloc(server, gfp_mask);
1426 	if (IS_ERR(p->a_label))
1427 		goto err_free_f;
1428 
1429 	alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1430 	p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
1431 	if (IS_ERR(p->o_arg.seqid))
1432 		goto err_free_label;
1433 	nfs_sb_active(dentry->d_sb);
1434 	p->dentry = dget(dentry);
1435 	p->dir = parent;
1436 	p->owner = sp;
1437 	atomic_inc(&sp->so_count);
1438 	p->o_arg.open_flags = flags;
1439 	p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
1440 	p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1441 	p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1442 			fmode, flags);
1443 	if (flags & O_CREAT) {
1444 		p->o_arg.umask = current_umask();
1445 		p->o_arg.label = nfs4_label_copy(p->a_label, label);
1446 		if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1447 			p->o_arg.u.attrs = &p->attrs;
1448 			memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1449 
1450 			memcpy(p->o_arg.u.verifier.data, c->verf,
1451 					sizeof(p->o_arg.u.verifier.data));
1452 		}
1453 	}
1454 	/* ask server to check for all possible rights as results
1455 	 * are cached */
1456 	switch (p->o_arg.claim) {
1457 	default:
1458 		break;
1459 	case NFS4_OPEN_CLAIM_NULL:
1460 	case NFS4_OPEN_CLAIM_FH:
1461 		p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1462 				  NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE |
1463 				  NFS4_ACCESS_EXECUTE |
1464 				  nfs_access_xattr_mask(server);
1465 	}
1466 	p->o_arg.clientid = server->nfs_client->cl_clientid;
1467 	p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1468 	p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
1469 	p->o_arg.name = &dentry->d_name;
1470 	p->o_arg.server = server;
1471 	p->o_arg.bitmask = nfs4_bitmask(server, label);
1472 	p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
1473 	switch (p->o_arg.claim) {
1474 	case NFS4_OPEN_CLAIM_NULL:
1475 	case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1476 	case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1477 		p->o_arg.fh = NFS_FH(dir);
1478 		break;
1479 	case NFS4_OPEN_CLAIM_PREVIOUS:
1480 	case NFS4_OPEN_CLAIM_FH:
1481 	case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1482 	case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1483 		p->o_arg.fh = NFS_FH(d_inode(dentry));
1484 	}
1485 	p->c_arg.fh = &p->o_res.fh;
1486 	p->c_arg.stateid = &p->o_res.stateid;
1487 	p->c_arg.seqid = p->o_arg.seqid;
1488 	nfs4_init_opendata_res(p);
1489 	kref_init(&p->kref);
1490 	return p;
1491 
1492 err_free_label:
1493 	nfs4_label_free(p->a_label);
1494 err_free_f:
1495 	nfs4_label_free(p->f_attr.label);
1496 err_free_p:
1497 	kfree(p);
1498 err:
1499 	dput(parent);
1500 	return NULL;
1501 }
1502 
nfs4_opendata_free(struct kref * kref)1503 static void nfs4_opendata_free(struct kref *kref)
1504 {
1505 	struct nfs4_opendata *p = container_of(kref,
1506 			struct nfs4_opendata, kref);
1507 	struct super_block *sb = p->dentry->d_sb;
1508 
1509 	nfs4_lgopen_release(p->lgp);
1510 	nfs_free_seqid(p->o_arg.seqid);
1511 	nfs4_sequence_free_slot(&p->o_res.seq_res);
1512 	if (p->state != NULL)
1513 		nfs4_put_open_state(p->state);
1514 	nfs4_put_state_owner(p->owner);
1515 
1516 	nfs4_label_free(p->a_label);
1517 	nfs4_label_free(p->f_attr.label);
1518 
1519 	dput(p->dir);
1520 	dput(p->dentry);
1521 	nfs_sb_deactive(sb);
1522 	nfs_fattr_free_names(&p->f_attr);
1523 	kfree(p->f_attr.mdsthreshold);
1524 	kfree(p);
1525 }
1526 
nfs4_opendata_put(struct nfs4_opendata * p)1527 static void nfs4_opendata_put(struct nfs4_opendata *p)
1528 {
1529 	if (p != NULL)
1530 		kref_put(&p->kref, nfs4_opendata_free);
1531 }
1532 
nfs4_mode_match_open_stateid(struct nfs4_state * state,fmode_t fmode)1533 static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1534 		fmode_t fmode)
1535 {
1536 	switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1537 	case FMODE_READ|FMODE_WRITE:
1538 		return state->n_rdwr != 0;
1539 	case FMODE_WRITE:
1540 		return state->n_wronly != 0;
1541 	case FMODE_READ:
1542 		return state->n_rdonly != 0;
1543 	}
1544 	WARN_ON_ONCE(1);
1545 	return false;
1546 }
1547 
can_open_cached(struct nfs4_state * state,fmode_t mode,int open_mode,enum open_claim_type4 claim)1548 static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1549 		int open_mode, enum open_claim_type4 claim)
1550 {
1551 	int ret = 0;
1552 
1553 	if (open_mode & (O_EXCL|O_TRUNC))
1554 		goto out;
1555 	switch (claim) {
1556 	case NFS4_OPEN_CLAIM_NULL:
1557 	case NFS4_OPEN_CLAIM_FH:
1558 		goto out;
1559 	default:
1560 		break;
1561 	}
1562 	switch (mode & (FMODE_READ|FMODE_WRITE)) {
1563 		case FMODE_READ:
1564 			ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1565 				&& state->n_rdonly != 0;
1566 			break;
1567 		case FMODE_WRITE:
1568 			ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1569 				&& state->n_wronly != 0;
1570 			break;
1571 		case FMODE_READ|FMODE_WRITE:
1572 			ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1573 				&& state->n_rdwr != 0;
1574 	}
1575 out:
1576 	return ret;
1577 }
1578 
can_open_delegated(struct nfs_delegation * delegation,fmode_t fmode,enum open_claim_type4 claim)1579 static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1580 		enum open_claim_type4 claim)
1581 {
1582 	if (delegation == NULL)
1583 		return 0;
1584 	if ((delegation->type & fmode) != fmode)
1585 		return 0;
1586 	switch (claim) {
1587 	case NFS4_OPEN_CLAIM_NULL:
1588 	case NFS4_OPEN_CLAIM_FH:
1589 		break;
1590 	case NFS4_OPEN_CLAIM_PREVIOUS:
1591 		if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1592 			break;
1593 		fallthrough;
1594 	default:
1595 		return 0;
1596 	}
1597 	nfs_mark_delegation_referenced(delegation);
1598 	return 1;
1599 }
1600 
update_open_stateflags(struct nfs4_state * state,fmode_t fmode)1601 static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
1602 {
1603 	switch (fmode) {
1604 		case FMODE_WRITE:
1605 			state->n_wronly++;
1606 			break;
1607 		case FMODE_READ:
1608 			state->n_rdonly++;
1609 			break;
1610 		case FMODE_READ|FMODE_WRITE:
1611 			state->n_rdwr++;
1612 	}
1613 	nfs4_state_set_mode_locked(state, state->state | fmode);
1614 }
1615 
1616 #ifdef CONFIG_NFS_V4_1
nfs_open_stateid_recover_openmode(struct nfs4_state * state)1617 static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1618 {
1619 	if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1620 		return true;
1621 	if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1622 		return true;
1623 	if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1624 		return true;
1625 	return false;
1626 }
1627 #endif /* CONFIG_NFS_V4_1 */
1628 
nfs_state_log_update_open_stateid(struct nfs4_state * state)1629 static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1630 {
1631 	if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1632 		wake_up_all(&state->waitq);
1633 }
1634 
nfs_test_and_clear_all_open_stateid(struct nfs4_state * state)1635 static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1636 {
1637 	struct nfs_client *clp = state->owner->so_server->nfs_client;
1638 	bool need_recover = false;
1639 
1640 	if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1641 		need_recover = true;
1642 	if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1643 		need_recover = true;
1644 	if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1645 		need_recover = true;
1646 	if (need_recover)
1647 		nfs4_state_mark_reclaim_nograce(clp, state);
1648 }
1649 
1650 /*
1651  * Check for whether or not the caller may update the open stateid
1652  * to the value passed in by stateid.
1653  *
1654  * Note: This function relies heavily on the server implementing
1655  * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1656  * correctly.
1657  * i.e. The stateid seqids have to be initialised to 1, and
1658  * are then incremented on every state transition.
1659  */
nfs_stateid_is_sequential(struct nfs4_state * state,const nfs4_stateid * stateid)1660 static bool nfs_stateid_is_sequential(struct nfs4_state *state,
1661 		const nfs4_stateid *stateid)
1662 {
1663 	if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1664 		/* The common case - we're updating to a new sequence number */
1665 		if (nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1666 			if (nfs4_stateid_is_next(&state->open_stateid, stateid))
1667 				return true;
1668 			return false;
1669 		}
1670 		/* The server returned a new stateid */
1671 	}
1672 	/* This is the first OPEN in this generation */
1673 	if (stateid->seqid == cpu_to_be32(1))
1674 		return true;
1675 	return false;
1676 }
1677 
nfs_resync_open_stateid_locked(struct nfs4_state * state)1678 static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1679 {
1680 	if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1681 		return;
1682 	if (state->n_wronly)
1683 		set_bit(NFS_O_WRONLY_STATE, &state->flags);
1684 	if (state->n_rdonly)
1685 		set_bit(NFS_O_RDONLY_STATE, &state->flags);
1686 	if (state->n_rdwr)
1687 		set_bit(NFS_O_RDWR_STATE, &state->flags);
1688 	set_bit(NFS_OPEN_STATE, &state->flags);
1689 }
1690 
nfs_clear_open_stateid_locked(struct nfs4_state * state,nfs4_stateid * stateid,fmode_t fmode)1691 static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1692 		nfs4_stateid *stateid, fmode_t fmode)
1693 {
1694 	clear_bit(NFS_O_RDWR_STATE, &state->flags);
1695 	switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1696 	case FMODE_WRITE:
1697 		clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1698 		break;
1699 	case FMODE_READ:
1700 		clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1701 		break;
1702 	case 0:
1703 		clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1704 		clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1705 		clear_bit(NFS_OPEN_STATE, &state->flags);
1706 	}
1707 	if (stateid == NULL)
1708 		return;
1709 	/* Handle OPEN+OPEN_DOWNGRADE races */
1710 	if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1711 	    !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1712 		nfs_resync_open_stateid_locked(state);
1713 		goto out;
1714 	}
1715 	if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1716 		nfs4_stateid_copy(&state->stateid, stateid);
1717 	nfs4_stateid_copy(&state->open_stateid, stateid);
1718 	trace_nfs4_open_stateid_update(state->inode, stateid, 0);
1719 out:
1720 	nfs_state_log_update_open_stateid(state);
1721 }
1722 
nfs_clear_open_stateid(struct nfs4_state * state,nfs4_stateid * arg_stateid,nfs4_stateid * stateid,fmode_t fmode)1723 static void nfs_clear_open_stateid(struct nfs4_state *state,
1724 	nfs4_stateid *arg_stateid,
1725 	nfs4_stateid *stateid, fmode_t fmode)
1726 {
1727 	write_seqlock(&state->seqlock);
1728 	/* Ignore, if the CLOSE argment doesn't match the current stateid */
1729 	if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1730 		nfs_clear_open_stateid_locked(state, stateid, fmode);
1731 	write_sequnlock(&state->seqlock);
1732 	if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1733 		nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
1734 }
1735 
nfs_set_open_stateid_locked(struct nfs4_state * state,const nfs4_stateid * stateid,nfs4_stateid * freeme)1736 static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1737 		const nfs4_stateid *stateid, nfs4_stateid *freeme)
1738 	__must_hold(&state->owner->so_lock)
1739 	__must_hold(&state->seqlock)
1740 	__must_hold(RCU)
1741 
1742 {
1743 	DEFINE_WAIT(wait);
1744 	int status = 0;
1745 	for (;;) {
1746 
1747 		if (nfs_stateid_is_sequential(state, stateid))
1748 			break;
1749 
1750 		if (status)
1751 			break;
1752 		/* Rely on seqids for serialisation with NFSv4.0 */
1753 		if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1754 			break;
1755 
1756 		set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1757 		prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1758 		/*
1759 		 * Ensure we process the state changes in the same order
1760 		 * in which the server processed them by delaying the
1761 		 * update of the stateid until we are in sequence.
1762 		 */
1763 		write_sequnlock(&state->seqlock);
1764 		spin_unlock(&state->owner->so_lock);
1765 		rcu_read_unlock();
1766 		trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
1767 
1768 		if (!fatal_signal_pending(current)) {
1769 			if (schedule_timeout(5*HZ) == 0)
1770 				status = -EAGAIN;
1771 			else
1772 				status = 0;
1773 		} else
1774 			status = -EINTR;
1775 		finish_wait(&state->waitq, &wait);
1776 		rcu_read_lock();
1777 		spin_lock(&state->owner->so_lock);
1778 		write_seqlock(&state->seqlock);
1779 	}
1780 
1781 	if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1782 	    !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1783 		nfs4_stateid_copy(freeme, &state->open_stateid);
1784 		nfs_test_and_clear_all_open_stateid(state);
1785 	}
1786 
1787 	if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1788 		nfs4_stateid_copy(&state->stateid, stateid);
1789 	nfs4_stateid_copy(&state->open_stateid, stateid);
1790 	trace_nfs4_open_stateid_update(state->inode, stateid, status);
1791 	nfs_state_log_update_open_stateid(state);
1792 }
1793 
nfs_state_set_open_stateid(struct nfs4_state * state,const nfs4_stateid * open_stateid,fmode_t fmode,nfs4_stateid * freeme)1794 static void nfs_state_set_open_stateid(struct nfs4_state *state,
1795 		const nfs4_stateid *open_stateid,
1796 		fmode_t fmode,
1797 		nfs4_stateid *freeme)
1798 {
1799 	/*
1800 	 * Protect the call to nfs4_state_set_mode_locked and
1801 	 * serialise the stateid update
1802 	 */
1803 	write_seqlock(&state->seqlock);
1804 	nfs_set_open_stateid_locked(state, open_stateid, freeme);
1805 	switch (fmode) {
1806 	case FMODE_READ:
1807 		set_bit(NFS_O_RDONLY_STATE, &state->flags);
1808 		break;
1809 	case FMODE_WRITE:
1810 		set_bit(NFS_O_WRONLY_STATE, &state->flags);
1811 		break;
1812 	case FMODE_READ|FMODE_WRITE:
1813 		set_bit(NFS_O_RDWR_STATE, &state->flags);
1814 	}
1815 	set_bit(NFS_OPEN_STATE, &state->flags);
1816 	write_sequnlock(&state->seqlock);
1817 }
1818 
nfs_state_clear_open_state_flags(struct nfs4_state * state)1819 static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1820 {
1821 	clear_bit(NFS_O_RDWR_STATE, &state->flags);
1822 	clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1823 	clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1824 	clear_bit(NFS_OPEN_STATE, &state->flags);
1825 }
1826 
nfs_state_set_delegation(struct nfs4_state * state,const nfs4_stateid * deleg_stateid,fmode_t fmode)1827 static void nfs_state_set_delegation(struct nfs4_state *state,
1828 		const nfs4_stateid *deleg_stateid,
1829 		fmode_t fmode)
1830 {
1831 	/*
1832 	 * Protect the call to nfs4_state_set_mode_locked and
1833 	 * serialise the stateid update
1834 	 */
1835 	write_seqlock(&state->seqlock);
1836 	nfs4_stateid_copy(&state->stateid, deleg_stateid);
1837 	set_bit(NFS_DELEGATED_STATE, &state->flags);
1838 	write_sequnlock(&state->seqlock);
1839 }
1840 
nfs_state_clear_delegation(struct nfs4_state * state)1841 static void nfs_state_clear_delegation(struct nfs4_state *state)
1842 {
1843 	write_seqlock(&state->seqlock);
1844 	nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1845 	clear_bit(NFS_DELEGATED_STATE, &state->flags);
1846 	write_sequnlock(&state->seqlock);
1847 }
1848 
update_open_stateid(struct nfs4_state * state,const nfs4_stateid * open_stateid,const nfs4_stateid * delegation,fmode_t fmode)1849 int update_open_stateid(struct nfs4_state *state,
1850 		const nfs4_stateid *open_stateid,
1851 		const nfs4_stateid *delegation,
1852 		fmode_t fmode)
1853 {
1854 	struct nfs_server *server = NFS_SERVER(state->inode);
1855 	struct nfs_client *clp = server->nfs_client;
1856 	struct nfs_inode *nfsi = NFS_I(state->inode);
1857 	struct nfs_delegation *deleg_cur;
1858 	nfs4_stateid freeme = { };
1859 	int ret = 0;
1860 
1861 	fmode &= (FMODE_READ|FMODE_WRITE);
1862 
1863 	rcu_read_lock();
1864 	spin_lock(&state->owner->so_lock);
1865 	if (open_stateid != NULL) {
1866 		nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1867 		ret = 1;
1868 	}
1869 
1870 	deleg_cur = nfs4_get_valid_delegation(state->inode);
1871 	if (deleg_cur == NULL)
1872 		goto no_delegation;
1873 
1874 	spin_lock(&deleg_cur->lock);
1875 	if (rcu_dereference(nfsi->delegation) != deleg_cur ||
1876 	   test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
1877 	    (deleg_cur->type & fmode) != fmode)
1878 		goto no_delegation_unlock;
1879 
1880 	if (delegation == NULL)
1881 		delegation = &deleg_cur->stateid;
1882 	else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
1883 		goto no_delegation_unlock;
1884 
1885 	nfs_mark_delegation_referenced(deleg_cur);
1886 	nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
1887 	ret = 1;
1888 no_delegation_unlock:
1889 	spin_unlock(&deleg_cur->lock);
1890 no_delegation:
1891 	if (ret)
1892 		update_open_stateflags(state, fmode);
1893 	spin_unlock(&state->owner->so_lock);
1894 	rcu_read_unlock();
1895 
1896 	if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1897 		nfs4_schedule_state_manager(clp);
1898 	if (freeme.type != 0)
1899 		nfs4_test_and_free_stateid(server, &freeme,
1900 				state->owner->so_cred);
1901 
1902 	return ret;
1903 }
1904 
nfs4_update_lock_stateid(struct nfs4_lock_state * lsp,const nfs4_stateid * stateid)1905 static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1906 		const nfs4_stateid *stateid)
1907 {
1908 	struct nfs4_state *state = lsp->ls_state;
1909 	bool ret = false;
1910 
1911 	spin_lock(&state->state_lock);
1912 	if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1913 		goto out_noupdate;
1914 	if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1915 		goto out_noupdate;
1916 	nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1917 	ret = true;
1918 out_noupdate:
1919 	spin_unlock(&state->state_lock);
1920 	return ret;
1921 }
1922 
nfs4_return_incompatible_delegation(struct inode * inode,fmode_t fmode)1923 static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
1924 {
1925 	struct nfs_delegation *delegation;
1926 
1927 	fmode &= FMODE_READ|FMODE_WRITE;
1928 	rcu_read_lock();
1929 	delegation = nfs4_get_valid_delegation(inode);
1930 	if (delegation == NULL || (delegation->type & fmode) == fmode) {
1931 		rcu_read_unlock();
1932 		return;
1933 	}
1934 	rcu_read_unlock();
1935 	nfs4_inode_return_delegation(inode);
1936 }
1937 
nfs4_try_open_cached(struct nfs4_opendata * opendata)1938 static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
1939 {
1940 	struct nfs4_state *state = opendata->state;
1941 	struct nfs_delegation *delegation;
1942 	int open_mode = opendata->o_arg.open_flags;
1943 	fmode_t fmode = opendata->o_arg.fmode;
1944 	enum open_claim_type4 claim = opendata->o_arg.claim;
1945 	nfs4_stateid stateid;
1946 	int ret = -EAGAIN;
1947 
1948 	for (;;) {
1949 		spin_lock(&state->owner->so_lock);
1950 		if (can_open_cached(state, fmode, open_mode, claim)) {
1951 			update_open_stateflags(state, fmode);
1952 			spin_unlock(&state->owner->so_lock);
1953 			goto out_return_state;
1954 		}
1955 		spin_unlock(&state->owner->so_lock);
1956 		rcu_read_lock();
1957 		delegation = nfs4_get_valid_delegation(state->inode);
1958 		if (!can_open_delegated(delegation, fmode, claim)) {
1959 			rcu_read_unlock();
1960 			break;
1961 		}
1962 		/* Save the delegation */
1963 		nfs4_stateid_copy(&stateid, &delegation->stateid);
1964 		rcu_read_unlock();
1965 		nfs_release_seqid(opendata->o_arg.seqid);
1966 		if (!opendata->is_recover) {
1967 			ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1968 			if (ret != 0)
1969 				goto out;
1970 		}
1971 		ret = -EAGAIN;
1972 
1973 		/* Try to update the stateid using the delegation */
1974 		if (update_open_stateid(state, NULL, &stateid, fmode))
1975 			goto out_return_state;
1976 	}
1977 out:
1978 	return ERR_PTR(ret);
1979 out_return_state:
1980 	refcount_inc(&state->count);
1981 	return state;
1982 }
1983 
1984 static void
nfs4_process_delegation(struct inode * inode,const struct cred * cred,enum open_claim_type4 claim,const struct nfs4_open_delegation * delegation)1985 nfs4_process_delegation(struct inode *inode, const struct cred *cred,
1986 			enum open_claim_type4 claim,
1987 			const struct nfs4_open_delegation *delegation)
1988 {
1989 	switch (delegation->open_delegation_type) {
1990 	case NFS4_OPEN_DELEGATE_READ:
1991 	case NFS4_OPEN_DELEGATE_WRITE:
1992 	case NFS4_OPEN_DELEGATE_READ_ATTRS_DELEG:
1993 	case NFS4_OPEN_DELEGATE_WRITE_ATTRS_DELEG:
1994 		break;
1995 	default:
1996 		return;
1997 	}
1998 	switch (claim) {
1999 	case NFS4_OPEN_CLAIM_DELEGATE_CUR:
2000 	case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2001 		pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
2002 				   "returning a delegation for "
2003 				   "OPEN(CLAIM_DELEGATE_CUR)\n",
2004 				   NFS_SERVER(inode)->nfs_client->cl_hostname);
2005 		break;
2006 	case NFS4_OPEN_CLAIM_PREVIOUS:
2007 		nfs_inode_reclaim_delegation(inode, cred, delegation->type,
2008 					     &delegation->stateid,
2009 					     delegation->pagemod_limit,
2010 					     delegation->open_delegation_type);
2011 		break;
2012 	default:
2013 		nfs_inode_set_delegation(inode, cred, delegation->type,
2014 					 &delegation->stateid,
2015 					 delegation->pagemod_limit,
2016 					 delegation->open_delegation_type);
2017 	}
2018 	if (delegation->do_recall)
2019 		nfs_async_inode_return_delegation(inode, &delegation->stateid);
2020 }
2021 
2022 /*
2023  * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
2024  * and update the nfs4_state.
2025  */
2026 static struct nfs4_state *
_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata * data)2027 _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
2028 {
2029 	struct inode *inode = data->state->inode;
2030 	struct nfs4_state *state = data->state;
2031 	int ret;
2032 
2033 	if (!data->rpc_done) {
2034 		if (data->rpc_status)
2035 			return ERR_PTR(data->rpc_status);
2036 		return nfs4_try_open_cached(data);
2037 	}
2038 
2039 	ret = nfs_refresh_inode(inode, &data->f_attr);
2040 	if (ret)
2041 		return ERR_PTR(ret);
2042 
2043 	nfs4_process_delegation(state->inode,
2044 				data->owner->so_cred,
2045 				data->o_arg.claim,
2046 				&data->o_res.delegation);
2047 
2048 	if (!(data->o_res.rflags & NFS4_OPEN_RESULT_NO_OPEN_STATEID)) {
2049 		if (!update_open_stateid(state, &data->o_res.stateid,
2050 					 NULL, data->o_arg.fmode))
2051 			return ERR_PTR(-EAGAIN);
2052 	} else if (!update_open_stateid(state, NULL, NULL, data->o_arg.fmode))
2053 		return ERR_PTR(-EAGAIN);
2054 	refcount_inc(&state->count);
2055 
2056 	return state;
2057 }
2058 
2059 static struct inode *
nfs4_opendata_get_inode(struct nfs4_opendata * data)2060 nfs4_opendata_get_inode(struct nfs4_opendata *data)
2061 {
2062 	struct inode *inode;
2063 
2064 	switch (data->o_arg.claim) {
2065 	case NFS4_OPEN_CLAIM_NULL:
2066 	case NFS4_OPEN_CLAIM_DELEGATE_CUR:
2067 	case NFS4_OPEN_CLAIM_DELEGATE_PREV:
2068 		if (!(data->f_attr.valid & NFS_ATTR_FATTR))
2069 			return ERR_PTR(-EAGAIN);
2070 		inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
2071 				&data->f_attr);
2072 		break;
2073 	default:
2074 		inode = d_inode(data->dentry);
2075 		ihold(inode);
2076 		nfs_refresh_inode(inode, &data->f_attr);
2077 	}
2078 	return inode;
2079 }
2080 
2081 static struct nfs4_state *
nfs4_opendata_find_nfs4_state(struct nfs4_opendata * data)2082 nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2083 {
2084 	struct nfs4_state *state;
2085 	struct inode *inode;
2086 
2087 	inode = nfs4_opendata_get_inode(data);
2088 	if (IS_ERR(inode))
2089 		return ERR_CAST(inode);
2090 	if (data->state != NULL && data->state->inode == inode) {
2091 		state = data->state;
2092 		refcount_inc(&state->count);
2093 	} else
2094 		state = nfs4_get_open_state(inode, data->owner);
2095 	iput(inode);
2096 	if (state == NULL)
2097 		state = ERR_PTR(-ENOMEM);
2098 	return state;
2099 }
2100 
2101 static struct nfs4_state *
_nfs4_opendata_to_nfs4_state(struct nfs4_opendata * data)2102 _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2103 {
2104 	struct nfs4_state *state;
2105 
2106 	if (!data->rpc_done) {
2107 		state = nfs4_try_open_cached(data);
2108 		trace_nfs4_cached_open(data->state);
2109 		goto out;
2110 	}
2111 
2112 	state = nfs4_opendata_find_nfs4_state(data);
2113 	if (IS_ERR(state))
2114 		goto out;
2115 
2116 	nfs4_process_delegation(state->inode,
2117 				data->owner->so_cred,
2118 				data->o_arg.claim,
2119 				&data->o_res.delegation);
2120 
2121 	if (!(data->o_res.rflags & NFS4_OPEN_RESULT_NO_OPEN_STATEID)) {
2122 		if (!update_open_stateid(state, &data->o_res.stateid,
2123 					 NULL, data->o_arg.fmode)) {
2124 			nfs4_put_open_state(state);
2125 			state = ERR_PTR(-EAGAIN);
2126 		}
2127 	} else if (!update_open_stateid(state, NULL, NULL, data->o_arg.fmode)) {
2128 		nfs4_put_open_state(state);
2129 		state = ERR_PTR(-EAGAIN);
2130 	}
2131 out:
2132 	nfs_release_seqid(data->o_arg.seqid);
2133 	return state;
2134 }
2135 
2136 static struct nfs4_state *
nfs4_opendata_to_nfs4_state(struct nfs4_opendata * data)2137 nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2138 {
2139 	struct nfs4_state *ret;
2140 
2141 	if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
2142 		ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2143 	else
2144 		ret = _nfs4_opendata_to_nfs4_state(data);
2145 	nfs4_sequence_free_slot(&data->o_res.seq_res);
2146 	return ret;
2147 }
2148 
2149 static struct nfs_open_context *
nfs4_state_find_open_context_mode(struct nfs4_state * state,fmode_t mode)2150 nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
2151 {
2152 	struct nfs_inode *nfsi = NFS_I(state->inode);
2153 	struct nfs_open_context *ctx;
2154 
2155 	rcu_read_lock();
2156 	list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
2157 		if (ctx->state != state)
2158 			continue;
2159 		if ((ctx->mode & mode) != mode)
2160 			continue;
2161 		if (!get_nfs_open_context(ctx))
2162 			continue;
2163 		rcu_read_unlock();
2164 		return ctx;
2165 	}
2166 	rcu_read_unlock();
2167 	return ERR_PTR(-ENOENT);
2168 }
2169 
2170 static struct nfs_open_context *
nfs4_state_find_open_context(struct nfs4_state * state)2171 nfs4_state_find_open_context(struct nfs4_state *state)
2172 {
2173 	struct nfs_open_context *ctx;
2174 
2175 	ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2176 	if (!IS_ERR(ctx))
2177 		return ctx;
2178 	ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2179 	if (!IS_ERR(ctx))
2180 		return ctx;
2181 	return nfs4_state_find_open_context_mode(state, FMODE_READ);
2182 }
2183 
nfs4_open_recoverdata_alloc(struct nfs_open_context * ctx,struct nfs4_state * state,enum open_claim_type4 claim)2184 static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2185 		struct nfs4_state *state, enum open_claim_type4 claim)
2186 {
2187 	struct nfs4_opendata *opendata;
2188 
2189 	opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
2190 			NULL, claim, GFP_NOFS);
2191 	if (opendata == NULL)
2192 		return ERR_PTR(-ENOMEM);
2193 	opendata->state = state;
2194 	refcount_inc(&state->count);
2195 	return opendata;
2196 }
2197 
nfs4_open_recover_helper(struct nfs4_opendata * opendata,fmode_t fmode)2198 static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2199 				    fmode_t fmode)
2200 {
2201 	struct nfs4_state *newstate;
2202 	struct nfs_server *server = NFS_SB(opendata->dentry->d_sb);
2203 	int openflags = opendata->o_arg.open_flags;
2204 	int ret;
2205 
2206 	if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
2207 		return 0;
2208 	opendata->o_arg.fmode = fmode;
2209 	opendata->o_arg.share_access =
2210 		nfs4_map_atomic_open_share(server, fmode, openflags);
2211 	memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2212 	memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2213 	nfs4_init_opendata_res(opendata);
2214 	ret = _nfs4_recover_proc_open(opendata);
2215 	if (ret != 0)
2216 		return ret;
2217 	newstate = nfs4_opendata_to_nfs4_state(opendata);
2218 	if (IS_ERR(newstate))
2219 		return PTR_ERR(newstate);
2220 	if (newstate != opendata->state)
2221 		ret = -ESTALE;
2222 	nfs4_close_state(newstate, fmode);
2223 	return ret;
2224 }
2225 
nfs4_open_recover(struct nfs4_opendata * opendata,struct nfs4_state * state)2226 static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2227 {
2228 	int ret;
2229 
2230 	/* memory barrier prior to reading state->n_* */
2231 	smp_rmb();
2232 	ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2233 	if (ret != 0)
2234 		return ret;
2235 	ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2236 	if (ret != 0)
2237 		return ret;
2238 	ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2239 	if (ret != 0)
2240 		return ret;
2241 	/*
2242 	 * We may have performed cached opens for all three recoveries.
2243 	 * Check if we need to update the current stateid.
2244 	 */
2245 	if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
2246 	    !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
2247 		write_seqlock(&state->seqlock);
2248 		if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2249 			nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2250 		write_sequnlock(&state->seqlock);
2251 	}
2252 	return 0;
2253 }
2254 
2255 /*
2256  * OPEN_RECLAIM:
2257  * 	reclaim state on the server after a reboot.
2258  */
_nfs4_do_open_reclaim(struct nfs_open_context * ctx,struct nfs4_state * state)2259 static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2260 {
2261 	struct nfs_delegation *delegation;
2262 	struct nfs4_opendata *opendata;
2263 	u32 delegation_type = NFS4_OPEN_DELEGATE_NONE;
2264 	int status;
2265 
2266 	opendata = nfs4_open_recoverdata_alloc(ctx, state,
2267 			NFS4_OPEN_CLAIM_PREVIOUS);
2268 	if (IS_ERR(opendata))
2269 		return PTR_ERR(opendata);
2270 	rcu_read_lock();
2271 	delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2272 	if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0) {
2273 		switch(delegation->type) {
2274 		case FMODE_READ:
2275 			delegation_type = NFS4_OPEN_DELEGATE_READ;
2276 			if (test_bit(NFS_DELEGATION_DELEGTIME, &delegation->flags))
2277 				delegation_type = NFS4_OPEN_DELEGATE_READ_ATTRS_DELEG;
2278 			break;
2279 		case FMODE_WRITE:
2280 		case FMODE_READ|FMODE_WRITE:
2281 			delegation_type = NFS4_OPEN_DELEGATE_WRITE;
2282 			if (test_bit(NFS_DELEGATION_DELEGTIME, &delegation->flags))
2283 				delegation_type = NFS4_OPEN_DELEGATE_WRITE_ATTRS_DELEG;
2284 		}
2285 	}
2286 	rcu_read_unlock();
2287 	opendata->o_arg.u.delegation_type = delegation_type;
2288 	status = nfs4_open_recover(opendata, state);
2289 	nfs4_opendata_put(opendata);
2290 	return status;
2291 }
2292 
nfs4_do_open_reclaim(struct nfs_open_context * ctx,struct nfs4_state * state)2293 static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
2294 {
2295 	struct nfs_server *server = NFS_SERVER(state->inode);
2296 	struct nfs4_exception exception = { };
2297 	int err;
2298 	do {
2299 		err = _nfs4_do_open_reclaim(ctx, state);
2300 		trace_nfs4_open_reclaim(ctx, 0, err);
2301 		if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2302 			continue;
2303 		if (err != -NFS4ERR_DELAY)
2304 			break;
2305 		nfs4_handle_exception(server, err, &exception);
2306 	} while (exception.retry);
2307 	return err;
2308 }
2309 
nfs4_open_reclaim(struct nfs4_state_owner * sp,struct nfs4_state * state)2310 static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2311 {
2312 	struct nfs_open_context *ctx;
2313 	int ret;
2314 
2315 	ctx = nfs4_state_find_open_context(state);
2316 	if (IS_ERR(ctx))
2317 		return -EAGAIN;
2318 	clear_bit(NFS_DELEGATED_STATE, &state->flags);
2319 	nfs_state_clear_open_state_flags(state);
2320 	ret = nfs4_do_open_reclaim(ctx, state);
2321 	put_nfs_open_context(ctx);
2322 	return ret;
2323 }
2324 
nfs4_handle_delegation_recall_error(struct nfs_server * server,struct nfs4_state * state,const nfs4_stateid * stateid,struct file_lock * fl,int err)2325 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)
2326 {
2327 	switch (err) {
2328 		default:
2329 			printk(KERN_ERR "NFS: %s: unhandled error "
2330 					"%d.\n", __func__, err);
2331 			fallthrough;
2332 		case 0:
2333 		case -ENOENT:
2334 		case -EAGAIN:
2335 		case -ESTALE:
2336 		case -ETIMEDOUT:
2337 			break;
2338 		case -NFS4ERR_BADSESSION:
2339 		case -NFS4ERR_BADSLOT:
2340 		case -NFS4ERR_BAD_HIGH_SLOT:
2341 		case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2342 		case -NFS4ERR_DEADSESSION:
2343 			return -EAGAIN;
2344 		case -NFS4ERR_STALE_CLIENTID:
2345 		case -NFS4ERR_STALE_STATEID:
2346 			/* Don't recall a delegation if it was lost */
2347 			nfs4_schedule_lease_recovery(server->nfs_client);
2348 			return -EAGAIN;
2349 		case -NFS4ERR_MOVED:
2350 			nfs4_schedule_migration_recovery(server);
2351 			return -EAGAIN;
2352 		case -NFS4ERR_LEASE_MOVED:
2353 			nfs4_schedule_lease_moved_recovery(server->nfs_client);
2354 			return -EAGAIN;
2355 		case -NFS4ERR_DELEG_REVOKED:
2356 		case -NFS4ERR_ADMIN_REVOKED:
2357 		case -NFS4ERR_EXPIRED:
2358 		case -NFS4ERR_BAD_STATEID:
2359 		case -NFS4ERR_OPENMODE:
2360 			nfs_inode_find_state_and_recover(state->inode,
2361 					stateid);
2362 			nfs4_schedule_stateid_recovery(server, state);
2363 			return -EAGAIN;
2364 		case -NFS4ERR_DELAY:
2365 		case -NFS4ERR_GRACE:
2366 			ssleep(1);
2367 			return -EAGAIN;
2368 		case -ENOMEM:
2369 		case -NFS4ERR_DENIED:
2370 			if (fl) {
2371 				struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2372 				if (lsp)
2373 					set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2374 			}
2375 			return 0;
2376 	}
2377 	return err;
2378 }
2379 
nfs4_open_delegation_recall(struct nfs_open_context * ctx,struct nfs4_state * state,const nfs4_stateid * stateid)2380 int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2381 		struct nfs4_state *state, const nfs4_stateid *stateid)
2382 {
2383 	struct nfs_server *server = NFS_SERVER(state->inode);
2384 	struct nfs4_opendata *opendata;
2385 	int err = 0;
2386 
2387 	opendata = nfs4_open_recoverdata_alloc(ctx, state,
2388 			NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2389 	if (IS_ERR(opendata))
2390 		return PTR_ERR(opendata);
2391 	nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
2392 	if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
2393 		err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2394 		if (err)
2395 			goto out;
2396 	}
2397 	if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
2398 		err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2399 		if (err)
2400 			goto out;
2401 	}
2402 	if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2403 		err = nfs4_open_recover_helper(opendata, FMODE_READ);
2404 		if (err)
2405 			goto out;
2406 	}
2407 	nfs_state_clear_delegation(state);
2408 out:
2409 	nfs4_opendata_put(opendata);
2410 	return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
2411 }
2412 
nfs4_open_confirm_prepare(struct rpc_task * task,void * calldata)2413 static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2414 {
2415 	struct nfs4_opendata *data = calldata;
2416 
2417 	nfs4_setup_sequence(data->o_arg.server->nfs_client,
2418 			   &data->c_arg.seq_args, &data->c_res.seq_res, task);
2419 }
2420 
nfs4_open_confirm_done(struct rpc_task * task,void * calldata)2421 static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2422 {
2423 	struct nfs4_opendata *data = calldata;
2424 
2425 	nfs40_sequence_done(task, &data->c_res.seq_res);
2426 
2427 	data->rpc_status = task->tk_status;
2428 	if (data->rpc_status == 0) {
2429 		nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
2430 		nfs_confirm_seqid(&data->owner->so_seqid, 0);
2431 		renew_lease(data->o_res.server, data->timestamp);
2432 		data->rpc_done = true;
2433 	}
2434 }
2435 
nfs4_open_confirm_release(void * calldata)2436 static void nfs4_open_confirm_release(void *calldata)
2437 {
2438 	struct nfs4_opendata *data = calldata;
2439 	struct nfs4_state *state = NULL;
2440 
2441 	/* If this request hasn't been cancelled, do nothing */
2442 	if (!data->cancelled)
2443 		goto out_free;
2444 	/* In case of error, no cleanup! */
2445 	if (!data->rpc_done)
2446 		goto out_free;
2447 	state = nfs4_opendata_to_nfs4_state(data);
2448 	if (!IS_ERR(state))
2449 		nfs4_close_state(state, data->o_arg.fmode);
2450 out_free:
2451 	nfs4_opendata_put(data);
2452 }
2453 
2454 static const struct rpc_call_ops nfs4_open_confirm_ops = {
2455 	.rpc_call_prepare = nfs4_open_confirm_prepare,
2456 	.rpc_call_done = nfs4_open_confirm_done,
2457 	.rpc_release = nfs4_open_confirm_release,
2458 };
2459 
2460 /*
2461  * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2462  */
_nfs4_proc_open_confirm(struct nfs4_opendata * data)2463 static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2464 {
2465 	struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
2466 	struct rpc_task *task;
2467 	struct  rpc_message msg = {
2468 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2469 		.rpc_argp = &data->c_arg,
2470 		.rpc_resp = &data->c_res,
2471 		.rpc_cred = data->owner->so_cred,
2472 	};
2473 	struct rpc_task_setup task_setup_data = {
2474 		.rpc_client = server->client,
2475 		.rpc_message = &msg,
2476 		.callback_ops = &nfs4_open_confirm_ops,
2477 		.callback_data = data,
2478 		.workqueue = nfsiod_workqueue,
2479 		.flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2480 	};
2481 	int status;
2482 
2483 	nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2484 				data->is_recover);
2485 	kref_get(&data->kref);
2486 	data->rpc_done = false;
2487 	data->rpc_status = 0;
2488 	data->timestamp = jiffies;
2489 	task = rpc_run_task(&task_setup_data);
2490 	if (IS_ERR(task))
2491 		return PTR_ERR(task);
2492 	status = rpc_wait_for_completion_task(task);
2493 	if (status != 0) {
2494 		data->cancelled = true;
2495 		smp_wmb();
2496 	} else
2497 		status = data->rpc_status;
2498 	rpc_put_task(task);
2499 	return status;
2500 }
2501 
nfs4_open_prepare(struct rpc_task * task,void * calldata)2502 static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
2503 {
2504 	struct nfs4_opendata *data = calldata;
2505 	struct nfs4_state_owner *sp = data->owner;
2506 	struct nfs_client *clp = sp->so_server->nfs_client;
2507 	enum open_claim_type4 claim = data->o_arg.claim;
2508 
2509 	if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
2510 		goto out_wait;
2511 	/*
2512 	 * Check if we still need to send an OPEN call, or if we can use
2513 	 * a delegation instead.
2514 	 */
2515 	if (data->state != NULL) {
2516 		struct nfs_delegation *delegation;
2517 
2518 		if (can_open_cached(data->state, data->o_arg.fmode,
2519 					data->o_arg.open_flags, claim))
2520 			goto out_no_action;
2521 		rcu_read_lock();
2522 		delegation = nfs4_get_valid_delegation(data->state->inode);
2523 		if (can_open_delegated(delegation, data->o_arg.fmode, claim))
2524 			goto unlock_no_action;
2525 		rcu_read_unlock();
2526 	}
2527 	/* Update client id. */
2528 	data->o_arg.clientid = clp->cl_clientid;
2529 	switch (claim) {
2530 	default:
2531 		break;
2532 	case NFS4_OPEN_CLAIM_PREVIOUS:
2533 	case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2534 	case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
2535 		data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
2536 		fallthrough;
2537 	case NFS4_OPEN_CLAIM_FH:
2538 		task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
2539 	}
2540 	data->timestamp = jiffies;
2541 	if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
2542 				&data->o_arg.seq_args,
2543 				&data->o_res.seq_res,
2544 				task) != 0)
2545 		nfs_release_seqid(data->o_arg.seqid);
2546 
2547 	/* Set the create mode (note dependency on the session type) */
2548 	data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2549 	if (data->o_arg.open_flags & O_EXCL) {
2550 		data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2551 		if (clp->cl_mvops->minor_version == 0) {
2552 			data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2553 			/* don't put an ACCESS op in OPEN compound if O_EXCL,
2554 			 * because ACCESS will return permission denied for
2555 			 * all bits until close */
2556 			data->o_res.access_request = data->o_arg.access = 0;
2557 		} else if (nfs4_has_persistent_session(clp))
2558 			data->o_arg.createmode = NFS4_CREATE_GUARDED;
2559 	}
2560 	return;
2561 unlock_no_action:
2562 	trace_nfs4_cached_open(data->state);
2563 	rcu_read_unlock();
2564 out_no_action:
2565 	task->tk_action = NULL;
2566 out_wait:
2567 	nfs4_sequence_done(task, &data->o_res.seq_res);
2568 }
2569 
nfs4_open_done(struct rpc_task * task,void * calldata)2570 static void nfs4_open_done(struct rpc_task *task, void *calldata)
2571 {
2572 	struct nfs4_opendata *data = calldata;
2573 
2574 	data->rpc_status = task->tk_status;
2575 
2576 	if (!nfs4_sequence_process(task, &data->o_res.seq_res))
2577 		return;
2578 
2579 	if (task->tk_status == 0) {
2580 		if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2581 			switch (data->o_res.f_attr->mode & S_IFMT) {
2582 			case S_IFREG:
2583 				break;
2584 			case S_IFLNK:
2585 				data->rpc_status = -ELOOP;
2586 				break;
2587 			case S_IFDIR:
2588 				data->rpc_status = -EISDIR;
2589 				break;
2590 			default:
2591 				data->rpc_status = -ENOTDIR;
2592 			}
2593 		}
2594 		renew_lease(data->o_res.server, data->timestamp);
2595 		if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2596 			nfs_confirm_seqid(&data->owner->so_seqid, 0);
2597 	}
2598 	data->rpc_done = true;
2599 }
2600 
nfs4_open_release(void * calldata)2601 static void nfs4_open_release(void *calldata)
2602 {
2603 	struct nfs4_opendata *data = calldata;
2604 	struct nfs4_state *state = NULL;
2605 
2606 	/* If this request hasn't been cancelled, do nothing */
2607 	if (!data->cancelled)
2608 		goto out_free;
2609 	/* In case of error, no cleanup! */
2610 	if (data->rpc_status != 0 || !data->rpc_done)
2611 		goto out_free;
2612 	/* In case we need an open_confirm, no cleanup! */
2613 	if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2614 		goto out_free;
2615 	state = nfs4_opendata_to_nfs4_state(data);
2616 	if (!IS_ERR(state))
2617 		nfs4_close_state(state, data->o_arg.fmode);
2618 out_free:
2619 	nfs4_opendata_put(data);
2620 }
2621 
2622 static const struct rpc_call_ops nfs4_open_ops = {
2623 	.rpc_call_prepare = nfs4_open_prepare,
2624 	.rpc_call_done = nfs4_open_done,
2625 	.rpc_release = nfs4_open_release,
2626 };
2627 
nfs4_run_open_task(struct nfs4_opendata * data,struct nfs_open_context * ctx)2628 static int nfs4_run_open_task(struct nfs4_opendata *data,
2629 			      struct nfs_open_context *ctx)
2630 {
2631 	struct inode *dir = d_inode(data->dir);
2632 	struct nfs_server *server = NFS_SERVER(dir);
2633 	struct nfs_openargs *o_arg = &data->o_arg;
2634 	struct nfs_openres *o_res = &data->o_res;
2635 	struct rpc_task *task;
2636 	struct rpc_message msg = {
2637 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2638 		.rpc_argp = o_arg,
2639 		.rpc_resp = o_res,
2640 		.rpc_cred = data->owner->so_cred,
2641 	};
2642 	struct rpc_task_setup task_setup_data = {
2643 		.rpc_client = server->client,
2644 		.rpc_message = &msg,
2645 		.callback_ops = &nfs4_open_ops,
2646 		.callback_data = data,
2647 		.workqueue = nfsiod_workqueue,
2648 		.flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
2649 	};
2650 	int status;
2651 
2652 	if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
2653 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
2654 
2655 	kref_get(&data->kref);
2656 	data->rpc_done = false;
2657 	data->rpc_status = 0;
2658 	data->cancelled = false;
2659 	data->is_recover = false;
2660 	if (!ctx) {
2661 		nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
2662 		data->is_recover = true;
2663 		task_setup_data.flags |= RPC_TASK_TIMEOUT;
2664 	} else {
2665 		nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
2666 		pnfs_lgopen_prepare(data, ctx);
2667 	}
2668 	task = rpc_run_task(&task_setup_data);
2669 	if (IS_ERR(task))
2670 		return PTR_ERR(task);
2671 	status = rpc_wait_for_completion_task(task);
2672 	if (status != 0) {
2673 		data->cancelled = true;
2674 		smp_wmb();
2675 	} else
2676 		status = data->rpc_status;
2677 	rpc_put_task(task);
2678 
2679 	return status;
2680 }
2681 
_nfs4_recover_proc_open(struct nfs4_opendata * data)2682 static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2683 {
2684 	struct inode *dir = d_inode(data->dir);
2685 	struct nfs_openres *o_res = &data->o_res;
2686 	int status;
2687 
2688 	status = nfs4_run_open_task(data, NULL);
2689 	if (status != 0 || !data->rpc_done)
2690 		return status;
2691 
2692 	nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2693 
2694 	if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
2695 		status = _nfs4_proc_open_confirm(data);
2696 
2697 	return status;
2698 }
2699 
2700 /*
2701  * Additional permission checks in order to distinguish between an
2702  * open for read, and an open for execute. This works around the
2703  * fact that NFSv4 OPEN treats read and execute permissions as being
2704  * the same.
2705  * Note that in the non-execute case, we want to turn off permission
2706  * checking if we just created a new file (POSIX open() semantics).
2707  */
nfs4_opendata_access(const struct cred * cred,struct nfs4_opendata * opendata,struct nfs4_state * state,fmode_t fmode)2708 static int nfs4_opendata_access(const struct cred *cred,
2709 				struct nfs4_opendata *opendata,
2710 				struct nfs4_state *state, fmode_t fmode)
2711 {
2712 	struct nfs_access_entry cache;
2713 	u32 mask, flags;
2714 
2715 	/* access call failed or for some reason the server doesn't
2716 	 * support any access modes -- defer access call until later */
2717 	if (opendata->o_res.access_supported == 0)
2718 		return 0;
2719 
2720 	mask = 0;
2721 	if (fmode & FMODE_EXEC) {
2722 		/* ONLY check for exec rights */
2723 		if (S_ISDIR(state->inode->i_mode))
2724 			mask = NFS4_ACCESS_LOOKUP;
2725 		else
2726 			mask = NFS4_ACCESS_EXECUTE;
2727 	} else if ((fmode & FMODE_READ) && !opendata->file_created)
2728 		mask = NFS4_ACCESS_READ;
2729 
2730 	nfs_access_set_mask(&cache, opendata->o_res.access_result);
2731 	nfs_access_add_cache(state->inode, &cache, cred);
2732 
2733 	flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2734 	if ((mask & ~cache.mask & flags) == 0)
2735 		return 0;
2736 
2737 	return -EACCES;
2738 }
2739 
2740 /*
2741  * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2742  */
_nfs4_proc_open(struct nfs4_opendata * data,struct nfs_open_context * ctx)2743 static int _nfs4_proc_open(struct nfs4_opendata *data,
2744 			   struct nfs_open_context *ctx)
2745 {
2746 	struct inode *dir = d_inode(data->dir);
2747 	struct nfs_server *server = NFS_SERVER(dir);
2748 	struct nfs_openargs *o_arg = &data->o_arg;
2749 	struct nfs_openres *o_res = &data->o_res;
2750 	int status;
2751 
2752 	status = nfs4_run_open_task(data, ctx);
2753 	if (!data->rpc_done)
2754 		return status;
2755 	if (status != 0) {
2756 		if (status == -NFS4ERR_BADNAME &&
2757 				!(o_arg->open_flags & O_CREAT))
2758 			return -ENOENT;
2759 		return status;
2760 	}
2761 
2762 	nfs_fattr_map_and_free_names(server, &data->f_attr);
2763 
2764 	if (o_arg->open_flags & O_CREAT) {
2765 		if (o_arg->open_flags & O_EXCL)
2766 			data->file_created = true;
2767 		else if (o_res->cinfo.before != o_res->cinfo.after)
2768 			data->file_created = true;
2769 		if (data->file_created ||
2770 		    inode_peek_iversion_raw(dir) != o_res->cinfo.after)
2771 			nfs4_update_changeattr(dir, &o_res->cinfo,
2772 					o_res->f_attr->time_start,
2773 					NFS_INO_INVALID_DATA);
2774 	}
2775 	if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2776 		server->caps &= ~NFS_CAP_POSIX_LOCK;
2777 	if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2778 		status = _nfs4_proc_open_confirm(data);
2779 		if (status != 0)
2780 			return status;
2781 	}
2782 	if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2783 		struct nfs_fh *fh = &o_res->fh;
2784 
2785 		nfs4_sequence_free_slot(&o_res->seq_res);
2786 		if (o_arg->claim == NFS4_OPEN_CLAIM_FH)
2787 			fh = NFS_FH(d_inode(data->dentry));
2788 		nfs4_proc_getattr(server, fh, o_res->f_attr, NULL);
2789 	}
2790 	return 0;
2791 }
2792 
2793 /*
2794  * OPEN_EXPIRED:
2795  * 	reclaim state on the server after a network partition.
2796  * 	Assumes caller holds the appropriate lock
2797  */
_nfs4_open_expired(struct nfs_open_context * ctx,struct nfs4_state * state)2798 static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2799 {
2800 	struct nfs4_opendata *opendata;
2801 	int ret;
2802 
2803 	opendata = nfs4_open_recoverdata_alloc(ctx, state, NFS4_OPEN_CLAIM_FH);
2804 	if (IS_ERR(opendata))
2805 		return PTR_ERR(opendata);
2806 	/*
2807 	 * We're not recovering a delegation, so ask for no delegation.
2808 	 * Otherwise the recovery thread could deadlock with an outstanding
2809 	 * delegation return.
2810 	 */
2811 	opendata->o_arg.open_flags = O_DIRECT;
2812 	ret = nfs4_open_recover(opendata, state);
2813 	if (ret == -ESTALE)
2814 		d_drop(ctx->dentry);
2815 	nfs4_opendata_put(opendata);
2816 	return ret;
2817 }
2818 
nfs4_do_open_expired(struct nfs_open_context * ctx,struct nfs4_state * state)2819 static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
2820 {
2821 	struct nfs_server *server = NFS_SERVER(state->inode);
2822 	struct nfs4_exception exception = { };
2823 	int err;
2824 
2825 	do {
2826 		err = _nfs4_open_expired(ctx, state);
2827 		trace_nfs4_open_expired(ctx, 0, err);
2828 		if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2829 			continue;
2830 		switch (err) {
2831 		default:
2832 			goto out;
2833 		case -NFS4ERR_GRACE:
2834 		case -NFS4ERR_DELAY:
2835 			nfs4_handle_exception(server, err, &exception);
2836 			err = 0;
2837 		}
2838 	} while (exception.retry);
2839 out:
2840 	return err;
2841 }
2842 
nfs4_open_expired(struct nfs4_state_owner * sp,struct nfs4_state * state)2843 static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2844 {
2845 	struct nfs_open_context *ctx;
2846 	int ret;
2847 
2848 	ctx = nfs4_state_find_open_context(state);
2849 	if (IS_ERR(ctx))
2850 		return -EAGAIN;
2851 	ret = nfs4_do_open_expired(ctx, state);
2852 	put_nfs_open_context(ctx);
2853 	return ret;
2854 }
2855 
nfs_finish_clear_delegation_stateid(struct nfs4_state * state,const nfs4_stateid * stateid)2856 static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2857 		const nfs4_stateid *stateid)
2858 {
2859 	nfs_remove_bad_delegation(state->inode, stateid);
2860 	nfs_state_clear_delegation(state);
2861 }
2862 
nfs40_clear_delegation_stateid(struct nfs4_state * state)2863 static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2864 {
2865 	if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2866 		nfs_finish_clear_delegation_stateid(state, NULL);
2867 }
2868 
nfs40_open_expired(struct nfs4_state_owner * sp,struct nfs4_state * state)2869 static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2870 {
2871 	/* NFSv4.0 doesn't allow for delegation recovery on open expire */
2872 	nfs40_clear_delegation_stateid(state);
2873 	nfs_state_clear_open_state_flags(state);
2874 	return nfs4_open_expired(sp, state);
2875 }
2876 
nfs40_test_and_free_expired_stateid(struct nfs_server * server,const nfs4_stateid * stateid,const struct cred * cred)2877 static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2878 					       const nfs4_stateid *stateid,
2879 					       const struct cred *cred)
2880 {
2881 	return -NFS4ERR_BAD_STATEID;
2882 }
2883 
2884 #if defined(CONFIG_NFS_V4_1)
nfs41_test_and_free_expired_stateid(struct nfs_server * server,const nfs4_stateid * stateid,const struct cred * cred)2885 static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2886 					       const nfs4_stateid *stateid,
2887 					       const struct cred *cred)
2888 {
2889 	int status;
2890 
2891 	switch (stateid->type) {
2892 	default:
2893 		break;
2894 	case NFS4_INVALID_STATEID_TYPE:
2895 	case NFS4_SPECIAL_STATEID_TYPE:
2896 		return -NFS4ERR_BAD_STATEID;
2897 	case NFS4_REVOKED_STATEID_TYPE:
2898 		goto out_free;
2899 	}
2900 
2901 	status = nfs41_test_stateid(server, stateid, cred);
2902 	switch (status) {
2903 	case -NFS4ERR_EXPIRED:
2904 	case -NFS4ERR_ADMIN_REVOKED:
2905 	case -NFS4ERR_DELEG_REVOKED:
2906 		break;
2907 	default:
2908 		return status;
2909 	}
2910 out_free:
2911 	/* Ack the revoked state to the server */
2912 	nfs41_free_stateid(server, stateid, cred, true);
2913 	return -NFS4ERR_EXPIRED;
2914 }
2915 
nfs41_check_delegation_stateid(struct nfs4_state * state)2916 static int nfs41_check_delegation_stateid(struct nfs4_state *state)
2917 {
2918 	struct nfs_server *server = NFS_SERVER(state->inode);
2919 	nfs4_stateid stateid;
2920 	struct nfs_delegation *delegation;
2921 	const struct cred *cred = NULL;
2922 	int status, ret = NFS_OK;
2923 
2924 	/* Get the delegation credential for use by test/free_stateid */
2925 	rcu_read_lock();
2926 	delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2927 	if (delegation == NULL) {
2928 		rcu_read_unlock();
2929 		nfs_state_clear_delegation(state);
2930 		return NFS_OK;
2931 	}
2932 
2933 	spin_lock(&delegation->lock);
2934 	nfs4_stateid_copy(&stateid, &delegation->stateid);
2935 
2936 	if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2937 				&delegation->flags)) {
2938 		spin_unlock(&delegation->lock);
2939 		rcu_read_unlock();
2940 		return NFS_OK;
2941 	}
2942 
2943 	if (delegation->cred)
2944 		cred = get_cred(delegation->cred);
2945 	spin_unlock(&delegation->lock);
2946 	rcu_read_unlock();
2947 	status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
2948 	trace_nfs4_test_delegation_stateid(state, NULL, status);
2949 	if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
2950 		nfs_finish_clear_delegation_stateid(state, &stateid);
2951 	else
2952 		ret = status;
2953 
2954 	put_cred(cred);
2955 	return ret;
2956 }
2957 
nfs41_delegation_recover_stateid(struct nfs4_state * state)2958 static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2959 {
2960 	nfs4_stateid tmp;
2961 
2962 	if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2963 	    nfs4_copy_delegation_stateid(state->inode, state->state,
2964 				&tmp, NULL) &&
2965 	    nfs4_stateid_match_other(&state->stateid, &tmp))
2966 		nfs_state_set_delegation(state, &tmp, state->state);
2967 	else
2968 		nfs_state_clear_delegation(state);
2969 }
2970 
2971 /**
2972  * nfs41_check_expired_locks - possibly free a lock stateid
2973  *
2974  * @state: NFSv4 state for an inode
2975  *
2976  * Returns NFS_OK if recovery for this stateid is now finished.
2977  * Otherwise a negative NFS4ERR value is returned.
2978  */
nfs41_check_expired_locks(struct nfs4_state * state)2979 static int nfs41_check_expired_locks(struct nfs4_state *state)
2980 {
2981 	int status, ret = NFS_OK;
2982 	struct nfs4_lock_state *lsp, *prev = NULL;
2983 	struct nfs_server *server = NFS_SERVER(state->inode);
2984 
2985 	if (!test_bit(LK_STATE_IN_USE, &state->flags))
2986 		goto out;
2987 
2988 	spin_lock(&state->state_lock);
2989 	list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2990 		if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2991 			const struct cred *cred = lsp->ls_state->owner->so_cred;
2992 
2993 			refcount_inc(&lsp->ls_count);
2994 			spin_unlock(&state->state_lock);
2995 
2996 			nfs4_put_lock_state(prev);
2997 			prev = lsp;
2998 
2999 			status = nfs41_test_and_free_expired_stateid(server,
3000 					&lsp->ls_stateid,
3001 					cred);
3002 			trace_nfs4_test_lock_stateid(state, lsp, status);
3003 			if (status == -NFS4ERR_EXPIRED ||
3004 			    status == -NFS4ERR_BAD_STATEID) {
3005 				clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
3006 				lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
3007 				if (!recover_lost_locks)
3008 					set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
3009 			} else if (status != NFS_OK) {
3010 				ret = status;
3011 				nfs4_put_lock_state(prev);
3012 				goto out;
3013 			}
3014 			spin_lock(&state->state_lock);
3015 		}
3016 	}
3017 	spin_unlock(&state->state_lock);
3018 	nfs4_put_lock_state(prev);
3019 out:
3020 	return ret;
3021 }
3022 
3023 /**
3024  * nfs41_check_open_stateid - possibly free an open stateid
3025  *
3026  * @state: NFSv4 state for an inode
3027  *
3028  * Returns NFS_OK if recovery for this stateid is now finished.
3029  * Otherwise a negative NFS4ERR value is returned.
3030  */
nfs41_check_open_stateid(struct nfs4_state * state)3031 static int nfs41_check_open_stateid(struct nfs4_state *state)
3032 {
3033 	struct nfs_server *server = NFS_SERVER(state->inode);
3034 	nfs4_stateid *stateid = &state->open_stateid;
3035 	const struct cred *cred = state->owner->so_cred;
3036 	int status;
3037 
3038 	if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
3039 		return -NFS4ERR_BAD_STATEID;
3040 	status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
3041 	trace_nfs4_test_open_stateid(state, NULL, status);
3042 	if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
3043 		nfs_state_clear_open_state_flags(state);
3044 		stateid->type = NFS4_INVALID_STATEID_TYPE;
3045 		return status;
3046 	}
3047 	if (nfs_open_stateid_recover_openmode(state))
3048 		return -NFS4ERR_OPENMODE;
3049 	return NFS_OK;
3050 }
3051 
nfs41_open_expired(struct nfs4_state_owner * sp,struct nfs4_state * state)3052 static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
3053 {
3054 	int status;
3055 
3056 	status = nfs41_check_delegation_stateid(state);
3057 	if (status != NFS_OK)
3058 		return status;
3059 	nfs41_delegation_recover_stateid(state);
3060 
3061 	status = nfs41_check_expired_locks(state);
3062 	if (status != NFS_OK)
3063 		return status;
3064 	status = nfs41_check_open_stateid(state);
3065 	if (status != NFS_OK)
3066 		status = nfs4_open_expired(sp, state);
3067 	return status;
3068 }
3069 #endif
3070 
3071 /*
3072  * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
3073  * fields corresponding to attributes that were used to store the verifier.
3074  * Make sure we clobber those fields in the later setattr call
3075  */
nfs4_exclusive_attrset(struct nfs4_opendata * opendata,struct iattr * sattr,struct nfs4_label ** label)3076 static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
3077 				struct iattr *sattr, struct nfs4_label **label)
3078 {
3079 	const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
3080 	__u32 attrset[3];
3081 	unsigned ret;
3082 	unsigned i;
3083 
3084 	for (i = 0; i < ARRAY_SIZE(attrset); i++) {
3085 		attrset[i] = opendata->o_res.attrset[i];
3086 		if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
3087 			attrset[i] &= ~bitmask[i];
3088 	}
3089 
3090 	ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
3091 		sattr->ia_valid : 0;
3092 
3093 	if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
3094 		if (sattr->ia_valid & ATTR_ATIME_SET)
3095 			ret |= ATTR_ATIME_SET;
3096 		else
3097 			ret |= ATTR_ATIME;
3098 	}
3099 
3100 	if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
3101 		if (sattr->ia_valid & ATTR_MTIME_SET)
3102 			ret |= ATTR_MTIME_SET;
3103 		else
3104 			ret |= ATTR_MTIME;
3105 	}
3106 
3107 	if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
3108 		*label = NULL;
3109 	return ret;
3110 }
3111 
_nfs4_open_and_get_state(struct nfs4_opendata * opendata,struct nfs_open_context * ctx)3112 static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
3113 		struct nfs_open_context *ctx)
3114 {
3115 	struct nfs4_state_owner *sp = opendata->owner;
3116 	struct nfs_server *server = sp->so_server;
3117 	struct dentry *dentry;
3118 	struct nfs4_state *state;
3119 	fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
3120 	struct inode *dir = d_inode(opendata->dir);
3121 	unsigned long dir_verifier;
3122 	int ret;
3123 
3124 	dir_verifier = nfs_save_change_attribute(dir);
3125 
3126 	ret = _nfs4_proc_open(opendata, ctx);
3127 	if (ret != 0)
3128 		goto out;
3129 
3130 	state = _nfs4_opendata_to_nfs4_state(opendata);
3131 	ret = PTR_ERR(state);
3132 	if (IS_ERR(state))
3133 		goto out;
3134 	ctx->state = state;
3135 	if (server->caps & NFS_CAP_POSIX_LOCK)
3136 		set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
3137 	if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3138 		set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
3139 	if (opendata->o_res.rflags & NFS4_OPEN_RESULT_PRESERVE_UNLINKED)
3140 		set_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(state->inode)->flags);
3141 
3142 	dentry = opendata->dentry;
3143 	if (d_really_is_negative(dentry)) {
3144 		struct dentry *alias;
3145 		d_drop(dentry);
3146 		alias = d_exact_alias(dentry, state->inode);
3147 		if (!alias)
3148 			alias = d_splice_alias(igrab(state->inode), dentry);
3149 		/* d_splice_alias() can't fail here - it's a non-directory */
3150 		if (alias) {
3151 			dput(ctx->dentry);
3152 			ctx->dentry = dentry = alias;
3153 		}
3154 	}
3155 
3156 	switch(opendata->o_arg.claim) {
3157 	default:
3158 		break;
3159 	case NFS4_OPEN_CLAIM_NULL:
3160 	case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3161 	case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3162 		if (!opendata->rpc_done)
3163 			break;
3164 		if (opendata->o_res.delegation.type != 0)
3165 			dir_verifier = nfs_save_change_attribute(dir);
3166 		nfs_set_verifier(dentry, dir_verifier);
3167 	}
3168 
3169 	/* Parse layoutget results before we check for access */
3170 	pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3171 
3172 	ret = nfs4_opendata_access(sp->so_cred, opendata, state, acc_mode);
3173 	if (ret != 0)
3174 		goto out;
3175 
3176 	if (d_inode(dentry) == state->inode)
3177 		nfs_inode_attach_open_context(ctx);
3178 
3179 out:
3180 	if (!opendata->cancelled) {
3181 		if (opendata->lgp) {
3182 			nfs4_lgopen_release(opendata->lgp);
3183 			opendata->lgp = NULL;
3184 		}
3185 		nfs4_sequence_free_slot(&opendata->o_res.seq_res);
3186 	}
3187 	return ret;
3188 }
3189 
3190 /*
3191  * Returns a referenced nfs4_state
3192  */
_nfs4_do_open(struct inode * dir,struct nfs_open_context * ctx,int flags,const struct nfs4_open_createattrs * c,int * opened)3193 static int _nfs4_do_open(struct inode *dir,
3194 			struct nfs_open_context *ctx,
3195 			int flags,
3196 			const struct nfs4_open_createattrs *c,
3197 			int *opened)
3198 {
3199 	struct nfs4_state_owner  *sp;
3200 	struct nfs4_state     *state = NULL;
3201 	struct nfs_server       *server = NFS_SERVER(dir);
3202 	struct nfs4_opendata *opendata;
3203 	struct dentry *dentry = ctx->dentry;
3204 	const struct cred *cred = ctx->cred;
3205 	struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
3206 	fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
3207 	enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
3208 	struct iattr *sattr = c->sattr;
3209 	struct nfs4_label *label = c->label;
3210 	int status;
3211 
3212 	/* Protect against reboot recovery conflicts */
3213 	status = -ENOMEM;
3214 	sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3215 	if (sp == NULL) {
3216 		dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3217 		goto out_err;
3218 	}
3219 	status = nfs4_client_recover_expired_lease(server->nfs_client);
3220 	if (status != 0)
3221 		goto err_put_state_owner;
3222 	if (d_really_is_positive(dentry))
3223 		nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
3224 	status = -ENOMEM;
3225 	if (d_really_is_positive(dentry))
3226 		claim = NFS4_OPEN_CLAIM_FH;
3227 	opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3228 			c, claim, GFP_KERNEL);
3229 	if (opendata == NULL)
3230 		goto err_put_state_owner;
3231 
3232 	if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3233 		if (!opendata->f_attr.mdsthreshold) {
3234 			opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3235 			if (!opendata->f_attr.mdsthreshold)
3236 				goto err_opendata_put;
3237 		}
3238 		opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
3239 	}
3240 	if (d_really_is_positive(dentry))
3241 		opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
3242 
3243 	status = _nfs4_open_and_get_state(opendata, ctx);
3244 	if (status != 0)
3245 		goto err_opendata_put;
3246 	state = ctx->state;
3247 
3248 	if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
3249 	    (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
3250 		unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
3251 		/*
3252 		 * send create attributes which was not set by open
3253 		 * with an extra setattr.
3254 		 */
3255 		if (attrs || label) {
3256 			unsigned ia_old = sattr->ia_valid;
3257 
3258 			sattr->ia_valid = attrs;
3259 			nfs_fattr_init(opendata->o_res.f_attr);
3260 			status = nfs4_do_setattr(state->inode, cred,
3261 					opendata->o_res.f_attr, sattr,
3262 					ctx, label);
3263 			if (status == 0) {
3264 				nfs_setattr_update_inode(state->inode, sattr,
3265 						opendata->o_res.f_attr);
3266 				nfs_setsecurity(state->inode, opendata->o_res.f_attr);
3267 			}
3268 			sattr->ia_valid = ia_old;
3269 		}
3270 	}
3271 	if (opened && opendata->file_created)
3272 		*opened = 1;
3273 
3274 	if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
3275 		*ctx_th = opendata->f_attr.mdsthreshold;
3276 		opendata->f_attr.mdsthreshold = NULL;
3277 	}
3278 
3279 	nfs4_opendata_put(opendata);
3280 	nfs4_put_state_owner(sp);
3281 	return 0;
3282 err_opendata_put:
3283 	nfs4_opendata_put(opendata);
3284 err_put_state_owner:
3285 	nfs4_put_state_owner(sp);
3286 out_err:
3287 	return status;
3288 }
3289 
3290 
nfs4_do_open(struct inode * dir,struct nfs_open_context * ctx,int flags,struct iattr * sattr,struct nfs4_label * label,int * opened)3291 static struct nfs4_state *nfs4_do_open(struct inode *dir,
3292 					struct nfs_open_context *ctx,
3293 					int flags,
3294 					struct iattr *sattr,
3295 					struct nfs4_label *label,
3296 					int *opened)
3297 {
3298 	struct nfs_server *server = NFS_SERVER(dir);
3299 	struct nfs4_exception exception = {
3300 		.interruptible = true,
3301 	};
3302 	struct nfs4_state *res;
3303 	struct nfs4_open_createattrs c = {
3304 		.label = label,
3305 		.sattr = sattr,
3306 		.verf = {
3307 			[0] = (__u32)jiffies,
3308 			[1] = (__u32)current->pid,
3309 		},
3310 	};
3311 	int status;
3312 
3313 	do {
3314 		status = _nfs4_do_open(dir, ctx, flags, &c, opened);
3315 		res = ctx->state;
3316 		trace_nfs4_open_file(ctx, flags, status);
3317 		if (status == 0)
3318 			break;
3319 		/* NOTE: BAD_SEQID means the server and client disagree about the
3320 		 * book-keeping w.r.t. state-changing operations
3321 		 * (OPEN/CLOSE/LOCK/LOCKU...)
3322 		 * It is actually a sign of a bug on the client or on the server.
3323 		 *
3324 		 * If we receive a BAD_SEQID error in the particular case of
3325 		 * doing an OPEN, we assume that nfs_increment_open_seqid() will
3326 		 * have unhashed the old state_owner for us, and that we can
3327 		 * therefore safely retry using a new one. We should still warn
3328 		 * the user though...
3329 		 */
3330 		if (status == -NFS4ERR_BAD_SEQID) {
3331 			pr_warn_ratelimited("NFS: v4 server %s "
3332 					" returned a bad sequence-id error!\n",
3333 					NFS_SERVER(dir)->nfs_client->cl_hostname);
3334 			exception.retry = 1;
3335 			continue;
3336 		}
3337 		/*
3338 		 * BAD_STATEID on OPEN means that the server cancelled our
3339 		 * state before it received the OPEN_CONFIRM.
3340 		 * Recover by retrying the request as per the discussion
3341 		 * on Page 181 of RFC3530.
3342 		 */
3343 		if (status == -NFS4ERR_BAD_STATEID) {
3344 			exception.retry = 1;
3345 			continue;
3346 		}
3347 		if (status == -NFS4ERR_EXPIRED) {
3348 			nfs4_schedule_lease_recovery(server->nfs_client);
3349 			exception.retry = 1;
3350 			continue;
3351 		}
3352 		if (status == -EAGAIN) {
3353 			/* We must have found a delegation */
3354 			exception.retry = 1;
3355 			continue;
3356 		}
3357 		if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3358 			continue;
3359 		res = ERR_PTR(nfs4_handle_exception(server,
3360 					status, &exception));
3361 	} while (exception.retry);
3362 	return res;
3363 }
3364 
_nfs4_do_setattr(struct inode * inode,struct nfs_setattrargs * arg,struct nfs_setattrres * res,const struct cred * cred,struct nfs_open_context * ctx)3365 static int _nfs4_do_setattr(struct inode *inode,
3366 			    struct nfs_setattrargs *arg,
3367 			    struct nfs_setattrres *res,
3368 			    const struct cred *cred,
3369 			    struct nfs_open_context *ctx)
3370 {
3371 	struct nfs_server *server = NFS_SERVER(inode);
3372 	struct rpc_message msg = {
3373 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3374 		.rpc_argp	= arg,
3375 		.rpc_resp	= res,
3376 		.rpc_cred	= cred,
3377 	};
3378 	const struct cred *delegation_cred = NULL;
3379 	unsigned long timestamp = jiffies;
3380 	bool truncate;
3381 	int status;
3382 
3383 	nfs_fattr_init(res->fattr);
3384 
3385 	/* Servers should only apply open mode checks for file size changes */
3386 	truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3387 	if (!truncate) {
3388 		nfs4_inode_make_writeable(inode);
3389 		goto zero_stateid;
3390 	}
3391 
3392 	if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
3393 		/* Use that stateid */
3394 	} else if (ctx != NULL && ctx->state) {
3395 		struct nfs_lock_context *l_ctx;
3396 		if (!nfs4_valid_open_stateid(ctx->state))
3397 			return -EBADF;
3398 		l_ctx = nfs_get_lock_context(ctx);
3399 		if (IS_ERR(l_ctx))
3400 			return PTR_ERR(l_ctx);
3401 		status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3402 						&arg->stateid, &delegation_cred);
3403 		nfs_put_lock_context(l_ctx);
3404 		if (status == -EIO)
3405 			return -EBADF;
3406 		else if (status == -EAGAIN)
3407 			goto zero_stateid;
3408 	} else {
3409 zero_stateid:
3410 		nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3411 	}
3412 	if (delegation_cred)
3413 		msg.rpc_cred = delegation_cred;
3414 
3415 	status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3416 
3417 	put_cred(delegation_cred);
3418 	if (status == 0 && ctx != NULL)
3419 		renew_lease(server, timestamp);
3420 	trace_nfs4_setattr(inode, &arg->stateid, status);
3421 	return status;
3422 }
3423 
nfs4_do_setattr(struct inode * inode,const struct cred * cred,struct nfs_fattr * fattr,struct iattr * sattr,struct nfs_open_context * ctx,struct nfs4_label * ilabel)3424 static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
3425 			   struct nfs_fattr *fattr, struct iattr *sattr,
3426 			   struct nfs_open_context *ctx, struct nfs4_label *ilabel)
3427 {
3428 	struct nfs_server *server = NFS_SERVER(inode);
3429 	__u32 bitmask[NFS4_BITMASK_SZ];
3430 	struct nfs4_state *state = ctx ? ctx->state : NULL;
3431 	struct nfs_setattrargs	arg = {
3432 		.fh		= NFS_FH(inode),
3433 		.iap		= sattr,
3434 		.server		= server,
3435 		.bitmask = bitmask,
3436 		.label		= ilabel,
3437 	};
3438 	struct nfs_setattrres  res = {
3439 		.fattr		= fattr,
3440 		.server		= server,
3441 	};
3442 	struct nfs4_exception exception = {
3443 		.state = state,
3444 		.inode = inode,
3445 		.stateid = &arg.stateid,
3446 	};
3447 	unsigned long adjust_flags = NFS_INO_INVALID_CHANGE |
3448 				     NFS_INO_INVALID_CTIME;
3449 	int err;
3450 
3451 	if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID))
3452 		adjust_flags |= NFS_INO_INVALID_MODE;
3453 	if (sattr->ia_valid & (ATTR_UID | ATTR_GID))
3454 		adjust_flags |= NFS_INO_INVALID_OTHER;
3455 
3456 	do {
3457 		nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label),
3458 					inode, adjust_flags);
3459 
3460 		err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3461 		switch (err) {
3462 		case -NFS4ERR_OPENMODE:
3463 			if (!(sattr->ia_valid & ATTR_SIZE)) {
3464 				pr_warn_once("NFSv4: server %s is incorrectly "
3465 						"applying open mode checks to "
3466 						"a SETATTR that is not "
3467 						"changing file size.\n",
3468 						server->nfs_client->cl_hostname);
3469 			}
3470 			if (state && !(state->state & FMODE_WRITE)) {
3471 				err = -EBADF;
3472 				if (sattr->ia_valid & ATTR_OPEN)
3473 					err = -EACCES;
3474 				goto out;
3475 			}
3476 		}
3477 		err = nfs4_handle_exception(server, err, &exception);
3478 	} while (exception.retry);
3479 out:
3480 	return err;
3481 }
3482 
3483 static bool
nfs4_wait_on_layoutreturn(struct inode * inode,struct rpc_task * task)3484 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3485 {
3486 	if (inode == NULL || !nfs_have_layout(inode))
3487 		return false;
3488 
3489 	return pnfs_wait_on_layoutreturn(inode, task);
3490 }
3491 
3492 /*
3493  * Update the seqid of an open stateid
3494  */
nfs4_sync_open_stateid(nfs4_stateid * dst,struct nfs4_state * state)3495 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3496 		struct nfs4_state *state)
3497 {
3498 	__be32 seqid_open;
3499 	u32 dst_seqid;
3500 	int seq;
3501 
3502 	for (;;) {
3503 		if (!nfs4_valid_open_stateid(state))
3504 			break;
3505 		seq = read_seqbegin(&state->seqlock);
3506 		if (!nfs4_state_match_open_stateid_other(state, dst)) {
3507 			nfs4_stateid_copy(dst, &state->open_stateid);
3508 			if (read_seqretry(&state->seqlock, seq))
3509 				continue;
3510 			break;
3511 		}
3512 		seqid_open = state->open_stateid.seqid;
3513 		if (read_seqretry(&state->seqlock, seq))
3514 			continue;
3515 
3516 		dst_seqid = be32_to_cpu(dst->seqid);
3517 		if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3518 			dst->seqid = seqid_open;
3519 		break;
3520 	}
3521 }
3522 
3523 /*
3524  * Update the seqid of an open stateid after receiving
3525  * NFS4ERR_OLD_STATEID
3526  */
nfs4_refresh_open_old_stateid(nfs4_stateid * dst,struct nfs4_state * state)3527 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3528 		struct nfs4_state *state)
3529 {
3530 	__be32 seqid_open;
3531 	u32 dst_seqid;
3532 	bool ret;
3533 	int seq, status = -EAGAIN;
3534 	DEFINE_WAIT(wait);
3535 
3536 	for (;;) {
3537 		ret = false;
3538 		if (!nfs4_valid_open_stateid(state))
3539 			break;
3540 		seq = read_seqbegin(&state->seqlock);
3541 		if (!nfs4_state_match_open_stateid_other(state, dst)) {
3542 			if (read_seqretry(&state->seqlock, seq))
3543 				continue;
3544 			break;
3545 		}
3546 
3547 		write_seqlock(&state->seqlock);
3548 		seqid_open = state->open_stateid.seqid;
3549 
3550 		dst_seqid = be32_to_cpu(dst->seqid);
3551 
3552 		/* Did another OPEN bump the state's seqid?  try again: */
3553 		if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3554 			dst->seqid = seqid_open;
3555 			write_sequnlock(&state->seqlock);
3556 			ret = true;
3557 			break;
3558 		}
3559 
3560 		/* server says we're behind but we haven't seen the update yet */
3561 		set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3562 		prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3563 		write_sequnlock(&state->seqlock);
3564 		trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3565 
3566 		if (fatal_signal_pending(current))
3567 			status = -EINTR;
3568 		else
3569 			if (schedule_timeout(5*HZ) != 0)
3570 				status = 0;
3571 
3572 		finish_wait(&state->waitq, &wait);
3573 
3574 		if (!status)
3575 			continue;
3576 		if (status == -EINTR)
3577 			break;
3578 
3579 		/* we slept the whole 5 seconds, we must have lost a seqid */
3580 		dst->seqid = cpu_to_be32(dst_seqid + 1);
3581 		ret = true;
3582 		break;
3583 	}
3584 
3585 	return ret;
3586 }
3587 
3588 struct nfs4_closedata {
3589 	struct inode *inode;
3590 	struct nfs4_state *state;
3591 	struct nfs_closeargs arg;
3592 	struct nfs_closeres res;
3593 	struct {
3594 		struct nfs4_layoutreturn_args arg;
3595 		struct nfs4_layoutreturn_res res;
3596 		struct nfs4_xdr_opaque_data ld_private;
3597 		u32 roc_barrier;
3598 		bool roc;
3599 	} lr;
3600 	struct nfs_fattr fattr;
3601 	unsigned long timestamp;
3602 };
3603 
nfs4_free_closedata(void * data)3604 static void nfs4_free_closedata(void *data)
3605 {
3606 	struct nfs4_closedata *calldata = data;
3607 	struct nfs4_state_owner *sp = calldata->state->owner;
3608 	struct super_block *sb = calldata->state->inode->i_sb;
3609 
3610 	if (calldata->lr.roc)
3611 		pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3612 				calldata->res.lr_ret);
3613 	nfs4_put_open_state(calldata->state);
3614 	nfs_free_seqid(calldata->arg.seqid);
3615 	nfs4_put_state_owner(sp);
3616 	nfs_sb_deactive(sb);
3617 	kfree(calldata);
3618 }
3619 
nfs4_close_done(struct rpc_task * task,void * data)3620 static void nfs4_close_done(struct rpc_task *task, void *data)
3621 {
3622 	struct nfs4_closedata *calldata = data;
3623 	struct nfs4_state *state = calldata->state;
3624 	struct nfs_server *server = NFS_SERVER(calldata->inode);
3625 	nfs4_stateid *res_stateid = NULL;
3626 	struct nfs4_exception exception = {
3627 		.state = state,
3628 		.inode = calldata->inode,
3629 		.stateid = &calldata->arg.stateid,
3630 	};
3631 
3632 	if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3633 		return;
3634 	trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3635 
3636 	/* Handle Layoutreturn errors */
3637 	if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3638 			  &calldata->res.lr_ret) == -EAGAIN)
3639 		goto out_restart;
3640 
3641 	/* hmm. we are done with the inode, and in the process of freeing
3642 	 * the state_owner. we keep this around to process errors
3643 	 */
3644 	switch (task->tk_status) {
3645 		case 0:
3646 			res_stateid = &calldata->res.stateid;
3647 			renew_lease(server, calldata->timestamp);
3648 			break;
3649 		case -NFS4ERR_ACCESS:
3650 			if (calldata->arg.bitmask != NULL) {
3651 				calldata->arg.bitmask = NULL;
3652 				calldata->res.fattr = NULL;
3653 				goto out_restart;
3654 
3655 			}
3656 			break;
3657 		case -NFS4ERR_OLD_STATEID:
3658 			/* Did we race with OPEN? */
3659 			if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3660 						state))
3661 				goto out_restart;
3662 			goto out_release;
3663 		case -NFS4ERR_ADMIN_REVOKED:
3664 		case -NFS4ERR_STALE_STATEID:
3665 		case -NFS4ERR_EXPIRED:
3666 			nfs4_free_revoked_stateid(server,
3667 					&calldata->arg.stateid,
3668 					task->tk_msg.rpc_cred);
3669 			fallthrough;
3670 		case -NFS4ERR_BAD_STATEID:
3671 			if (calldata->arg.fmode == 0)
3672 				break;
3673 			fallthrough;
3674 		default:
3675 			task->tk_status = nfs4_async_handle_exception(task,
3676 					server, task->tk_status, &exception);
3677 			if (exception.retry)
3678 				goto out_restart;
3679 	}
3680 	nfs_clear_open_stateid(state, &calldata->arg.stateid,
3681 			res_stateid, calldata->arg.fmode);
3682 out_release:
3683 	task->tk_status = 0;
3684 	nfs_release_seqid(calldata->arg.seqid);
3685 	nfs_refresh_inode(calldata->inode, &calldata->fattr);
3686 	dprintk("%s: ret = %d\n", __func__, task->tk_status);
3687 	return;
3688 out_restart:
3689 	task->tk_status = 0;
3690 	rpc_restart_call_prepare(task);
3691 	goto out_release;
3692 }
3693 
nfs4_close_prepare(struct rpc_task * task,void * data)3694 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3695 {
3696 	struct nfs4_closedata *calldata = data;
3697 	struct nfs4_state *state = calldata->state;
3698 	struct inode *inode = calldata->inode;
3699 	struct nfs_server *server = NFS_SERVER(inode);
3700 	struct pnfs_layout_hdr *lo;
3701 	bool is_rdonly, is_wronly, is_rdwr;
3702 	int call_close = 0;
3703 
3704 	if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3705 		goto out_wait;
3706 
3707 	task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3708 	spin_lock(&state->owner->so_lock);
3709 	is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3710 	is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3711 	is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3712 	/* Calculate the change in open mode */
3713 	calldata->arg.fmode = 0;
3714 	if (state->n_rdwr == 0) {
3715 		if (state->n_rdonly == 0)
3716 			call_close |= is_rdonly;
3717 		else if (is_rdonly)
3718 			calldata->arg.fmode |= FMODE_READ;
3719 		if (state->n_wronly == 0)
3720 			call_close |= is_wronly;
3721 		else if (is_wronly)
3722 			calldata->arg.fmode |= FMODE_WRITE;
3723 		if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3724 			call_close |= is_rdwr;
3725 	} else if (is_rdwr)
3726 		calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3727 
3728 	nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3729 	if (!nfs4_valid_open_stateid(state))
3730 		call_close = 0;
3731 	spin_unlock(&state->owner->so_lock);
3732 
3733 	if (!call_close) {
3734 		/* Note: exit _without_ calling nfs4_close_done */
3735 		goto out_no_action;
3736 	}
3737 
3738 	if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3739 		nfs_release_seqid(calldata->arg.seqid);
3740 		goto out_wait;
3741 	}
3742 
3743 	lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3744 	if (lo && !pnfs_layout_is_valid(lo)) {
3745 		calldata->arg.lr_args = NULL;
3746 		calldata->res.lr_res = NULL;
3747 	}
3748 
3749 	if (calldata->arg.fmode == 0)
3750 		task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3751 
3752 	if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3753 		/* Close-to-open cache consistency revalidation */
3754 		if (!nfs4_have_delegation(inode, FMODE_READ, 0)) {
3755 			nfs4_bitmask_set(calldata->arg.bitmask_store,
3756 					 server->cache_consistency_bitmask,
3757 					 inode, 0);
3758 			calldata->arg.bitmask = calldata->arg.bitmask_store;
3759 		} else
3760 			calldata->arg.bitmask = NULL;
3761 	}
3762 
3763 	calldata->arg.share_access =
3764 		nfs4_fmode_to_share_access(calldata->arg.fmode);
3765 
3766 	if (calldata->res.fattr == NULL)
3767 		calldata->arg.bitmask = NULL;
3768 	else if (calldata->arg.bitmask == NULL)
3769 		calldata->res.fattr = NULL;
3770 	calldata->timestamp = jiffies;
3771 	if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3772 				&calldata->arg.seq_args,
3773 				&calldata->res.seq_res,
3774 				task) != 0)
3775 		nfs_release_seqid(calldata->arg.seqid);
3776 	return;
3777 out_no_action:
3778 	task->tk_action = NULL;
3779 out_wait:
3780 	nfs4_sequence_done(task, &calldata->res.seq_res);
3781 }
3782 
3783 static const struct rpc_call_ops nfs4_close_ops = {
3784 	.rpc_call_prepare = nfs4_close_prepare,
3785 	.rpc_call_done = nfs4_close_done,
3786 	.rpc_release = nfs4_free_closedata,
3787 };
3788 
3789 /*
3790  * It is possible for data to be read/written from a mem-mapped file
3791  * after the sys_close call (which hits the vfs layer as a flush).
3792  * This means that we can't safely call nfsv4 close on a file until
3793  * the inode is cleared. This in turn means that we are not good
3794  * NFSv4 citizens - we do not indicate to the server to update the file's
3795  * share state even when we are done with one of the three share
3796  * stateid's in the inode.
3797  *
3798  * NOTE: Caller must be holding the sp->so_owner semaphore!
3799  */
nfs4_do_close(struct nfs4_state * state,gfp_t gfp_mask,int wait)3800 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3801 {
3802 	struct nfs_server *server = NFS_SERVER(state->inode);
3803 	struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3804 	struct nfs4_closedata *calldata;
3805 	struct nfs4_state_owner *sp = state->owner;
3806 	struct rpc_task *task;
3807 	struct rpc_message msg = {
3808 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3809 		.rpc_cred = state->owner->so_cred,
3810 	};
3811 	struct rpc_task_setup task_setup_data = {
3812 		.rpc_client = server->client,
3813 		.rpc_message = &msg,
3814 		.callback_ops = &nfs4_close_ops,
3815 		.workqueue = nfsiod_workqueue,
3816 		.flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3817 	};
3818 	int status = -ENOMEM;
3819 
3820 	if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
3821 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
3822 
3823 	nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3824 		&task_setup_data.rpc_client, &msg);
3825 
3826 	calldata = kzalloc(sizeof(*calldata), gfp_mask);
3827 	if (calldata == NULL)
3828 		goto out;
3829 	nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3830 	calldata->inode = state->inode;
3831 	calldata->state = state;
3832 	calldata->arg.fh = NFS_FH(state->inode);
3833 	if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3834 		goto out_free_calldata;
3835 	/* Serialization for the sequence id */
3836 	alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3837 	calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3838 	if (IS_ERR(calldata->arg.seqid))
3839 		goto out_free_calldata;
3840 	nfs_fattr_init(&calldata->fattr);
3841 	calldata->arg.fmode = 0;
3842 	calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3843 	calldata->res.fattr = &calldata->fattr;
3844 	calldata->res.seqid = calldata->arg.seqid;
3845 	calldata->res.server = server;
3846 	calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3847 	calldata->lr.roc = pnfs_roc(state->inode,
3848 			&calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3849 	if (calldata->lr.roc) {
3850 		calldata->arg.lr_args = &calldata->lr.arg;
3851 		calldata->res.lr_res = &calldata->lr.res;
3852 	}
3853 	nfs_sb_active(calldata->inode->i_sb);
3854 
3855 	msg.rpc_argp = &calldata->arg;
3856 	msg.rpc_resp = &calldata->res;
3857 	task_setup_data.callback_data = calldata;
3858 	task = rpc_run_task(&task_setup_data);
3859 	if (IS_ERR(task))
3860 		return PTR_ERR(task);
3861 	status = 0;
3862 	if (wait)
3863 		status = rpc_wait_for_completion_task(task);
3864 	rpc_put_task(task);
3865 	return status;
3866 out_free_calldata:
3867 	kfree(calldata);
3868 out:
3869 	nfs4_put_open_state(state);
3870 	nfs4_put_state_owner(sp);
3871 	return status;
3872 }
3873 
3874 static struct inode *
nfs4_atomic_open(struct inode * dir,struct nfs_open_context * ctx,int open_flags,struct iattr * attr,int * opened)3875 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3876 		int open_flags, struct iattr *attr, int *opened)
3877 {
3878 	struct nfs4_state *state;
3879 	struct nfs4_label l, *label;
3880 
3881 	label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3882 
3883 	/* Protect against concurrent sillydeletes */
3884 	state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3885 
3886 	nfs4_label_release_security(label);
3887 
3888 	if (IS_ERR(state))
3889 		return ERR_CAST(state);
3890 	return state->inode;
3891 }
3892 
nfs4_close_context(struct nfs_open_context * ctx,int is_sync)3893 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3894 {
3895 	if (ctx->state == NULL)
3896 		return;
3897 	if (is_sync)
3898 		nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3899 	else
3900 		nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3901 }
3902 
3903 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3904 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3905 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_OPEN_ARGUMENTS - 1UL)
3906 
3907 #define FATTR4_WORD2_NFS42_TIME_DELEG_MASK \
3908 	(FATTR4_WORD2_TIME_DELEG_MODIFY|FATTR4_WORD2_TIME_DELEG_ACCESS)
nfs4_server_delegtime_capable(struct nfs4_server_caps_res * res)3909 static bool nfs4_server_delegtime_capable(struct nfs4_server_caps_res *res)
3910 {
3911 	u32 share_access_want = res->open_caps.oa_share_access_want[0];
3912 	u32 attr_bitmask = res->attr_bitmask[2];
3913 
3914 	return (share_access_want & NFS4_SHARE_WANT_DELEG_TIMESTAMPS) &&
3915 	       ((attr_bitmask & FATTR4_WORD2_NFS42_TIME_DELEG_MASK) ==
3916 					FATTR4_WORD2_NFS42_TIME_DELEG_MASK);
3917 }
3918 
_nfs4_server_capabilities(struct nfs_server * server,struct nfs_fh * fhandle)3919 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3920 {
3921 	u32 minorversion = server->nfs_client->cl_minorversion;
3922 	u32 bitmask[3] = {
3923 		[0] = FATTR4_WORD0_SUPPORTED_ATTRS,
3924 	};
3925 	struct nfs4_server_caps_arg args = {
3926 		.fhandle = fhandle,
3927 		.bitmask = bitmask,
3928 	};
3929 	struct nfs4_server_caps_res res = {};
3930 	struct rpc_message msg = {
3931 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3932 		.rpc_argp = &args,
3933 		.rpc_resp = &res,
3934 	};
3935 	int status;
3936 	int i;
3937 
3938 	bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3939 		     FATTR4_WORD0_FH_EXPIRE_TYPE |
3940 		     FATTR4_WORD0_LINK_SUPPORT |
3941 		     FATTR4_WORD0_SYMLINK_SUPPORT |
3942 		     FATTR4_WORD0_ACLSUPPORT |
3943 		     FATTR4_WORD0_CASE_INSENSITIVE |
3944 		     FATTR4_WORD0_CASE_PRESERVING;
3945 	if (minorversion)
3946 		bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT |
3947 			     FATTR4_WORD2_OPEN_ARGUMENTS;
3948 
3949 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3950 	if (status == 0) {
3951 		bitmask[0] = (FATTR4_WORD0_SUPPORTED_ATTRS |
3952 			      FATTR4_WORD0_FH_EXPIRE_TYPE |
3953 			      FATTR4_WORD0_LINK_SUPPORT |
3954 			      FATTR4_WORD0_SYMLINK_SUPPORT |
3955 			      FATTR4_WORD0_ACLSUPPORT |
3956 			      FATTR4_WORD0_CASE_INSENSITIVE |
3957 			      FATTR4_WORD0_CASE_PRESERVING) &
3958 			     res.attr_bitmask[0];
3959 		/* Sanity check the server answers */
3960 		switch (minorversion) {
3961 		case 0:
3962 			res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3963 			res.attr_bitmask[2] = 0;
3964 			break;
3965 		case 1:
3966 			res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3967 			bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT &
3968 				     res.attr_bitmask[2];
3969 			break;
3970 		case 2:
3971 			res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3972 			bitmask[2] = (FATTR4_WORD2_SUPPATTR_EXCLCREAT |
3973 				      FATTR4_WORD2_OPEN_ARGUMENTS) &
3974 				     res.attr_bitmask[2];
3975 		}
3976 		memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3977 		server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS |
3978 				  NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL);
3979 		server->fattr_valid = NFS_ATTR_FATTR_V4;
3980 		if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3981 				res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3982 			server->caps |= NFS_CAP_ACLS;
3983 		if (res.has_links != 0)
3984 			server->caps |= NFS_CAP_HARDLINKS;
3985 		if (res.has_symlinks != 0)
3986 			server->caps |= NFS_CAP_SYMLINKS;
3987 		if (res.case_insensitive)
3988 			server->caps |= NFS_CAP_CASE_INSENSITIVE;
3989 		if (res.case_preserving)
3990 			server->caps |= NFS_CAP_CASE_PRESERVING;
3991 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3992 		if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3993 			server->caps |= NFS_CAP_SECURITY_LABEL;
3994 #endif
3995 		if (res.attr_bitmask[0] & FATTR4_WORD0_FS_LOCATIONS)
3996 			server->caps |= NFS_CAP_FS_LOCATIONS;
3997 		if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
3998 			server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
3999 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
4000 			server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
4001 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
4002 			server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
4003 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
4004 			server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
4005 				NFS_ATTR_FATTR_OWNER_NAME);
4006 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
4007 			server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
4008 				NFS_ATTR_FATTR_GROUP_NAME);
4009 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
4010 			server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
4011 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
4012 			server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
4013 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
4014 			server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
4015 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
4016 			server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
4017 		memcpy(server->attr_bitmask_nl, res.attr_bitmask,
4018 				sizeof(server->attr_bitmask));
4019 		server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
4020 
4021 		if (res.open_caps.oa_share_access_want[0] &
4022 		    NFS4_SHARE_WANT_OPEN_XOR_DELEGATION)
4023 			server->caps |= NFS_CAP_OPEN_XOR;
4024 		if (nfs4_server_delegtime_capable(&res))
4025 			server->caps |= NFS_CAP_DELEGTIME;
4026 
4027 		memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
4028 		server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
4029 		server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
4030 		server->cache_consistency_bitmask[2] = 0;
4031 
4032 		/* Avoid a regression due to buggy server */
4033 		for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
4034 			res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
4035 		memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
4036 			sizeof(server->exclcreat_bitmask));
4037 
4038 		server->acl_bitmask = res.acl_bitmask;
4039 		server->fh_expire_type = res.fh_expire_type;
4040 	}
4041 
4042 	return status;
4043 }
4044 
nfs4_server_capabilities(struct nfs_server * server,struct nfs_fh * fhandle)4045 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
4046 {
4047 	struct nfs4_exception exception = {
4048 		.interruptible = true,
4049 	};
4050 	int err;
4051 
4052 	nfs4_server_set_init_caps(server);
4053 	do {
4054 		err = nfs4_handle_exception(server,
4055 				_nfs4_server_capabilities(server, fhandle),
4056 				&exception);
4057 	} while (exception.retry);
4058 	return err;
4059 }
4060 
test_fs_location_for_trunking(struct nfs4_fs_location * location,struct nfs_client * clp,struct nfs_server * server)4061 static void test_fs_location_for_trunking(struct nfs4_fs_location *location,
4062 					  struct nfs_client *clp,
4063 					  struct nfs_server *server)
4064 {
4065 	int i;
4066 
4067 	for (i = 0; i < location->nservers; i++) {
4068 		struct nfs4_string *srv_loc = &location->servers[i];
4069 		struct sockaddr_storage addr;
4070 		size_t addrlen;
4071 		struct xprt_create xprt_args = {
4072 			.ident = 0,
4073 			.net = clp->cl_net,
4074 		};
4075 		struct nfs4_add_xprt_data xprtdata = {
4076 			.clp = clp,
4077 		};
4078 		struct rpc_add_xprt_test rpcdata = {
4079 			.add_xprt_test = clp->cl_mvops->session_trunk,
4080 			.data = &xprtdata,
4081 		};
4082 		char *servername = NULL;
4083 
4084 		if (!srv_loc->len)
4085 			continue;
4086 
4087 		addrlen = nfs_parse_server_name(srv_loc->data, srv_loc->len,
4088 						&addr, sizeof(addr),
4089 						clp->cl_net, server->port);
4090 		if (!addrlen)
4091 			return;
4092 		xprt_args.dstaddr = (struct sockaddr *)&addr;
4093 		xprt_args.addrlen = addrlen;
4094 		servername = kmalloc(srv_loc->len + 1, GFP_KERNEL);
4095 		if (!servername)
4096 			return;
4097 		memcpy(servername, srv_loc->data, srv_loc->len);
4098 		servername[srv_loc->len] = '\0';
4099 		xprt_args.servername = servername;
4100 
4101 		xprtdata.cred = nfs4_get_clid_cred(clp);
4102 		rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
4103 				  rpc_clnt_setup_test_and_add_xprt,
4104 				  &rpcdata);
4105 		if (xprtdata.cred)
4106 			put_cred(xprtdata.cred);
4107 		kfree(servername);
4108 	}
4109 }
4110 
_is_same_nfs4_pathname(struct nfs4_pathname * path1,struct nfs4_pathname * path2)4111 static bool _is_same_nfs4_pathname(struct nfs4_pathname *path1,
4112 				   struct nfs4_pathname *path2)
4113 {
4114 	int i;
4115 
4116 	if (path1->ncomponents != path2->ncomponents)
4117 		return false;
4118 	for (i = 0; i < path1->ncomponents; i++) {
4119 		if (path1->components[i].len != path2->components[i].len)
4120 			return false;
4121 		if (memcmp(path1->components[i].data, path2->components[i].data,
4122 				path1->components[i].len))
4123 			return false;
4124 	}
4125 	return true;
4126 }
4127 
_nfs4_discover_trunking(struct nfs_server * server,struct nfs_fh * fhandle)4128 static int _nfs4_discover_trunking(struct nfs_server *server,
4129 				   struct nfs_fh *fhandle)
4130 {
4131 	struct nfs4_fs_locations *locations = NULL;
4132 	struct page *page;
4133 	const struct cred *cred;
4134 	struct nfs_client *clp = server->nfs_client;
4135 	const struct nfs4_state_maintenance_ops *ops =
4136 		clp->cl_mvops->state_renewal_ops;
4137 	int status = -ENOMEM, i;
4138 
4139 	cred = ops->get_state_renewal_cred(clp);
4140 	if (cred == NULL) {
4141 		cred = nfs4_get_clid_cred(clp);
4142 		if (cred == NULL)
4143 			return -ENOKEY;
4144 	}
4145 
4146 	page = alloc_page(GFP_KERNEL);
4147 	if (!page)
4148 		goto out_put_cred;
4149 	locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4150 	if (!locations)
4151 		goto out_free;
4152 	locations->fattr = nfs_alloc_fattr();
4153 	if (!locations->fattr)
4154 		goto out_free_2;
4155 
4156 	status = nfs4_proc_get_locations(server, fhandle, locations, page,
4157 					 cred);
4158 	if (status)
4159 		goto out_free_3;
4160 
4161 	for (i = 0; i < locations->nlocations; i++) {
4162 		if (!_is_same_nfs4_pathname(&locations->fs_path,
4163 					&locations->locations[i].rootpath))
4164 			continue;
4165 		test_fs_location_for_trunking(&locations->locations[i], clp,
4166 					      server);
4167 	}
4168 out_free_3:
4169 	kfree(locations->fattr);
4170 out_free_2:
4171 	kfree(locations);
4172 out_free:
4173 	__free_page(page);
4174 out_put_cred:
4175 	put_cred(cred);
4176 	return status;
4177 }
4178 
nfs4_discover_trunking(struct nfs_server * server,struct nfs_fh * fhandle)4179 static int nfs4_discover_trunking(struct nfs_server *server,
4180 				  struct nfs_fh *fhandle)
4181 {
4182 	struct nfs4_exception exception = {
4183 		.interruptible = true,
4184 	};
4185 	struct nfs_client *clp = server->nfs_client;
4186 	int err = 0;
4187 
4188 	if (!nfs4_has_session(clp))
4189 		goto out;
4190 	do {
4191 		err = nfs4_handle_exception(server,
4192 				_nfs4_discover_trunking(server, fhandle),
4193 				&exception);
4194 	} while (exception.retry);
4195 out:
4196 	return err;
4197 }
4198 
_nfs4_lookup_root(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * info)4199 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4200 		struct nfs_fsinfo *info)
4201 {
4202 	u32 bitmask[3];
4203 	struct nfs4_lookup_root_arg args = {
4204 		.bitmask = bitmask,
4205 	};
4206 	struct nfs4_lookup_res res = {
4207 		.server = server,
4208 		.fattr = info->fattr,
4209 		.fh = fhandle,
4210 	};
4211 	struct rpc_message msg = {
4212 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
4213 		.rpc_argp = &args,
4214 		.rpc_resp = &res,
4215 	};
4216 
4217 	bitmask[0] = nfs4_fattr_bitmap[0];
4218 	bitmask[1] = nfs4_fattr_bitmap[1];
4219 	/*
4220 	 * Process the label in the upcoming getfattr
4221 	 */
4222 	bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
4223 
4224 	nfs_fattr_init(info->fattr);
4225 	return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4226 }
4227 
nfs4_lookup_root(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * info)4228 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4229 		struct nfs_fsinfo *info)
4230 {
4231 	struct nfs4_exception exception = {
4232 		.interruptible = true,
4233 	};
4234 	int err;
4235 	do {
4236 		err = _nfs4_lookup_root(server, fhandle, info);
4237 		trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
4238 		switch (err) {
4239 		case 0:
4240 		case -NFS4ERR_WRONGSEC:
4241 			goto out;
4242 		default:
4243 			err = nfs4_handle_exception(server, err, &exception);
4244 		}
4245 	} while (exception.retry);
4246 out:
4247 	return err;
4248 }
4249 
nfs4_lookup_root_sec(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * info,rpc_authflavor_t flavor)4250 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4251 				struct nfs_fsinfo *info, rpc_authflavor_t flavor)
4252 {
4253 	struct rpc_auth_create_args auth_args = {
4254 		.pseudoflavor = flavor,
4255 	};
4256 	struct rpc_auth *auth;
4257 
4258 	auth = rpcauth_create(&auth_args, server->client);
4259 	if (IS_ERR(auth))
4260 		return -EACCES;
4261 	return nfs4_lookup_root(server, fhandle, info);
4262 }
4263 
4264 /*
4265  * Retry pseudoroot lookup with various security flavors.  We do this when:
4266  *
4267  *   NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4268  *   NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4269  *
4270  * Returns zero on success, or a negative NFS4ERR value, or a
4271  * negative errno value.
4272  */
nfs4_find_root_sec(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * info)4273 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4274 			      struct nfs_fsinfo *info)
4275 {
4276 	/* Per 3530bis 15.33.5 */
4277 	static const rpc_authflavor_t flav_array[] = {
4278 		RPC_AUTH_GSS_KRB5P,
4279 		RPC_AUTH_GSS_KRB5I,
4280 		RPC_AUTH_GSS_KRB5,
4281 		RPC_AUTH_UNIX,			/* courtesy */
4282 		RPC_AUTH_NULL,
4283 	};
4284 	int status = -EPERM;
4285 	size_t i;
4286 
4287 	if (server->auth_info.flavor_len > 0) {
4288 		/* try each flavor specified by user */
4289 		for (i = 0; i < server->auth_info.flavor_len; i++) {
4290 			status = nfs4_lookup_root_sec(server, fhandle, info,
4291 						server->auth_info.flavors[i]);
4292 			if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4293 				continue;
4294 			break;
4295 		}
4296 	} else {
4297 		/* no flavors specified by user, try default list */
4298 		for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4299 			status = nfs4_lookup_root_sec(server, fhandle, info,
4300 						      flav_array[i]);
4301 			if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4302 				continue;
4303 			break;
4304 		}
4305 	}
4306 
4307 	/*
4308 	 * -EACCES could mean that the user doesn't have correct permissions
4309 	 * to access the mount.  It could also mean that we tried to mount
4310 	 * with a gss auth flavor, but rpc.gssd isn't running.  Either way,
4311 	 * existing mount programs don't handle -EACCES very well so it should
4312 	 * be mapped to -EPERM instead.
4313 	 */
4314 	if (status == -EACCES)
4315 		status = -EPERM;
4316 	return status;
4317 }
4318 
4319 /**
4320  * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4321  * @server: initialized nfs_server handle
4322  * @fhandle: we fill in the pseudo-fs root file handle
4323  * @info: we fill in an FSINFO struct
4324  * @auth_probe: probe the auth flavours
4325  *
4326  * Returns zero on success, or a negative errno.
4327  */
nfs4_proc_get_rootfh(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * info,bool auth_probe)4328 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4329 			 struct nfs_fsinfo *info,
4330 			 bool auth_probe)
4331 {
4332 	int status = 0;
4333 
4334 	if (!auth_probe)
4335 		status = nfs4_lookup_root(server, fhandle, info);
4336 
4337 	if (auth_probe || status == NFS4ERR_WRONGSEC)
4338 		status = server->nfs_client->cl_mvops->find_root_sec(server,
4339 				fhandle, info);
4340 
4341 	if (status == 0)
4342 		status = nfs4_server_capabilities(server, fhandle);
4343 	if (status == 0)
4344 		status = nfs4_do_fsinfo(server, fhandle, info);
4345 
4346 	return nfs4_map_errors(status);
4347 }
4348 
nfs4_proc_get_root(struct nfs_server * server,struct nfs_fh * mntfh,struct nfs_fsinfo * info)4349 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4350 			      struct nfs_fsinfo *info)
4351 {
4352 	int error;
4353 	struct nfs_fattr *fattr = info->fattr;
4354 
4355 	error = nfs4_server_capabilities(server, mntfh);
4356 	if (error < 0) {
4357 		dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4358 		return error;
4359 	}
4360 
4361 	error = nfs4_proc_getattr(server, mntfh, fattr, NULL);
4362 	if (error < 0) {
4363 		dprintk("nfs4_get_root: getattr error = %d\n", -error);
4364 		goto out;
4365 	}
4366 
4367 	if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4368 	    !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4369 		memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4370 
4371 out:
4372 	return error;
4373 }
4374 
4375 /*
4376  * Get locations and (maybe) other attributes of a referral.
4377  * Note that we'll actually follow the referral later when
4378  * we detect fsid mismatch in inode revalidation
4379  */
nfs4_get_referral(struct rpc_clnt * client,struct inode * dir,const struct qstr * name,struct nfs_fattr * fattr,struct nfs_fh * fhandle)4380 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4381 			     const struct qstr *name, struct nfs_fattr *fattr,
4382 			     struct nfs_fh *fhandle)
4383 {
4384 	int status = -ENOMEM;
4385 	struct page *page = NULL;
4386 	struct nfs4_fs_locations *locations = NULL;
4387 
4388 	page = alloc_page(GFP_KERNEL);
4389 	if (page == NULL)
4390 		goto out;
4391 	locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4392 	if (locations == NULL)
4393 		goto out;
4394 
4395 	locations->fattr = fattr;
4396 
4397 	status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4398 	if (status != 0)
4399 		goto out;
4400 
4401 	/*
4402 	 * If the fsid didn't change, this is a migration event, not a
4403 	 * referral.  Cause us to drop into the exception handler, which
4404 	 * will kick off migration recovery.
4405 	 */
4406 	if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &fattr->fsid)) {
4407 		dprintk("%s: server did not return a different fsid for"
4408 			" a referral at %s\n", __func__, name->name);
4409 		status = -NFS4ERR_MOVED;
4410 		goto out;
4411 	}
4412 	/* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4413 	nfs_fixup_referral_attributes(fattr);
4414 	memset(fhandle, 0, sizeof(struct nfs_fh));
4415 out:
4416 	if (page)
4417 		__free_page(page);
4418 	kfree(locations);
4419 	return status;
4420 }
4421 
_nfs4_proc_getattr(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fattr * fattr,struct inode * inode)4422 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4423 				struct nfs_fattr *fattr, struct inode *inode)
4424 {
4425 	__u32 bitmask[NFS4_BITMASK_SZ];
4426 	struct nfs4_getattr_arg args = {
4427 		.fh = fhandle,
4428 		.bitmask = bitmask,
4429 	};
4430 	struct nfs4_getattr_res res = {
4431 		.fattr = fattr,
4432 		.server = server,
4433 	};
4434 	struct rpc_message msg = {
4435 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4436 		.rpc_argp = &args,
4437 		.rpc_resp = &res,
4438 	};
4439 	unsigned short task_flags = 0;
4440 
4441 	if (nfs4_has_session(server->nfs_client))
4442 		task_flags = RPC_TASK_MOVEABLE;
4443 
4444 	/* Is this is an attribute revalidation, subject to softreval? */
4445 	if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4446 		task_flags |= RPC_TASK_TIMEOUT;
4447 
4448 	nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label), inode, 0);
4449 	nfs_fattr_init(fattr);
4450 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4451 	return nfs4_do_call_sync(server->client, server, &msg,
4452 			&args.seq_args, &res.seq_res, task_flags);
4453 }
4454 
nfs4_proc_getattr(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fattr * fattr,struct inode * inode)4455 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4456 				struct nfs_fattr *fattr, struct inode *inode)
4457 {
4458 	struct nfs4_exception exception = {
4459 		.interruptible = true,
4460 	};
4461 	int err;
4462 	do {
4463 		err = _nfs4_proc_getattr(server, fhandle, fattr, inode);
4464 		trace_nfs4_getattr(server, fhandle, fattr, err);
4465 		err = nfs4_handle_exception(server, err,
4466 				&exception);
4467 	} while (exception.retry);
4468 	return err;
4469 }
4470 
4471 /*
4472  * The file is not closed if it is opened due to the a request to change
4473  * the size of the file. The open call will not be needed once the
4474  * VFS layer lookup-intents are implemented.
4475  *
4476  * Close is called when the inode is destroyed.
4477  * If we haven't opened the file for O_WRONLY, we
4478  * need to in the size_change case to obtain a stateid.
4479  *
4480  * Got race?
4481  * Because OPEN is always done by name in nfsv4, it is
4482  * possible that we opened a different file by the same
4483  * name.  We can recognize this race condition, but we
4484  * can't do anything about it besides returning an error.
4485  *
4486  * This will be fixed with VFS changes (lookup-intent).
4487  */
4488 static int
nfs4_proc_setattr(struct dentry * dentry,struct nfs_fattr * fattr,struct iattr * sattr)4489 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4490 		  struct iattr *sattr)
4491 {
4492 	struct inode *inode = d_inode(dentry);
4493 	const struct cred *cred = NULL;
4494 	struct nfs_open_context *ctx = NULL;
4495 	int status;
4496 
4497 	if (pnfs_ld_layoutret_on_setattr(inode) &&
4498 	    sattr->ia_valid & ATTR_SIZE &&
4499 	    sattr->ia_size < i_size_read(inode))
4500 		pnfs_commit_and_return_layout(inode);
4501 
4502 	nfs_fattr_init(fattr);
4503 
4504 	/* Deal with open(O_TRUNC) */
4505 	if (sattr->ia_valid & ATTR_OPEN)
4506 		sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4507 
4508 	/* Optimization: if the end result is no change, don't RPC */
4509 	if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4510 		return 0;
4511 
4512 	/* Search for an existing open(O_WRITE) file */
4513 	if (sattr->ia_valid & ATTR_FILE) {
4514 
4515 		ctx = nfs_file_open_context(sattr->ia_file);
4516 		if (ctx)
4517 			cred = ctx->cred;
4518 	}
4519 
4520 	/* Return any delegations if we're going to change ACLs */
4521 	if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4522 		nfs4_inode_make_writeable(inode);
4523 
4524 	status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL);
4525 	if (status == 0) {
4526 		nfs_setattr_update_inode(inode, sattr, fattr);
4527 		nfs_setsecurity(inode, fattr);
4528 	}
4529 	return status;
4530 }
4531 
_nfs4_proc_lookup(struct rpc_clnt * clnt,struct inode * dir,struct dentry * dentry,struct nfs_fh * fhandle,struct nfs_fattr * fattr)4532 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4533 		struct dentry *dentry, struct nfs_fh *fhandle,
4534 		struct nfs_fattr *fattr)
4535 {
4536 	struct nfs_server *server = NFS_SERVER(dir);
4537 	int		       status;
4538 	struct nfs4_lookup_arg args = {
4539 		.bitmask = server->attr_bitmask,
4540 		.dir_fh = NFS_FH(dir),
4541 		.name = &dentry->d_name,
4542 	};
4543 	struct nfs4_lookup_res res = {
4544 		.server = server,
4545 		.fattr = fattr,
4546 		.fh = fhandle,
4547 	};
4548 	struct rpc_message msg = {
4549 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4550 		.rpc_argp = &args,
4551 		.rpc_resp = &res,
4552 	};
4553 	unsigned short task_flags = 0;
4554 
4555 	if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
4556 		task_flags = RPC_TASK_MOVEABLE;
4557 
4558 	/* Is this is an attribute revalidation, subject to softreval? */
4559 	if (nfs_lookup_is_soft_revalidate(dentry))
4560 		task_flags |= RPC_TASK_TIMEOUT;
4561 
4562 	args.bitmask = nfs4_bitmask(server, fattr->label);
4563 
4564 	nfs_fattr_init(fattr);
4565 
4566 	dprintk("NFS call  lookup %pd2\n", dentry);
4567 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4568 	status = nfs4_do_call_sync(clnt, server, &msg,
4569 			&args.seq_args, &res.seq_res, task_flags);
4570 	dprintk("NFS reply lookup: %d\n", status);
4571 	return status;
4572 }
4573 
nfs_fixup_secinfo_attributes(struct nfs_fattr * fattr)4574 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4575 {
4576 	fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4577 		NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4578 	fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4579 	fattr->nlink = 2;
4580 }
4581 
nfs4_proc_lookup_common(struct rpc_clnt ** clnt,struct inode * dir,struct dentry * dentry,struct nfs_fh * fhandle,struct nfs_fattr * fattr)4582 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4583 				   struct dentry *dentry, struct nfs_fh *fhandle,
4584 				   struct nfs_fattr *fattr)
4585 {
4586 	struct nfs4_exception exception = {
4587 		.interruptible = true,
4588 	};
4589 	struct rpc_clnt *client = *clnt;
4590 	const struct qstr *name = &dentry->d_name;
4591 	int err;
4592 	do {
4593 		err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr);
4594 		trace_nfs4_lookup(dir, name, err);
4595 		switch (err) {
4596 		case -NFS4ERR_BADNAME:
4597 			err = -ENOENT;
4598 			goto out;
4599 		case -NFS4ERR_MOVED:
4600 			err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4601 			if (err == -NFS4ERR_MOVED)
4602 				err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4603 			goto out;
4604 		case -NFS4ERR_WRONGSEC:
4605 			err = -EPERM;
4606 			if (client != *clnt)
4607 				goto out;
4608 			client = nfs4_negotiate_security(client, dir, name);
4609 			if (IS_ERR(client))
4610 				return PTR_ERR(client);
4611 
4612 			exception.retry = 1;
4613 			break;
4614 		default:
4615 			err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4616 		}
4617 	} while (exception.retry);
4618 
4619 out:
4620 	if (err == 0)
4621 		*clnt = client;
4622 	else if (client != *clnt)
4623 		rpc_shutdown_client(client);
4624 
4625 	return err;
4626 }
4627 
nfs4_proc_lookup(struct inode * dir,struct dentry * dentry,struct nfs_fh * fhandle,struct nfs_fattr * fattr)4628 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4629 			    struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4630 {
4631 	int status;
4632 	struct rpc_clnt *client = NFS_CLIENT(dir);
4633 
4634 	status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4635 	if (client != NFS_CLIENT(dir)) {
4636 		rpc_shutdown_client(client);
4637 		nfs_fixup_secinfo_attributes(fattr);
4638 	}
4639 	return status;
4640 }
4641 
4642 struct rpc_clnt *
nfs4_proc_lookup_mountpoint(struct inode * dir,struct dentry * dentry,struct nfs_fh * fhandle,struct nfs_fattr * fattr)4643 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4644 			    struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4645 {
4646 	struct rpc_clnt *client = NFS_CLIENT(dir);
4647 	int status;
4648 
4649 	status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4650 	if (status < 0)
4651 		return ERR_PTR(status);
4652 	return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4653 }
4654 
_nfs4_proc_lookupp(struct inode * inode,struct nfs_fh * fhandle,struct nfs_fattr * fattr)4655 static int _nfs4_proc_lookupp(struct inode *inode,
4656 		struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4657 {
4658 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
4659 	struct nfs_server *server = NFS_SERVER(inode);
4660 	int		       status;
4661 	struct nfs4_lookupp_arg args = {
4662 		.bitmask = server->attr_bitmask,
4663 		.fh = NFS_FH(inode),
4664 	};
4665 	struct nfs4_lookupp_res res = {
4666 		.server = server,
4667 		.fattr = fattr,
4668 		.fh = fhandle,
4669 	};
4670 	struct rpc_message msg = {
4671 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4672 		.rpc_argp = &args,
4673 		.rpc_resp = &res,
4674 	};
4675 	unsigned short task_flags = 0;
4676 
4677 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4678 		task_flags |= RPC_TASK_TIMEOUT;
4679 
4680 	args.bitmask = nfs4_bitmask(server, fattr->label);
4681 
4682 	nfs_fattr_init(fattr);
4683 
4684 	dprintk("NFS call  lookupp ino=0x%lx\n", inode->i_ino);
4685 	status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4686 				&res.seq_res, task_flags);
4687 	dprintk("NFS reply lookupp: %d\n", status);
4688 	return status;
4689 }
4690 
nfs4_proc_lookupp(struct inode * inode,struct nfs_fh * fhandle,struct nfs_fattr * fattr)4691 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4692 			     struct nfs_fattr *fattr)
4693 {
4694 	struct nfs4_exception exception = {
4695 		.interruptible = true,
4696 	};
4697 	int err;
4698 	do {
4699 		err = _nfs4_proc_lookupp(inode, fhandle, fattr);
4700 		trace_nfs4_lookupp(inode, err);
4701 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
4702 				&exception);
4703 	} while (exception.retry);
4704 	return err;
4705 }
4706 
_nfs4_proc_access(struct inode * inode,struct nfs_access_entry * entry,const struct cred * cred)4707 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4708 			     const struct cred *cred)
4709 {
4710 	struct nfs_server *server = NFS_SERVER(inode);
4711 	struct nfs4_accessargs args = {
4712 		.fh = NFS_FH(inode),
4713 		.access = entry->mask,
4714 	};
4715 	struct nfs4_accessres res = {
4716 		.server = server,
4717 	};
4718 	struct rpc_message msg = {
4719 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4720 		.rpc_argp = &args,
4721 		.rpc_resp = &res,
4722 		.rpc_cred = cred,
4723 	};
4724 	int status = 0;
4725 
4726 	if (!nfs4_have_delegation(inode, FMODE_READ, 0)) {
4727 		res.fattr = nfs_alloc_fattr();
4728 		if (res.fattr == NULL)
4729 			return -ENOMEM;
4730 		args.bitmask = server->cache_consistency_bitmask;
4731 	}
4732 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4733 	if (!status) {
4734 		nfs_access_set_mask(entry, res.access);
4735 		if (res.fattr)
4736 			nfs_refresh_inode(inode, res.fattr);
4737 	}
4738 	nfs_free_fattr(res.fattr);
4739 	return status;
4740 }
4741 
nfs4_proc_access(struct inode * inode,struct nfs_access_entry * entry,const struct cred * cred)4742 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4743 			    const struct cred *cred)
4744 {
4745 	struct nfs4_exception exception = {
4746 		.interruptible = true,
4747 	};
4748 	int err;
4749 	do {
4750 		err = _nfs4_proc_access(inode, entry, cred);
4751 		trace_nfs4_access(inode, err);
4752 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
4753 				&exception);
4754 	} while (exception.retry);
4755 	return err;
4756 }
4757 
4758 /*
4759  * TODO: For the time being, we don't try to get any attributes
4760  * along with any of the zero-copy operations READ, READDIR,
4761  * READLINK, WRITE.
4762  *
4763  * In the case of the first three, we want to put the GETATTR
4764  * after the read-type operation -- this is because it is hard
4765  * to predict the length of a GETATTR response in v4, and thus
4766  * align the READ data correctly.  This means that the GETATTR
4767  * may end up partially falling into the page cache, and we should
4768  * shift it into the 'tail' of the xdr_buf before processing.
4769  * To do this efficiently, we need to know the total length
4770  * of data received, which doesn't seem to be available outside
4771  * of the RPC layer.
4772  *
4773  * In the case of WRITE, we also want to put the GETATTR after
4774  * the operation -- in this case because we want to make sure
4775  * we get the post-operation mtime and size.
4776  *
4777  * Both of these changes to the XDR layer would in fact be quite
4778  * minor, but I decided to leave them for a subsequent patch.
4779  */
_nfs4_proc_readlink(struct inode * inode,struct page * page,unsigned int pgbase,unsigned int pglen)4780 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4781 		unsigned int pgbase, unsigned int pglen)
4782 {
4783 	struct nfs4_readlink args = {
4784 		.fh       = NFS_FH(inode),
4785 		.pgbase	  = pgbase,
4786 		.pglen    = pglen,
4787 		.pages    = &page,
4788 	};
4789 	struct nfs4_readlink_res res;
4790 	struct rpc_message msg = {
4791 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4792 		.rpc_argp = &args,
4793 		.rpc_resp = &res,
4794 	};
4795 
4796 	return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4797 }
4798 
nfs4_proc_readlink(struct inode * inode,struct page * page,unsigned int pgbase,unsigned int pglen)4799 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4800 		unsigned int pgbase, unsigned int pglen)
4801 {
4802 	struct nfs4_exception exception = {
4803 		.interruptible = true,
4804 	};
4805 	int err;
4806 	do {
4807 		err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4808 		trace_nfs4_readlink(inode, err);
4809 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
4810 				&exception);
4811 	} while (exception.retry);
4812 	return err;
4813 }
4814 
4815 /*
4816  * This is just for mknod.  open(O_CREAT) will always do ->open_context().
4817  */
4818 static int
nfs4_proc_create(struct inode * dir,struct dentry * dentry,struct iattr * sattr,int flags)4819 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4820 		 int flags)
4821 {
4822 	struct nfs_server *server = NFS_SERVER(dir);
4823 	struct nfs4_label l, *ilabel;
4824 	struct nfs_open_context *ctx;
4825 	struct nfs4_state *state;
4826 	int status = 0;
4827 
4828 	ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4829 	if (IS_ERR(ctx))
4830 		return PTR_ERR(ctx);
4831 
4832 	ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4833 
4834 	if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4835 		sattr->ia_mode &= ~current_umask();
4836 	state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4837 	if (IS_ERR(state)) {
4838 		status = PTR_ERR(state);
4839 		goto out;
4840 	}
4841 out:
4842 	nfs4_label_release_security(ilabel);
4843 	put_nfs_open_context(ctx);
4844 	return status;
4845 }
4846 
4847 static int
_nfs4_proc_remove(struct inode * dir,const struct qstr * name,u32 ftype)4848 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4849 {
4850 	struct nfs_server *server = NFS_SERVER(dir);
4851 	struct nfs_removeargs args = {
4852 		.fh = NFS_FH(dir),
4853 		.name = *name,
4854 	};
4855 	struct nfs_removeres res = {
4856 		.server = server,
4857 	};
4858 	struct rpc_message msg = {
4859 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4860 		.rpc_argp = &args,
4861 		.rpc_resp = &res,
4862 	};
4863 	unsigned long timestamp = jiffies;
4864 	int status;
4865 
4866 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4867 	if (status == 0) {
4868 		spin_lock(&dir->i_lock);
4869 		/* Removing a directory decrements nlink in the parent */
4870 		if (ftype == NF4DIR && dir->i_nlink > 2)
4871 			nfs4_dec_nlink_locked(dir);
4872 		nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4873 					      NFS_INO_INVALID_DATA);
4874 		spin_unlock(&dir->i_lock);
4875 	}
4876 	return status;
4877 }
4878 
nfs4_proc_remove(struct inode * dir,struct dentry * dentry)4879 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4880 {
4881 	struct nfs4_exception exception = {
4882 		.interruptible = true,
4883 	};
4884 	struct inode *inode = d_inode(dentry);
4885 	int err;
4886 
4887 	if (inode) {
4888 		if (inode->i_nlink == 1)
4889 			nfs4_inode_return_delegation(inode);
4890 		else
4891 			nfs4_inode_make_writeable(inode);
4892 	}
4893 	do {
4894 		err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4895 		trace_nfs4_remove(dir, &dentry->d_name, err);
4896 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
4897 				&exception);
4898 	} while (exception.retry);
4899 	return err;
4900 }
4901 
nfs4_proc_rmdir(struct inode * dir,const struct qstr * name)4902 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4903 {
4904 	struct nfs4_exception exception = {
4905 		.interruptible = true,
4906 	};
4907 	int err;
4908 
4909 	do {
4910 		err = _nfs4_proc_remove(dir, name, NF4DIR);
4911 		trace_nfs4_remove(dir, name, err);
4912 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
4913 				&exception);
4914 	} while (exception.retry);
4915 	return err;
4916 }
4917 
nfs4_proc_unlink_setup(struct rpc_message * msg,struct dentry * dentry,struct inode * inode)4918 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4919 		struct dentry *dentry,
4920 		struct inode *inode)
4921 {
4922 	struct nfs_removeargs *args = msg->rpc_argp;
4923 	struct nfs_removeres *res = msg->rpc_resp;
4924 
4925 	res->server = NFS_SB(dentry->d_sb);
4926 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4927 	nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4928 
4929 	nfs_fattr_init(res->dir_attr);
4930 
4931 	if (inode) {
4932 		nfs4_inode_return_delegation(inode);
4933 		nfs_d_prune_case_insensitive_aliases(inode);
4934 	}
4935 }
4936 
nfs4_proc_unlink_rpc_prepare(struct rpc_task * task,struct nfs_unlinkdata * data)4937 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4938 {
4939 	nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4940 			&data->args.seq_args,
4941 			&data->res.seq_res,
4942 			task);
4943 }
4944 
nfs4_proc_unlink_done(struct rpc_task * task,struct inode * dir)4945 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4946 {
4947 	struct nfs_unlinkdata *data = task->tk_calldata;
4948 	struct nfs_removeres *res = &data->res;
4949 
4950 	if (!nfs4_sequence_done(task, &res->seq_res))
4951 		return 0;
4952 	if (nfs4_async_handle_error(task, res->server, NULL,
4953 				    &data->timeout) == -EAGAIN)
4954 		return 0;
4955 	if (task->tk_status == 0)
4956 		nfs4_update_changeattr(dir, &res->cinfo,
4957 				res->dir_attr->time_start,
4958 				NFS_INO_INVALID_DATA);
4959 	return 1;
4960 }
4961 
nfs4_proc_rename_setup(struct rpc_message * msg,struct dentry * old_dentry,struct dentry * new_dentry)4962 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4963 		struct dentry *old_dentry,
4964 		struct dentry *new_dentry)
4965 {
4966 	struct nfs_renameargs *arg = msg->rpc_argp;
4967 	struct nfs_renameres *res = msg->rpc_resp;
4968 	struct inode *old_inode = d_inode(old_dentry);
4969 	struct inode *new_inode = d_inode(new_dentry);
4970 
4971 	if (old_inode)
4972 		nfs4_inode_make_writeable(old_inode);
4973 	if (new_inode)
4974 		nfs4_inode_return_delegation(new_inode);
4975 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4976 	res->server = NFS_SB(old_dentry->d_sb);
4977 	nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4978 }
4979 
nfs4_proc_rename_rpc_prepare(struct rpc_task * task,struct nfs_renamedata * data)4980 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4981 {
4982 	nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4983 			&data->args.seq_args,
4984 			&data->res.seq_res,
4985 			task);
4986 }
4987 
nfs4_proc_rename_done(struct rpc_task * task,struct inode * old_dir,struct inode * new_dir)4988 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4989 				 struct inode *new_dir)
4990 {
4991 	struct nfs_renamedata *data = task->tk_calldata;
4992 	struct nfs_renameres *res = &data->res;
4993 
4994 	if (!nfs4_sequence_done(task, &res->seq_res))
4995 		return 0;
4996 	if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
4997 		return 0;
4998 
4999 	if (task->tk_status == 0) {
5000 		nfs_d_prune_case_insensitive_aliases(d_inode(data->old_dentry));
5001 		if (new_dir != old_dir) {
5002 			/* Note: If we moved a directory, nlink will change */
5003 			nfs4_update_changeattr(old_dir, &res->old_cinfo,
5004 					res->old_fattr->time_start,
5005 					NFS_INO_INVALID_NLINK |
5006 					    NFS_INO_INVALID_DATA);
5007 			nfs4_update_changeattr(new_dir, &res->new_cinfo,
5008 					res->new_fattr->time_start,
5009 					NFS_INO_INVALID_NLINK |
5010 					    NFS_INO_INVALID_DATA);
5011 		} else
5012 			nfs4_update_changeattr(old_dir, &res->old_cinfo,
5013 					res->old_fattr->time_start,
5014 					NFS_INO_INVALID_DATA);
5015 	}
5016 	return 1;
5017 }
5018 
_nfs4_proc_link(struct inode * inode,struct inode * dir,const struct qstr * name)5019 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
5020 {
5021 	struct nfs_server *server = NFS_SERVER(inode);
5022 	__u32 bitmask[NFS4_BITMASK_SZ];
5023 	struct nfs4_link_arg arg = {
5024 		.fh     = NFS_FH(inode),
5025 		.dir_fh = NFS_FH(dir),
5026 		.name   = name,
5027 		.bitmask = bitmask,
5028 	};
5029 	struct nfs4_link_res res = {
5030 		.server = server,
5031 	};
5032 	struct rpc_message msg = {
5033 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
5034 		.rpc_argp = &arg,
5035 		.rpc_resp = &res,
5036 	};
5037 	int status = -ENOMEM;
5038 
5039 	res.fattr = nfs_alloc_fattr_with_label(server);
5040 	if (res.fattr == NULL)
5041 		goto out;
5042 
5043 	nfs4_inode_make_writeable(inode);
5044 	nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.fattr->label),
5045 				inode,
5046 				NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME);
5047 	status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5048 	if (!status) {
5049 		nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
5050 				       NFS_INO_INVALID_DATA);
5051 		nfs4_inc_nlink(inode);
5052 		status = nfs_post_op_update_inode(inode, res.fattr);
5053 		if (!status)
5054 			nfs_setsecurity(inode, res.fattr);
5055 	}
5056 
5057 out:
5058 	nfs_free_fattr(res.fattr);
5059 	return status;
5060 }
5061 
nfs4_proc_link(struct inode * inode,struct inode * dir,const struct qstr * name)5062 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
5063 {
5064 	struct nfs4_exception exception = {
5065 		.interruptible = true,
5066 	};
5067 	int err;
5068 	do {
5069 		err = nfs4_handle_exception(NFS_SERVER(inode),
5070 				_nfs4_proc_link(inode, dir, name),
5071 				&exception);
5072 	} while (exception.retry);
5073 	return err;
5074 }
5075 
5076 struct nfs4_createdata {
5077 	struct rpc_message msg;
5078 	struct nfs4_create_arg arg;
5079 	struct nfs4_create_res res;
5080 	struct nfs_fh fh;
5081 	struct nfs_fattr fattr;
5082 };
5083 
nfs4_alloc_createdata(struct inode * dir,const struct qstr * name,struct iattr * sattr,u32 ftype)5084 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
5085 		const struct qstr *name, struct iattr *sattr, u32 ftype)
5086 {
5087 	struct nfs4_createdata *data;
5088 
5089 	data = kzalloc(sizeof(*data), GFP_KERNEL);
5090 	if (data != NULL) {
5091 		struct nfs_server *server = NFS_SERVER(dir);
5092 
5093 		data->fattr.label = nfs4_label_alloc(server, GFP_KERNEL);
5094 		if (IS_ERR(data->fattr.label))
5095 			goto out_free;
5096 
5097 		data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
5098 		data->msg.rpc_argp = &data->arg;
5099 		data->msg.rpc_resp = &data->res;
5100 		data->arg.dir_fh = NFS_FH(dir);
5101 		data->arg.server = server;
5102 		data->arg.name = name;
5103 		data->arg.attrs = sattr;
5104 		data->arg.ftype = ftype;
5105 		data->arg.bitmask = nfs4_bitmask(server, data->fattr.label);
5106 		data->arg.umask = current_umask();
5107 		data->res.server = server;
5108 		data->res.fh = &data->fh;
5109 		data->res.fattr = &data->fattr;
5110 		nfs_fattr_init(data->res.fattr);
5111 	}
5112 	return data;
5113 out_free:
5114 	kfree(data);
5115 	return NULL;
5116 }
5117 
nfs4_do_create(struct inode * dir,struct dentry * dentry,struct nfs4_createdata * data)5118 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
5119 {
5120 	int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
5121 				    &data->arg.seq_args, &data->res.seq_res, 1);
5122 	if (status == 0) {
5123 		spin_lock(&dir->i_lock);
5124 		/* Creating a directory bumps nlink in the parent */
5125 		if (data->arg.ftype == NF4DIR)
5126 			nfs4_inc_nlink_locked(dir);
5127 		nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
5128 					      data->res.fattr->time_start,
5129 					      NFS_INO_INVALID_DATA);
5130 		spin_unlock(&dir->i_lock);
5131 		status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
5132 	}
5133 	return status;
5134 }
5135 
nfs4_free_createdata(struct nfs4_createdata * data)5136 static void nfs4_free_createdata(struct nfs4_createdata *data)
5137 {
5138 	nfs4_label_free(data->fattr.label);
5139 	kfree(data);
5140 }
5141 
_nfs4_proc_symlink(struct inode * dir,struct dentry * dentry,struct folio * folio,unsigned int len,struct iattr * sattr,struct nfs4_label * label)5142 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5143 		struct folio *folio, unsigned int len, struct iattr *sattr,
5144 		struct nfs4_label *label)
5145 {
5146 	struct page *page = &folio->page;
5147 	struct nfs4_createdata *data;
5148 	int status = -ENAMETOOLONG;
5149 
5150 	if (len > NFS4_MAXPATHLEN)
5151 		goto out;
5152 
5153 	status = -ENOMEM;
5154 	data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
5155 	if (data == NULL)
5156 		goto out;
5157 
5158 	data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
5159 	data->arg.u.symlink.pages = &page;
5160 	data->arg.u.symlink.len = len;
5161 	data->arg.label = label;
5162 
5163 	status = nfs4_do_create(dir, dentry, data);
5164 
5165 	nfs4_free_createdata(data);
5166 out:
5167 	return status;
5168 }
5169 
nfs4_proc_symlink(struct inode * dir,struct dentry * dentry,struct folio * folio,unsigned int len,struct iattr * sattr)5170 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5171 		struct folio *folio, unsigned int len, struct iattr *sattr)
5172 {
5173 	struct nfs4_exception exception = {
5174 		.interruptible = true,
5175 	};
5176 	struct nfs4_label l, *label;
5177 	int err;
5178 
5179 	label = nfs4_label_init_security(dir, dentry, sattr, &l);
5180 
5181 	do {
5182 		err = _nfs4_proc_symlink(dir, dentry, folio, len, sattr, label);
5183 		trace_nfs4_symlink(dir, &dentry->d_name, err);
5184 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
5185 				&exception);
5186 	} while (exception.retry);
5187 
5188 	nfs4_label_release_security(label);
5189 	return err;
5190 }
5191 
_nfs4_proc_mkdir(struct inode * dir,struct dentry * dentry,struct iattr * sattr,struct nfs4_label * label)5192 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5193 		struct iattr *sattr, struct nfs4_label *label)
5194 {
5195 	struct nfs4_createdata *data;
5196 	int status = -ENOMEM;
5197 
5198 	data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
5199 	if (data == NULL)
5200 		goto out;
5201 
5202 	data->arg.label = label;
5203 	status = nfs4_do_create(dir, dentry, data);
5204 
5205 	nfs4_free_createdata(data);
5206 out:
5207 	return status;
5208 }
5209 
nfs4_proc_mkdir(struct inode * dir,struct dentry * dentry,struct iattr * sattr)5210 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5211 		struct iattr *sattr)
5212 {
5213 	struct nfs_server *server = NFS_SERVER(dir);
5214 	struct nfs4_exception exception = {
5215 		.interruptible = true,
5216 	};
5217 	struct nfs4_label l, *label;
5218 	int err;
5219 
5220 	label = nfs4_label_init_security(dir, dentry, sattr, &l);
5221 
5222 	if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5223 		sattr->ia_mode &= ~current_umask();
5224 	do {
5225 		err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
5226 		trace_nfs4_mkdir(dir, &dentry->d_name, err);
5227 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
5228 				&exception);
5229 	} while (exception.retry);
5230 	nfs4_label_release_security(label);
5231 
5232 	return err;
5233 }
5234 
_nfs4_proc_readdir(struct nfs_readdir_arg * nr_arg,struct nfs_readdir_res * nr_res)5235 static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
5236 			      struct nfs_readdir_res *nr_res)
5237 {
5238 	struct inode		*dir = d_inode(nr_arg->dentry);
5239 	struct nfs_server	*server = NFS_SERVER(dir);
5240 	struct nfs4_readdir_arg args = {
5241 		.fh = NFS_FH(dir),
5242 		.pages = nr_arg->pages,
5243 		.pgbase = 0,
5244 		.count = nr_arg->page_len,
5245 		.plus = nr_arg->plus,
5246 	};
5247 	struct nfs4_readdir_res res;
5248 	struct rpc_message msg = {
5249 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5250 		.rpc_argp = &args,
5251 		.rpc_resp = &res,
5252 		.rpc_cred = nr_arg->cred,
5253 	};
5254 	int			status;
5255 
5256 	dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
5257 		nr_arg->dentry, (unsigned long long)nr_arg->cookie);
5258 	if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5259 		args.bitmask = server->attr_bitmask_nl;
5260 	else
5261 		args.bitmask = server->attr_bitmask;
5262 
5263 	nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
5264 	res.pgbase = args.pgbase;
5265 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5266 			&res.seq_res, 0);
5267 	if (status >= 0) {
5268 		memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
5269 		status += args.pgbase;
5270 	}
5271 
5272 	nfs_invalidate_atime(dir);
5273 
5274 	dprintk("%s: returns %d\n", __func__, status);
5275 	return status;
5276 }
5277 
nfs4_proc_readdir(struct nfs_readdir_arg * arg,struct nfs_readdir_res * res)5278 static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5279 			     struct nfs_readdir_res *res)
5280 {
5281 	struct nfs4_exception exception = {
5282 		.interruptible = true,
5283 	};
5284 	int err;
5285 	do {
5286 		err = _nfs4_proc_readdir(arg, res);
5287 		trace_nfs4_readdir(d_inode(arg->dentry), err);
5288 		err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5289 					    err, &exception);
5290 	} while (exception.retry);
5291 	return err;
5292 }
5293 
_nfs4_proc_mknod(struct inode * dir,struct dentry * dentry,struct iattr * sattr,struct nfs4_label * label,dev_t rdev)5294 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5295 		struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5296 {
5297 	struct nfs4_createdata *data;
5298 	int mode = sattr->ia_mode;
5299 	int status = -ENOMEM;
5300 
5301 	data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5302 	if (data == NULL)
5303 		goto out;
5304 
5305 	if (S_ISFIFO(mode))
5306 		data->arg.ftype = NF4FIFO;
5307 	else if (S_ISBLK(mode)) {
5308 		data->arg.ftype = NF4BLK;
5309 		data->arg.u.device.specdata1 = MAJOR(rdev);
5310 		data->arg.u.device.specdata2 = MINOR(rdev);
5311 	}
5312 	else if (S_ISCHR(mode)) {
5313 		data->arg.ftype = NF4CHR;
5314 		data->arg.u.device.specdata1 = MAJOR(rdev);
5315 		data->arg.u.device.specdata2 = MINOR(rdev);
5316 	} else if (!S_ISSOCK(mode)) {
5317 		status = -EINVAL;
5318 		goto out_free;
5319 	}
5320 
5321 	data->arg.label = label;
5322 	status = nfs4_do_create(dir, dentry, data);
5323 out_free:
5324 	nfs4_free_createdata(data);
5325 out:
5326 	return status;
5327 }
5328 
nfs4_proc_mknod(struct inode * dir,struct dentry * dentry,struct iattr * sattr,dev_t rdev)5329 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5330 		struct iattr *sattr, dev_t rdev)
5331 {
5332 	struct nfs_server *server = NFS_SERVER(dir);
5333 	struct nfs4_exception exception = {
5334 		.interruptible = true,
5335 	};
5336 	struct nfs4_label l, *label;
5337 	int err;
5338 
5339 	label = nfs4_label_init_security(dir, dentry, sattr, &l);
5340 
5341 	if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5342 		sattr->ia_mode &= ~current_umask();
5343 	do {
5344 		err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5345 		trace_nfs4_mknod(dir, &dentry->d_name, err);
5346 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
5347 				&exception);
5348 	} while (exception.retry);
5349 
5350 	nfs4_label_release_security(label);
5351 
5352 	return err;
5353 }
5354 
_nfs4_proc_statfs(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsstat * fsstat)5355 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5356 		 struct nfs_fsstat *fsstat)
5357 {
5358 	struct nfs4_statfs_arg args = {
5359 		.fh = fhandle,
5360 		.bitmask = server->attr_bitmask,
5361 	};
5362 	struct nfs4_statfs_res res = {
5363 		.fsstat = fsstat,
5364 	};
5365 	struct rpc_message msg = {
5366 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5367 		.rpc_argp = &args,
5368 		.rpc_resp = &res,
5369 	};
5370 
5371 	nfs_fattr_init(fsstat->fattr);
5372 	return  nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5373 }
5374 
nfs4_proc_statfs(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsstat * fsstat)5375 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5376 {
5377 	struct nfs4_exception exception = {
5378 		.interruptible = true,
5379 	};
5380 	int err;
5381 	do {
5382 		err = nfs4_handle_exception(server,
5383 				_nfs4_proc_statfs(server, fhandle, fsstat),
5384 				&exception);
5385 	} while (exception.retry);
5386 	return err;
5387 }
5388 
_nfs4_do_fsinfo(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * fsinfo)5389 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5390 		struct nfs_fsinfo *fsinfo)
5391 {
5392 	struct nfs4_fsinfo_arg args = {
5393 		.fh = fhandle,
5394 		.bitmask = server->attr_bitmask,
5395 	};
5396 	struct nfs4_fsinfo_res res = {
5397 		.fsinfo = fsinfo,
5398 	};
5399 	struct rpc_message msg = {
5400 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5401 		.rpc_argp = &args,
5402 		.rpc_resp = &res,
5403 	};
5404 
5405 	return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5406 }
5407 
nfs4_do_fsinfo(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * fsinfo)5408 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5409 {
5410 	struct nfs4_exception exception = {
5411 		.interruptible = true,
5412 	};
5413 	int err;
5414 
5415 	do {
5416 		err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5417 		trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5418 		if (err == 0) {
5419 			nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5420 			break;
5421 		}
5422 		err = nfs4_handle_exception(server, err, &exception);
5423 	} while (exception.retry);
5424 	return err;
5425 }
5426 
nfs4_proc_fsinfo(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * fsinfo)5427 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5428 {
5429 	int error;
5430 
5431 	nfs_fattr_init(fsinfo->fattr);
5432 	error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5433 	if (error == 0) {
5434 		/* block layout checks this! */
5435 		server->pnfs_blksize = fsinfo->blksize;
5436 		set_pnfs_layoutdriver(server, fhandle, fsinfo);
5437 	}
5438 
5439 	return error;
5440 }
5441 
_nfs4_proc_pathconf(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_pathconf * pathconf)5442 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5443 		struct nfs_pathconf *pathconf)
5444 {
5445 	struct nfs4_pathconf_arg args = {
5446 		.fh = fhandle,
5447 		.bitmask = server->attr_bitmask,
5448 	};
5449 	struct nfs4_pathconf_res res = {
5450 		.pathconf = pathconf,
5451 	};
5452 	struct rpc_message msg = {
5453 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5454 		.rpc_argp = &args,
5455 		.rpc_resp = &res,
5456 	};
5457 
5458 	/* None of the pathconf attributes are mandatory to implement */
5459 	if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5460 		memset(pathconf, 0, sizeof(*pathconf));
5461 		return 0;
5462 	}
5463 
5464 	nfs_fattr_init(pathconf->fattr);
5465 	return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5466 }
5467 
nfs4_proc_pathconf(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_pathconf * pathconf)5468 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5469 		struct nfs_pathconf *pathconf)
5470 {
5471 	struct nfs4_exception exception = {
5472 		.interruptible = true,
5473 	};
5474 	int err;
5475 
5476 	do {
5477 		err = nfs4_handle_exception(server,
5478 				_nfs4_proc_pathconf(server, fhandle, pathconf),
5479 				&exception);
5480 	} while (exception.retry);
5481 	return err;
5482 }
5483 
nfs4_set_rw_stateid(nfs4_stateid * stateid,const struct nfs_open_context * ctx,const struct nfs_lock_context * l_ctx,fmode_t fmode)5484 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5485 		const struct nfs_open_context *ctx,
5486 		const struct nfs_lock_context *l_ctx,
5487 		fmode_t fmode)
5488 {
5489 	return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5490 }
5491 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5492 
nfs4_stateid_is_current(nfs4_stateid * stateid,const struct nfs_open_context * ctx,const struct nfs_lock_context * l_ctx,fmode_t fmode)5493 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5494 		const struct nfs_open_context *ctx,
5495 		const struct nfs_lock_context *l_ctx,
5496 		fmode_t fmode)
5497 {
5498 	nfs4_stateid _current_stateid;
5499 
5500 	/* If the current stateid represents a lost lock, then exit */
5501 	if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5502 		return true;
5503 	return nfs4_stateid_match(stateid, &_current_stateid);
5504 }
5505 
nfs4_error_stateid_expired(int err)5506 static bool nfs4_error_stateid_expired(int err)
5507 {
5508 	switch (err) {
5509 	case -NFS4ERR_DELEG_REVOKED:
5510 	case -NFS4ERR_ADMIN_REVOKED:
5511 	case -NFS4ERR_BAD_STATEID:
5512 	case -NFS4ERR_STALE_STATEID:
5513 	case -NFS4ERR_OLD_STATEID:
5514 	case -NFS4ERR_OPENMODE:
5515 	case -NFS4ERR_EXPIRED:
5516 		return true;
5517 	}
5518 	return false;
5519 }
5520 
nfs4_read_done_cb(struct rpc_task * task,struct nfs_pgio_header * hdr)5521 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5522 {
5523 	struct nfs_server *server = NFS_SERVER(hdr->inode);
5524 
5525 	trace_nfs4_read(hdr, task->tk_status);
5526 	if (task->tk_status < 0) {
5527 		struct nfs4_exception exception = {
5528 			.inode = hdr->inode,
5529 			.state = hdr->args.context->state,
5530 			.stateid = &hdr->args.stateid,
5531 		};
5532 		task->tk_status = nfs4_async_handle_exception(task,
5533 				server, task->tk_status, &exception);
5534 		if (exception.retry) {
5535 			rpc_restart_call_prepare(task);
5536 			return -EAGAIN;
5537 		}
5538 	}
5539 
5540 	if (task->tk_status > 0)
5541 		renew_lease(server, hdr->timestamp);
5542 	return 0;
5543 }
5544 
nfs4_read_stateid_changed(struct rpc_task * task,struct nfs_pgio_args * args)5545 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5546 		struct nfs_pgio_args *args)
5547 {
5548 
5549 	if (!nfs4_error_stateid_expired(task->tk_status) ||
5550 		nfs4_stateid_is_current(&args->stateid,
5551 				args->context,
5552 				args->lock_context,
5553 				FMODE_READ))
5554 		return false;
5555 	rpc_restart_call_prepare(task);
5556 	return true;
5557 }
5558 
nfs4_read_plus_not_supported(struct rpc_task * task,struct nfs_pgio_header * hdr)5559 static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5560 					 struct nfs_pgio_header *hdr)
5561 {
5562 	struct nfs_server *server = NFS_SERVER(hdr->inode);
5563 	struct rpc_message *msg = &task->tk_msg;
5564 
5565 	if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5566 	    task->tk_status == -ENOTSUPP) {
5567 		server->caps &= ~NFS_CAP_READ_PLUS;
5568 		msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5569 		rpc_restart_call_prepare(task);
5570 		return true;
5571 	}
5572 	return false;
5573 }
5574 
nfs4_read_done(struct rpc_task * task,struct nfs_pgio_header * hdr)5575 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5576 {
5577 	if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5578 		return -EAGAIN;
5579 	if (nfs4_read_stateid_changed(task, &hdr->args))
5580 		return -EAGAIN;
5581 	if (nfs4_read_plus_not_supported(task, hdr))
5582 		return -EAGAIN;
5583 	if (task->tk_status > 0)
5584 		nfs_invalidate_atime(hdr->inode);
5585 	return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5586 				    nfs4_read_done_cb(task, hdr);
5587 }
5588 
5589 #if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS
nfs42_read_plus_support(struct nfs_pgio_header * hdr,struct rpc_message * msg)5590 static bool nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5591 				    struct rpc_message *msg)
5592 {
5593 	/* Note: We don't use READ_PLUS with pNFS yet */
5594 	if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp) {
5595 		msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5596 		return nfs_read_alloc_scratch(hdr, READ_PLUS_SCRATCH_SIZE);
5597 	}
5598 	return false;
5599 }
5600 #else
nfs42_read_plus_support(struct nfs_pgio_header * hdr,struct rpc_message * msg)5601 static bool nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5602 				    struct rpc_message *msg)
5603 {
5604 	return false;
5605 }
5606 #endif /* CONFIG_NFS_V4_2 */
5607 
nfs4_proc_read_setup(struct nfs_pgio_header * hdr,struct rpc_message * msg)5608 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5609 				 struct rpc_message *msg)
5610 {
5611 	hdr->timestamp   = jiffies;
5612 	if (!hdr->pgio_done_cb)
5613 		hdr->pgio_done_cb = nfs4_read_done_cb;
5614 	if (!nfs42_read_plus_support(hdr, msg))
5615 		msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5616 	nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5617 }
5618 
nfs4_proc_pgio_rpc_prepare(struct rpc_task * task,struct nfs_pgio_header * hdr)5619 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5620 				      struct nfs_pgio_header *hdr)
5621 {
5622 	if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5623 			&hdr->args.seq_args,
5624 			&hdr->res.seq_res,
5625 			task))
5626 		return 0;
5627 	if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5628 				hdr->args.lock_context,
5629 				hdr->rw_mode) == -EIO)
5630 		return -EIO;
5631 	if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5632 		return -EIO;
5633 	return 0;
5634 }
5635 
nfs4_write_done_cb(struct rpc_task * task,struct nfs_pgio_header * hdr)5636 static int nfs4_write_done_cb(struct rpc_task *task,
5637 			      struct nfs_pgio_header *hdr)
5638 {
5639 	struct inode *inode = hdr->inode;
5640 
5641 	trace_nfs4_write(hdr, task->tk_status);
5642 	if (task->tk_status < 0) {
5643 		struct nfs4_exception exception = {
5644 			.inode = hdr->inode,
5645 			.state = hdr->args.context->state,
5646 			.stateid = &hdr->args.stateid,
5647 		};
5648 		task->tk_status = nfs4_async_handle_exception(task,
5649 				NFS_SERVER(inode), task->tk_status,
5650 				&exception);
5651 		if (exception.retry) {
5652 			rpc_restart_call_prepare(task);
5653 			return -EAGAIN;
5654 		}
5655 	}
5656 	if (task->tk_status >= 0) {
5657 		renew_lease(NFS_SERVER(inode), hdr->timestamp);
5658 		nfs_writeback_update_inode(hdr);
5659 	}
5660 	return 0;
5661 }
5662 
nfs4_write_stateid_changed(struct rpc_task * task,struct nfs_pgio_args * args)5663 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5664 		struct nfs_pgio_args *args)
5665 {
5666 
5667 	if (!nfs4_error_stateid_expired(task->tk_status) ||
5668 		nfs4_stateid_is_current(&args->stateid,
5669 				args->context,
5670 				args->lock_context,
5671 				FMODE_WRITE))
5672 		return false;
5673 	rpc_restart_call_prepare(task);
5674 	return true;
5675 }
5676 
nfs4_write_done(struct rpc_task * task,struct nfs_pgio_header * hdr)5677 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5678 {
5679 	if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5680 		return -EAGAIN;
5681 	if (nfs4_write_stateid_changed(task, &hdr->args))
5682 		return -EAGAIN;
5683 	return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5684 		nfs4_write_done_cb(task, hdr);
5685 }
5686 
5687 static
nfs4_write_need_cache_consistency_data(struct nfs_pgio_header * hdr)5688 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5689 {
5690 	/* Don't request attributes for pNFS or O_DIRECT writes */
5691 	if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5692 		return false;
5693 	/* Otherwise, request attributes if and only if we don't hold
5694 	 * a delegation
5695 	 */
5696 	return nfs4_have_delegation(hdr->inode, FMODE_READ, 0) == 0;
5697 }
5698 
nfs4_bitmask_set(__u32 bitmask[],const __u32 src[],struct inode * inode,unsigned long cache_validity)5699 void nfs4_bitmask_set(__u32 bitmask[], const __u32 src[],
5700 		      struct inode *inode, unsigned long cache_validity)
5701 {
5702 	struct nfs_server *server = NFS_SERVER(inode);
5703 	unsigned int i;
5704 
5705 	memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
5706 	cache_validity |= READ_ONCE(NFS_I(inode)->cache_validity);
5707 
5708 	if (cache_validity & NFS_INO_INVALID_CHANGE)
5709 		bitmask[0] |= FATTR4_WORD0_CHANGE;
5710 	if (cache_validity & NFS_INO_INVALID_ATIME)
5711 		bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5712 	if (cache_validity & NFS_INO_INVALID_MODE)
5713 		bitmask[1] |= FATTR4_WORD1_MODE;
5714 	if (cache_validity & NFS_INO_INVALID_OTHER)
5715 		bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
5716 	if (cache_validity & NFS_INO_INVALID_NLINK)
5717 		bitmask[1] |= FATTR4_WORD1_NUMLINKS;
5718 	if (cache_validity & NFS_INO_INVALID_CTIME)
5719 		bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
5720 	if (cache_validity & NFS_INO_INVALID_MTIME)
5721 		bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5722 	if (cache_validity & NFS_INO_INVALID_BLOCKS)
5723 		bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5724 
5725 	if (cache_validity & NFS_INO_INVALID_SIZE)
5726 		bitmask[0] |= FATTR4_WORD0_SIZE;
5727 
5728 	for (i = 0; i < NFS4_BITMASK_SZ; i++)
5729 		bitmask[i] &= server->attr_bitmask[i];
5730 }
5731 
nfs4_proc_write_setup(struct nfs_pgio_header * hdr,struct rpc_message * msg,struct rpc_clnt ** clnt)5732 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5733 				  struct rpc_message *msg,
5734 				  struct rpc_clnt **clnt)
5735 {
5736 	struct nfs_server *server = NFS_SERVER(hdr->inode);
5737 
5738 	if (!nfs4_write_need_cache_consistency_data(hdr)) {
5739 		hdr->args.bitmask = NULL;
5740 		hdr->res.fattr = NULL;
5741 	} else {
5742 		nfs4_bitmask_set(hdr->args.bitmask_store,
5743 				 server->cache_consistency_bitmask,
5744 				 hdr->inode, NFS_INO_INVALID_BLOCKS);
5745 		hdr->args.bitmask = hdr->args.bitmask_store;
5746 	}
5747 
5748 	if (!hdr->pgio_done_cb)
5749 		hdr->pgio_done_cb = nfs4_write_done_cb;
5750 	hdr->res.server = server;
5751 	hdr->timestamp   = jiffies;
5752 
5753 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5754 	nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5755 	nfs4_state_protect_write(hdr->ds_clp ? hdr->ds_clp : server->nfs_client, clnt, msg, hdr);
5756 }
5757 
nfs4_proc_commit_rpc_prepare(struct rpc_task * task,struct nfs_commit_data * data)5758 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5759 {
5760 	nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5761 			&data->args.seq_args,
5762 			&data->res.seq_res,
5763 			task);
5764 }
5765 
nfs4_commit_done_cb(struct rpc_task * task,struct nfs_commit_data * data)5766 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5767 {
5768 	struct inode *inode = data->inode;
5769 
5770 	trace_nfs4_commit(data, task->tk_status);
5771 	if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5772 				    NULL, NULL) == -EAGAIN) {
5773 		rpc_restart_call_prepare(task);
5774 		return -EAGAIN;
5775 	}
5776 	return 0;
5777 }
5778 
nfs4_commit_done(struct rpc_task * task,struct nfs_commit_data * data)5779 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5780 {
5781 	if (!nfs4_sequence_done(task, &data->res.seq_res))
5782 		return -EAGAIN;
5783 	return data->commit_done_cb(task, data);
5784 }
5785 
nfs4_proc_commit_setup(struct nfs_commit_data * data,struct rpc_message * msg,struct rpc_clnt ** clnt)5786 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5787 				   struct rpc_clnt **clnt)
5788 {
5789 	struct nfs_server *server = NFS_SERVER(data->inode);
5790 
5791 	if (data->commit_done_cb == NULL)
5792 		data->commit_done_cb = nfs4_commit_done_cb;
5793 	data->res.server = server;
5794 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5795 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5796 	nfs4_state_protect(data->ds_clp ? data->ds_clp : server->nfs_client,
5797 			NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5798 }
5799 
_nfs4_proc_commit(struct file * dst,struct nfs_commitargs * args,struct nfs_commitres * res)5800 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5801 				struct nfs_commitres *res)
5802 {
5803 	struct inode *dst_inode = file_inode(dst);
5804 	struct nfs_server *server = NFS_SERVER(dst_inode);
5805 	struct rpc_message msg = {
5806 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5807 		.rpc_argp = args,
5808 		.rpc_resp = res,
5809 	};
5810 
5811 	args->fh = NFS_FH(dst_inode);
5812 	return nfs4_call_sync(server->client, server, &msg,
5813 			&args->seq_args, &res->seq_res, 1);
5814 }
5815 
nfs4_proc_commit(struct file * dst,__u64 offset,__u32 count,struct nfs_commitres * res)5816 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5817 {
5818 	struct nfs_commitargs args = {
5819 		.offset = offset,
5820 		.count = count,
5821 	};
5822 	struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5823 	struct nfs4_exception exception = { };
5824 	int status;
5825 
5826 	do {
5827 		status = _nfs4_proc_commit(dst, &args, res);
5828 		status = nfs4_handle_exception(dst_server, status, &exception);
5829 	} while (exception.retry);
5830 
5831 	return status;
5832 }
5833 
5834 struct nfs4_renewdata {
5835 	struct nfs_client	*client;
5836 	unsigned long		timestamp;
5837 };
5838 
5839 /*
5840  * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5841  * standalone procedure for queueing an asynchronous RENEW.
5842  */
nfs4_renew_release(void * calldata)5843 static void nfs4_renew_release(void *calldata)
5844 {
5845 	struct nfs4_renewdata *data = calldata;
5846 	struct nfs_client *clp = data->client;
5847 
5848 	if (refcount_read(&clp->cl_count) > 1)
5849 		nfs4_schedule_state_renewal(clp);
5850 	nfs_put_client(clp);
5851 	kfree(data);
5852 }
5853 
nfs4_renew_done(struct rpc_task * task,void * calldata)5854 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5855 {
5856 	struct nfs4_renewdata *data = calldata;
5857 	struct nfs_client *clp = data->client;
5858 	unsigned long timestamp = data->timestamp;
5859 
5860 	trace_nfs4_renew_async(clp, task->tk_status);
5861 	switch (task->tk_status) {
5862 	case 0:
5863 		break;
5864 	case -NFS4ERR_LEASE_MOVED:
5865 		nfs4_schedule_lease_moved_recovery(clp);
5866 		break;
5867 	default:
5868 		/* Unless we're shutting down, schedule state recovery! */
5869 		if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5870 			return;
5871 		if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5872 			nfs4_schedule_lease_recovery(clp);
5873 			return;
5874 		}
5875 		nfs4_schedule_path_down_recovery(clp);
5876 	}
5877 	do_renew_lease(clp, timestamp);
5878 }
5879 
5880 static const struct rpc_call_ops nfs4_renew_ops = {
5881 	.rpc_call_done = nfs4_renew_done,
5882 	.rpc_release = nfs4_renew_release,
5883 };
5884 
nfs4_proc_async_renew(struct nfs_client * clp,const struct cred * cred,unsigned renew_flags)5885 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5886 {
5887 	struct rpc_message msg = {
5888 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5889 		.rpc_argp	= clp,
5890 		.rpc_cred	= cred,
5891 	};
5892 	struct nfs4_renewdata *data;
5893 
5894 	if (renew_flags == 0)
5895 		return 0;
5896 	if (!refcount_inc_not_zero(&clp->cl_count))
5897 		return -EIO;
5898 	data = kmalloc(sizeof(*data), GFP_NOFS);
5899 	if (data == NULL) {
5900 		nfs_put_client(clp);
5901 		return -ENOMEM;
5902 	}
5903 	data->client = clp;
5904 	data->timestamp = jiffies;
5905 	return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5906 			&nfs4_renew_ops, data);
5907 }
5908 
nfs4_proc_renew(struct nfs_client * clp,const struct cred * cred)5909 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5910 {
5911 	struct rpc_message msg = {
5912 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5913 		.rpc_argp	= clp,
5914 		.rpc_cred	= cred,
5915 	};
5916 	unsigned long now = jiffies;
5917 	int status;
5918 
5919 	status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5920 	if (status < 0)
5921 		return status;
5922 	do_renew_lease(clp, now);
5923 	return 0;
5924 }
5925 
nfs4_server_supports_acls(const struct nfs_server * server,enum nfs4_acl_type type)5926 static bool nfs4_server_supports_acls(const struct nfs_server *server,
5927 				      enum nfs4_acl_type type)
5928 {
5929 	switch (type) {
5930 	default:
5931 		return server->attr_bitmask[0] & FATTR4_WORD0_ACL;
5932 	case NFS4ACL_DACL:
5933 		return server->attr_bitmask[1] & FATTR4_WORD1_DACL;
5934 	case NFS4ACL_SACL:
5935 		return server->attr_bitmask[1] & FATTR4_WORD1_SACL;
5936 	}
5937 }
5938 
5939 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5940  * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5941  * the stack.
5942  */
5943 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5944 
nfs4_buf_to_pages_noslab(const void * buf,size_t buflen,struct page ** pages)5945 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5946 		struct page **pages)
5947 {
5948 	struct page *newpage, **spages;
5949 	int rc = 0;
5950 	size_t len;
5951 	spages = pages;
5952 
5953 	do {
5954 		len = min_t(size_t, PAGE_SIZE, buflen);
5955 		newpage = alloc_page(GFP_KERNEL);
5956 
5957 		if (newpage == NULL)
5958 			goto unwind;
5959 		memcpy(page_address(newpage), buf, len);
5960 		buf += len;
5961 		buflen -= len;
5962 		*pages++ = newpage;
5963 		rc++;
5964 	} while (buflen != 0);
5965 
5966 	return rc;
5967 
5968 unwind:
5969 	for(; rc > 0; rc--)
5970 		__free_page(spages[rc-1]);
5971 	return -ENOMEM;
5972 }
5973 
5974 struct nfs4_cached_acl {
5975 	enum nfs4_acl_type type;
5976 	int cached;
5977 	size_t len;
5978 	char data[];
5979 };
5980 
nfs4_set_cached_acl(struct inode * inode,struct nfs4_cached_acl * acl)5981 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5982 {
5983 	struct nfs_inode *nfsi = NFS_I(inode);
5984 
5985 	spin_lock(&inode->i_lock);
5986 	kfree(nfsi->nfs4_acl);
5987 	nfsi->nfs4_acl = acl;
5988 	spin_unlock(&inode->i_lock);
5989 }
5990 
nfs4_zap_acl_attr(struct inode * inode)5991 static void nfs4_zap_acl_attr(struct inode *inode)
5992 {
5993 	nfs4_set_cached_acl(inode, NULL);
5994 }
5995 
nfs4_read_cached_acl(struct inode * inode,char * buf,size_t buflen,enum nfs4_acl_type type)5996 static ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf,
5997 				    size_t buflen, enum nfs4_acl_type type)
5998 {
5999 	struct nfs_inode *nfsi = NFS_I(inode);
6000 	struct nfs4_cached_acl *acl;
6001 	int ret = -ENOENT;
6002 
6003 	spin_lock(&inode->i_lock);
6004 	acl = nfsi->nfs4_acl;
6005 	if (acl == NULL)
6006 		goto out;
6007 	if (acl->type != type)
6008 		goto out;
6009 	if (buf == NULL) /* user is just asking for length */
6010 		goto out_len;
6011 	if (acl->cached == 0)
6012 		goto out;
6013 	ret = -ERANGE; /* see getxattr(2) man page */
6014 	if (acl->len > buflen)
6015 		goto out;
6016 	memcpy(buf, acl->data, acl->len);
6017 out_len:
6018 	ret = acl->len;
6019 out:
6020 	spin_unlock(&inode->i_lock);
6021 	return ret;
6022 }
6023 
nfs4_write_cached_acl(struct inode * inode,struct page ** pages,size_t pgbase,size_t acl_len,enum nfs4_acl_type type)6024 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages,
6025 				  size_t pgbase, size_t acl_len,
6026 				  enum nfs4_acl_type type)
6027 {
6028 	struct nfs4_cached_acl *acl;
6029 	size_t buflen = sizeof(*acl) + acl_len;
6030 
6031 	if (buflen <= PAGE_SIZE) {
6032 		acl = kmalloc(buflen, GFP_KERNEL);
6033 		if (acl == NULL)
6034 			goto out;
6035 		acl->cached = 1;
6036 		_copy_from_pages(acl->data, pages, pgbase, acl_len);
6037 	} else {
6038 		acl = kmalloc(sizeof(*acl), GFP_KERNEL);
6039 		if (acl == NULL)
6040 			goto out;
6041 		acl->cached = 0;
6042 	}
6043 	acl->type = type;
6044 	acl->len = acl_len;
6045 out:
6046 	nfs4_set_cached_acl(inode, acl);
6047 }
6048 
6049 /*
6050  * The getxattr API returns the required buffer length when called with a
6051  * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
6052  * the required buf.  On a NULL buf, we send a page of data to the server
6053  * guessing that the ACL request can be serviced by a page. If so, we cache
6054  * up to the page of ACL data, and the 2nd call to getxattr is serviced by
6055  * the cache. If not so, we throw away the page, and cache the required
6056  * length. The next getxattr call will then produce another round trip to
6057  * the server, this time with the input buf of the required size.
6058  */
__nfs4_get_acl_uncached(struct inode * inode,void * buf,size_t buflen,enum nfs4_acl_type type)6059 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf,
6060 				       size_t buflen, enum nfs4_acl_type type)
6061 {
6062 	struct page **pages;
6063 	struct nfs_getaclargs args = {
6064 		.fh = NFS_FH(inode),
6065 		.acl_type = type,
6066 		.acl_len = buflen,
6067 	};
6068 	struct nfs_getaclres res = {
6069 		.acl_type = type,
6070 		.acl_len = buflen,
6071 	};
6072 	struct rpc_message msg = {
6073 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
6074 		.rpc_argp = &args,
6075 		.rpc_resp = &res,
6076 	};
6077 	unsigned int npages;
6078 	int ret = -ENOMEM, i;
6079 	struct nfs_server *server = NFS_SERVER(inode);
6080 
6081 	if (buflen == 0)
6082 		buflen = server->rsize;
6083 
6084 	npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
6085 	pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
6086 	if (!pages)
6087 		return -ENOMEM;
6088 
6089 	args.acl_pages = pages;
6090 
6091 	for (i = 0; i < npages; i++) {
6092 		pages[i] = alloc_page(GFP_KERNEL);
6093 		if (!pages[i])
6094 			goto out_free;
6095 	}
6096 
6097 	/* for decoding across pages */
6098 	res.acl_scratch = alloc_page(GFP_KERNEL);
6099 	if (!res.acl_scratch)
6100 		goto out_free;
6101 
6102 	args.acl_len = npages * PAGE_SIZE;
6103 
6104 	dprintk("%s  buf %p buflen %zu npages %d args.acl_len %zu\n",
6105 		__func__, buf, buflen, npages, args.acl_len);
6106 	ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
6107 			     &msg, &args.seq_args, &res.seq_res, 0);
6108 	if (ret)
6109 		goto out_free;
6110 
6111 	/* Handle the case where the passed-in buffer is too short */
6112 	if (res.acl_flags & NFS4_ACL_TRUNC) {
6113 		/* Did the user only issue a request for the acl length? */
6114 		if (buf == NULL)
6115 			goto out_ok;
6116 		ret = -ERANGE;
6117 		goto out_free;
6118 	}
6119 	nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len,
6120 			      type);
6121 	if (buf) {
6122 		if (res.acl_len > buflen) {
6123 			ret = -ERANGE;
6124 			goto out_free;
6125 		}
6126 		_copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
6127 	}
6128 out_ok:
6129 	ret = res.acl_len;
6130 out_free:
6131 	while (--i >= 0)
6132 		__free_page(pages[i]);
6133 	if (res.acl_scratch)
6134 		__free_page(res.acl_scratch);
6135 	kfree(pages);
6136 	return ret;
6137 }
6138 
nfs4_get_acl_uncached(struct inode * inode,void * buf,size_t buflen,enum nfs4_acl_type type)6139 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf,
6140 				     size_t buflen, enum nfs4_acl_type type)
6141 {
6142 	struct nfs4_exception exception = {
6143 		.interruptible = true,
6144 	};
6145 	ssize_t ret;
6146 	do {
6147 		ret = __nfs4_get_acl_uncached(inode, buf, buflen, type);
6148 		trace_nfs4_get_acl(inode, ret);
6149 		if (ret >= 0)
6150 			break;
6151 		ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
6152 	} while (exception.retry);
6153 	return ret;
6154 }
6155 
nfs4_proc_get_acl(struct inode * inode,void * buf,size_t buflen,enum nfs4_acl_type type)6156 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen,
6157 				 enum nfs4_acl_type type)
6158 {
6159 	struct nfs_server *server = NFS_SERVER(inode);
6160 	int ret;
6161 
6162 	if (!nfs4_server_supports_acls(server, type))
6163 		return -EOPNOTSUPP;
6164 	ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
6165 	if (ret < 0)
6166 		return ret;
6167 	if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
6168 		nfs_zap_acl_cache(inode);
6169 	ret = nfs4_read_cached_acl(inode, buf, buflen, type);
6170 	if (ret != -ENOENT)
6171 		/* -ENOENT is returned if there is no ACL or if there is an ACL
6172 		 * but no cached acl data, just the acl length */
6173 		return ret;
6174 	return nfs4_get_acl_uncached(inode, buf, buflen, type);
6175 }
6176 
__nfs4_proc_set_acl(struct inode * inode,const void * buf,size_t buflen,enum nfs4_acl_type type)6177 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf,
6178 			       size_t buflen, enum nfs4_acl_type type)
6179 {
6180 	struct nfs_server *server = NFS_SERVER(inode);
6181 	struct page *pages[NFS4ACL_MAXPAGES];
6182 	struct nfs_setaclargs arg = {
6183 		.fh = NFS_FH(inode),
6184 		.acl_type = type,
6185 		.acl_len = buflen,
6186 		.acl_pages = pages,
6187 	};
6188 	struct nfs_setaclres res;
6189 	struct rpc_message msg = {
6190 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_SETACL],
6191 		.rpc_argp	= &arg,
6192 		.rpc_resp	= &res,
6193 	};
6194 	unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
6195 	int ret, i;
6196 
6197 	/* You can't remove system.nfs4_acl: */
6198 	if (buflen == 0)
6199 		return -EINVAL;
6200 	if (!nfs4_server_supports_acls(server, type))
6201 		return -EOPNOTSUPP;
6202 	if (npages > ARRAY_SIZE(pages))
6203 		return -ERANGE;
6204 	i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
6205 	if (i < 0)
6206 		return i;
6207 	nfs4_inode_make_writeable(inode);
6208 	ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6209 
6210 	/*
6211 	 * Free each page after tx, so the only ref left is
6212 	 * held by the network stack
6213 	 */
6214 	for (; i > 0; i--)
6215 		put_page(pages[i-1]);
6216 
6217 	/*
6218 	 * Acl update can result in inode attribute update.
6219 	 * so mark the attribute cache invalid.
6220 	 */
6221 	spin_lock(&inode->i_lock);
6222 	nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
6223 					     NFS_INO_INVALID_CTIME |
6224 					     NFS_INO_REVAL_FORCED);
6225 	spin_unlock(&inode->i_lock);
6226 	nfs_access_zap_cache(inode);
6227 	nfs_zap_acl_cache(inode);
6228 	return ret;
6229 }
6230 
nfs4_proc_set_acl(struct inode * inode,const void * buf,size_t buflen,enum nfs4_acl_type type)6231 static int nfs4_proc_set_acl(struct inode *inode, const void *buf,
6232 			     size_t buflen, enum nfs4_acl_type type)
6233 {
6234 	struct nfs4_exception exception = { };
6235 	int err;
6236 	do {
6237 		err = __nfs4_proc_set_acl(inode, buf, buflen, type);
6238 		trace_nfs4_set_acl(inode, err);
6239 		if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
6240 			/*
6241 			 * no need to retry since the kernel
6242 			 * isn't involved in encoding the ACEs.
6243 			 */
6244 			err = -EINVAL;
6245 			break;
6246 		}
6247 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
6248 				&exception);
6249 	} while (exception.retry);
6250 	return err;
6251 }
6252 
6253 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
_nfs4_get_security_label(struct inode * inode,void * buf,size_t buflen)6254 static int _nfs4_get_security_label(struct inode *inode, void *buf,
6255 					size_t buflen)
6256 {
6257 	struct nfs_server *server = NFS_SERVER(inode);
6258 	struct nfs4_label label = {0, 0, buflen, buf};
6259 
6260 	u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6261 	struct nfs_fattr fattr = {
6262 		.label = &label,
6263 	};
6264 	struct nfs4_getattr_arg arg = {
6265 		.fh		= NFS_FH(inode),
6266 		.bitmask	= bitmask,
6267 	};
6268 	struct nfs4_getattr_res res = {
6269 		.fattr		= &fattr,
6270 		.server		= server,
6271 	};
6272 	struct rpc_message msg = {
6273 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
6274 		.rpc_argp	= &arg,
6275 		.rpc_resp	= &res,
6276 	};
6277 	int ret;
6278 
6279 	nfs_fattr_init(&fattr);
6280 
6281 	ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
6282 	if (ret)
6283 		return ret;
6284 	if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6285 		return -ENOENT;
6286 	return label.len;
6287 }
6288 
nfs4_get_security_label(struct inode * inode,void * buf,size_t buflen)6289 static int nfs4_get_security_label(struct inode *inode, void *buf,
6290 					size_t buflen)
6291 {
6292 	struct nfs4_exception exception = {
6293 		.interruptible = true,
6294 	};
6295 	int err;
6296 
6297 	if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6298 		return -EOPNOTSUPP;
6299 
6300 	do {
6301 		err = _nfs4_get_security_label(inode, buf, buflen);
6302 		trace_nfs4_get_security_label(inode, err);
6303 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
6304 				&exception);
6305 	} while (exception.retry);
6306 	return err;
6307 }
6308 
_nfs4_do_set_security_label(struct inode * inode,struct nfs4_label * ilabel,struct nfs_fattr * fattr)6309 static int _nfs4_do_set_security_label(struct inode *inode,
6310 		struct nfs4_label *ilabel,
6311 		struct nfs_fattr *fattr)
6312 {
6313 
6314 	struct iattr sattr = {0};
6315 	struct nfs_server *server = NFS_SERVER(inode);
6316 	const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6317 	struct nfs_setattrargs arg = {
6318 		.fh		= NFS_FH(inode),
6319 		.iap		= &sattr,
6320 		.server		= server,
6321 		.bitmask	= bitmask,
6322 		.label		= ilabel,
6323 	};
6324 	struct nfs_setattrres res = {
6325 		.fattr		= fattr,
6326 		.server		= server,
6327 	};
6328 	struct rpc_message msg = {
6329 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6330 		.rpc_argp	= &arg,
6331 		.rpc_resp	= &res,
6332 	};
6333 	int status;
6334 
6335 	nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6336 
6337 	status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6338 	if (status)
6339 		dprintk("%s failed: %d\n", __func__, status);
6340 
6341 	return status;
6342 }
6343 
nfs4_do_set_security_label(struct inode * inode,struct nfs4_label * ilabel,struct nfs_fattr * fattr)6344 static int nfs4_do_set_security_label(struct inode *inode,
6345 		struct nfs4_label *ilabel,
6346 		struct nfs_fattr *fattr)
6347 {
6348 	struct nfs4_exception exception = { };
6349 	int err;
6350 
6351 	do {
6352 		err = _nfs4_do_set_security_label(inode, ilabel, fattr);
6353 		trace_nfs4_set_security_label(inode, err);
6354 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
6355 				&exception);
6356 	} while (exception.retry);
6357 	return err;
6358 }
6359 
6360 static int
nfs4_set_security_label(struct inode * inode,const void * buf,size_t buflen)6361 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6362 {
6363 	struct nfs4_label ilabel = {0, 0, buflen, (char *)buf };
6364 	struct nfs_fattr *fattr;
6365 	int status;
6366 
6367 	if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6368 		return -EOPNOTSUPP;
6369 
6370 	fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
6371 	if (fattr == NULL)
6372 		return -ENOMEM;
6373 
6374 	status = nfs4_do_set_security_label(inode, &ilabel, fattr);
6375 	if (status == 0)
6376 		nfs_setsecurity(inode, fattr);
6377 
6378 	nfs_free_fattr(fattr);
6379 	return status;
6380 }
6381 #endif	/* CONFIG_NFS_V4_SECURITY_LABEL */
6382 
6383 
nfs4_init_boot_verifier(const struct nfs_client * clp,nfs4_verifier * bootverf)6384 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6385 				    nfs4_verifier *bootverf)
6386 {
6387 	__be32 verf[2];
6388 
6389 	if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6390 		/* An impossible timestamp guarantees this value
6391 		 * will never match a generated boot time. */
6392 		verf[0] = cpu_to_be32(U32_MAX);
6393 		verf[1] = cpu_to_be32(U32_MAX);
6394 	} else {
6395 		struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6396 		u64 ns = ktime_to_ns(nn->boot_time);
6397 
6398 		verf[0] = cpu_to_be32(ns >> 32);
6399 		verf[1] = cpu_to_be32(ns);
6400 	}
6401 	memcpy(bootverf->data, verf, sizeof(bootverf->data));
6402 }
6403 
6404 static size_t
nfs4_get_uniquifier(struct nfs_client * clp,char * buf,size_t buflen)6405 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6406 {
6407 	struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6408 	struct nfs_netns_client *nn_clp = nn->nfs_client;
6409 	const char *id;
6410 
6411 	buf[0] = '\0';
6412 
6413 	if (nn_clp) {
6414 		rcu_read_lock();
6415 		id = rcu_dereference(nn_clp->identifier);
6416 		if (id)
6417 			strscpy(buf, id, buflen);
6418 		rcu_read_unlock();
6419 	}
6420 
6421 	if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6422 		strscpy(buf, nfs4_client_id_uniquifier, buflen);
6423 
6424 	return strlen(buf);
6425 }
6426 
6427 static int
nfs4_init_nonuniform_client_string(struct nfs_client * clp)6428 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6429 {
6430 	char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6431 	size_t buflen;
6432 	size_t len;
6433 	char *str;
6434 
6435 	if (clp->cl_owner_id != NULL)
6436 		return 0;
6437 
6438 	rcu_read_lock();
6439 	len = 14 +
6440 		strlen(clp->cl_rpcclient->cl_nodename) +
6441 		1 +
6442 		strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6443 		1;
6444 	rcu_read_unlock();
6445 
6446 	buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6447 	if (buflen)
6448 		len += buflen + 1;
6449 
6450 	if (len > NFS4_OPAQUE_LIMIT + 1)
6451 		return -EINVAL;
6452 
6453 	/*
6454 	 * Since this string is allocated at mount time, and held until the
6455 	 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6456 	 * about a memory-reclaim deadlock.
6457 	 */
6458 	str = kmalloc(len, GFP_KERNEL);
6459 	if (!str)
6460 		return -ENOMEM;
6461 
6462 	rcu_read_lock();
6463 	if (buflen)
6464 		scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6465 			  clp->cl_rpcclient->cl_nodename, buf,
6466 			  rpc_peeraddr2str(clp->cl_rpcclient,
6467 					   RPC_DISPLAY_ADDR));
6468 	else
6469 		scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6470 			  clp->cl_rpcclient->cl_nodename,
6471 			  rpc_peeraddr2str(clp->cl_rpcclient,
6472 					   RPC_DISPLAY_ADDR));
6473 	rcu_read_unlock();
6474 
6475 	clp->cl_owner_id = str;
6476 	return 0;
6477 }
6478 
6479 static int
nfs4_init_uniform_client_string(struct nfs_client * clp)6480 nfs4_init_uniform_client_string(struct nfs_client *clp)
6481 {
6482 	char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6483 	size_t buflen;
6484 	size_t len;
6485 	char *str;
6486 
6487 	if (clp->cl_owner_id != NULL)
6488 		return 0;
6489 
6490 	len = 10 + 10 + 1 + 10 + 1 +
6491 		strlen(clp->cl_rpcclient->cl_nodename) + 1;
6492 
6493 	buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6494 	if (buflen)
6495 		len += buflen + 1;
6496 
6497 	if (len > NFS4_OPAQUE_LIMIT + 1)
6498 		return -EINVAL;
6499 
6500 	/*
6501 	 * Since this string is allocated at mount time, and held until the
6502 	 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6503 	 * about a memory-reclaim deadlock.
6504 	 */
6505 	str = kmalloc(len, GFP_KERNEL);
6506 	if (!str)
6507 		return -ENOMEM;
6508 
6509 	if (buflen)
6510 		scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6511 			  clp->rpc_ops->version, clp->cl_minorversion,
6512 			  buf, clp->cl_rpcclient->cl_nodename);
6513 	else
6514 		scnprintf(str, len, "Linux NFSv%u.%u %s",
6515 			  clp->rpc_ops->version, clp->cl_minorversion,
6516 			  clp->cl_rpcclient->cl_nodename);
6517 	clp->cl_owner_id = str;
6518 	return 0;
6519 }
6520 
6521 /*
6522  * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6523  * services.  Advertise one based on the address family of the
6524  * clientaddr.
6525  */
6526 static unsigned int
nfs4_init_callback_netid(const struct nfs_client * clp,char * buf,size_t len)6527 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6528 {
6529 	if (strchr(clp->cl_ipaddr, ':') != NULL)
6530 		return scnprintf(buf, len, "tcp6");
6531 	else
6532 		return scnprintf(buf, len, "tcp");
6533 }
6534 
nfs4_setclientid_done(struct rpc_task * task,void * calldata)6535 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6536 {
6537 	struct nfs4_setclientid *sc = calldata;
6538 
6539 	if (task->tk_status == 0)
6540 		sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6541 }
6542 
6543 static const struct rpc_call_ops nfs4_setclientid_ops = {
6544 	.rpc_call_done = nfs4_setclientid_done,
6545 };
6546 
6547 /**
6548  * nfs4_proc_setclientid - Negotiate client ID
6549  * @clp: state data structure
6550  * @program: RPC program for NFSv4 callback service
6551  * @port: IP port number for NFS4 callback service
6552  * @cred: credential to use for this call
6553  * @res: where to place the result
6554  *
6555  * Returns zero, a negative errno, or a negative NFS4ERR status code.
6556  */
nfs4_proc_setclientid(struct nfs_client * clp,u32 program,unsigned short port,const struct cred * cred,struct nfs4_setclientid_res * res)6557 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6558 		unsigned short port, const struct cred *cred,
6559 		struct nfs4_setclientid_res *res)
6560 {
6561 	nfs4_verifier sc_verifier;
6562 	struct nfs4_setclientid setclientid = {
6563 		.sc_verifier = &sc_verifier,
6564 		.sc_prog = program,
6565 		.sc_clnt = clp,
6566 	};
6567 	struct rpc_message msg = {
6568 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6569 		.rpc_argp = &setclientid,
6570 		.rpc_resp = res,
6571 		.rpc_cred = cred,
6572 	};
6573 	struct rpc_task_setup task_setup_data = {
6574 		.rpc_client = clp->cl_rpcclient,
6575 		.rpc_message = &msg,
6576 		.callback_ops = &nfs4_setclientid_ops,
6577 		.callback_data = &setclientid,
6578 		.flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6579 	};
6580 	unsigned long now = jiffies;
6581 	int status;
6582 
6583 	/* nfs_client_id4 */
6584 	nfs4_init_boot_verifier(clp, &sc_verifier);
6585 
6586 	if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6587 		status = nfs4_init_uniform_client_string(clp);
6588 	else
6589 		status = nfs4_init_nonuniform_client_string(clp);
6590 
6591 	if (status)
6592 		goto out;
6593 
6594 	/* cb_client4 */
6595 	setclientid.sc_netid_len =
6596 				nfs4_init_callback_netid(clp,
6597 						setclientid.sc_netid,
6598 						sizeof(setclientid.sc_netid));
6599 	setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6600 				sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6601 				clp->cl_ipaddr, port >> 8, port & 255);
6602 
6603 	dprintk("NFS call  setclientid auth=%s, '%s'\n",
6604 		clp->cl_rpcclient->cl_auth->au_ops->au_name,
6605 		clp->cl_owner_id);
6606 
6607 	status = nfs4_call_sync_custom(&task_setup_data);
6608 	if (setclientid.sc_cred) {
6609 		kfree(clp->cl_acceptor);
6610 		clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6611 		put_rpccred(setclientid.sc_cred);
6612 	}
6613 
6614 	if (status == 0)
6615 		do_renew_lease(clp, now);
6616 out:
6617 	trace_nfs4_setclientid(clp, status);
6618 	dprintk("NFS reply setclientid: %d\n", status);
6619 	return status;
6620 }
6621 
6622 /**
6623  * nfs4_proc_setclientid_confirm - Confirm client ID
6624  * @clp: state data structure
6625  * @arg: result of a previous SETCLIENTID
6626  * @cred: credential to use for this call
6627  *
6628  * Returns zero, a negative errno, or a negative NFS4ERR status code.
6629  */
nfs4_proc_setclientid_confirm(struct nfs_client * clp,struct nfs4_setclientid_res * arg,const struct cred * cred)6630 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6631 		struct nfs4_setclientid_res *arg,
6632 		const struct cred *cred)
6633 {
6634 	struct rpc_message msg = {
6635 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6636 		.rpc_argp = arg,
6637 		.rpc_cred = cred,
6638 	};
6639 	int status;
6640 
6641 	dprintk("NFS call  setclientid_confirm auth=%s, (client ID %llx)\n",
6642 		clp->cl_rpcclient->cl_auth->au_ops->au_name,
6643 		clp->cl_clientid);
6644 	status = rpc_call_sync(clp->cl_rpcclient, &msg,
6645 			       RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6646 	trace_nfs4_setclientid_confirm(clp, status);
6647 	dprintk("NFS reply setclientid_confirm: %d\n", status);
6648 	return status;
6649 }
6650 
6651 struct nfs4_delegreturndata {
6652 	struct nfs4_delegreturnargs args;
6653 	struct nfs4_delegreturnres res;
6654 	struct nfs_fh fh;
6655 	nfs4_stateid stateid;
6656 	unsigned long timestamp;
6657 	struct {
6658 		struct nfs4_layoutreturn_args arg;
6659 		struct nfs4_layoutreturn_res res;
6660 		struct nfs4_xdr_opaque_data ld_private;
6661 		u32 roc_barrier;
6662 		bool roc;
6663 	} lr;
6664 	struct nfs4_delegattr sattr;
6665 	struct nfs_fattr fattr;
6666 	int rpc_status;
6667 	struct inode *inode;
6668 };
6669 
nfs4_delegreturn_done(struct rpc_task * task,void * calldata)6670 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6671 {
6672 	struct nfs4_delegreturndata *data = calldata;
6673 	struct nfs4_exception exception = {
6674 		.inode = data->inode,
6675 		.stateid = &data->stateid,
6676 		.task_is_privileged = data->args.seq_args.sa_privileged,
6677 	};
6678 
6679 	if (!nfs4_sequence_done(task, &data->res.seq_res))
6680 		return;
6681 
6682 	trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6683 
6684 	/* Handle Layoutreturn errors */
6685 	if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6686 			  &data->res.lr_ret) == -EAGAIN)
6687 		goto out_restart;
6688 
6689 	if (data->args.sattr_args && task->tk_status != 0) {
6690 		switch(data->res.sattr_ret) {
6691 		case 0:
6692 			data->args.sattr_args = NULL;
6693 			data->res.sattr_res = false;
6694 			break;
6695 		case -NFS4ERR_ADMIN_REVOKED:
6696 		case -NFS4ERR_DELEG_REVOKED:
6697 		case -NFS4ERR_EXPIRED:
6698 		case -NFS4ERR_BAD_STATEID:
6699 			/* Let the main handler below do stateid recovery */
6700 			break;
6701 		case -NFS4ERR_OLD_STATEID:
6702 			if (nfs4_refresh_delegation_stateid(&data->stateid,
6703 						data->inode))
6704 				goto out_restart;
6705 			fallthrough;
6706 		default:
6707 			data->args.sattr_args = NULL;
6708 			data->res.sattr_res = false;
6709 			goto out_restart;
6710 		}
6711 	}
6712 
6713 	switch (task->tk_status) {
6714 	case 0:
6715 		renew_lease(data->res.server, data->timestamp);
6716 		break;
6717 	case -NFS4ERR_ADMIN_REVOKED:
6718 	case -NFS4ERR_DELEG_REVOKED:
6719 	case -NFS4ERR_EXPIRED:
6720 		nfs4_free_revoked_stateid(data->res.server,
6721 				data->args.stateid,
6722 				task->tk_msg.rpc_cred);
6723 		fallthrough;
6724 	case -NFS4ERR_BAD_STATEID:
6725 	case -NFS4ERR_STALE_STATEID:
6726 	case -ETIMEDOUT:
6727 		task->tk_status = 0;
6728 		break;
6729 	case -NFS4ERR_OLD_STATEID:
6730 		if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6731 			nfs4_stateid_seqid_inc(&data->stateid);
6732 		if (data->args.bitmask) {
6733 			data->args.bitmask = NULL;
6734 			data->res.fattr = NULL;
6735 		}
6736 		goto out_restart;
6737 	case -NFS4ERR_ACCESS:
6738 		if (data->args.bitmask) {
6739 			data->args.bitmask = NULL;
6740 			data->res.fattr = NULL;
6741 			goto out_restart;
6742 		}
6743 		fallthrough;
6744 	default:
6745 		task->tk_status = nfs4_async_handle_exception(task,
6746 				data->res.server, task->tk_status,
6747 				&exception);
6748 		if (exception.retry)
6749 			goto out_restart;
6750 	}
6751 	nfs_delegation_mark_returned(data->inode, data->args.stateid);
6752 	data->rpc_status = task->tk_status;
6753 	return;
6754 out_restart:
6755 	task->tk_status = 0;
6756 	rpc_restart_call_prepare(task);
6757 }
6758 
nfs4_delegreturn_release(void * calldata)6759 static void nfs4_delegreturn_release(void *calldata)
6760 {
6761 	struct nfs4_delegreturndata *data = calldata;
6762 	struct inode *inode = data->inode;
6763 
6764 	if (data->lr.roc)
6765 		pnfs_roc_release(&data->lr.arg, &data->lr.res,
6766 				 data->res.lr_ret);
6767 	if (inode) {
6768 		nfs4_fattr_set_prechange(&data->fattr,
6769 					 inode_peek_iversion_raw(inode));
6770 		nfs_refresh_inode(inode, &data->fattr);
6771 		nfs_iput_and_deactive(inode);
6772 	}
6773 	kfree(calldata);
6774 }
6775 
nfs4_delegreturn_prepare(struct rpc_task * task,void * data)6776 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6777 {
6778 	struct nfs4_delegreturndata *d_data;
6779 	struct pnfs_layout_hdr *lo;
6780 
6781 	d_data = data;
6782 
6783 	if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6784 		nfs4_sequence_done(task, &d_data->res.seq_res);
6785 		return;
6786 	}
6787 
6788 	lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6789 	if (lo && !pnfs_layout_is_valid(lo)) {
6790 		d_data->args.lr_args = NULL;
6791 		d_data->res.lr_res = NULL;
6792 	}
6793 
6794 	nfs4_setup_sequence(d_data->res.server->nfs_client,
6795 			&d_data->args.seq_args,
6796 			&d_data->res.seq_res,
6797 			task);
6798 }
6799 
6800 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6801 	.rpc_call_prepare = nfs4_delegreturn_prepare,
6802 	.rpc_call_done = nfs4_delegreturn_done,
6803 	.rpc_release = nfs4_delegreturn_release,
6804 };
6805 
_nfs4_proc_delegreturn(struct inode * inode,const struct cred * cred,const nfs4_stateid * stateid,struct nfs_delegation * delegation,int issync)6806 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred,
6807 				  const nfs4_stateid *stateid,
6808 				  struct nfs_delegation *delegation,
6809 				  int issync)
6810 {
6811 	struct nfs4_delegreturndata *data;
6812 	struct nfs_server *server = NFS_SERVER(inode);
6813 	struct rpc_task *task;
6814 	struct rpc_message msg = {
6815 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6816 		.rpc_cred = cred,
6817 	};
6818 	struct rpc_task_setup task_setup_data = {
6819 		.rpc_client = server->client,
6820 		.rpc_message = &msg,
6821 		.callback_ops = &nfs4_delegreturn_ops,
6822 		.flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
6823 	};
6824 	int status = 0;
6825 
6826 	if (nfs_server_capable(inode, NFS_CAP_MOVEABLE))
6827 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
6828 
6829 	data = kzalloc(sizeof(*data), GFP_KERNEL);
6830 	if (data == NULL)
6831 		return -ENOMEM;
6832 
6833 	nfs4_state_protect(server->nfs_client,
6834 			NFS_SP4_MACH_CRED_CLEANUP,
6835 			&task_setup_data.rpc_client, &msg);
6836 
6837 	data->args.fhandle = &data->fh;
6838 	data->args.stateid = &data->stateid;
6839 	nfs4_bitmask_set(data->args.bitmask_store,
6840 			 server->cache_consistency_bitmask, inode, 0);
6841 	data->args.bitmask = data->args.bitmask_store;
6842 	nfs_copy_fh(&data->fh, NFS_FH(inode));
6843 	nfs4_stateid_copy(&data->stateid, stateid);
6844 	data->res.fattr = &data->fattr;
6845 	data->res.server = server;
6846 	data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6847 	data->lr.arg.ld_private = &data->lr.ld_private;
6848 	nfs_fattr_init(data->res.fattr);
6849 	data->timestamp = jiffies;
6850 	data->rpc_status = 0;
6851 	data->inode = nfs_igrab_and_active(inode);
6852 	if (data->inode || issync) {
6853 		data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6854 					cred);
6855 		if (data->lr.roc) {
6856 			data->args.lr_args = &data->lr.arg;
6857 			data->res.lr_res = &data->lr.res;
6858 		}
6859 	}
6860 
6861 	if (delegation &&
6862 	    test_bit(NFS_DELEGATION_DELEGTIME, &delegation->flags)) {
6863 		if (delegation->type & FMODE_READ) {
6864 			data->sattr.atime = inode_get_atime(inode);
6865 			data->sattr.atime_set = true;
6866 		}
6867 		if (delegation->type & FMODE_WRITE) {
6868 			data->sattr.mtime = inode_get_mtime(inode);
6869 			data->sattr.mtime_set = true;
6870 		}
6871 		data->args.sattr_args = &data->sattr;
6872 		data->res.sattr_res = true;
6873 	}
6874 
6875 	if (!data->inode)
6876 		nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6877 				   1);
6878 	else
6879 		nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6880 				   0);
6881 
6882 	task_setup_data.callback_data = data;
6883 	msg.rpc_argp = &data->args;
6884 	msg.rpc_resp = &data->res;
6885 	task = rpc_run_task(&task_setup_data);
6886 	if (IS_ERR(task))
6887 		return PTR_ERR(task);
6888 	if (!issync)
6889 		goto out;
6890 	status = rpc_wait_for_completion_task(task);
6891 	if (status != 0)
6892 		goto out;
6893 	status = data->rpc_status;
6894 out:
6895 	rpc_put_task(task);
6896 	return status;
6897 }
6898 
nfs4_proc_delegreturn(struct inode * inode,const struct cred * cred,const nfs4_stateid * stateid,struct nfs_delegation * delegation,int issync)6899 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred,
6900 			  const nfs4_stateid *stateid,
6901 			  struct nfs_delegation *delegation, int issync)
6902 {
6903 	struct nfs_server *server = NFS_SERVER(inode);
6904 	struct nfs4_exception exception = { };
6905 	int err;
6906 	do {
6907 		err = _nfs4_proc_delegreturn(inode, cred, stateid,
6908 					     delegation, issync);
6909 		trace_nfs4_delegreturn(inode, stateid, err);
6910 		switch (err) {
6911 			case -NFS4ERR_STALE_STATEID:
6912 			case -NFS4ERR_EXPIRED:
6913 			case 0:
6914 				return 0;
6915 		}
6916 		err = nfs4_handle_exception(server, err, &exception);
6917 	} while (exception.retry);
6918 	return err;
6919 }
6920 
_nfs4_proc_getlk(struct nfs4_state * state,int cmd,struct file_lock * request)6921 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6922 {
6923 	struct inode *inode = state->inode;
6924 	struct nfs_server *server = NFS_SERVER(inode);
6925 	struct nfs_client *clp = server->nfs_client;
6926 	struct nfs_lockt_args arg = {
6927 		.fh = NFS_FH(inode),
6928 		.fl = request,
6929 	};
6930 	struct nfs_lockt_res res = {
6931 		.denied = request,
6932 	};
6933 	struct rpc_message msg = {
6934 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6935 		.rpc_argp	= &arg,
6936 		.rpc_resp	= &res,
6937 		.rpc_cred	= state->owner->so_cred,
6938 	};
6939 	struct nfs4_lock_state *lsp;
6940 	int status;
6941 
6942 	arg.lock_owner.clientid = clp->cl_clientid;
6943 	status = nfs4_set_lock_state(state, request);
6944 	if (status != 0)
6945 		goto out;
6946 	lsp = request->fl_u.nfs4_fl.owner;
6947 	arg.lock_owner.id = lsp->ls_seqid.owner_id;
6948 	arg.lock_owner.s_dev = server->s_dev;
6949 	status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6950 	switch (status) {
6951 		case 0:
6952 			request->c.flc_type = F_UNLCK;
6953 			break;
6954 		case -NFS4ERR_DENIED:
6955 			status = 0;
6956 	}
6957 	request->fl_ops->fl_release_private(request);
6958 	request->fl_ops = NULL;
6959 out:
6960 	return status;
6961 }
6962 
nfs4_proc_getlk(struct nfs4_state * state,int cmd,struct file_lock * request)6963 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6964 {
6965 	struct nfs4_exception exception = {
6966 		.interruptible = true,
6967 	};
6968 	int err;
6969 
6970 	do {
6971 		err = _nfs4_proc_getlk(state, cmd, request);
6972 		trace_nfs4_get_lock(request, state, cmd, err);
6973 		err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6974 				&exception);
6975 	} while (exception.retry);
6976 	return err;
6977 }
6978 
6979 /*
6980  * Update the seqid of a lock stateid after receiving
6981  * NFS4ERR_OLD_STATEID
6982  */
nfs4_refresh_lock_old_stateid(nfs4_stateid * dst,struct nfs4_lock_state * lsp)6983 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6984 		struct nfs4_lock_state *lsp)
6985 {
6986 	struct nfs4_state *state = lsp->ls_state;
6987 	bool ret = false;
6988 
6989 	spin_lock(&state->state_lock);
6990 	if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6991 		goto out;
6992 	if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6993 		nfs4_stateid_seqid_inc(dst);
6994 	else
6995 		dst->seqid = lsp->ls_stateid.seqid;
6996 	ret = true;
6997 out:
6998 	spin_unlock(&state->state_lock);
6999 	return ret;
7000 }
7001 
nfs4_sync_lock_stateid(nfs4_stateid * dst,struct nfs4_lock_state * lsp)7002 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
7003 		struct nfs4_lock_state *lsp)
7004 {
7005 	struct nfs4_state *state = lsp->ls_state;
7006 	bool ret;
7007 
7008 	spin_lock(&state->state_lock);
7009 	ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
7010 	nfs4_stateid_copy(dst, &lsp->ls_stateid);
7011 	spin_unlock(&state->state_lock);
7012 	return ret;
7013 }
7014 
7015 struct nfs4_unlockdata {
7016 	struct nfs_locku_args arg;
7017 	struct nfs_locku_res res;
7018 	struct nfs4_lock_state *lsp;
7019 	struct nfs_open_context *ctx;
7020 	struct nfs_lock_context *l_ctx;
7021 	struct file_lock fl;
7022 	struct nfs_server *server;
7023 	unsigned long timestamp;
7024 };
7025 
nfs4_alloc_unlockdata(struct file_lock * fl,struct nfs_open_context * ctx,struct nfs4_lock_state * lsp,struct nfs_seqid * seqid)7026 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
7027 		struct nfs_open_context *ctx,
7028 		struct nfs4_lock_state *lsp,
7029 		struct nfs_seqid *seqid)
7030 {
7031 	struct nfs4_unlockdata *p;
7032 	struct nfs4_state *state = lsp->ls_state;
7033 	struct inode *inode = state->inode;
7034 
7035 	p = kzalloc(sizeof(*p), GFP_KERNEL);
7036 	if (p == NULL)
7037 		return NULL;
7038 	p->arg.fh = NFS_FH(inode);
7039 	p->arg.fl = &p->fl;
7040 	p->arg.seqid = seqid;
7041 	p->res.seqid = seqid;
7042 	p->lsp = lsp;
7043 	/* Ensure we don't close file until we're done freeing locks! */
7044 	p->ctx = get_nfs_open_context(ctx);
7045 	p->l_ctx = nfs_get_lock_context(ctx);
7046 	locks_init_lock(&p->fl);
7047 	locks_copy_lock(&p->fl, fl);
7048 	p->server = NFS_SERVER(inode);
7049 	spin_lock(&state->state_lock);
7050 	nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
7051 	spin_unlock(&state->state_lock);
7052 	return p;
7053 }
7054 
nfs4_locku_release_calldata(void * data)7055 static void nfs4_locku_release_calldata(void *data)
7056 {
7057 	struct nfs4_unlockdata *calldata = data;
7058 	nfs_free_seqid(calldata->arg.seqid);
7059 	nfs4_put_lock_state(calldata->lsp);
7060 	nfs_put_lock_context(calldata->l_ctx);
7061 	put_nfs_open_context(calldata->ctx);
7062 	kfree(calldata);
7063 }
7064 
nfs4_locku_done(struct rpc_task * task,void * data)7065 static void nfs4_locku_done(struct rpc_task *task, void *data)
7066 {
7067 	struct nfs4_unlockdata *calldata = data;
7068 	struct nfs4_exception exception = {
7069 		.inode = calldata->lsp->ls_state->inode,
7070 		.stateid = &calldata->arg.stateid,
7071 	};
7072 
7073 	if (!nfs4_sequence_done(task, &calldata->res.seq_res))
7074 		return;
7075 	switch (task->tk_status) {
7076 		case 0:
7077 			renew_lease(calldata->server, calldata->timestamp);
7078 			locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
7079 			if (nfs4_update_lock_stateid(calldata->lsp,
7080 					&calldata->res.stateid))
7081 				break;
7082 			fallthrough;
7083 		case -NFS4ERR_ADMIN_REVOKED:
7084 		case -NFS4ERR_EXPIRED:
7085 			nfs4_free_revoked_stateid(calldata->server,
7086 					&calldata->arg.stateid,
7087 					task->tk_msg.rpc_cred);
7088 			fallthrough;
7089 		case -NFS4ERR_BAD_STATEID:
7090 		case -NFS4ERR_STALE_STATEID:
7091 			if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
7092 						calldata->lsp))
7093 				rpc_restart_call_prepare(task);
7094 			break;
7095 		case -NFS4ERR_OLD_STATEID:
7096 			if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
7097 						calldata->lsp))
7098 				rpc_restart_call_prepare(task);
7099 			break;
7100 		default:
7101 			task->tk_status = nfs4_async_handle_exception(task,
7102 					calldata->server, task->tk_status,
7103 					&exception);
7104 			if (exception.retry)
7105 				rpc_restart_call_prepare(task);
7106 	}
7107 	nfs_release_seqid(calldata->arg.seqid);
7108 }
7109 
nfs4_locku_prepare(struct rpc_task * task,void * data)7110 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
7111 {
7112 	struct nfs4_unlockdata *calldata = data;
7113 
7114 	if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
7115 		nfs_async_iocounter_wait(task, calldata->l_ctx))
7116 		return;
7117 
7118 	if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
7119 		goto out_wait;
7120 	if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
7121 		/* Note: exit _without_ running nfs4_locku_done */
7122 		goto out_no_action;
7123 	}
7124 	calldata->timestamp = jiffies;
7125 	if (nfs4_setup_sequence(calldata->server->nfs_client,
7126 				&calldata->arg.seq_args,
7127 				&calldata->res.seq_res,
7128 				task) != 0)
7129 		nfs_release_seqid(calldata->arg.seqid);
7130 	return;
7131 out_no_action:
7132 	task->tk_action = NULL;
7133 out_wait:
7134 	nfs4_sequence_done(task, &calldata->res.seq_res);
7135 }
7136 
7137 static const struct rpc_call_ops nfs4_locku_ops = {
7138 	.rpc_call_prepare = nfs4_locku_prepare,
7139 	.rpc_call_done = nfs4_locku_done,
7140 	.rpc_release = nfs4_locku_release_calldata,
7141 };
7142 
nfs4_do_unlck(struct file_lock * fl,struct nfs_open_context * ctx,struct nfs4_lock_state * lsp,struct nfs_seqid * seqid)7143 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
7144 		struct nfs_open_context *ctx,
7145 		struct nfs4_lock_state *lsp,
7146 		struct nfs_seqid *seqid)
7147 {
7148 	struct nfs4_unlockdata *data;
7149 	struct rpc_message msg = {
7150 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
7151 		.rpc_cred = ctx->cred,
7152 	};
7153 	struct rpc_task_setup task_setup_data = {
7154 		.rpc_client = NFS_CLIENT(lsp->ls_state->inode),
7155 		.rpc_message = &msg,
7156 		.callback_ops = &nfs4_locku_ops,
7157 		.workqueue = nfsiod_workqueue,
7158 		.flags = RPC_TASK_ASYNC,
7159 	};
7160 
7161 	if (nfs_server_capable(lsp->ls_state->inode, NFS_CAP_MOVEABLE))
7162 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
7163 
7164 	nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
7165 		NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
7166 
7167 	/* Ensure this is an unlock - when canceling a lock, the
7168 	 * canceled lock is passed in, and it won't be an unlock.
7169 	 */
7170 	fl->c.flc_type = F_UNLCK;
7171 	if (fl->c.flc_flags & FL_CLOSE)
7172 		set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
7173 
7174 	data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
7175 	if (data == NULL) {
7176 		nfs_free_seqid(seqid);
7177 		return ERR_PTR(-ENOMEM);
7178 	}
7179 
7180 	nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
7181 	msg.rpc_argp = &data->arg;
7182 	msg.rpc_resp = &data->res;
7183 	task_setup_data.callback_data = data;
7184 	return rpc_run_task(&task_setup_data);
7185 }
7186 
nfs4_proc_unlck(struct nfs4_state * state,int cmd,struct file_lock * request)7187 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
7188 {
7189 	struct inode *inode = state->inode;
7190 	struct nfs4_state_owner *sp = state->owner;
7191 	struct nfs_inode *nfsi = NFS_I(inode);
7192 	struct nfs_seqid *seqid;
7193 	struct nfs4_lock_state *lsp;
7194 	struct rpc_task *task;
7195 	struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7196 	int status = 0;
7197 	unsigned char saved_flags = request->c.flc_flags;
7198 
7199 	status = nfs4_set_lock_state(state, request);
7200 	/* Unlock _before_ we do the RPC call */
7201 	request->c.flc_flags |= FL_EXISTS;
7202 	/* Exclude nfs_delegation_claim_locks() */
7203 	mutex_lock(&sp->so_delegreturn_mutex);
7204 	/* Exclude nfs4_reclaim_open_stateid() - note nesting! */
7205 	down_read(&nfsi->rwsem);
7206 	if (locks_lock_inode_wait(inode, request) == -ENOENT) {
7207 		up_read(&nfsi->rwsem);
7208 		mutex_unlock(&sp->so_delegreturn_mutex);
7209 		goto out;
7210 	}
7211 	lsp = request->fl_u.nfs4_fl.owner;
7212 	set_bit(NFS_LOCK_UNLOCKING, &lsp->ls_flags);
7213 	up_read(&nfsi->rwsem);
7214 	mutex_unlock(&sp->so_delegreturn_mutex);
7215 	if (status != 0)
7216 		goto out;
7217 	/* Is this a delegated lock? */
7218 	if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
7219 		goto out;
7220 	alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
7221 	seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
7222 	status = -ENOMEM;
7223 	if (IS_ERR(seqid))
7224 		goto out;
7225 	task = nfs4_do_unlck(request,
7226 			     nfs_file_open_context(request->c.flc_file),
7227 			     lsp, seqid);
7228 	status = PTR_ERR(task);
7229 	if (IS_ERR(task))
7230 		goto out;
7231 	status = rpc_wait_for_completion_task(task);
7232 	rpc_put_task(task);
7233 out:
7234 	request->c.flc_flags = saved_flags;
7235 	trace_nfs4_unlock(request, state, F_SETLK, status);
7236 	return status;
7237 }
7238 
7239 struct nfs4_lockdata {
7240 	struct nfs_lock_args arg;
7241 	struct nfs_lock_res res;
7242 	struct nfs4_lock_state *lsp;
7243 	struct nfs_open_context *ctx;
7244 	struct file_lock fl;
7245 	unsigned long timestamp;
7246 	int rpc_status;
7247 	int cancelled;
7248 	struct nfs_server *server;
7249 };
7250 
nfs4_alloc_lockdata(struct file_lock * fl,struct nfs_open_context * ctx,struct nfs4_lock_state * lsp,gfp_t gfp_mask)7251 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
7252 		struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
7253 		gfp_t gfp_mask)
7254 {
7255 	struct nfs4_lockdata *p;
7256 	struct inode *inode = lsp->ls_state->inode;
7257 	struct nfs_server *server = NFS_SERVER(inode);
7258 	struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7259 
7260 	p = kzalloc(sizeof(*p), gfp_mask);
7261 	if (p == NULL)
7262 		return NULL;
7263 
7264 	p->arg.fh = NFS_FH(inode);
7265 	p->arg.fl = &p->fl;
7266 	p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
7267 	if (IS_ERR(p->arg.open_seqid))
7268 		goto out_free;
7269 	alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
7270 	p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
7271 	if (IS_ERR(p->arg.lock_seqid))
7272 		goto out_free_seqid;
7273 	p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
7274 	p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
7275 	p->arg.lock_owner.s_dev = server->s_dev;
7276 	p->res.lock_seqid = p->arg.lock_seqid;
7277 	p->lsp = lsp;
7278 	p->server = server;
7279 	p->ctx = get_nfs_open_context(ctx);
7280 	locks_init_lock(&p->fl);
7281 	locks_copy_lock(&p->fl, fl);
7282 	return p;
7283 out_free_seqid:
7284 	nfs_free_seqid(p->arg.open_seqid);
7285 out_free:
7286 	kfree(p);
7287 	return NULL;
7288 }
7289 
nfs4_lock_prepare(struct rpc_task * task,void * calldata)7290 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
7291 {
7292 	struct nfs4_lockdata *data = calldata;
7293 	struct nfs4_state *state = data->lsp->ls_state;
7294 
7295 	if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
7296 		goto out_wait;
7297 	/* Do we need to do an open_to_lock_owner? */
7298 	if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
7299 		if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
7300 			goto out_release_lock_seqid;
7301 		}
7302 		nfs4_stateid_copy(&data->arg.open_stateid,
7303 				&state->open_stateid);
7304 		data->arg.new_lock_owner = 1;
7305 		data->res.open_seqid = data->arg.open_seqid;
7306 	} else {
7307 		data->arg.new_lock_owner = 0;
7308 		nfs4_stateid_copy(&data->arg.lock_stateid,
7309 				&data->lsp->ls_stateid);
7310 	}
7311 	if (!nfs4_valid_open_stateid(state)) {
7312 		data->rpc_status = -EBADF;
7313 		task->tk_action = NULL;
7314 		goto out_release_open_seqid;
7315 	}
7316 	data->timestamp = jiffies;
7317 	if (nfs4_setup_sequence(data->server->nfs_client,
7318 				&data->arg.seq_args,
7319 				&data->res.seq_res,
7320 				task) == 0)
7321 		return;
7322 out_release_open_seqid:
7323 	nfs_release_seqid(data->arg.open_seqid);
7324 out_release_lock_seqid:
7325 	nfs_release_seqid(data->arg.lock_seqid);
7326 out_wait:
7327 	nfs4_sequence_done(task, &data->res.seq_res);
7328 	dprintk("%s: ret = %d\n", __func__, data->rpc_status);
7329 }
7330 
nfs4_lock_done(struct rpc_task * task,void * calldata)7331 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7332 {
7333 	struct nfs4_lockdata *data = calldata;
7334 	struct nfs4_lock_state *lsp = data->lsp;
7335 
7336 	if (!nfs4_sequence_done(task, &data->res.seq_res))
7337 		return;
7338 
7339 	data->rpc_status = task->tk_status;
7340 	switch (task->tk_status) {
7341 	case 0:
7342 		renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
7343 				data->timestamp);
7344 		if (data->arg.new_lock && !data->cancelled) {
7345 			data->fl.c.flc_flags &= ~(FL_SLEEP | FL_ACCESS);
7346 			if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
7347 				goto out_restart;
7348 		}
7349 		if (data->arg.new_lock_owner != 0) {
7350 			nfs_confirm_seqid(&lsp->ls_seqid, 0);
7351 			nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7352 			set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
7353 		} else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7354 			goto out_restart;
7355 		break;
7356 	case -NFS4ERR_OLD_STATEID:
7357 		if (data->arg.new_lock_owner != 0 &&
7358 			nfs4_refresh_open_old_stateid(&data->arg.open_stateid,
7359 					lsp->ls_state))
7360 			goto out_restart;
7361 		if (nfs4_refresh_lock_old_stateid(&data->arg.lock_stateid, lsp))
7362 			goto out_restart;
7363 		fallthrough;
7364 	case -NFS4ERR_BAD_STATEID:
7365 	case -NFS4ERR_STALE_STATEID:
7366 	case -NFS4ERR_EXPIRED:
7367 		if (data->arg.new_lock_owner != 0) {
7368 			if (!nfs4_stateid_match(&data->arg.open_stateid,
7369 						&lsp->ls_state->open_stateid))
7370 				goto out_restart;
7371 		} else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7372 						&lsp->ls_stateid))
7373 				goto out_restart;
7374 	}
7375 out_done:
7376 	dprintk("%s: ret = %d!\n", __func__, data->rpc_status);
7377 	return;
7378 out_restart:
7379 	if (!data->cancelled)
7380 		rpc_restart_call_prepare(task);
7381 	goto out_done;
7382 }
7383 
nfs4_lock_release(void * calldata)7384 static void nfs4_lock_release(void *calldata)
7385 {
7386 	struct nfs4_lockdata *data = calldata;
7387 
7388 	nfs_free_seqid(data->arg.open_seqid);
7389 	if (data->cancelled && data->rpc_status == 0) {
7390 		struct rpc_task *task;
7391 		task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7392 				data->arg.lock_seqid);
7393 		if (!IS_ERR(task))
7394 			rpc_put_task_async(task);
7395 		dprintk("%s: cancelling lock!\n", __func__);
7396 	} else
7397 		nfs_free_seqid(data->arg.lock_seqid);
7398 	nfs4_put_lock_state(data->lsp);
7399 	put_nfs_open_context(data->ctx);
7400 	kfree(data);
7401 }
7402 
7403 static const struct rpc_call_ops nfs4_lock_ops = {
7404 	.rpc_call_prepare = nfs4_lock_prepare,
7405 	.rpc_call_done = nfs4_lock_done,
7406 	.rpc_release = nfs4_lock_release,
7407 };
7408 
nfs4_handle_setlk_error(struct nfs_server * server,struct nfs4_lock_state * lsp,int new_lock_owner,int error)7409 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7410 {
7411 	switch (error) {
7412 	case -NFS4ERR_ADMIN_REVOKED:
7413 	case -NFS4ERR_EXPIRED:
7414 	case -NFS4ERR_BAD_STATEID:
7415 		lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7416 		if (new_lock_owner != 0 ||
7417 		   test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7418 			nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7419 		break;
7420 	case -NFS4ERR_STALE_STATEID:
7421 		lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7422 		nfs4_schedule_lease_recovery(server->nfs_client);
7423 	}
7424 }
7425 
_nfs4_do_setlk(struct nfs4_state * state,int cmd,struct file_lock * fl,int recovery_type)7426 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7427 {
7428 	struct nfs4_lockdata *data;
7429 	struct rpc_task *task;
7430 	struct rpc_message msg = {
7431 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7432 		.rpc_cred = state->owner->so_cred,
7433 	};
7434 	struct rpc_task_setup task_setup_data = {
7435 		.rpc_client = NFS_CLIENT(state->inode),
7436 		.rpc_message = &msg,
7437 		.callback_ops = &nfs4_lock_ops,
7438 		.workqueue = nfsiod_workqueue,
7439 		.flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7440 	};
7441 	int ret;
7442 
7443 	if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
7444 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
7445 
7446 	data = nfs4_alloc_lockdata(fl,
7447 				   nfs_file_open_context(fl->c.flc_file),
7448 				   fl->fl_u.nfs4_fl.owner, GFP_KERNEL);
7449 	if (data == NULL)
7450 		return -ENOMEM;
7451 	if (IS_SETLKW(cmd))
7452 		data->arg.block = 1;
7453 	nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7454 				recovery_type > NFS_LOCK_NEW);
7455 	msg.rpc_argp = &data->arg;
7456 	msg.rpc_resp = &data->res;
7457 	task_setup_data.callback_data = data;
7458 	if (recovery_type > NFS_LOCK_NEW) {
7459 		if (recovery_type == NFS_LOCK_RECLAIM)
7460 			data->arg.reclaim = NFS_LOCK_RECLAIM;
7461 	} else
7462 		data->arg.new_lock = 1;
7463 	task = rpc_run_task(&task_setup_data);
7464 	if (IS_ERR(task))
7465 		return PTR_ERR(task);
7466 	ret = rpc_wait_for_completion_task(task);
7467 	if (ret == 0) {
7468 		ret = data->rpc_status;
7469 		if (ret)
7470 			nfs4_handle_setlk_error(data->server, data->lsp,
7471 					data->arg.new_lock_owner, ret);
7472 	} else
7473 		data->cancelled = true;
7474 	trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7475 	rpc_put_task(task);
7476 	dprintk("%s: ret = %d\n", __func__, ret);
7477 	return ret;
7478 }
7479 
nfs4_lock_reclaim(struct nfs4_state * state,struct file_lock * request)7480 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7481 {
7482 	struct nfs_server *server = NFS_SERVER(state->inode);
7483 	struct nfs4_exception exception = {
7484 		.inode = state->inode,
7485 	};
7486 	int err;
7487 
7488 	do {
7489 		/* Cache the lock if possible... */
7490 		if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7491 			return 0;
7492 		err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7493 		if (err != -NFS4ERR_DELAY)
7494 			break;
7495 		nfs4_handle_exception(server, err, &exception);
7496 	} while (exception.retry);
7497 	return err;
7498 }
7499 
nfs4_lock_expired(struct nfs4_state * state,struct file_lock * request)7500 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7501 {
7502 	struct nfs_server *server = NFS_SERVER(state->inode);
7503 	struct nfs4_exception exception = {
7504 		.inode = state->inode,
7505 	};
7506 	int err;
7507 
7508 	err = nfs4_set_lock_state(state, request);
7509 	if (err != 0)
7510 		return err;
7511 	if (!recover_lost_locks) {
7512 		set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7513 		return 0;
7514 	}
7515 	do {
7516 		if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7517 			return 0;
7518 		err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7519 		switch (err) {
7520 		default:
7521 			goto out;
7522 		case -NFS4ERR_GRACE:
7523 		case -NFS4ERR_DELAY:
7524 			nfs4_handle_exception(server, err, &exception);
7525 			err = 0;
7526 		}
7527 	} while (exception.retry);
7528 out:
7529 	return err;
7530 }
7531 
7532 #if defined(CONFIG_NFS_V4_1)
nfs41_lock_expired(struct nfs4_state * state,struct file_lock * request)7533 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7534 {
7535 	struct nfs4_lock_state *lsp;
7536 	int status;
7537 
7538 	status = nfs4_set_lock_state(state, request);
7539 	if (status != 0)
7540 		return status;
7541 	lsp = request->fl_u.nfs4_fl.owner;
7542 	if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7543 	    test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7544 		return 0;
7545 	return nfs4_lock_expired(state, request);
7546 }
7547 #endif
7548 
_nfs4_proc_setlk(struct nfs4_state * state,int cmd,struct file_lock * request)7549 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7550 {
7551 	struct nfs_inode *nfsi = NFS_I(state->inode);
7552 	struct nfs4_state_owner *sp = state->owner;
7553 	unsigned char flags = request->c.flc_flags;
7554 	int status;
7555 
7556 	request->c.flc_flags |= FL_ACCESS;
7557 	status = locks_lock_inode_wait(state->inode, request);
7558 	if (status < 0)
7559 		goto out;
7560 	mutex_lock(&sp->so_delegreturn_mutex);
7561 	down_read(&nfsi->rwsem);
7562 	if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7563 		/* Yes: cache locks! */
7564 		/* ...but avoid races with delegation recall... */
7565 		request->c.flc_flags = flags & ~FL_SLEEP;
7566 		status = locks_lock_inode_wait(state->inode, request);
7567 		up_read(&nfsi->rwsem);
7568 		mutex_unlock(&sp->so_delegreturn_mutex);
7569 		goto out;
7570 	}
7571 	up_read(&nfsi->rwsem);
7572 	mutex_unlock(&sp->so_delegreturn_mutex);
7573 	status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7574 out:
7575 	request->c.flc_flags = flags;
7576 	return status;
7577 }
7578 
nfs4_proc_setlk(struct nfs4_state * state,int cmd,struct file_lock * request)7579 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7580 {
7581 	struct nfs4_exception exception = {
7582 		.state = state,
7583 		.inode = state->inode,
7584 		.interruptible = true,
7585 	};
7586 	int err;
7587 
7588 	do {
7589 		err = _nfs4_proc_setlk(state, cmd, request);
7590 		if (err == -NFS4ERR_DENIED)
7591 			err = -EAGAIN;
7592 		err = nfs4_handle_exception(NFS_SERVER(state->inode),
7593 				err, &exception);
7594 	} while (exception.retry);
7595 	return err;
7596 }
7597 
7598 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7599 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7600 
7601 static int
nfs4_retry_setlk_simple(struct nfs4_state * state,int cmd,struct file_lock * request)7602 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7603 			struct file_lock *request)
7604 {
7605 	int		status = -ERESTARTSYS;
7606 	unsigned long	timeout = NFS4_LOCK_MINTIMEOUT;
7607 
7608 	while(!signalled()) {
7609 		status = nfs4_proc_setlk(state, cmd, request);
7610 		if ((status != -EAGAIN) || IS_SETLK(cmd))
7611 			break;
7612 		__set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
7613 		schedule_timeout(timeout);
7614 		timeout *= 2;
7615 		timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7616 		status = -ERESTARTSYS;
7617 	}
7618 	return status;
7619 }
7620 
7621 #ifdef CONFIG_NFS_V4_1
7622 struct nfs4_lock_waiter {
7623 	struct inode		*inode;
7624 	struct nfs_lowner	owner;
7625 	wait_queue_entry_t	wait;
7626 };
7627 
7628 static int
nfs4_wake_lock_waiter(wait_queue_entry_t * wait,unsigned int mode,int flags,void * key)7629 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7630 {
7631 	struct nfs4_lock_waiter	*waiter	=
7632 		container_of(wait, struct nfs4_lock_waiter, wait);
7633 
7634 	/* NULL key means to wake up everyone */
7635 	if (key) {
7636 		struct cb_notify_lock_args	*cbnl = key;
7637 		struct nfs_lowner		*lowner = &cbnl->cbnl_owner,
7638 						*wowner = &waiter->owner;
7639 
7640 		/* Only wake if the callback was for the same owner. */
7641 		if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7642 			return 0;
7643 
7644 		/* Make sure it's for the right inode */
7645 		if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7646 			return 0;
7647 	}
7648 
7649 	return woken_wake_function(wait, mode, flags, key);
7650 }
7651 
7652 static int
nfs4_retry_setlk(struct nfs4_state * state,int cmd,struct file_lock * request)7653 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7654 {
7655 	struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7656 	struct nfs_server *server = NFS_SERVER(state->inode);
7657 	struct nfs_client *clp = server->nfs_client;
7658 	wait_queue_head_t *q = &clp->cl_lock_waitq;
7659 	struct nfs4_lock_waiter waiter = {
7660 		.inode = state->inode,
7661 		.owner = { .clientid = clp->cl_clientid,
7662 			   .id = lsp->ls_seqid.owner_id,
7663 			   .s_dev = server->s_dev },
7664 	};
7665 	int status;
7666 
7667 	/* Don't bother with waitqueue if we don't expect a callback */
7668 	if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7669 		return nfs4_retry_setlk_simple(state, cmd, request);
7670 
7671 	init_wait(&waiter.wait);
7672 	waiter.wait.func = nfs4_wake_lock_waiter;
7673 	add_wait_queue(q, &waiter.wait);
7674 
7675 	do {
7676 		status = nfs4_proc_setlk(state, cmd, request);
7677 		if (status != -EAGAIN || IS_SETLK(cmd))
7678 			break;
7679 
7680 		status = -ERESTARTSYS;
7681 		wait_woken(&waiter.wait, TASK_INTERRUPTIBLE|TASK_FREEZABLE,
7682 			   NFS4_LOCK_MAXTIMEOUT);
7683 	} while (!signalled());
7684 
7685 	remove_wait_queue(q, &waiter.wait);
7686 
7687 	return status;
7688 }
7689 #else /* !CONFIG_NFS_V4_1 */
7690 static inline int
nfs4_retry_setlk(struct nfs4_state * state,int cmd,struct file_lock * request)7691 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7692 {
7693 	return nfs4_retry_setlk_simple(state, cmd, request);
7694 }
7695 #endif
7696 
7697 static int
nfs4_proc_lock(struct file * filp,int cmd,struct file_lock * request)7698 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7699 {
7700 	struct nfs_open_context *ctx;
7701 	struct nfs4_state *state;
7702 	int status;
7703 
7704 	/* verify open state */
7705 	ctx = nfs_file_open_context(filp);
7706 	state = ctx->state;
7707 
7708 	if (IS_GETLK(cmd)) {
7709 		if (state != NULL)
7710 			return nfs4_proc_getlk(state, F_GETLK, request);
7711 		return 0;
7712 	}
7713 
7714 	if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7715 		return -EINVAL;
7716 
7717 	if (lock_is_unlock(request)) {
7718 		if (state != NULL)
7719 			return nfs4_proc_unlck(state, cmd, request);
7720 		return 0;
7721 	}
7722 
7723 	if (state == NULL)
7724 		return -ENOLCK;
7725 
7726 	if ((request->c.flc_flags & FL_POSIX) &&
7727 	    !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7728 		return -ENOLCK;
7729 
7730 	/*
7731 	 * Don't rely on the VFS having checked the file open mode,
7732 	 * since it won't do this for flock() locks.
7733 	 */
7734 	switch (request->c.flc_type) {
7735 	case F_RDLCK:
7736 		if (!(filp->f_mode & FMODE_READ))
7737 			return -EBADF;
7738 		break;
7739 	case F_WRLCK:
7740 		if (!(filp->f_mode & FMODE_WRITE))
7741 			return -EBADF;
7742 	}
7743 
7744 	status = nfs4_set_lock_state(state, request);
7745 	if (status != 0)
7746 		return status;
7747 
7748 	return nfs4_retry_setlk(state, cmd, request);
7749 }
7750 
nfs4_delete_lease(struct file * file,void ** priv)7751 static int nfs4_delete_lease(struct file *file, void **priv)
7752 {
7753 	return generic_setlease(file, F_UNLCK, NULL, priv);
7754 }
7755 
nfs4_add_lease(struct file * file,int arg,struct file_lease ** lease,void ** priv)7756 static int nfs4_add_lease(struct file *file, int arg, struct file_lease **lease,
7757 			  void **priv)
7758 {
7759 	struct inode *inode = file_inode(file);
7760 	fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE;
7761 	int ret;
7762 
7763 	/* No delegation, no lease */
7764 	if (!nfs4_have_delegation(inode, type, 0))
7765 		return -EAGAIN;
7766 	ret = generic_setlease(file, arg, lease, priv);
7767 	if (ret || nfs4_have_delegation(inode, type, 0))
7768 		return ret;
7769 	/* We raced with a delegation return */
7770 	nfs4_delete_lease(file, priv);
7771 	return -EAGAIN;
7772 }
7773 
nfs4_proc_setlease(struct file * file,int arg,struct file_lease ** lease,void ** priv)7774 int nfs4_proc_setlease(struct file *file, int arg, struct file_lease **lease,
7775 		       void **priv)
7776 {
7777 	switch (arg) {
7778 	case F_RDLCK:
7779 	case F_WRLCK:
7780 		return nfs4_add_lease(file, arg, lease, priv);
7781 	case F_UNLCK:
7782 		return nfs4_delete_lease(file, priv);
7783 	default:
7784 		return -EINVAL;
7785 	}
7786 }
7787 
nfs4_lock_delegation_recall(struct file_lock * fl,struct nfs4_state * state,const nfs4_stateid * stateid)7788 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7789 {
7790 	struct nfs_server *server = NFS_SERVER(state->inode);
7791 	int err;
7792 
7793 	err = nfs4_set_lock_state(state, fl);
7794 	if (err != 0)
7795 		return err;
7796 	do {
7797 		err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7798 		if (err != -NFS4ERR_DELAY)
7799 			break;
7800 		ssleep(1);
7801 	} while (err == -NFS4ERR_DELAY);
7802 	return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7803 }
7804 
7805 struct nfs_release_lockowner_data {
7806 	struct nfs4_lock_state *lsp;
7807 	struct nfs_server *server;
7808 	struct nfs_release_lockowner_args args;
7809 	struct nfs_release_lockowner_res res;
7810 	unsigned long timestamp;
7811 };
7812 
nfs4_release_lockowner_prepare(struct rpc_task * task,void * calldata)7813 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7814 {
7815 	struct nfs_release_lockowner_data *data = calldata;
7816 	struct nfs_server *server = data->server;
7817 	nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7818 			   &data->res.seq_res, task);
7819 	data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7820 	data->timestamp = jiffies;
7821 }
7822 
nfs4_release_lockowner_done(struct rpc_task * task,void * calldata)7823 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7824 {
7825 	struct nfs_release_lockowner_data *data = calldata;
7826 	struct nfs_server *server = data->server;
7827 
7828 	nfs40_sequence_done(task, &data->res.seq_res);
7829 
7830 	switch (task->tk_status) {
7831 	case 0:
7832 		renew_lease(server, data->timestamp);
7833 		break;
7834 	case -NFS4ERR_STALE_CLIENTID:
7835 	case -NFS4ERR_EXPIRED:
7836 		nfs4_schedule_lease_recovery(server->nfs_client);
7837 		break;
7838 	case -NFS4ERR_LEASE_MOVED:
7839 	case -NFS4ERR_DELAY:
7840 		if (nfs4_async_handle_error(task, server,
7841 					    NULL, NULL) == -EAGAIN)
7842 			rpc_restart_call_prepare(task);
7843 	}
7844 }
7845 
nfs4_release_lockowner_release(void * calldata)7846 static void nfs4_release_lockowner_release(void *calldata)
7847 {
7848 	struct nfs_release_lockowner_data *data = calldata;
7849 	nfs4_free_lock_state(data->server, data->lsp);
7850 	kfree(calldata);
7851 }
7852 
7853 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7854 	.rpc_call_prepare = nfs4_release_lockowner_prepare,
7855 	.rpc_call_done = nfs4_release_lockowner_done,
7856 	.rpc_release = nfs4_release_lockowner_release,
7857 };
7858 
7859 static void
nfs4_release_lockowner(struct nfs_server * server,struct nfs4_lock_state * lsp)7860 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7861 {
7862 	struct nfs_release_lockowner_data *data;
7863 	struct rpc_message msg = {
7864 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7865 	};
7866 
7867 	if (server->nfs_client->cl_mvops->minor_version != 0)
7868 		return;
7869 
7870 	data = kmalloc(sizeof(*data), GFP_KERNEL);
7871 	if (!data)
7872 		return;
7873 	data->lsp = lsp;
7874 	data->server = server;
7875 	data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7876 	data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7877 	data->args.lock_owner.s_dev = server->s_dev;
7878 
7879 	msg.rpc_argp = &data->args;
7880 	msg.rpc_resp = &data->res;
7881 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7882 	rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7883 }
7884 
7885 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7886 
nfs4_xattr_set_nfs4_acl(const struct xattr_handler * handler,struct mnt_idmap * idmap,struct dentry * unused,struct inode * inode,const char * key,const void * buf,size_t buflen,int flags)7887 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7888 				   struct mnt_idmap *idmap,
7889 				   struct dentry *unused, struct inode *inode,
7890 				   const char *key, const void *buf,
7891 				   size_t buflen, int flags)
7892 {
7893 	return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_ACL);
7894 }
7895 
nfs4_xattr_get_nfs4_acl(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * key,void * buf,size_t buflen)7896 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7897 				   struct dentry *unused, struct inode *inode,
7898 				   const char *key, void *buf, size_t buflen)
7899 {
7900 	return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_ACL);
7901 }
7902 
nfs4_xattr_list_nfs4_acl(struct dentry * dentry)7903 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7904 {
7905 	return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_ACL);
7906 }
7907 
7908 #if defined(CONFIG_NFS_V4_1)
7909 #define XATTR_NAME_NFSV4_DACL "system.nfs4_dacl"
7910 
nfs4_xattr_set_nfs4_dacl(const struct xattr_handler * handler,struct mnt_idmap * idmap,struct dentry * unused,struct inode * inode,const char * key,const void * buf,size_t buflen,int flags)7911 static int nfs4_xattr_set_nfs4_dacl(const struct xattr_handler *handler,
7912 				    struct mnt_idmap *idmap,
7913 				    struct dentry *unused, struct inode *inode,
7914 				    const char *key, const void *buf,
7915 				    size_t buflen, int flags)
7916 {
7917 	return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_DACL);
7918 }
7919 
nfs4_xattr_get_nfs4_dacl(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * key,void * buf,size_t buflen)7920 static int nfs4_xattr_get_nfs4_dacl(const struct xattr_handler *handler,
7921 				    struct dentry *unused, struct inode *inode,
7922 				    const char *key, void *buf, size_t buflen)
7923 {
7924 	return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_DACL);
7925 }
7926 
nfs4_xattr_list_nfs4_dacl(struct dentry * dentry)7927 static bool nfs4_xattr_list_nfs4_dacl(struct dentry *dentry)
7928 {
7929 	return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_DACL);
7930 }
7931 
7932 #define XATTR_NAME_NFSV4_SACL "system.nfs4_sacl"
7933 
nfs4_xattr_set_nfs4_sacl(const struct xattr_handler * handler,struct mnt_idmap * idmap,struct dentry * unused,struct inode * inode,const char * key,const void * buf,size_t buflen,int flags)7934 static int nfs4_xattr_set_nfs4_sacl(const struct xattr_handler *handler,
7935 				    struct mnt_idmap *idmap,
7936 				    struct dentry *unused, struct inode *inode,
7937 				    const char *key, const void *buf,
7938 				    size_t buflen, int flags)
7939 {
7940 	return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_SACL);
7941 }
7942 
nfs4_xattr_get_nfs4_sacl(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * key,void * buf,size_t buflen)7943 static int nfs4_xattr_get_nfs4_sacl(const struct xattr_handler *handler,
7944 				    struct dentry *unused, struct inode *inode,
7945 				    const char *key, void *buf, size_t buflen)
7946 {
7947 	return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_SACL);
7948 }
7949 
nfs4_xattr_list_nfs4_sacl(struct dentry * dentry)7950 static bool nfs4_xattr_list_nfs4_sacl(struct dentry *dentry)
7951 {
7952 	return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_SACL);
7953 }
7954 
7955 #endif
7956 
7957 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7958 
nfs4_xattr_set_nfs4_label(const struct xattr_handler * handler,struct mnt_idmap * idmap,struct dentry * unused,struct inode * inode,const char * key,const void * buf,size_t buflen,int flags)7959 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7960 				     struct mnt_idmap *idmap,
7961 				     struct dentry *unused, struct inode *inode,
7962 				     const char *key, const void *buf,
7963 				     size_t buflen, int flags)
7964 {
7965 	if (security_ismaclabel(key))
7966 		return nfs4_set_security_label(inode, buf, buflen);
7967 
7968 	return -EOPNOTSUPP;
7969 }
7970 
nfs4_xattr_get_nfs4_label(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * key,void * buf,size_t buflen)7971 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7972 				     struct dentry *unused, struct inode *inode,
7973 				     const char *key, void *buf, size_t buflen)
7974 {
7975 	if (security_ismaclabel(key))
7976 		return nfs4_get_security_label(inode, buf, buflen);
7977 	return -EOPNOTSUPP;
7978 }
7979 
7980 static ssize_t
nfs4_listxattr_nfs4_label(struct inode * inode,char * list,size_t list_len)7981 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7982 {
7983 	int len = 0;
7984 
7985 	if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7986 		len = security_inode_listsecurity(inode, list, list_len);
7987 		if (len >= 0 && list_len && len > list_len)
7988 			return -ERANGE;
7989 	}
7990 	return len;
7991 }
7992 
7993 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7994 	.prefix = XATTR_SECURITY_PREFIX,
7995 	.get	= nfs4_xattr_get_nfs4_label,
7996 	.set	= nfs4_xattr_set_nfs4_label,
7997 };
7998 
7999 #else
8000 
8001 static ssize_t
nfs4_listxattr_nfs4_label(struct inode * inode,char * list,size_t list_len)8002 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
8003 {
8004 	return 0;
8005 }
8006 
8007 #endif
8008 
8009 #ifdef CONFIG_NFS_V4_2
nfs4_xattr_set_nfs4_user(const struct xattr_handler * handler,struct mnt_idmap * idmap,struct dentry * unused,struct inode * inode,const char * key,const void * buf,size_t buflen,int flags)8010 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
8011 				    struct mnt_idmap *idmap,
8012 				    struct dentry *unused, struct inode *inode,
8013 				    const char *key, const void *buf,
8014 				    size_t buflen, int flags)
8015 {
8016 	u32 mask;
8017 	int ret;
8018 
8019 	if (!nfs_server_capable(inode, NFS_CAP_XATTR))
8020 		return -EOPNOTSUPP;
8021 
8022 	/*
8023 	 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
8024 	 * flags right now. Handling of xattr operations use the normal
8025 	 * file read/write permissions.
8026 	 *
8027 	 * Just in case the server has other ideas (which RFC 8276 allows),
8028 	 * do a cached access check for the XA* flags to possibly avoid
8029 	 * doing an RPC and getting EACCES back.
8030 	 */
8031 	if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
8032 		if (!(mask & NFS_ACCESS_XAWRITE))
8033 			return -EACCES;
8034 	}
8035 
8036 	if (buf == NULL) {
8037 		ret = nfs42_proc_removexattr(inode, key);
8038 		if (!ret)
8039 			nfs4_xattr_cache_remove(inode, key);
8040 	} else {
8041 		ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
8042 		if (!ret)
8043 			nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
8044 	}
8045 
8046 	return ret;
8047 }
8048 
nfs4_xattr_get_nfs4_user(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * key,void * buf,size_t buflen)8049 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
8050 				    struct dentry *unused, struct inode *inode,
8051 				    const char *key, void *buf, size_t buflen)
8052 {
8053 	u32 mask;
8054 	ssize_t ret;
8055 
8056 	if (!nfs_server_capable(inode, NFS_CAP_XATTR))
8057 		return -EOPNOTSUPP;
8058 
8059 	if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
8060 		if (!(mask & NFS_ACCESS_XAREAD))
8061 			return -EACCES;
8062 	}
8063 
8064 	ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
8065 	if (ret)
8066 		return ret;
8067 
8068 	ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
8069 	if (ret >= 0 || (ret < 0 && ret != -ENOENT))
8070 		return ret;
8071 
8072 	ret = nfs42_proc_getxattr(inode, key, buf, buflen);
8073 
8074 	return ret;
8075 }
8076 
8077 static ssize_t
nfs4_listxattr_nfs4_user(struct inode * inode,char * list,size_t list_len)8078 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
8079 {
8080 	u64 cookie;
8081 	bool eof;
8082 	ssize_t ret, size;
8083 	char *buf;
8084 	size_t buflen;
8085 	u32 mask;
8086 
8087 	if (!nfs_server_capable(inode, NFS_CAP_XATTR))
8088 		return 0;
8089 
8090 	if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
8091 		if (!(mask & NFS_ACCESS_XALIST))
8092 			return 0;
8093 	}
8094 
8095 	ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
8096 	if (ret)
8097 		return ret;
8098 
8099 	ret = nfs4_xattr_cache_list(inode, list, list_len);
8100 	if (ret >= 0 || (ret < 0 && ret != -ENOENT))
8101 		return ret;
8102 
8103 	cookie = 0;
8104 	eof = false;
8105 	buflen = list_len ? list_len : XATTR_LIST_MAX;
8106 	buf = list_len ? list : NULL;
8107 	size = 0;
8108 
8109 	while (!eof) {
8110 		ret = nfs42_proc_listxattrs(inode, buf, buflen,
8111 		    &cookie, &eof);
8112 		if (ret < 0)
8113 			return ret;
8114 
8115 		if (list_len) {
8116 			buf += ret;
8117 			buflen -= ret;
8118 		}
8119 		size += ret;
8120 	}
8121 
8122 	if (list_len)
8123 		nfs4_xattr_cache_set_list(inode, list, size);
8124 
8125 	return size;
8126 }
8127 
8128 #else
8129 
8130 static ssize_t
nfs4_listxattr_nfs4_user(struct inode * inode,char * list,size_t list_len)8131 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
8132 {
8133 	return 0;
8134 }
8135 #endif /* CONFIG_NFS_V4_2 */
8136 
8137 /*
8138  * nfs_fhget will use either the mounted_on_fileid or the fileid
8139  */
nfs_fixup_referral_attributes(struct nfs_fattr * fattr)8140 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
8141 {
8142 	if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
8143 	       (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
8144 	      (fattr->valid & NFS_ATTR_FATTR_FSID) &&
8145 	      (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
8146 		return;
8147 
8148 	fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
8149 		NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
8150 	fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
8151 	fattr->nlink = 2;
8152 }
8153 
_nfs4_proc_fs_locations(struct rpc_clnt * client,struct inode * dir,const struct qstr * name,struct nfs4_fs_locations * fs_locations,struct page * page)8154 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
8155 				   const struct qstr *name,
8156 				   struct nfs4_fs_locations *fs_locations,
8157 				   struct page *page)
8158 {
8159 	struct nfs_server *server = NFS_SERVER(dir);
8160 	u32 bitmask[3];
8161 	struct nfs4_fs_locations_arg args = {
8162 		.dir_fh = NFS_FH(dir),
8163 		.name = name,
8164 		.page = page,
8165 		.bitmask = bitmask,
8166 	};
8167 	struct nfs4_fs_locations_res res = {
8168 		.fs_locations = fs_locations,
8169 	};
8170 	struct rpc_message msg = {
8171 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8172 		.rpc_argp = &args,
8173 		.rpc_resp = &res,
8174 	};
8175 	int status;
8176 
8177 	dprintk("%s: start\n", __func__);
8178 
8179 	bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
8180 	bitmask[1] = nfs4_fattr_bitmap[1];
8181 
8182 	/* Ask for the fileid of the absent filesystem if mounted_on_fileid
8183 	 * is not supported */
8184 	if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
8185 		bitmask[0] &= ~FATTR4_WORD0_FILEID;
8186 	else
8187 		bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
8188 
8189 	nfs_fattr_init(fs_locations->fattr);
8190 	fs_locations->server = server;
8191 	fs_locations->nlocations = 0;
8192 	status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
8193 	dprintk("%s: returned status = %d\n", __func__, status);
8194 	return status;
8195 }
8196 
nfs4_proc_fs_locations(struct rpc_clnt * client,struct inode * dir,const struct qstr * name,struct nfs4_fs_locations * fs_locations,struct page * page)8197 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
8198 			   const struct qstr *name,
8199 			   struct nfs4_fs_locations *fs_locations,
8200 			   struct page *page)
8201 {
8202 	struct nfs4_exception exception = {
8203 		.interruptible = true,
8204 	};
8205 	int err;
8206 	do {
8207 		err = _nfs4_proc_fs_locations(client, dir, name,
8208 				fs_locations, page);
8209 		trace_nfs4_get_fs_locations(dir, name, err);
8210 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
8211 				&exception);
8212 	} while (exception.retry);
8213 	return err;
8214 }
8215 
8216 /*
8217  * This operation also signals the server that this client is
8218  * performing migration recovery.  The server can stop returning
8219  * NFS4ERR_LEASE_MOVED to this client.  A RENEW operation is
8220  * appended to this compound to identify the client ID which is
8221  * performing recovery.
8222  */
_nfs40_proc_get_locations(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs4_fs_locations * locations,struct page * page,const struct cred * cred)8223 static int _nfs40_proc_get_locations(struct nfs_server *server,
8224 				     struct nfs_fh *fhandle,
8225 				     struct nfs4_fs_locations *locations,
8226 				     struct page *page, const struct cred *cred)
8227 {
8228 	struct rpc_clnt *clnt = server->client;
8229 	u32 bitmask[2] = {
8230 		[0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8231 	};
8232 	struct nfs4_fs_locations_arg args = {
8233 		.clientid	= server->nfs_client->cl_clientid,
8234 		.fh		= fhandle,
8235 		.page		= page,
8236 		.bitmask	= bitmask,
8237 		.migration	= 1,		/* skip LOOKUP */
8238 		.renew		= 1,		/* append RENEW */
8239 	};
8240 	struct nfs4_fs_locations_res res = {
8241 		.fs_locations	= locations,
8242 		.migration	= 1,
8243 		.renew		= 1,
8244 	};
8245 	struct rpc_message msg = {
8246 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8247 		.rpc_argp	= &args,
8248 		.rpc_resp	= &res,
8249 		.rpc_cred	= cred,
8250 	};
8251 	unsigned long now = jiffies;
8252 	int status;
8253 
8254 	nfs_fattr_init(locations->fattr);
8255 	locations->server = server;
8256 	locations->nlocations = 0;
8257 
8258 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8259 	status = nfs4_call_sync_sequence(clnt, server, &msg,
8260 					&args.seq_args, &res.seq_res);
8261 	if (status)
8262 		return status;
8263 
8264 	renew_lease(server, now);
8265 	return 0;
8266 }
8267 
8268 #ifdef CONFIG_NFS_V4_1
8269 
8270 /*
8271  * This operation also signals the server that this client is
8272  * performing migration recovery.  The server can stop asserting
8273  * SEQ4_STATUS_LEASE_MOVED for this client.  The client ID
8274  * performing this operation is identified in the SEQUENCE
8275  * operation in this compound.
8276  *
8277  * When the client supports GETATTR(fs_locations_info), it can
8278  * be plumbed in here.
8279  */
_nfs41_proc_get_locations(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs4_fs_locations * locations,struct page * page,const struct cred * cred)8280 static int _nfs41_proc_get_locations(struct nfs_server *server,
8281 				     struct nfs_fh *fhandle,
8282 				     struct nfs4_fs_locations *locations,
8283 				     struct page *page, const struct cred *cred)
8284 {
8285 	struct rpc_clnt *clnt = server->client;
8286 	u32 bitmask[2] = {
8287 		[0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8288 	};
8289 	struct nfs4_fs_locations_arg args = {
8290 		.fh		= fhandle,
8291 		.page		= page,
8292 		.bitmask	= bitmask,
8293 		.migration	= 1,		/* skip LOOKUP */
8294 	};
8295 	struct nfs4_fs_locations_res res = {
8296 		.fs_locations	= locations,
8297 		.migration	= 1,
8298 	};
8299 	struct rpc_message msg = {
8300 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8301 		.rpc_argp	= &args,
8302 		.rpc_resp	= &res,
8303 		.rpc_cred	= cred,
8304 	};
8305 	struct nfs4_call_sync_data data = {
8306 		.seq_server = server,
8307 		.seq_args = &args.seq_args,
8308 		.seq_res = &res.seq_res,
8309 	};
8310 	struct rpc_task_setup task_setup_data = {
8311 		.rpc_client = clnt,
8312 		.rpc_message = &msg,
8313 		.callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
8314 		.callback_data = &data,
8315 		.flags = RPC_TASK_NO_ROUND_ROBIN,
8316 	};
8317 	int status;
8318 
8319 	nfs_fattr_init(locations->fattr);
8320 	locations->server = server;
8321 	locations->nlocations = 0;
8322 
8323 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8324 	status = nfs4_call_sync_custom(&task_setup_data);
8325 	if (status == NFS4_OK &&
8326 	    res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8327 		status = -NFS4ERR_LEASE_MOVED;
8328 	return status;
8329 }
8330 
8331 #endif	/* CONFIG_NFS_V4_1 */
8332 
8333 /**
8334  * nfs4_proc_get_locations - discover locations for a migrated FSID
8335  * @server: pointer to nfs_server to process
8336  * @fhandle: pointer to the kernel NFS client file handle
8337  * @locations: result of query
8338  * @page: buffer
8339  * @cred: credential to use for this operation
8340  *
8341  * Returns NFS4_OK on success, a negative NFS4ERR status code if the
8342  * operation failed, or a negative errno if a local error occurred.
8343  *
8344  * On success, "locations" is filled in, but if the server has
8345  * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
8346  * asserted.
8347  *
8348  * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
8349  * from this client that require migration recovery.
8350  */
nfs4_proc_get_locations(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs4_fs_locations * locations,struct page * page,const struct cred * cred)8351 int nfs4_proc_get_locations(struct nfs_server *server,
8352 			    struct nfs_fh *fhandle,
8353 			    struct nfs4_fs_locations *locations,
8354 			    struct page *page, const struct cred *cred)
8355 {
8356 	struct nfs_client *clp = server->nfs_client;
8357 	const struct nfs4_mig_recovery_ops *ops =
8358 					clp->cl_mvops->mig_recovery_ops;
8359 	struct nfs4_exception exception = {
8360 		.interruptible = true,
8361 	};
8362 	int status;
8363 
8364 	dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8365 		(unsigned long long)server->fsid.major,
8366 		(unsigned long long)server->fsid.minor,
8367 		clp->cl_hostname);
8368 	nfs_display_fhandle(fhandle, __func__);
8369 
8370 	do {
8371 		status = ops->get_locations(server, fhandle, locations, page,
8372 					    cred);
8373 		if (status != -NFS4ERR_DELAY)
8374 			break;
8375 		nfs4_handle_exception(server, status, &exception);
8376 	} while (exception.retry);
8377 	return status;
8378 }
8379 
8380 /*
8381  * This operation also signals the server that this client is
8382  * performing "lease moved" recovery.  The server can stop
8383  * returning NFS4ERR_LEASE_MOVED to this client.  A RENEW operation
8384  * is appended to this compound to identify the client ID which is
8385  * performing recovery.
8386  */
_nfs40_proc_fsid_present(struct inode * inode,const struct cred * cred)8387 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
8388 {
8389 	struct nfs_server *server = NFS_SERVER(inode);
8390 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
8391 	struct rpc_clnt *clnt = server->client;
8392 	struct nfs4_fsid_present_arg args = {
8393 		.fh		= NFS_FH(inode),
8394 		.clientid	= clp->cl_clientid,
8395 		.renew		= 1,		/* append RENEW */
8396 	};
8397 	struct nfs4_fsid_present_res res = {
8398 		.renew		= 1,
8399 	};
8400 	struct rpc_message msg = {
8401 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8402 		.rpc_argp	= &args,
8403 		.rpc_resp	= &res,
8404 		.rpc_cred	= cred,
8405 	};
8406 	unsigned long now = jiffies;
8407 	int status;
8408 
8409 	res.fh = nfs_alloc_fhandle();
8410 	if (res.fh == NULL)
8411 		return -ENOMEM;
8412 
8413 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8414 	status = nfs4_call_sync_sequence(clnt, server, &msg,
8415 						&args.seq_args, &res.seq_res);
8416 	nfs_free_fhandle(res.fh);
8417 	if (status)
8418 		return status;
8419 
8420 	do_renew_lease(clp, now);
8421 	return 0;
8422 }
8423 
8424 #ifdef CONFIG_NFS_V4_1
8425 
8426 /*
8427  * This operation also signals the server that this client is
8428  * performing "lease moved" recovery.  The server can stop asserting
8429  * SEQ4_STATUS_LEASE_MOVED for this client.  The client ID performing
8430  * this operation is identified in the SEQUENCE operation in this
8431  * compound.
8432  */
_nfs41_proc_fsid_present(struct inode * inode,const struct cred * cred)8433 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
8434 {
8435 	struct nfs_server *server = NFS_SERVER(inode);
8436 	struct rpc_clnt *clnt = server->client;
8437 	struct nfs4_fsid_present_arg args = {
8438 		.fh		= NFS_FH(inode),
8439 	};
8440 	struct nfs4_fsid_present_res res = {
8441 	};
8442 	struct rpc_message msg = {
8443 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8444 		.rpc_argp	= &args,
8445 		.rpc_resp	= &res,
8446 		.rpc_cred	= cred,
8447 	};
8448 	int status;
8449 
8450 	res.fh = nfs_alloc_fhandle();
8451 	if (res.fh == NULL)
8452 		return -ENOMEM;
8453 
8454 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8455 	status = nfs4_call_sync_sequence(clnt, server, &msg,
8456 						&args.seq_args, &res.seq_res);
8457 	nfs_free_fhandle(res.fh);
8458 	if (status == NFS4_OK &&
8459 	    res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8460 		status = -NFS4ERR_LEASE_MOVED;
8461 	return status;
8462 }
8463 
8464 #endif	/* CONFIG_NFS_V4_1 */
8465 
8466 /**
8467  * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8468  * @inode: inode on FSID to check
8469  * @cred: credential to use for this operation
8470  *
8471  * Server indicates whether the FSID is present, moved, or not
8472  * recognized.  This operation is necessary to clear a LEASE_MOVED
8473  * condition for this client ID.
8474  *
8475  * Returns NFS4_OK if the FSID is present on this server,
8476  * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8477  *  NFS4ERR code if some error occurred on the server, or a
8478  *  negative errno if a local failure occurred.
8479  */
nfs4_proc_fsid_present(struct inode * inode,const struct cred * cred)8480 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
8481 {
8482 	struct nfs_server *server = NFS_SERVER(inode);
8483 	struct nfs_client *clp = server->nfs_client;
8484 	const struct nfs4_mig_recovery_ops *ops =
8485 					clp->cl_mvops->mig_recovery_ops;
8486 	struct nfs4_exception exception = {
8487 		.interruptible = true,
8488 	};
8489 	int status;
8490 
8491 	dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8492 		(unsigned long long)server->fsid.major,
8493 		(unsigned long long)server->fsid.minor,
8494 		clp->cl_hostname);
8495 	nfs_display_fhandle(NFS_FH(inode), __func__);
8496 
8497 	do {
8498 		status = ops->fsid_present(inode, cred);
8499 		if (status != -NFS4ERR_DELAY)
8500 			break;
8501 		nfs4_handle_exception(server, status, &exception);
8502 	} while (exception.retry);
8503 	return status;
8504 }
8505 
8506 /*
8507  * If 'use_integrity' is true and the state managment nfs_client
8508  * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8509  * and the machine credential as per RFC3530bis and RFC5661 Security
8510  * Considerations sections. Otherwise, just use the user cred with the
8511  * filesystem's rpc_client.
8512  */
_nfs4_proc_secinfo(struct inode * dir,const struct qstr * name,struct nfs4_secinfo_flavors * flavors,bool use_integrity)8513 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8514 {
8515 	int status;
8516 	struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8517 	struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
8518 	struct nfs4_secinfo_arg args = {
8519 		.dir_fh = NFS_FH(dir),
8520 		.name   = name,
8521 	};
8522 	struct nfs4_secinfo_res res = {
8523 		.flavors     = flavors,
8524 	};
8525 	struct rpc_message msg = {
8526 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8527 		.rpc_argp = &args,
8528 		.rpc_resp = &res,
8529 	};
8530 	struct nfs4_call_sync_data data = {
8531 		.seq_server = NFS_SERVER(dir),
8532 		.seq_args = &args.seq_args,
8533 		.seq_res = &res.seq_res,
8534 	};
8535 	struct rpc_task_setup task_setup = {
8536 		.rpc_client = clnt,
8537 		.rpc_message = &msg,
8538 		.callback_ops = clp->cl_mvops->call_sync_ops,
8539 		.callback_data = &data,
8540 		.flags = RPC_TASK_NO_ROUND_ROBIN,
8541 	};
8542 	const struct cred *cred = NULL;
8543 
8544 	if (use_integrity) {
8545 		clnt = clp->cl_rpcclient;
8546 		task_setup.rpc_client = clnt;
8547 
8548 		cred = nfs4_get_clid_cred(clp);
8549 		msg.rpc_cred = cred;
8550 	}
8551 
8552 	dprintk("NFS call  secinfo %s\n", name->name);
8553 
8554 	nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8555 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8556 	status = nfs4_call_sync_custom(&task_setup);
8557 
8558 	dprintk("NFS reply  secinfo: %d\n", status);
8559 
8560 	put_cred(cred);
8561 	return status;
8562 }
8563 
nfs4_proc_secinfo(struct inode * dir,const struct qstr * name,struct nfs4_secinfo_flavors * flavors)8564 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8565 		      struct nfs4_secinfo_flavors *flavors)
8566 {
8567 	struct nfs4_exception exception = {
8568 		.interruptible = true,
8569 	};
8570 	int err;
8571 	do {
8572 		err = -NFS4ERR_WRONGSEC;
8573 
8574 		/* try to use integrity protection with machine cred */
8575 		if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8576 			err = _nfs4_proc_secinfo(dir, name, flavors, true);
8577 
8578 		/*
8579 		 * if unable to use integrity protection, or SECINFO with
8580 		 * integrity protection returns NFS4ERR_WRONGSEC (which is
8581 		 * disallowed by spec, but exists in deployed servers) use
8582 		 * the current filesystem's rpc_client and the user cred.
8583 		 */
8584 		if (err == -NFS4ERR_WRONGSEC)
8585 			err = _nfs4_proc_secinfo(dir, name, flavors, false);
8586 
8587 		trace_nfs4_secinfo(dir, name, err);
8588 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
8589 				&exception);
8590 	} while (exception.retry);
8591 	return err;
8592 }
8593 
8594 #ifdef CONFIG_NFS_V4_1
8595 /*
8596  * Check the exchange flags returned by the server for invalid flags, having
8597  * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8598  * DS flags set.
8599  */
nfs4_check_cl_exchange_flags(u32 flags,u32 version)8600 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
8601 {
8602 	if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8603 		goto out_inval;
8604 	else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
8605 		goto out_inval;
8606 	if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8607 	    (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8608 		goto out_inval;
8609 	if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8610 		goto out_inval;
8611 	return NFS_OK;
8612 out_inval:
8613 	return -NFS4ERR_INVAL;
8614 }
8615 
8616 static bool
nfs41_same_server_scope(struct nfs41_server_scope * a,struct nfs41_server_scope * b)8617 nfs41_same_server_scope(struct nfs41_server_scope *a,
8618 			struct nfs41_server_scope *b)
8619 {
8620 	if (a->server_scope_sz != b->server_scope_sz)
8621 		return false;
8622 	return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8623 }
8624 
8625 static void
nfs4_bind_one_conn_to_session_done(struct rpc_task * task,void * calldata)8626 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8627 {
8628 	struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8629 	struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8630 	struct nfs_client *clp = args->client;
8631 
8632 	switch (task->tk_status) {
8633 	case -NFS4ERR_BADSESSION:
8634 	case -NFS4ERR_DEADSESSION:
8635 		nfs4_schedule_session_recovery(clp->cl_session,
8636 				task->tk_status);
8637 		return;
8638 	}
8639 	if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8640 			res->dir != NFS4_CDFS4_BOTH) {
8641 		rpc_task_close_connection(task);
8642 		if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8643 			rpc_restart_call(task);
8644 	}
8645 }
8646 
8647 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8648 	.rpc_call_done =  nfs4_bind_one_conn_to_session_done,
8649 };
8650 
8651 /*
8652  * nfs4_proc_bind_one_conn_to_session()
8653  *
8654  * The 4.1 client currently uses the same TCP connection for the
8655  * fore and backchannel.
8656  */
8657 static
nfs4_proc_bind_one_conn_to_session(struct rpc_clnt * clnt,struct rpc_xprt * xprt,struct nfs_client * clp,const struct cred * cred)8658 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8659 		struct rpc_xprt *xprt,
8660 		struct nfs_client *clp,
8661 		const struct cred *cred)
8662 {
8663 	int status;
8664 	struct nfs41_bind_conn_to_session_args args = {
8665 		.client = clp,
8666 		.dir = NFS4_CDFC4_FORE_OR_BOTH,
8667 		.retries = 0,
8668 	};
8669 	struct nfs41_bind_conn_to_session_res res;
8670 	struct rpc_message msg = {
8671 		.rpc_proc =
8672 			&nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8673 		.rpc_argp = &args,
8674 		.rpc_resp = &res,
8675 		.rpc_cred = cred,
8676 	};
8677 	struct rpc_task_setup task_setup_data = {
8678 		.rpc_client = clnt,
8679 		.rpc_xprt = xprt,
8680 		.callback_ops = &nfs4_bind_one_conn_to_session_ops,
8681 		.rpc_message = &msg,
8682 		.flags = RPC_TASK_TIMEOUT,
8683 	};
8684 	struct rpc_task *task;
8685 
8686 	nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8687 	if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8688 		args.dir = NFS4_CDFC4_FORE;
8689 
8690 	/* Do not set the backchannel flag unless this is clnt->cl_xprt */
8691 	if (xprt != rcu_access_pointer(clnt->cl_xprt))
8692 		args.dir = NFS4_CDFC4_FORE;
8693 
8694 	task = rpc_run_task(&task_setup_data);
8695 	if (!IS_ERR(task)) {
8696 		status = task->tk_status;
8697 		rpc_put_task(task);
8698 	} else
8699 		status = PTR_ERR(task);
8700 	trace_nfs4_bind_conn_to_session(clp, status);
8701 	if (status == 0) {
8702 		if (memcmp(res.sessionid.data,
8703 		    clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8704 			dprintk("NFS: %s: Session ID mismatch\n", __func__);
8705 			return -EIO;
8706 		}
8707 		if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8708 			dprintk("NFS: %s: Unexpected direction from server\n",
8709 				__func__);
8710 			return -EIO;
8711 		}
8712 		if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8713 			dprintk("NFS: %s: Server returned RDMA mode = true\n",
8714 				__func__);
8715 			return -EIO;
8716 		}
8717 	}
8718 
8719 	return status;
8720 }
8721 
8722 struct rpc_bind_conn_calldata {
8723 	struct nfs_client *clp;
8724 	const struct cred *cred;
8725 };
8726 
8727 static int
nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt * clnt,struct rpc_xprt * xprt,void * calldata)8728 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8729 		struct rpc_xprt *xprt,
8730 		void *calldata)
8731 {
8732 	struct rpc_bind_conn_calldata *p = calldata;
8733 
8734 	return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8735 }
8736 
nfs4_proc_bind_conn_to_session(struct nfs_client * clp,const struct cred * cred)8737 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8738 {
8739 	struct rpc_bind_conn_calldata data = {
8740 		.clp = clp,
8741 		.cred = cred,
8742 	};
8743 	return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8744 			nfs4_proc_bind_conn_to_session_callback, &data);
8745 }
8746 
8747 /*
8748  * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8749  * and operations we'd like to see to enable certain features in the allow map
8750  */
8751 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8752 	.how = SP4_MACH_CRED,
8753 	.enforce.u.words = {
8754 		[1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8755 		      1 << (OP_EXCHANGE_ID - 32) |
8756 		      1 << (OP_CREATE_SESSION - 32) |
8757 		      1 << (OP_DESTROY_SESSION - 32) |
8758 		      1 << (OP_DESTROY_CLIENTID - 32)
8759 	},
8760 	.allow.u.words = {
8761 		[0] = 1 << (OP_CLOSE) |
8762 		      1 << (OP_OPEN_DOWNGRADE) |
8763 		      1 << (OP_LOCKU) |
8764 		      1 << (OP_DELEGRETURN) |
8765 		      1 << (OP_COMMIT),
8766 		[1] = 1 << (OP_SECINFO - 32) |
8767 		      1 << (OP_SECINFO_NO_NAME - 32) |
8768 		      1 << (OP_LAYOUTRETURN - 32) |
8769 		      1 << (OP_TEST_STATEID - 32) |
8770 		      1 << (OP_FREE_STATEID - 32) |
8771 		      1 << (OP_WRITE - 32)
8772 	}
8773 };
8774 
8775 /*
8776  * Select the state protection mode for client `clp' given the server results
8777  * from exchange_id in `sp'.
8778  *
8779  * Returns 0 on success, negative errno otherwise.
8780  */
nfs4_sp4_select_mode(struct nfs_client * clp,struct nfs41_state_protection * sp)8781 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8782 				 struct nfs41_state_protection *sp)
8783 {
8784 	static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8785 		[1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8786 		      1 << (OP_EXCHANGE_ID - 32) |
8787 		      1 << (OP_CREATE_SESSION - 32) |
8788 		      1 << (OP_DESTROY_SESSION - 32) |
8789 		      1 << (OP_DESTROY_CLIENTID - 32)
8790 	};
8791 	unsigned long flags = 0;
8792 	unsigned int i;
8793 	int ret = 0;
8794 
8795 	if (sp->how == SP4_MACH_CRED) {
8796 		/* Print state protect result */
8797 		dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8798 		for (i = 0; i <= LAST_NFS4_OP; i++) {
8799 			if (test_bit(i, sp->enforce.u.longs))
8800 				dfprintk(MOUNT, "  enforce op %d\n", i);
8801 			if (test_bit(i, sp->allow.u.longs))
8802 				dfprintk(MOUNT, "  allow op %d\n", i);
8803 		}
8804 
8805 		/* make sure nothing is on enforce list that isn't supported */
8806 		for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8807 			if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8808 				dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8809 				ret = -EINVAL;
8810 				goto out;
8811 			}
8812 		}
8813 
8814 		/*
8815 		 * Minimal mode - state operations are allowed to use machine
8816 		 * credential.  Note this already happens by default, so the
8817 		 * client doesn't have to do anything more than the negotiation.
8818 		 *
8819 		 * NOTE: we don't care if EXCHANGE_ID is in the list -
8820 		 *       we're already using the machine cred for exchange_id
8821 		 *       and will never use a different cred.
8822 		 */
8823 		if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8824 		    test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8825 		    test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8826 		    test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8827 			dfprintk(MOUNT, "sp4_mach_cred:\n");
8828 			dfprintk(MOUNT, "  minimal mode enabled\n");
8829 			__set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8830 		} else {
8831 			dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8832 			ret = -EINVAL;
8833 			goto out;
8834 		}
8835 
8836 		if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8837 		    test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8838 		    test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8839 		    test_bit(OP_LOCKU, sp->allow.u.longs)) {
8840 			dfprintk(MOUNT, "  cleanup mode enabled\n");
8841 			__set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8842 		}
8843 
8844 		if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8845 			dfprintk(MOUNT, "  pnfs cleanup mode enabled\n");
8846 			__set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8847 		}
8848 
8849 		if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8850 		    test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8851 			dfprintk(MOUNT, "  secinfo mode enabled\n");
8852 			__set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8853 		}
8854 
8855 		if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8856 		    test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8857 			dfprintk(MOUNT, "  stateid mode enabled\n");
8858 			__set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8859 		}
8860 
8861 		if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8862 			dfprintk(MOUNT, "  write mode enabled\n");
8863 			__set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8864 		}
8865 
8866 		if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8867 			dfprintk(MOUNT, "  commit mode enabled\n");
8868 			__set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8869 		}
8870 	}
8871 out:
8872 	clp->cl_sp4_flags = flags;
8873 	return ret;
8874 }
8875 
8876 struct nfs41_exchange_id_data {
8877 	struct nfs41_exchange_id_res res;
8878 	struct nfs41_exchange_id_args args;
8879 };
8880 
nfs4_exchange_id_release(void * data)8881 static void nfs4_exchange_id_release(void *data)
8882 {
8883 	struct nfs41_exchange_id_data *cdata =
8884 					(struct nfs41_exchange_id_data *)data;
8885 
8886 	nfs_put_client(cdata->args.client);
8887 	kfree(cdata->res.impl_id);
8888 	kfree(cdata->res.server_scope);
8889 	kfree(cdata->res.server_owner);
8890 	kfree(cdata);
8891 }
8892 
8893 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8894 	.rpc_release = nfs4_exchange_id_release,
8895 };
8896 
8897 /*
8898  * _nfs4_proc_exchange_id()
8899  *
8900  * Wrapper for EXCHANGE_ID operation.
8901  */
8902 static struct rpc_task *
nfs4_run_exchange_id(struct nfs_client * clp,const struct cred * cred,u32 sp4_how,struct rpc_xprt * xprt)8903 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8904 			u32 sp4_how, struct rpc_xprt *xprt)
8905 {
8906 	struct rpc_message msg = {
8907 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8908 		.rpc_cred = cred,
8909 	};
8910 	struct rpc_task_setup task_setup_data = {
8911 		.rpc_client = clp->cl_rpcclient,
8912 		.callback_ops = &nfs4_exchange_id_call_ops,
8913 		.rpc_message = &msg,
8914 		.flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8915 	};
8916 	struct nfs41_exchange_id_data *calldata;
8917 	int status;
8918 
8919 	if (!refcount_inc_not_zero(&clp->cl_count))
8920 		return ERR_PTR(-EIO);
8921 
8922 	status = -ENOMEM;
8923 	calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8924 	if (!calldata)
8925 		goto out;
8926 
8927 	nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8928 
8929 	status = nfs4_init_uniform_client_string(clp);
8930 	if (status)
8931 		goto out_calldata;
8932 
8933 	calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8934 						GFP_NOFS);
8935 	status = -ENOMEM;
8936 	if (unlikely(calldata->res.server_owner == NULL))
8937 		goto out_calldata;
8938 
8939 	calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8940 					GFP_NOFS);
8941 	if (unlikely(calldata->res.server_scope == NULL))
8942 		goto out_server_owner;
8943 
8944 	calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8945 	if (unlikely(calldata->res.impl_id == NULL))
8946 		goto out_server_scope;
8947 
8948 	switch (sp4_how) {
8949 	case SP4_NONE:
8950 		calldata->args.state_protect.how = SP4_NONE;
8951 		break;
8952 
8953 	case SP4_MACH_CRED:
8954 		calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8955 		break;
8956 
8957 	default:
8958 		/* unsupported! */
8959 		WARN_ON_ONCE(1);
8960 		status = -EINVAL;
8961 		goto out_impl_id;
8962 	}
8963 	if (xprt) {
8964 		task_setup_data.rpc_xprt = xprt;
8965 		task_setup_data.flags |= RPC_TASK_SOFTCONN;
8966 		memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8967 				sizeof(calldata->args.verifier.data));
8968 	}
8969 	calldata->args.client = clp;
8970 	calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8971 	EXCHGID4_FLAG_BIND_PRINC_STATEID;
8972 #ifdef CONFIG_NFS_V4_1_MIGRATION
8973 	calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8974 #endif
8975 	if (test_bit(NFS_CS_PNFS, &clp->cl_flags))
8976 		calldata->args.flags |= EXCHGID4_FLAG_USE_PNFS_DS;
8977 	msg.rpc_argp = &calldata->args;
8978 	msg.rpc_resp = &calldata->res;
8979 	task_setup_data.callback_data = calldata;
8980 
8981 	return rpc_run_task(&task_setup_data);
8982 
8983 out_impl_id:
8984 	kfree(calldata->res.impl_id);
8985 out_server_scope:
8986 	kfree(calldata->res.server_scope);
8987 out_server_owner:
8988 	kfree(calldata->res.server_owner);
8989 out_calldata:
8990 	kfree(calldata);
8991 out:
8992 	nfs_put_client(clp);
8993 	return ERR_PTR(status);
8994 }
8995 
8996 /*
8997  * _nfs4_proc_exchange_id()
8998  *
8999  * Wrapper for EXCHANGE_ID operation.
9000  */
_nfs4_proc_exchange_id(struct nfs_client * clp,const struct cred * cred,u32 sp4_how)9001 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
9002 			u32 sp4_how)
9003 {
9004 	struct rpc_task *task;
9005 	struct nfs41_exchange_id_args *argp;
9006 	struct nfs41_exchange_id_res *resp;
9007 	unsigned long now = jiffies;
9008 	int status;
9009 
9010 	task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
9011 	if (IS_ERR(task))
9012 		return PTR_ERR(task);
9013 
9014 	argp = task->tk_msg.rpc_argp;
9015 	resp = task->tk_msg.rpc_resp;
9016 	status = task->tk_status;
9017 	if (status  != 0)
9018 		goto out;
9019 
9020 	status = nfs4_check_cl_exchange_flags(resp->flags,
9021 			clp->cl_mvops->minor_version);
9022 	if (status  != 0)
9023 		goto out;
9024 
9025 	status = nfs4_sp4_select_mode(clp, &resp->state_protect);
9026 	if (status != 0)
9027 		goto out;
9028 
9029 	do_renew_lease(clp, now);
9030 
9031 	clp->cl_clientid = resp->clientid;
9032 	clp->cl_exchange_flags = resp->flags;
9033 	clp->cl_seqid = resp->seqid;
9034 	/* Client ID is not confirmed */
9035 	if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
9036 		clear_bit(NFS4_SESSION_ESTABLISHED,
9037 			  &clp->cl_session->session_state);
9038 
9039 	if (clp->cl_serverscope != NULL &&
9040 	    !nfs41_same_server_scope(clp->cl_serverscope,
9041 				resp->server_scope)) {
9042 		dprintk("%s: server_scope mismatch detected\n",
9043 			__func__);
9044 		set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
9045 	}
9046 
9047 	swap(clp->cl_serverowner, resp->server_owner);
9048 	swap(clp->cl_serverscope, resp->server_scope);
9049 	swap(clp->cl_implid, resp->impl_id);
9050 
9051 	/* Save the EXCHANGE_ID verifier session trunk tests */
9052 	memcpy(clp->cl_confirm.data, argp->verifier.data,
9053 	       sizeof(clp->cl_confirm.data));
9054 out:
9055 	trace_nfs4_exchange_id(clp, status);
9056 	rpc_put_task(task);
9057 	return status;
9058 }
9059 
9060 /*
9061  * nfs4_proc_exchange_id()
9062  *
9063  * Returns zero, a negative errno, or a negative NFS4ERR status code.
9064  *
9065  * Since the clientid has expired, all compounds using sessions
9066  * associated with the stale clientid will be returning
9067  * NFS4ERR_BADSESSION in the sequence operation, and will therefore
9068  * be in some phase of session reset.
9069  *
9070  * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
9071  */
nfs4_proc_exchange_id(struct nfs_client * clp,const struct cred * cred)9072 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
9073 {
9074 	rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
9075 	int status;
9076 
9077 	/* try SP4_MACH_CRED if krb5i/p	*/
9078 	if (authflavor == RPC_AUTH_GSS_KRB5I ||
9079 	    authflavor == RPC_AUTH_GSS_KRB5P) {
9080 		status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
9081 		if (!status)
9082 			return 0;
9083 	}
9084 
9085 	/* try SP4_NONE */
9086 	return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
9087 }
9088 
9089 /**
9090  * nfs4_test_session_trunk
9091  *
9092  * This is an add_xprt_test() test function called from
9093  * rpc_clnt_setup_test_and_add_xprt.
9094  *
9095  * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
9096  * and is dereferrenced in nfs4_exchange_id_release
9097  *
9098  * Upon success, add the new transport to the rpc_clnt
9099  *
9100  * @clnt: struct rpc_clnt to get new transport
9101  * @xprt: the rpc_xprt to test
9102  * @data: call data for _nfs4_proc_exchange_id.
9103  */
nfs4_test_session_trunk(struct rpc_clnt * clnt,struct rpc_xprt * xprt,void * data)9104 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
9105 			    void *data)
9106 {
9107 	struct nfs4_add_xprt_data *adata = data;
9108 	struct rpc_task *task;
9109 	int status;
9110 
9111 	u32 sp4_how;
9112 
9113 	dprintk("--> %s try %s\n", __func__,
9114 		xprt->address_strings[RPC_DISPLAY_ADDR]);
9115 
9116 	sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
9117 
9118 try_again:
9119 	/* Test connection for session trunking. Async exchange_id call */
9120 	task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
9121 	if (IS_ERR(task))
9122 		return;
9123 
9124 	status = task->tk_status;
9125 	if (status == 0) {
9126 		status = nfs4_detect_session_trunking(adata->clp,
9127 				task->tk_msg.rpc_resp, xprt);
9128 		trace_nfs4_trunked_exchange_id(adata->clp,
9129 			xprt->address_strings[RPC_DISPLAY_ADDR], status);
9130 	}
9131 	if (status == 0)
9132 		rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
9133 	else if (status != -NFS4ERR_DELAY && rpc_clnt_xprt_switch_has_addr(clnt,
9134 				(struct sockaddr *)&xprt->addr))
9135 		rpc_clnt_xprt_switch_remove_xprt(clnt, xprt);
9136 
9137 	rpc_put_task(task);
9138 	if (status == -NFS4ERR_DELAY) {
9139 		ssleep(1);
9140 		goto try_again;
9141 	}
9142 }
9143 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
9144 
_nfs4_proc_destroy_clientid(struct nfs_client * clp,const struct cred * cred)9145 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
9146 		const struct cred *cred)
9147 {
9148 	struct rpc_message msg = {
9149 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
9150 		.rpc_argp = clp,
9151 		.rpc_cred = cred,
9152 	};
9153 	int status;
9154 
9155 	status = rpc_call_sync(clp->cl_rpcclient, &msg,
9156 			       RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9157 	trace_nfs4_destroy_clientid(clp, status);
9158 	if (status)
9159 		dprintk("NFS: Got error %d from the server %s on "
9160 			"DESTROY_CLIENTID.", status, clp->cl_hostname);
9161 	return status;
9162 }
9163 
nfs4_proc_destroy_clientid(struct nfs_client * clp,const struct cred * cred)9164 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
9165 		const struct cred *cred)
9166 {
9167 	unsigned int loop;
9168 	int ret;
9169 
9170 	for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
9171 		ret = _nfs4_proc_destroy_clientid(clp, cred);
9172 		switch (ret) {
9173 		case -NFS4ERR_DELAY:
9174 		case -NFS4ERR_CLIENTID_BUSY:
9175 			ssleep(1);
9176 			break;
9177 		default:
9178 			return ret;
9179 		}
9180 	}
9181 	return 0;
9182 }
9183 
nfs4_destroy_clientid(struct nfs_client * clp)9184 int nfs4_destroy_clientid(struct nfs_client *clp)
9185 {
9186 	const struct cred *cred;
9187 	int ret = 0;
9188 
9189 	if (clp->cl_mvops->minor_version < 1)
9190 		goto out;
9191 	if (clp->cl_exchange_flags == 0)
9192 		goto out;
9193 	if (clp->cl_preserve_clid)
9194 		goto out;
9195 	cred = nfs4_get_clid_cred(clp);
9196 	ret = nfs4_proc_destroy_clientid(clp, cred);
9197 	put_cred(cred);
9198 	switch (ret) {
9199 	case 0:
9200 	case -NFS4ERR_STALE_CLIENTID:
9201 		clp->cl_exchange_flags = 0;
9202 	}
9203 out:
9204 	return ret;
9205 }
9206 
9207 #endif /* CONFIG_NFS_V4_1 */
9208 
9209 struct nfs4_get_lease_time_data {
9210 	struct nfs4_get_lease_time_args *args;
9211 	struct nfs4_get_lease_time_res *res;
9212 	struct nfs_client *clp;
9213 };
9214 
nfs4_get_lease_time_prepare(struct rpc_task * task,void * calldata)9215 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
9216 					void *calldata)
9217 {
9218 	struct nfs4_get_lease_time_data *data =
9219 			(struct nfs4_get_lease_time_data *)calldata;
9220 
9221 	/* just setup sequence, do not trigger session recovery
9222 	   since we're invoked within one */
9223 	nfs4_setup_sequence(data->clp,
9224 			&data->args->la_seq_args,
9225 			&data->res->lr_seq_res,
9226 			task);
9227 }
9228 
9229 /*
9230  * Called from nfs4_state_manager thread for session setup, so don't recover
9231  * from sequence operation or clientid errors.
9232  */
nfs4_get_lease_time_done(struct rpc_task * task,void * calldata)9233 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
9234 {
9235 	struct nfs4_get_lease_time_data *data =
9236 			(struct nfs4_get_lease_time_data *)calldata;
9237 
9238 	if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
9239 		return;
9240 	switch (task->tk_status) {
9241 	case -NFS4ERR_DELAY:
9242 	case -NFS4ERR_GRACE:
9243 		rpc_delay(task, NFS4_POLL_RETRY_MIN);
9244 		task->tk_status = 0;
9245 		fallthrough;
9246 	case -NFS4ERR_RETRY_UNCACHED_REP:
9247 		rpc_restart_call_prepare(task);
9248 		return;
9249 	}
9250 }
9251 
9252 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
9253 	.rpc_call_prepare = nfs4_get_lease_time_prepare,
9254 	.rpc_call_done = nfs4_get_lease_time_done,
9255 };
9256 
nfs4_proc_get_lease_time(struct nfs_client * clp,struct nfs_fsinfo * fsinfo)9257 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
9258 {
9259 	struct nfs4_get_lease_time_args args;
9260 	struct nfs4_get_lease_time_res res = {
9261 		.lr_fsinfo = fsinfo,
9262 	};
9263 	struct nfs4_get_lease_time_data data = {
9264 		.args = &args,
9265 		.res = &res,
9266 		.clp = clp,
9267 	};
9268 	struct rpc_message msg = {
9269 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
9270 		.rpc_argp = &args,
9271 		.rpc_resp = &res,
9272 	};
9273 	struct rpc_task_setup task_setup = {
9274 		.rpc_client = clp->cl_rpcclient,
9275 		.rpc_message = &msg,
9276 		.callback_ops = &nfs4_get_lease_time_ops,
9277 		.callback_data = &data,
9278 		.flags = RPC_TASK_TIMEOUT,
9279 	};
9280 
9281 	nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
9282 	return nfs4_call_sync_custom(&task_setup);
9283 }
9284 
9285 #ifdef CONFIG_NFS_V4_1
9286 
9287 /*
9288  * Initialize the values to be used by the client in CREATE_SESSION
9289  * If nfs4_init_session set the fore channel request and response sizes,
9290  * use them.
9291  *
9292  * Set the back channel max_resp_sz_cached to zero to force the client to
9293  * always set csa_cachethis to FALSE because the current implementation
9294  * of the back channel DRC only supports caching the CB_SEQUENCE operation.
9295  */
nfs4_init_channel_attrs(struct nfs41_create_session_args * args,struct rpc_clnt * clnt)9296 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
9297 				    struct rpc_clnt *clnt)
9298 {
9299 	unsigned int max_rqst_sz, max_resp_sz;
9300 	unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
9301 	unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
9302 
9303 	max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
9304 	max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
9305 
9306 	/* Fore channel attributes */
9307 	args->fc_attrs.max_rqst_sz = max_rqst_sz;
9308 	args->fc_attrs.max_resp_sz = max_resp_sz;
9309 	args->fc_attrs.max_ops = NFS4_MAX_OPS;
9310 	args->fc_attrs.max_reqs = max_session_slots;
9311 
9312 	dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
9313 		"max_ops=%u max_reqs=%u\n",
9314 		__func__,
9315 		args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
9316 		args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
9317 
9318 	/* Back channel attributes */
9319 	args->bc_attrs.max_rqst_sz = max_bc_payload;
9320 	args->bc_attrs.max_resp_sz = max_bc_payload;
9321 	args->bc_attrs.max_resp_sz_cached = 0;
9322 	args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
9323 	args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
9324 	if (args->bc_attrs.max_reqs > max_bc_slots)
9325 		args->bc_attrs.max_reqs = max_bc_slots;
9326 
9327 	dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
9328 		"max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
9329 		__func__,
9330 		args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
9331 		args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
9332 		args->bc_attrs.max_reqs);
9333 }
9334 
nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args * args,struct nfs41_create_session_res * res)9335 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
9336 		struct nfs41_create_session_res *res)
9337 {
9338 	struct nfs4_channel_attrs *sent = &args->fc_attrs;
9339 	struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
9340 
9341 	if (rcvd->max_resp_sz > sent->max_resp_sz)
9342 		return -EINVAL;
9343 	/*
9344 	 * Our requested max_ops is the minimum we need; we're not
9345 	 * prepared to break up compounds into smaller pieces than that.
9346 	 * So, no point even trying to continue if the server won't
9347 	 * cooperate:
9348 	 */
9349 	if (rcvd->max_ops < sent->max_ops)
9350 		return -EINVAL;
9351 	if (rcvd->max_reqs == 0)
9352 		return -EINVAL;
9353 	if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
9354 		rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
9355 	return 0;
9356 }
9357 
nfs4_verify_back_channel_attrs(struct nfs41_create_session_args * args,struct nfs41_create_session_res * res)9358 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
9359 		struct nfs41_create_session_res *res)
9360 {
9361 	struct nfs4_channel_attrs *sent = &args->bc_attrs;
9362 	struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
9363 
9364 	if (!(res->flags & SESSION4_BACK_CHAN))
9365 		goto out;
9366 	if (rcvd->max_rqst_sz > sent->max_rqst_sz)
9367 		return -EINVAL;
9368 	if (rcvd->max_resp_sz < sent->max_resp_sz)
9369 		return -EINVAL;
9370 	if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
9371 		return -EINVAL;
9372 	if (rcvd->max_ops > sent->max_ops)
9373 		return -EINVAL;
9374 	if (rcvd->max_reqs > sent->max_reqs)
9375 		return -EINVAL;
9376 out:
9377 	return 0;
9378 }
9379 
nfs4_verify_channel_attrs(struct nfs41_create_session_args * args,struct nfs41_create_session_res * res)9380 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
9381 				     struct nfs41_create_session_res *res)
9382 {
9383 	int ret;
9384 
9385 	ret = nfs4_verify_fore_channel_attrs(args, res);
9386 	if (ret)
9387 		return ret;
9388 	return nfs4_verify_back_channel_attrs(args, res);
9389 }
9390 
nfs4_update_session(struct nfs4_session * session,struct nfs41_create_session_res * res)9391 static void nfs4_update_session(struct nfs4_session *session,
9392 		struct nfs41_create_session_res *res)
9393 {
9394 	nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
9395 	/* Mark client id and session as being confirmed */
9396 	session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
9397 	set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
9398 	session->flags = res->flags;
9399 	memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
9400 	if (res->flags & SESSION4_BACK_CHAN)
9401 		memcpy(&session->bc_attrs, &res->bc_attrs,
9402 				sizeof(session->bc_attrs));
9403 }
9404 
_nfs4_proc_create_session(struct nfs_client * clp,const struct cred * cred)9405 static int _nfs4_proc_create_session(struct nfs_client *clp,
9406 		const struct cred *cred)
9407 {
9408 	struct nfs4_session *session = clp->cl_session;
9409 	struct nfs41_create_session_args args = {
9410 		.client = clp,
9411 		.clientid = clp->cl_clientid,
9412 		.seqid = clp->cl_seqid,
9413 		.cb_program = NFS4_CALLBACK,
9414 	};
9415 	struct nfs41_create_session_res res;
9416 
9417 	struct rpc_message msg = {
9418 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
9419 		.rpc_argp = &args,
9420 		.rpc_resp = &res,
9421 		.rpc_cred = cred,
9422 	};
9423 	int status;
9424 
9425 	nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
9426 	args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
9427 
9428 	status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9429 			       RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9430 	trace_nfs4_create_session(clp, status);
9431 
9432 	switch (status) {
9433 	case -NFS4ERR_STALE_CLIENTID:
9434 	case -NFS4ERR_DELAY:
9435 	case -ETIMEDOUT:
9436 	case -EACCES:
9437 	case -EAGAIN:
9438 		goto out;
9439 	}
9440 
9441 	clp->cl_seqid++;
9442 	if (!status) {
9443 		/* Verify the session's negotiated channel_attrs values */
9444 		status = nfs4_verify_channel_attrs(&args, &res);
9445 		/* Increment the clientid slot sequence id */
9446 		if (status)
9447 			goto out;
9448 		nfs4_update_session(session, &res);
9449 	}
9450 out:
9451 	return status;
9452 }
9453 
9454 /*
9455  * Issues a CREATE_SESSION operation to the server.
9456  * It is the responsibility of the caller to verify the session is
9457  * expired before calling this routine.
9458  */
nfs4_proc_create_session(struct nfs_client * clp,const struct cred * cred)9459 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
9460 {
9461 	int status;
9462 	unsigned *ptr;
9463 	struct nfs4_session *session = clp->cl_session;
9464 	struct nfs4_add_xprt_data xprtdata = {
9465 		.clp = clp,
9466 	};
9467 	struct rpc_add_xprt_test rpcdata = {
9468 		.add_xprt_test = clp->cl_mvops->session_trunk,
9469 		.data = &xprtdata,
9470 	};
9471 
9472 	dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9473 
9474 	status = _nfs4_proc_create_session(clp, cred);
9475 	if (status)
9476 		goto out;
9477 
9478 	/* Init or reset the session slot tables */
9479 	status = nfs4_setup_session_slot_tables(session);
9480 	dprintk("slot table setup returned %d\n", status);
9481 	if (status)
9482 		goto out;
9483 
9484 	ptr = (unsigned *)&session->sess_id.data[0];
9485 	dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9486 		clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
9487 	rpc_clnt_probe_trunked_xprts(clp->cl_rpcclient, &rpcdata);
9488 out:
9489 	return status;
9490 }
9491 
9492 /*
9493  * Issue the over-the-wire RPC DESTROY_SESSION.
9494  * The caller must serialize access to this routine.
9495  */
nfs4_proc_destroy_session(struct nfs4_session * session,const struct cred * cred)9496 int nfs4_proc_destroy_session(struct nfs4_session *session,
9497 		const struct cred *cred)
9498 {
9499 	struct rpc_message msg = {
9500 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9501 		.rpc_argp = session,
9502 		.rpc_cred = cred,
9503 	};
9504 	int status = 0;
9505 
9506 	/* session is still being setup */
9507 	if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9508 		return 0;
9509 
9510 	status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9511 			       RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9512 	trace_nfs4_destroy_session(session->clp, status);
9513 
9514 	if (status)
9515 		dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
9516 			"Session has been destroyed regardless...\n", status);
9517 	rpc_clnt_manage_trunked_xprts(session->clp->cl_rpcclient);
9518 	return status;
9519 }
9520 
9521 /*
9522  * Renew the cl_session lease.
9523  */
9524 struct nfs4_sequence_data {
9525 	struct nfs_client *clp;
9526 	struct nfs4_sequence_args args;
9527 	struct nfs4_sequence_res res;
9528 };
9529 
nfs41_sequence_release(void * data)9530 static void nfs41_sequence_release(void *data)
9531 {
9532 	struct nfs4_sequence_data *calldata = data;
9533 	struct nfs_client *clp = calldata->clp;
9534 
9535 	if (refcount_read(&clp->cl_count) > 1)
9536 		nfs4_schedule_state_renewal(clp);
9537 	nfs_put_client(clp);
9538 	kfree(calldata);
9539 }
9540 
nfs41_sequence_handle_errors(struct rpc_task * task,struct nfs_client * clp)9541 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9542 {
9543 	switch(task->tk_status) {
9544 	case -NFS4ERR_DELAY:
9545 		rpc_delay(task, NFS4_POLL_RETRY_MAX);
9546 		return -EAGAIN;
9547 	default:
9548 		nfs4_schedule_lease_recovery(clp);
9549 	}
9550 	return 0;
9551 }
9552 
nfs41_sequence_call_done(struct rpc_task * task,void * data)9553 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
9554 {
9555 	struct nfs4_sequence_data *calldata = data;
9556 	struct nfs_client *clp = calldata->clp;
9557 
9558 	if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9559 		return;
9560 
9561 	trace_nfs4_sequence(clp, task->tk_status);
9562 	if (task->tk_status < 0 && !task->tk_client->cl_shutdown) {
9563 		dprintk("%s ERROR %d\n", __func__, task->tk_status);
9564 		if (refcount_read(&clp->cl_count) == 1)
9565 			return;
9566 
9567 		if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9568 			rpc_restart_call_prepare(task);
9569 			return;
9570 		}
9571 	}
9572 	dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
9573 }
9574 
nfs41_sequence_prepare(struct rpc_task * task,void * data)9575 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9576 {
9577 	struct nfs4_sequence_data *calldata = data;
9578 	struct nfs_client *clp = calldata->clp;
9579 	struct nfs4_sequence_args *args;
9580 	struct nfs4_sequence_res *res;
9581 
9582 	args = task->tk_msg.rpc_argp;
9583 	res = task->tk_msg.rpc_resp;
9584 
9585 	nfs4_setup_sequence(clp, args, res, task);
9586 }
9587 
9588 static const struct rpc_call_ops nfs41_sequence_ops = {
9589 	.rpc_call_done = nfs41_sequence_call_done,
9590 	.rpc_call_prepare = nfs41_sequence_prepare,
9591 	.rpc_release = nfs41_sequence_release,
9592 };
9593 
_nfs41_proc_sequence(struct nfs_client * clp,const struct cred * cred,struct nfs4_slot * slot,bool is_privileged)9594 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9595 		const struct cred *cred,
9596 		struct nfs4_slot *slot,
9597 		bool is_privileged)
9598 {
9599 	struct nfs4_sequence_data *calldata;
9600 	struct rpc_message msg = {
9601 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9602 		.rpc_cred = cred,
9603 	};
9604 	struct rpc_task_setup task_setup_data = {
9605 		.rpc_client = clp->cl_rpcclient,
9606 		.rpc_message = &msg,
9607 		.callback_ops = &nfs41_sequence_ops,
9608 		.flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
9609 	};
9610 	struct rpc_task *ret;
9611 
9612 	ret = ERR_PTR(-EIO);
9613 	if (!refcount_inc_not_zero(&clp->cl_count))
9614 		goto out_err;
9615 
9616 	ret = ERR_PTR(-ENOMEM);
9617 	calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
9618 	if (calldata == NULL)
9619 		goto out_put_clp;
9620 	nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9621 	nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9622 	msg.rpc_argp = &calldata->args;
9623 	msg.rpc_resp = &calldata->res;
9624 	calldata->clp = clp;
9625 	task_setup_data.callback_data = calldata;
9626 
9627 	ret = rpc_run_task(&task_setup_data);
9628 	if (IS_ERR(ret))
9629 		goto out_err;
9630 	return ret;
9631 out_put_clp:
9632 	nfs_put_client(clp);
9633 out_err:
9634 	nfs41_release_slot(slot);
9635 	return ret;
9636 }
9637 
nfs41_proc_async_sequence(struct nfs_client * clp,const struct cred * cred,unsigned renew_flags)9638 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9639 {
9640 	struct rpc_task *task;
9641 	int ret = 0;
9642 
9643 	if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9644 		return -EAGAIN;
9645 	task = _nfs41_proc_sequence(clp, cred, NULL, false);
9646 	if (IS_ERR(task))
9647 		ret = PTR_ERR(task);
9648 	else
9649 		rpc_put_task_async(task);
9650 	dprintk("<-- %s status=%d\n", __func__, ret);
9651 	return ret;
9652 }
9653 
nfs4_proc_sequence(struct nfs_client * clp,const struct cred * cred)9654 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9655 {
9656 	struct rpc_task *task;
9657 	int ret;
9658 
9659 	task = _nfs41_proc_sequence(clp, cred, NULL, true);
9660 	if (IS_ERR(task)) {
9661 		ret = PTR_ERR(task);
9662 		goto out;
9663 	}
9664 	ret = rpc_wait_for_completion_task(task);
9665 	if (!ret)
9666 		ret = task->tk_status;
9667 	rpc_put_task(task);
9668 out:
9669 	dprintk("<-- %s status=%d\n", __func__, ret);
9670 	return ret;
9671 }
9672 
9673 struct nfs4_reclaim_complete_data {
9674 	struct nfs_client *clp;
9675 	struct nfs41_reclaim_complete_args arg;
9676 	struct nfs41_reclaim_complete_res res;
9677 };
9678 
nfs4_reclaim_complete_prepare(struct rpc_task * task,void * data)9679 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9680 {
9681 	struct nfs4_reclaim_complete_data *calldata = data;
9682 
9683 	nfs4_setup_sequence(calldata->clp,
9684 			&calldata->arg.seq_args,
9685 			&calldata->res.seq_res,
9686 			task);
9687 }
9688 
nfs41_reclaim_complete_handle_errors(struct rpc_task * task,struct nfs_client * clp)9689 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9690 {
9691 	switch(task->tk_status) {
9692 	case 0:
9693 		wake_up_all(&clp->cl_lock_waitq);
9694 		fallthrough;
9695 	case -NFS4ERR_COMPLETE_ALREADY:
9696 	case -NFS4ERR_WRONG_CRED: /* What to do here? */
9697 		break;
9698 	case -NFS4ERR_DELAY:
9699 		rpc_delay(task, NFS4_POLL_RETRY_MAX);
9700 		fallthrough;
9701 	case -NFS4ERR_RETRY_UNCACHED_REP:
9702 	case -EACCES:
9703 		dprintk("%s: failed to reclaim complete error %d for server %s, retrying\n",
9704 			__func__, task->tk_status, clp->cl_hostname);
9705 		return -EAGAIN;
9706 	case -NFS4ERR_BADSESSION:
9707 	case -NFS4ERR_DEADSESSION:
9708 	case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9709 		break;
9710 	default:
9711 		nfs4_schedule_lease_recovery(clp);
9712 	}
9713 	return 0;
9714 }
9715 
nfs4_reclaim_complete_done(struct rpc_task * task,void * data)9716 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9717 {
9718 	struct nfs4_reclaim_complete_data *calldata = data;
9719 	struct nfs_client *clp = calldata->clp;
9720 	struct nfs4_sequence_res *res = &calldata->res.seq_res;
9721 
9722 	if (!nfs41_sequence_done(task, res))
9723 		return;
9724 
9725 	trace_nfs4_reclaim_complete(clp, task->tk_status);
9726 	if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9727 		rpc_restart_call_prepare(task);
9728 		return;
9729 	}
9730 }
9731 
nfs4_free_reclaim_complete_data(void * data)9732 static void nfs4_free_reclaim_complete_data(void *data)
9733 {
9734 	struct nfs4_reclaim_complete_data *calldata = data;
9735 
9736 	kfree(calldata);
9737 }
9738 
9739 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9740 	.rpc_call_prepare = nfs4_reclaim_complete_prepare,
9741 	.rpc_call_done = nfs4_reclaim_complete_done,
9742 	.rpc_release = nfs4_free_reclaim_complete_data,
9743 };
9744 
9745 /*
9746  * Issue a global reclaim complete.
9747  */
nfs41_proc_reclaim_complete(struct nfs_client * clp,const struct cred * cred)9748 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9749 		const struct cred *cred)
9750 {
9751 	struct nfs4_reclaim_complete_data *calldata;
9752 	struct rpc_message msg = {
9753 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9754 		.rpc_cred = cred,
9755 	};
9756 	struct rpc_task_setup task_setup_data = {
9757 		.rpc_client = clp->cl_rpcclient,
9758 		.rpc_message = &msg,
9759 		.callback_ops = &nfs4_reclaim_complete_call_ops,
9760 		.flags = RPC_TASK_NO_ROUND_ROBIN,
9761 	};
9762 	int status = -ENOMEM;
9763 
9764 	calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9765 	if (calldata == NULL)
9766 		goto out;
9767 	calldata->clp = clp;
9768 	calldata->arg.one_fs = 0;
9769 
9770 	nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9771 	msg.rpc_argp = &calldata->arg;
9772 	msg.rpc_resp = &calldata->res;
9773 	task_setup_data.callback_data = calldata;
9774 	status = nfs4_call_sync_custom(&task_setup_data);
9775 out:
9776 	dprintk("<-- %s status=%d\n", __func__, status);
9777 	return status;
9778 }
9779 
9780 static void
nfs4_layoutget_prepare(struct rpc_task * task,void * calldata)9781 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9782 {
9783 	struct nfs4_layoutget *lgp = calldata;
9784 	struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9785 
9786 	nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9787 				&lgp->res.seq_res, task);
9788 }
9789 
nfs4_layoutget_done(struct rpc_task * task,void * calldata)9790 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9791 {
9792 	struct nfs4_layoutget *lgp = calldata;
9793 
9794 	nfs41_sequence_process(task, &lgp->res.seq_res);
9795 }
9796 
9797 static int
nfs4_layoutget_handle_exception(struct rpc_task * task,struct nfs4_layoutget * lgp,struct nfs4_exception * exception)9798 nfs4_layoutget_handle_exception(struct rpc_task *task,
9799 		struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9800 {
9801 	struct inode *inode = lgp->args.inode;
9802 	struct nfs_server *server = NFS_SERVER(inode);
9803 	struct pnfs_layout_hdr *lo = lgp->lo;
9804 	int nfs4err = task->tk_status;
9805 	int err, status = 0;
9806 	LIST_HEAD(head);
9807 
9808 	dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9809 
9810 	nfs4_sequence_free_slot(&lgp->res.seq_res);
9811 
9812 	exception->state = NULL;
9813 	exception->stateid = NULL;
9814 
9815 	switch (nfs4err) {
9816 	case 0:
9817 		goto out;
9818 
9819 	/*
9820 	 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9821 	 * on the file. set tk_status to -ENODATA to tell upper layer to
9822 	 * retry go inband.
9823 	 */
9824 	case -NFS4ERR_LAYOUTUNAVAILABLE:
9825 		status = -ENODATA;
9826 		goto out;
9827 	/*
9828 	 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9829 	 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9830 	 */
9831 	case -NFS4ERR_BADLAYOUT:
9832 		status = -EOVERFLOW;
9833 		goto out;
9834 	/*
9835 	 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9836 	 * (or clients) writing to the same RAID stripe except when
9837 	 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9838 	 *
9839 	 * Treat it like we would RECALLCONFLICT -- we retry for a little
9840 	 * while, and then eventually give up.
9841 	 */
9842 	case -NFS4ERR_LAYOUTTRYLATER:
9843 		if (lgp->args.minlength == 0) {
9844 			status = -EOVERFLOW;
9845 			goto out;
9846 		}
9847 		status = -EBUSY;
9848 		break;
9849 	case -NFS4ERR_RECALLCONFLICT:
9850 	case -NFS4ERR_RETURNCONFLICT:
9851 		status = -ERECALLCONFLICT;
9852 		break;
9853 	case -NFS4ERR_DELEG_REVOKED:
9854 	case -NFS4ERR_ADMIN_REVOKED:
9855 	case -NFS4ERR_EXPIRED:
9856 	case -NFS4ERR_BAD_STATEID:
9857 		exception->timeout = 0;
9858 		spin_lock(&inode->i_lock);
9859 		/* If the open stateid was bad, then recover it. */
9860 		if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9861 		    !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9862 			spin_unlock(&inode->i_lock);
9863 			exception->state = lgp->args.ctx->state;
9864 			exception->stateid = &lgp->args.stateid;
9865 			break;
9866 		}
9867 
9868 		/*
9869 		 * Mark the bad layout state as invalid, then retry
9870 		 */
9871 		pnfs_mark_layout_stateid_invalid(lo, &head);
9872 		spin_unlock(&inode->i_lock);
9873 		nfs_commit_inode(inode, 0);
9874 		pnfs_free_lseg_list(&head);
9875 		status = -EAGAIN;
9876 		goto out;
9877 	}
9878 
9879 	err = nfs4_handle_exception(server, nfs4err, exception);
9880 	if (!status) {
9881 		if (exception->retry)
9882 			status = -EAGAIN;
9883 		else
9884 			status = err;
9885 	}
9886 out:
9887 	return status;
9888 }
9889 
max_response_pages(struct nfs_server * server)9890 size_t max_response_pages(struct nfs_server *server)
9891 {
9892 	u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9893 	return nfs_page_array_len(0, max_resp_sz);
9894 }
9895 
nfs4_layoutget_release(void * calldata)9896 static void nfs4_layoutget_release(void *calldata)
9897 {
9898 	struct nfs4_layoutget *lgp = calldata;
9899 
9900 	nfs4_sequence_free_slot(&lgp->res.seq_res);
9901 	pnfs_layoutget_free(lgp);
9902 }
9903 
9904 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9905 	.rpc_call_prepare = nfs4_layoutget_prepare,
9906 	.rpc_call_done = nfs4_layoutget_done,
9907 	.rpc_release = nfs4_layoutget_release,
9908 };
9909 
9910 struct pnfs_layout_segment *
nfs4_proc_layoutget(struct nfs4_layoutget * lgp,struct nfs4_exception * exception)9911 nfs4_proc_layoutget(struct nfs4_layoutget *lgp,
9912 		    struct nfs4_exception *exception)
9913 {
9914 	struct inode *inode = lgp->args.inode;
9915 	struct nfs_server *server = NFS_SERVER(inode);
9916 	struct rpc_task *task;
9917 	struct rpc_message msg = {
9918 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9919 		.rpc_argp = &lgp->args,
9920 		.rpc_resp = &lgp->res,
9921 		.rpc_cred = lgp->cred,
9922 	};
9923 	struct rpc_task_setup task_setup_data = {
9924 		.rpc_client = server->client,
9925 		.rpc_message = &msg,
9926 		.callback_ops = &nfs4_layoutget_call_ops,
9927 		.callback_data = lgp,
9928 		.flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF |
9929 			 RPC_TASK_MOVEABLE,
9930 	};
9931 	struct pnfs_layout_segment *lseg = NULL;
9932 	int status = 0;
9933 
9934 	nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9935 	exception->retry = 0;
9936 
9937 	task = rpc_run_task(&task_setup_data);
9938 	if (IS_ERR(task))
9939 		return ERR_CAST(task);
9940 
9941 	status = rpc_wait_for_completion_task(task);
9942 	if (status != 0)
9943 		goto out;
9944 
9945 	if (task->tk_status < 0) {
9946 		exception->retry = 1;
9947 		status = nfs4_layoutget_handle_exception(task, lgp, exception);
9948 	} else if (lgp->res.layoutp->len == 0) {
9949 		exception->retry = 1;
9950 		status = -EAGAIN;
9951 		nfs4_update_delay(&exception->timeout);
9952 	} else
9953 		lseg = pnfs_layout_process(lgp);
9954 out:
9955 	trace_nfs4_layoutget(lgp->args.ctx,
9956 			&lgp->args.range,
9957 			&lgp->res.range,
9958 			&lgp->res.stateid,
9959 			status);
9960 
9961 	rpc_put_task(task);
9962 	dprintk("<-- %s status=%d\n", __func__, status);
9963 	if (status)
9964 		return ERR_PTR(status);
9965 	return lseg;
9966 }
9967 
9968 static void
nfs4_layoutreturn_prepare(struct rpc_task * task,void * calldata)9969 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9970 {
9971 	struct nfs4_layoutreturn *lrp = calldata;
9972 
9973 	nfs4_setup_sequence(lrp->clp,
9974 			&lrp->args.seq_args,
9975 			&lrp->res.seq_res,
9976 			task);
9977 	if (!pnfs_layout_is_valid(lrp->args.layout))
9978 		rpc_exit(task, 0);
9979 }
9980 
nfs4_layoutreturn_done(struct rpc_task * task,void * calldata)9981 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9982 {
9983 	struct nfs4_layoutreturn *lrp = calldata;
9984 	struct nfs_server *server;
9985 
9986 	if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9987 		return;
9988 
9989 	if (task->tk_rpc_status == -ETIMEDOUT) {
9990 		lrp->rpc_status = -EAGAIN;
9991 		lrp->res.lrs_present = 0;
9992 		return;
9993 	}
9994 	/*
9995 	 * Was there an RPC level error? Assume the call succeeded,
9996 	 * and that we need to release the layout
9997 	 */
9998 	if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9999 		lrp->res.lrs_present = 0;
10000 		return;
10001 	}
10002 
10003 	server = NFS_SERVER(lrp->args.inode);
10004 	switch (task->tk_status) {
10005 	case -NFS4ERR_OLD_STATEID:
10006 		if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
10007 					&lrp->args.range,
10008 					lrp->args.inode))
10009 			goto out_restart;
10010 		fallthrough;
10011 	default:
10012 		task->tk_status = 0;
10013 		lrp->res.lrs_present = 0;
10014 		fallthrough;
10015 	case 0:
10016 		break;
10017 	case -NFS4ERR_BADSESSION:
10018 	case -NFS4ERR_DEADSESSION:
10019 	case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10020 		nfs4_schedule_session_recovery(server->nfs_client->cl_session,
10021 					       task->tk_status);
10022 		lrp->res.lrs_present = 0;
10023 		lrp->rpc_status = -EAGAIN;
10024 		task->tk_status = 0;
10025 		break;
10026 	case -NFS4ERR_DELAY:
10027 		if (nfs4_async_handle_error(task, server, NULL, NULL) ==
10028 		    -EAGAIN)
10029 			goto out_restart;
10030 		lrp->res.lrs_present = 0;
10031 		break;
10032 	}
10033 	return;
10034 out_restart:
10035 	task->tk_status = 0;
10036 	nfs4_sequence_free_slot(&lrp->res.seq_res);
10037 	rpc_restart_call_prepare(task);
10038 }
10039 
nfs4_layoutreturn_release(void * calldata)10040 static void nfs4_layoutreturn_release(void *calldata)
10041 {
10042 	struct nfs4_layoutreturn *lrp = calldata;
10043 	struct pnfs_layout_hdr *lo = lrp->args.layout;
10044 
10045 	if (lrp->rpc_status == 0 || !lrp->inode)
10046 		pnfs_layoutreturn_free_lsegs(
10047 			lo, &lrp->args.stateid, &lrp->args.range,
10048 			lrp->res.lrs_present ? &lrp->res.stateid : NULL);
10049 	else
10050 		pnfs_layoutreturn_retry_later(lo, &lrp->args.stateid,
10051 					      &lrp->args.range);
10052 	nfs4_sequence_free_slot(&lrp->res.seq_res);
10053 	if (lrp->ld_private.ops && lrp->ld_private.ops->free)
10054 		lrp->ld_private.ops->free(&lrp->ld_private);
10055 	pnfs_put_layout_hdr(lrp->args.layout);
10056 	nfs_iput_and_deactive(lrp->inode);
10057 	put_cred(lrp->cred);
10058 	kfree(calldata);
10059 }
10060 
10061 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
10062 	.rpc_call_prepare = nfs4_layoutreturn_prepare,
10063 	.rpc_call_done = nfs4_layoutreturn_done,
10064 	.rpc_release = nfs4_layoutreturn_release,
10065 };
10066 
nfs4_proc_layoutreturn(struct nfs4_layoutreturn * lrp,unsigned int flags)10067 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, unsigned int flags)
10068 {
10069 	struct rpc_task *task;
10070 	struct rpc_message msg = {
10071 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
10072 		.rpc_argp = &lrp->args,
10073 		.rpc_resp = &lrp->res,
10074 		.rpc_cred = lrp->cred,
10075 	};
10076 	struct rpc_task_setup task_setup_data = {
10077 		.rpc_client = NFS_SERVER(lrp->args.inode)->client,
10078 		.rpc_message = &msg,
10079 		.callback_ops = &nfs4_layoutreturn_call_ops,
10080 		.callback_data = lrp,
10081 		.flags = RPC_TASK_MOVEABLE,
10082 	};
10083 	int status = 0;
10084 
10085 	nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
10086 			NFS_SP4_MACH_CRED_PNFS_CLEANUP,
10087 			&task_setup_data.rpc_client, &msg);
10088 
10089 	lrp->inode = nfs_igrab_and_active(lrp->args.inode);
10090 	if (flags & PNFS_FL_LAYOUTRETURN_ASYNC) {
10091 		if (!lrp->inode) {
10092 			nfs4_layoutreturn_release(lrp);
10093 			return -EAGAIN;
10094 		}
10095 		task_setup_data.flags |= RPC_TASK_ASYNC;
10096 	}
10097 	if (!lrp->inode)
10098 		flags |= PNFS_FL_LAYOUTRETURN_PRIVILEGED;
10099 	if (flags & PNFS_FL_LAYOUTRETURN_PRIVILEGED)
10100 		nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
10101 				   1);
10102 	else
10103 		nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
10104 				   0);
10105 	task = rpc_run_task(&task_setup_data);
10106 	if (IS_ERR(task))
10107 		return PTR_ERR(task);
10108 	if (!(flags & PNFS_FL_LAYOUTRETURN_ASYNC))
10109 		status = task->tk_status;
10110 	trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
10111 	dprintk("<-- %s status=%d\n", __func__, status);
10112 	rpc_put_task(task);
10113 	return status;
10114 }
10115 
10116 static int
_nfs4_proc_getdeviceinfo(struct nfs_server * server,struct pnfs_device * pdev,const struct cred * cred)10117 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
10118 		struct pnfs_device *pdev,
10119 		const struct cred *cred)
10120 {
10121 	struct nfs4_getdeviceinfo_args args = {
10122 		.pdev = pdev,
10123 		.notify_types = NOTIFY_DEVICEID4_CHANGE |
10124 			NOTIFY_DEVICEID4_DELETE,
10125 	};
10126 	struct nfs4_getdeviceinfo_res res = {
10127 		.pdev = pdev,
10128 	};
10129 	struct rpc_message msg = {
10130 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
10131 		.rpc_argp = &args,
10132 		.rpc_resp = &res,
10133 		.rpc_cred = cred,
10134 	};
10135 	int status;
10136 
10137 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
10138 	if (res.notification & ~args.notify_types)
10139 		dprintk("%s: unsupported notification\n", __func__);
10140 	if (res.notification != args.notify_types)
10141 		pdev->nocache = 1;
10142 
10143 	trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
10144 
10145 	dprintk("<-- %s status=%d\n", __func__, status);
10146 
10147 	return status;
10148 }
10149 
nfs4_proc_getdeviceinfo(struct nfs_server * server,struct pnfs_device * pdev,const struct cred * cred)10150 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
10151 		struct pnfs_device *pdev,
10152 		const struct cred *cred)
10153 {
10154 	struct nfs4_exception exception = { };
10155 	int err;
10156 
10157 	do {
10158 		err = nfs4_handle_exception(server,
10159 					_nfs4_proc_getdeviceinfo(server, pdev, cred),
10160 					&exception);
10161 	} while (exception.retry);
10162 	return err;
10163 }
10164 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
10165 
nfs4_layoutcommit_prepare(struct rpc_task * task,void * calldata)10166 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
10167 {
10168 	struct nfs4_layoutcommit_data *data = calldata;
10169 	struct nfs_server *server = NFS_SERVER(data->args.inode);
10170 
10171 	nfs4_setup_sequence(server->nfs_client,
10172 			&data->args.seq_args,
10173 			&data->res.seq_res,
10174 			task);
10175 }
10176 
10177 static void
nfs4_layoutcommit_done(struct rpc_task * task,void * calldata)10178 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
10179 {
10180 	struct nfs4_layoutcommit_data *data = calldata;
10181 	struct nfs_server *server = NFS_SERVER(data->args.inode);
10182 
10183 	if (!nfs41_sequence_done(task, &data->res.seq_res))
10184 		return;
10185 
10186 	switch (task->tk_status) { /* Just ignore these failures */
10187 	case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
10188 	case -NFS4ERR_BADIOMODE:     /* no IOMODE_RW layout for range */
10189 	case -NFS4ERR_BADLAYOUT:     /* no layout */
10190 	case -NFS4ERR_GRACE:	    /* loca_recalim always false */
10191 		task->tk_status = 0;
10192 		break;
10193 	case 0:
10194 		break;
10195 	default:
10196 		if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
10197 			rpc_restart_call_prepare(task);
10198 			return;
10199 		}
10200 	}
10201 }
10202 
nfs4_layoutcommit_release(void * calldata)10203 static void nfs4_layoutcommit_release(void *calldata)
10204 {
10205 	struct nfs4_layoutcommit_data *data = calldata;
10206 
10207 	pnfs_cleanup_layoutcommit(data);
10208 	nfs_post_op_update_inode_force_wcc(data->args.inode,
10209 					   data->res.fattr);
10210 	put_cred(data->cred);
10211 	nfs_iput_and_deactive(data->inode);
10212 	kfree(data);
10213 }
10214 
10215 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
10216 	.rpc_call_prepare = nfs4_layoutcommit_prepare,
10217 	.rpc_call_done = nfs4_layoutcommit_done,
10218 	.rpc_release = nfs4_layoutcommit_release,
10219 };
10220 
10221 int
nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data * data,bool sync)10222 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
10223 {
10224 	struct rpc_message msg = {
10225 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
10226 		.rpc_argp = &data->args,
10227 		.rpc_resp = &data->res,
10228 		.rpc_cred = data->cred,
10229 	};
10230 	struct rpc_task_setup task_setup_data = {
10231 		.task = &data->task,
10232 		.rpc_client = NFS_CLIENT(data->args.inode),
10233 		.rpc_message = &msg,
10234 		.callback_ops = &nfs4_layoutcommit_ops,
10235 		.callback_data = data,
10236 		.flags = RPC_TASK_MOVEABLE,
10237 	};
10238 	struct rpc_task *task;
10239 	int status = 0;
10240 
10241 	dprintk("NFS: initiating layoutcommit call. sync %d "
10242 		"lbw: %llu inode %lu\n", sync,
10243 		data->args.lastbytewritten,
10244 		data->args.inode->i_ino);
10245 
10246 	if (!sync) {
10247 		data->inode = nfs_igrab_and_active(data->args.inode);
10248 		if (data->inode == NULL) {
10249 			nfs4_layoutcommit_release(data);
10250 			return -EAGAIN;
10251 		}
10252 		task_setup_data.flags = RPC_TASK_ASYNC;
10253 	}
10254 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
10255 	task = rpc_run_task(&task_setup_data);
10256 	if (IS_ERR(task))
10257 		return PTR_ERR(task);
10258 	if (sync)
10259 		status = task->tk_status;
10260 	trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
10261 	dprintk("%s: status %d\n", __func__, status);
10262 	rpc_put_task(task);
10263 	return status;
10264 }
10265 
10266 /*
10267  * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
10268  * possible) as per RFC3530bis and RFC5661 Security Considerations sections
10269  */
10270 static int
_nfs41_proc_secinfo_no_name(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * info,struct nfs4_secinfo_flavors * flavors,bool use_integrity)10271 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10272 		    struct nfs_fsinfo *info,
10273 		    struct nfs4_secinfo_flavors *flavors, bool use_integrity)
10274 {
10275 	struct nfs41_secinfo_no_name_args args = {
10276 		.style = SECINFO_STYLE_CURRENT_FH,
10277 	};
10278 	struct nfs4_secinfo_res res = {
10279 		.flavors = flavors,
10280 	};
10281 	struct rpc_message msg = {
10282 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
10283 		.rpc_argp = &args,
10284 		.rpc_resp = &res,
10285 	};
10286 	struct nfs4_call_sync_data data = {
10287 		.seq_server = server,
10288 		.seq_args = &args.seq_args,
10289 		.seq_res = &res.seq_res,
10290 	};
10291 	struct rpc_task_setup task_setup = {
10292 		.rpc_client = server->client,
10293 		.rpc_message = &msg,
10294 		.callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
10295 		.callback_data = &data,
10296 		.flags = RPC_TASK_NO_ROUND_ROBIN,
10297 	};
10298 	const struct cred *cred = NULL;
10299 	int status;
10300 
10301 	if (use_integrity) {
10302 		task_setup.rpc_client = server->nfs_client->cl_rpcclient;
10303 
10304 		cred = nfs4_get_clid_cred(server->nfs_client);
10305 		msg.rpc_cred = cred;
10306 	}
10307 
10308 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
10309 	status = nfs4_call_sync_custom(&task_setup);
10310 	dprintk("<-- %s status=%d\n", __func__, status);
10311 
10312 	put_cred(cred);
10313 
10314 	return status;
10315 }
10316 
10317 static int
nfs41_proc_secinfo_no_name(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * info,struct nfs4_secinfo_flavors * flavors)10318 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10319 			   struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
10320 {
10321 	struct nfs4_exception exception = {
10322 		.interruptible = true,
10323 	};
10324 	int err;
10325 	do {
10326 		/* first try using integrity protection */
10327 		err = -NFS4ERR_WRONGSEC;
10328 
10329 		/* try to use integrity protection with machine cred */
10330 		if (_nfs4_is_integrity_protected(server->nfs_client))
10331 			err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10332 							  flavors, true);
10333 
10334 		/*
10335 		 * if unable to use integrity protection, or SECINFO with
10336 		 * integrity protection returns NFS4ERR_WRONGSEC (which is
10337 		 * disallowed by spec, but exists in deployed servers) use
10338 		 * the current filesystem's rpc_client and the user cred.
10339 		 */
10340 		if (err == -NFS4ERR_WRONGSEC)
10341 			err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10342 							  flavors, false);
10343 
10344 		switch (err) {
10345 		case 0:
10346 		case -NFS4ERR_WRONGSEC:
10347 		case -ENOTSUPP:
10348 			goto out;
10349 		default:
10350 			err = nfs4_handle_exception(server, err, &exception);
10351 		}
10352 	} while (exception.retry);
10353 out:
10354 	return err;
10355 }
10356 
10357 static int
nfs41_find_root_sec(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * info)10358 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
10359 		    struct nfs_fsinfo *info)
10360 {
10361 	int err;
10362 	struct page *page;
10363 	rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
10364 	struct nfs4_secinfo_flavors *flavors;
10365 	struct nfs4_secinfo4 *secinfo;
10366 	int i;
10367 
10368 	page = alloc_page(GFP_KERNEL);
10369 	if (!page) {
10370 		err = -ENOMEM;
10371 		goto out;
10372 	}
10373 
10374 	flavors = page_address(page);
10375 	err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
10376 
10377 	/*
10378 	 * Fall back on "guess and check" method if
10379 	 * the server doesn't support SECINFO_NO_NAME
10380 	 */
10381 	if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
10382 		err = nfs4_find_root_sec(server, fhandle, info);
10383 		goto out_freepage;
10384 	}
10385 	if (err)
10386 		goto out_freepage;
10387 
10388 	for (i = 0; i < flavors->num_flavors; i++) {
10389 		secinfo = &flavors->flavors[i];
10390 
10391 		switch (secinfo->flavor) {
10392 		case RPC_AUTH_NULL:
10393 		case RPC_AUTH_UNIX:
10394 		case RPC_AUTH_GSS:
10395 			flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
10396 					&secinfo->flavor_info);
10397 			break;
10398 		default:
10399 			flavor = RPC_AUTH_MAXFLAVOR;
10400 			break;
10401 		}
10402 
10403 		if (!nfs_auth_info_match(&server->auth_info, flavor))
10404 			flavor = RPC_AUTH_MAXFLAVOR;
10405 
10406 		if (flavor != RPC_AUTH_MAXFLAVOR) {
10407 			err = nfs4_lookup_root_sec(server, fhandle,
10408 						   info, flavor);
10409 			if (!err)
10410 				break;
10411 		}
10412 	}
10413 
10414 	if (flavor == RPC_AUTH_MAXFLAVOR)
10415 		err = -EPERM;
10416 
10417 out_freepage:
10418 	put_page(page);
10419 	if (err == -EACCES)
10420 		return -EPERM;
10421 out:
10422 	return err;
10423 }
10424 
_nfs41_test_stateid(struct nfs_server * server,const nfs4_stateid * stateid,const struct cred * cred)10425 static int _nfs41_test_stateid(struct nfs_server *server,
10426 			       const nfs4_stateid *stateid,
10427 			       const struct cred *cred)
10428 {
10429 	int status;
10430 	struct nfs41_test_stateid_args args = {
10431 		.stateid = *stateid,
10432 	};
10433 	struct nfs41_test_stateid_res res;
10434 	struct rpc_message msg = {
10435 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
10436 		.rpc_argp = &args,
10437 		.rpc_resp = &res,
10438 		.rpc_cred = cred,
10439 	};
10440 	struct rpc_clnt *rpc_client = server->client;
10441 
10442 	nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10443 		&rpc_client, &msg);
10444 
10445 	dprintk("NFS call  test_stateid %p\n", stateid);
10446 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
10447 	status = nfs4_call_sync_sequence(rpc_client, server, &msg,
10448 			&args.seq_args, &res.seq_res);
10449 	if (status != NFS_OK) {
10450 		dprintk("NFS reply test_stateid: failed, %d\n", status);
10451 		return status;
10452 	}
10453 	dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
10454 	return -res.status;
10455 }
10456 
nfs4_handle_delay_or_session_error(struct nfs_server * server,int err,struct nfs4_exception * exception)10457 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10458 		int err, struct nfs4_exception *exception)
10459 {
10460 	exception->retry = 0;
10461 	switch(err) {
10462 	case -NFS4ERR_DELAY:
10463 	case -NFS4ERR_RETRY_UNCACHED_REP:
10464 		nfs4_handle_exception(server, err, exception);
10465 		break;
10466 	case -NFS4ERR_BADSESSION:
10467 	case -NFS4ERR_BADSLOT:
10468 	case -NFS4ERR_BAD_HIGH_SLOT:
10469 	case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10470 	case -NFS4ERR_DEADSESSION:
10471 		nfs4_do_handle_exception(server, err, exception);
10472 	}
10473 }
10474 
10475 /**
10476  * nfs41_test_stateid - perform a TEST_STATEID operation
10477  *
10478  * @server: server / transport on which to perform the operation
10479  * @stateid: state ID to test
10480  * @cred: credential
10481  *
10482  * Returns NFS_OK if the server recognizes that "stateid" is valid.
10483  * Otherwise a negative NFS4ERR value is returned if the operation
10484  * failed or the state ID is not currently valid.
10485  */
nfs41_test_stateid(struct nfs_server * server,const nfs4_stateid * stateid,const struct cred * cred)10486 static int nfs41_test_stateid(struct nfs_server *server,
10487 			      const nfs4_stateid *stateid,
10488 			      const struct cred *cred)
10489 {
10490 	struct nfs4_exception exception = {
10491 		.interruptible = true,
10492 	};
10493 	int err;
10494 	do {
10495 		err = _nfs41_test_stateid(server, stateid, cred);
10496 		nfs4_handle_delay_or_session_error(server, err, &exception);
10497 	} while (exception.retry);
10498 	return err;
10499 }
10500 
10501 struct nfs_free_stateid_data {
10502 	struct nfs_server *server;
10503 	struct nfs41_free_stateid_args args;
10504 	struct nfs41_free_stateid_res res;
10505 };
10506 
nfs41_free_stateid_prepare(struct rpc_task * task,void * calldata)10507 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10508 {
10509 	struct nfs_free_stateid_data *data = calldata;
10510 	nfs4_setup_sequence(data->server->nfs_client,
10511 			&data->args.seq_args,
10512 			&data->res.seq_res,
10513 			task);
10514 }
10515 
nfs41_free_stateid_done(struct rpc_task * task,void * calldata)10516 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10517 {
10518 	struct nfs_free_stateid_data *data = calldata;
10519 
10520 	nfs41_sequence_done(task, &data->res.seq_res);
10521 
10522 	switch (task->tk_status) {
10523 	case -NFS4ERR_DELAY:
10524 		if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
10525 			rpc_restart_call_prepare(task);
10526 	}
10527 }
10528 
nfs41_free_stateid_release(void * calldata)10529 static void nfs41_free_stateid_release(void *calldata)
10530 {
10531 	struct nfs_free_stateid_data *data = calldata;
10532 	struct nfs_client *clp = data->server->nfs_client;
10533 
10534 	nfs_put_client(clp);
10535 	kfree(calldata);
10536 }
10537 
10538 static const struct rpc_call_ops nfs41_free_stateid_ops = {
10539 	.rpc_call_prepare = nfs41_free_stateid_prepare,
10540 	.rpc_call_done = nfs41_free_stateid_done,
10541 	.rpc_release = nfs41_free_stateid_release,
10542 };
10543 
10544 /**
10545  * nfs41_free_stateid - perform a FREE_STATEID operation
10546  *
10547  * @server: server / transport on which to perform the operation
10548  * @stateid: state ID to release
10549  * @cred: credential
10550  * @privileged: set to true if this call needs to be privileged
10551  *
10552  * Note: this function is always asynchronous.
10553  */
nfs41_free_stateid(struct nfs_server * server,const nfs4_stateid * stateid,const struct cred * cred,bool privileged)10554 static int nfs41_free_stateid(struct nfs_server *server,
10555 		const nfs4_stateid *stateid,
10556 		const struct cred *cred,
10557 		bool privileged)
10558 {
10559 	struct rpc_message msg = {
10560 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
10561 		.rpc_cred = cred,
10562 	};
10563 	struct rpc_task_setup task_setup = {
10564 		.rpc_client = server->client,
10565 		.rpc_message = &msg,
10566 		.callback_ops = &nfs41_free_stateid_ops,
10567 		.flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE,
10568 	};
10569 	struct nfs_free_stateid_data *data;
10570 	struct rpc_task *task;
10571 	struct nfs_client *clp = server->nfs_client;
10572 
10573 	if (!refcount_inc_not_zero(&clp->cl_count))
10574 		return -EIO;
10575 
10576 	nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10577 		&task_setup.rpc_client, &msg);
10578 
10579 	dprintk("NFS call  free_stateid %p\n", stateid);
10580 	data = kmalloc(sizeof(*data), GFP_KERNEL);
10581 	if (!data)
10582 		return -ENOMEM;
10583 	data->server = server;
10584 	nfs4_stateid_copy(&data->args.stateid, stateid);
10585 
10586 	task_setup.callback_data = data;
10587 
10588 	msg.rpc_argp = &data->args;
10589 	msg.rpc_resp = &data->res;
10590 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
10591 	task = rpc_run_task(&task_setup);
10592 	if (IS_ERR(task))
10593 		return PTR_ERR(task);
10594 	rpc_put_task(task);
10595 	return 0;
10596 }
10597 
10598 static void
nfs41_free_lock_state(struct nfs_server * server,struct nfs4_lock_state * lsp)10599 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
10600 {
10601 	const struct cred *cred = lsp->ls_state->owner->so_cred;
10602 
10603 	nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10604 	nfs4_free_lock_state(server, lsp);
10605 }
10606 
nfs41_match_stateid(const nfs4_stateid * s1,const nfs4_stateid * s2)10607 static bool nfs41_match_stateid(const nfs4_stateid *s1,
10608 		const nfs4_stateid *s2)
10609 {
10610 	if (s1->type != s2->type)
10611 		return false;
10612 
10613 	if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10614 		return false;
10615 
10616 	if (s1->seqid == s2->seqid)
10617 		return true;
10618 
10619 	return s1->seqid == 0 || s2->seqid == 0;
10620 }
10621 
10622 #endif /* CONFIG_NFS_V4_1 */
10623 
nfs4_match_stateid(const nfs4_stateid * s1,const nfs4_stateid * s2)10624 static bool nfs4_match_stateid(const nfs4_stateid *s1,
10625 		const nfs4_stateid *s2)
10626 {
10627 	return nfs4_stateid_match(s1, s2);
10628 }
10629 
10630 
10631 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10632 	.owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10633 	.state_flag_bit	= NFS_STATE_RECLAIM_REBOOT,
10634 	.recover_open	= nfs4_open_reclaim,
10635 	.recover_lock	= nfs4_lock_reclaim,
10636 	.establish_clid = nfs4_init_clientid,
10637 	.detect_trunking = nfs40_discover_server_trunking,
10638 };
10639 
10640 #if defined(CONFIG_NFS_V4_1)
10641 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10642 	.owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10643 	.state_flag_bit	= NFS_STATE_RECLAIM_REBOOT,
10644 	.recover_open	= nfs4_open_reclaim,
10645 	.recover_lock	= nfs4_lock_reclaim,
10646 	.establish_clid = nfs41_init_clientid,
10647 	.reclaim_complete = nfs41_proc_reclaim_complete,
10648 	.detect_trunking = nfs41_discover_server_trunking,
10649 };
10650 #endif /* CONFIG_NFS_V4_1 */
10651 
10652 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10653 	.owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10654 	.state_flag_bit	= NFS_STATE_RECLAIM_NOGRACE,
10655 	.recover_open	= nfs40_open_expired,
10656 	.recover_lock	= nfs4_lock_expired,
10657 	.establish_clid = nfs4_init_clientid,
10658 };
10659 
10660 #if defined(CONFIG_NFS_V4_1)
10661 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10662 	.owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10663 	.state_flag_bit	= NFS_STATE_RECLAIM_NOGRACE,
10664 	.recover_open	= nfs41_open_expired,
10665 	.recover_lock	= nfs41_lock_expired,
10666 	.establish_clid = nfs41_init_clientid,
10667 };
10668 #endif /* CONFIG_NFS_V4_1 */
10669 
10670 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10671 	.sched_state_renewal = nfs4_proc_async_renew,
10672 	.get_state_renewal_cred = nfs4_get_renew_cred,
10673 	.renew_lease = nfs4_proc_renew,
10674 };
10675 
10676 #if defined(CONFIG_NFS_V4_1)
10677 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10678 	.sched_state_renewal = nfs41_proc_async_sequence,
10679 	.get_state_renewal_cred = nfs4_get_machine_cred,
10680 	.renew_lease = nfs4_proc_sequence,
10681 };
10682 #endif
10683 
10684 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10685 	.get_locations = _nfs40_proc_get_locations,
10686 	.fsid_present = _nfs40_proc_fsid_present,
10687 };
10688 
10689 #if defined(CONFIG_NFS_V4_1)
10690 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10691 	.get_locations = _nfs41_proc_get_locations,
10692 	.fsid_present = _nfs41_proc_fsid_present,
10693 };
10694 #endif	/* CONFIG_NFS_V4_1 */
10695 
10696 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10697 	.minor_version = 0,
10698 	.init_caps = NFS_CAP_READDIRPLUS
10699 		| NFS_CAP_ATOMIC_OPEN
10700 		| NFS_CAP_POSIX_LOCK,
10701 	.init_client = nfs40_init_client,
10702 	.shutdown_client = nfs40_shutdown_client,
10703 	.match_stateid = nfs4_match_stateid,
10704 	.find_root_sec = nfs4_find_root_sec,
10705 	.free_lock_state = nfs4_release_lockowner,
10706 	.test_and_free_expired = nfs40_test_and_free_expired_stateid,
10707 	.alloc_seqid = nfs_alloc_seqid,
10708 	.call_sync_ops = &nfs40_call_sync_ops,
10709 	.reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10710 	.nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10711 	.state_renewal_ops = &nfs40_state_renewal_ops,
10712 	.mig_recovery_ops = &nfs40_mig_recovery_ops,
10713 };
10714 
10715 #if defined(CONFIG_NFS_V4_1)
10716 static struct nfs_seqid *
nfs_alloc_no_seqid(struct nfs_seqid_counter * arg1,gfp_t arg2)10717 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10718 {
10719 	return NULL;
10720 }
10721 
10722 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10723 	.minor_version = 1,
10724 	.init_caps = NFS_CAP_READDIRPLUS
10725 		| NFS_CAP_ATOMIC_OPEN
10726 		| NFS_CAP_POSIX_LOCK
10727 		| NFS_CAP_STATEID_NFSV41
10728 		| NFS_CAP_ATOMIC_OPEN_V1
10729 		| NFS_CAP_LGOPEN
10730 		| NFS_CAP_MOVEABLE,
10731 	.init_client = nfs41_init_client,
10732 	.shutdown_client = nfs41_shutdown_client,
10733 	.match_stateid = nfs41_match_stateid,
10734 	.find_root_sec = nfs41_find_root_sec,
10735 	.free_lock_state = nfs41_free_lock_state,
10736 	.test_and_free_expired = nfs41_test_and_free_expired_stateid,
10737 	.alloc_seqid = nfs_alloc_no_seqid,
10738 	.session_trunk = nfs4_test_session_trunk,
10739 	.call_sync_ops = &nfs41_call_sync_ops,
10740 	.reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10741 	.nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10742 	.state_renewal_ops = &nfs41_state_renewal_ops,
10743 	.mig_recovery_ops = &nfs41_mig_recovery_ops,
10744 };
10745 #endif
10746 
10747 #if defined(CONFIG_NFS_V4_2)
10748 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10749 	.minor_version = 2,
10750 	.init_caps = NFS_CAP_READDIRPLUS
10751 		| NFS_CAP_ATOMIC_OPEN
10752 		| NFS_CAP_POSIX_LOCK
10753 		| NFS_CAP_STATEID_NFSV41
10754 		| NFS_CAP_ATOMIC_OPEN_V1
10755 		| NFS_CAP_LGOPEN
10756 		| NFS_CAP_ALLOCATE
10757 		| NFS_CAP_COPY
10758 		| NFS_CAP_OFFLOAD_CANCEL
10759 		| NFS_CAP_COPY_NOTIFY
10760 		| NFS_CAP_DEALLOCATE
10761 		| NFS_CAP_SEEK
10762 		| NFS_CAP_LAYOUTSTATS
10763 		| NFS_CAP_CLONE
10764 		| NFS_CAP_LAYOUTERROR
10765 		| NFS_CAP_READ_PLUS
10766 		| NFS_CAP_MOVEABLE,
10767 	.init_client = nfs41_init_client,
10768 	.shutdown_client = nfs41_shutdown_client,
10769 	.match_stateid = nfs41_match_stateid,
10770 	.find_root_sec = nfs41_find_root_sec,
10771 	.free_lock_state = nfs41_free_lock_state,
10772 	.call_sync_ops = &nfs41_call_sync_ops,
10773 	.test_and_free_expired = nfs41_test_and_free_expired_stateid,
10774 	.alloc_seqid = nfs_alloc_no_seqid,
10775 	.session_trunk = nfs4_test_session_trunk,
10776 	.reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10777 	.nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10778 	.state_renewal_ops = &nfs41_state_renewal_ops,
10779 	.mig_recovery_ops = &nfs41_mig_recovery_ops,
10780 };
10781 #endif
10782 
10783 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10784 	[0] = &nfs_v4_0_minor_ops,
10785 #if defined(CONFIG_NFS_V4_1)
10786 	[1] = &nfs_v4_1_minor_ops,
10787 #endif
10788 #if defined(CONFIG_NFS_V4_2)
10789 	[2] = &nfs_v4_2_minor_ops,
10790 #endif
10791 };
10792 
nfs4_listxattr(struct dentry * dentry,char * list,size_t size)10793 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10794 {
10795 	ssize_t error, error2, error3;
10796 	size_t left = size;
10797 
10798 	error = generic_listxattr(dentry, list, left);
10799 	if (error < 0)
10800 		return error;
10801 	if (list) {
10802 		list += error;
10803 		left -= error;
10804 	}
10805 
10806 	error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, left);
10807 	if (error2 < 0)
10808 		return error2;
10809 
10810 	if (list) {
10811 		list += error2;
10812 		left -= error2;
10813 	}
10814 
10815 	error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, left);
10816 	if (error3 < 0)
10817 		return error3;
10818 
10819 	error += error2 + error3;
10820 	if (size && error > size)
10821 		return -ERANGE;
10822 	return error;
10823 }
10824 
nfs4_enable_swap(struct inode * inode)10825 static void nfs4_enable_swap(struct inode *inode)
10826 {
10827 	/* The state manager thread must always be running.
10828 	 * It will notice the client is a swapper, and stay put.
10829 	 */
10830 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10831 
10832 	nfs4_schedule_state_manager(clp);
10833 }
10834 
nfs4_disable_swap(struct inode * inode)10835 static void nfs4_disable_swap(struct inode *inode)
10836 {
10837 	/* The state manager thread will now exit once it is
10838 	 * woken.
10839 	 */
10840 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10841 
10842 	set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
10843 	clear_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state);
10844 	wake_up_var(&clp->cl_state);
10845 }
10846 
10847 static const struct inode_operations nfs4_dir_inode_operations = {
10848 	.create		= nfs_create,
10849 	.lookup		= nfs_lookup,
10850 	.atomic_open	= nfs_atomic_open,
10851 	.link		= nfs_link,
10852 	.unlink		= nfs_unlink,
10853 	.symlink	= nfs_symlink,
10854 	.mkdir		= nfs_mkdir,
10855 	.rmdir		= nfs_rmdir,
10856 	.mknod		= nfs_mknod,
10857 	.rename		= nfs_rename,
10858 	.permission	= nfs_permission,
10859 	.getattr	= nfs_getattr,
10860 	.setattr	= nfs_setattr,
10861 	.listxattr	= nfs4_listxattr,
10862 };
10863 
10864 static const struct inode_operations nfs4_file_inode_operations = {
10865 	.permission	= nfs_permission,
10866 	.getattr	= nfs_getattr,
10867 	.setattr	= nfs_setattr,
10868 	.listxattr	= nfs4_listxattr,
10869 };
10870 
10871 const struct nfs_rpc_ops nfs_v4_clientops = {
10872 	.version	= 4,			/* protocol version */
10873 	.dentry_ops	= &nfs4_dentry_operations,
10874 	.dir_inode_ops	= &nfs4_dir_inode_operations,
10875 	.file_inode_ops	= &nfs4_file_inode_operations,
10876 	.file_ops	= &nfs4_file_operations,
10877 	.getroot	= nfs4_proc_get_root,
10878 	.submount	= nfs4_submount,
10879 	.try_get_tree	= nfs4_try_get_tree,
10880 	.getattr	= nfs4_proc_getattr,
10881 	.setattr	= nfs4_proc_setattr,
10882 	.lookup		= nfs4_proc_lookup,
10883 	.lookupp	= nfs4_proc_lookupp,
10884 	.access		= nfs4_proc_access,
10885 	.readlink	= nfs4_proc_readlink,
10886 	.create		= nfs4_proc_create,
10887 	.remove		= nfs4_proc_remove,
10888 	.unlink_setup	= nfs4_proc_unlink_setup,
10889 	.unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10890 	.unlink_done	= nfs4_proc_unlink_done,
10891 	.rename_setup	= nfs4_proc_rename_setup,
10892 	.rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10893 	.rename_done	= nfs4_proc_rename_done,
10894 	.link		= nfs4_proc_link,
10895 	.symlink	= nfs4_proc_symlink,
10896 	.mkdir		= nfs4_proc_mkdir,
10897 	.rmdir		= nfs4_proc_rmdir,
10898 	.readdir	= nfs4_proc_readdir,
10899 	.mknod		= nfs4_proc_mknod,
10900 	.statfs		= nfs4_proc_statfs,
10901 	.fsinfo		= nfs4_proc_fsinfo,
10902 	.pathconf	= nfs4_proc_pathconf,
10903 	.set_capabilities = nfs4_server_capabilities,
10904 	.decode_dirent	= nfs4_decode_dirent,
10905 	.pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10906 	.read_setup	= nfs4_proc_read_setup,
10907 	.read_done	= nfs4_read_done,
10908 	.write_setup	= nfs4_proc_write_setup,
10909 	.write_done	= nfs4_write_done,
10910 	.commit_setup	= nfs4_proc_commit_setup,
10911 	.commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10912 	.commit_done	= nfs4_commit_done,
10913 	.lock		= nfs4_proc_lock,
10914 	.clear_acl_cache = nfs4_zap_acl_attr,
10915 	.close_context  = nfs4_close_context,
10916 	.open_context	= nfs4_atomic_open,
10917 	.have_delegation = nfs4_have_delegation,
10918 	.return_delegation = nfs4_inode_return_delegation,
10919 	.alloc_client	= nfs4_alloc_client,
10920 	.init_client	= nfs4_init_client,
10921 	.free_client	= nfs4_free_client,
10922 	.create_server	= nfs4_create_server,
10923 	.clone_server	= nfs_clone_server,
10924 	.discover_trunking = nfs4_discover_trunking,
10925 	.enable_swap	= nfs4_enable_swap,
10926 	.disable_swap	= nfs4_disable_swap,
10927 };
10928 
10929 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10930 	.name	= XATTR_NAME_NFSV4_ACL,
10931 	.list	= nfs4_xattr_list_nfs4_acl,
10932 	.get	= nfs4_xattr_get_nfs4_acl,
10933 	.set	= nfs4_xattr_set_nfs4_acl,
10934 };
10935 
10936 #if defined(CONFIG_NFS_V4_1)
10937 static const struct xattr_handler nfs4_xattr_nfs4_dacl_handler = {
10938 	.name	= XATTR_NAME_NFSV4_DACL,
10939 	.list	= nfs4_xattr_list_nfs4_dacl,
10940 	.get	= nfs4_xattr_get_nfs4_dacl,
10941 	.set	= nfs4_xattr_set_nfs4_dacl,
10942 };
10943 
10944 static const struct xattr_handler nfs4_xattr_nfs4_sacl_handler = {
10945 	.name	= XATTR_NAME_NFSV4_SACL,
10946 	.list	= nfs4_xattr_list_nfs4_sacl,
10947 	.get	= nfs4_xattr_get_nfs4_sacl,
10948 	.set	= nfs4_xattr_set_nfs4_sacl,
10949 };
10950 #endif
10951 
10952 #ifdef CONFIG_NFS_V4_2
10953 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10954 	.prefix	= XATTR_USER_PREFIX,
10955 	.get	= nfs4_xattr_get_nfs4_user,
10956 	.set	= nfs4_xattr_set_nfs4_user,
10957 };
10958 #endif
10959 
10960 const struct xattr_handler * const nfs4_xattr_handlers[] = {
10961 	&nfs4_xattr_nfs4_acl_handler,
10962 #if defined(CONFIG_NFS_V4_1)
10963 	&nfs4_xattr_nfs4_dacl_handler,
10964 	&nfs4_xattr_nfs4_sacl_handler,
10965 #endif
10966 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10967 	&nfs4_xattr_nfs4_label_handler,
10968 #endif
10969 #ifdef CONFIG_NFS_V4_2
10970 	&nfs4_xattr_nfs4_user_handler,
10971 #endif
10972 	NULL
10973 };
10974