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