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