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