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