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