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