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