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