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