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 	if (sattr->ia_valid & ATTR_ATIME)
3456 		adjust_flags |= NFS_INO_INVALID_ATIME;
3457 	if (sattr->ia_valid & ATTR_MTIME)
3458 		adjust_flags |= NFS_INO_INVALID_MTIME;
3459 
3460 	do {
3461 		nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label),
3462 					inode, adjust_flags);
3463 
3464 		err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
3465 		switch (err) {
3466 		case -NFS4ERR_OPENMODE:
3467 			if (!(sattr->ia_valid & ATTR_SIZE)) {
3468 				pr_warn_once("NFSv4: server %s is incorrectly "
3469 						"applying open mode checks to "
3470 						"a SETATTR that is not "
3471 						"changing file size.\n",
3472 						server->nfs_client->cl_hostname);
3473 			}
3474 			if (state && !(state->state & FMODE_WRITE)) {
3475 				err = -EBADF;
3476 				if (sattr->ia_valid & ATTR_OPEN)
3477 					err = -EACCES;
3478 				goto out;
3479 			}
3480 		}
3481 		err = nfs4_handle_exception(server, err, &exception);
3482 	} while (exception.retry);
3483 out:
3484 	return err;
3485 }
3486 
3487 static bool
nfs4_wait_on_layoutreturn(struct inode * inode,struct rpc_task * task)3488 nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3489 {
3490 	if (inode == NULL || !nfs_have_layout(inode))
3491 		return false;
3492 
3493 	return pnfs_wait_on_layoutreturn(inode, task);
3494 }
3495 
3496 /*
3497  * Update the seqid of an open stateid
3498  */
nfs4_sync_open_stateid(nfs4_stateid * dst,struct nfs4_state * state)3499 static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3500 		struct nfs4_state *state)
3501 {
3502 	__be32 seqid_open;
3503 	u32 dst_seqid;
3504 	int seq;
3505 
3506 	for (;;) {
3507 		if (!nfs4_valid_open_stateid(state))
3508 			break;
3509 		seq = read_seqbegin(&state->seqlock);
3510 		if (!nfs4_state_match_open_stateid_other(state, dst)) {
3511 			nfs4_stateid_copy(dst, &state->open_stateid);
3512 			if (read_seqretry(&state->seqlock, seq))
3513 				continue;
3514 			break;
3515 		}
3516 		seqid_open = state->open_stateid.seqid;
3517 		if (read_seqretry(&state->seqlock, seq))
3518 			continue;
3519 
3520 		dst_seqid = be32_to_cpu(dst->seqid);
3521 		if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3522 			dst->seqid = seqid_open;
3523 		break;
3524 	}
3525 }
3526 
3527 /*
3528  * Update the seqid of an open stateid after receiving
3529  * NFS4ERR_OLD_STATEID
3530  */
nfs4_refresh_open_old_stateid(nfs4_stateid * dst,struct nfs4_state * state)3531 static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3532 		struct nfs4_state *state)
3533 {
3534 	__be32 seqid_open;
3535 	u32 dst_seqid;
3536 	bool ret;
3537 	int seq, status = -EAGAIN;
3538 	DEFINE_WAIT(wait);
3539 
3540 	for (;;) {
3541 		ret = false;
3542 		if (!nfs4_valid_open_stateid(state))
3543 			break;
3544 		seq = read_seqbegin(&state->seqlock);
3545 		if (!nfs4_state_match_open_stateid_other(state, dst)) {
3546 			if (read_seqretry(&state->seqlock, seq))
3547 				continue;
3548 			break;
3549 		}
3550 
3551 		write_seqlock(&state->seqlock);
3552 		seqid_open = state->open_stateid.seqid;
3553 
3554 		dst_seqid = be32_to_cpu(dst->seqid);
3555 
3556 		/* Did another OPEN bump the state's seqid?  try again: */
3557 		if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) {
3558 			dst->seqid = seqid_open;
3559 			write_sequnlock(&state->seqlock);
3560 			ret = true;
3561 			break;
3562 		}
3563 
3564 		/* server says we're behind but we haven't seen the update yet */
3565 		set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
3566 		prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
3567 		write_sequnlock(&state->seqlock);
3568 		trace_nfs4_close_stateid_update_wait(state->inode, dst, 0);
3569 
3570 		if (fatal_signal_pending(current))
3571 			status = -EINTR;
3572 		else
3573 			if (schedule_timeout(5*HZ) != 0)
3574 				status = 0;
3575 
3576 		finish_wait(&state->waitq, &wait);
3577 
3578 		if (!status)
3579 			continue;
3580 		if (status == -EINTR)
3581 			break;
3582 
3583 		/* we slept the whole 5 seconds, we must have lost a seqid */
3584 		dst->seqid = cpu_to_be32(dst_seqid + 1);
3585 		ret = true;
3586 		break;
3587 	}
3588 
3589 	return ret;
3590 }
3591 
3592 struct nfs4_closedata {
3593 	struct inode *inode;
3594 	struct nfs4_state *state;
3595 	struct nfs_closeargs arg;
3596 	struct nfs_closeres res;
3597 	struct {
3598 		struct nfs4_layoutreturn_args arg;
3599 		struct nfs4_layoutreturn_res res;
3600 		struct nfs4_xdr_opaque_data ld_private;
3601 		u32 roc_barrier;
3602 		bool roc;
3603 	} lr;
3604 	struct nfs_fattr fattr;
3605 	unsigned long timestamp;
3606 };
3607 
nfs4_free_closedata(void * data)3608 static void nfs4_free_closedata(void *data)
3609 {
3610 	struct nfs4_closedata *calldata = data;
3611 	struct nfs4_state_owner *sp = calldata->state->owner;
3612 	struct super_block *sb = calldata->state->inode->i_sb;
3613 
3614 	if (calldata->lr.roc)
3615 		pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3616 				calldata->res.lr_ret);
3617 	nfs4_put_open_state(calldata->state);
3618 	nfs_free_seqid(calldata->arg.seqid);
3619 	nfs4_put_state_owner(sp);
3620 	nfs_sb_deactive(sb);
3621 	kfree(calldata);
3622 }
3623 
nfs4_close_done(struct rpc_task * task,void * data)3624 static void nfs4_close_done(struct rpc_task *task, void *data)
3625 {
3626 	struct nfs4_closedata *calldata = data;
3627 	struct nfs4_state *state = calldata->state;
3628 	struct nfs_server *server = NFS_SERVER(calldata->inode);
3629 	nfs4_stateid *res_stateid = NULL;
3630 	struct nfs4_exception exception = {
3631 		.state = state,
3632 		.inode = calldata->inode,
3633 		.stateid = &calldata->arg.stateid,
3634 	};
3635 
3636 	if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3637 		return;
3638 	trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
3639 
3640 	/* Handle Layoutreturn errors */
3641 	if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res,
3642 			  &calldata->res.lr_ret) == -EAGAIN)
3643 		goto out_restart;
3644 
3645 	/* hmm. we are done with the inode, and in the process of freeing
3646 	 * the state_owner. we keep this around to process errors
3647 	 */
3648 	switch (task->tk_status) {
3649 		case 0:
3650 			res_stateid = &calldata->res.stateid;
3651 			renew_lease(server, calldata->timestamp);
3652 			break;
3653 		case -NFS4ERR_ACCESS:
3654 			if (calldata->arg.bitmask != NULL) {
3655 				calldata->arg.bitmask = NULL;
3656 				calldata->res.fattr = NULL;
3657 				goto out_restart;
3658 
3659 			}
3660 			break;
3661 		case -NFS4ERR_OLD_STATEID:
3662 			/* Did we race with OPEN? */
3663 			if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
3664 						state))
3665 				goto out_restart;
3666 			goto out_release;
3667 		case -NFS4ERR_ADMIN_REVOKED:
3668 		case -NFS4ERR_STALE_STATEID:
3669 		case -NFS4ERR_EXPIRED:
3670 			nfs4_free_revoked_stateid(server,
3671 					&calldata->arg.stateid,
3672 					task->tk_msg.rpc_cred);
3673 			fallthrough;
3674 		case -NFS4ERR_BAD_STATEID:
3675 			if (calldata->arg.fmode == 0)
3676 				break;
3677 			fallthrough;
3678 		default:
3679 			task->tk_status = nfs4_async_handle_exception(task,
3680 					server, task->tk_status, &exception);
3681 			if (exception.retry)
3682 				goto out_restart;
3683 	}
3684 	nfs_clear_open_stateid(state, &calldata->arg.stateid,
3685 			res_stateid, calldata->arg.fmode);
3686 out_release:
3687 	task->tk_status = 0;
3688 	nfs_release_seqid(calldata->arg.seqid);
3689 	nfs_refresh_inode(calldata->inode, &calldata->fattr);
3690 	dprintk("%s: ret = %d\n", __func__, task->tk_status);
3691 	return;
3692 out_restart:
3693 	task->tk_status = 0;
3694 	rpc_restart_call_prepare(task);
3695 	goto out_release;
3696 }
3697 
nfs4_close_prepare(struct rpc_task * task,void * data)3698 static void nfs4_close_prepare(struct rpc_task *task, void *data)
3699 {
3700 	struct nfs4_closedata *calldata = data;
3701 	struct nfs4_state *state = calldata->state;
3702 	struct inode *inode = calldata->inode;
3703 	struct nfs_server *server = NFS_SERVER(inode);
3704 	struct pnfs_layout_hdr *lo;
3705 	bool is_rdonly, is_wronly, is_rdwr;
3706 	int call_close = 0;
3707 
3708 	if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
3709 		goto out_wait;
3710 
3711 	task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
3712 	spin_lock(&state->owner->so_lock);
3713 	is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3714 	is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3715 	is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
3716 	/* Calculate the change in open mode */
3717 	calldata->arg.fmode = 0;
3718 	if (state->n_rdwr == 0) {
3719 		if (state->n_rdonly == 0)
3720 			call_close |= is_rdonly;
3721 		else if (is_rdonly)
3722 			calldata->arg.fmode |= FMODE_READ;
3723 		if (state->n_wronly == 0)
3724 			call_close |= is_wronly;
3725 		else if (is_wronly)
3726 			calldata->arg.fmode |= FMODE_WRITE;
3727 		if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3728 			call_close |= is_rdwr;
3729 	} else if (is_rdwr)
3730 		calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3731 
3732 	nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3733 	if (!nfs4_valid_open_stateid(state))
3734 		call_close = 0;
3735 	spin_unlock(&state->owner->so_lock);
3736 
3737 	if (!call_close) {
3738 		/* Note: exit _without_ calling nfs4_close_done */
3739 		goto out_no_action;
3740 	}
3741 
3742 	if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
3743 		nfs_release_seqid(calldata->arg.seqid);
3744 		goto out_wait;
3745 	}
3746 
3747 	lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3748 	if (lo && !pnfs_layout_is_valid(lo)) {
3749 		calldata->arg.lr_args = NULL;
3750 		calldata->res.lr_res = NULL;
3751 	}
3752 
3753 	if (calldata->arg.fmode == 0)
3754 		task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
3755 
3756 	if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
3757 		/* Close-to-open cache consistency revalidation */
3758 		if (!nfs4_have_delegation(inode, FMODE_READ, 0)) {
3759 			nfs4_bitmask_set(calldata->arg.bitmask_store,
3760 					 server->cache_consistency_bitmask,
3761 					 inode, 0);
3762 			calldata->arg.bitmask = calldata->arg.bitmask_store;
3763 		} else
3764 			calldata->arg.bitmask = NULL;
3765 	}
3766 
3767 	calldata->arg.share_access =
3768 		nfs4_fmode_to_share_access(calldata->arg.fmode);
3769 
3770 	if (calldata->res.fattr == NULL)
3771 		calldata->arg.bitmask = NULL;
3772 	else if (calldata->arg.bitmask == NULL)
3773 		calldata->res.fattr = NULL;
3774 	calldata->timestamp = jiffies;
3775 	if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
3776 				&calldata->arg.seq_args,
3777 				&calldata->res.seq_res,
3778 				task) != 0)
3779 		nfs_release_seqid(calldata->arg.seqid);
3780 	return;
3781 out_no_action:
3782 	task->tk_action = NULL;
3783 out_wait:
3784 	nfs4_sequence_done(task, &calldata->res.seq_res);
3785 }
3786 
3787 static const struct rpc_call_ops nfs4_close_ops = {
3788 	.rpc_call_prepare = nfs4_close_prepare,
3789 	.rpc_call_done = nfs4_close_done,
3790 	.rpc_release = nfs4_free_closedata,
3791 };
3792 
3793 /*
3794  * It is possible for data to be read/written from a mem-mapped file
3795  * after the sys_close call (which hits the vfs layer as a flush).
3796  * This means that we can't safely call nfsv4 close on a file until
3797  * the inode is cleared. This in turn means that we are not good
3798  * NFSv4 citizens - we do not indicate to the server to update the file's
3799  * share state even when we are done with one of the three share
3800  * stateid's in the inode.
3801  *
3802  * NOTE: Caller must be holding the sp->so_owner semaphore!
3803  */
nfs4_do_close(struct nfs4_state * state,gfp_t gfp_mask,int wait)3804 int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
3805 {
3806 	struct nfs_server *server = NFS_SERVER(state->inode);
3807 	struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
3808 	struct nfs4_closedata *calldata;
3809 	struct nfs4_state_owner *sp = state->owner;
3810 	struct rpc_task *task;
3811 	struct rpc_message msg = {
3812 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3813 		.rpc_cred = state->owner->so_cred,
3814 	};
3815 	struct rpc_task_setup task_setup_data = {
3816 		.rpc_client = server->client,
3817 		.rpc_message = &msg,
3818 		.callback_ops = &nfs4_close_ops,
3819 		.workqueue = nfsiod_workqueue,
3820 		.flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
3821 	};
3822 	int status = -ENOMEM;
3823 
3824 	if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
3825 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
3826 
3827 	nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3828 		&task_setup_data.rpc_client, &msg);
3829 
3830 	calldata = kzalloc(sizeof(*calldata), gfp_mask);
3831 	if (calldata == NULL)
3832 		goto out;
3833 	nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
3834 	calldata->inode = state->inode;
3835 	calldata->state = state;
3836 	calldata->arg.fh = NFS_FH(state->inode);
3837 	if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3838 		goto out_free_calldata;
3839 	/* Serialization for the sequence id */
3840 	alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3841 	calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
3842 	if (IS_ERR(calldata->arg.seqid))
3843 		goto out_free_calldata;
3844 	nfs_fattr_init(&calldata->fattr);
3845 	calldata->arg.fmode = 0;
3846 	calldata->lr.arg.ld_private = &calldata->lr.ld_private;
3847 	calldata->res.fattr = &calldata->fattr;
3848 	calldata->res.seqid = calldata->arg.seqid;
3849 	calldata->res.server = server;
3850 	calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3851 	calldata->lr.roc = pnfs_roc(state->inode,
3852 			&calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3853 	if (calldata->lr.roc) {
3854 		calldata->arg.lr_args = &calldata->lr.arg;
3855 		calldata->res.lr_res = &calldata->lr.res;
3856 	}
3857 	nfs_sb_active(calldata->inode->i_sb);
3858 
3859 	msg.rpc_argp = &calldata->arg;
3860 	msg.rpc_resp = &calldata->res;
3861 	task_setup_data.callback_data = calldata;
3862 	task = rpc_run_task(&task_setup_data);
3863 	if (IS_ERR(task))
3864 		return PTR_ERR(task);
3865 	status = 0;
3866 	if (wait)
3867 		status = rpc_wait_for_completion_task(task);
3868 	rpc_put_task(task);
3869 	return status;
3870 out_free_calldata:
3871 	kfree(calldata);
3872 out:
3873 	nfs4_put_open_state(state);
3874 	nfs4_put_state_owner(sp);
3875 	return status;
3876 }
3877 
3878 static struct inode *
nfs4_atomic_open(struct inode * dir,struct nfs_open_context * ctx,int open_flags,struct iattr * attr,int * opened)3879 nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3880 		int open_flags, struct iattr *attr, int *opened)
3881 {
3882 	struct nfs4_state *state;
3883 	struct nfs4_label l, *label;
3884 
3885 	label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
3886 
3887 	/* Protect against concurrent sillydeletes */
3888 	state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
3889 
3890 	nfs4_label_release_security(label);
3891 
3892 	if (IS_ERR(state))
3893 		return ERR_CAST(state);
3894 	return state->inode;
3895 }
3896 
nfs4_close_context(struct nfs_open_context * ctx,int is_sync)3897 static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
3898 {
3899 	if (ctx->state == NULL)
3900 		return;
3901 	if (is_sync)
3902 		nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
3903 	else
3904 		nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
3905 }
3906 
3907 #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3908 #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
3909 #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_OPEN_ARGUMENTS - 1UL)
3910 
3911 #define FATTR4_WORD2_NFS42_TIME_DELEG_MASK \
3912 	(FATTR4_WORD2_TIME_DELEG_MODIFY|FATTR4_WORD2_TIME_DELEG_ACCESS)
nfs4_server_delegtime_capable(struct nfs4_server_caps_res * res)3913 static bool nfs4_server_delegtime_capable(struct nfs4_server_caps_res *res)
3914 {
3915 	u32 share_access_want = res->open_caps.oa_share_access_want[0];
3916 	u32 attr_bitmask = res->attr_bitmask[2];
3917 
3918 	return (share_access_want & NFS4_SHARE_WANT_DELEG_TIMESTAMPS) &&
3919 	       ((attr_bitmask & FATTR4_WORD2_NFS42_TIME_DELEG_MASK) ==
3920 					FATTR4_WORD2_NFS42_TIME_DELEG_MASK);
3921 }
3922 
_nfs4_server_capabilities(struct nfs_server * server,struct nfs_fh * fhandle)3923 static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3924 {
3925 	u32 minorversion = server->nfs_client->cl_minorversion;
3926 	u32 bitmask[3] = {
3927 		[0] = FATTR4_WORD0_SUPPORTED_ATTRS,
3928 	};
3929 	struct nfs4_server_caps_arg args = {
3930 		.fhandle = fhandle,
3931 		.bitmask = bitmask,
3932 	};
3933 	struct nfs4_server_caps_res res = {};
3934 	struct rpc_message msg = {
3935 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
3936 		.rpc_argp = &args,
3937 		.rpc_resp = &res,
3938 	};
3939 	int status;
3940 	int i;
3941 
3942 	bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3943 		     FATTR4_WORD0_FH_EXPIRE_TYPE |
3944 		     FATTR4_WORD0_LINK_SUPPORT |
3945 		     FATTR4_WORD0_SYMLINK_SUPPORT |
3946 		     FATTR4_WORD0_ACLSUPPORT |
3947 		     FATTR4_WORD0_CASE_INSENSITIVE |
3948 		     FATTR4_WORD0_CASE_PRESERVING;
3949 	if (minorversion)
3950 		bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT |
3951 			     FATTR4_WORD2_OPEN_ARGUMENTS;
3952 
3953 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
3954 	if (status == 0) {
3955 		bitmask[0] = (FATTR4_WORD0_SUPPORTED_ATTRS |
3956 			      FATTR4_WORD0_FH_EXPIRE_TYPE |
3957 			      FATTR4_WORD0_LINK_SUPPORT |
3958 			      FATTR4_WORD0_SYMLINK_SUPPORT |
3959 			      FATTR4_WORD0_ACLSUPPORT |
3960 			      FATTR4_WORD0_CASE_INSENSITIVE |
3961 			      FATTR4_WORD0_CASE_PRESERVING) &
3962 			     res.attr_bitmask[0];
3963 		/* Sanity check the server answers */
3964 		switch (minorversion) {
3965 		case 0:
3966 			res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3967 			res.attr_bitmask[2] = 0;
3968 			break;
3969 		case 1:
3970 			res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3971 			bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT &
3972 				     res.attr_bitmask[2];
3973 			break;
3974 		case 2:
3975 			res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3976 			bitmask[2] = (FATTR4_WORD2_SUPPATTR_EXCLCREAT |
3977 				      FATTR4_WORD2_OPEN_ARGUMENTS) &
3978 				     res.attr_bitmask[2];
3979 		}
3980 		memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
3981 		server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS |
3982 				  NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL);
3983 		server->fattr_valid = NFS_ATTR_FATTR_V4;
3984 		if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3985 				res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3986 			server->caps |= NFS_CAP_ACLS;
3987 		if (res.has_links != 0)
3988 			server->caps |= NFS_CAP_HARDLINKS;
3989 		if (res.has_symlinks != 0)
3990 			server->caps |= NFS_CAP_SYMLINKS;
3991 		if (res.case_insensitive)
3992 			server->caps |= NFS_CAP_CASE_INSENSITIVE;
3993 		if (res.case_preserving)
3994 			server->caps |= NFS_CAP_CASE_PRESERVING;
3995 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
3996 		if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3997 			server->caps |= NFS_CAP_SECURITY_LABEL;
3998 #endif
3999 		if (res.attr_bitmask[0] & FATTR4_WORD0_FS_LOCATIONS)
4000 			server->caps |= NFS_CAP_FS_LOCATIONS;
4001 		if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID))
4002 			server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID;
4003 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE))
4004 			server->fattr_valid &= ~NFS_ATTR_FATTR_MODE;
4005 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS))
4006 			server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK;
4007 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER))
4008 			server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER |
4009 				NFS_ATTR_FATTR_OWNER_NAME);
4010 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP))
4011 			server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP |
4012 				NFS_ATTR_FATTR_GROUP_NAME);
4013 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED))
4014 			server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED;
4015 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS))
4016 			server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME;
4017 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA))
4018 			server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME;
4019 		if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY))
4020 			server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME;
4021 		memcpy(server->attr_bitmask_nl, res.attr_bitmask,
4022 				sizeof(server->attr_bitmask));
4023 		server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
4024 
4025 		if (res.open_caps.oa_share_access_want[0] &
4026 		    NFS4_SHARE_WANT_OPEN_XOR_DELEGATION)
4027 			server->caps |= NFS_CAP_OPEN_XOR;
4028 		if (nfs4_server_delegtime_capable(&res))
4029 			server->caps |= NFS_CAP_DELEGTIME;
4030 
4031 		memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
4032 		server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
4033 		server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
4034 		server->cache_consistency_bitmask[2] = 0;
4035 
4036 		/* Avoid a regression due to buggy server */
4037 		for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
4038 			res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
4039 		memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
4040 			sizeof(server->exclcreat_bitmask));
4041 
4042 		server->acl_bitmask = res.acl_bitmask;
4043 		server->fh_expire_type = res.fh_expire_type;
4044 	}
4045 
4046 	return status;
4047 }
4048 
nfs4_server_capabilities(struct nfs_server * server,struct nfs_fh * fhandle)4049 int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
4050 {
4051 	struct nfs4_exception exception = {
4052 		.interruptible = true,
4053 	};
4054 	int err;
4055 
4056 	nfs4_server_set_init_caps(server);
4057 	do {
4058 		err = nfs4_handle_exception(server,
4059 				_nfs4_server_capabilities(server, fhandle),
4060 				&exception);
4061 	} while (exception.retry);
4062 	return err;
4063 }
4064 
test_fs_location_for_trunking(struct nfs4_fs_location * location,struct nfs_client * clp,struct nfs_server * server)4065 static void test_fs_location_for_trunking(struct nfs4_fs_location *location,
4066 					  struct nfs_client *clp,
4067 					  struct nfs_server *server)
4068 {
4069 	int i;
4070 
4071 	for (i = 0; i < location->nservers; i++) {
4072 		struct nfs4_string *srv_loc = &location->servers[i];
4073 		struct sockaddr_storage addr;
4074 		size_t addrlen;
4075 		struct xprt_create xprt_args = {
4076 			.ident = 0,
4077 			.net = clp->cl_net,
4078 		};
4079 		struct nfs4_add_xprt_data xprtdata = {
4080 			.clp = clp,
4081 		};
4082 		struct rpc_add_xprt_test rpcdata = {
4083 			.add_xprt_test = clp->cl_mvops->session_trunk,
4084 			.data = &xprtdata,
4085 		};
4086 		char *servername = NULL;
4087 
4088 		if (!srv_loc->len)
4089 			continue;
4090 
4091 		addrlen = nfs_parse_server_name(srv_loc->data, srv_loc->len,
4092 						&addr, sizeof(addr),
4093 						clp->cl_net, server->port);
4094 		if (!addrlen)
4095 			return;
4096 		xprt_args.dstaddr = (struct sockaddr *)&addr;
4097 		xprt_args.addrlen = addrlen;
4098 		servername = kmalloc(srv_loc->len + 1, GFP_KERNEL);
4099 		if (!servername)
4100 			return;
4101 		memcpy(servername, srv_loc->data, srv_loc->len);
4102 		servername[srv_loc->len] = '\0';
4103 		xprt_args.servername = servername;
4104 
4105 		xprtdata.cred = nfs4_get_clid_cred(clp);
4106 		rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
4107 				  rpc_clnt_setup_test_and_add_xprt,
4108 				  &rpcdata);
4109 		if (xprtdata.cred)
4110 			put_cred(xprtdata.cred);
4111 		kfree(servername);
4112 	}
4113 }
4114 
_is_same_nfs4_pathname(struct nfs4_pathname * path1,struct nfs4_pathname * path2)4115 static bool _is_same_nfs4_pathname(struct nfs4_pathname *path1,
4116 				   struct nfs4_pathname *path2)
4117 {
4118 	int i;
4119 
4120 	if (path1->ncomponents != path2->ncomponents)
4121 		return false;
4122 	for (i = 0; i < path1->ncomponents; i++) {
4123 		if (path1->components[i].len != path2->components[i].len)
4124 			return false;
4125 		if (memcmp(path1->components[i].data, path2->components[i].data,
4126 				path1->components[i].len))
4127 			return false;
4128 	}
4129 	return true;
4130 }
4131 
_nfs4_discover_trunking(struct nfs_server * server,struct nfs_fh * fhandle)4132 static int _nfs4_discover_trunking(struct nfs_server *server,
4133 				   struct nfs_fh *fhandle)
4134 {
4135 	struct nfs4_fs_locations *locations = NULL;
4136 	struct page *page;
4137 	const struct cred *cred;
4138 	struct nfs_client *clp = server->nfs_client;
4139 	const struct nfs4_state_maintenance_ops *ops =
4140 		clp->cl_mvops->state_renewal_ops;
4141 	int status = -ENOMEM, i;
4142 
4143 	cred = ops->get_state_renewal_cred(clp);
4144 	if (cred == NULL) {
4145 		cred = nfs4_get_clid_cred(clp);
4146 		if (cred == NULL)
4147 			return -ENOKEY;
4148 	}
4149 
4150 	page = alloc_page(GFP_KERNEL);
4151 	if (!page)
4152 		goto out_put_cred;
4153 	locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4154 	if (!locations)
4155 		goto out_free;
4156 	locations->fattr = nfs_alloc_fattr();
4157 	if (!locations->fattr)
4158 		goto out_free_2;
4159 
4160 	status = nfs4_proc_get_locations(server, fhandle, locations, page,
4161 					 cred);
4162 	if (status)
4163 		goto out_free_3;
4164 
4165 	for (i = 0; i < locations->nlocations; i++) {
4166 		if (!_is_same_nfs4_pathname(&locations->fs_path,
4167 					&locations->locations[i].rootpath))
4168 			continue;
4169 		test_fs_location_for_trunking(&locations->locations[i], clp,
4170 					      server);
4171 	}
4172 out_free_3:
4173 	kfree(locations->fattr);
4174 out_free_2:
4175 	kfree(locations);
4176 out_free:
4177 	__free_page(page);
4178 out_put_cred:
4179 	put_cred(cred);
4180 	return status;
4181 }
4182 
nfs4_discover_trunking(struct nfs_server * server,struct nfs_fh * fhandle)4183 static int nfs4_discover_trunking(struct nfs_server *server,
4184 				  struct nfs_fh *fhandle)
4185 {
4186 	struct nfs4_exception exception = {
4187 		.interruptible = true,
4188 	};
4189 	struct nfs_client *clp = server->nfs_client;
4190 	int err = 0;
4191 
4192 	if (!nfs4_has_session(clp))
4193 		goto out;
4194 	do {
4195 		err = nfs4_handle_exception(server,
4196 				_nfs4_discover_trunking(server, fhandle),
4197 				&exception);
4198 	} while (exception.retry);
4199 out:
4200 	return err;
4201 }
4202 
_nfs4_lookup_root(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * info)4203 static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4204 		struct nfs_fsinfo *info)
4205 {
4206 	u32 bitmask[3];
4207 	struct nfs4_lookup_root_arg args = {
4208 		.bitmask = bitmask,
4209 	};
4210 	struct nfs4_lookup_res res = {
4211 		.server = server,
4212 		.fattr = info->fattr,
4213 		.fh = fhandle,
4214 	};
4215 	struct rpc_message msg = {
4216 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
4217 		.rpc_argp = &args,
4218 		.rpc_resp = &res,
4219 	};
4220 
4221 	bitmask[0] = nfs4_fattr_bitmap[0];
4222 	bitmask[1] = nfs4_fattr_bitmap[1];
4223 	/*
4224 	 * Process the label in the upcoming getfattr
4225 	 */
4226 	bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
4227 
4228 	nfs_fattr_init(info->fattr);
4229 	return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4230 }
4231 
nfs4_lookup_root(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * info)4232 static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
4233 		struct nfs_fsinfo *info)
4234 {
4235 	struct nfs4_exception exception = {
4236 		.interruptible = true,
4237 	};
4238 	int err;
4239 	do {
4240 		err = _nfs4_lookup_root(server, fhandle, info);
4241 		trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
4242 		switch (err) {
4243 		case 0:
4244 		case -NFS4ERR_WRONGSEC:
4245 			goto out;
4246 		default:
4247 			err = nfs4_handle_exception(server, err, &exception);
4248 		}
4249 	} while (exception.retry);
4250 out:
4251 	return err;
4252 }
4253 
nfs4_lookup_root_sec(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * info,rpc_authflavor_t flavor)4254 static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4255 				struct nfs_fsinfo *info, rpc_authflavor_t flavor)
4256 {
4257 	struct rpc_auth_create_args auth_args = {
4258 		.pseudoflavor = flavor,
4259 	};
4260 	struct rpc_auth *auth;
4261 
4262 	auth = rpcauth_create(&auth_args, server->client);
4263 	if (IS_ERR(auth))
4264 		return -EACCES;
4265 	return nfs4_lookup_root(server, fhandle, info);
4266 }
4267 
4268 /*
4269  * Retry pseudoroot lookup with various security flavors.  We do this when:
4270  *
4271  *   NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
4272  *   NFSv4.1: the server does not support the SECINFO_NO_NAME operation
4273  *
4274  * Returns zero on success, or a negative NFS4ERR value, or a
4275  * negative errno value.
4276  */
nfs4_find_root_sec(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * info)4277 static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
4278 			      struct nfs_fsinfo *info)
4279 {
4280 	/* Per 3530bis 15.33.5 */
4281 	static const rpc_authflavor_t flav_array[] = {
4282 		RPC_AUTH_GSS_KRB5P,
4283 		RPC_AUTH_GSS_KRB5I,
4284 		RPC_AUTH_GSS_KRB5,
4285 		RPC_AUTH_UNIX,			/* courtesy */
4286 		RPC_AUTH_NULL,
4287 	};
4288 	int status = -EPERM;
4289 	size_t i;
4290 
4291 	if (server->auth_info.flavor_len > 0) {
4292 		/* try each flavor specified by user */
4293 		for (i = 0; i < server->auth_info.flavor_len; i++) {
4294 			status = nfs4_lookup_root_sec(server, fhandle, info,
4295 						server->auth_info.flavors[i]);
4296 			if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4297 				continue;
4298 			break;
4299 		}
4300 	} else {
4301 		/* no flavors specified by user, try default list */
4302 		for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
4303 			status = nfs4_lookup_root_sec(server, fhandle, info,
4304 						      flav_array[i]);
4305 			if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
4306 				continue;
4307 			break;
4308 		}
4309 	}
4310 
4311 	/*
4312 	 * -EACCES could mean that the user doesn't have correct permissions
4313 	 * to access the mount.  It could also mean that we tried to mount
4314 	 * with a gss auth flavor, but rpc.gssd isn't running.  Either way,
4315 	 * existing mount programs don't handle -EACCES very well so it should
4316 	 * be mapped to -EPERM instead.
4317 	 */
4318 	if (status == -EACCES)
4319 		status = -EPERM;
4320 	return status;
4321 }
4322 
4323 /**
4324  * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4325  * @server: initialized nfs_server handle
4326  * @fhandle: we fill in the pseudo-fs root file handle
4327  * @info: we fill in an FSINFO struct
4328  * @auth_probe: probe the auth flavours
4329  *
4330  * Returns zero on success, or a negative errno.
4331  */
nfs4_proc_get_rootfh(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * info,bool auth_probe)4332 int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
4333 			 struct nfs_fsinfo *info,
4334 			 bool auth_probe)
4335 {
4336 	int status = 0;
4337 
4338 	if (!auth_probe)
4339 		status = nfs4_lookup_root(server, fhandle, info);
4340 
4341 	if (auth_probe || status == NFS4ERR_WRONGSEC)
4342 		status = server->nfs_client->cl_mvops->find_root_sec(server,
4343 				fhandle, info);
4344 
4345 	if (status == 0)
4346 		status = nfs4_server_capabilities(server, fhandle);
4347 	if (status == 0)
4348 		status = nfs4_do_fsinfo(server, fhandle, info);
4349 
4350 	return nfs4_map_errors(status);
4351 }
4352 
nfs4_proc_get_root(struct nfs_server * server,struct nfs_fh * mntfh,struct nfs_fsinfo * info)4353 static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4354 			      struct nfs_fsinfo *info)
4355 {
4356 	int error;
4357 	struct nfs_fattr *fattr = info->fattr;
4358 
4359 	error = nfs4_server_capabilities(server, mntfh);
4360 	if (error < 0) {
4361 		dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4362 		return error;
4363 	}
4364 
4365 	error = nfs4_proc_getattr(server, mntfh, fattr, NULL);
4366 	if (error < 0) {
4367 		dprintk("nfs4_get_root: getattr error = %d\n", -error);
4368 		goto out;
4369 	}
4370 
4371 	if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4372 	    !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4373 		memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4374 
4375 out:
4376 	return error;
4377 }
4378 
4379 /*
4380  * Get locations and (maybe) other attributes of a referral.
4381  * Note that we'll actually follow the referral later when
4382  * we detect fsid mismatch in inode revalidation
4383  */
nfs4_get_referral(struct rpc_clnt * client,struct inode * dir,const struct qstr * name,struct nfs_fattr * fattr,struct nfs_fh * fhandle)4384 static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4385 			     const struct qstr *name, struct nfs_fattr *fattr,
4386 			     struct nfs_fh *fhandle)
4387 {
4388 	int status = -ENOMEM;
4389 	struct page *page = NULL;
4390 	struct nfs4_fs_locations *locations = NULL;
4391 
4392 	page = alloc_page(GFP_KERNEL);
4393 	if (page == NULL)
4394 		goto out;
4395 	locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4396 	if (locations == NULL)
4397 		goto out;
4398 
4399 	locations->fattr = fattr;
4400 
4401 	status = nfs4_proc_fs_locations(client, dir, name, locations, page);
4402 	if (status != 0)
4403 		goto out;
4404 
4405 	/*
4406 	 * If the fsid didn't change, this is a migration event, not a
4407 	 * referral.  Cause us to drop into the exception handler, which
4408 	 * will kick off migration recovery.
4409 	 */
4410 	if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &fattr->fsid)) {
4411 		dprintk("%s: server did not return a different fsid for"
4412 			" a referral at %s\n", __func__, name->name);
4413 		status = -NFS4ERR_MOVED;
4414 		goto out;
4415 	}
4416 	/* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4417 	nfs_fixup_referral_attributes(fattr);
4418 	memset(fhandle, 0, sizeof(struct nfs_fh));
4419 out:
4420 	if (page)
4421 		__free_page(page);
4422 	kfree(locations);
4423 	return status;
4424 }
4425 
_nfs4_proc_getattr(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fattr * fattr,struct inode * inode)4426 static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4427 				struct nfs_fattr *fattr, struct inode *inode)
4428 {
4429 	__u32 bitmask[NFS4_BITMASK_SZ];
4430 	struct nfs4_getattr_arg args = {
4431 		.fh = fhandle,
4432 		.bitmask = bitmask,
4433 	};
4434 	struct nfs4_getattr_res res = {
4435 		.fattr = fattr,
4436 		.server = server,
4437 	};
4438 	struct rpc_message msg = {
4439 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4440 		.rpc_argp = &args,
4441 		.rpc_resp = &res,
4442 	};
4443 	unsigned short task_flags = 0;
4444 
4445 	if (nfs4_has_session(server->nfs_client))
4446 		task_flags = RPC_TASK_MOVEABLE;
4447 
4448 	/* Is this is an attribute revalidation, subject to softreval? */
4449 	if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4450 		task_flags |= RPC_TASK_TIMEOUT;
4451 
4452 	nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label), inode, 0);
4453 	nfs_fattr_init(fattr);
4454 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4455 	return nfs4_do_call_sync(server->client, server, &msg,
4456 			&args.seq_args, &res.seq_res, task_flags);
4457 }
4458 
nfs4_proc_getattr(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fattr * fattr,struct inode * inode)4459 int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
4460 				struct nfs_fattr *fattr, struct inode *inode)
4461 {
4462 	struct nfs4_exception exception = {
4463 		.interruptible = true,
4464 	};
4465 	int err;
4466 	do {
4467 		err = _nfs4_proc_getattr(server, fhandle, fattr, inode);
4468 		trace_nfs4_getattr(server, fhandle, fattr, err);
4469 		err = nfs4_handle_exception(server, err,
4470 				&exception);
4471 	} while (exception.retry);
4472 	return err;
4473 }
4474 
4475 /*
4476  * The file is not closed if it is opened due to the a request to change
4477  * the size of the file. The open call will not be needed once the
4478  * VFS layer lookup-intents are implemented.
4479  *
4480  * Close is called when the inode is destroyed.
4481  * If we haven't opened the file for O_WRONLY, we
4482  * need to in the size_change case to obtain a stateid.
4483  *
4484  * Got race?
4485  * Because OPEN is always done by name in nfsv4, it is
4486  * possible that we opened a different file by the same
4487  * name.  We can recognize this race condition, but we
4488  * can't do anything about it besides returning an error.
4489  *
4490  * This will be fixed with VFS changes (lookup-intent).
4491  */
4492 static int
nfs4_proc_setattr(struct dentry * dentry,struct nfs_fattr * fattr,struct iattr * sattr)4493 nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4494 		  struct iattr *sattr)
4495 {
4496 	struct inode *inode = d_inode(dentry);
4497 	const struct cred *cred = NULL;
4498 	struct nfs_open_context *ctx = NULL;
4499 	int status;
4500 
4501 	if (pnfs_ld_layoutret_on_setattr(inode) &&
4502 	    sattr->ia_valid & ATTR_SIZE &&
4503 	    sattr->ia_size < i_size_read(inode))
4504 		pnfs_commit_and_return_layout(inode);
4505 
4506 	nfs_fattr_init(fattr);
4507 
4508 	/* Deal with open(O_TRUNC) */
4509 	if (sattr->ia_valid & ATTR_OPEN)
4510 		sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
4511 
4512 	/* Optimization: if the end result is no change, don't RPC */
4513 	if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
4514 		return 0;
4515 
4516 	/* Search for an existing open(O_WRITE) file */
4517 	if (sattr->ia_valid & ATTR_FILE) {
4518 
4519 		ctx = nfs_file_open_context(sattr->ia_file);
4520 		if (ctx)
4521 			cred = ctx->cred;
4522 	}
4523 
4524 	/* Return any delegations if we're going to change ACLs */
4525 	if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
4526 		nfs4_inode_make_writeable(inode);
4527 
4528 	status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL);
4529 	if (status == 0) {
4530 		nfs_setattr_update_inode(inode, sattr, fattr);
4531 		nfs_setsecurity(inode, fattr);
4532 	}
4533 	return status;
4534 }
4535 
_nfs4_proc_lookup(struct rpc_clnt * clnt,struct inode * dir,struct dentry * dentry,struct nfs_fh * fhandle,struct nfs_fattr * fattr)4536 static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4537 		struct dentry *dentry, struct nfs_fh *fhandle,
4538 		struct nfs_fattr *fattr)
4539 {
4540 	struct nfs_server *server = NFS_SERVER(dir);
4541 	int		       status;
4542 	struct nfs4_lookup_arg args = {
4543 		.bitmask = server->attr_bitmask,
4544 		.dir_fh = NFS_FH(dir),
4545 		.name = &dentry->d_name,
4546 	};
4547 	struct nfs4_lookup_res res = {
4548 		.server = server,
4549 		.fattr = fattr,
4550 		.fh = fhandle,
4551 	};
4552 	struct rpc_message msg = {
4553 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4554 		.rpc_argp = &args,
4555 		.rpc_resp = &res,
4556 	};
4557 	unsigned short task_flags = 0;
4558 
4559 	if (nfs_server_capable(dir, NFS_CAP_MOVEABLE))
4560 		task_flags = RPC_TASK_MOVEABLE;
4561 
4562 	/* Is this is an attribute revalidation, subject to softreval? */
4563 	if (nfs_lookup_is_soft_revalidate(dentry))
4564 		task_flags |= RPC_TASK_TIMEOUT;
4565 
4566 	args.bitmask = nfs4_bitmask(server, fattr->label);
4567 
4568 	nfs_fattr_init(fattr);
4569 
4570 	dprintk("NFS call  lookup %pd2\n", dentry);
4571 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4572 	status = nfs4_do_call_sync(clnt, server, &msg,
4573 			&args.seq_args, &res.seq_res, task_flags);
4574 	dprintk("NFS reply lookup: %d\n", status);
4575 	return status;
4576 }
4577 
nfs_fixup_secinfo_attributes(struct nfs_fattr * fattr)4578 static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
4579 {
4580 	fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4581 		NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
4582 	fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4583 	fattr->nlink = 2;
4584 }
4585 
nfs4_proc_lookup_common(struct rpc_clnt ** clnt,struct inode * dir,struct dentry * dentry,struct nfs_fh * fhandle,struct nfs_fattr * fattr)4586 static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
4587 				   struct dentry *dentry, struct nfs_fh *fhandle,
4588 				   struct nfs_fattr *fattr)
4589 {
4590 	struct nfs4_exception exception = {
4591 		.interruptible = true,
4592 	};
4593 	struct rpc_clnt *client = *clnt;
4594 	const struct qstr *name = &dentry->d_name;
4595 	int err;
4596 	do {
4597 		err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr);
4598 		trace_nfs4_lookup(dir, name, err);
4599 		switch (err) {
4600 		case -NFS4ERR_BADNAME:
4601 			err = -ENOENT;
4602 			goto out;
4603 		case -NFS4ERR_MOVED:
4604 			err = nfs4_get_referral(client, dir, name, fattr, fhandle);
4605 			if (err == -NFS4ERR_MOVED)
4606 				err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4607 			goto out;
4608 		case -NFS4ERR_WRONGSEC:
4609 			err = -EPERM;
4610 			if (client != *clnt)
4611 				goto out;
4612 			client = nfs4_negotiate_security(client, dir, name);
4613 			if (IS_ERR(client))
4614 				return PTR_ERR(client);
4615 
4616 			exception.retry = 1;
4617 			break;
4618 		default:
4619 			err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4620 		}
4621 	} while (exception.retry);
4622 
4623 out:
4624 	if (err == 0)
4625 		*clnt = client;
4626 	else if (client != *clnt)
4627 		rpc_shutdown_client(client);
4628 
4629 	return err;
4630 }
4631 
nfs4_proc_lookup(struct inode * dir,struct dentry * dentry,struct nfs_fh * fhandle,struct nfs_fattr * fattr)4632 static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
4633 			    struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4634 {
4635 	int status;
4636 	struct rpc_clnt *client = NFS_CLIENT(dir);
4637 
4638 	status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4639 	if (client != NFS_CLIENT(dir)) {
4640 		rpc_shutdown_client(client);
4641 		nfs_fixup_secinfo_attributes(fattr);
4642 	}
4643 	return status;
4644 }
4645 
4646 struct rpc_clnt *
nfs4_proc_lookup_mountpoint(struct inode * dir,struct dentry * dentry,struct nfs_fh * fhandle,struct nfs_fattr * fattr)4647 nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
4648 			    struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4649 {
4650 	struct rpc_clnt *client = NFS_CLIENT(dir);
4651 	int status;
4652 
4653 	status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr);
4654 	if (status < 0)
4655 		return ERR_PTR(status);
4656 	return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
4657 }
4658 
_nfs4_proc_lookupp(struct inode * inode,struct nfs_fh * fhandle,struct nfs_fattr * fattr)4659 static int _nfs4_proc_lookupp(struct inode *inode,
4660 		struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4661 {
4662 	struct rpc_clnt *clnt = NFS_CLIENT(inode);
4663 	struct nfs_server *server = NFS_SERVER(inode);
4664 	int		       status;
4665 	struct nfs4_lookupp_arg args = {
4666 		.bitmask = server->attr_bitmask,
4667 		.fh = NFS_FH(inode),
4668 	};
4669 	struct nfs4_lookupp_res res = {
4670 		.server = server,
4671 		.fattr = fattr,
4672 		.fh = fhandle,
4673 	};
4674 	struct rpc_message msg = {
4675 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4676 		.rpc_argp = &args,
4677 		.rpc_resp = &res,
4678 	};
4679 	unsigned short task_flags = 0;
4680 
4681 	if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL)
4682 		task_flags |= RPC_TASK_TIMEOUT;
4683 
4684 	args.bitmask = nfs4_bitmask(server, fattr->label);
4685 
4686 	nfs_fattr_init(fattr);
4687 
4688 	dprintk("NFS call  lookupp ino=0x%lx\n", inode->i_ino);
4689 	status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4690 				&res.seq_res, task_flags);
4691 	dprintk("NFS reply lookupp: %d\n", status);
4692 	return status;
4693 }
4694 
nfs4_proc_lookupp(struct inode * inode,struct nfs_fh * fhandle,struct nfs_fattr * fattr)4695 static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4696 			     struct nfs_fattr *fattr)
4697 {
4698 	struct nfs4_exception exception = {
4699 		.interruptible = true,
4700 	};
4701 	int err;
4702 	do {
4703 		err = _nfs4_proc_lookupp(inode, fhandle, fattr);
4704 		trace_nfs4_lookupp(inode, err);
4705 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
4706 				&exception);
4707 	} while (exception.retry);
4708 	return err;
4709 }
4710 
_nfs4_proc_access(struct inode * inode,struct nfs_access_entry * entry,const struct cred * cred)4711 static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4712 			     const struct cred *cred)
4713 {
4714 	struct nfs_server *server = NFS_SERVER(inode);
4715 	struct nfs4_accessargs args = {
4716 		.fh = NFS_FH(inode),
4717 		.access = entry->mask,
4718 	};
4719 	struct nfs4_accessres res = {
4720 		.server = server,
4721 	};
4722 	struct rpc_message msg = {
4723 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4724 		.rpc_argp = &args,
4725 		.rpc_resp = &res,
4726 		.rpc_cred = cred,
4727 	};
4728 	int status = 0;
4729 
4730 	if (!nfs4_have_delegation(inode, FMODE_READ, 0)) {
4731 		res.fattr = nfs_alloc_fattr();
4732 		if (res.fattr == NULL)
4733 			return -ENOMEM;
4734 		args.bitmask = server->cache_consistency_bitmask;
4735 	}
4736 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
4737 	if (!status) {
4738 		nfs_access_set_mask(entry, res.access);
4739 		if (res.fattr)
4740 			nfs_refresh_inode(inode, res.fattr);
4741 	}
4742 	nfs_free_fattr(res.fattr);
4743 	return status;
4744 }
4745 
nfs4_proc_access(struct inode * inode,struct nfs_access_entry * entry,const struct cred * cred)4746 static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry,
4747 			    const struct cred *cred)
4748 {
4749 	struct nfs4_exception exception = {
4750 		.interruptible = true,
4751 	};
4752 	int err;
4753 	do {
4754 		err = _nfs4_proc_access(inode, entry, cred);
4755 		trace_nfs4_access(inode, err);
4756 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
4757 				&exception);
4758 	} while (exception.retry);
4759 	return err;
4760 }
4761 
4762 /*
4763  * TODO: For the time being, we don't try to get any attributes
4764  * along with any of the zero-copy operations READ, READDIR,
4765  * READLINK, WRITE.
4766  *
4767  * In the case of the first three, we want to put the GETATTR
4768  * after the read-type operation -- this is because it is hard
4769  * to predict the length of a GETATTR response in v4, and thus
4770  * align the READ data correctly.  This means that the GETATTR
4771  * may end up partially falling into the page cache, and we should
4772  * shift it into the 'tail' of the xdr_buf before processing.
4773  * To do this efficiently, we need to know the total length
4774  * of data received, which doesn't seem to be available outside
4775  * of the RPC layer.
4776  *
4777  * In the case of WRITE, we also want to put the GETATTR after
4778  * the operation -- in this case because we want to make sure
4779  * we get the post-operation mtime and size.
4780  *
4781  * Both of these changes to the XDR layer would in fact be quite
4782  * minor, but I decided to leave them for a subsequent patch.
4783  */
_nfs4_proc_readlink(struct inode * inode,struct page * page,unsigned int pgbase,unsigned int pglen)4784 static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4785 		unsigned int pgbase, unsigned int pglen)
4786 {
4787 	struct nfs4_readlink args = {
4788 		.fh       = NFS_FH(inode),
4789 		.pgbase	  = pgbase,
4790 		.pglen    = pglen,
4791 		.pages    = &page,
4792 	};
4793 	struct nfs4_readlink_res res;
4794 	struct rpc_message msg = {
4795 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4796 		.rpc_argp = &args,
4797 		.rpc_resp = &res,
4798 	};
4799 
4800 	return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
4801 }
4802 
nfs4_proc_readlink(struct inode * inode,struct page * page,unsigned int pgbase,unsigned int pglen)4803 static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4804 		unsigned int pgbase, unsigned int pglen)
4805 {
4806 	struct nfs4_exception exception = {
4807 		.interruptible = true,
4808 	};
4809 	int err;
4810 	do {
4811 		err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4812 		trace_nfs4_readlink(inode, err);
4813 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
4814 				&exception);
4815 	} while (exception.retry);
4816 	return err;
4817 }
4818 
4819 /*
4820  * This is just for mknod.  open(O_CREAT) will always do ->open_context().
4821  */
4822 static int
nfs4_proc_create(struct inode * dir,struct dentry * dentry,struct iattr * sattr,int flags)4823 nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
4824 		 int flags)
4825 {
4826 	struct nfs_server *server = NFS_SERVER(dir);
4827 	struct nfs4_label l, *ilabel;
4828 	struct nfs_open_context *ctx;
4829 	struct nfs4_state *state;
4830 	int status = 0;
4831 
4832 	ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
4833 	if (IS_ERR(ctx))
4834 		return PTR_ERR(ctx);
4835 
4836 	ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4837 
4838 	if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4839 		sattr->ia_mode &= ~current_umask();
4840 	state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
4841 	if (IS_ERR(state)) {
4842 		status = PTR_ERR(state);
4843 		goto out;
4844 	}
4845 out:
4846 	nfs4_label_release_security(ilabel);
4847 	put_nfs_open_context(ctx);
4848 	return status;
4849 }
4850 
4851 static int
_nfs4_proc_remove(struct inode * dir,const struct qstr * name,u32 ftype)4852 _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
4853 {
4854 	struct nfs_server *server = NFS_SERVER(dir);
4855 	struct nfs_removeargs args = {
4856 		.fh = NFS_FH(dir),
4857 		.name = *name,
4858 	};
4859 	struct nfs_removeres res = {
4860 		.server = server,
4861 	};
4862 	struct rpc_message msg = {
4863 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4864 		.rpc_argp = &args,
4865 		.rpc_resp = &res,
4866 	};
4867 	unsigned long timestamp = jiffies;
4868 	int status;
4869 
4870 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
4871 	if (status == 0) {
4872 		spin_lock(&dir->i_lock);
4873 		/* Removing a directory decrements nlink in the parent */
4874 		if (ftype == NF4DIR && dir->i_nlink > 2)
4875 			nfs4_dec_nlink_locked(dir);
4876 		nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4877 					      NFS_INO_INVALID_DATA);
4878 		spin_unlock(&dir->i_lock);
4879 	}
4880 	return status;
4881 }
4882 
nfs4_proc_remove(struct inode * dir,struct dentry * dentry)4883 static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4884 {
4885 	struct nfs4_exception exception = {
4886 		.interruptible = true,
4887 	};
4888 	struct inode *inode = d_inode(dentry);
4889 	int err;
4890 
4891 	if (inode) {
4892 		if (inode->i_nlink == 1)
4893 			nfs4_inode_return_delegation(inode);
4894 		else
4895 			nfs4_inode_make_writeable(inode);
4896 	}
4897 	do {
4898 		err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
4899 		trace_nfs4_remove(dir, &dentry->d_name, err);
4900 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
4901 				&exception);
4902 	} while (exception.retry);
4903 	return err;
4904 }
4905 
nfs4_proc_rmdir(struct inode * dir,const struct qstr * name)4906 static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
4907 {
4908 	struct nfs4_exception exception = {
4909 		.interruptible = true,
4910 	};
4911 	int err;
4912 
4913 	do {
4914 		err = _nfs4_proc_remove(dir, name, NF4DIR);
4915 		trace_nfs4_remove(dir, name, err);
4916 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
4917 				&exception);
4918 	} while (exception.retry);
4919 	return err;
4920 }
4921 
nfs4_proc_unlink_setup(struct rpc_message * msg,struct dentry * dentry,struct inode * inode)4922 static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4923 		struct dentry *dentry,
4924 		struct inode *inode)
4925 {
4926 	struct nfs_removeargs *args = msg->rpc_argp;
4927 	struct nfs_removeres *res = msg->rpc_resp;
4928 
4929 	res->server = NFS_SB(dentry->d_sb);
4930 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
4931 	nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
4932 
4933 	nfs_fattr_init(res->dir_attr);
4934 
4935 	if (inode) {
4936 		nfs4_inode_return_delegation(inode);
4937 		nfs_d_prune_case_insensitive_aliases(inode);
4938 	}
4939 }
4940 
nfs4_proc_unlink_rpc_prepare(struct rpc_task * task,struct nfs_unlinkdata * data)4941 static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4942 {
4943 	nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
4944 			&data->args.seq_args,
4945 			&data->res.seq_res,
4946 			task);
4947 }
4948 
nfs4_proc_unlink_done(struct rpc_task * task,struct inode * dir)4949 static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
4950 {
4951 	struct nfs_unlinkdata *data = task->tk_calldata;
4952 	struct nfs_removeres *res = &data->res;
4953 
4954 	if (!nfs4_sequence_done(task, &res->seq_res))
4955 		return 0;
4956 	if (nfs4_async_handle_error(task, res->server, NULL,
4957 				    &data->timeout) == -EAGAIN)
4958 		return 0;
4959 	if (task->tk_status == 0)
4960 		nfs4_update_changeattr(dir, &res->cinfo,
4961 				res->dir_attr->time_start,
4962 				NFS_INO_INVALID_DATA);
4963 	return 1;
4964 }
4965 
nfs4_proc_rename_setup(struct rpc_message * msg,struct dentry * old_dentry,struct dentry * new_dentry)4966 static void nfs4_proc_rename_setup(struct rpc_message *msg,
4967 		struct dentry *old_dentry,
4968 		struct dentry *new_dentry)
4969 {
4970 	struct nfs_renameargs *arg = msg->rpc_argp;
4971 	struct nfs_renameres *res = msg->rpc_resp;
4972 	struct inode *old_inode = d_inode(old_dentry);
4973 	struct inode *new_inode = d_inode(new_dentry);
4974 
4975 	if (old_inode)
4976 		nfs4_inode_make_writeable(old_inode);
4977 	if (new_inode)
4978 		nfs4_inode_return_delegation(new_inode);
4979 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
4980 	res->server = NFS_SB(old_dentry->d_sb);
4981 	nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
4982 }
4983 
nfs4_proc_rename_rpc_prepare(struct rpc_task * task,struct nfs_renamedata * data)4984 static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4985 {
4986 	nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
4987 			&data->args.seq_args,
4988 			&data->res.seq_res,
4989 			task);
4990 }
4991 
nfs4_proc_rename_done(struct rpc_task * task,struct inode * old_dir,struct inode * new_dir)4992 static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4993 				 struct inode *new_dir)
4994 {
4995 	struct nfs_renamedata *data = task->tk_calldata;
4996 	struct nfs_renameres *res = &data->res;
4997 
4998 	if (!nfs4_sequence_done(task, &res->seq_res))
4999 		return 0;
5000 	if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
5001 		return 0;
5002 
5003 	if (task->tk_status == 0) {
5004 		nfs_d_prune_case_insensitive_aliases(d_inode(data->old_dentry));
5005 		if (new_dir != old_dir) {
5006 			/* Note: If we moved a directory, nlink will change */
5007 			nfs4_update_changeattr(old_dir, &res->old_cinfo,
5008 					res->old_fattr->time_start,
5009 					NFS_INO_INVALID_NLINK |
5010 					    NFS_INO_INVALID_DATA);
5011 			nfs4_update_changeattr(new_dir, &res->new_cinfo,
5012 					res->new_fattr->time_start,
5013 					NFS_INO_INVALID_NLINK |
5014 					    NFS_INO_INVALID_DATA);
5015 		} else
5016 			nfs4_update_changeattr(old_dir, &res->old_cinfo,
5017 					res->old_fattr->time_start,
5018 					NFS_INO_INVALID_DATA);
5019 	}
5020 	return 1;
5021 }
5022 
_nfs4_proc_link(struct inode * inode,struct inode * dir,const struct qstr * name)5023 static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
5024 {
5025 	struct nfs_server *server = NFS_SERVER(inode);
5026 	__u32 bitmask[NFS4_BITMASK_SZ];
5027 	struct nfs4_link_arg arg = {
5028 		.fh     = NFS_FH(inode),
5029 		.dir_fh = NFS_FH(dir),
5030 		.name   = name,
5031 		.bitmask = bitmask,
5032 	};
5033 	struct nfs4_link_res res = {
5034 		.server = server,
5035 	};
5036 	struct rpc_message msg = {
5037 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
5038 		.rpc_argp = &arg,
5039 		.rpc_resp = &res,
5040 	};
5041 	int status = -ENOMEM;
5042 
5043 	res.fattr = nfs_alloc_fattr_with_label(server);
5044 	if (res.fattr == NULL)
5045 		goto out;
5046 
5047 	nfs4_inode_make_writeable(inode);
5048 	nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.fattr->label),
5049 				inode,
5050 				NFS_INO_INVALID_CHANGE | NFS_INO_INVALID_CTIME);
5051 	status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
5052 	if (!status) {
5053 		nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
5054 				       NFS_INO_INVALID_DATA);
5055 		nfs4_inc_nlink(inode);
5056 		status = nfs_post_op_update_inode(inode, res.fattr);
5057 		if (!status)
5058 			nfs_setsecurity(inode, res.fattr);
5059 	}
5060 
5061 out:
5062 	nfs_free_fattr(res.fattr);
5063 	return status;
5064 }
5065 
nfs4_proc_link(struct inode * inode,struct inode * dir,const struct qstr * name)5066 static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
5067 {
5068 	struct nfs4_exception exception = {
5069 		.interruptible = true,
5070 	};
5071 	int err;
5072 	do {
5073 		err = nfs4_handle_exception(NFS_SERVER(inode),
5074 				_nfs4_proc_link(inode, dir, name),
5075 				&exception);
5076 	} while (exception.retry);
5077 	return err;
5078 }
5079 
5080 struct nfs4_createdata {
5081 	struct rpc_message msg;
5082 	struct nfs4_create_arg arg;
5083 	struct nfs4_create_res res;
5084 	struct nfs_fh fh;
5085 	struct nfs_fattr fattr;
5086 };
5087 
nfs4_alloc_createdata(struct inode * dir,const struct qstr * name,struct iattr * sattr,u32 ftype)5088 static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
5089 		const struct qstr *name, struct iattr *sattr, u32 ftype)
5090 {
5091 	struct nfs4_createdata *data;
5092 
5093 	data = kzalloc(sizeof(*data), GFP_KERNEL);
5094 	if (data != NULL) {
5095 		struct nfs_server *server = NFS_SERVER(dir);
5096 
5097 		data->fattr.label = nfs4_label_alloc(server, GFP_KERNEL);
5098 		if (IS_ERR(data->fattr.label))
5099 			goto out_free;
5100 
5101 		data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
5102 		data->msg.rpc_argp = &data->arg;
5103 		data->msg.rpc_resp = &data->res;
5104 		data->arg.dir_fh = NFS_FH(dir);
5105 		data->arg.server = server;
5106 		data->arg.name = name;
5107 		data->arg.attrs = sattr;
5108 		data->arg.ftype = ftype;
5109 		data->arg.bitmask = nfs4_bitmask(server, data->fattr.label);
5110 		data->arg.umask = current_umask();
5111 		data->res.server = server;
5112 		data->res.fh = &data->fh;
5113 		data->res.fattr = &data->fattr;
5114 		nfs_fattr_init(data->res.fattr);
5115 	}
5116 	return data;
5117 out_free:
5118 	kfree(data);
5119 	return NULL;
5120 }
5121 
nfs4_do_create(struct inode * dir,struct dentry * dentry,struct nfs4_createdata * data)5122 static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
5123 {
5124 	int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
5125 				    &data->arg.seq_args, &data->res.seq_res, 1);
5126 	if (status == 0) {
5127 		spin_lock(&dir->i_lock);
5128 		/* Creating a directory bumps nlink in the parent */
5129 		if (data->arg.ftype == NF4DIR)
5130 			nfs4_inc_nlink_locked(dir);
5131 		nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
5132 					      data->res.fattr->time_start,
5133 					      NFS_INO_INVALID_DATA);
5134 		spin_unlock(&dir->i_lock);
5135 		status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
5136 	}
5137 	return status;
5138 }
5139 
nfs4_free_createdata(struct nfs4_createdata * data)5140 static void nfs4_free_createdata(struct nfs4_createdata *data)
5141 {
5142 	nfs4_label_free(data->fattr.label);
5143 	kfree(data);
5144 }
5145 
_nfs4_proc_symlink(struct inode * dir,struct dentry * dentry,struct folio * folio,unsigned int len,struct iattr * sattr,struct nfs4_label * label)5146 static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5147 		struct folio *folio, unsigned int len, struct iattr *sattr,
5148 		struct nfs4_label *label)
5149 {
5150 	struct page *page = &folio->page;
5151 	struct nfs4_createdata *data;
5152 	int status = -ENAMETOOLONG;
5153 
5154 	if (len > NFS4_MAXPATHLEN)
5155 		goto out;
5156 
5157 	status = -ENOMEM;
5158 	data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
5159 	if (data == NULL)
5160 		goto out;
5161 
5162 	data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
5163 	data->arg.u.symlink.pages = &page;
5164 	data->arg.u.symlink.len = len;
5165 	data->arg.label = label;
5166 
5167 	status = nfs4_do_create(dir, dentry, data);
5168 
5169 	nfs4_free_createdata(data);
5170 out:
5171 	return status;
5172 }
5173 
nfs4_proc_symlink(struct inode * dir,struct dentry * dentry,struct folio * folio,unsigned int len,struct iattr * sattr)5174 static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
5175 		struct folio *folio, unsigned int len, struct iattr *sattr)
5176 {
5177 	struct nfs4_exception exception = {
5178 		.interruptible = true,
5179 	};
5180 	struct nfs4_label l, *label;
5181 	int err;
5182 
5183 	label = nfs4_label_init_security(dir, dentry, sattr, &l);
5184 
5185 	do {
5186 		err = _nfs4_proc_symlink(dir, dentry, folio, len, sattr, label);
5187 		trace_nfs4_symlink(dir, &dentry->d_name, err);
5188 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
5189 				&exception);
5190 	} while (exception.retry);
5191 
5192 	nfs4_label_release_security(label);
5193 	return err;
5194 }
5195 
_nfs4_proc_mkdir(struct inode * dir,struct dentry * dentry,struct iattr * sattr,struct nfs4_label * label)5196 static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5197 		struct iattr *sattr, struct nfs4_label *label)
5198 {
5199 	struct nfs4_createdata *data;
5200 	int status = -ENOMEM;
5201 
5202 	data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
5203 	if (data == NULL)
5204 		goto out;
5205 
5206 	data->arg.label = label;
5207 	status = nfs4_do_create(dir, dentry, data);
5208 
5209 	nfs4_free_createdata(data);
5210 out:
5211 	return status;
5212 }
5213 
nfs4_proc_mkdir(struct inode * dir,struct dentry * dentry,struct iattr * sattr)5214 static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
5215 		struct iattr *sattr)
5216 {
5217 	struct nfs_server *server = NFS_SERVER(dir);
5218 	struct nfs4_exception exception = {
5219 		.interruptible = true,
5220 	};
5221 	struct nfs4_label l, *label;
5222 	int err;
5223 
5224 	label = nfs4_label_init_security(dir, dentry, sattr, &l);
5225 
5226 	if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5227 		sattr->ia_mode &= ~current_umask();
5228 	do {
5229 		err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
5230 		trace_nfs4_mkdir(dir, &dentry->d_name, err);
5231 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
5232 				&exception);
5233 	} while (exception.retry);
5234 	nfs4_label_release_security(label);
5235 
5236 	return err;
5237 }
5238 
_nfs4_proc_readdir(struct nfs_readdir_arg * nr_arg,struct nfs_readdir_res * nr_res)5239 static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg,
5240 			      struct nfs_readdir_res *nr_res)
5241 {
5242 	struct inode		*dir = d_inode(nr_arg->dentry);
5243 	struct nfs_server	*server = NFS_SERVER(dir);
5244 	struct nfs4_readdir_arg args = {
5245 		.fh = NFS_FH(dir),
5246 		.pages = nr_arg->pages,
5247 		.pgbase = 0,
5248 		.count = nr_arg->page_len,
5249 		.plus = nr_arg->plus,
5250 	};
5251 	struct nfs4_readdir_res res;
5252 	struct rpc_message msg = {
5253 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
5254 		.rpc_argp = &args,
5255 		.rpc_resp = &res,
5256 		.rpc_cred = nr_arg->cred,
5257 	};
5258 	int			status;
5259 
5260 	dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__,
5261 		nr_arg->dentry, (unsigned long long)nr_arg->cookie);
5262 	if (!(server->caps & NFS_CAP_SECURITY_LABEL))
5263 		args.bitmask = server->attr_bitmask_nl;
5264 	else
5265 		args.bitmask = server->attr_bitmask;
5266 
5267 	nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args);
5268 	res.pgbase = args.pgbase;
5269 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5270 			&res.seq_res, 0);
5271 	if (status >= 0) {
5272 		memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE);
5273 		status += args.pgbase;
5274 	}
5275 
5276 	nfs_invalidate_atime(dir);
5277 
5278 	dprintk("%s: returns %d\n", __func__, status);
5279 	return status;
5280 }
5281 
nfs4_proc_readdir(struct nfs_readdir_arg * arg,struct nfs_readdir_res * res)5282 static int nfs4_proc_readdir(struct nfs_readdir_arg *arg,
5283 			     struct nfs_readdir_res *res)
5284 {
5285 	struct nfs4_exception exception = {
5286 		.interruptible = true,
5287 	};
5288 	int err;
5289 	do {
5290 		err = _nfs4_proc_readdir(arg, res);
5291 		trace_nfs4_readdir(d_inode(arg->dentry), err);
5292 		err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)),
5293 					    err, &exception);
5294 	} while (exception.retry);
5295 	return err;
5296 }
5297 
_nfs4_proc_mknod(struct inode * dir,struct dentry * dentry,struct iattr * sattr,struct nfs4_label * label,dev_t rdev)5298 static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5299 		struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
5300 {
5301 	struct nfs4_createdata *data;
5302 	int mode = sattr->ia_mode;
5303 	int status = -ENOMEM;
5304 
5305 	data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
5306 	if (data == NULL)
5307 		goto out;
5308 
5309 	if (S_ISFIFO(mode))
5310 		data->arg.ftype = NF4FIFO;
5311 	else if (S_ISBLK(mode)) {
5312 		data->arg.ftype = NF4BLK;
5313 		data->arg.u.device.specdata1 = MAJOR(rdev);
5314 		data->arg.u.device.specdata2 = MINOR(rdev);
5315 	}
5316 	else if (S_ISCHR(mode)) {
5317 		data->arg.ftype = NF4CHR;
5318 		data->arg.u.device.specdata1 = MAJOR(rdev);
5319 		data->arg.u.device.specdata2 = MINOR(rdev);
5320 	} else if (!S_ISSOCK(mode)) {
5321 		status = -EINVAL;
5322 		goto out_free;
5323 	}
5324 
5325 	data->arg.label = label;
5326 	status = nfs4_do_create(dir, dentry, data);
5327 out_free:
5328 	nfs4_free_createdata(data);
5329 out:
5330 	return status;
5331 }
5332 
nfs4_proc_mknod(struct inode * dir,struct dentry * dentry,struct iattr * sattr,dev_t rdev)5333 static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5334 		struct iattr *sattr, dev_t rdev)
5335 {
5336 	struct nfs_server *server = NFS_SERVER(dir);
5337 	struct nfs4_exception exception = {
5338 		.interruptible = true,
5339 	};
5340 	struct nfs4_label l, *label;
5341 	int err;
5342 
5343 	label = nfs4_label_init_security(dir, dentry, sattr, &l);
5344 
5345 	if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5346 		sattr->ia_mode &= ~current_umask();
5347 	do {
5348 		err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5349 		trace_nfs4_mknod(dir, &dentry->d_name, err);
5350 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
5351 				&exception);
5352 	} while (exception.retry);
5353 
5354 	nfs4_label_release_security(label);
5355 
5356 	return err;
5357 }
5358 
_nfs4_proc_statfs(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsstat * fsstat)5359 static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5360 		 struct nfs_fsstat *fsstat)
5361 {
5362 	struct nfs4_statfs_arg args = {
5363 		.fh = fhandle,
5364 		.bitmask = server->attr_bitmask,
5365 	};
5366 	struct nfs4_statfs_res res = {
5367 		.fsstat = fsstat,
5368 	};
5369 	struct rpc_message msg = {
5370 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5371 		.rpc_argp = &args,
5372 		.rpc_resp = &res,
5373 	};
5374 
5375 	nfs_fattr_init(fsstat->fattr);
5376 	return  nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5377 }
5378 
nfs4_proc_statfs(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsstat * fsstat)5379 static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
5380 {
5381 	struct nfs4_exception exception = {
5382 		.interruptible = true,
5383 	};
5384 	int err;
5385 	do {
5386 		err = nfs4_handle_exception(server,
5387 				_nfs4_proc_statfs(server, fhandle, fsstat),
5388 				&exception);
5389 	} while (exception.retry);
5390 	return err;
5391 }
5392 
_nfs4_do_fsinfo(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * fsinfo)5393 static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5394 		struct nfs_fsinfo *fsinfo)
5395 {
5396 	struct nfs4_fsinfo_arg args = {
5397 		.fh = fhandle,
5398 		.bitmask = server->attr_bitmask,
5399 	};
5400 	struct nfs4_fsinfo_res res = {
5401 		.fsinfo = fsinfo,
5402 	};
5403 	struct rpc_message msg = {
5404 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5405 		.rpc_argp = &args,
5406 		.rpc_resp = &res,
5407 	};
5408 
5409 	return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5410 }
5411 
nfs4_do_fsinfo(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * fsinfo)5412 static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5413 {
5414 	struct nfs4_exception exception = {
5415 		.interruptible = true,
5416 	};
5417 	int err;
5418 
5419 	do {
5420 		err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
5421 		trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
5422 		if (err == 0) {
5423 			nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
5424 			break;
5425 		}
5426 		err = nfs4_handle_exception(server, err, &exception);
5427 	} while (exception.retry);
5428 	return err;
5429 }
5430 
nfs4_proc_fsinfo(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * fsinfo)5431 static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5432 {
5433 	int error;
5434 
5435 	nfs_fattr_init(fsinfo->fattr);
5436 	error = nfs4_do_fsinfo(server, fhandle, fsinfo);
5437 	if (error == 0) {
5438 		/* block layout checks this! */
5439 		server->pnfs_blksize = fsinfo->blksize;
5440 		set_pnfs_layoutdriver(server, fhandle, fsinfo);
5441 	}
5442 
5443 	return error;
5444 }
5445 
_nfs4_proc_pathconf(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_pathconf * pathconf)5446 static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5447 		struct nfs_pathconf *pathconf)
5448 {
5449 	struct nfs4_pathconf_arg args = {
5450 		.fh = fhandle,
5451 		.bitmask = server->attr_bitmask,
5452 	};
5453 	struct nfs4_pathconf_res res = {
5454 		.pathconf = pathconf,
5455 	};
5456 	struct rpc_message msg = {
5457 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5458 		.rpc_argp = &args,
5459 		.rpc_resp = &res,
5460 	};
5461 
5462 	/* None of the pathconf attributes are mandatory to implement */
5463 	if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5464 		memset(pathconf, 0, sizeof(*pathconf));
5465 		return 0;
5466 	}
5467 
5468 	nfs_fattr_init(pathconf->fattr);
5469 	return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5470 }
5471 
nfs4_proc_pathconf(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_pathconf * pathconf)5472 static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5473 		struct nfs_pathconf *pathconf)
5474 {
5475 	struct nfs4_exception exception = {
5476 		.interruptible = true,
5477 	};
5478 	int err;
5479 
5480 	do {
5481 		err = nfs4_handle_exception(server,
5482 				_nfs4_proc_pathconf(server, fhandle, pathconf),
5483 				&exception);
5484 	} while (exception.retry);
5485 	return err;
5486 }
5487 
nfs4_set_rw_stateid(nfs4_stateid * stateid,const struct nfs_open_context * ctx,const struct nfs_lock_context * l_ctx,fmode_t fmode)5488 int nfs4_set_rw_stateid(nfs4_stateid *stateid,
5489 		const struct nfs_open_context *ctx,
5490 		const struct nfs_lock_context *l_ctx,
5491 		fmode_t fmode)
5492 {
5493 	return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
5494 }
5495 EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5496 
nfs4_stateid_is_current(nfs4_stateid * stateid,const struct nfs_open_context * ctx,const struct nfs_lock_context * l_ctx,fmode_t fmode)5497 static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5498 		const struct nfs_open_context *ctx,
5499 		const struct nfs_lock_context *l_ctx,
5500 		fmode_t fmode)
5501 {
5502 	nfs4_stateid _current_stateid;
5503 
5504 	/* If the current stateid represents a lost lock, then exit */
5505 	if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
5506 		return true;
5507 	return nfs4_stateid_match(stateid, &_current_stateid);
5508 }
5509 
nfs4_error_stateid_expired(int err)5510 static bool nfs4_error_stateid_expired(int err)
5511 {
5512 	switch (err) {
5513 	case -NFS4ERR_DELEG_REVOKED:
5514 	case -NFS4ERR_ADMIN_REVOKED:
5515 	case -NFS4ERR_BAD_STATEID:
5516 	case -NFS4ERR_STALE_STATEID:
5517 	case -NFS4ERR_OLD_STATEID:
5518 	case -NFS4ERR_OPENMODE:
5519 	case -NFS4ERR_EXPIRED:
5520 		return true;
5521 	}
5522 	return false;
5523 }
5524 
nfs4_read_done_cb(struct rpc_task * task,struct nfs_pgio_header * hdr)5525 static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
5526 {
5527 	struct nfs_server *server = NFS_SERVER(hdr->inode);
5528 
5529 	trace_nfs4_read(hdr, task->tk_status);
5530 	if (task->tk_status < 0) {
5531 		struct nfs4_exception exception = {
5532 			.inode = hdr->inode,
5533 			.state = hdr->args.context->state,
5534 			.stateid = &hdr->args.stateid,
5535 		};
5536 		task->tk_status = nfs4_async_handle_exception(task,
5537 				server, task->tk_status, &exception);
5538 		if (exception.retry) {
5539 			rpc_restart_call_prepare(task);
5540 			return -EAGAIN;
5541 		}
5542 	}
5543 
5544 	if (task->tk_status > 0)
5545 		renew_lease(server, hdr->timestamp);
5546 	return 0;
5547 }
5548 
nfs4_read_stateid_changed(struct rpc_task * task,struct nfs_pgio_args * args)5549 static bool nfs4_read_stateid_changed(struct rpc_task *task,
5550 		struct nfs_pgio_args *args)
5551 {
5552 
5553 	if (!nfs4_error_stateid_expired(task->tk_status) ||
5554 		nfs4_stateid_is_current(&args->stateid,
5555 				args->context,
5556 				args->lock_context,
5557 				FMODE_READ))
5558 		return false;
5559 	rpc_restart_call_prepare(task);
5560 	return true;
5561 }
5562 
nfs4_read_plus_not_supported(struct rpc_task * task,struct nfs_pgio_header * hdr)5563 static bool nfs4_read_plus_not_supported(struct rpc_task *task,
5564 					 struct nfs_pgio_header *hdr)
5565 {
5566 	struct nfs_server *server = NFS_SERVER(hdr->inode);
5567 	struct rpc_message *msg = &task->tk_msg;
5568 
5569 	if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] &&
5570 	    task->tk_status == -ENOTSUPP) {
5571 		server->caps &= ~NFS_CAP_READ_PLUS;
5572 		msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5573 		rpc_restart_call_prepare(task);
5574 		return true;
5575 	}
5576 	return false;
5577 }
5578 
nfs4_read_done(struct rpc_task * task,struct nfs_pgio_header * hdr)5579 static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5580 {
5581 	if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5582 		return -EAGAIN;
5583 	if (nfs4_read_stateid_changed(task, &hdr->args))
5584 		return -EAGAIN;
5585 	if (nfs4_read_plus_not_supported(task, hdr))
5586 		return -EAGAIN;
5587 	if (task->tk_status > 0)
5588 		nfs_invalidate_atime(hdr->inode);
5589 	return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5590 				    nfs4_read_done_cb(task, hdr);
5591 }
5592 
5593 #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)5594 static bool nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5595 				    struct rpc_message *msg)
5596 {
5597 	/* Note: We don't use READ_PLUS with pNFS yet */
5598 	if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp) {
5599 		msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS];
5600 		return nfs_read_alloc_scratch(hdr, READ_PLUS_SCRATCH_SIZE);
5601 	}
5602 	return false;
5603 }
5604 #else
nfs42_read_plus_support(struct nfs_pgio_header * hdr,struct rpc_message * msg)5605 static bool nfs42_read_plus_support(struct nfs_pgio_header *hdr,
5606 				    struct rpc_message *msg)
5607 {
5608 	return false;
5609 }
5610 #endif /* CONFIG_NFS_V4_2 */
5611 
nfs4_proc_read_setup(struct nfs_pgio_header * hdr,struct rpc_message * msg)5612 static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5613 				 struct rpc_message *msg)
5614 {
5615 	hdr->timestamp   = jiffies;
5616 	if (!hdr->pgio_done_cb)
5617 		hdr->pgio_done_cb = nfs4_read_done_cb;
5618 	if (!nfs42_read_plus_support(hdr, msg))
5619 		msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
5620 	nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5621 }
5622 
nfs4_proc_pgio_rpc_prepare(struct rpc_task * task,struct nfs_pgio_header * hdr)5623 static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5624 				      struct nfs_pgio_header *hdr)
5625 {
5626 	if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
5627 			&hdr->args.seq_args,
5628 			&hdr->res.seq_res,
5629 			task))
5630 		return 0;
5631 	if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5632 				hdr->args.lock_context,
5633 				hdr->rw_mode) == -EIO)
5634 		return -EIO;
5635 	if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
5636 		return -EIO;
5637 	return 0;
5638 }
5639 
nfs4_write_done_cb(struct rpc_task * task,struct nfs_pgio_header * hdr)5640 static int nfs4_write_done_cb(struct rpc_task *task,
5641 			      struct nfs_pgio_header *hdr)
5642 {
5643 	struct inode *inode = hdr->inode;
5644 
5645 	trace_nfs4_write(hdr, task->tk_status);
5646 	if (task->tk_status < 0) {
5647 		struct nfs4_exception exception = {
5648 			.inode = hdr->inode,
5649 			.state = hdr->args.context->state,
5650 			.stateid = &hdr->args.stateid,
5651 		};
5652 		task->tk_status = nfs4_async_handle_exception(task,
5653 				NFS_SERVER(inode), task->tk_status,
5654 				&exception);
5655 		if (exception.retry) {
5656 			rpc_restart_call_prepare(task);
5657 			return -EAGAIN;
5658 		}
5659 	}
5660 	if (task->tk_status >= 0) {
5661 		renew_lease(NFS_SERVER(inode), hdr->timestamp);
5662 		nfs_writeback_update_inode(hdr);
5663 	}
5664 	return 0;
5665 }
5666 
nfs4_write_stateid_changed(struct rpc_task * task,struct nfs_pgio_args * args)5667 static bool nfs4_write_stateid_changed(struct rpc_task *task,
5668 		struct nfs_pgio_args *args)
5669 {
5670 
5671 	if (!nfs4_error_stateid_expired(task->tk_status) ||
5672 		nfs4_stateid_is_current(&args->stateid,
5673 				args->context,
5674 				args->lock_context,
5675 				FMODE_WRITE))
5676 		return false;
5677 	rpc_restart_call_prepare(task);
5678 	return true;
5679 }
5680 
nfs4_write_done(struct rpc_task * task,struct nfs_pgio_header * hdr)5681 static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
5682 {
5683 	if (!nfs4_sequence_done(task, &hdr->res.seq_res))
5684 		return -EAGAIN;
5685 	if (nfs4_write_stateid_changed(task, &hdr->args))
5686 		return -EAGAIN;
5687 	return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5688 		nfs4_write_done_cb(task, hdr);
5689 }
5690 
5691 static
nfs4_write_need_cache_consistency_data(struct nfs_pgio_header * hdr)5692 bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
5693 {
5694 	/* Don't request attributes for pNFS or O_DIRECT writes */
5695 	if (hdr->ds_clp != NULL || hdr->dreq != NULL)
5696 		return false;
5697 	/* Otherwise, request attributes if and only if we don't hold
5698 	 * a delegation
5699 	 */
5700 	return nfs4_have_delegation(hdr->inode, FMODE_READ, 0) == 0;
5701 }
5702 
nfs4_bitmask_set(__u32 bitmask[],const __u32 src[],struct inode * inode,unsigned long cache_validity)5703 void nfs4_bitmask_set(__u32 bitmask[], const __u32 src[],
5704 		      struct inode *inode, unsigned long cache_validity)
5705 {
5706 	struct nfs_server *server = NFS_SERVER(inode);
5707 	unsigned int i;
5708 
5709 	memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ);
5710 	cache_validity |= READ_ONCE(NFS_I(inode)->cache_validity);
5711 
5712 	if (cache_validity & NFS_INO_INVALID_CHANGE)
5713 		bitmask[0] |= FATTR4_WORD0_CHANGE;
5714 	if (cache_validity & NFS_INO_INVALID_ATIME)
5715 		bitmask[1] |= FATTR4_WORD1_TIME_ACCESS;
5716 	if (cache_validity & NFS_INO_INVALID_MODE)
5717 		bitmask[1] |= FATTR4_WORD1_MODE;
5718 	if (cache_validity & NFS_INO_INVALID_OTHER)
5719 		bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP;
5720 	if (cache_validity & NFS_INO_INVALID_NLINK)
5721 		bitmask[1] |= FATTR4_WORD1_NUMLINKS;
5722 	if (cache_validity & NFS_INO_INVALID_CTIME)
5723 		bitmask[1] |= FATTR4_WORD1_TIME_METADATA;
5724 	if (cache_validity & NFS_INO_INVALID_MTIME)
5725 		bitmask[1] |= FATTR4_WORD1_TIME_MODIFY;
5726 	if (cache_validity & NFS_INO_INVALID_BLOCKS)
5727 		bitmask[1] |= FATTR4_WORD1_SPACE_USED;
5728 
5729 	if (cache_validity & NFS_INO_INVALID_SIZE)
5730 		bitmask[0] |= FATTR4_WORD0_SIZE;
5731 
5732 	for (i = 0; i < NFS4_BITMASK_SZ; i++)
5733 		bitmask[i] &= server->attr_bitmask[i];
5734 }
5735 
nfs4_proc_write_setup(struct nfs_pgio_header * hdr,struct rpc_message * msg,struct rpc_clnt ** clnt)5736 static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
5737 				  struct rpc_message *msg,
5738 				  struct rpc_clnt **clnt)
5739 {
5740 	struct nfs_server *server = NFS_SERVER(hdr->inode);
5741 
5742 	if (!nfs4_write_need_cache_consistency_data(hdr)) {
5743 		hdr->args.bitmask = NULL;
5744 		hdr->res.fattr = NULL;
5745 	} else {
5746 		nfs4_bitmask_set(hdr->args.bitmask_store,
5747 				 server->cache_consistency_bitmask,
5748 				 hdr->inode, NFS_INO_INVALID_BLOCKS);
5749 		hdr->args.bitmask = hdr->args.bitmask_store;
5750 	}
5751 
5752 	if (!hdr->pgio_done_cb)
5753 		hdr->pgio_done_cb = nfs4_write_done_cb;
5754 	hdr->res.server = server;
5755 	hdr->timestamp   = jiffies;
5756 
5757 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
5758 	nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
5759 	nfs4_state_protect_write(hdr->ds_clp ? hdr->ds_clp : server->nfs_client, clnt, msg, hdr);
5760 }
5761 
nfs4_proc_commit_rpc_prepare(struct rpc_task * task,struct nfs_commit_data * data)5762 static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5763 {
5764 	nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
5765 			&data->args.seq_args,
5766 			&data->res.seq_res,
5767 			task);
5768 }
5769 
nfs4_commit_done_cb(struct rpc_task * task,struct nfs_commit_data * data)5770 static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
5771 {
5772 	struct inode *inode = data->inode;
5773 
5774 	trace_nfs4_commit(data, task->tk_status);
5775 	if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5776 				    NULL, NULL) == -EAGAIN) {
5777 		rpc_restart_call_prepare(task);
5778 		return -EAGAIN;
5779 	}
5780 	return 0;
5781 }
5782 
nfs4_commit_done(struct rpc_task * task,struct nfs_commit_data * data)5783 static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
5784 {
5785 	if (!nfs4_sequence_done(task, &data->res.seq_res))
5786 		return -EAGAIN;
5787 	return data->commit_done_cb(task, data);
5788 }
5789 
nfs4_proc_commit_setup(struct nfs_commit_data * data,struct rpc_message * msg,struct rpc_clnt ** clnt)5790 static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5791 				   struct rpc_clnt **clnt)
5792 {
5793 	struct nfs_server *server = NFS_SERVER(data->inode);
5794 
5795 	if (data->commit_done_cb == NULL)
5796 		data->commit_done_cb = nfs4_commit_done_cb;
5797 	data->res.server = server;
5798 	msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
5799 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
5800 	nfs4_state_protect(data->ds_clp ? data->ds_clp : server->nfs_client,
5801 			NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
5802 }
5803 
_nfs4_proc_commit(struct file * dst,struct nfs_commitargs * args,struct nfs_commitres * res)5804 static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5805 				struct nfs_commitres *res)
5806 {
5807 	struct inode *dst_inode = file_inode(dst);
5808 	struct nfs_server *server = NFS_SERVER(dst_inode);
5809 	struct rpc_message msg = {
5810 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5811 		.rpc_argp = args,
5812 		.rpc_resp = res,
5813 	};
5814 
5815 	args->fh = NFS_FH(dst_inode);
5816 	return nfs4_call_sync(server->client, server, &msg,
5817 			&args->seq_args, &res->seq_res, 1);
5818 }
5819 
nfs4_proc_commit(struct file * dst,__u64 offset,__u32 count,struct nfs_commitres * res)5820 int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5821 {
5822 	struct nfs_commitargs args = {
5823 		.offset = offset,
5824 		.count = count,
5825 	};
5826 	struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5827 	struct nfs4_exception exception = { };
5828 	int status;
5829 
5830 	do {
5831 		status = _nfs4_proc_commit(dst, &args, res);
5832 		status = nfs4_handle_exception(dst_server, status, &exception);
5833 	} while (exception.retry);
5834 
5835 	return status;
5836 }
5837 
5838 struct nfs4_renewdata {
5839 	struct nfs_client	*client;
5840 	unsigned long		timestamp;
5841 };
5842 
5843 /*
5844  * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5845  * standalone procedure for queueing an asynchronous RENEW.
5846  */
nfs4_renew_release(void * calldata)5847 static void nfs4_renew_release(void *calldata)
5848 {
5849 	struct nfs4_renewdata *data = calldata;
5850 	struct nfs_client *clp = data->client;
5851 
5852 	if (refcount_read(&clp->cl_count) > 1)
5853 		nfs4_schedule_state_renewal(clp);
5854 	nfs_put_client(clp);
5855 	kfree(data);
5856 }
5857 
nfs4_renew_done(struct rpc_task * task,void * calldata)5858 static void nfs4_renew_done(struct rpc_task *task, void *calldata)
5859 {
5860 	struct nfs4_renewdata *data = calldata;
5861 	struct nfs_client *clp = data->client;
5862 	unsigned long timestamp = data->timestamp;
5863 
5864 	trace_nfs4_renew_async(clp, task->tk_status);
5865 	switch (task->tk_status) {
5866 	case 0:
5867 		break;
5868 	case -NFS4ERR_LEASE_MOVED:
5869 		nfs4_schedule_lease_moved_recovery(clp);
5870 		break;
5871 	default:
5872 		/* Unless we're shutting down, schedule state recovery! */
5873 		if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5874 			return;
5875 		if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
5876 			nfs4_schedule_lease_recovery(clp);
5877 			return;
5878 		}
5879 		nfs4_schedule_path_down_recovery(clp);
5880 	}
5881 	do_renew_lease(clp, timestamp);
5882 }
5883 
5884 static const struct rpc_call_ops nfs4_renew_ops = {
5885 	.rpc_call_done = nfs4_renew_done,
5886 	.rpc_release = nfs4_renew_release,
5887 };
5888 
nfs4_proc_async_renew(struct nfs_client * clp,const struct cred * cred,unsigned renew_flags)5889 static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
5890 {
5891 	struct rpc_message msg = {
5892 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5893 		.rpc_argp	= clp,
5894 		.rpc_cred	= cred,
5895 	};
5896 	struct nfs4_renewdata *data;
5897 
5898 	if (renew_flags == 0)
5899 		return 0;
5900 	if (!refcount_inc_not_zero(&clp->cl_count))
5901 		return -EIO;
5902 	data = kmalloc(sizeof(*data), GFP_NOFS);
5903 	if (data == NULL) {
5904 		nfs_put_client(clp);
5905 		return -ENOMEM;
5906 	}
5907 	data->client = clp;
5908 	data->timestamp = jiffies;
5909 	return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
5910 			&nfs4_renew_ops, data);
5911 }
5912 
nfs4_proc_renew(struct nfs_client * clp,const struct cred * cred)5913 static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
5914 {
5915 	struct rpc_message msg = {
5916 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5917 		.rpc_argp	= clp,
5918 		.rpc_cred	= cred,
5919 	};
5920 	unsigned long now = jiffies;
5921 	int status;
5922 
5923 	status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5924 	if (status < 0)
5925 		return status;
5926 	do_renew_lease(clp, now);
5927 	return 0;
5928 }
5929 
nfs4_server_supports_acls(const struct nfs_server * server,enum nfs4_acl_type type)5930 static bool nfs4_server_supports_acls(const struct nfs_server *server,
5931 				      enum nfs4_acl_type type)
5932 {
5933 	switch (type) {
5934 	default:
5935 		return server->attr_bitmask[0] & FATTR4_WORD0_ACL;
5936 	case NFS4ACL_DACL:
5937 		return server->attr_bitmask[1] & FATTR4_WORD1_DACL;
5938 	case NFS4ACL_SACL:
5939 		return server->attr_bitmask[1] & FATTR4_WORD1_SACL;
5940 	}
5941 }
5942 
5943 /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5944  * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
5945  * the stack.
5946  */
5947 #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
5948 
nfs4_buf_to_pages_noslab(const void * buf,size_t buflen,struct page ** pages)5949 int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
5950 		struct page **pages)
5951 {
5952 	struct page *newpage, **spages;
5953 	int rc = 0;
5954 	size_t len;
5955 	spages = pages;
5956 
5957 	do {
5958 		len = min_t(size_t, PAGE_SIZE, buflen);
5959 		newpage = alloc_page(GFP_KERNEL);
5960 
5961 		if (newpage == NULL)
5962 			goto unwind;
5963 		memcpy(page_address(newpage), buf, len);
5964 		buf += len;
5965 		buflen -= len;
5966 		*pages++ = newpage;
5967 		rc++;
5968 	} while (buflen != 0);
5969 
5970 	return rc;
5971 
5972 unwind:
5973 	for(; rc > 0; rc--)
5974 		__free_page(spages[rc-1]);
5975 	return -ENOMEM;
5976 }
5977 
5978 struct nfs4_cached_acl {
5979 	enum nfs4_acl_type type;
5980 	int cached;
5981 	size_t len;
5982 	char data[];
5983 };
5984 
nfs4_set_cached_acl(struct inode * inode,struct nfs4_cached_acl * acl)5985 static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
5986 {
5987 	struct nfs_inode *nfsi = NFS_I(inode);
5988 
5989 	spin_lock(&inode->i_lock);
5990 	kfree(nfsi->nfs4_acl);
5991 	nfsi->nfs4_acl = acl;
5992 	spin_unlock(&inode->i_lock);
5993 }
5994 
nfs4_zap_acl_attr(struct inode * inode)5995 static void nfs4_zap_acl_attr(struct inode *inode)
5996 {
5997 	nfs4_set_cached_acl(inode, NULL);
5998 }
5999 
nfs4_read_cached_acl(struct inode * inode,char * buf,size_t buflen,enum nfs4_acl_type type)6000 static ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf,
6001 				    size_t buflen, enum nfs4_acl_type type)
6002 {
6003 	struct nfs_inode *nfsi = NFS_I(inode);
6004 	struct nfs4_cached_acl *acl;
6005 	int ret = -ENOENT;
6006 
6007 	spin_lock(&inode->i_lock);
6008 	acl = nfsi->nfs4_acl;
6009 	if (acl == NULL)
6010 		goto out;
6011 	if (acl->type != type)
6012 		goto out;
6013 	if (buf == NULL) /* user is just asking for length */
6014 		goto out_len;
6015 	if (acl->cached == 0)
6016 		goto out;
6017 	ret = -ERANGE; /* see getxattr(2) man page */
6018 	if (acl->len > buflen)
6019 		goto out;
6020 	memcpy(buf, acl->data, acl->len);
6021 out_len:
6022 	ret = acl->len;
6023 out:
6024 	spin_unlock(&inode->i_lock);
6025 	return ret;
6026 }
6027 
nfs4_write_cached_acl(struct inode * inode,struct page ** pages,size_t pgbase,size_t acl_len,enum nfs4_acl_type type)6028 static void nfs4_write_cached_acl(struct inode *inode, struct page **pages,
6029 				  size_t pgbase, size_t acl_len,
6030 				  enum nfs4_acl_type type)
6031 {
6032 	struct nfs4_cached_acl *acl;
6033 	size_t buflen = sizeof(*acl) + acl_len;
6034 
6035 	if (buflen <= PAGE_SIZE) {
6036 		acl = kmalloc(buflen, GFP_KERNEL);
6037 		if (acl == NULL)
6038 			goto out;
6039 		acl->cached = 1;
6040 		_copy_from_pages(acl->data, pages, pgbase, acl_len);
6041 	} else {
6042 		acl = kmalloc(sizeof(*acl), GFP_KERNEL);
6043 		if (acl == NULL)
6044 			goto out;
6045 		acl->cached = 0;
6046 	}
6047 	acl->type = type;
6048 	acl->len = acl_len;
6049 out:
6050 	nfs4_set_cached_acl(inode, acl);
6051 }
6052 
6053 /*
6054  * The getxattr API returns the required buffer length when called with a
6055  * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
6056  * the required buf.  On a NULL buf, we send a page of data to the server
6057  * guessing that the ACL request can be serviced by a page. If so, we cache
6058  * up to the page of ACL data, and the 2nd call to getxattr is serviced by
6059  * the cache. If not so, we throw away the page, and cache the required
6060  * length. The next getxattr call will then produce another round trip to
6061  * the server, this time with the input buf of the required size.
6062  */
__nfs4_get_acl_uncached(struct inode * inode,void * buf,size_t buflen,enum nfs4_acl_type type)6063 static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf,
6064 				       size_t buflen, enum nfs4_acl_type type)
6065 {
6066 	struct page **pages;
6067 	struct nfs_getaclargs args = {
6068 		.fh = NFS_FH(inode),
6069 		.acl_type = type,
6070 		.acl_len = buflen,
6071 	};
6072 	struct nfs_getaclres res = {
6073 		.acl_type = type,
6074 		.acl_len = buflen,
6075 	};
6076 	struct rpc_message msg = {
6077 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
6078 		.rpc_argp = &args,
6079 		.rpc_resp = &res,
6080 	};
6081 	unsigned int npages;
6082 	int ret = -ENOMEM, i;
6083 	struct nfs_server *server = NFS_SERVER(inode);
6084 
6085 	if (buflen == 0)
6086 		buflen = server->rsize;
6087 
6088 	npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
6089 	pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
6090 	if (!pages)
6091 		return -ENOMEM;
6092 
6093 	args.acl_pages = pages;
6094 
6095 	for (i = 0; i < npages; i++) {
6096 		pages[i] = alloc_page(GFP_KERNEL);
6097 		if (!pages[i])
6098 			goto out_free;
6099 	}
6100 
6101 	/* for decoding across pages */
6102 	res.acl_scratch = alloc_page(GFP_KERNEL);
6103 	if (!res.acl_scratch)
6104 		goto out_free;
6105 
6106 	args.acl_len = npages * PAGE_SIZE;
6107 
6108 	dprintk("%s  buf %p buflen %zu npages %d args.acl_len %zu\n",
6109 		__func__, buf, buflen, npages, args.acl_len);
6110 	ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
6111 			     &msg, &args.seq_args, &res.seq_res, 0);
6112 	if (ret)
6113 		goto out_free;
6114 
6115 	/* Handle the case where the passed-in buffer is too short */
6116 	if (res.acl_flags & NFS4_ACL_TRUNC) {
6117 		/* Did the user only issue a request for the acl length? */
6118 		if (buf == NULL)
6119 			goto out_ok;
6120 		ret = -ERANGE;
6121 		goto out_free;
6122 	}
6123 	nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len,
6124 			      type);
6125 	if (buf) {
6126 		if (res.acl_len > buflen) {
6127 			ret = -ERANGE;
6128 			goto out_free;
6129 		}
6130 		_copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
6131 	}
6132 out_ok:
6133 	ret = res.acl_len;
6134 out_free:
6135 	while (--i >= 0)
6136 		__free_page(pages[i]);
6137 	if (res.acl_scratch)
6138 		__free_page(res.acl_scratch);
6139 	kfree(pages);
6140 	return ret;
6141 }
6142 
nfs4_get_acl_uncached(struct inode * inode,void * buf,size_t buflen,enum nfs4_acl_type type)6143 static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf,
6144 				     size_t buflen, enum nfs4_acl_type type)
6145 {
6146 	struct nfs4_exception exception = {
6147 		.interruptible = true,
6148 	};
6149 	ssize_t ret;
6150 	do {
6151 		ret = __nfs4_get_acl_uncached(inode, buf, buflen, type);
6152 		trace_nfs4_get_acl(inode, ret);
6153 		if (ret >= 0)
6154 			break;
6155 		ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
6156 	} while (exception.retry);
6157 	return ret;
6158 }
6159 
nfs4_proc_get_acl(struct inode * inode,void * buf,size_t buflen,enum nfs4_acl_type type)6160 static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen,
6161 				 enum nfs4_acl_type type)
6162 {
6163 	struct nfs_server *server = NFS_SERVER(inode);
6164 	int ret;
6165 
6166 	if (!nfs4_server_supports_acls(server, type))
6167 		return -EOPNOTSUPP;
6168 	ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
6169 	if (ret < 0)
6170 		return ret;
6171 	if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
6172 		nfs_zap_acl_cache(inode);
6173 	ret = nfs4_read_cached_acl(inode, buf, buflen, type);
6174 	if (ret != -ENOENT)
6175 		/* -ENOENT is returned if there is no ACL or if there is an ACL
6176 		 * but no cached acl data, just the acl length */
6177 		return ret;
6178 	return nfs4_get_acl_uncached(inode, buf, buflen, type);
6179 }
6180 
__nfs4_proc_set_acl(struct inode * inode,const void * buf,size_t buflen,enum nfs4_acl_type type)6181 static int __nfs4_proc_set_acl(struct inode *inode, const void *buf,
6182 			       size_t buflen, enum nfs4_acl_type type)
6183 {
6184 	struct nfs_server *server = NFS_SERVER(inode);
6185 	struct page *pages[NFS4ACL_MAXPAGES];
6186 	struct nfs_setaclargs arg = {
6187 		.fh = NFS_FH(inode),
6188 		.acl_type = type,
6189 		.acl_len = buflen,
6190 		.acl_pages = pages,
6191 	};
6192 	struct nfs_setaclres res;
6193 	struct rpc_message msg = {
6194 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_SETACL],
6195 		.rpc_argp	= &arg,
6196 		.rpc_resp	= &res,
6197 	};
6198 	unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
6199 	int ret, i;
6200 
6201 	/* You can't remove system.nfs4_acl: */
6202 	if (buflen == 0)
6203 		return -EINVAL;
6204 	if (!nfs4_server_supports_acls(server, type))
6205 		return -EOPNOTSUPP;
6206 	if (npages > ARRAY_SIZE(pages))
6207 		return -ERANGE;
6208 	i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
6209 	if (i < 0)
6210 		return i;
6211 	nfs4_inode_make_writeable(inode);
6212 	ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6213 
6214 	/*
6215 	 * Free each page after tx, so the only ref left is
6216 	 * held by the network stack
6217 	 */
6218 	for (; i > 0; i--)
6219 		put_page(pages[i-1]);
6220 
6221 	/*
6222 	 * Acl update can result in inode attribute update.
6223 	 * so mark the attribute cache invalid.
6224 	 */
6225 	spin_lock(&inode->i_lock);
6226 	nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE |
6227 					     NFS_INO_INVALID_CTIME |
6228 					     NFS_INO_REVAL_FORCED);
6229 	spin_unlock(&inode->i_lock);
6230 	nfs_access_zap_cache(inode);
6231 	nfs_zap_acl_cache(inode);
6232 	return ret;
6233 }
6234 
nfs4_proc_set_acl(struct inode * inode,const void * buf,size_t buflen,enum nfs4_acl_type type)6235 static int nfs4_proc_set_acl(struct inode *inode, const void *buf,
6236 			     size_t buflen, enum nfs4_acl_type type)
6237 {
6238 	struct nfs4_exception exception = { };
6239 	int err;
6240 	do {
6241 		err = __nfs4_proc_set_acl(inode, buf, buflen, type);
6242 		trace_nfs4_set_acl(inode, err);
6243 		if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) {
6244 			/*
6245 			 * no need to retry since the kernel
6246 			 * isn't involved in encoding the ACEs.
6247 			 */
6248 			err = -EINVAL;
6249 			break;
6250 		}
6251 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
6252 				&exception);
6253 	} while (exception.retry);
6254 	return err;
6255 }
6256 
6257 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
_nfs4_get_security_label(struct inode * inode,void * buf,size_t buflen)6258 static int _nfs4_get_security_label(struct inode *inode, void *buf,
6259 					size_t buflen)
6260 {
6261 	struct nfs_server *server = NFS_SERVER(inode);
6262 	struct nfs4_label label = {0, 0, buflen, buf};
6263 
6264 	u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6265 	struct nfs_fattr fattr = {
6266 		.label = &label,
6267 	};
6268 	struct nfs4_getattr_arg arg = {
6269 		.fh		= NFS_FH(inode),
6270 		.bitmask	= bitmask,
6271 	};
6272 	struct nfs4_getattr_res res = {
6273 		.fattr		= &fattr,
6274 		.server		= server,
6275 	};
6276 	struct rpc_message msg = {
6277 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
6278 		.rpc_argp	= &arg,
6279 		.rpc_resp	= &res,
6280 	};
6281 	int ret;
6282 
6283 	nfs_fattr_init(&fattr);
6284 
6285 	ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
6286 	if (ret)
6287 		return ret;
6288 	if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
6289 		return -ENOENT;
6290 	return label.len;
6291 }
6292 
nfs4_get_security_label(struct inode * inode,void * buf,size_t buflen)6293 static int nfs4_get_security_label(struct inode *inode, void *buf,
6294 					size_t buflen)
6295 {
6296 	struct nfs4_exception exception = {
6297 		.interruptible = true,
6298 	};
6299 	int err;
6300 
6301 	if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6302 		return -EOPNOTSUPP;
6303 
6304 	do {
6305 		err = _nfs4_get_security_label(inode, buf, buflen);
6306 		trace_nfs4_get_security_label(inode, err);
6307 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
6308 				&exception);
6309 	} while (exception.retry);
6310 	return err;
6311 }
6312 
_nfs4_do_set_security_label(struct inode * inode,struct nfs4_label * ilabel,struct nfs_fattr * fattr)6313 static int _nfs4_do_set_security_label(struct inode *inode,
6314 		struct nfs4_label *ilabel,
6315 		struct nfs_fattr *fattr)
6316 {
6317 
6318 	struct iattr sattr = {0};
6319 	struct nfs_server *server = NFS_SERVER(inode);
6320 	const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
6321 	struct nfs_setattrargs arg = {
6322 		.fh		= NFS_FH(inode),
6323 		.iap		= &sattr,
6324 		.server		= server,
6325 		.bitmask	= bitmask,
6326 		.label		= ilabel,
6327 	};
6328 	struct nfs_setattrres res = {
6329 		.fattr		= fattr,
6330 		.server		= server,
6331 	};
6332 	struct rpc_message msg = {
6333 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
6334 		.rpc_argp	= &arg,
6335 		.rpc_resp	= &res,
6336 	};
6337 	int status;
6338 
6339 	nfs4_stateid_copy(&arg.stateid, &zero_stateid);
6340 
6341 	status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6342 	if (status)
6343 		dprintk("%s failed: %d\n", __func__, status);
6344 
6345 	return status;
6346 }
6347 
nfs4_do_set_security_label(struct inode * inode,struct nfs4_label * ilabel,struct nfs_fattr * fattr)6348 static int nfs4_do_set_security_label(struct inode *inode,
6349 		struct nfs4_label *ilabel,
6350 		struct nfs_fattr *fattr)
6351 {
6352 	struct nfs4_exception exception = { };
6353 	int err;
6354 
6355 	do {
6356 		err = _nfs4_do_set_security_label(inode, ilabel, fattr);
6357 		trace_nfs4_set_security_label(inode, err);
6358 		err = nfs4_handle_exception(NFS_SERVER(inode), err,
6359 				&exception);
6360 	} while (exception.retry);
6361 	return err;
6362 }
6363 
6364 static int
nfs4_set_security_label(struct inode * inode,const void * buf,size_t buflen)6365 nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
6366 {
6367 	struct nfs4_label ilabel = {0, 0, buflen, (char *)buf };
6368 	struct nfs_fattr *fattr;
6369 	int status;
6370 
6371 	if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
6372 		return -EOPNOTSUPP;
6373 
6374 	fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode));
6375 	if (fattr == NULL)
6376 		return -ENOMEM;
6377 
6378 	status = nfs4_do_set_security_label(inode, &ilabel, fattr);
6379 	if (status == 0)
6380 		nfs_setsecurity(inode, fattr);
6381 
6382 	nfs_free_fattr(fattr);
6383 	return status;
6384 }
6385 #endif	/* CONFIG_NFS_V4_SECURITY_LABEL */
6386 
6387 
nfs4_init_boot_verifier(const struct nfs_client * clp,nfs4_verifier * bootverf)6388 static void nfs4_init_boot_verifier(const struct nfs_client *clp,
6389 				    nfs4_verifier *bootverf)
6390 {
6391 	__be32 verf[2];
6392 
6393 	if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
6394 		/* An impossible timestamp guarantees this value
6395 		 * will never match a generated boot time. */
6396 		verf[0] = cpu_to_be32(U32_MAX);
6397 		verf[1] = cpu_to_be32(U32_MAX);
6398 	} else {
6399 		struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6400 		u64 ns = ktime_to_ns(nn->boot_time);
6401 
6402 		verf[0] = cpu_to_be32(ns >> 32);
6403 		verf[1] = cpu_to_be32(ns);
6404 	}
6405 	memcpy(bootverf->data, verf, sizeof(bootverf->data));
6406 }
6407 
6408 static size_t
nfs4_get_uniquifier(struct nfs_client * clp,char * buf,size_t buflen)6409 nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen)
6410 {
6411 	struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
6412 	struct nfs_netns_client *nn_clp = nn->nfs_client;
6413 	const char *id;
6414 
6415 	buf[0] = '\0';
6416 
6417 	if (nn_clp) {
6418 		rcu_read_lock();
6419 		id = rcu_dereference(nn_clp->identifier);
6420 		if (id)
6421 			strscpy(buf, id, buflen);
6422 		rcu_read_unlock();
6423 	}
6424 
6425 	if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0')
6426 		strscpy(buf, nfs4_client_id_uniquifier, buflen);
6427 
6428 	return strlen(buf);
6429 }
6430 
6431 static int
nfs4_init_nonuniform_client_string(struct nfs_client * clp)6432 nfs4_init_nonuniform_client_string(struct nfs_client *clp)
6433 {
6434 	char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6435 	size_t buflen;
6436 	size_t len;
6437 	char *str;
6438 
6439 	if (clp->cl_owner_id != NULL)
6440 		return 0;
6441 
6442 	rcu_read_lock();
6443 	len = 14 +
6444 		strlen(clp->cl_rpcclient->cl_nodename) +
6445 		1 +
6446 		strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
6447 		1;
6448 	rcu_read_unlock();
6449 
6450 	buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6451 	if (buflen)
6452 		len += buflen + 1;
6453 
6454 	if (len > NFS4_OPAQUE_LIMIT + 1)
6455 		return -EINVAL;
6456 
6457 	/*
6458 	 * Since this string is allocated at mount time, and held until the
6459 	 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6460 	 * about a memory-reclaim deadlock.
6461 	 */
6462 	str = kmalloc(len, GFP_KERNEL);
6463 	if (!str)
6464 		return -ENOMEM;
6465 
6466 	rcu_read_lock();
6467 	if (buflen)
6468 		scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
6469 			  clp->cl_rpcclient->cl_nodename, buf,
6470 			  rpc_peeraddr2str(clp->cl_rpcclient,
6471 					   RPC_DISPLAY_ADDR));
6472 	else
6473 		scnprintf(str, len, "Linux NFSv4.0 %s/%s",
6474 			  clp->cl_rpcclient->cl_nodename,
6475 			  rpc_peeraddr2str(clp->cl_rpcclient,
6476 					   RPC_DISPLAY_ADDR));
6477 	rcu_read_unlock();
6478 
6479 	clp->cl_owner_id = str;
6480 	return 0;
6481 }
6482 
6483 static int
nfs4_init_uniform_client_string(struct nfs_client * clp)6484 nfs4_init_uniform_client_string(struct nfs_client *clp)
6485 {
6486 	char buf[NFS4_CLIENT_ID_UNIQ_LEN];
6487 	size_t buflen;
6488 	size_t len;
6489 	char *str;
6490 
6491 	if (clp->cl_owner_id != NULL)
6492 		return 0;
6493 
6494 	len = 10 + 10 + 1 + 10 + 1 +
6495 		strlen(clp->cl_rpcclient->cl_nodename) + 1;
6496 
6497 	buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf));
6498 	if (buflen)
6499 		len += buflen + 1;
6500 
6501 	if (len > NFS4_OPAQUE_LIMIT + 1)
6502 		return -EINVAL;
6503 
6504 	/*
6505 	 * Since this string is allocated at mount time, and held until the
6506 	 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6507 	 * about a memory-reclaim deadlock.
6508 	 */
6509 	str = kmalloc(len, GFP_KERNEL);
6510 	if (!str)
6511 		return -ENOMEM;
6512 
6513 	if (buflen)
6514 		scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
6515 			  clp->rpc_ops->version, clp->cl_minorversion,
6516 			  buf, clp->cl_rpcclient->cl_nodename);
6517 	else
6518 		scnprintf(str, len, "Linux NFSv%u.%u %s",
6519 			  clp->rpc_ops->version, clp->cl_minorversion,
6520 			  clp->cl_rpcclient->cl_nodename);
6521 	clp->cl_owner_id = str;
6522 	return 0;
6523 }
6524 
6525 /*
6526  * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6527  * services.  Advertise one based on the address family of the
6528  * clientaddr.
6529  */
6530 static unsigned int
nfs4_init_callback_netid(const struct nfs_client * clp,char * buf,size_t len)6531 nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6532 {
6533 	if (strchr(clp->cl_ipaddr, ':') != NULL)
6534 		return scnprintf(buf, len, "tcp6");
6535 	else
6536 		return scnprintf(buf, len, "tcp");
6537 }
6538 
nfs4_setclientid_done(struct rpc_task * task,void * calldata)6539 static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6540 {
6541 	struct nfs4_setclientid *sc = calldata;
6542 
6543 	if (task->tk_status == 0)
6544 		sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6545 }
6546 
6547 static const struct rpc_call_ops nfs4_setclientid_ops = {
6548 	.rpc_call_done = nfs4_setclientid_done,
6549 };
6550 
6551 /**
6552  * nfs4_proc_setclientid - Negotiate client ID
6553  * @clp: state data structure
6554  * @program: RPC program for NFSv4 callback service
6555  * @port: IP port number for NFS4 callback service
6556  * @cred: credential to use for this call
6557  * @res: where to place the result
6558  *
6559  * Returns zero, a negative errno, or a negative NFS4ERR status code.
6560  */
nfs4_proc_setclientid(struct nfs_client * clp,u32 program,unsigned short port,const struct cred * cred,struct nfs4_setclientid_res * res)6561 int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
6562 		unsigned short port, const struct cred *cred,
6563 		struct nfs4_setclientid_res *res)
6564 {
6565 	nfs4_verifier sc_verifier;
6566 	struct nfs4_setclientid setclientid = {
6567 		.sc_verifier = &sc_verifier,
6568 		.sc_prog = program,
6569 		.sc_clnt = clp,
6570 	};
6571 	struct rpc_message msg = {
6572 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6573 		.rpc_argp = &setclientid,
6574 		.rpc_resp = res,
6575 		.rpc_cred = cred,
6576 	};
6577 	struct rpc_task_setup task_setup_data = {
6578 		.rpc_client = clp->cl_rpcclient,
6579 		.rpc_message = &msg,
6580 		.callback_ops = &nfs4_setclientid_ops,
6581 		.callback_data = &setclientid,
6582 		.flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
6583 	};
6584 	unsigned long now = jiffies;
6585 	int status;
6586 
6587 	/* nfs_client_id4 */
6588 	nfs4_init_boot_verifier(clp, &sc_verifier);
6589 
6590 	if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6591 		status = nfs4_init_uniform_client_string(clp);
6592 	else
6593 		status = nfs4_init_nonuniform_client_string(clp);
6594 
6595 	if (status)
6596 		goto out;
6597 
6598 	/* cb_client4 */
6599 	setclientid.sc_netid_len =
6600 				nfs4_init_callback_netid(clp,
6601 						setclientid.sc_netid,
6602 						sizeof(setclientid.sc_netid));
6603 	setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
6604 				sizeof(setclientid.sc_uaddr), "%s.%u.%u",
6605 				clp->cl_ipaddr, port >> 8, port & 255);
6606 
6607 	dprintk("NFS call  setclientid auth=%s, '%s'\n",
6608 		clp->cl_rpcclient->cl_auth->au_ops->au_name,
6609 		clp->cl_owner_id);
6610 
6611 	status = nfs4_call_sync_custom(&task_setup_data);
6612 	if (setclientid.sc_cred) {
6613 		kfree(clp->cl_acceptor);
6614 		clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6615 		put_rpccred(setclientid.sc_cred);
6616 	}
6617 
6618 	if (status == 0)
6619 		do_renew_lease(clp, now);
6620 out:
6621 	trace_nfs4_setclientid(clp, status);
6622 	dprintk("NFS reply setclientid: %d\n", status);
6623 	return status;
6624 }
6625 
6626 /**
6627  * nfs4_proc_setclientid_confirm - Confirm client ID
6628  * @clp: state data structure
6629  * @arg: result of a previous SETCLIENTID
6630  * @cred: credential to use for this call
6631  *
6632  * Returns zero, a negative errno, or a negative NFS4ERR status code.
6633  */
nfs4_proc_setclientid_confirm(struct nfs_client * clp,struct nfs4_setclientid_res * arg,const struct cred * cred)6634 int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
6635 		struct nfs4_setclientid_res *arg,
6636 		const struct cred *cred)
6637 {
6638 	struct rpc_message msg = {
6639 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
6640 		.rpc_argp = arg,
6641 		.rpc_cred = cred,
6642 	};
6643 	int status;
6644 
6645 	dprintk("NFS call  setclientid_confirm auth=%s, (client ID %llx)\n",
6646 		clp->cl_rpcclient->cl_auth->au_ops->au_name,
6647 		clp->cl_clientid);
6648 	status = rpc_call_sync(clp->cl_rpcclient, &msg,
6649 			       RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
6650 	trace_nfs4_setclientid_confirm(clp, status);
6651 	dprintk("NFS reply setclientid_confirm: %d\n", status);
6652 	return status;
6653 }
6654 
6655 struct nfs4_delegreturndata {
6656 	struct nfs4_delegreturnargs args;
6657 	struct nfs4_delegreturnres res;
6658 	struct nfs_fh fh;
6659 	nfs4_stateid stateid;
6660 	unsigned long timestamp;
6661 	struct {
6662 		struct nfs4_layoutreturn_args arg;
6663 		struct nfs4_layoutreturn_res res;
6664 		struct nfs4_xdr_opaque_data ld_private;
6665 		u32 roc_barrier;
6666 		bool roc;
6667 	} lr;
6668 	struct nfs4_delegattr sattr;
6669 	struct nfs_fattr fattr;
6670 	int rpc_status;
6671 	struct inode *inode;
6672 };
6673 
nfs4_delegreturn_done(struct rpc_task * task,void * calldata)6674 static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6675 {
6676 	struct nfs4_delegreturndata *data = calldata;
6677 	struct nfs4_exception exception = {
6678 		.inode = data->inode,
6679 		.stateid = &data->stateid,
6680 		.task_is_privileged = data->args.seq_args.sa_privileged,
6681 	};
6682 
6683 	if (!nfs4_sequence_done(task, &data->res.seq_res))
6684 		return;
6685 
6686 	trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
6687 
6688 	/* Handle Layoutreturn errors */
6689 	if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res,
6690 			  &data->res.lr_ret) == -EAGAIN)
6691 		goto out_restart;
6692 
6693 	if (data->args.sattr_args && task->tk_status != 0) {
6694 		switch(data->res.sattr_ret) {
6695 		case 0:
6696 			data->args.sattr_args = NULL;
6697 			data->res.sattr_res = false;
6698 			break;
6699 		case -NFS4ERR_ADMIN_REVOKED:
6700 		case -NFS4ERR_DELEG_REVOKED:
6701 		case -NFS4ERR_EXPIRED:
6702 		case -NFS4ERR_BAD_STATEID:
6703 			/* Let the main handler below do stateid recovery */
6704 			break;
6705 		case -NFS4ERR_OLD_STATEID:
6706 			if (nfs4_refresh_delegation_stateid(&data->stateid,
6707 						data->inode))
6708 				goto out_restart;
6709 			fallthrough;
6710 		default:
6711 			data->args.sattr_args = NULL;
6712 			data->res.sattr_res = false;
6713 			goto out_restart;
6714 		}
6715 	}
6716 
6717 	switch (task->tk_status) {
6718 	case 0:
6719 		renew_lease(data->res.server, data->timestamp);
6720 		break;
6721 	case -NFS4ERR_ADMIN_REVOKED:
6722 	case -NFS4ERR_DELEG_REVOKED:
6723 	case -NFS4ERR_EXPIRED:
6724 		nfs4_free_revoked_stateid(data->res.server,
6725 				data->args.stateid,
6726 				task->tk_msg.rpc_cred);
6727 		fallthrough;
6728 	case -NFS4ERR_BAD_STATEID:
6729 	case -NFS4ERR_STALE_STATEID:
6730 	case -ETIMEDOUT:
6731 		task->tk_status = 0;
6732 		break;
6733 	case -NFS4ERR_OLD_STATEID:
6734 		if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6735 			nfs4_stateid_seqid_inc(&data->stateid);
6736 		if (data->args.bitmask) {
6737 			data->args.bitmask = NULL;
6738 			data->res.fattr = NULL;
6739 		}
6740 		goto out_restart;
6741 	case -NFS4ERR_ACCESS:
6742 		if (data->args.bitmask) {
6743 			data->args.bitmask = NULL;
6744 			data->res.fattr = NULL;
6745 			goto out_restart;
6746 		}
6747 		fallthrough;
6748 	default:
6749 		task->tk_status = nfs4_async_handle_exception(task,
6750 				data->res.server, task->tk_status,
6751 				&exception);
6752 		if (exception.retry)
6753 			goto out_restart;
6754 	}
6755 	nfs_delegation_mark_returned(data->inode, data->args.stateid);
6756 	data->rpc_status = task->tk_status;
6757 	return;
6758 out_restart:
6759 	task->tk_status = 0;
6760 	rpc_restart_call_prepare(task);
6761 }
6762 
nfs4_delegreturn_release(void * calldata)6763 static void nfs4_delegreturn_release(void *calldata)
6764 {
6765 	struct nfs4_delegreturndata *data = calldata;
6766 	struct inode *inode = data->inode;
6767 
6768 	if (data->lr.roc)
6769 		pnfs_roc_release(&data->lr.arg, &data->lr.res,
6770 				 data->res.lr_ret);
6771 	if (inode) {
6772 		nfs4_fattr_set_prechange(&data->fattr,
6773 					 inode_peek_iversion_raw(inode));
6774 		nfs_refresh_inode(inode, &data->fattr);
6775 		nfs_iput_and_deactive(inode);
6776 	}
6777 	kfree(calldata);
6778 }
6779 
nfs4_delegreturn_prepare(struct rpc_task * task,void * data)6780 static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6781 {
6782 	struct nfs4_delegreturndata *d_data;
6783 	struct pnfs_layout_hdr *lo;
6784 
6785 	d_data = data;
6786 
6787 	if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6788 		nfs4_sequence_done(task, &d_data->res.seq_res);
6789 		return;
6790 	}
6791 
6792 	lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6793 	if (lo && !pnfs_layout_is_valid(lo)) {
6794 		d_data->args.lr_args = NULL;
6795 		d_data->res.lr_res = NULL;
6796 	}
6797 
6798 	nfs4_setup_sequence(d_data->res.server->nfs_client,
6799 			&d_data->args.seq_args,
6800 			&d_data->res.seq_res,
6801 			task);
6802 }
6803 
6804 static const struct rpc_call_ops nfs4_delegreturn_ops = {
6805 	.rpc_call_prepare = nfs4_delegreturn_prepare,
6806 	.rpc_call_done = nfs4_delegreturn_done,
6807 	.rpc_release = nfs4_delegreturn_release,
6808 };
6809 
_nfs4_proc_delegreturn(struct inode * inode,const struct cred * cred,const nfs4_stateid * stateid,struct nfs_delegation * delegation,int issync)6810 static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred,
6811 				  const nfs4_stateid *stateid,
6812 				  struct nfs_delegation *delegation,
6813 				  int issync)
6814 {
6815 	struct nfs4_delegreturndata *data;
6816 	struct nfs_server *server = NFS_SERVER(inode);
6817 	struct rpc_task *task;
6818 	struct rpc_message msg = {
6819 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6820 		.rpc_cred = cred,
6821 	};
6822 	struct rpc_task_setup task_setup_data = {
6823 		.rpc_client = server->client,
6824 		.rpc_message = &msg,
6825 		.callback_ops = &nfs4_delegreturn_ops,
6826 		.flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
6827 	};
6828 	int status = 0;
6829 
6830 	if (nfs_server_capable(inode, NFS_CAP_MOVEABLE))
6831 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
6832 
6833 	data = kzalloc(sizeof(*data), GFP_KERNEL);
6834 	if (data == NULL)
6835 		return -ENOMEM;
6836 
6837 	nfs4_state_protect(server->nfs_client,
6838 			NFS_SP4_MACH_CRED_CLEANUP,
6839 			&task_setup_data.rpc_client, &msg);
6840 
6841 	data->args.fhandle = &data->fh;
6842 	data->args.stateid = &data->stateid;
6843 	nfs4_bitmask_set(data->args.bitmask_store,
6844 			 server->cache_consistency_bitmask, inode, 0);
6845 	data->args.bitmask = data->args.bitmask_store;
6846 	nfs_copy_fh(&data->fh, NFS_FH(inode));
6847 	nfs4_stateid_copy(&data->stateid, stateid);
6848 	data->res.fattr = &data->fattr;
6849 	data->res.server = server;
6850 	data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6851 	data->lr.arg.ld_private = &data->lr.ld_private;
6852 	nfs_fattr_init(data->res.fattr);
6853 	data->timestamp = jiffies;
6854 	data->rpc_status = 0;
6855 	data->inode = nfs_igrab_and_active(inode);
6856 	if (data->inode || issync) {
6857 		data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
6858 					cred);
6859 		if (data->lr.roc) {
6860 			data->args.lr_args = &data->lr.arg;
6861 			data->res.lr_res = &data->lr.res;
6862 		}
6863 	}
6864 
6865 	if (delegation &&
6866 	    test_bit(NFS_DELEGATION_DELEGTIME, &delegation->flags)) {
6867 		if (delegation->type & FMODE_READ) {
6868 			data->sattr.atime = inode_get_atime(inode);
6869 			data->sattr.atime_set = true;
6870 		}
6871 		if (delegation->type & FMODE_WRITE) {
6872 			data->sattr.mtime = inode_get_mtime(inode);
6873 			data->sattr.mtime_set = true;
6874 		}
6875 		data->args.sattr_args = &data->sattr;
6876 		data->res.sattr_res = true;
6877 	}
6878 
6879 	if (!data->inode)
6880 		nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6881 				   1);
6882 	else
6883 		nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1,
6884 				   0);
6885 
6886 	task_setup_data.callback_data = data;
6887 	msg.rpc_argp = &data->args;
6888 	msg.rpc_resp = &data->res;
6889 	task = rpc_run_task(&task_setup_data);
6890 	if (IS_ERR(task))
6891 		return PTR_ERR(task);
6892 	if (!issync)
6893 		goto out;
6894 	status = rpc_wait_for_completion_task(task);
6895 	if (status != 0)
6896 		goto out;
6897 	status = data->rpc_status;
6898 out:
6899 	rpc_put_task(task);
6900 	return status;
6901 }
6902 
nfs4_proc_delegreturn(struct inode * inode,const struct cred * cred,const nfs4_stateid * stateid,struct nfs_delegation * delegation,int issync)6903 int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred,
6904 			  const nfs4_stateid *stateid,
6905 			  struct nfs_delegation *delegation, int issync)
6906 {
6907 	struct nfs_server *server = NFS_SERVER(inode);
6908 	struct nfs4_exception exception = { };
6909 	int err;
6910 	do {
6911 		err = _nfs4_proc_delegreturn(inode, cred, stateid,
6912 					     delegation, issync);
6913 		trace_nfs4_delegreturn(inode, stateid, err);
6914 		switch (err) {
6915 			case -NFS4ERR_STALE_STATEID:
6916 			case -NFS4ERR_EXPIRED:
6917 			case 0:
6918 				return 0;
6919 		}
6920 		err = nfs4_handle_exception(server, err, &exception);
6921 	} while (exception.retry);
6922 	return err;
6923 }
6924 
_nfs4_proc_getlk(struct nfs4_state * state,int cmd,struct file_lock * request)6925 static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6926 {
6927 	struct inode *inode = state->inode;
6928 	struct nfs_server *server = NFS_SERVER(inode);
6929 	struct nfs_client *clp = server->nfs_client;
6930 	struct nfs_lockt_args arg = {
6931 		.fh = NFS_FH(inode),
6932 		.fl = request,
6933 	};
6934 	struct nfs_lockt_res res = {
6935 		.denied = request,
6936 	};
6937 	struct rpc_message msg = {
6938 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
6939 		.rpc_argp	= &arg,
6940 		.rpc_resp	= &res,
6941 		.rpc_cred	= state->owner->so_cred,
6942 	};
6943 	struct nfs4_lock_state *lsp;
6944 	int status;
6945 
6946 	arg.lock_owner.clientid = clp->cl_clientid;
6947 	status = nfs4_set_lock_state(state, request);
6948 	if (status != 0)
6949 		goto out;
6950 	lsp = request->fl_u.nfs4_fl.owner;
6951 	arg.lock_owner.id = lsp->ls_seqid.owner_id;
6952 	arg.lock_owner.s_dev = server->s_dev;
6953 	status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
6954 	switch (status) {
6955 		case 0:
6956 			request->c.flc_type = F_UNLCK;
6957 			break;
6958 		case -NFS4ERR_DENIED:
6959 			status = 0;
6960 	}
6961 	request->fl_ops->fl_release_private(request);
6962 	request->fl_ops = NULL;
6963 out:
6964 	return status;
6965 }
6966 
nfs4_proc_getlk(struct nfs4_state * state,int cmd,struct file_lock * request)6967 static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6968 {
6969 	struct nfs4_exception exception = {
6970 		.interruptible = true,
6971 	};
6972 	int err;
6973 
6974 	do {
6975 		err = _nfs4_proc_getlk(state, cmd, request);
6976 		trace_nfs4_get_lock(request, state, cmd, err);
6977 		err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
6978 				&exception);
6979 	} while (exception.retry);
6980 	return err;
6981 }
6982 
6983 /*
6984  * Update the seqid of a lock stateid after receiving
6985  * NFS4ERR_OLD_STATEID
6986  */
nfs4_refresh_lock_old_stateid(nfs4_stateid * dst,struct nfs4_lock_state * lsp)6987 static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6988 		struct nfs4_lock_state *lsp)
6989 {
6990 	struct nfs4_state *state = lsp->ls_state;
6991 	bool ret = false;
6992 
6993 	spin_lock(&state->state_lock);
6994 	if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6995 		goto out;
6996 	if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6997 		nfs4_stateid_seqid_inc(dst);
6998 	else
6999 		dst->seqid = lsp->ls_stateid.seqid;
7000 	ret = true;
7001 out:
7002 	spin_unlock(&state->state_lock);
7003 	return ret;
7004 }
7005 
nfs4_sync_lock_stateid(nfs4_stateid * dst,struct nfs4_lock_state * lsp)7006 static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
7007 		struct nfs4_lock_state *lsp)
7008 {
7009 	struct nfs4_state *state = lsp->ls_state;
7010 	bool ret;
7011 
7012 	spin_lock(&state->state_lock);
7013 	ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
7014 	nfs4_stateid_copy(dst, &lsp->ls_stateid);
7015 	spin_unlock(&state->state_lock);
7016 	return ret;
7017 }
7018 
7019 struct nfs4_unlockdata {
7020 	struct nfs_locku_args arg;
7021 	struct nfs_locku_res res;
7022 	struct nfs4_lock_state *lsp;
7023 	struct nfs_open_context *ctx;
7024 	struct nfs_lock_context *l_ctx;
7025 	struct file_lock fl;
7026 	struct nfs_server *server;
7027 	unsigned long timestamp;
7028 };
7029 
nfs4_alloc_unlockdata(struct file_lock * fl,struct nfs_open_context * ctx,struct nfs4_lock_state * lsp,struct nfs_seqid * seqid)7030 static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
7031 		struct nfs_open_context *ctx,
7032 		struct nfs4_lock_state *lsp,
7033 		struct nfs_seqid *seqid)
7034 {
7035 	struct nfs4_unlockdata *p;
7036 	struct nfs4_state *state = lsp->ls_state;
7037 	struct inode *inode = state->inode;
7038 
7039 	p = kzalloc(sizeof(*p), GFP_KERNEL);
7040 	if (p == NULL)
7041 		return NULL;
7042 	p->arg.fh = NFS_FH(inode);
7043 	p->arg.fl = &p->fl;
7044 	p->arg.seqid = seqid;
7045 	p->res.seqid = seqid;
7046 	p->lsp = lsp;
7047 	/* Ensure we don't close file until we're done freeing locks! */
7048 	p->ctx = get_nfs_open_context(ctx);
7049 	p->l_ctx = nfs_get_lock_context(ctx);
7050 	locks_init_lock(&p->fl);
7051 	locks_copy_lock(&p->fl, fl);
7052 	p->server = NFS_SERVER(inode);
7053 	spin_lock(&state->state_lock);
7054 	nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
7055 	spin_unlock(&state->state_lock);
7056 	return p;
7057 }
7058 
nfs4_locku_release_calldata(void * data)7059 static void nfs4_locku_release_calldata(void *data)
7060 {
7061 	struct nfs4_unlockdata *calldata = data;
7062 	nfs_free_seqid(calldata->arg.seqid);
7063 	nfs4_put_lock_state(calldata->lsp);
7064 	nfs_put_lock_context(calldata->l_ctx);
7065 	put_nfs_open_context(calldata->ctx);
7066 	kfree(calldata);
7067 }
7068 
nfs4_locku_done(struct rpc_task * task,void * data)7069 static void nfs4_locku_done(struct rpc_task *task, void *data)
7070 {
7071 	struct nfs4_unlockdata *calldata = data;
7072 	struct nfs4_exception exception = {
7073 		.inode = calldata->lsp->ls_state->inode,
7074 		.stateid = &calldata->arg.stateid,
7075 	};
7076 
7077 	if (!nfs4_sequence_done(task, &calldata->res.seq_res))
7078 		return;
7079 	switch (task->tk_status) {
7080 		case 0:
7081 			renew_lease(calldata->server, calldata->timestamp);
7082 			locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
7083 			if (nfs4_update_lock_stateid(calldata->lsp,
7084 					&calldata->res.stateid))
7085 				break;
7086 			fallthrough;
7087 		case -NFS4ERR_ADMIN_REVOKED:
7088 		case -NFS4ERR_EXPIRED:
7089 			nfs4_free_revoked_stateid(calldata->server,
7090 					&calldata->arg.stateid,
7091 					task->tk_msg.rpc_cred);
7092 			fallthrough;
7093 		case -NFS4ERR_BAD_STATEID:
7094 		case -NFS4ERR_STALE_STATEID:
7095 			if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
7096 						calldata->lsp))
7097 				rpc_restart_call_prepare(task);
7098 			break;
7099 		case -NFS4ERR_OLD_STATEID:
7100 			if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
7101 						calldata->lsp))
7102 				rpc_restart_call_prepare(task);
7103 			break;
7104 		default:
7105 			task->tk_status = nfs4_async_handle_exception(task,
7106 					calldata->server, task->tk_status,
7107 					&exception);
7108 			if (exception.retry)
7109 				rpc_restart_call_prepare(task);
7110 	}
7111 	nfs_release_seqid(calldata->arg.seqid);
7112 }
7113 
nfs4_locku_prepare(struct rpc_task * task,void * data)7114 static void nfs4_locku_prepare(struct rpc_task *task, void *data)
7115 {
7116 	struct nfs4_unlockdata *calldata = data;
7117 
7118 	if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
7119 		nfs_async_iocounter_wait(task, calldata->l_ctx))
7120 		return;
7121 
7122 	if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
7123 		goto out_wait;
7124 	if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
7125 		/* Note: exit _without_ running nfs4_locku_done */
7126 		goto out_no_action;
7127 	}
7128 	calldata->timestamp = jiffies;
7129 	if (nfs4_setup_sequence(calldata->server->nfs_client,
7130 				&calldata->arg.seq_args,
7131 				&calldata->res.seq_res,
7132 				task) != 0)
7133 		nfs_release_seqid(calldata->arg.seqid);
7134 	return;
7135 out_no_action:
7136 	task->tk_action = NULL;
7137 out_wait:
7138 	nfs4_sequence_done(task, &calldata->res.seq_res);
7139 }
7140 
7141 static const struct rpc_call_ops nfs4_locku_ops = {
7142 	.rpc_call_prepare = nfs4_locku_prepare,
7143 	.rpc_call_done = nfs4_locku_done,
7144 	.rpc_release = nfs4_locku_release_calldata,
7145 };
7146 
nfs4_do_unlck(struct file_lock * fl,struct nfs_open_context * ctx,struct nfs4_lock_state * lsp,struct nfs_seqid * seqid)7147 static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
7148 		struct nfs_open_context *ctx,
7149 		struct nfs4_lock_state *lsp,
7150 		struct nfs_seqid *seqid)
7151 {
7152 	struct nfs4_unlockdata *data;
7153 	struct rpc_message msg = {
7154 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
7155 		.rpc_cred = ctx->cred,
7156 	};
7157 	struct rpc_task_setup task_setup_data = {
7158 		.rpc_client = NFS_CLIENT(lsp->ls_state->inode),
7159 		.rpc_message = &msg,
7160 		.callback_ops = &nfs4_locku_ops,
7161 		.workqueue = nfsiod_workqueue,
7162 		.flags = RPC_TASK_ASYNC,
7163 	};
7164 
7165 	if (nfs_server_capable(lsp->ls_state->inode, NFS_CAP_MOVEABLE))
7166 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
7167 
7168 	nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
7169 		NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
7170 
7171 	/* Ensure this is an unlock - when canceling a lock, the
7172 	 * canceled lock is passed in, and it won't be an unlock.
7173 	 */
7174 	fl->c.flc_type = F_UNLCK;
7175 	if (fl->c.flc_flags & FL_CLOSE)
7176 		set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
7177 
7178 	data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
7179 	if (data == NULL) {
7180 		nfs_free_seqid(seqid);
7181 		return ERR_PTR(-ENOMEM);
7182 	}
7183 
7184 	nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
7185 	msg.rpc_argp = &data->arg;
7186 	msg.rpc_resp = &data->res;
7187 	task_setup_data.callback_data = data;
7188 	return rpc_run_task(&task_setup_data);
7189 }
7190 
nfs4_proc_unlck(struct nfs4_state * state,int cmd,struct file_lock * request)7191 static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
7192 {
7193 	struct inode *inode = state->inode;
7194 	struct nfs4_state_owner *sp = state->owner;
7195 	struct nfs_inode *nfsi = NFS_I(inode);
7196 	struct nfs_seqid *seqid;
7197 	struct nfs4_lock_state *lsp;
7198 	struct rpc_task *task;
7199 	struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7200 	int status = 0;
7201 	unsigned char saved_flags = request->c.flc_flags;
7202 
7203 	status = nfs4_set_lock_state(state, request);
7204 	/* Unlock _before_ we do the RPC call */
7205 	request->c.flc_flags |= FL_EXISTS;
7206 	/* Exclude nfs_delegation_claim_locks() */
7207 	mutex_lock(&sp->so_delegreturn_mutex);
7208 	/* Exclude nfs4_reclaim_open_stateid() - note nesting! */
7209 	down_read(&nfsi->rwsem);
7210 	if (locks_lock_inode_wait(inode, request) == -ENOENT) {
7211 		up_read(&nfsi->rwsem);
7212 		mutex_unlock(&sp->so_delegreturn_mutex);
7213 		goto out;
7214 	}
7215 	lsp = request->fl_u.nfs4_fl.owner;
7216 	set_bit(NFS_LOCK_UNLOCKING, &lsp->ls_flags);
7217 	up_read(&nfsi->rwsem);
7218 	mutex_unlock(&sp->so_delegreturn_mutex);
7219 	if (status != 0)
7220 		goto out;
7221 	/* Is this a delegated lock? */
7222 	if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
7223 		goto out;
7224 	alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
7225 	seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
7226 	status = -ENOMEM;
7227 	if (IS_ERR(seqid))
7228 		goto out;
7229 	task = nfs4_do_unlck(request,
7230 			     nfs_file_open_context(request->c.flc_file),
7231 			     lsp, seqid);
7232 	status = PTR_ERR(task);
7233 	if (IS_ERR(task))
7234 		goto out;
7235 	status = rpc_wait_for_completion_task(task);
7236 	rpc_put_task(task);
7237 out:
7238 	request->c.flc_flags = saved_flags;
7239 	trace_nfs4_unlock(request, state, F_SETLK, status);
7240 	return status;
7241 }
7242 
7243 struct nfs4_lockdata {
7244 	struct nfs_lock_args arg;
7245 	struct nfs_lock_res res;
7246 	struct nfs4_lock_state *lsp;
7247 	struct nfs_open_context *ctx;
7248 	struct file_lock fl;
7249 	unsigned long timestamp;
7250 	int rpc_status;
7251 	int cancelled;
7252 	struct nfs_server *server;
7253 };
7254 
nfs4_alloc_lockdata(struct file_lock * fl,struct nfs_open_context * ctx,struct nfs4_lock_state * lsp,gfp_t gfp_mask)7255 static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
7256 		struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
7257 		gfp_t gfp_mask)
7258 {
7259 	struct nfs4_lockdata *p;
7260 	struct inode *inode = lsp->ls_state->inode;
7261 	struct nfs_server *server = NFS_SERVER(inode);
7262 	struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
7263 
7264 	p = kzalloc(sizeof(*p), gfp_mask);
7265 	if (p == NULL)
7266 		return NULL;
7267 
7268 	p->arg.fh = NFS_FH(inode);
7269 	p->arg.fl = &p->fl;
7270 	p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
7271 	if (IS_ERR(p->arg.open_seqid))
7272 		goto out_free;
7273 	alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
7274 	p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
7275 	if (IS_ERR(p->arg.lock_seqid))
7276 		goto out_free_seqid;
7277 	p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
7278 	p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
7279 	p->arg.lock_owner.s_dev = server->s_dev;
7280 	p->res.lock_seqid = p->arg.lock_seqid;
7281 	p->lsp = lsp;
7282 	p->server = server;
7283 	p->ctx = get_nfs_open_context(ctx);
7284 	locks_init_lock(&p->fl);
7285 	locks_copy_lock(&p->fl, fl);
7286 	return p;
7287 out_free_seqid:
7288 	nfs_free_seqid(p->arg.open_seqid);
7289 out_free:
7290 	kfree(p);
7291 	return NULL;
7292 }
7293 
nfs4_lock_prepare(struct rpc_task * task,void * calldata)7294 static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
7295 {
7296 	struct nfs4_lockdata *data = calldata;
7297 	struct nfs4_state *state = data->lsp->ls_state;
7298 
7299 	if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
7300 		goto out_wait;
7301 	/* Do we need to do an open_to_lock_owner? */
7302 	if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
7303 		if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
7304 			goto out_release_lock_seqid;
7305 		}
7306 		nfs4_stateid_copy(&data->arg.open_stateid,
7307 				&state->open_stateid);
7308 		data->arg.new_lock_owner = 1;
7309 		data->res.open_seqid = data->arg.open_seqid;
7310 	} else {
7311 		data->arg.new_lock_owner = 0;
7312 		nfs4_stateid_copy(&data->arg.lock_stateid,
7313 				&data->lsp->ls_stateid);
7314 	}
7315 	if (!nfs4_valid_open_stateid(state)) {
7316 		data->rpc_status = -EBADF;
7317 		task->tk_action = NULL;
7318 		goto out_release_open_seqid;
7319 	}
7320 	data->timestamp = jiffies;
7321 	if (nfs4_setup_sequence(data->server->nfs_client,
7322 				&data->arg.seq_args,
7323 				&data->res.seq_res,
7324 				task) == 0)
7325 		return;
7326 out_release_open_seqid:
7327 	nfs_release_seqid(data->arg.open_seqid);
7328 out_release_lock_seqid:
7329 	nfs_release_seqid(data->arg.lock_seqid);
7330 out_wait:
7331 	nfs4_sequence_done(task, &data->res.seq_res);
7332 	dprintk("%s: ret = %d\n", __func__, data->rpc_status);
7333 }
7334 
nfs4_lock_done(struct rpc_task * task,void * calldata)7335 static void nfs4_lock_done(struct rpc_task *task, void *calldata)
7336 {
7337 	struct nfs4_lockdata *data = calldata;
7338 	struct nfs4_lock_state *lsp = data->lsp;
7339 
7340 	if (!nfs4_sequence_done(task, &data->res.seq_res))
7341 		return;
7342 
7343 	data->rpc_status = task->tk_status;
7344 	switch (task->tk_status) {
7345 	case 0:
7346 		renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
7347 				data->timestamp);
7348 		if (data->arg.new_lock && !data->cancelled) {
7349 			data->fl.c.flc_flags &= ~(FL_SLEEP | FL_ACCESS);
7350 			if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
7351 				goto out_restart;
7352 		}
7353 		if (data->arg.new_lock_owner != 0) {
7354 			nfs_confirm_seqid(&lsp->ls_seqid, 0);
7355 			nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
7356 			set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
7357 		} else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
7358 			goto out_restart;
7359 		break;
7360 	case -NFS4ERR_OLD_STATEID:
7361 		if (data->arg.new_lock_owner != 0 &&
7362 			nfs4_refresh_open_old_stateid(&data->arg.open_stateid,
7363 					lsp->ls_state))
7364 			goto out_restart;
7365 		if (nfs4_refresh_lock_old_stateid(&data->arg.lock_stateid, lsp))
7366 			goto out_restart;
7367 		fallthrough;
7368 	case -NFS4ERR_BAD_STATEID:
7369 	case -NFS4ERR_STALE_STATEID:
7370 	case -NFS4ERR_EXPIRED:
7371 		if (data->arg.new_lock_owner != 0) {
7372 			if (!nfs4_stateid_match(&data->arg.open_stateid,
7373 						&lsp->ls_state->open_stateid))
7374 				goto out_restart;
7375 		} else if (!nfs4_stateid_match(&data->arg.lock_stateid,
7376 						&lsp->ls_stateid))
7377 				goto out_restart;
7378 	}
7379 out_done:
7380 	dprintk("%s: ret = %d!\n", __func__, data->rpc_status);
7381 	return;
7382 out_restart:
7383 	if (!data->cancelled)
7384 		rpc_restart_call_prepare(task);
7385 	goto out_done;
7386 }
7387 
nfs4_lock_release(void * calldata)7388 static void nfs4_lock_release(void *calldata)
7389 {
7390 	struct nfs4_lockdata *data = calldata;
7391 
7392 	nfs_free_seqid(data->arg.open_seqid);
7393 	if (data->cancelled && data->rpc_status == 0) {
7394 		struct rpc_task *task;
7395 		task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
7396 				data->arg.lock_seqid);
7397 		if (!IS_ERR(task))
7398 			rpc_put_task_async(task);
7399 		dprintk("%s: cancelling lock!\n", __func__);
7400 	} else
7401 		nfs_free_seqid(data->arg.lock_seqid);
7402 	nfs4_put_lock_state(data->lsp);
7403 	put_nfs_open_context(data->ctx);
7404 	kfree(data);
7405 }
7406 
7407 static const struct rpc_call_ops nfs4_lock_ops = {
7408 	.rpc_call_prepare = nfs4_lock_prepare,
7409 	.rpc_call_done = nfs4_lock_done,
7410 	.rpc_release = nfs4_lock_release,
7411 };
7412 
nfs4_handle_setlk_error(struct nfs_server * server,struct nfs4_lock_state * lsp,int new_lock_owner,int error)7413 static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
7414 {
7415 	switch (error) {
7416 	case -NFS4ERR_ADMIN_REVOKED:
7417 	case -NFS4ERR_EXPIRED:
7418 	case -NFS4ERR_BAD_STATEID:
7419 		lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7420 		if (new_lock_owner != 0 ||
7421 		   test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
7422 			nfs4_schedule_stateid_recovery(server, lsp->ls_state);
7423 		break;
7424 	case -NFS4ERR_STALE_STATEID:
7425 		lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
7426 		nfs4_schedule_lease_recovery(server->nfs_client);
7427 	}
7428 }
7429 
_nfs4_do_setlk(struct nfs4_state * state,int cmd,struct file_lock * fl,int recovery_type)7430 static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
7431 {
7432 	struct nfs4_lockdata *data;
7433 	struct rpc_task *task;
7434 	struct rpc_message msg = {
7435 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
7436 		.rpc_cred = state->owner->so_cred,
7437 	};
7438 	struct rpc_task_setup task_setup_data = {
7439 		.rpc_client = NFS_CLIENT(state->inode),
7440 		.rpc_message = &msg,
7441 		.callback_ops = &nfs4_lock_ops,
7442 		.workqueue = nfsiod_workqueue,
7443 		.flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
7444 	};
7445 	int ret;
7446 
7447 	if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE))
7448 		task_setup_data.flags |= RPC_TASK_MOVEABLE;
7449 
7450 	data = nfs4_alloc_lockdata(fl,
7451 				   nfs_file_open_context(fl->c.flc_file),
7452 				   fl->fl_u.nfs4_fl.owner, GFP_KERNEL);
7453 	if (data == NULL)
7454 		return -ENOMEM;
7455 	if (IS_SETLKW(cmd))
7456 		data->arg.block = 1;
7457 	nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
7458 				recovery_type > NFS_LOCK_NEW);
7459 	msg.rpc_argp = &data->arg;
7460 	msg.rpc_resp = &data->res;
7461 	task_setup_data.callback_data = data;
7462 	if (recovery_type > NFS_LOCK_NEW) {
7463 		if (recovery_type == NFS_LOCK_RECLAIM)
7464 			data->arg.reclaim = NFS_LOCK_RECLAIM;
7465 	} else
7466 		data->arg.new_lock = 1;
7467 	task = rpc_run_task(&task_setup_data);
7468 	if (IS_ERR(task))
7469 		return PTR_ERR(task);
7470 	ret = rpc_wait_for_completion_task(task);
7471 	if (ret == 0) {
7472 		ret = data->rpc_status;
7473 		if (ret)
7474 			nfs4_handle_setlk_error(data->server, data->lsp,
7475 					data->arg.new_lock_owner, ret);
7476 	} else
7477 		data->cancelled = true;
7478 	trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
7479 	rpc_put_task(task);
7480 	dprintk("%s: ret = %d\n", __func__, ret);
7481 	return ret;
7482 }
7483 
nfs4_lock_reclaim(struct nfs4_state * state,struct file_lock * request)7484 static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7485 {
7486 	struct nfs_server *server = NFS_SERVER(state->inode);
7487 	struct nfs4_exception exception = {
7488 		.inode = state->inode,
7489 	};
7490 	int err;
7491 
7492 	do {
7493 		/* Cache the lock if possible... */
7494 		if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7495 			return 0;
7496 		err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
7497 		if (err != -NFS4ERR_DELAY)
7498 			break;
7499 		nfs4_handle_exception(server, err, &exception);
7500 	} while (exception.retry);
7501 	return err;
7502 }
7503 
nfs4_lock_expired(struct nfs4_state * state,struct file_lock * request)7504 static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7505 {
7506 	struct nfs_server *server = NFS_SERVER(state->inode);
7507 	struct nfs4_exception exception = {
7508 		.inode = state->inode,
7509 	};
7510 	int err;
7511 
7512 	err = nfs4_set_lock_state(state, request);
7513 	if (err != 0)
7514 		return err;
7515 	if (!recover_lost_locks) {
7516 		set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7517 		return 0;
7518 	}
7519 	do {
7520 		if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7521 			return 0;
7522 		err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
7523 		switch (err) {
7524 		default:
7525 			goto out;
7526 		case -NFS4ERR_GRACE:
7527 		case -NFS4ERR_DELAY:
7528 			nfs4_handle_exception(server, err, &exception);
7529 			err = 0;
7530 		}
7531 	} while (exception.retry);
7532 out:
7533 	return err;
7534 }
7535 
7536 #if defined(CONFIG_NFS_V4_1)
nfs41_lock_expired(struct nfs4_state * state,struct file_lock * request)7537 static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7538 {
7539 	struct nfs4_lock_state *lsp;
7540 	int status;
7541 
7542 	status = nfs4_set_lock_state(state, request);
7543 	if (status != 0)
7544 		return status;
7545 	lsp = request->fl_u.nfs4_fl.owner;
7546 	if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7547 	    test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7548 		return 0;
7549 	return nfs4_lock_expired(state, request);
7550 }
7551 #endif
7552 
_nfs4_proc_setlk(struct nfs4_state * state,int cmd,struct file_lock * request)7553 static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7554 {
7555 	struct nfs_inode *nfsi = NFS_I(state->inode);
7556 	struct nfs4_state_owner *sp = state->owner;
7557 	unsigned char flags = request->c.flc_flags;
7558 	int status;
7559 
7560 	request->c.flc_flags |= FL_ACCESS;
7561 	status = locks_lock_inode_wait(state->inode, request);
7562 	if (status < 0)
7563 		goto out;
7564 	mutex_lock(&sp->so_delegreturn_mutex);
7565 	down_read(&nfsi->rwsem);
7566 	if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
7567 		/* Yes: cache locks! */
7568 		/* ...but avoid races with delegation recall... */
7569 		request->c.flc_flags = flags & ~FL_SLEEP;
7570 		status = locks_lock_inode_wait(state->inode, request);
7571 		up_read(&nfsi->rwsem);
7572 		mutex_unlock(&sp->so_delegreturn_mutex);
7573 		goto out;
7574 	}
7575 	up_read(&nfsi->rwsem);
7576 	mutex_unlock(&sp->so_delegreturn_mutex);
7577 	status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
7578 out:
7579 	request->c.flc_flags = flags;
7580 	return status;
7581 }
7582 
nfs4_proc_setlk(struct nfs4_state * state,int cmd,struct file_lock * request)7583 static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7584 {
7585 	struct nfs4_exception exception = {
7586 		.state = state,
7587 		.inode = state->inode,
7588 		.interruptible = true,
7589 	};
7590 	int err;
7591 
7592 	do {
7593 		err = _nfs4_proc_setlk(state, cmd, request);
7594 		if (err == -NFS4ERR_DENIED)
7595 			err = -EAGAIN;
7596 		err = nfs4_handle_exception(NFS_SERVER(state->inode),
7597 				err, &exception);
7598 	} while (exception.retry);
7599 	return err;
7600 }
7601 
7602 #define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7603 #define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7604 
7605 static int
nfs4_retry_setlk_simple(struct nfs4_state * state,int cmd,struct file_lock * request)7606 nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7607 			struct file_lock *request)
7608 {
7609 	int		status = -ERESTARTSYS;
7610 	unsigned long	timeout = NFS4_LOCK_MINTIMEOUT;
7611 
7612 	while(!signalled()) {
7613 		status = nfs4_proc_setlk(state, cmd, request);
7614 		if ((status != -EAGAIN) || IS_SETLK(cmd))
7615 			break;
7616 		__set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
7617 		schedule_timeout(timeout);
7618 		timeout *= 2;
7619 		timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7620 		status = -ERESTARTSYS;
7621 	}
7622 	return status;
7623 }
7624 
7625 #ifdef CONFIG_NFS_V4_1
7626 struct nfs4_lock_waiter {
7627 	struct inode		*inode;
7628 	struct nfs_lowner	owner;
7629 	wait_queue_entry_t	wait;
7630 };
7631 
7632 static int
nfs4_wake_lock_waiter(wait_queue_entry_t * wait,unsigned int mode,int flags,void * key)7633 nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
7634 {
7635 	struct nfs4_lock_waiter	*waiter	=
7636 		container_of(wait, struct nfs4_lock_waiter, wait);
7637 
7638 	/* NULL key means to wake up everyone */
7639 	if (key) {
7640 		struct cb_notify_lock_args	*cbnl = key;
7641 		struct nfs_lowner		*lowner = &cbnl->cbnl_owner,
7642 						*wowner = &waiter->owner;
7643 
7644 		/* Only wake if the callback was for the same owner. */
7645 		if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7646 			return 0;
7647 
7648 		/* Make sure it's for the right inode */
7649 		if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7650 			return 0;
7651 	}
7652 
7653 	return woken_wake_function(wait, mode, flags, key);
7654 }
7655 
7656 static int
nfs4_retry_setlk(struct nfs4_state * state,int cmd,struct file_lock * request)7657 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7658 {
7659 	struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7660 	struct nfs_server *server = NFS_SERVER(state->inode);
7661 	struct nfs_client *clp = server->nfs_client;
7662 	wait_queue_head_t *q = &clp->cl_lock_waitq;
7663 	struct nfs4_lock_waiter waiter = {
7664 		.inode = state->inode,
7665 		.owner = { .clientid = clp->cl_clientid,
7666 			   .id = lsp->ls_seqid.owner_id,
7667 			   .s_dev = server->s_dev },
7668 	};
7669 	int status;
7670 
7671 	/* Don't bother with waitqueue if we don't expect a callback */
7672 	if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7673 		return nfs4_retry_setlk_simple(state, cmd, request);
7674 
7675 	init_wait(&waiter.wait);
7676 	waiter.wait.func = nfs4_wake_lock_waiter;
7677 	add_wait_queue(q, &waiter.wait);
7678 
7679 	do {
7680 		status = nfs4_proc_setlk(state, cmd, request);
7681 		if (status != -EAGAIN || IS_SETLK(cmd))
7682 			break;
7683 
7684 		status = -ERESTARTSYS;
7685 		wait_woken(&waiter.wait, TASK_INTERRUPTIBLE|TASK_FREEZABLE,
7686 			   NFS4_LOCK_MAXTIMEOUT);
7687 	} while (!signalled());
7688 
7689 	remove_wait_queue(q, &waiter.wait);
7690 
7691 	return status;
7692 }
7693 #else /* !CONFIG_NFS_V4_1 */
7694 static inline int
nfs4_retry_setlk(struct nfs4_state * state,int cmd,struct file_lock * request)7695 nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7696 {
7697 	return nfs4_retry_setlk_simple(state, cmd, request);
7698 }
7699 #endif
7700 
7701 static int
nfs4_proc_lock(struct file * filp,int cmd,struct file_lock * request)7702 nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7703 {
7704 	struct nfs_open_context *ctx;
7705 	struct nfs4_state *state;
7706 	int status;
7707 
7708 	/* verify open state */
7709 	ctx = nfs_file_open_context(filp);
7710 	state = ctx->state;
7711 
7712 	if (IS_GETLK(cmd)) {
7713 		if (state != NULL)
7714 			return nfs4_proc_getlk(state, F_GETLK, request);
7715 		return 0;
7716 	}
7717 
7718 	if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7719 		return -EINVAL;
7720 
7721 	if (lock_is_unlock(request)) {
7722 		if (state != NULL)
7723 			return nfs4_proc_unlck(state, cmd, request);
7724 		return 0;
7725 	}
7726 
7727 	if (state == NULL)
7728 		return -ENOLCK;
7729 
7730 	if ((request->c.flc_flags & FL_POSIX) &&
7731 	    !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7732 		return -ENOLCK;
7733 
7734 	/*
7735 	 * Don't rely on the VFS having checked the file open mode,
7736 	 * since it won't do this for flock() locks.
7737 	 */
7738 	switch (request->c.flc_type) {
7739 	case F_RDLCK:
7740 		if (!(filp->f_mode & FMODE_READ))
7741 			return -EBADF;
7742 		break;
7743 	case F_WRLCK:
7744 		if (!(filp->f_mode & FMODE_WRITE))
7745 			return -EBADF;
7746 	}
7747 
7748 	status = nfs4_set_lock_state(state, request);
7749 	if (status != 0)
7750 		return status;
7751 
7752 	return nfs4_retry_setlk(state, cmd, request);
7753 }
7754 
nfs4_delete_lease(struct file * file,void ** priv)7755 static int nfs4_delete_lease(struct file *file, void **priv)
7756 {
7757 	return generic_setlease(file, F_UNLCK, NULL, priv);
7758 }
7759 
nfs4_add_lease(struct file * file,int arg,struct file_lease ** lease,void ** priv)7760 static int nfs4_add_lease(struct file *file, int arg, struct file_lease **lease,
7761 			  void **priv)
7762 {
7763 	struct inode *inode = file_inode(file);
7764 	fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE;
7765 	int ret;
7766 
7767 	/* No delegation, no lease */
7768 	if (!nfs4_have_delegation(inode, type, 0))
7769 		return -EAGAIN;
7770 	ret = generic_setlease(file, arg, lease, priv);
7771 	if (ret || nfs4_have_delegation(inode, type, 0))
7772 		return ret;
7773 	/* We raced with a delegation return */
7774 	nfs4_delete_lease(file, priv);
7775 	return -EAGAIN;
7776 }
7777 
nfs4_proc_setlease(struct file * file,int arg,struct file_lease ** lease,void ** priv)7778 int nfs4_proc_setlease(struct file *file, int arg, struct file_lease **lease,
7779 		       void **priv)
7780 {
7781 	switch (arg) {
7782 	case F_RDLCK:
7783 	case F_WRLCK:
7784 		return nfs4_add_lease(file, arg, lease, priv);
7785 	case F_UNLCK:
7786 		return nfs4_delete_lease(file, priv);
7787 	default:
7788 		return -EINVAL;
7789 	}
7790 }
7791 
nfs4_lock_delegation_recall(struct file_lock * fl,struct nfs4_state * state,const nfs4_stateid * stateid)7792 int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
7793 {
7794 	struct nfs_server *server = NFS_SERVER(state->inode);
7795 	int err;
7796 
7797 	err = nfs4_set_lock_state(state, fl);
7798 	if (err != 0)
7799 		return err;
7800 	do {
7801 		err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7802 		if (err != -NFS4ERR_DELAY)
7803 			break;
7804 		ssleep(1);
7805 	} while (err == -NFS4ERR_DELAY);
7806 	return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
7807 }
7808 
7809 struct nfs_release_lockowner_data {
7810 	struct nfs4_lock_state *lsp;
7811 	struct nfs_server *server;
7812 	struct nfs_release_lockowner_args args;
7813 	struct nfs_release_lockowner_res res;
7814 	unsigned long timestamp;
7815 };
7816 
nfs4_release_lockowner_prepare(struct rpc_task * task,void * calldata)7817 static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7818 {
7819 	struct nfs_release_lockowner_data *data = calldata;
7820 	struct nfs_server *server = data->server;
7821 	nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7822 			   &data->res.seq_res, task);
7823 	data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7824 	data->timestamp = jiffies;
7825 }
7826 
nfs4_release_lockowner_done(struct rpc_task * task,void * calldata)7827 static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7828 {
7829 	struct nfs_release_lockowner_data *data = calldata;
7830 	struct nfs_server *server = data->server;
7831 
7832 	nfs40_sequence_done(task, &data->res.seq_res);
7833 
7834 	switch (task->tk_status) {
7835 	case 0:
7836 		renew_lease(server, data->timestamp);
7837 		break;
7838 	case -NFS4ERR_STALE_CLIENTID:
7839 	case -NFS4ERR_EXPIRED:
7840 		nfs4_schedule_lease_recovery(server->nfs_client);
7841 		break;
7842 	case -NFS4ERR_LEASE_MOVED:
7843 	case -NFS4ERR_DELAY:
7844 		if (nfs4_async_handle_error(task, server,
7845 					    NULL, NULL) == -EAGAIN)
7846 			rpc_restart_call_prepare(task);
7847 	}
7848 }
7849 
nfs4_release_lockowner_release(void * calldata)7850 static void nfs4_release_lockowner_release(void *calldata)
7851 {
7852 	struct nfs_release_lockowner_data *data = calldata;
7853 	nfs4_free_lock_state(data->server, data->lsp);
7854 	kfree(calldata);
7855 }
7856 
7857 static const struct rpc_call_ops nfs4_release_lockowner_ops = {
7858 	.rpc_call_prepare = nfs4_release_lockowner_prepare,
7859 	.rpc_call_done = nfs4_release_lockowner_done,
7860 	.rpc_release = nfs4_release_lockowner_release,
7861 };
7862 
7863 static void
nfs4_release_lockowner(struct nfs_server * server,struct nfs4_lock_state * lsp)7864 nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
7865 {
7866 	struct nfs_release_lockowner_data *data;
7867 	struct rpc_message msg = {
7868 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7869 	};
7870 
7871 	if (server->nfs_client->cl_mvops->minor_version != 0)
7872 		return;
7873 
7874 	data = kmalloc(sizeof(*data), GFP_KERNEL);
7875 	if (!data)
7876 		return;
7877 	data->lsp = lsp;
7878 	data->server = server;
7879 	data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7880 	data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7881 	data->args.lock_owner.s_dev = server->s_dev;
7882 
7883 	msg.rpc_argp = &data->args;
7884 	msg.rpc_resp = &data->res;
7885 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
7886 	rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
7887 }
7888 
7889 #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7890 
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)7891 static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
7892 				   struct mnt_idmap *idmap,
7893 				   struct dentry *unused, struct inode *inode,
7894 				   const char *key, const void *buf,
7895 				   size_t buflen, int flags)
7896 {
7897 	return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_ACL);
7898 }
7899 
nfs4_xattr_get_nfs4_acl(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * key,void * buf,size_t buflen)7900 static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
7901 				   struct dentry *unused, struct inode *inode,
7902 				   const char *key, void *buf, size_t buflen)
7903 {
7904 	return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_ACL);
7905 }
7906 
nfs4_xattr_list_nfs4_acl(struct dentry * dentry)7907 static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
7908 {
7909 	return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_ACL);
7910 }
7911 
7912 #if defined(CONFIG_NFS_V4_1)
7913 #define XATTR_NAME_NFSV4_DACL "system.nfs4_dacl"
7914 
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)7915 static int nfs4_xattr_set_nfs4_dacl(const struct xattr_handler *handler,
7916 				    struct mnt_idmap *idmap,
7917 				    struct dentry *unused, struct inode *inode,
7918 				    const char *key, const void *buf,
7919 				    size_t buflen, int flags)
7920 {
7921 	return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_DACL);
7922 }
7923 
nfs4_xattr_get_nfs4_dacl(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * key,void * buf,size_t buflen)7924 static int nfs4_xattr_get_nfs4_dacl(const struct xattr_handler *handler,
7925 				    struct dentry *unused, struct inode *inode,
7926 				    const char *key, void *buf, size_t buflen)
7927 {
7928 	return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_DACL);
7929 }
7930 
nfs4_xattr_list_nfs4_dacl(struct dentry * dentry)7931 static bool nfs4_xattr_list_nfs4_dacl(struct dentry *dentry)
7932 {
7933 	return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_DACL);
7934 }
7935 
7936 #define XATTR_NAME_NFSV4_SACL "system.nfs4_sacl"
7937 
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)7938 static int nfs4_xattr_set_nfs4_sacl(const struct xattr_handler *handler,
7939 				    struct mnt_idmap *idmap,
7940 				    struct dentry *unused, struct inode *inode,
7941 				    const char *key, const void *buf,
7942 				    size_t buflen, int flags)
7943 {
7944 	return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_SACL);
7945 }
7946 
nfs4_xattr_get_nfs4_sacl(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * key,void * buf,size_t buflen)7947 static int nfs4_xattr_get_nfs4_sacl(const struct xattr_handler *handler,
7948 				    struct dentry *unused, struct inode *inode,
7949 				    const char *key, void *buf, size_t buflen)
7950 {
7951 	return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_SACL);
7952 }
7953 
nfs4_xattr_list_nfs4_sacl(struct dentry * dentry)7954 static bool nfs4_xattr_list_nfs4_sacl(struct dentry *dentry)
7955 {
7956 	return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_SACL);
7957 }
7958 
7959 #endif
7960 
7961 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
7962 
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)7963 static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
7964 				     struct mnt_idmap *idmap,
7965 				     struct dentry *unused, struct inode *inode,
7966 				     const char *key, const void *buf,
7967 				     size_t buflen, int flags)
7968 {
7969 	if (security_ismaclabel(key))
7970 		return nfs4_set_security_label(inode, buf, buflen);
7971 
7972 	return -EOPNOTSUPP;
7973 }
7974 
nfs4_xattr_get_nfs4_label(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * key,void * buf,size_t buflen)7975 static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
7976 				     struct dentry *unused, struct inode *inode,
7977 				     const char *key, void *buf, size_t buflen)
7978 {
7979 	if (security_ismaclabel(key))
7980 		return nfs4_get_security_label(inode, buf, buflen);
7981 	return -EOPNOTSUPP;
7982 }
7983 
7984 static ssize_t
nfs4_listxattr_nfs4_label(struct inode * inode,char * list,size_t list_len)7985 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7986 {
7987 	int len = 0;
7988 
7989 	if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7990 		len = security_inode_listsecurity(inode, list, list_len);
7991 		if (len >= 0 && list_len && len > list_len)
7992 			return -ERANGE;
7993 	}
7994 	return len;
7995 }
7996 
7997 static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7998 	.prefix = XATTR_SECURITY_PREFIX,
7999 	.get	= nfs4_xattr_get_nfs4_label,
8000 	.set	= nfs4_xattr_set_nfs4_label,
8001 };
8002 
8003 #else
8004 
8005 static ssize_t
nfs4_listxattr_nfs4_label(struct inode * inode,char * list,size_t list_len)8006 nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
8007 {
8008 	return 0;
8009 }
8010 
8011 #endif
8012 
8013 #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)8014 static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
8015 				    struct mnt_idmap *idmap,
8016 				    struct dentry *unused, struct inode *inode,
8017 				    const char *key, const void *buf,
8018 				    size_t buflen, int flags)
8019 {
8020 	u32 mask;
8021 	int ret;
8022 
8023 	if (!nfs_server_capable(inode, NFS_CAP_XATTR))
8024 		return -EOPNOTSUPP;
8025 
8026 	/*
8027 	 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
8028 	 * flags right now. Handling of xattr operations use the normal
8029 	 * file read/write permissions.
8030 	 *
8031 	 * Just in case the server has other ideas (which RFC 8276 allows),
8032 	 * do a cached access check for the XA* flags to possibly avoid
8033 	 * doing an RPC and getting EACCES back.
8034 	 */
8035 	if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
8036 		if (!(mask & NFS_ACCESS_XAWRITE))
8037 			return -EACCES;
8038 	}
8039 
8040 	if (buf == NULL) {
8041 		ret = nfs42_proc_removexattr(inode, key);
8042 		if (!ret)
8043 			nfs4_xattr_cache_remove(inode, key);
8044 	} else {
8045 		ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
8046 		if (!ret)
8047 			nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
8048 	}
8049 
8050 	return ret;
8051 }
8052 
nfs4_xattr_get_nfs4_user(const struct xattr_handler * handler,struct dentry * unused,struct inode * inode,const char * key,void * buf,size_t buflen)8053 static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
8054 				    struct dentry *unused, struct inode *inode,
8055 				    const char *key, void *buf, size_t buflen)
8056 {
8057 	u32 mask;
8058 	ssize_t ret;
8059 
8060 	if (!nfs_server_capable(inode, NFS_CAP_XATTR))
8061 		return -EOPNOTSUPP;
8062 
8063 	if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
8064 		if (!(mask & NFS_ACCESS_XAREAD))
8065 			return -EACCES;
8066 	}
8067 
8068 	ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
8069 	if (ret)
8070 		return ret;
8071 
8072 	ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
8073 	if (ret >= 0 || (ret < 0 && ret != -ENOENT))
8074 		return ret;
8075 
8076 	ret = nfs42_proc_getxattr(inode, key, buf, buflen);
8077 
8078 	return ret;
8079 }
8080 
8081 static ssize_t
nfs4_listxattr_nfs4_user(struct inode * inode,char * list,size_t list_len)8082 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
8083 {
8084 	u64 cookie;
8085 	bool eof;
8086 	ssize_t ret, size;
8087 	char *buf;
8088 	size_t buflen;
8089 	u32 mask;
8090 
8091 	if (!nfs_server_capable(inode, NFS_CAP_XATTR))
8092 		return 0;
8093 
8094 	if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) {
8095 		if (!(mask & NFS_ACCESS_XALIST))
8096 			return 0;
8097 	}
8098 
8099 	ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
8100 	if (ret)
8101 		return ret;
8102 
8103 	ret = nfs4_xattr_cache_list(inode, list, list_len);
8104 	if (ret >= 0 || (ret < 0 && ret != -ENOENT))
8105 		return ret;
8106 
8107 	cookie = 0;
8108 	eof = false;
8109 	buflen = list_len ? list_len : XATTR_LIST_MAX;
8110 	buf = list_len ? list : NULL;
8111 	size = 0;
8112 
8113 	while (!eof) {
8114 		ret = nfs42_proc_listxattrs(inode, buf, buflen,
8115 		    &cookie, &eof);
8116 		if (ret < 0)
8117 			return ret;
8118 
8119 		if (list_len) {
8120 			buf += ret;
8121 			buflen -= ret;
8122 		}
8123 		size += ret;
8124 	}
8125 
8126 	if (list_len)
8127 		nfs4_xattr_cache_set_list(inode, list, size);
8128 
8129 	return size;
8130 }
8131 
8132 #else
8133 
8134 static ssize_t
nfs4_listxattr_nfs4_user(struct inode * inode,char * list,size_t list_len)8135 nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
8136 {
8137 	return 0;
8138 }
8139 #endif /* CONFIG_NFS_V4_2 */
8140 
8141 /*
8142  * nfs_fhget will use either the mounted_on_fileid or the fileid
8143  */
nfs_fixup_referral_attributes(struct nfs_fattr * fattr)8144 static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
8145 {
8146 	if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
8147 	       (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
8148 	      (fattr->valid & NFS_ATTR_FATTR_FSID) &&
8149 	      (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
8150 		return;
8151 
8152 	fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
8153 		NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
8154 	fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
8155 	fattr->nlink = 2;
8156 }
8157 
_nfs4_proc_fs_locations(struct rpc_clnt * client,struct inode * dir,const struct qstr * name,struct nfs4_fs_locations * fs_locations,struct page * page)8158 static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
8159 				   const struct qstr *name,
8160 				   struct nfs4_fs_locations *fs_locations,
8161 				   struct page *page)
8162 {
8163 	struct nfs_server *server = NFS_SERVER(dir);
8164 	u32 bitmask[3];
8165 	struct nfs4_fs_locations_arg args = {
8166 		.dir_fh = NFS_FH(dir),
8167 		.name = name,
8168 		.page = page,
8169 		.bitmask = bitmask,
8170 	};
8171 	struct nfs4_fs_locations_res res = {
8172 		.fs_locations = fs_locations,
8173 	};
8174 	struct rpc_message msg = {
8175 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8176 		.rpc_argp = &args,
8177 		.rpc_resp = &res,
8178 	};
8179 	int status;
8180 
8181 	dprintk("%s: start\n", __func__);
8182 
8183 	bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
8184 	bitmask[1] = nfs4_fattr_bitmap[1];
8185 
8186 	/* Ask for the fileid of the absent filesystem if mounted_on_fileid
8187 	 * is not supported */
8188 	if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
8189 		bitmask[0] &= ~FATTR4_WORD0_FILEID;
8190 	else
8191 		bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
8192 
8193 	nfs_fattr_init(fs_locations->fattr);
8194 	fs_locations->server = server;
8195 	fs_locations->nlocations = 0;
8196 	status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
8197 	dprintk("%s: returned status = %d\n", __func__, status);
8198 	return status;
8199 }
8200 
nfs4_proc_fs_locations(struct rpc_clnt * client,struct inode * dir,const struct qstr * name,struct nfs4_fs_locations * fs_locations,struct page * page)8201 int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
8202 			   const struct qstr *name,
8203 			   struct nfs4_fs_locations *fs_locations,
8204 			   struct page *page)
8205 {
8206 	struct nfs4_exception exception = {
8207 		.interruptible = true,
8208 	};
8209 	int err;
8210 	do {
8211 		err = _nfs4_proc_fs_locations(client, dir, name,
8212 				fs_locations, page);
8213 		trace_nfs4_get_fs_locations(dir, name, err);
8214 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
8215 				&exception);
8216 	} while (exception.retry);
8217 	return err;
8218 }
8219 
8220 /*
8221  * This operation also signals the server that this client is
8222  * performing migration recovery.  The server can stop returning
8223  * NFS4ERR_LEASE_MOVED to this client.  A RENEW operation is
8224  * appended to this compound to identify the client ID which is
8225  * performing recovery.
8226  */
_nfs40_proc_get_locations(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs4_fs_locations * locations,struct page * page,const struct cred * cred)8227 static int _nfs40_proc_get_locations(struct nfs_server *server,
8228 				     struct nfs_fh *fhandle,
8229 				     struct nfs4_fs_locations *locations,
8230 				     struct page *page, const struct cred *cred)
8231 {
8232 	struct rpc_clnt *clnt = server->client;
8233 	u32 bitmask[2] = {
8234 		[0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8235 	};
8236 	struct nfs4_fs_locations_arg args = {
8237 		.clientid	= server->nfs_client->cl_clientid,
8238 		.fh		= fhandle,
8239 		.page		= page,
8240 		.bitmask	= bitmask,
8241 		.migration	= 1,		/* skip LOOKUP */
8242 		.renew		= 1,		/* append RENEW */
8243 	};
8244 	struct nfs4_fs_locations_res res = {
8245 		.fs_locations	= locations,
8246 		.migration	= 1,
8247 		.renew		= 1,
8248 	};
8249 	struct rpc_message msg = {
8250 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8251 		.rpc_argp	= &args,
8252 		.rpc_resp	= &res,
8253 		.rpc_cred	= cred,
8254 	};
8255 	unsigned long now = jiffies;
8256 	int status;
8257 
8258 	nfs_fattr_init(locations->fattr);
8259 	locations->server = server;
8260 	locations->nlocations = 0;
8261 
8262 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8263 	status = nfs4_call_sync_sequence(clnt, server, &msg,
8264 					&args.seq_args, &res.seq_res);
8265 	if (status)
8266 		return status;
8267 
8268 	renew_lease(server, now);
8269 	return 0;
8270 }
8271 
8272 #ifdef CONFIG_NFS_V4_1
8273 
8274 /*
8275  * This operation also signals the server that this client is
8276  * performing migration recovery.  The server can stop asserting
8277  * SEQ4_STATUS_LEASE_MOVED for this client.  The client ID
8278  * performing this operation is identified in the SEQUENCE
8279  * operation in this compound.
8280  *
8281  * When the client supports GETATTR(fs_locations_info), it can
8282  * be plumbed in here.
8283  */
_nfs41_proc_get_locations(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs4_fs_locations * locations,struct page * page,const struct cred * cred)8284 static int _nfs41_proc_get_locations(struct nfs_server *server,
8285 				     struct nfs_fh *fhandle,
8286 				     struct nfs4_fs_locations *locations,
8287 				     struct page *page, const struct cred *cred)
8288 {
8289 	struct rpc_clnt *clnt = server->client;
8290 	u32 bitmask[2] = {
8291 		[0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
8292 	};
8293 	struct nfs4_fs_locations_arg args = {
8294 		.fh		= fhandle,
8295 		.page		= page,
8296 		.bitmask	= bitmask,
8297 		.migration	= 1,		/* skip LOOKUP */
8298 	};
8299 	struct nfs4_fs_locations_res res = {
8300 		.fs_locations	= locations,
8301 		.migration	= 1,
8302 	};
8303 	struct rpc_message msg = {
8304 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
8305 		.rpc_argp	= &args,
8306 		.rpc_resp	= &res,
8307 		.rpc_cred	= cred,
8308 	};
8309 	struct nfs4_call_sync_data data = {
8310 		.seq_server = server,
8311 		.seq_args = &args.seq_args,
8312 		.seq_res = &res.seq_res,
8313 	};
8314 	struct rpc_task_setup task_setup_data = {
8315 		.rpc_client = clnt,
8316 		.rpc_message = &msg,
8317 		.callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
8318 		.callback_data = &data,
8319 		.flags = RPC_TASK_NO_ROUND_ROBIN,
8320 	};
8321 	int status;
8322 
8323 	nfs_fattr_init(locations->fattr);
8324 	locations->server = server;
8325 	locations->nlocations = 0;
8326 
8327 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8328 	status = nfs4_call_sync_custom(&task_setup_data);
8329 	if (status == NFS4_OK &&
8330 	    res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8331 		status = -NFS4ERR_LEASE_MOVED;
8332 	return status;
8333 }
8334 
8335 #endif	/* CONFIG_NFS_V4_1 */
8336 
8337 /**
8338  * nfs4_proc_get_locations - discover locations for a migrated FSID
8339  * @server: pointer to nfs_server to process
8340  * @fhandle: pointer to the kernel NFS client file handle
8341  * @locations: result of query
8342  * @page: buffer
8343  * @cred: credential to use for this operation
8344  *
8345  * Returns NFS4_OK on success, a negative NFS4ERR status code if the
8346  * operation failed, or a negative errno if a local error occurred.
8347  *
8348  * On success, "locations" is filled in, but if the server has
8349  * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
8350  * asserted.
8351  *
8352  * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
8353  * from this client that require migration recovery.
8354  */
nfs4_proc_get_locations(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs4_fs_locations * locations,struct page * page,const struct cred * cred)8355 int nfs4_proc_get_locations(struct nfs_server *server,
8356 			    struct nfs_fh *fhandle,
8357 			    struct nfs4_fs_locations *locations,
8358 			    struct page *page, const struct cred *cred)
8359 {
8360 	struct nfs_client *clp = server->nfs_client;
8361 	const struct nfs4_mig_recovery_ops *ops =
8362 					clp->cl_mvops->mig_recovery_ops;
8363 	struct nfs4_exception exception = {
8364 		.interruptible = true,
8365 	};
8366 	int status;
8367 
8368 	dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8369 		(unsigned long long)server->fsid.major,
8370 		(unsigned long long)server->fsid.minor,
8371 		clp->cl_hostname);
8372 	nfs_display_fhandle(fhandle, __func__);
8373 
8374 	do {
8375 		status = ops->get_locations(server, fhandle, locations, page,
8376 					    cred);
8377 		if (status != -NFS4ERR_DELAY)
8378 			break;
8379 		nfs4_handle_exception(server, status, &exception);
8380 	} while (exception.retry);
8381 	return status;
8382 }
8383 
8384 /*
8385  * This operation also signals the server that this client is
8386  * performing "lease moved" recovery.  The server can stop
8387  * returning NFS4ERR_LEASE_MOVED to this client.  A RENEW operation
8388  * is appended to this compound to identify the client ID which is
8389  * performing recovery.
8390  */
_nfs40_proc_fsid_present(struct inode * inode,const struct cred * cred)8391 static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
8392 {
8393 	struct nfs_server *server = NFS_SERVER(inode);
8394 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
8395 	struct rpc_clnt *clnt = server->client;
8396 	struct nfs4_fsid_present_arg args = {
8397 		.fh		= NFS_FH(inode),
8398 		.clientid	= clp->cl_clientid,
8399 		.renew		= 1,		/* append RENEW */
8400 	};
8401 	struct nfs4_fsid_present_res res = {
8402 		.renew		= 1,
8403 	};
8404 	struct rpc_message msg = {
8405 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8406 		.rpc_argp	= &args,
8407 		.rpc_resp	= &res,
8408 		.rpc_cred	= cred,
8409 	};
8410 	unsigned long now = jiffies;
8411 	int status;
8412 
8413 	res.fh = nfs_alloc_fhandle();
8414 	if (res.fh == NULL)
8415 		return -ENOMEM;
8416 
8417 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8418 	status = nfs4_call_sync_sequence(clnt, server, &msg,
8419 						&args.seq_args, &res.seq_res);
8420 	nfs_free_fhandle(res.fh);
8421 	if (status)
8422 		return status;
8423 
8424 	do_renew_lease(clp, now);
8425 	return 0;
8426 }
8427 
8428 #ifdef CONFIG_NFS_V4_1
8429 
8430 /*
8431  * This operation also signals the server that this client is
8432  * performing "lease moved" recovery.  The server can stop asserting
8433  * SEQ4_STATUS_LEASE_MOVED for this client.  The client ID performing
8434  * this operation is identified in the SEQUENCE operation in this
8435  * compound.
8436  */
_nfs41_proc_fsid_present(struct inode * inode,const struct cred * cred)8437 static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
8438 {
8439 	struct nfs_server *server = NFS_SERVER(inode);
8440 	struct rpc_clnt *clnt = server->client;
8441 	struct nfs4_fsid_present_arg args = {
8442 		.fh		= NFS_FH(inode),
8443 	};
8444 	struct nfs4_fsid_present_res res = {
8445 	};
8446 	struct rpc_message msg = {
8447 		.rpc_proc	= &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
8448 		.rpc_argp	= &args,
8449 		.rpc_resp	= &res,
8450 		.rpc_cred	= cred,
8451 	};
8452 	int status;
8453 
8454 	res.fh = nfs_alloc_fhandle();
8455 	if (res.fh == NULL)
8456 		return -ENOMEM;
8457 
8458 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
8459 	status = nfs4_call_sync_sequence(clnt, server, &msg,
8460 						&args.seq_args, &res.seq_res);
8461 	nfs_free_fhandle(res.fh);
8462 	if (status == NFS4_OK &&
8463 	    res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
8464 		status = -NFS4ERR_LEASE_MOVED;
8465 	return status;
8466 }
8467 
8468 #endif	/* CONFIG_NFS_V4_1 */
8469 
8470 /**
8471  * nfs4_proc_fsid_present - Is this FSID present or absent on server?
8472  * @inode: inode on FSID to check
8473  * @cred: credential to use for this operation
8474  *
8475  * Server indicates whether the FSID is present, moved, or not
8476  * recognized.  This operation is necessary to clear a LEASE_MOVED
8477  * condition for this client ID.
8478  *
8479  * Returns NFS4_OK if the FSID is present on this server,
8480  * -NFS4ERR_MOVED if the FSID is no longer present, a negative
8481  *  NFS4ERR code if some error occurred on the server, or a
8482  *  negative errno if a local failure occurred.
8483  */
nfs4_proc_fsid_present(struct inode * inode,const struct cred * cred)8484 int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
8485 {
8486 	struct nfs_server *server = NFS_SERVER(inode);
8487 	struct nfs_client *clp = server->nfs_client;
8488 	const struct nfs4_mig_recovery_ops *ops =
8489 					clp->cl_mvops->mig_recovery_ops;
8490 	struct nfs4_exception exception = {
8491 		.interruptible = true,
8492 	};
8493 	int status;
8494 
8495 	dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
8496 		(unsigned long long)server->fsid.major,
8497 		(unsigned long long)server->fsid.minor,
8498 		clp->cl_hostname);
8499 	nfs_display_fhandle(NFS_FH(inode), __func__);
8500 
8501 	do {
8502 		status = ops->fsid_present(inode, cred);
8503 		if (status != -NFS4ERR_DELAY)
8504 			break;
8505 		nfs4_handle_exception(server, status, &exception);
8506 	} while (exception.retry);
8507 	return status;
8508 }
8509 
8510 /*
8511  * If 'use_integrity' is true and the state managment nfs_client
8512  * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
8513  * and the machine credential as per RFC3530bis and RFC5661 Security
8514  * Considerations sections. Otherwise, just use the user cred with the
8515  * filesystem's rpc_client.
8516  */
_nfs4_proc_secinfo(struct inode * dir,const struct qstr * name,struct nfs4_secinfo_flavors * flavors,bool use_integrity)8517 static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
8518 {
8519 	int status;
8520 	struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
8521 	struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
8522 	struct nfs4_secinfo_arg args = {
8523 		.dir_fh = NFS_FH(dir),
8524 		.name   = name,
8525 	};
8526 	struct nfs4_secinfo_res res = {
8527 		.flavors     = flavors,
8528 	};
8529 	struct rpc_message msg = {
8530 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
8531 		.rpc_argp = &args,
8532 		.rpc_resp = &res,
8533 	};
8534 	struct nfs4_call_sync_data data = {
8535 		.seq_server = NFS_SERVER(dir),
8536 		.seq_args = &args.seq_args,
8537 		.seq_res = &res.seq_res,
8538 	};
8539 	struct rpc_task_setup task_setup = {
8540 		.rpc_client = clnt,
8541 		.rpc_message = &msg,
8542 		.callback_ops = clp->cl_mvops->call_sync_ops,
8543 		.callback_data = &data,
8544 		.flags = RPC_TASK_NO_ROUND_ROBIN,
8545 	};
8546 	const struct cred *cred = NULL;
8547 
8548 	if (use_integrity) {
8549 		clnt = clp->cl_rpcclient;
8550 		task_setup.rpc_client = clnt;
8551 
8552 		cred = nfs4_get_clid_cred(clp);
8553 		msg.rpc_cred = cred;
8554 	}
8555 
8556 	dprintk("NFS call  secinfo %s\n", name->name);
8557 
8558 	nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
8559 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
8560 	status = nfs4_call_sync_custom(&task_setup);
8561 
8562 	dprintk("NFS reply  secinfo: %d\n", status);
8563 
8564 	put_cred(cred);
8565 	return status;
8566 }
8567 
nfs4_proc_secinfo(struct inode * dir,const struct qstr * name,struct nfs4_secinfo_flavors * flavors)8568 int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8569 		      struct nfs4_secinfo_flavors *flavors)
8570 {
8571 	struct nfs4_exception exception = {
8572 		.interruptible = true,
8573 	};
8574 	int err;
8575 	do {
8576 		err = -NFS4ERR_WRONGSEC;
8577 
8578 		/* try to use integrity protection with machine cred */
8579 		if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8580 			err = _nfs4_proc_secinfo(dir, name, flavors, true);
8581 
8582 		/*
8583 		 * if unable to use integrity protection, or SECINFO with
8584 		 * integrity protection returns NFS4ERR_WRONGSEC (which is
8585 		 * disallowed by spec, but exists in deployed servers) use
8586 		 * the current filesystem's rpc_client and the user cred.
8587 		 */
8588 		if (err == -NFS4ERR_WRONGSEC)
8589 			err = _nfs4_proc_secinfo(dir, name, flavors, false);
8590 
8591 		trace_nfs4_secinfo(dir, name, err);
8592 		err = nfs4_handle_exception(NFS_SERVER(dir), err,
8593 				&exception);
8594 	} while (exception.retry);
8595 	return err;
8596 }
8597 
8598 #ifdef CONFIG_NFS_V4_1
8599 /*
8600  * Check the exchange flags returned by the server for invalid flags, having
8601  * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8602  * DS flags set.
8603  */
nfs4_check_cl_exchange_flags(u32 flags,u32 version)8604 static int nfs4_check_cl_exchange_flags(u32 flags, u32 version)
8605 {
8606 	if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R))
8607 		goto out_inval;
8608 	else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R))
8609 		goto out_inval;
8610 	if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8611 	    (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8612 		goto out_inval;
8613 	if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8614 		goto out_inval;
8615 	return NFS_OK;
8616 out_inval:
8617 	return -NFS4ERR_INVAL;
8618 }
8619 
8620 static bool
nfs41_same_server_scope(struct nfs41_server_scope * a,struct nfs41_server_scope * b)8621 nfs41_same_server_scope(struct nfs41_server_scope *a,
8622 			struct nfs41_server_scope *b)
8623 {
8624 	if (a->server_scope_sz != b->server_scope_sz)
8625 		return false;
8626 	return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
8627 }
8628 
8629 static void
nfs4_bind_one_conn_to_session_done(struct rpc_task * task,void * calldata)8630 nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8631 {
8632 	struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
8633 	struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
8634 	struct nfs_client *clp = args->client;
8635 
8636 	switch (task->tk_status) {
8637 	case -NFS4ERR_BADSESSION:
8638 	case -NFS4ERR_DEADSESSION:
8639 		nfs4_schedule_session_recovery(clp->cl_session,
8640 				task->tk_status);
8641 		return;
8642 	}
8643 	if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8644 			res->dir != NFS4_CDFS4_BOTH) {
8645 		rpc_task_close_connection(task);
8646 		if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8647 			rpc_restart_call(task);
8648 	}
8649 }
8650 
8651 static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
8652 	.rpc_call_done =  nfs4_bind_one_conn_to_session_done,
8653 };
8654 
8655 /*
8656  * nfs4_proc_bind_one_conn_to_session()
8657  *
8658  * The 4.1 client currently uses the same TCP connection for the
8659  * fore and backchannel.
8660  */
8661 static
nfs4_proc_bind_one_conn_to_session(struct rpc_clnt * clnt,struct rpc_xprt * xprt,struct nfs_client * clp,const struct cred * cred)8662 int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8663 		struct rpc_xprt *xprt,
8664 		struct nfs_client *clp,
8665 		const struct cred *cred)
8666 {
8667 	int status;
8668 	struct nfs41_bind_conn_to_session_args args = {
8669 		.client = clp,
8670 		.dir = NFS4_CDFC4_FORE_OR_BOTH,
8671 		.retries = 0,
8672 	};
8673 	struct nfs41_bind_conn_to_session_res res;
8674 	struct rpc_message msg = {
8675 		.rpc_proc =
8676 			&nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
8677 		.rpc_argp = &args,
8678 		.rpc_resp = &res,
8679 		.rpc_cred = cred,
8680 	};
8681 	struct rpc_task_setup task_setup_data = {
8682 		.rpc_client = clnt,
8683 		.rpc_xprt = xprt,
8684 		.callback_ops = &nfs4_bind_one_conn_to_session_ops,
8685 		.rpc_message = &msg,
8686 		.flags = RPC_TASK_TIMEOUT,
8687 	};
8688 	struct rpc_task *task;
8689 
8690 	nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8691 	if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8692 		args.dir = NFS4_CDFC4_FORE;
8693 
8694 	/* Do not set the backchannel flag unless this is clnt->cl_xprt */
8695 	if (xprt != rcu_access_pointer(clnt->cl_xprt))
8696 		args.dir = NFS4_CDFC4_FORE;
8697 
8698 	task = rpc_run_task(&task_setup_data);
8699 	if (!IS_ERR(task)) {
8700 		status = task->tk_status;
8701 		rpc_put_task(task);
8702 	} else
8703 		status = PTR_ERR(task);
8704 	trace_nfs4_bind_conn_to_session(clp, status);
8705 	if (status == 0) {
8706 		if (memcmp(res.sessionid.data,
8707 		    clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8708 			dprintk("NFS: %s: Session ID mismatch\n", __func__);
8709 			return -EIO;
8710 		}
8711 		if ((res.dir & args.dir) != res.dir || res.dir == 0) {
8712 			dprintk("NFS: %s: Unexpected direction from server\n",
8713 				__func__);
8714 			return -EIO;
8715 		}
8716 		if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
8717 			dprintk("NFS: %s: Server returned RDMA mode = true\n",
8718 				__func__);
8719 			return -EIO;
8720 		}
8721 	}
8722 
8723 	return status;
8724 }
8725 
8726 struct rpc_bind_conn_calldata {
8727 	struct nfs_client *clp;
8728 	const struct cred *cred;
8729 };
8730 
8731 static int
nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt * clnt,struct rpc_xprt * xprt,void * calldata)8732 nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8733 		struct rpc_xprt *xprt,
8734 		void *calldata)
8735 {
8736 	struct rpc_bind_conn_calldata *p = calldata;
8737 
8738 	return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8739 }
8740 
nfs4_proc_bind_conn_to_session(struct nfs_client * clp,const struct cred * cred)8741 int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
8742 {
8743 	struct rpc_bind_conn_calldata data = {
8744 		.clp = clp,
8745 		.cred = cred,
8746 	};
8747 	return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8748 			nfs4_proc_bind_conn_to_session_callback, &data);
8749 }
8750 
8751 /*
8752  * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8753  * and operations we'd like to see to enable certain features in the allow map
8754  */
8755 static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8756 	.how = SP4_MACH_CRED,
8757 	.enforce.u.words = {
8758 		[1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8759 		      1 << (OP_EXCHANGE_ID - 32) |
8760 		      1 << (OP_CREATE_SESSION - 32) |
8761 		      1 << (OP_DESTROY_SESSION - 32) |
8762 		      1 << (OP_DESTROY_CLIENTID - 32)
8763 	},
8764 	.allow.u.words = {
8765 		[0] = 1 << (OP_CLOSE) |
8766 		      1 << (OP_OPEN_DOWNGRADE) |
8767 		      1 << (OP_LOCKU) |
8768 		      1 << (OP_DELEGRETURN) |
8769 		      1 << (OP_COMMIT),
8770 		[1] = 1 << (OP_SECINFO - 32) |
8771 		      1 << (OP_SECINFO_NO_NAME - 32) |
8772 		      1 << (OP_LAYOUTRETURN - 32) |
8773 		      1 << (OP_TEST_STATEID - 32) |
8774 		      1 << (OP_FREE_STATEID - 32) |
8775 		      1 << (OP_WRITE - 32)
8776 	}
8777 };
8778 
8779 /*
8780  * Select the state protection mode for client `clp' given the server results
8781  * from exchange_id in `sp'.
8782  *
8783  * Returns 0 on success, negative errno otherwise.
8784  */
nfs4_sp4_select_mode(struct nfs_client * clp,struct nfs41_state_protection * sp)8785 static int nfs4_sp4_select_mode(struct nfs_client *clp,
8786 				 struct nfs41_state_protection *sp)
8787 {
8788 	static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8789 		[1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8790 		      1 << (OP_EXCHANGE_ID - 32) |
8791 		      1 << (OP_CREATE_SESSION - 32) |
8792 		      1 << (OP_DESTROY_SESSION - 32) |
8793 		      1 << (OP_DESTROY_CLIENTID - 32)
8794 	};
8795 	unsigned long flags = 0;
8796 	unsigned int i;
8797 	int ret = 0;
8798 
8799 	if (sp->how == SP4_MACH_CRED) {
8800 		/* Print state protect result */
8801 		dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8802 		for (i = 0; i <= LAST_NFS4_OP; i++) {
8803 			if (test_bit(i, sp->enforce.u.longs))
8804 				dfprintk(MOUNT, "  enforce op %d\n", i);
8805 			if (test_bit(i, sp->allow.u.longs))
8806 				dfprintk(MOUNT, "  allow op %d\n", i);
8807 		}
8808 
8809 		/* make sure nothing is on enforce list that isn't supported */
8810 		for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8811 			if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8812 				dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8813 				ret = -EINVAL;
8814 				goto out;
8815 			}
8816 		}
8817 
8818 		/*
8819 		 * Minimal mode - state operations are allowed to use machine
8820 		 * credential.  Note this already happens by default, so the
8821 		 * client doesn't have to do anything more than the negotiation.
8822 		 *
8823 		 * NOTE: we don't care if EXCHANGE_ID is in the list -
8824 		 *       we're already using the machine cred for exchange_id
8825 		 *       and will never use a different cred.
8826 		 */
8827 		if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8828 		    test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8829 		    test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8830 		    test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8831 			dfprintk(MOUNT, "sp4_mach_cred:\n");
8832 			dfprintk(MOUNT, "  minimal mode enabled\n");
8833 			__set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
8834 		} else {
8835 			dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
8836 			ret = -EINVAL;
8837 			goto out;
8838 		}
8839 
8840 		if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
8841 		    test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8842 		    test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
8843 		    test_bit(OP_LOCKU, sp->allow.u.longs)) {
8844 			dfprintk(MOUNT, "  cleanup mode enabled\n");
8845 			__set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
8846 		}
8847 
8848 		if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8849 			dfprintk(MOUNT, "  pnfs cleanup mode enabled\n");
8850 			__set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
8851 		}
8852 
8853 		if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8854 		    test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8855 			dfprintk(MOUNT, "  secinfo mode enabled\n");
8856 			__set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
8857 		}
8858 
8859 		if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8860 		    test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8861 			dfprintk(MOUNT, "  stateid mode enabled\n");
8862 			__set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
8863 		}
8864 
8865 		if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8866 			dfprintk(MOUNT, "  write mode enabled\n");
8867 			__set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
8868 		}
8869 
8870 		if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8871 			dfprintk(MOUNT, "  commit mode enabled\n");
8872 			__set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
8873 		}
8874 	}
8875 out:
8876 	clp->cl_sp4_flags = flags;
8877 	return ret;
8878 }
8879 
8880 struct nfs41_exchange_id_data {
8881 	struct nfs41_exchange_id_res res;
8882 	struct nfs41_exchange_id_args args;
8883 };
8884 
nfs4_exchange_id_release(void * data)8885 static void nfs4_exchange_id_release(void *data)
8886 {
8887 	struct nfs41_exchange_id_data *cdata =
8888 					(struct nfs41_exchange_id_data *)data;
8889 
8890 	nfs_put_client(cdata->args.client);
8891 	kfree(cdata->res.impl_id);
8892 	kfree(cdata->res.server_scope);
8893 	kfree(cdata->res.server_owner);
8894 	kfree(cdata);
8895 }
8896 
8897 static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
8898 	.rpc_release = nfs4_exchange_id_release,
8899 };
8900 
8901 /*
8902  * _nfs4_proc_exchange_id()
8903  *
8904  * Wrapper for EXCHANGE_ID operation.
8905  */
8906 static struct rpc_task *
nfs4_run_exchange_id(struct nfs_client * clp,const struct cred * cred,u32 sp4_how,struct rpc_xprt * xprt)8907 nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
8908 			u32 sp4_how, struct rpc_xprt *xprt)
8909 {
8910 	struct rpc_message msg = {
8911 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
8912 		.rpc_cred = cred,
8913 	};
8914 	struct rpc_task_setup task_setup_data = {
8915 		.rpc_client = clp->cl_rpcclient,
8916 		.callback_ops = &nfs4_exchange_id_call_ops,
8917 		.rpc_message = &msg,
8918 		.flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
8919 	};
8920 	struct nfs41_exchange_id_data *calldata;
8921 	int status;
8922 
8923 	if (!refcount_inc_not_zero(&clp->cl_count))
8924 		return ERR_PTR(-EIO);
8925 
8926 	status = -ENOMEM;
8927 	calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
8928 	if (!calldata)
8929 		goto out;
8930 
8931 	nfs4_init_boot_verifier(clp, &calldata->args.verifier);
8932 
8933 	status = nfs4_init_uniform_client_string(clp);
8934 	if (status)
8935 		goto out_calldata;
8936 
8937 	calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8938 						GFP_NOFS);
8939 	status = -ENOMEM;
8940 	if (unlikely(calldata->res.server_owner == NULL))
8941 		goto out_calldata;
8942 
8943 	calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
8944 					GFP_NOFS);
8945 	if (unlikely(calldata->res.server_scope == NULL))
8946 		goto out_server_owner;
8947 
8948 	calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8949 	if (unlikely(calldata->res.impl_id == NULL))
8950 		goto out_server_scope;
8951 
8952 	switch (sp4_how) {
8953 	case SP4_NONE:
8954 		calldata->args.state_protect.how = SP4_NONE;
8955 		break;
8956 
8957 	case SP4_MACH_CRED:
8958 		calldata->args.state_protect = nfs4_sp4_mach_cred_request;
8959 		break;
8960 
8961 	default:
8962 		/* unsupported! */
8963 		WARN_ON_ONCE(1);
8964 		status = -EINVAL;
8965 		goto out_impl_id;
8966 	}
8967 	if (xprt) {
8968 		task_setup_data.rpc_xprt = xprt;
8969 		task_setup_data.flags |= RPC_TASK_SOFTCONN;
8970 		memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8971 				sizeof(calldata->args.verifier.data));
8972 	}
8973 	calldata->args.client = clp;
8974 	calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8975 	EXCHGID4_FLAG_BIND_PRINC_STATEID;
8976 #ifdef CONFIG_NFS_V4_1_MIGRATION
8977 	calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
8978 #endif
8979 	if (test_bit(NFS_CS_PNFS, &clp->cl_flags))
8980 		calldata->args.flags |= EXCHGID4_FLAG_USE_PNFS_DS;
8981 	msg.rpc_argp = &calldata->args;
8982 	msg.rpc_resp = &calldata->res;
8983 	task_setup_data.callback_data = calldata;
8984 
8985 	return rpc_run_task(&task_setup_data);
8986 
8987 out_impl_id:
8988 	kfree(calldata->res.impl_id);
8989 out_server_scope:
8990 	kfree(calldata->res.server_scope);
8991 out_server_owner:
8992 	kfree(calldata->res.server_owner);
8993 out_calldata:
8994 	kfree(calldata);
8995 out:
8996 	nfs_put_client(clp);
8997 	return ERR_PTR(status);
8998 }
8999 
9000 /*
9001  * _nfs4_proc_exchange_id()
9002  *
9003  * Wrapper for EXCHANGE_ID operation.
9004  */
_nfs4_proc_exchange_id(struct nfs_client * clp,const struct cred * cred,u32 sp4_how)9005 static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
9006 			u32 sp4_how)
9007 {
9008 	struct rpc_task *task;
9009 	struct nfs41_exchange_id_args *argp;
9010 	struct nfs41_exchange_id_res *resp;
9011 	unsigned long now = jiffies;
9012 	int status;
9013 
9014 	task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
9015 	if (IS_ERR(task))
9016 		return PTR_ERR(task);
9017 
9018 	argp = task->tk_msg.rpc_argp;
9019 	resp = task->tk_msg.rpc_resp;
9020 	status = task->tk_status;
9021 	if (status  != 0)
9022 		goto out;
9023 
9024 	status = nfs4_check_cl_exchange_flags(resp->flags,
9025 			clp->cl_mvops->minor_version);
9026 	if (status  != 0)
9027 		goto out;
9028 
9029 	status = nfs4_sp4_select_mode(clp, &resp->state_protect);
9030 	if (status != 0)
9031 		goto out;
9032 
9033 	do_renew_lease(clp, now);
9034 
9035 	clp->cl_clientid = resp->clientid;
9036 	clp->cl_exchange_flags = resp->flags;
9037 	clp->cl_seqid = resp->seqid;
9038 	/* Client ID is not confirmed */
9039 	if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
9040 		clear_bit(NFS4_SESSION_ESTABLISHED,
9041 			  &clp->cl_session->session_state);
9042 
9043 	if (clp->cl_serverscope != NULL &&
9044 	    !nfs41_same_server_scope(clp->cl_serverscope,
9045 				resp->server_scope)) {
9046 		dprintk("%s: server_scope mismatch detected\n",
9047 			__func__);
9048 		set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
9049 	}
9050 
9051 	swap(clp->cl_serverowner, resp->server_owner);
9052 	swap(clp->cl_serverscope, resp->server_scope);
9053 	swap(clp->cl_implid, resp->impl_id);
9054 
9055 	/* Save the EXCHANGE_ID verifier session trunk tests */
9056 	memcpy(clp->cl_confirm.data, argp->verifier.data,
9057 	       sizeof(clp->cl_confirm.data));
9058 out:
9059 	trace_nfs4_exchange_id(clp, status);
9060 	rpc_put_task(task);
9061 	return status;
9062 }
9063 
9064 /*
9065  * nfs4_proc_exchange_id()
9066  *
9067  * Returns zero, a negative errno, or a negative NFS4ERR status code.
9068  *
9069  * Since the clientid has expired, all compounds using sessions
9070  * associated with the stale clientid will be returning
9071  * NFS4ERR_BADSESSION in the sequence operation, and will therefore
9072  * be in some phase of session reset.
9073  *
9074  * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
9075  */
nfs4_proc_exchange_id(struct nfs_client * clp,const struct cred * cred)9076 int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
9077 {
9078 	rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
9079 	int status;
9080 
9081 	/* try SP4_MACH_CRED if krb5i/p	*/
9082 	if (authflavor == RPC_AUTH_GSS_KRB5I ||
9083 	    authflavor == RPC_AUTH_GSS_KRB5P) {
9084 		status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
9085 		if (!status)
9086 			return 0;
9087 	}
9088 
9089 	/* try SP4_NONE */
9090 	return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
9091 }
9092 
9093 /**
9094  * nfs4_test_session_trunk
9095  *
9096  * This is an add_xprt_test() test function called from
9097  * rpc_clnt_setup_test_and_add_xprt.
9098  *
9099  * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
9100  * and is dereferrenced in nfs4_exchange_id_release
9101  *
9102  * Upon success, add the new transport to the rpc_clnt
9103  *
9104  * @clnt: struct rpc_clnt to get new transport
9105  * @xprt: the rpc_xprt to test
9106  * @data: call data for _nfs4_proc_exchange_id.
9107  */
nfs4_test_session_trunk(struct rpc_clnt * clnt,struct rpc_xprt * xprt,void * data)9108 void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
9109 			    void *data)
9110 {
9111 	struct nfs4_add_xprt_data *adata = data;
9112 	struct rpc_task *task;
9113 	int status;
9114 
9115 	u32 sp4_how;
9116 
9117 	dprintk("--> %s try %s\n", __func__,
9118 		xprt->address_strings[RPC_DISPLAY_ADDR]);
9119 
9120 	sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
9121 
9122 try_again:
9123 	/* Test connection for session trunking. Async exchange_id call */
9124 	task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
9125 	if (IS_ERR(task))
9126 		return;
9127 
9128 	status = task->tk_status;
9129 	if (status == 0) {
9130 		status = nfs4_detect_session_trunking(adata->clp,
9131 				task->tk_msg.rpc_resp, xprt);
9132 		trace_nfs4_trunked_exchange_id(adata->clp,
9133 			xprt->address_strings[RPC_DISPLAY_ADDR], status);
9134 	}
9135 	if (status == 0)
9136 		rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
9137 	else if (status != -NFS4ERR_DELAY && rpc_clnt_xprt_switch_has_addr(clnt,
9138 				(struct sockaddr *)&xprt->addr))
9139 		rpc_clnt_xprt_switch_remove_xprt(clnt, xprt);
9140 
9141 	rpc_put_task(task);
9142 	if (status == -NFS4ERR_DELAY) {
9143 		ssleep(1);
9144 		goto try_again;
9145 	}
9146 }
9147 EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
9148 
_nfs4_proc_destroy_clientid(struct nfs_client * clp,const struct cred * cred)9149 static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
9150 		const struct cred *cred)
9151 {
9152 	struct rpc_message msg = {
9153 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
9154 		.rpc_argp = clp,
9155 		.rpc_cred = cred,
9156 	};
9157 	int status;
9158 
9159 	status = rpc_call_sync(clp->cl_rpcclient, &msg,
9160 			       RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9161 	trace_nfs4_destroy_clientid(clp, status);
9162 	if (status)
9163 		dprintk("NFS: Got error %d from the server %s on "
9164 			"DESTROY_CLIENTID.", status, clp->cl_hostname);
9165 	return status;
9166 }
9167 
nfs4_proc_destroy_clientid(struct nfs_client * clp,const struct cred * cred)9168 static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
9169 		const struct cred *cred)
9170 {
9171 	unsigned int loop;
9172 	int ret;
9173 
9174 	for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
9175 		ret = _nfs4_proc_destroy_clientid(clp, cred);
9176 		switch (ret) {
9177 		case -NFS4ERR_DELAY:
9178 		case -NFS4ERR_CLIENTID_BUSY:
9179 			ssleep(1);
9180 			break;
9181 		default:
9182 			return ret;
9183 		}
9184 	}
9185 	return 0;
9186 }
9187 
nfs4_destroy_clientid(struct nfs_client * clp)9188 int nfs4_destroy_clientid(struct nfs_client *clp)
9189 {
9190 	const struct cred *cred;
9191 	int ret = 0;
9192 
9193 	if (clp->cl_mvops->minor_version < 1)
9194 		goto out;
9195 	if (clp->cl_exchange_flags == 0)
9196 		goto out;
9197 	if (clp->cl_preserve_clid)
9198 		goto out;
9199 	cred = nfs4_get_clid_cred(clp);
9200 	ret = nfs4_proc_destroy_clientid(clp, cred);
9201 	put_cred(cred);
9202 	switch (ret) {
9203 	case 0:
9204 	case -NFS4ERR_STALE_CLIENTID:
9205 		clp->cl_exchange_flags = 0;
9206 	}
9207 out:
9208 	return ret;
9209 }
9210 
9211 #endif /* CONFIG_NFS_V4_1 */
9212 
9213 struct nfs4_get_lease_time_data {
9214 	struct nfs4_get_lease_time_args *args;
9215 	struct nfs4_get_lease_time_res *res;
9216 	struct nfs_client *clp;
9217 };
9218 
nfs4_get_lease_time_prepare(struct rpc_task * task,void * calldata)9219 static void nfs4_get_lease_time_prepare(struct rpc_task *task,
9220 					void *calldata)
9221 {
9222 	struct nfs4_get_lease_time_data *data =
9223 			(struct nfs4_get_lease_time_data *)calldata;
9224 
9225 	/* just setup sequence, do not trigger session recovery
9226 	   since we're invoked within one */
9227 	nfs4_setup_sequence(data->clp,
9228 			&data->args->la_seq_args,
9229 			&data->res->lr_seq_res,
9230 			task);
9231 }
9232 
9233 /*
9234  * Called from nfs4_state_manager thread for session setup, so don't recover
9235  * from sequence operation or clientid errors.
9236  */
nfs4_get_lease_time_done(struct rpc_task * task,void * calldata)9237 static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
9238 {
9239 	struct nfs4_get_lease_time_data *data =
9240 			(struct nfs4_get_lease_time_data *)calldata;
9241 
9242 	if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
9243 		return;
9244 	switch (task->tk_status) {
9245 	case -NFS4ERR_DELAY:
9246 	case -NFS4ERR_GRACE:
9247 		rpc_delay(task, NFS4_POLL_RETRY_MIN);
9248 		task->tk_status = 0;
9249 		fallthrough;
9250 	case -NFS4ERR_RETRY_UNCACHED_REP:
9251 		rpc_restart_call_prepare(task);
9252 		return;
9253 	}
9254 }
9255 
9256 static const struct rpc_call_ops nfs4_get_lease_time_ops = {
9257 	.rpc_call_prepare = nfs4_get_lease_time_prepare,
9258 	.rpc_call_done = nfs4_get_lease_time_done,
9259 };
9260 
nfs4_proc_get_lease_time(struct nfs_client * clp,struct nfs_fsinfo * fsinfo)9261 int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
9262 {
9263 	struct nfs4_get_lease_time_args args;
9264 	struct nfs4_get_lease_time_res res = {
9265 		.lr_fsinfo = fsinfo,
9266 	};
9267 	struct nfs4_get_lease_time_data data = {
9268 		.args = &args,
9269 		.res = &res,
9270 		.clp = clp,
9271 	};
9272 	struct rpc_message msg = {
9273 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
9274 		.rpc_argp = &args,
9275 		.rpc_resp = &res,
9276 	};
9277 	struct rpc_task_setup task_setup = {
9278 		.rpc_client = clp->cl_rpcclient,
9279 		.rpc_message = &msg,
9280 		.callback_ops = &nfs4_get_lease_time_ops,
9281 		.callback_data = &data,
9282 		.flags = RPC_TASK_TIMEOUT,
9283 	};
9284 
9285 	nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
9286 	return nfs4_call_sync_custom(&task_setup);
9287 }
9288 
9289 #ifdef CONFIG_NFS_V4_1
9290 
9291 /*
9292  * Initialize the values to be used by the client in CREATE_SESSION
9293  * If nfs4_init_session set the fore channel request and response sizes,
9294  * use them.
9295  *
9296  * Set the back channel max_resp_sz_cached to zero to force the client to
9297  * always set csa_cachethis to FALSE because the current implementation
9298  * of the back channel DRC only supports caching the CB_SEQUENCE operation.
9299  */
nfs4_init_channel_attrs(struct nfs41_create_session_args * args,struct rpc_clnt * clnt)9300 static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
9301 				    struct rpc_clnt *clnt)
9302 {
9303 	unsigned int max_rqst_sz, max_resp_sz;
9304 	unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
9305 	unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
9306 
9307 	max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
9308 	max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
9309 
9310 	/* Fore channel attributes */
9311 	args->fc_attrs.max_rqst_sz = max_rqst_sz;
9312 	args->fc_attrs.max_resp_sz = max_resp_sz;
9313 	args->fc_attrs.max_ops = NFS4_MAX_OPS;
9314 	args->fc_attrs.max_reqs = max_session_slots;
9315 
9316 	dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
9317 		"max_ops=%u max_reqs=%u\n",
9318 		__func__,
9319 		args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
9320 		args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
9321 
9322 	/* Back channel attributes */
9323 	args->bc_attrs.max_rqst_sz = max_bc_payload;
9324 	args->bc_attrs.max_resp_sz = max_bc_payload;
9325 	args->bc_attrs.max_resp_sz_cached = 0;
9326 	args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
9327 	args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
9328 	if (args->bc_attrs.max_reqs > max_bc_slots)
9329 		args->bc_attrs.max_reqs = max_bc_slots;
9330 
9331 	dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
9332 		"max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
9333 		__func__,
9334 		args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
9335 		args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
9336 		args->bc_attrs.max_reqs);
9337 }
9338 
nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args * args,struct nfs41_create_session_res * res)9339 static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
9340 		struct nfs41_create_session_res *res)
9341 {
9342 	struct nfs4_channel_attrs *sent = &args->fc_attrs;
9343 	struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
9344 
9345 	if (rcvd->max_resp_sz > sent->max_resp_sz)
9346 		return -EINVAL;
9347 	/*
9348 	 * Our requested max_ops is the minimum we need; we're not
9349 	 * prepared to break up compounds into smaller pieces than that.
9350 	 * So, no point even trying to continue if the server won't
9351 	 * cooperate:
9352 	 */
9353 	if (rcvd->max_ops < sent->max_ops)
9354 		return -EINVAL;
9355 	if (rcvd->max_reqs == 0)
9356 		return -EINVAL;
9357 	if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
9358 		rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
9359 	return 0;
9360 }
9361 
nfs4_verify_back_channel_attrs(struct nfs41_create_session_args * args,struct nfs41_create_session_res * res)9362 static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
9363 		struct nfs41_create_session_res *res)
9364 {
9365 	struct nfs4_channel_attrs *sent = &args->bc_attrs;
9366 	struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
9367 
9368 	if (!(res->flags & SESSION4_BACK_CHAN))
9369 		goto out;
9370 	if (rcvd->max_rqst_sz > sent->max_rqst_sz)
9371 		return -EINVAL;
9372 	if (rcvd->max_resp_sz < sent->max_resp_sz)
9373 		return -EINVAL;
9374 	if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
9375 		return -EINVAL;
9376 	if (rcvd->max_ops > sent->max_ops)
9377 		return -EINVAL;
9378 	if (rcvd->max_reqs > sent->max_reqs)
9379 		return -EINVAL;
9380 out:
9381 	return 0;
9382 }
9383 
nfs4_verify_channel_attrs(struct nfs41_create_session_args * args,struct nfs41_create_session_res * res)9384 static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
9385 				     struct nfs41_create_session_res *res)
9386 {
9387 	int ret;
9388 
9389 	ret = nfs4_verify_fore_channel_attrs(args, res);
9390 	if (ret)
9391 		return ret;
9392 	return nfs4_verify_back_channel_attrs(args, res);
9393 }
9394 
nfs4_update_session(struct nfs4_session * session,struct nfs41_create_session_res * res)9395 static void nfs4_update_session(struct nfs4_session *session,
9396 		struct nfs41_create_session_res *res)
9397 {
9398 	nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
9399 	/* Mark client id and session as being confirmed */
9400 	session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
9401 	set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
9402 	session->flags = res->flags;
9403 	memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
9404 	if (res->flags & SESSION4_BACK_CHAN)
9405 		memcpy(&session->bc_attrs, &res->bc_attrs,
9406 				sizeof(session->bc_attrs));
9407 }
9408 
_nfs4_proc_create_session(struct nfs_client * clp,const struct cred * cred)9409 static int _nfs4_proc_create_session(struct nfs_client *clp,
9410 		const struct cred *cred)
9411 {
9412 	struct nfs4_session *session = clp->cl_session;
9413 	struct nfs41_create_session_args args = {
9414 		.client = clp,
9415 		.clientid = clp->cl_clientid,
9416 		.seqid = clp->cl_seqid,
9417 		.cb_program = NFS4_CALLBACK,
9418 	};
9419 	struct nfs41_create_session_res res;
9420 
9421 	struct rpc_message msg = {
9422 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
9423 		.rpc_argp = &args,
9424 		.rpc_resp = &res,
9425 		.rpc_cred = cred,
9426 	};
9427 	int status;
9428 
9429 	nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
9430 	args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
9431 
9432 	status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9433 			       RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9434 	trace_nfs4_create_session(clp, status);
9435 
9436 	switch (status) {
9437 	case -NFS4ERR_STALE_CLIENTID:
9438 	case -NFS4ERR_DELAY:
9439 	case -ETIMEDOUT:
9440 	case -EACCES:
9441 	case -EAGAIN:
9442 		goto out;
9443 	}
9444 
9445 	clp->cl_seqid++;
9446 	if (!status) {
9447 		/* Verify the session's negotiated channel_attrs values */
9448 		status = nfs4_verify_channel_attrs(&args, &res);
9449 		/* Increment the clientid slot sequence id */
9450 		if (status)
9451 			goto out;
9452 		nfs4_update_session(session, &res);
9453 	}
9454 out:
9455 	return status;
9456 }
9457 
9458 /*
9459  * Issues a CREATE_SESSION operation to the server.
9460  * It is the responsibility of the caller to verify the session is
9461  * expired before calling this routine.
9462  */
nfs4_proc_create_session(struct nfs_client * clp,const struct cred * cred)9463 int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
9464 {
9465 	int status;
9466 	unsigned *ptr;
9467 	struct nfs4_session *session = clp->cl_session;
9468 	struct nfs4_add_xprt_data xprtdata = {
9469 		.clp = clp,
9470 	};
9471 	struct rpc_add_xprt_test rpcdata = {
9472 		.add_xprt_test = clp->cl_mvops->session_trunk,
9473 		.data = &xprtdata,
9474 	};
9475 
9476 	dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
9477 
9478 	status = _nfs4_proc_create_session(clp, cred);
9479 	if (status)
9480 		goto out;
9481 
9482 	/* Init or reset the session slot tables */
9483 	status = nfs4_setup_session_slot_tables(session);
9484 	dprintk("slot table setup returned %d\n", status);
9485 	if (status)
9486 		goto out;
9487 
9488 	ptr = (unsigned *)&session->sess_id.data[0];
9489 	dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
9490 		clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
9491 	rpc_clnt_probe_trunked_xprts(clp->cl_rpcclient, &rpcdata);
9492 out:
9493 	return status;
9494 }
9495 
9496 /*
9497  * Issue the over-the-wire RPC DESTROY_SESSION.
9498  * The caller must serialize access to this routine.
9499  */
nfs4_proc_destroy_session(struct nfs4_session * session,const struct cred * cred)9500 int nfs4_proc_destroy_session(struct nfs4_session *session,
9501 		const struct cred *cred)
9502 {
9503 	struct rpc_message msg = {
9504 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
9505 		.rpc_argp = session,
9506 		.rpc_cred = cred,
9507 	};
9508 	int status = 0;
9509 
9510 	/* session is still being setup */
9511 	if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
9512 		return 0;
9513 
9514 	status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
9515 			       RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
9516 	trace_nfs4_destroy_session(session->clp, status);
9517 
9518 	if (status)
9519 		dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
9520 			"Session has been destroyed regardless...\n", status);
9521 	rpc_clnt_manage_trunked_xprts(session->clp->cl_rpcclient);
9522 	return status;
9523 }
9524 
9525 /*
9526  * Renew the cl_session lease.
9527  */
9528 struct nfs4_sequence_data {
9529 	struct nfs_client *clp;
9530 	struct nfs4_sequence_args args;
9531 	struct nfs4_sequence_res res;
9532 };
9533 
nfs41_sequence_release(void * data)9534 static void nfs41_sequence_release(void *data)
9535 {
9536 	struct nfs4_sequence_data *calldata = data;
9537 	struct nfs_client *clp = calldata->clp;
9538 
9539 	if (refcount_read(&clp->cl_count) > 1)
9540 		nfs4_schedule_state_renewal(clp);
9541 	nfs_put_client(clp);
9542 	kfree(calldata);
9543 }
9544 
nfs41_sequence_handle_errors(struct rpc_task * task,struct nfs_client * clp)9545 static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9546 {
9547 	switch(task->tk_status) {
9548 	case -NFS4ERR_DELAY:
9549 		rpc_delay(task, NFS4_POLL_RETRY_MAX);
9550 		return -EAGAIN;
9551 	default:
9552 		nfs4_schedule_lease_recovery(clp);
9553 	}
9554 	return 0;
9555 }
9556 
nfs41_sequence_call_done(struct rpc_task * task,void * data)9557 static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
9558 {
9559 	struct nfs4_sequence_data *calldata = data;
9560 	struct nfs_client *clp = calldata->clp;
9561 
9562 	if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
9563 		return;
9564 
9565 	trace_nfs4_sequence(clp, task->tk_status);
9566 	if (task->tk_status < 0 && !task->tk_client->cl_shutdown) {
9567 		dprintk("%s ERROR %d\n", __func__, task->tk_status);
9568 		if (refcount_read(&clp->cl_count) == 1)
9569 			return;
9570 
9571 		if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
9572 			rpc_restart_call_prepare(task);
9573 			return;
9574 		}
9575 	}
9576 	dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
9577 }
9578 
nfs41_sequence_prepare(struct rpc_task * task,void * data)9579 static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9580 {
9581 	struct nfs4_sequence_data *calldata = data;
9582 	struct nfs_client *clp = calldata->clp;
9583 	struct nfs4_sequence_args *args;
9584 	struct nfs4_sequence_res *res;
9585 
9586 	args = task->tk_msg.rpc_argp;
9587 	res = task->tk_msg.rpc_resp;
9588 
9589 	nfs4_setup_sequence(clp, args, res, task);
9590 }
9591 
9592 static const struct rpc_call_ops nfs41_sequence_ops = {
9593 	.rpc_call_done = nfs41_sequence_call_done,
9594 	.rpc_call_prepare = nfs41_sequence_prepare,
9595 	.rpc_release = nfs41_sequence_release,
9596 };
9597 
_nfs41_proc_sequence(struct nfs_client * clp,const struct cred * cred,struct nfs4_slot * slot,bool is_privileged)9598 static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
9599 		const struct cred *cred,
9600 		struct nfs4_slot *slot,
9601 		bool is_privileged)
9602 {
9603 	struct nfs4_sequence_data *calldata;
9604 	struct rpc_message msg = {
9605 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9606 		.rpc_cred = cred,
9607 	};
9608 	struct rpc_task_setup task_setup_data = {
9609 		.rpc_client = clp->cl_rpcclient,
9610 		.rpc_message = &msg,
9611 		.callback_ops = &nfs41_sequence_ops,
9612 		.flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE,
9613 	};
9614 	struct rpc_task *ret;
9615 
9616 	ret = ERR_PTR(-EIO);
9617 	if (!refcount_inc_not_zero(&clp->cl_count))
9618 		goto out_err;
9619 
9620 	ret = ERR_PTR(-ENOMEM);
9621 	calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
9622 	if (calldata == NULL)
9623 		goto out_put_clp;
9624 	nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
9625 	nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
9626 	msg.rpc_argp = &calldata->args;
9627 	msg.rpc_resp = &calldata->res;
9628 	calldata->clp = clp;
9629 	task_setup_data.callback_data = calldata;
9630 
9631 	ret = rpc_run_task(&task_setup_data);
9632 	if (IS_ERR(ret))
9633 		goto out_err;
9634 	return ret;
9635 out_put_clp:
9636 	nfs_put_client(clp);
9637 out_err:
9638 	nfs41_release_slot(slot);
9639 	return ret;
9640 }
9641 
nfs41_proc_async_sequence(struct nfs_client * clp,const struct cred * cred,unsigned renew_flags)9642 static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
9643 {
9644 	struct rpc_task *task;
9645 	int ret = 0;
9646 
9647 	if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
9648 		return -EAGAIN;
9649 	task = _nfs41_proc_sequence(clp, cred, NULL, false);
9650 	if (IS_ERR(task))
9651 		ret = PTR_ERR(task);
9652 	else
9653 		rpc_put_task_async(task);
9654 	dprintk("<-- %s status=%d\n", __func__, ret);
9655 	return ret;
9656 }
9657 
nfs4_proc_sequence(struct nfs_client * clp,const struct cred * cred)9658 static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
9659 {
9660 	struct rpc_task *task;
9661 	int ret;
9662 
9663 	task = _nfs41_proc_sequence(clp, cred, NULL, true);
9664 	if (IS_ERR(task)) {
9665 		ret = PTR_ERR(task);
9666 		goto out;
9667 	}
9668 	ret = rpc_wait_for_completion_task(task);
9669 	if (!ret)
9670 		ret = task->tk_status;
9671 	rpc_put_task(task);
9672 out:
9673 	dprintk("<-- %s status=%d\n", __func__, ret);
9674 	return ret;
9675 }
9676 
9677 struct nfs4_reclaim_complete_data {
9678 	struct nfs_client *clp;
9679 	struct nfs41_reclaim_complete_args arg;
9680 	struct nfs41_reclaim_complete_res res;
9681 };
9682 
nfs4_reclaim_complete_prepare(struct rpc_task * task,void * data)9683 static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9684 {
9685 	struct nfs4_reclaim_complete_data *calldata = data;
9686 
9687 	nfs4_setup_sequence(calldata->clp,
9688 			&calldata->arg.seq_args,
9689 			&calldata->res.seq_res,
9690 			task);
9691 }
9692 
nfs41_reclaim_complete_handle_errors(struct rpc_task * task,struct nfs_client * clp)9693 static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9694 {
9695 	switch(task->tk_status) {
9696 	case 0:
9697 		wake_up_all(&clp->cl_lock_waitq);
9698 		fallthrough;
9699 	case -NFS4ERR_COMPLETE_ALREADY:
9700 	case -NFS4ERR_WRONG_CRED: /* What to do here? */
9701 		break;
9702 	case -NFS4ERR_DELAY:
9703 		rpc_delay(task, NFS4_POLL_RETRY_MAX);
9704 		fallthrough;
9705 	case -NFS4ERR_RETRY_UNCACHED_REP:
9706 	case -EACCES:
9707 		dprintk("%s: failed to reclaim complete error %d for server %s, retrying\n",
9708 			__func__, task->tk_status, clp->cl_hostname);
9709 		return -EAGAIN;
9710 	case -NFS4ERR_BADSESSION:
9711 	case -NFS4ERR_DEADSESSION:
9712 	case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9713 		break;
9714 	default:
9715 		nfs4_schedule_lease_recovery(clp);
9716 	}
9717 	return 0;
9718 }
9719 
nfs4_reclaim_complete_done(struct rpc_task * task,void * data)9720 static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9721 {
9722 	struct nfs4_reclaim_complete_data *calldata = data;
9723 	struct nfs_client *clp = calldata->clp;
9724 	struct nfs4_sequence_res *res = &calldata->res.seq_res;
9725 
9726 	if (!nfs41_sequence_done(task, res))
9727 		return;
9728 
9729 	trace_nfs4_reclaim_complete(clp, task->tk_status);
9730 	if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9731 		rpc_restart_call_prepare(task);
9732 		return;
9733 	}
9734 }
9735 
nfs4_free_reclaim_complete_data(void * data)9736 static void nfs4_free_reclaim_complete_data(void *data)
9737 {
9738 	struct nfs4_reclaim_complete_data *calldata = data;
9739 
9740 	kfree(calldata);
9741 }
9742 
9743 static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9744 	.rpc_call_prepare = nfs4_reclaim_complete_prepare,
9745 	.rpc_call_done = nfs4_reclaim_complete_done,
9746 	.rpc_release = nfs4_free_reclaim_complete_data,
9747 };
9748 
9749 /*
9750  * Issue a global reclaim complete.
9751  */
nfs41_proc_reclaim_complete(struct nfs_client * clp,const struct cred * cred)9752 static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
9753 		const struct cred *cred)
9754 {
9755 	struct nfs4_reclaim_complete_data *calldata;
9756 	struct rpc_message msg = {
9757 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
9758 		.rpc_cred = cred,
9759 	};
9760 	struct rpc_task_setup task_setup_data = {
9761 		.rpc_client = clp->cl_rpcclient,
9762 		.rpc_message = &msg,
9763 		.callback_ops = &nfs4_reclaim_complete_call_ops,
9764 		.flags = RPC_TASK_NO_ROUND_ROBIN,
9765 	};
9766 	int status = -ENOMEM;
9767 
9768 	calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
9769 	if (calldata == NULL)
9770 		goto out;
9771 	calldata->clp = clp;
9772 	calldata->arg.one_fs = 0;
9773 
9774 	nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
9775 	msg.rpc_argp = &calldata->arg;
9776 	msg.rpc_resp = &calldata->res;
9777 	task_setup_data.callback_data = calldata;
9778 	status = nfs4_call_sync_custom(&task_setup_data);
9779 out:
9780 	dprintk("<-- %s status=%d\n", __func__, status);
9781 	return status;
9782 }
9783 
9784 static void
nfs4_layoutget_prepare(struct rpc_task * task,void * calldata)9785 nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9786 {
9787 	struct nfs4_layoutget *lgp = calldata;
9788 	struct nfs_server *server = NFS_SERVER(lgp->args.inode);
9789 
9790 	nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
9791 				&lgp->res.seq_res, task);
9792 }
9793 
nfs4_layoutget_done(struct rpc_task * task,void * calldata)9794 static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9795 {
9796 	struct nfs4_layoutget *lgp = calldata;
9797 
9798 	nfs41_sequence_process(task, &lgp->res.seq_res);
9799 }
9800 
9801 static int
nfs4_layoutget_handle_exception(struct rpc_task * task,struct nfs4_layoutget * lgp,struct nfs4_exception * exception)9802 nfs4_layoutget_handle_exception(struct rpc_task *task,
9803 		struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9804 {
9805 	struct inode *inode = lgp->args.inode;
9806 	struct nfs_server *server = NFS_SERVER(inode);
9807 	struct pnfs_layout_hdr *lo = lgp->lo;
9808 	int nfs4err = task->tk_status;
9809 	int err, status = 0;
9810 	LIST_HEAD(head);
9811 
9812 	dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
9813 
9814 	nfs4_sequence_free_slot(&lgp->res.seq_res);
9815 
9816 	exception->state = NULL;
9817 	exception->stateid = NULL;
9818 
9819 	switch (nfs4err) {
9820 	case 0:
9821 		goto out;
9822 
9823 	/*
9824 	 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9825 	 * on the file. set tk_status to -ENODATA to tell upper layer to
9826 	 * retry go inband.
9827 	 */
9828 	case -NFS4ERR_LAYOUTUNAVAILABLE:
9829 		status = -ENODATA;
9830 		goto out;
9831 	/*
9832 	 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9833 	 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9834 	 */
9835 	case -NFS4ERR_BADLAYOUT:
9836 		status = -EOVERFLOW;
9837 		goto out;
9838 	/*
9839 	 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
9840 	 * (or clients) writing to the same RAID stripe except when
9841 	 * the minlength argument is 0 (see RFC5661 section 18.43.3).
9842 	 *
9843 	 * Treat it like we would RECALLCONFLICT -- we retry for a little
9844 	 * while, and then eventually give up.
9845 	 */
9846 	case -NFS4ERR_LAYOUTTRYLATER:
9847 		if (lgp->args.minlength == 0) {
9848 			status = -EOVERFLOW;
9849 			goto out;
9850 		}
9851 		status = -EBUSY;
9852 		break;
9853 	case -NFS4ERR_RECALLCONFLICT:
9854 	case -NFS4ERR_RETURNCONFLICT:
9855 		status = -ERECALLCONFLICT;
9856 		break;
9857 	case -NFS4ERR_DELEG_REVOKED:
9858 	case -NFS4ERR_ADMIN_REVOKED:
9859 	case -NFS4ERR_EXPIRED:
9860 	case -NFS4ERR_BAD_STATEID:
9861 		exception->timeout = 0;
9862 		spin_lock(&inode->i_lock);
9863 		/* If the open stateid was bad, then recover it. */
9864 		if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
9865 		    !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
9866 			spin_unlock(&inode->i_lock);
9867 			exception->state = lgp->args.ctx->state;
9868 			exception->stateid = &lgp->args.stateid;
9869 			break;
9870 		}
9871 
9872 		/*
9873 		 * Mark the bad layout state as invalid, then retry
9874 		 */
9875 		pnfs_mark_layout_stateid_invalid(lo, &head);
9876 		spin_unlock(&inode->i_lock);
9877 		nfs_commit_inode(inode, 0);
9878 		pnfs_free_lseg_list(&head);
9879 		status = -EAGAIN;
9880 		goto out;
9881 	}
9882 
9883 	err = nfs4_handle_exception(server, nfs4err, exception);
9884 	if (!status) {
9885 		if (exception->retry)
9886 			status = -EAGAIN;
9887 		else
9888 			status = err;
9889 	}
9890 out:
9891 	return status;
9892 }
9893 
max_response_pages(struct nfs_server * server)9894 size_t max_response_pages(struct nfs_server *server)
9895 {
9896 	u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9897 	return nfs_page_array_len(0, max_resp_sz);
9898 }
9899 
nfs4_layoutget_release(void * calldata)9900 static void nfs4_layoutget_release(void *calldata)
9901 {
9902 	struct nfs4_layoutget *lgp = calldata;
9903 
9904 	nfs4_sequence_free_slot(&lgp->res.seq_res);
9905 	pnfs_layoutget_free(lgp);
9906 }
9907 
9908 static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9909 	.rpc_call_prepare = nfs4_layoutget_prepare,
9910 	.rpc_call_done = nfs4_layoutget_done,
9911 	.rpc_release = nfs4_layoutget_release,
9912 };
9913 
9914 struct pnfs_layout_segment *
nfs4_proc_layoutget(struct nfs4_layoutget * lgp,struct nfs4_exception * exception)9915 nfs4_proc_layoutget(struct nfs4_layoutget *lgp,
9916 		    struct nfs4_exception *exception)
9917 {
9918 	struct inode *inode = lgp->args.inode;
9919 	struct nfs_server *server = NFS_SERVER(inode);
9920 	struct rpc_task *task;
9921 	struct rpc_message msg = {
9922 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9923 		.rpc_argp = &lgp->args,
9924 		.rpc_resp = &lgp->res,
9925 		.rpc_cred = lgp->cred,
9926 	};
9927 	struct rpc_task_setup task_setup_data = {
9928 		.rpc_client = server->client,
9929 		.rpc_message = &msg,
9930 		.callback_ops = &nfs4_layoutget_call_ops,
9931 		.callback_data = lgp,
9932 		.flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF |
9933 			 RPC_TASK_MOVEABLE,
9934 	};
9935 	struct pnfs_layout_segment *lseg = NULL;
9936 	int status = 0;
9937 
9938 	nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
9939 	exception->retry = 0;
9940 
9941 	task = rpc_run_task(&task_setup_data);
9942 	if (IS_ERR(task))
9943 		return ERR_CAST(task);
9944 
9945 	status = rpc_wait_for_completion_task(task);
9946 	if (status != 0)
9947 		goto out;
9948 
9949 	if (task->tk_status < 0) {
9950 		exception->retry = 1;
9951 		status = nfs4_layoutget_handle_exception(task, lgp, exception);
9952 	} else if (lgp->res.layoutp->len == 0) {
9953 		exception->retry = 1;
9954 		status = -EAGAIN;
9955 		nfs4_update_delay(&exception->timeout);
9956 	} else
9957 		lseg = pnfs_layout_process(lgp);
9958 out:
9959 	trace_nfs4_layoutget(lgp->args.ctx,
9960 			&lgp->args.range,
9961 			&lgp->res.range,
9962 			&lgp->res.stateid,
9963 			status);
9964 
9965 	rpc_put_task(task);
9966 	dprintk("<-- %s status=%d\n", __func__, status);
9967 	if (status)
9968 		return ERR_PTR(status);
9969 	return lseg;
9970 }
9971 
9972 static void
nfs4_layoutreturn_prepare(struct rpc_task * task,void * calldata)9973 nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9974 {
9975 	struct nfs4_layoutreturn *lrp = calldata;
9976 
9977 	nfs4_setup_sequence(lrp->clp,
9978 			&lrp->args.seq_args,
9979 			&lrp->res.seq_res,
9980 			task);
9981 	if (!pnfs_layout_is_valid(lrp->args.layout))
9982 		rpc_exit(task, 0);
9983 }
9984 
nfs4_layoutreturn_done(struct rpc_task * task,void * calldata)9985 static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9986 {
9987 	struct nfs4_layoutreturn *lrp = calldata;
9988 	struct nfs_server *server;
9989 
9990 	if (!nfs41_sequence_process(task, &lrp->res.seq_res))
9991 		return;
9992 
9993 	if (task->tk_rpc_status == -ETIMEDOUT) {
9994 		lrp->rpc_status = -EAGAIN;
9995 		lrp->res.lrs_present = 0;
9996 		return;
9997 	}
9998 	/*
9999 	 * Was there an RPC level error? Assume the call succeeded,
10000 	 * and that we need to release the layout
10001 	 */
10002 	if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
10003 		lrp->res.lrs_present = 0;
10004 		return;
10005 	}
10006 
10007 	server = NFS_SERVER(lrp->args.inode);
10008 	switch (task->tk_status) {
10009 	case -NFS4ERR_OLD_STATEID:
10010 		if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
10011 					&lrp->args.range,
10012 					lrp->args.inode))
10013 			goto out_restart;
10014 		fallthrough;
10015 	default:
10016 		task->tk_status = 0;
10017 		lrp->res.lrs_present = 0;
10018 		fallthrough;
10019 	case 0:
10020 		break;
10021 	case -NFS4ERR_BADSESSION:
10022 	case -NFS4ERR_DEADSESSION:
10023 	case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10024 		nfs4_schedule_session_recovery(server->nfs_client->cl_session,
10025 					       task->tk_status);
10026 		lrp->res.lrs_present = 0;
10027 		lrp->rpc_status = -EAGAIN;
10028 		task->tk_status = 0;
10029 		break;
10030 	case -NFS4ERR_DELAY:
10031 		if (nfs4_async_handle_error(task, server, NULL, NULL) ==
10032 		    -EAGAIN)
10033 			goto out_restart;
10034 		lrp->res.lrs_present = 0;
10035 		break;
10036 	}
10037 	return;
10038 out_restart:
10039 	task->tk_status = 0;
10040 	nfs4_sequence_free_slot(&lrp->res.seq_res);
10041 	rpc_restart_call_prepare(task);
10042 }
10043 
nfs4_layoutreturn_release(void * calldata)10044 static void nfs4_layoutreturn_release(void *calldata)
10045 {
10046 	struct nfs4_layoutreturn *lrp = calldata;
10047 	struct pnfs_layout_hdr *lo = lrp->args.layout;
10048 
10049 	if (lrp->rpc_status == 0 || !lrp->inode)
10050 		pnfs_layoutreturn_free_lsegs(
10051 			lo, &lrp->args.stateid, &lrp->args.range,
10052 			lrp->res.lrs_present ? &lrp->res.stateid : NULL);
10053 	else
10054 		pnfs_layoutreturn_retry_later(lo, &lrp->args.stateid,
10055 					      &lrp->args.range);
10056 	nfs4_sequence_free_slot(&lrp->res.seq_res);
10057 	if (lrp->ld_private.ops && lrp->ld_private.ops->free)
10058 		lrp->ld_private.ops->free(&lrp->ld_private);
10059 	pnfs_put_layout_hdr(lrp->args.layout);
10060 	nfs_iput_and_deactive(lrp->inode);
10061 	put_cred(lrp->cred);
10062 	kfree(calldata);
10063 }
10064 
10065 static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
10066 	.rpc_call_prepare = nfs4_layoutreturn_prepare,
10067 	.rpc_call_done = nfs4_layoutreturn_done,
10068 	.rpc_release = nfs4_layoutreturn_release,
10069 };
10070 
nfs4_proc_layoutreturn(struct nfs4_layoutreturn * lrp,unsigned int flags)10071 int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, unsigned int flags)
10072 {
10073 	struct rpc_task *task;
10074 	struct rpc_message msg = {
10075 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
10076 		.rpc_argp = &lrp->args,
10077 		.rpc_resp = &lrp->res,
10078 		.rpc_cred = lrp->cred,
10079 	};
10080 	struct rpc_task_setup task_setup_data = {
10081 		.rpc_client = NFS_SERVER(lrp->args.inode)->client,
10082 		.rpc_message = &msg,
10083 		.callback_ops = &nfs4_layoutreturn_call_ops,
10084 		.callback_data = lrp,
10085 		.flags = RPC_TASK_MOVEABLE,
10086 	};
10087 	int status = 0;
10088 
10089 	nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
10090 			NFS_SP4_MACH_CRED_PNFS_CLEANUP,
10091 			&task_setup_data.rpc_client, &msg);
10092 
10093 	lrp->inode = nfs_igrab_and_active(lrp->args.inode);
10094 	if (flags & PNFS_FL_LAYOUTRETURN_ASYNC) {
10095 		if (!lrp->inode) {
10096 			nfs4_layoutreturn_release(lrp);
10097 			return -EAGAIN;
10098 		}
10099 		task_setup_data.flags |= RPC_TASK_ASYNC;
10100 	}
10101 	if (!lrp->inode)
10102 		flags |= PNFS_FL_LAYOUTRETURN_PRIVILEGED;
10103 	if (flags & PNFS_FL_LAYOUTRETURN_PRIVILEGED)
10104 		nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
10105 				   1);
10106 	else
10107 		nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1,
10108 				   0);
10109 	task = rpc_run_task(&task_setup_data);
10110 	if (IS_ERR(task))
10111 		return PTR_ERR(task);
10112 	if (!(flags & PNFS_FL_LAYOUTRETURN_ASYNC))
10113 		status = task->tk_status;
10114 	trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
10115 	dprintk("<-- %s status=%d\n", __func__, status);
10116 	rpc_put_task(task);
10117 	return status;
10118 }
10119 
10120 static int
_nfs4_proc_getdeviceinfo(struct nfs_server * server,struct pnfs_device * pdev,const struct cred * cred)10121 _nfs4_proc_getdeviceinfo(struct nfs_server *server,
10122 		struct pnfs_device *pdev,
10123 		const struct cred *cred)
10124 {
10125 	struct nfs4_getdeviceinfo_args args = {
10126 		.pdev = pdev,
10127 		.notify_types = NOTIFY_DEVICEID4_CHANGE |
10128 			NOTIFY_DEVICEID4_DELETE,
10129 	};
10130 	struct nfs4_getdeviceinfo_res res = {
10131 		.pdev = pdev,
10132 	};
10133 	struct rpc_message msg = {
10134 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
10135 		.rpc_argp = &args,
10136 		.rpc_resp = &res,
10137 		.rpc_cred = cred,
10138 	};
10139 	int status;
10140 
10141 	status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
10142 	if (res.notification & ~args.notify_types)
10143 		dprintk("%s: unsupported notification\n", __func__);
10144 	if (res.notification != args.notify_types)
10145 		pdev->nocache = 1;
10146 
10147 	trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status);
10148 
10149 	dprintk("<-- %s status=%d\n", __func__, status);
10150 
10151 	return status;
10152 }
10153 
nfs4_proc_getdeviceinfo(struct nfs_server * server,struct pnfs_device * pdev,const struct cred * cred)10154 int nfs4_proc_getdeviceinfo(struct nfs_server *server,
10155 		struct pnfs_device *pdev,
10156 		const struct cred *cred)
10157 {
10158 	struct nfs4_exception exception = { };
10159 	int err;
10160 
10161 	do {
10162 		err = nfs4_handle_exception(server,
10163 					_nfs4_proc_getdeviceinfo(server, pdev, cred),
10164 					&exception);
10165 	} while (exception.retry);
10166 	return err;
10167 }
10168 EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
10169 
nfs4_layoutcommit_prepare(struct rpc_task * task,void * calldata)10170 static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
10171 {
10172 	struct nfs4_layoutcommit_data *data = calldata;
10173 	struct nfs_server *server = NFS_SERVER(data->args.inode);
10174 
10175 	nfs4_setup_sequence(server->nfs_client,
10176 			&data->args.seq_args,
10177 			&data->res.seq_res,
10178 			task);
10179 }
10180 
10181 static void
nfs4_layoutcommit_done(struct rpc_task * task,void * calldata)10182 nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
10183 {
10184 	struct nfs4_layoutcommit_data *data = calldata;
10185 	struct nfs_server *server = NFS_SERVER(data->args.inode);
10186 
10187 	if (!nfs41_sequence_done(task, &data->res.seq_res))
10188 		return;
10189 
10190 	switch (task->tk_status) { /* Just ignore these failures */
10191 	case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
10192 	case -NFS4ERR_BADIOMODE:     /* no IOMODE_RW layout for range */
10193 	case -NFS4ERR_BADLAYOUT:     /* no layout */
10194 	case -NFS4ERR_GRACE:	    /* loca_recalim always false */
10195 		task->tk_status = 0;
10196 		break;
10197 	case 0:
10198 		break;
10199 	default:
10200 		if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
10201 			rpc_restart_call_prepare(task);
10202 			return;
10203 		}
10204 	}
10205 }
10206 
nfs4_layoutcommit_release(void * calldata)10207 static void nfs4_layoutcommit_release(void *calldata)
10208 {
10209 	struct nfs4_layoutcommit_data *data = calldata;
10210 
10211 	pnfs_cleanup_layoutcommit(data);
10212 	nfs_post_op_update_inode_force_wcc(data->args.inode,
10213 					   data->res.fattr);
10214 	put_cred(data->cred);
10215 	nfs_iput_and_deactive(data->inode);
10216 	kfree(data);
10217 }
10218 
10219 static const struct rpc_call_ops nfs4_layoutcommit_ops = {
10220 	.rpc_call_prepare = nfs4_layoutcommit_prepare,
10221 	.rpc_call_done = nfs4_layoutcommit_done,
10222 	.rpc_release = nfs4_layoutcommit_release,
10223 };
10224 
10225 int
nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data * data,bool sync)10226 nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
10227 {
10228 	struct rpc_message msg = {
10229 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
10230 		.rpc_argp = &data->args,
10231 		.rpc_resp = &data->res,
10232 		.rpc_cred = data->cred,
10233 	};
10234 	struct rpc_task_setup task_setup_data = {
10235 		.task = &data->task,
10236 		.rpc_client = NFS_CLIENT(data->args.inode),
10237 		.rpc_message = &msg,
10238 		.callback_ops = &nfs4_layoutcommit_ops,
10239 		.callback_data = data,
10240 		.flags = RPC_TASK_MOVEABLE,
10241 	};
10242 	struct rpc_task *task;
10243 	int status = 0;
10244 
10245 	dprintk("NFS: initiating layoutcommit call. sync %d "
10246 		"lbw: %llu inode %lu\n", sync,
10247 		data->args.lastbytewritten,
10248 		data->args.inode->i_ino);
10249 
10250 	if (!sync) {
10251 		data->inode = nfs_igrab_and_active(data->args.inode);
10252 		if (data->inode == NULL) {
10253 			nfs4_layoutcommit_release(data);
10254 			return -EAGAIN;
10255 		}
10256 		task_setup_data.flags = RPC_TASK_ASYNC;
10257 	}
10258 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
10259 	task = rpc_run_task(&task_setup_data);
10260 	if (IS_ERR(task))
10261 		return PTR_ERR(task);
10262 	if (sync)
10263 		status = task->tk_status;
10264 	trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
10265 	dprintk("%s: status %d\n", __func__, status);
10266 	rpc_put_task(task);
10267 	return status;
10268 }
10269 
10270 /*
10271  * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
10272  * possible) as per RFC3530bis and RFC5661 Security Considerations sections
10273  */
10274 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)10275 _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10276 		    struct nfs_fsinfo *info,
10277 		    struct nfs4_secinfo_flavors *flavors, bool use_integrity)
10278 {
10279 	struct nfs41_secinfo_no_name_args args = {
10280 		.style = SECINFO_STYLE_CURRENT_FH,
10281 	};
10282 	struct nfs4_secinfo_res res = {
10283 		.flavors = flavors,
10284 	};
10285 	struct rpc_message msg = {
10286 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
10287 		.rpc_argp = &args,
10288 		.rpc_resp = &res,
10289 	};
10290 	struct nfs4_call_sync_data data = {
10291 		.seq_server = server,
10292 		.seq_args = &args.seq_args,
10293 		.seq_res = &res.seq_res,
10294 	};
10295 	struct rpc_task_setup task_setup = {
10296 		.rpc_client = server->client,
10297 		.rpc_message = &msg,
10298 		.callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
10299 		.callback_data = &data,
10300 		.flags = RPC_TASK_NO_ROUND_ROBIN,
10301 	};
10302 	const struct cred *cred = NULL;
10303 	int status;
10304 
10305 	if (use_integrity) {
10306 		task_setup.rpc_client = server->nfs_client->cl_rpcclient;
10307 
10308 		cred = nfs4_get_clid_cred(server->nfs_client);
10309 		msg.rpc_cred = cred;
10310 	}
10311 
10312 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
10313 	status = nfs4_call_sync_custom(&task_setup);
10314 	dprintk("<-- %s status=%d\n", __func__, status);
10315 
10316 	put_cred(cred);
10317 
10318 	return status;
10319 }
10320 
10321 static int
nfs41_proc_secinfo_no_name(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * info,struct nfs4_secinfo_flavors * flavors)10322 nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
10323 			   struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
10324 {
10325 	struct nfs4_exception exception = {
10326 		.interruptible = true,
10327 	};
10328 	int err;
10329 	do {
10330 		/* first try using integrity protection */
10331 		err = -NFS4ERR_WRONGSEC;
10332 
10333 		/* try to use integrity protection with machine cred */
10334 		if (_nfs4_is_integrity_protected(server->nfs_client))
10335 			err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10336 							  flavors, true);
10337 
10338 		/*
10339 		 * if unable to use integrity protection, or SECINFO with
10340 		 * integrity protection returns NFS4ERR_WRONGSEC (which is
10341 		 * disallowed by spec, but exists in deployed servers) use
10342 		 * the current filesystem's rpc_client and the user cred.
10343 		 */
10344 		if (err == -NFS4ERR_WRONGSEC)
10345 			err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
10346 							  flavors, false);
10347 
10348 		switch (err) {
10349 		case 0:
10350 		case -NFS4ERR_WRONGSEC:
10351 		case -ENOTSUPP:
10352 			goto out;
10353 		default:
10354 			err = nfs4_handle_exception(server, err, &exception);
10355 		}
10356 	} while (exception.retry);
10357 out:
10358 	return err;
10359 }
10360 
10361 static int
nfs41_find_root_sec(struct nfs_server * server,struct nfs_fh * fhandle,struct nfs_fsinfo * info)10362 nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
10363 		    struct nfs_fsinfo *info)
10364 {
10365 	int err;
10366 	struct page *page;
10367 	rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
10368 	struct nfs4_secinfo_flavors *flavors;
10369 	struct nfs4_secinfo4 *secinfo;
10370 	int i;
10371 
10372 	page = alloc_page(GFP_KERNEL);
10373 	if (!page) {
10374 		err = -ENOMEM;
10375 		goto out;
10376 	}
10377 
10378 	flavors = page_address(page);
10379 	err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
10380 
10381 	/*
10382 	 * Fall back on "guess and check" method if
10383 	 * the server doesn't support SECINFO_NO_NAME
10384 	 */
10385 	if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
10386 		err = nfs4_find_root_sec(server, fhandle, info);
10387 		goto out_freepage;
10388 	}
10389 	if (err)
10390 		goto out_freepage;
10391 
10392 	for (i = 0; i < flavors->num_flavors; i++) {
10393 		secinfo = &flavors->flavors[i];
10394 
10395 		switch (secinfo->flavor) {
10396 		case RPC_AUTH_NULL:
10397 		case RPC_AUTH_UNIX:
10398 		case RPC_AUTH_GSS:
10399 			flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
10400 					&secinfo->flavor_info);
10401 			break;
10402 		default:
10403 			flavor = RPC_AUTH_MAXFLAVOR;
10404 			break;
10405 		}
10406 
10407 		if (!nfs_auth_info_match(&server->auth_info, flavor))
10408 			flavor = RPC_AUTH_MAXFLAVOR;
10409 
10410 		if (flavor != RPC_AUTH_MAXFLAVOR) {
10411 			err = nfs4_lookup_root_sec(server, fhandle,
10412 						   info, flavor);
10413 			if (!err)
10414 				break;
10415 		}
10416 	}
10417 
10418 	if (flavor == RPC_AUTH_MAXFLAVOR)
10419 		err = -EPERM;
10420 
10421 out_freepage:
10422 	put_page(page);
10423 	if (err == -EACCES)
10424 		return -EPERM;
10425 out:
10426 	return err;
10427 }
10428 
_nfs41_test_stateid(struct nfs_server * server,const nfs4_stateid * stateid,const struct cred * cred)10429 static int _nfs41_test_stateid(struct nfs_server *server,
10430 			       const nfs4_stateid *stateid,
10431 			       const struct cred *cred)
10432 {
10433 	int status;
10434 	struct nfs41_test_stateid_args args = {
10435 		.stateid = *stateid,
10436 	};
10437 	struct nfs41_test_stateid_res res;
10438 	struct rpc_message msg = {
10439 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
10440 		.rpc_argp = &args,
10441 		.rpc_resp = &res,
10442 		.rpc_cred = cred,
10443 	};
10444 	struct rpc_clnt *rpc_client = server->client;
10445 
10446 	nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10447 		&rpc_client, &msg);
10448 
10449 	dprintk("NFS call  test_stateid %p\n", stateid);
10450 	nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
10451 	status = nfs4_call_sync_sequence(rpc_client, server, &msg,
10452 			&args.seq_args, &res.seq_res);
10453 	if (status != NFS_OK) {
10454 		dprintk("NFS reply test_stateid: failed, %d\n", status);
10455 		return status;
10456 	}
10457 	dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
10458 	return -res.status;
10459 }
10460 
nfs4_handle_delay_or_session_error(struct nfs_server * server,int err,struct nfs4_exception * exception)10461 static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
10462 		int err, struct nfs4_exception *exception)
10463 {
10464 	exception->retry = 0;
10465 	switch(err) {
10466 	case -NFS4ERR_DELAY:
10467 	case -NFS4ERR_RETRY_UNCACHED_REP:
10468 		nfs4_handle_exception(server, err, exception);
10469 		break;
10470 	case -NFS4ERR_BADSESSION:
10471 	case -NFS4ERR_BADSLOT:
10472 	case -NFS4ERR_BAD_HIGH_SLOT:
10473 	case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
10474 	case -NFS4ERR_DEADSESSION:
10475 		nfs4_do_handle_exception(server, err, exception);
10476 	}
10477 }
10478 
10479 /**
10480  * nfs41_test_stateid - perform a TEST_STATEID operation
10481  *
10482  * @server: server / transport on which to perform the operation
10483  * @stateid: state ID to test
10484  * @cred: credential
10485  *
10486  * Returns NFS_OK if the server recognizes that "stateid" is valid.
10487  * Otherwise a negative NFS4ERR value is returned if the operation
10488  * failed or the state ID is not currently valid.
10489  */
nfs41_test_stateid(struct nfs_server * server,const nfs4_stateid * stateid,const struct cred * cred)10490 static int nfs41_test_stateid(struct nfs_server *server,
10491 			      const nfs4_stateid *stateid,
10492 			      const struct cred *cred)
10493 {
10494 	struct nfs4_exception exception = {
10495 		.interruptible = true,
10496 	};
10497 	int err;
10498 	do {
10499 		err = _nfs41_test_stateid(server, stateid, cred);
10500 		nfs4_handle_delay_or_session_error(server, err, &exception);
10501 	} while (exception.retry);
10502 	return err;
10503 }
10504 
10505 struct nfs_free_stateid_data {
10506 	struct nfs_server *server;
10507 	struct nfs41_free_stateid_args args;
10508 	struct nfs41_free_stateid_res res;
10509 };
10510 
nfs41_free_stateid_prepare(struct rpc_task * task,void * calldata)10511 static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
10512 {
10513 	struct nfs_free_stateid_data *data = calldata;
10514 	nfs4_setup_sequence(data->server->nfs_client,
10515 			&data->args.seq_args,
10516 			&data->res.seq_res,
10517 			task);
10518 }
10519 
nfs41_free_stateid_done(struct rpc_task * task,void * calldata)10520 static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
10521 {
10522 	struct nfs_free_stateid_data *data = calldata;
10523 
10524 	nfs41_sequence_done(task, &data->res.seq_res);
10525 
10526 	switch (task->tk_status) {
10527 	case -NFS4ERR_DELAY:
10528 		if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
10529 			rpc_restart_call_prepare(task);
10530 	}
10531 }
10532 
nfs41_free_stateid_release(void * calldata)10533 static void nfs41_free_stateid_release(void *calldata)
10534 {
10535 	struct nfs_free_stateid_data *data = calldata;
10536 	struct nfs_client *clp = data->server->nfs_client;
10537 
10538 	nfs_put_client(clp);
10539 	kfree(calldata);
10540 }
10541 
10542 static const struct rpc_call_ops nfs41_free_stateid_ops = {
10543 	.rpc_call_prepare = nfs41_free_stateid_prepare,
10544 	.rpc_call_done = nfs41_free_stateid_done,
10545 	.rpc_release = nfs41_free_stateid_release,
10546 };
10547 
10548 /**
10549  * nfs41_free_stateid - perform a FREE_STATEID operation
10550  *
10551  * @server: server / transport on which to perform the operation
10552  * @stateid: state ID to release
10553  * @cred: credential
10554  * @privileged: set to true if this call needs to be privileged
10555  *
10556  * Note: this function is always asynchronous.
10557  */
nfs41_free_stateid(struct nfs_server * server,const nfs4_stateid * stateid,const struct cred * cred,bool privileged)10558 static int nfs41_free_stateid(struct nfs_server *server,
10559 		const nfs4_stateid *stateid,
10560 		const struct cred *cred,
10561 		bool privileged)
10562 {
10563 	struct rpc_message msg = {
10564 		.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
10565 		.rpc_cred = cred,
10566 	};
10567 	struct rpc_task_setup task_setup = {
10568 		.rpc_client = server->client,
10569 		.rpc_message = &msg,
10570 		.callback_ops = &nfs41_free_stateid_ops,
10571 		.flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE,
10572 	};
10573 	struct nfs_free_stateid_data *data;
10574 	struct rpc_task *task;
10575 	struct nfs_client *clp = server->nfs_client;
10576 
10577 	if (!refcount_inc_not_zero(&clp->cl_count))
10578 		return -EIO;
10579 
10580 	nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
10581 		&task_setup.rpc_client, &msg);
10582 
10583 	dprintk("NFS call  free_stateid %p\n", stateid);
10584 	data = kmalloc(sizeof(*data), GFP_KERNEL);
10585 	if (!data)
10586 		return -ENOMEM;
10587 	data->server = server;
10588 	nfs4_stateid_copy(&data->args.stateid, stateid);
10589 
10590 	task_setup.callback_data = data;
10591 
10592 	msg.rpc_argp = &data->args;
10593 	msg.rpc_resp = &data->res;
10594 	nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
10595 	task = rpc_run_task(&task_setup);
10596 	if (IS_ERR(task))
10597 		return PTR_ERR(task);
10598 	rpc_put_task(task);
10599 	return 0;
10600 }
10601 
10602 static void
nfs41_free_lock_state(struct nfs_server * server,struct nfs4_lock_state * lsp)10603 nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
10604 {
10605 	const struct cred *cred = lsp->ls_state->owner->so_cred;
10606 
10607 	nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
10608 	nfs4_free_lock_state(server, lsp);
10609 }
10610 
nfs41_match_stateid(const nfs4_stateid * s1,const nfs4_stateid * s2)10611 static bool nfs41_match_stateid(const nfs4_stateid *s1,
10612 		const nfs4_stateid *s2)
10613 {
10614 	if (s1->type != s2->type)
10615 		return false;
10616 
10617 	if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
10618 		return false;
10619 
10620 	if (s1->seqid == s2->seqid)
10621 		return true;
10622 
10623 	return s1->seqid == 0 || s2->seqid == 0;
10624 }
10625 
10626 #endif /* CONFIG_NFS_V4_1 */
10627 
nfs4_match_stateid(const nfs4_stateid * s1,const nfs4_stateid * s2)10628 static bool nfs4_match_stateid(const nfs4_stateid *s1,
10629 		const nfs4_stateid *s2)
10630 {
10631 	return nfs4_stateid_match(s1, s2);
10632 }
10633 
10634 
10635 static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
10636 	.owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10637 	.state_flag_bit	= NFS_STATE_RECLAIM_REBOOT,
10638 	.recover_open	= nfs4_open_reclaim,
10639 	.recover_lock	= nfs4_lock_reclaim,
10640 	.establish_clid = nfs4_init_clientid,
10641 	.detect_trunking = nfs40_discover_server_trunking,
10642 };
10643 
10644 #if defined(CONFIG_NFS_V4_1)
10645 static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
10646 	.owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10647 	.state_flag_bit	= NFS_STATE_RECLAIM_REBOOT,
10648 	.recover_open	= nfs4_open_reclaim,
10649 	.recover_lock	= nfs4_lock_reclaim,
10650 	.establish_clid = nfs41_init_clientid,
10651 	.reclaim_complete = nfs41_proc_reclaim_complete,
10652 	.detect_trunking = nfs41_discover_server_trunking,
10653 };
10654 #endif /* CONFIG_NFS_V4_1 */
10655 
10656 static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
10657 	.owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10658 	.state_flag_bit	= NFS_STATE_RECLAIM_NOGRACE,
10659 	.recover_open	= nfs40_open_expired,
10660 	.recover_lock	= nfs4_lock_expired,
10661 	.establish_clid = nfs4_init_clientid,
10662 };
10663 
10664 #if defined(CONFIG_NFS_V4_1)
10665 static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
10666 	.owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10667 	.state_flag_bit	= NFS_STATE_RECLAIM_NOGRACE,
10668 	.recover_open	= nfs41_open_expired,
10669 	.recover_lock	= nfs41_lock_expired,
10670 	.establish_clid = nfs41_init_clientid,
10671 };
10672 #endif /* CONFIG_NFS_V4_1 */
10673 
10674 static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
10675 	.sched_state_renewal = nfs4_proc_async_renew,
10676 	.get_state_renewal_cred = nfs4_get_renew_cred,
10677 	.renew_lease = nfs4_proc_renew,
10678 };
10679 
10680 #if defined(CONFIG_NFS_V4_1)
10681 static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
10682 	.sched_state_renewal = nfs41_proc_async_sequence,
10683 	.get_state_renewal_cred = nfs4_get_machine_cred,
10684 	.renew_lease = nfs4_proc_sequence,
10685 };
10686 #endif
10687 
10688 static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
10689 	.get_locations = _nfs40_proc_get_locations,
10690 	.fsid_present = _nfs40_proc_fsid_present,
10691 };
10692 
10693 #if defined(CONFIG_NFS_V4_1)
10694 static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
10695 	.get_locations = _nfs41_proc_get_locations,
10696 	.fsid_present = _nfs41_proc_fsid_present,
10697 };
10698 #endif	/* CONFIG_NFS_V4_1 */
10699 
10700 static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10701 	.minor_version = 0,
10702 	.init_caps = NFS_CAP_READDIRPLUS
10703 		| NFS_CAP_ATOMIC_OPEN
10704 		| NFS_CAP_POSIX_LOCK,
10705 	.init_client = nfs40_init_client,
10706 	.shutdown_client = nfs40_shutdown_client,
10707 	.match_stateid = nfs4_match_stateid,
10708 	.find_root_sec = nfs4_find_root_sec,
10709 	.free_lock_state = nfs4_release_lockowner,
10710 	.test_and_free_expired = nfs40_test_and_free_expired_stateid,
10711 	.alloc_seqid = nfs_alloc_seqid,
10712 	.call_sync_ops = &nfs40_call_sync_ops,
10713 	.reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10714 	.nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10715 	.state_renewal_ops = &nfs40_state_renewal_ops,
10716 	.mig_recovery_ops = &nfs40_mig_recovery_ops,
10717 };
10718 
10719 #if defined(CONFIG_NFS_V4_1)
10720 static struct nfs_seqid *
nfs_alloc_no_seqid(struct nfs_seqid_counter * arg1,gfp_t arg2)10721 nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10722 {
10723 	return NULL;
10724 }
10725 
10726 static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10727 	.minor_version = 1,
10728 	.init_caps = NFS_CAP_READDIRPLUS
10729 		| NFS_CAP_ATOMIC_OPEN
10730 		| NFS_CAP_POSIX_LOCK
10731 		| NFS_CAP_STATEID_NFSV41
10732 		| NFS_CAP_ATOMIC_OPEN_V1
10733 		| NFS_CAP_LGOPEN
10734 		| NFS_CAP_MOVEABLE,
10735 	.init_client = nfs41_init_client,
10736 	.shutdown_client = nfs41_shutdown_client,
10737 	.match_stateid = nfs41_match_stateid,
10738 	.find_root_sec = nfs41_find_root_sec,
10739 	.free_lock_state = nfs41_free_lock_state,
10740 	.test_and_free_expired = nfs41_test_and_free_expired_stateid,
10741 	.alloc_seqid = nfs_alloc_no_seqid,
10742 	.session_trunk = nfs4_test_session_trunk,
10743 	.call_sync_ops = &nfs41_call_sync_ops,
10744 	.reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10745 	.nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10746 	.state_renewal_ops = &nfs41_state_renewal_ops,
10747 	.mig_recovery_ops = &nfs41_mig_recovery_ops,
10748 };
10749 #endif
10750 
10751 #if defined(CONFIG_NFS_V4_2)
10752 static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10753 	.minor_version = 2,
10754 	.init_caps = NFS_CAP_READDIRPLUS
10755 		| NFS_CAP_ATOMIC_OPEN
10756 		| NFS_CAP_POSIX_LOCK
10757 		| NFS_CAP_STATEID_NFSV41
10758 		| NFS_CAP_ATOMIC_OPEN_V1
10759 		| NFS_CAP_LGOPEN
10760 		| NFS_CAP_ALLOCATE
10761 		| NFS_CAP_COPY
10762 		| NFS_CAP_OFFLOAD_CANCEL
10763 		| NFS_CAP_COPY_NOTIFY
10764 		| NFS_CAP_DEALLOCATE
10765 		| NFS_CAP_SEEK
10766 		| NFS_CAP_LAYOUTSTATS
10767 		| NFS_CAP_CLONE
10768 		| NFS_CAP_LAYOUTERROR
10769 		| NFS_CAP_READ_PLUS
10770 		| NFS_CAP_MOVEABLE,
10771 	.init_client = nfs41_init_client,
10772 	.shutdown_client = nfs41_shutdown_client,
10773 	.match_stateid = nfs41_match_stateid,
10774 	.find_root_sec = nfs41_find_root_sec,
10775 	.free_lock_state = nfs41_free_lock_state,
10776 	.call_sync_ops = &nfs41_call_sync_ops,
10777 	.test_and_free_expired = nfs41_test_and_free_expired_stateid,
10778 	.alloc_seqid = nfs_alloc_no_seqid,
10779 	.session_trunk = nfs4_test_session_trunk,
10780 	.reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10781 	.nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10782 	.state_renewal_ops = &nfs41_state_renewal_ops,
10783 	.mig_recovery_ops = &nfs41_mig_recovery_ops,
10784 };
10785 #endif
10786 
10787 const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10788 	[0] = &nfs_v4_0_minor_ops,
10789 #if defined(CONFIG_NFS_V4_1)
10790 	[1] = &nfs_v4_1_minor_ops,
10791 #endif
10792 #if defined(CONFIG_NFS_V4_2)
10793 	[2] = &nfs_v4_2_minor_ops,
10794 #endif
10795 };
10796 
nfs4_listxattr(struct dentry * dentry,char * list,size_t size)10797 static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
10798 {
10799 	ssize_t error, error2, error3;
10800 	size_t left = size;
10801 
10802 	error = generic_listxattr(dentry, list, left);
10803 	if (error < 0)
10804 		return error;
10805 	if (list) {
10806 		list += error;
10807 		left -= error;
10808 	}
10809 
10810 	error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, left);
10811 	if (error2 < 0)
10812 		return error2;
10813 
10814 	if (list) {
10815 		list += error2;
10816 		left -= error2;
10817 	}
10818 
10819 	error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, left);
10820 	if (error3 < 0)
10821 		return error3;
10822 
10823 	error += error2 + error3;
10824 	if (size && error > size)
10825 		return -ERANGE;
10826 	return error;
10827 }
10828 
nfs4_enable_swap(struct inode * inode)10829 static void nfs4_enable_swap(struct inode *inode)
10830 {
10831 	/* The state manager thread must always be running.
10832 	 * It will notice the client is a swapper, and stay put.
10833 	 */
10834 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10835 
10836 	nfs4_schedule_state_manager(clp);
10837 }
10838 
nfs4_disable_swap(struct inode * inode)10839 static void nfs4_disable_swap(struct inode *inode)
10840 {
10841 	/* The state manager thread will now exit once it is
10842 	 * woken.
10843 	 */
10844 	struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
10845 
10846 	set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
10847 	clear_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state);
10848 	wake_up_var(&clp->cl_state);
10849 }
10850 
10851 static const struct inode_operations nfs4_dir_inode_operations = {
10852 	.create		= nfs_create,
10853 	.lookup		= nfs_lookup,
10854 	.atomic_open	= nfs_atomic_open,
10855 	.link		= nfs_link,
10856 	.unlink		= nfs_unlink,
10857 	.symlink	= nfs_symlink,
10858 	.mkdir		= nfs_mkdir,
10859 	.rmdir		= nfs_rmdir,
10860 	.mknod		= nfs_mknod,
10861 	.rename		= nfs_rename,
10862 	.permission	= nfs_permission,
10863 	.getattr	= nfs_getattr,
10864 	.setattr	= nfs_setattr,
10865 	.listxattr	= nfs4_listxattr,
10866 };
10867 
10868 static const struct inode_operations nfs4_file_inode_operations = {
10869 	.permission	= nfs_permission,
10870 	.getattr	= nfs_getattr,
10871 	.setattr	= nfs_setattr,
10872 	.listxattr	= nfs4_listxattr,
10873 };
10874 
10875 const struct nfs_rpc_ops nfs_v4_clientops = {
10876 	.version	= 4,			/* protocol version */
10877 	.dentry_ops	= &nfs4_dentry_operations,
10878 	.dir_inode_ops	= &nfs4_dir_inode_operations,
10879 	.file_inode_ops	= &nfs4_file_inode_operations,
10880 	.file_ops	= &nfs4_file_operations,
10881 	.getroot	= nfs4_proc_get_root,
10882 	.submount	= nfs4_submount,
10883 	.try_get_tree	= nfs4_try_get_tree,
10884 	.getattr	= nfs4_proc_getattr,
10885 	.setattr	= nfs4_proc_setattr,
10886 	.lookup		= nfs4_proc_lookup,
10887 	.lookupp	= nfs4_proc_lookupp,
10888 	.access		= nfs4_proc_access,
10889 	.readlink	= nfs4_proc_readlink,
10890 	.create		= nfs4_proc_create,
10891 	.remove		= nfs4_proc_remove,
10892 	.unlink_setup	= nfs4_proc_unlink_setup,
10893 	.unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
10894 	.unlink_done	= nfs4_proc_unlink_done,
10895 	.rename_setup	= nfs4_proc_rename_setup,
10896 	.rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
10897 	.rename_done	= nfs4_proc_rename_done,
10898 	.link		= nfs4_proc_link,
10899 	.symlink	= nfs4_proc_symlink,
10900 	.mkdir		= nfs4_proc_mkdir,
10901 	.rmdir		= nfs4_proc_rmdir,
10902 	.readdir	= nfs4_proc_readdir,
10903 	.mknod		= nfs4_proc_mknod,
10904 	.statfs		= nfs4_proc_statfs,
10905 	.fsinfo		= nfs4_proc_fsinfo,
10906 	.pathconf	= nfs4_proc_pathconf,
10907 	.set_capabilities = nfs4_server_capabilities,
10908 	.decode_dirent	= nfs4_decode_dirent,
10909 	.pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
10910 	.read_setup	= nfs4_proc_read_setup,
10911 	.read_done	= nfs4_read_done,
10912 	.write_setup	= nfs4_proc_write_setup,
10913 	.write_done	= nfs4_write_done,
10914 	.commit_setup	= nfs4_proc_commit_setup,
10915 	.commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
10916 	.commit_done	= nfs4_commit_done,
10917 	.lock		= nfs4_proc_lock,
10918 	.clear_acl_cache = nfs4_zap_acl_attr,
10919 	.close_context  = nfs4_close_context,
10920 	.open_context	= nfs4_atomic_open,
10921 	.have_delegation = nfs4_have_delegation,
10922 	.return_delegation = nfs4_inode_return_delegation,
10923 	.alloc_client	= nfs4_alloc_client,
10924 	.init_client	= nfs4_init_client,
10925 	.free_client	= nfs4_free_client,
10926 	.create_server	= nfs4_create_server,
10927 	.clone_server	= nfs_clone_server,
10928 	.discover_trunking = nfs4_discover_trunking,
10929 	.enable_swap	= nfs4_enable_swap,
10930 	.disable_swap	= nfs4_disable_swap,
10931 };
10932 
10933 static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
10934 	.name	= XATTR_NAME_NFSV4_ACL,
10935 	.list	= nfs4_xattr_list_nfs4_acl,
10936 	.get	= nfs4_xattr_get_nfs4_acl,
10937 	.set	= nfs4_xattr_set_nfs4_acl,
10938 };
10939 
10940 #if defined(CONFIG_NFS_V4_1)
10941 static const struct xattr_handler nfs4_xattr_nfs4_dacl_handler = {
10942 	.name	= XATTR_NAME_NFSV4_DACL,
10943 	.list	= nfs4_xattr_list_nfs4_dacl,
10944 	.get	= nfs4_xattr_get_nfs4_dacl,
10945 	.set	= nfs4_xattr_set_nfs4_dacl,
10946 };
10947 
10948 static const struct xattr_handler nfs4_xattr_nfs4_sacl_handler = {
10949 	.name	= XATTR_NAME_NFSV4_SACL,
10950 	.list	= nfs4_xattr_list_nfs4_sacl,
10951 	.get	= nfs4_xattr_get_nfs4_sacl,
10952 	.set	= nfs4_xattr_set_nfs4_sacl,
10953 };
10954 #endif
10955 
10956 #ifdef CONFIG_NFS_V4_2
10957 static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10958 	.prefix	= XATTR_USER_PREFIX,
10959 	.get	= nfs4_xattr_get_nfs4_user,
10960 	.set	= nfs4_xattr_set_nfs4_user,
10961 };
10962 #endif
10963 
10964 const struct xattr_handler * const nfs4_xattr_handlers[] = {
10965 	&nfs4_xattr_nfs4_acl_handler,
10966 #if defined(CONFIG_NFS_V4_1)
10967 	&nfs4_xattr_nfs4_dacl_handler,
10968 	&nfs4_xattr_nfs4_sacl_handler,
10969 #endif
10970 #ifdef CONFIG_NFS_V4_SECURITY_LABEL
10971 	&nfs4_xattr_nfs4_label_handler,
10972 #endif
10973 #ifdef CONFIG_NFS_V4_2
10974 	&nfs4_xattr_nfs4_user_handler,
10975 #endif
10976 	NULL
10977 };
10978