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