1 /*
2  *  pNFS functions to call and manage layout drivers.
3  *
4  *  Copyright (c) 2002 [year of first publication]
5  *  The Regents of the University of Michigan
6  *  All Rights Reserved
7  *
8  *  Dean Hildebrand <dhildebz@umich.edu>
9  *
10  *  Permission is granted to use, copy, create derivative works, and
11  *  redistribute this software and such derivative works for any purpose,
12  *  so long as the name of the University of Michigan is not used in
13  *  any advertising or publicity pertaining to the use or distribution
14  *  of this software without specific, written prior authorization. If
15  *  the above copyright notice or any other identification of the
16  *  University of Michigan is included in any copy of any portion of
17  *  this software, then the disclaimer below must also be included.
18  *
19  *  This software is provided as is, without representation or warranty
20  *  of any kind either express or implied, including without limitation
21  *  the implied warranties of merchantability, fitness for a particular
22  *  purpose, or noninfringement.  The Regents of the University of
23  *  Michigan shall not be liable for any damages, including special,
24  *  indirect, incidental, or consequential damages, with respect to any
25  *  claim arising out of or in connection with the use of the software,
26  *  even if it has been or is hereafter advised of the possibility of
27  *  such damages.
28  */
29 
30 #include <linux/nfs_fs.h>
31 #include <linux/nfs_page.h>
32 #include <linux/module.h>
33 #include <linux/sort.h>
34 #include "internal.h"
35 #include "pnfs.h"
36 #include "iostat.h"
37 #include "nfs4trace.h"
38 #include "delegation.h"
39 #include "nfs42.h"
40 #include "nfs4_fs.h"
41 
42 #define NFSDBG_FACILITY		NFSDBG_PNFS
43 #define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ)
44 
45 /* Locking:
46  *
47  * pnfs_spinlock:
48  *      protects pnfs_modules_tbl.
49  */
50 static DEFINE_SPINLOCK(pnfs_spinlock);
51 
52 /*
53  * pnfs_modules_tbl holds all pnfs modules
54  */
55 static LIST_HEAD(pnfs_modules_tbl);
56 
57 static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo);
58 static void pnfs_free_returned_lsegs(struct pnfs_layout_hdr *lo,
59 		struct list_head *free_me,
60 		const struct pnfs_layout_range *range,
61 		u32 seq);
62 static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg,
63 		                struct list_head *tmp_list);
64 static int pnfs_layout_return_on_reboot(struct pnfs_layout_hdr *lo);
65 
66 /* Return the registered pnfs layout driver module matching given id */
67 static struct pnfs_layoutdriver_type *
find_pnfs_driver_locked(u32 id)68 find_pnfs_driver_locked(u32 id)
69 {
70 	struct pnfs_layoutdriver_type *local;
71 
72 	list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
73 		if (local->id == id)
74 			goto out;
75 	local = NULL;
76 out:
77 	dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
78 	return local;
79 }
80 
81 static struct pnfs_layoutdriver_type *
find_pnfs_driver(u32 id)82 find_pnfs_driver(u32 id)
83 {
84 	struct pnfs_layoutdriver_type *local;
85 
86 	spin_lock(&pnfs_spinlock);
87 	local = find_pnfs_driver_locked(id);
88 	if (local != NULL && !try_module_get(local->owner)) {
89 		dprintk("%s: Could not grab reference on module\n", __func__);
90 		local = NULL;
91 	}
92 	spin_unlock(&pnfs_spinlock);
93 	return local;
94 }
95 
pnfs_find_layoutdriver(u32 id)96 const struct pnfs_layoutdriver_type *pnfs_find_layoutdriver(u32 id)
97 {
98 	return find_pnfs_driver(id);
99 }
100 
pnfs_put_layoutdriver(const struct pnfs_layoutdriver_type * ld)101 void pnfs_put_layoutdriver(const struct pnfs_layoutdriver_type *ld)
102 {
103 	if (ld)
104 		module_put(ld->owner);
105 }
106 
107 void
unset_pnfs_layoutdriver(struct nfs_server * nfss)108 unset_pnfs_layoutdriver(struct nfs_server *nfss)
109 {
110 	if (nfss->pnfs_curr_ld) {
111 		if (nfss->pnfs_curr_ld->clear_layoutdriver)
112 			nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
113 		/* Decrement the MDS count. Purge the deviceid cache if zero */
114 		if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count))
115 			nfs4_deviceid_purge_client(nfss->nfs_client);
116 		module_put(nfss->pnfs_curr_ld->owner);
117 	}
118 	nfss->pnfs_curr_ld = NULL;
119 }
120 
121 /*
122  * When the server sends a list of layout types, we choose one in the order
123  * given in the list below.
124  *
125  * FIXME: should this list be configurable in some fashion? module param?
126  * 	  mount option? something else?
127  */
128 static const u32 ld_prefs[] = {
129 	LAYOUT_SCSI,
130 	LAYOUT_BLOCK_VOLUME,
131 	LAYOUT_OSD2_OBJECTS,
132 	LAYOUT_FLEX_FILES,
133 	LAYOUT_NFSV4_1_FILES,
134 	0
135 };
136 
137 static int
ld_cmp(const void * e1,const void * e2)138 ld_cmp(const void *e1, const void *e2)
139 {
140 	u32 ld1 = *((u32 *)e1);
141 	u32 ld2 = *((u32 *)e2);
142 	int i;
143 
144 	for (i = 0; ld_prefs[i] != 0; i++) {
145 		if (ld1 == ld_prefs[i])
146 			return -1;
147 
148 		if (ld2 == ld_prefs[i])
149 			return 1;
150 	}
151 	return 0;
152 }
153 
154 /*
155  * Try to set the server's pnfs module to the pnfs layout type specified by id.
156  * Currently only one pNFS layout driver per filesystem is supported.
157  *
158  * @ids array of layout types supported by MDS.
159  */
160 void
set_pnfs_layoutdriver(struct nfs_server * server,const struct nfs_fh * mntfh,struct nfs_fsinfo * fsinfo)161 set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
162 		      struct nfs_fsinfo *fsinfo)
163 {
164 	struct pnfs_layoutdriver_type *ld_type = NULL;
165 	u32 id;
166 	int i;
167 
168 	if (fsinfo->nlayouttypes == 0)
169 		goto out_no_driver;
170 	if (!(server->nfs_client->cl_exchange_flags &
171 		 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
172 		printk(KERN_ERR "NFS: %s: cl_exchange_flags 0x%x\n",
173 			__func__, server->nfs_client->cl_exchange_flags);
174 		goto out_no_driver;
175 	}
176 
177 	sort(fsinfo->layouttype, fsinfo->nlayouttypes,
178 		sizeof(*fsinfo->layouttype), ld_cmp, NULL);
179 
180 	for (i = 0; i < fsinfo->nlayouttypes; i++) {
181 		id = fsinfo->layouttype[i];
182 		ld_type = find_pnfs_driver(id);
183 		if (!ld_type) {
184 			request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX,
185 					id);
186 			ld_type = find_pnfs_driver(id);
187 		}
188 		if (ld_type)
189 			break;
190 	}
191 
192 	if (!ld_type) {
193 		dprintk("%s: No pNFS module found!\n", __func__);
194 		goto out_no_driver;
195 	}
196 
197 	server->pnfs_curr_ld = ld_type;
198 	if (ld_type->set_layoutdriver
199 	    && ld_type->set_layoutdriver(server, mntfh)) {
200 		printk(KERN_ERR "NFS: %s: Error initializing pNFS layout "
201 			"driver %u.\n", __func__, id);
202 		module_put(ld_type->owner);
203 		goto out_no_driver;
204 	}
205 	/* Bump the MDS count */
206 	atomic_inc(&server->nfs_client->cl_mds_count);
207 
208 	dprintk("%s: pNFS module for %u set\n", __func__, id);
209 	return;
210 
211 out_no_driver:
212 	dprintk("%s: Using NFSv4 I/O\n", __func__);
213 	server->pnfs_curr_ld = NULL;
214 }
215 
216 int
pnfs_register_layoutdriver(struct pnfs_layoutdriver_type * ld_type)217 pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
218 {
219 	int status = -EINVAL;
220 	struct pnfs_layoutdriver_type *tmp;
221 
222 	if (ld_type->id == 0) {
223 		printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__);
224 		return status;
225 	}
226 	if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
227 		printk(KERN_ERR "NFS: %s Layout driver must provide "
228 		       "alloc_lseg and free_lseg.\n", __func__);
229 		return status;
230 	}
231 
232 	spin_lock(&pnfs_spinlock);
233 	tmp = find_pnfs_driver_locked(ld_type->id);
234 	if (!tmp) {
235 		list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
236 		status = 0;
237 		dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
238 			ld_type->name);
239 	} else {
240 		printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n",
241 			__func__, ld_type->id);
242 	}
243 	spin_unlock(&pnfs_spinlock);
244 
245 	return status;
246 }
247 EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
248 
249 void
pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type * ld_type)250 pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
251 {
252 	dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
253 	spin_lock(&pnfs_spinlock);
254 	list_del(&ld_type->pnfs_tblid);
255 	spin_unlock(&pnfs_spinlock);
256 }
257 EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
258 
259 /*
260  * pNFS client layout cache
261  */
262 
263 /* Need to hold i_lock if caller does not already hold reference */
264 void
pnfs_get_layout_hdr(struct pnfs_layout_hdr * lo)265 pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo)
266 {
267 	refcount_inc(&lo->plh_refcount);
268 }
269 
270 static struct pnfs_layout_hdr *
pnfs_alloc_layout_hdr(struct inode * ino,gfp_t gfp_flags)271 pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
272 {
273 	struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
274 	return ld->alloc_layout_hdr(ino, gfp_flags);
275 }
276 
277 static void
pnfs_free_layout_hdr(struct pnfs_layout_hdr * lo)278 pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
279 {
280 	struct nfs_server *server = NFS_SERVER(lo->plh_inode);
281 	struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
282 
283 	if (test_and_clear_bit(NFS_LAYOUT_HASHED, &lo->plh_flags)) {
284 		struct nfs_client *clp = server->nfs_client;
285 
286 		spin_lock(&clp->cl_lock);
287 		list_del_rcu(&lo->plh_layouts);
288 		spin_unlock(&clp->cl_lock);
289 	}
290 	put_cred(lo->plh_lc_cred);
291 	return ld->free_layout_hdr(lo);
292 }
293 
294 static void
pnfs_detach_layout_hdr(struct pnfs_layout_hdr * lo)295 pnfs_detach_layout_hdr(struct pnfs_layout_hdr *lo)
296 {
297 	struct nfs_inode *nfsi = NFS_I(lo->plh_inode);
298 	dprintk("%s: freeing layout cache %p\n", __func__, lo);
299 	nfsi->layout = NULL;
300 	/* Reset MDS Threshold I/O counters */
301 	nfsi->write_io = 0;
302 	nfsi->read_io = 0;
303 }
304 
305 void
pnfs_put_layout_hdr(struct pnfs_layout_hdr * lo)306 pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo)
307 {
308 	struct inode *inode;
309 	unsigned long i_state;
310 
311 	if (!lo)
312 		return;
313 	inode = lo->plh_inode;
314 	pnfs_layoutreturn_before_put_layout_hdr(lo);
315 
316 	if (refcount_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
317 		if (!list_empty(&lo->plh_segs))
318 			WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n");
319 		pnfs_detach_layout_hdr(lo);
320 		i_state = inode->i_state;
321 		spin_unlock(&inode->i_lock);
322 		pnfs_free_layout_hdr(lo);
323 		/* Notify pnfs_destroy_layout_final() that we're done */
324 		if (i_state & (I_FREEING | I_CLEAR))
325 			wake_up_var(lo);
326 	}
327 }
328 
329 static struct inode *
pnfs_grab_inode_layout_hdr(struct pnfs_layout_hdr * lo)330 pnfs_grab_inode_layout_hdr(struct pnfs_layout_hdr *lo)
331 {
332 	struct inode *inode = igrab(lo->plh_inode);
333 	if (inode)
334 		return inode;
335 	set_bit(NFS_LAYOUT_INODE_FREEING, &lo->plh_flags);
336 	return NULL;
337 }
338 
339 /*
340  * Compare 2 layout stateid sequence ids, to see which is newer,
341  * taking into account wraparound issues.
342  */
pnfs_seqid_is_newer(u32 s1,u32 s2)343 static bool pnfs_seqid_is_newer(u32 s1, u32 s2)
344 {
345 	return (s32)(s1 - s2) > 0;
346 }
347 
pnfs_barrier_update(struct pnfs_layout_hdr * lo,u32 newseq)348 static void pnfs_barrier_update(struct pnfs_layout_hdr *lo, u32 newseq)
349 {
350 	if (pnfs_seqid_is_newer(newseq, lo->plh_barrier) || !lo->plh_barrier)
351 		lo->plh_barrier = newseq;
352 }
353 
354 static void
pnfs_set_plh_return_info(struct pnfs_layout_hdr * lo,enum pnfs_iomode iomode,u32 seq)355 pnfs_set_plh_return_info(struct pnfs_layout_hdr *lo, enum pnfs_iomode iomode,
356 			 u32 seq)
357 {
358 	if (lo->plh_return_iomode != 0 && lo->plh_return_iomode != iomode)
359 		iomode = IOMODE_ANY;
360 	lo->plh_return_iomode = iomode;
361 	set_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
362 	/*
363 	 * We must set lo->plh_return_seq to avoid livelocks with
364 	 * pnfs_layout_need_return()
365 	 */
366 	if (seq == 0)
367 		seq = be32_to_cpu(lo->plh_stateid.seqid);
368 	if (!lo->plh_return_seq || pnfs_seqid_is_newer(seq, lo->plh_return_seq))
369 		lo->plh_return_seq = seq;
370 	pnfs_barrier_update(lo, seq);
371 }
372 
373 static void
pnfs_clear_layoutreturn_info(struct pnfs_layout_hdr * lo)374 pnfs_clear_layoutreturn_info(struct pnfs_layout_hdr *lo)
375 {
376 	struct pnfs_layout_segment *lseg;
377 	lo->plh_return_iomode = 0;
378 	lo->plh_return_seq = 0;
379 	clear_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
380 	list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
381 		if (!test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
382 			continue;
383 		pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
384 	}
385 }
386 
pnfs_clear_layoutreturn_waitbit(struct pnfs_layout_hdr * lo)387 static void pnfs_clear_layoutreturn_waitbit(struct pnfs_layout_hdr *lo)
388 {
389 	clear_bit_unlock(NFS_LAYOUT_RETURN, &lo->plh_flags);
390 	clear_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags);
391 	smp_mb__after_atomic();
392 	wake_up_bit(&lo->plh_flags, NFS_LAYOUT_RETURN);
393 	rpc_wake_up(&NFS_SERVER(lo->plh_inode)->roc_rpcwaitq);
394 }
395 
396 static void
pnfs_clear_lseg_state(struct pnfs_layout_segment * lseg,struct list_head * free_me)397 pnfs_clear_lseg_state(struct pnfs_layout_segment *lseg,
398 		struct list_head *free_me)
399 {
400 	clear_bit(NFS_LSEG_ROC, &lseg->pls_flags);
401 	clear_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
402 	if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags))
403 		pnfs_lseg_dec_and_remove_zero(lseg, free_me);
404 	if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
405 		pnfs_lseg_dec_and_remove_zero(lseg, free_me);
406 }
407 
408 /*
409  * Update the seqid of a layout stateid after receiving
410  * NFS4ERR_OLD_STATEID
411  */
nfs4_layout_refresh_old_stateid(nfs4_stateid * dst,struct pnfs_layout_range * dst_range,struct inode * inode)412 bool nfs4_layout_refresh_old_stateid(nfs4_stateid *dst,
413 		struct pnfs_layout_range *dst_range,
414 		struct inode *inode)
415 {
416 	struct pnfs_layout_hdr *lo;
417 	struct pnfs_layout_range range = {
418 		.iomode = IOMODE_ANY,
419 		.offset = 0,
420 		.length = NFS4_MAX_UINT64,
421 	};
422 	bool ret = false;
423 	LIST_HEAD(head);
424 	int err;
425 
426 	spin_lock(&inode->i_lock);
427 	lo = NFS_I(inode)->layout;
428 	if (lo &&  pnfs_layout_is_valid(lo) &&
429 	    nfs4_stateid_match_other(dst, &lo->plh_stateid)) {
430 		/* Is our call using the most recent seqid? If so, bump it */
431 		if (!nfs4_stateid_is_newer(&lo->plh_stateid, dst)) {
432 			nfs4_stateid_seqid_inc(dst);
433 			ret = true;
434 			goto out;
435 		}
436 		/* Try to update the seqid to the most recent */
437 		err = pnfs_mark_matching_lsegs_return(lo, &head, &range, 0);
438 		if (err != -EBUSY) {
439 			dst->seqid = lo->plh_stateid.seqid;
440 			*dst_range = range;
441 			ret = true;
442 		}
443 	}
444 out:
445 	spin_unlock(&inode->i_lock);
446 	pnfs_free_lseg_list(&head);
447 	return ret;
448 }
449 
450 /*
451  * Mark a pnfs_layout_hdr and all associated layout segments as invalid
452  *
453  * In order to continue using the pnfs_layout_hdr, a full recovery
454  * is required.
455  * Note that caller must hold inode->i_lock.
456  */
457 int
pnfs_mark_layout_stateid_invalid(struct pnfs_layout_hdr * lo,struct list_head * lseg_list)458 pnfs_mark_layout_stateid_invalid(struct pnfs_layout_hdr *lo,
459 		struct list_head *lseg_list)
460 {
461 	struct pnfs_layout_range range = {
462 		.iomode = IOMODE_ANY,
463 		.offset = 0,
464 		.length = NFS4_MAX_UINT64,
465 	};
466 	struct pnfs_layout_segment *lseg, *next;
467 
468 	set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
469 	list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
470 		pnfs_clear_lseg_state(lseg, lseg_list);
471 	pnfs_clear_layoutreturn_info(lo);
472 	pnfs_free_returned_lsegs(lo, lseg_list, &range, 0);
473 	set_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags);
474 	if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags) &&
475 	    !test_and_set_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags))
476 		pnfs_clear_layoutreturn_waitbit(lo);
477 	return !list_empty(&lo->plh_segs);
478 }
479 
pnfs_mark_layout_stateid_return(struct pnfs_layout_hdr * lo,struct list_head * lseg_list,enum pnfs_iomode iomode,u32 seq)480 static int pnfs_mark_layout_stateid_return(struct pnfs_layout_hdr *lo,
481 					   struct list_head *lseg_list,
482 					   enum pnfs_iomode iomode, u32 seq)
483 {
484 	struct pnfs_layout_range range = {
485 		.iomode = iomode,
486 		.length = NFS4_MAX_UINT64,
487 	};
488 
489 	return pnfs_mark_matching_lsegs_return(lo, lseg_list, &range, seq);
490 }
491 
492 static int
pnfs_iomode_to_fail_bit(u32 iomode)493 pnfs_iomode_to_fail_bit(u32 iomode)
494 {
495 	return iomode == IOMODE_RW ?
496 		NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED;
497 }
498 
499 static void
pnfs_layout_set_fail_bit(struct pnfs_layout_hdr * lo,int fail_bit)500 pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
501 {
502 	lo->plh_retry_timestamp = jiffies;
503 	if (!test_and_set_bit(fail_bit, &lo->plh_flags))
504 		refcount_inc(&lo->plh_refcount);
505 }
506 
507 static void
pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr * lo,int fail_bit)508 pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
509 {
510 	if (test_and_clear_bit(fail_bit, &lo->plh_flags))
511 		refcount_dec(&lo->plh_refcount);
512 }
513 
514 static void
pnfs_layout_io_set_failed(struct pnfs_layout_hdr * lo,u32 iomode)515 pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode)
516 {
517 	struct inode *inode = lo->plh_inode;
518 	struct pnfs_layout_range range = {
519 		.iomode = iomode,
520 		.offset = 0,
521 		.length = NFS4_MAX_UINT64,
522 	};
523 	LIST_HEAD(head);
524 
525 	spin_lock(&inode->i_lock);
526 	pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
527 	pnfs_mark_matching_lsegs_return(lo, &head, &range, 0);
528 	spin_unlock(&inode->i_lock);
529 	pnfs_free_lseg_list(&head);
530 	dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__,
531 			iomode == IOMODE_RW ?  "RW" : "READ");
532 }
533 
534 static bool
pnfs_layout_io_test_failed(struct pnfs_layout_hdr * lo,u32 iomode)535 pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode)
536 {
537 	unsigned long start, end;
538 	int fail_bit = pnfs_iomode_to_fail_bit(iomode);
539 
540 	if (test_bit(fail_bit, &lo->plh_flags) == 0)
541 		return false;
542 	end = jiffies;
543 	start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT;
544 	if (!time_in_range(lo->plh_retry_timestamp, start, end)) {
545 		/* It is time to retry the failed layoutgets */
546 		pnfs_layout_clear_fail_bit(lo, fail_bit);
547 		return false;
548 	}
549 	return true;
550 }
551 
552 static void
pnfs_init_lseg(struct pnfs_layout_hdr * lo,struct pnfs_layout_segment * lseg,const struct pnfs_layout_range * range,const nfs4_stateid * stateid)553 pnfs_init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg,
554 		const struct pnfs_layout_range *range,
555 		const nfs4_stateid *stateid)
556 {
557 	INIT_LIST_HEAD(&lseg->pls_list);
558 	INIT_LIST_HEAD(&lseg->pls_lc_list);
559 	INIT_LIST_HEAD(&lseg->pls_commits);
560 	refcount_set(&lseg->pls_refcount, 1);
561 	set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
562 	lseg->pls_layout = lo;
563 	lseg->pls_range = *range;
564 	lseg->pls_seq = be32_to_cpu(stateid->seqid);
565 }
566 
pnfs_free_lseg(struct pnfs_layout_segment * lseg)567 static void pnfs_free_lseg(struct pnfs_layout_segment *lseg)
568 {
569 	if (lseg != NULL) {
570 		struct inode *inode = lseg->pls_layout->plh_inode;
571 		NFS_SERVER(inode)->pnfs_curr_ld->free_lseg(lseg);
572 	}
573 }
574 
575 static void
pnfs_layout_remove_lseg(struct pnfs_layout_hdr * lo,struct pnfs_layout_segment * lseg)576 pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo,
577 		struct pnfs_layout_segment *lseg)
578 {
579 	WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
580 	list_del_init(&lseg->pls_list);
581 	/* Matched by pnfs_get_layout_hdr in pnfs_layout_insert_lseg */
582 	refcount_dec(&lo->plh_refcount);
583 	if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
584 		return;
585 	if (list_empty(&lo->plh_segs) &&
586 	    !test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags) &&
587 	    !test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
588 		if (atomic_read(&lo->plh_outstanding) == 0)
589 			set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
590 		clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
591 	}
592 }
593 
594 static bool
pnfs_cache_lseg_for_layoutreturn(struct pnfs_layout_hdr * lo,struct pnfs_layout_segment * lseg)595 pnfs_cache_lseg_for_layoutreturn(struct pnfs_layout_hdr *lo,
596 		struct pnfs_layout_segment *lseg)
597 {
598 	if (test_and_clear_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) &&
599 	    pnfs_layout_is_valid(lo)) {
600 		pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
601 		list_move_tail(&lseg->pls_list, &lo->plh_return_segs);
602 		return true;
603 	}
604 	return false;
605 }
606 
607 void
pnfs_put_lseg(struct pnfs_layout_segment * lseg)608 pnfs_put_lseg(struct pnfs_layout_segment *lseg)
609 {
610 	struct pnfs_layout_hdr *lo;
611 	struct inode *inode;
612 
613 	if (!lseg)
614 		return;
615 
616 	dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
617 		refcount_read(&lseg->pls_refcount),
618 		test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
619 
620 	lo = lseg->pls_layout;
621 	inode = lo->plh_inode;
622 
623 	if (refcount_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
624 		pnfs_get_layout_hdr(lo);
625 		pnfs_layout_remove_lseg(lo, lseg);
626 		if (pnfs_cache_lseg_for_layoutreturn(lo, lseg))
627 			lseg = NULL;
628 		spin_unlock(&inode->i_lock);
629 		pnfs_free_lseg(lseg);
630 		pnfs_put_layout_hdr(lo);
631 	}
632 }
633 EXPORT_SYMBOL_GPL(pnfs_put_lseg);
634 
635 /*
636  * is l2 fully contained in l1?
637  *   start1                             end1
638  *   [----------------------------------)
639  *           start2           end2
640  *           [----------------)
641  */
642 static bool
pnfs_lseg_range_contained(const struct pnfs_layout_range * l1,const struct pnfs_layout_range * l2)643 pnfs_lseg_range_contained(const struct pnfs_layout_range *l1,
644 		 const struct pnfs_layout_range *l2)
645 {
646 	u64 start1 = l1->offset;
647 	u64 end1 = pnfs_end_offset(start1, l1->length);
648 	u64 start2 = l2->offset;
649 	u64 end2 = pnfs_end_offset(start2, l2->length);
650 
651 	return (start1 <= start2) && (end1 >= end2);
652 }
653 
pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment * lseg,struct list_head * tmp_list)654 static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg,
655 		struct list_head *tmp_list)
656 {
657 	if (!refcount_dec_and_test(&lseg->pls_refcount))
658 		return false;
659 	pnfs_layout_remove_lseg(lseg->pls_layout, lseg);
660 	list_add(&lseg->pls_list, tmp_list);
661 	return true;
662 }
663 
664 /* Returns 1 if lseg is removed from list, 0 otherwise */
mark_lseg_invalid(struct pnfs_layout_segment * lseg,struct list_head * tmp_list)665 static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
666 			     struct list_head *tmp_list)
667 {
668 	int rv = 0;
669 
670 	if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
671 		/* Remove the reference keeping the lseg in the
672 		 * list.  It will now be removed when all
673 		 * outstanding io is finished.
674 		 */
675 		dprintk("%s: lseg %p ref %d\n", __func__, lseg,
676 			refcount_read(&lseg->pls_refcount));
677 		if (pnfs_lseg_dec_and_remove_zero(lseg, tmp_list))
678 			rv = 1;
679 	}
680 	return rv;
681 }
682 
683 static bool
pnfs_should_free_range(const struct pnfs_layout_range * lseg_range,const struct pnfs_layout_range * recall_range)684 pnfs_should_free_range(const struct pnfs_layout_range *lseg_range,
685 		 const struct pnfs_layout_range *recall_range)
686 {
687 	return (recall_range->iomode == IOMODE_ANY ||
688 		lseg_range->iomode == recall_range->iomode) &&
689 	       pnfs_lseg_range_intersecting(lseg_range, recall_range);
690 }
691 
692 static bool
pnfs_match_lseg_recall(const struct pnfs_layout_segment * lseg,const struct pnfs_layout_range * recall_range,u32 seq)693 pnfs_match_lseg_recall(const struct pnfs_layout_segment *lseg,
694 		const struct pnfs_layout_range *recall_range,
695 		u32 seq)
696 {
697 	if (seq != 0 && pnfs_seqid_is_newer(lseg->pls_seq, seq))
698 		return false;
699 	if (recall_range == NULL)
700 		return true;
701 	return pnfs_should_free_range(&lseg->pls_range, recall_range);
702 }
703 
704 /**
705  * pnfs_mark_matching_lsegs_invalid - tear down lsegs or mark them for later
706  * @lo: layout header containing the lsegs
707  * @tmp_list: list head where doomed lsegs should go
708  * @recall_range: optional recall range argument to match (may be NULL)
709  * @seq: only invalidate lsegs obtained prior to this sequence (may be 0)
710  *
711  * Walk the list of lsegs in the layout header, and tear down any that should
712  * be destroyed. If "recall_range" is specified then the segment must match
713  * that range. If "seq" is non-zero, then only match segments that were handed
714  * out at or before that sequence.
715  *
716  * Returns number of matching invalid lsegs remaining in list after scanning
717  * it and purging them.
718  */
719 int
pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr * lo,struct list_head * tmp_list,const struct pnfs_layout_range * recall_range,u32 seq)720 pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
721 			    struct list_head *tmp_list,
722 			    const struct pnfs_layout_range *recall_range,
723 			    u32 seq)
724 {
725 	struct pnfs_layout_segment *lseg, *next;
726 	struct nfs_server *server = NFS_SERVER(lo->plh_inode);
727 	int remaining = 0;
728 
729 	dprintk("%s:Begin lo %p\n", __func__, lo);
730 
731 	if (list_empty(&lo->plh_segs))
732 		return 0;
733 	list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
734 		if (pnfs_match_lseg_recall(lseg, recall_range, seq)) {
735 			dprintk("%s: freeing lseg %p iomode %d seq %u "
736 				"offset %llu length %llu\n", __func__,
737 				lseg, lseg->pls_range.iomode, lseg->pls_seq,
738 				lseg->pls_range.offset, lseg->pls_range.length);
739 			if (mark_lseg_invalid(lseg, tmp_list))
740 				continue;
741 			remaining++;
742 			pnfs_lseg_cancel_io(server, lseg);
743 		}
744 	dprintk("%s:Return %i\n", __func__, remaining);
745 	return remaining;
746 }
747 
pnfs_reset_return_info(struct pnfs_layout_hdr * lo)748 static void pnfs_reset_return_info(struct pnfs_layout_hdr *lo)
749 {
750 	struct pnfs_layout_segment *lseg;
751 
752 	list_for_each_entry(lseg, &lo->plh_return_segs, pls_list)
753 		pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
754 }
755 
756 static void
pnfs_free_returned_lsegs(struct pnfs_layout_hdr * lo,struct list_head * free_me,const struct pnfs_layout_range * range,u32 seq)757 pnfs_free_returned_lsegs(struct pnfs_layout_hdr *lo,
758 		struct list_head *free_me,
759 		const struct pnfs_layout_range *range,
760 		u32 seq)
761 {
762 	struct pnfs_layout_segment *lseg, *next;
763 
764 	list_for_each_entry_safe(lseg, next, &lo->plh_return_segs, pls_list) {
765 		if (pnfs_match_lseg_recall(lseg, range, seq))
766 			list_move_tail(&lseg->pls_list, free_me);
767 	}
768 }
769 
770 /* note free_me must contain lsegs from a single layout_hdr */
771 void
pnfs_free_lseg_list(struct list_head * free_me)772 pnfs_free_lseg_list(struct list_head *free_me)
773 {
774 	struct pnfs_layout_segment *lseg, *tmp;
775 
776 	if (list_empty(free_me))
777 		return;
778 
779 	list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
780 		list_del(&lseg->pls_list);
781 		pnfs_free_lseg(lseg);
782 	}
783 }
784 
__pnfs_destroy_layout(struct nfs_inode * nfsi)785 static struct pnfs_layout_hdr *__pnfs_destroy_layout(struct nfs_inode *nfsi)
786 {
787 	struct pnfs_layout_hdr *lo;
788 	LIST_HEAD(tmp_list);
789 
790 	spin_lock(&nfsi->vfs_inode.i_lock);
791 	lo = nfsi->layout;
792 	if (lo) {
793 		pnfs_get_layout_hdr(lo);
794 		pnfs_mark_layout_stateid_invalid(lo, &tmp_list);
795 		pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RO_FAILED);
796 		pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RW_FAILED);
797 		spin_unlock(&nfsi->vfs_inode.i_lock);
798 		pnfs_free_lseg_list(&tmp_list);
799 		nfs_commit_inode(&nfsi->vfs_inode, 0);
800 		pnfs_put_layout_hdr(lo);
801 	} else
802 		spin_unlock(&nfsi->vfs_inode.i_lock);
803 	return lo;
804 }
805 
pnfs_destroy_layout(struct nfs_inode * nfsi)806 void pnfs_destroy_layout(struct nfs_inode *nfsi)
807 {
808 	__pnfs_destroy_layout(nfsi);
809 }
810 EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
811 
pnfs_layout_removed(struct nfs_inode * nfsi,struct pnfs_layout_hdr * lo)812 static bool pnfs_layout_removed(struct nfs_inode *nfsi,
813 				struct pnfs_layout_hdr *lo)
814 {
815 	bool ret;
816 
817 	spin_lock(&nfsi->vfs_inode.i_lock);
818 	ret = nfsi->layout != lo;
819 	spin_unlock(&nfsi->vfs_inode.i_lock);
820 	return ret;
821 }
822 
pnfs_destroy_layout_final(struct nfs_inode * nfsi)823 void pnfs_destroy_layout_final(struct nfs_inode *nfsi)
824 {
825 	struct pnfs_layout_hdr *lo = __pnfs_destroy_layout(nfsi);
826 
827 	if (lo)
828 		wait_var_event(lo, pnfs_layout_removed(nfsi, lo));
829 }
830 
831 static bool
pnfs_layout_add_bulk_destroy_list(struct inode * inode,struct list_head * layout_list)832 pnfs_layout_add_bulk_destroy_list(struct inode *inode,
833 		struct list_head *layout_list)
834 {
835 	struct pnfs_layout_hdr *lo;
836 	bool ret = false;
837 
838 	spin_lock(&inode->i_lock);
839 	lo = NFS_I(inode)->layout;
840 	if (lo != NULL && list_empty(&lo->plh_bulk_destroy)) {
841 		pnfs_get_layout_hdr(lo);
842 		list_add(&lo->plh_bulk_destroy, layout_list);
843 		ret = true;
844 	}
845 	spin_unlock(&inode->i_lock);
846 	return ret;
847 }
848 
849 /* Caller must hold rcu_read_lock and clp->cl_lock */
850 static int
pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client * clp,struct nfs_server * server,struct list_head * layout_list)851 pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client *clp,
852 		struct nfs_server *server,
853 		struct list_head *layout_list)
854 	__must_hold(&clp->cl_lock)
855 	__must_hold(RCU)
856 {
857 	struct pnfs_layout_hdr *lo, *next;
858 	struct inode *inode;
859 
860 	list_for_each_entry_safe(lo, next, &server->layouts, plh_layouts) {
861 		if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
862 		    test_bit(NFS_LAYOUT_INODE_FREEING, &lo->plh_flags) ||
863 		    !list_empty(&lo->plh_bulk_destroy))
864 			continue;
865 		/* If the sb is being destroyed, just bail */
866 		if (!nfs_sb_active(server->super))
867 			break;
868 		inode = pnfs_grab_inode_layout_hdr(lo);
869 		if (inode != NULL) {
870 			if (pnfs_layout_add_bulk_destroy_list(inode,
871 						layout_list))
872 				continue;
873 			rcu_read_unlock();
874 			spin_unlock(&clp->cl_lock);
875 			iput(inode);
876 		} else {
877 			rcu_read_unlock();
878 			spin_unlock(&clp->cl_lock);
879 		}
880 		nfs_sb_deactive(server->super);
881 		spin_lock(&clp->cl_lock);
882 		rcu_read_lock();
883 		return -EAGAIN;
884 	}
885 	return 0;
886 }
887 
888 static int
pnfs_layout_free_bulk_destroy_list(struct list_head * layout_list,enum pnfs_layout_destroy_mode mode)889 pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list,
890 				   enum pnfs_layout_destroy_mode mode)
891 {
892 	struct pnfs_layout_hdr *lo;
893 	struct inode *inode;
894 	LIST_HEAD(lseg_list);
895 	int ret = 0;
896 
897 	while (!list_empty(layout_list)) {
898 		lo = list_entry(layout_list->next, struct pnfs_layout_hdr,
899 				plh_bulk_destroy);
900 		dprintk("%s freeing layout for inode %lu\n", __func__,
901 			lo->plh_inode->i_ino);
902 		inode = lo->plh_inode;
903 
904 		pnfs_layoutcommit_inode(inode, false);
905 
906 		spin_lock(&inode->i_lock);
907 		list_del_init(&lo->plh_bulk_destroy);
908 		if (mode == PNFS_LAYOUT_FILE_BULK_RETURN) {
909 			pnfs_mark_layout_stateid_return(lo, &lseg_list,
910 							IOMODE_ANY, 0);
911 		} else if (pnfs_mark_layout_stateid_invalid(lo, &lseg_list)) {
912 			if (mode == PNFS_LAYOUT_BULK_RETURN)
913 				set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
914 			ret = -EAGAIN;
915 		}
916 		spin_unlock(&inode->i_lock);
917 		pnfs_free_lseg_list(&lseg_list);
918 		/* Free all lsegs that are attached to commit buckets */
919 		nfs_commit_inode(inode, 0);
920 		pnfs_put_layout_hdr(lo);
921 		nfs_iput_and_deactive(inode);
922 	}
923 	return ret;
924 }
925 
pnfs_layout_destroy_byfsid(struct nfs_client * clp,struct nfs_fsid * fsid,enum pnfs_layout_destroy_mode mode)926 int pnfs_layout_destroy_byfsid(struct nfs_client *clp, struct nfs_fsid *fsid,
927 			       enum pnfs_layout_destroy_mode mode)
928 {
929 	struct nfs_server *server;
930 	LIST_HEAD(layout_list);
931 
932 	spin_lock(&clp->cl_lock);
933 	rcu_read_lock();
934 restart:
935 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
936 		if (memcmp(&server->fsid, fsid, sizeof(*fsid)) != 0)
937 			continue;
938 		if (pnfs_layout_bulk_destroy_byserver_locked(clp,
939 				server,
940 				&layout_list) != 0)
941 			goto restart;
942 	}
943 	rcu_read_unlock();
944 	spin_unlock(&clp->cl_lock);
945 
946 	return pnfs_layout_free_bulk_destroy_list(&layout_list, mode);
947 }
948 
pnfs_layout_build_destroy_list_byclient(struct nfs_client * clp,struct list_head * list)949 static void pnfs_layout_build_destroy_list_byclient(struct nfs_client *clp,
950 						    struct list_head *list)
951 {
952 	struct nfs_server *server;
953 
954 	spin_lock(&clp->cl_lock);
955 	rcu_read_lock();
956 restart:
957 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
958 		if (pnfs_layout_bulk_destroy_byserver_locked(clp, server,
959 							     list) != 0)
960 			goto restart;
961 	}
962 	rcu_read_unlock();
963 	spin_unlock(&clp->cl_lock);
964 }
965 
pnfs_layout_do_destroy_byclid(struct nfs_client * clp,struct list_head * list,enum pnfs_layout_destroy_mode mode)966 static int pnfs_layout_do_destroy_byclid(struct nfs_client *clp,
967 					 struct list_head *list,
968 					 enum pnfs_layout_destroy_mode mode)
969 {
970 	pnfs_layout_build_destroy_list_byclient(clp, list);
971 	return pnfs_layout_free_bulk_destroy_list(list, mode);
972 }
973 
pnfs_layout_destroy_byclid(struct nfs_client * clp,enum pnfs_layout_destroy_mode mode)974 int pnfs_layout_destroy_byclid(struct nfs_client *clp,
975 			       enum pnfs_layout_destroy_mode mode)
976 {
977 	LIST_HEAD(layout_list);
978 
979 	return pnfs_layout_do_destroy_byclid(clp, &layout_list, mode);
980 }
981 
982 /*
983  * Called by the state manager to remove all layouts established under an
984  * expired lease.
985  */
986 void
pnfs_destroy_all_layouts(struct nfs_client * clp)987 pnfs_destroy_all_layouts(struct nfs_client *clp)
988 {
989 	nfs4_deviceid_mark_client_invalid(clp);
990 	nfs4_deviceid_purge_client(clp);
991 
992 	pnfs_layout_destroy_byclid(clp, PNFS_LAYOUT_INVALIDATE);
993 }
994 
pnfs_layout_build_recover_list_byclient(struct nfs_client * clp,struct list_head * list)995 static void pnfs_layout_build_recover_list_byclient(struct nfs_client *clp,
996 						    struct list_head *list)
997 {
998 	struct nfs_server *server;
999 
1000 	spin_lock(&clp->cl_lock);
1001 	rcu_read_lock();
1002 restart:
1003 	list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1004 		if (!(server->caps & NFS_CAP_REBOOT_LAYOUTRETURN))
1005 			continue;
1006 		if (pnfs_layout_bulk_destroy_byserver_locked(clp, server,
1007 							     list) != 0)
1008 			goto restart;
1009 	}
1010 	rcu_read_unlock();
1011 	spin_unlock(&clp->cl_lock);
1012 }
1013 
pnfs_layout_bulk_list_reboot(struct list_head * list)1014 static int pnfs_layout_bulk_list_reboot(struct list_head *list)
1015 {
1016 	struct pnfs_layout_hdr *lo;
1017 	struct nfs_server *server;
1018 	int ret;
1019 
1020 	list_for_each_entry(lo, list, plh_bulk_destroy) {
1021 		server = NFS_SERVER(lo->plh_inode);
1022 		ret = pnfs_layout_return_on_reboot(lo);
1023 		switch (ret) {
1024 		case 0:
1025 			continue;
1026 		case -NFS4ERR_BAD_STATEID:
1027 			server->caps &= ~NFS_CAP_REBOOT_LAYOUTRETURN;
1028 			break;
1029 		case -NFS4ERR_NO_GRACE:
1030 			break;
1031 		default:
1032 			goto err;
1033 		}
1034 		break;
1035 	}
1036 	return 0;
1037 err:
1038 	return ret;
1039 }
1040 
pnfs_layout_handle_reboot(struct nfs_client * clp)1041 int pnfs_layout_handle_reboot(struct nfs_client *clp)
1042 {
1043 	LIST_HEAD(list);
1044 	int ret = 0, ret2;
1045 
1046 	pnfs_layout_build_recover_list_byclient(clp, &list);
1047 	if (!list_empty(&list))
1048 		ret = pnfs_layout_bulk_list_reboot(&list);
1049 	ret2 = pnfs_layout_do_destroy_byclid(clp, &list,
1050 					     PNFS_LAYOUT_INVALIDATE);
1051 	if (!ret)
1052 		ret = ret2;
1053 	return (ret == 0) ?  0 : -EAGAIN;
1054 }
1055 
1056 static void
pnfs_set_layout_cred(struct pnfs_layout_hdr * lo,const struct cred * cred)1057 pnfs_set_layout_cred(struct pnfs_layout_hdr *lo, const struct cred *cred)
1058 {
1059 	const struct cred *old;
1060 
1061 	if (cred && cred_fscmp(lo->plh_lc_cred, cred) != 0) {
1062 		old = xchg(&lo->plh_lc_cred, get_cred(cred));
1063 		put_cred(old);
1064 	}
1065 }
1066 
1067 /* update lo->plh_stateid with new if is more recent */
1068 void
pnfs_set_layout_stateid(struct pnfs_layout_hdr * lo,const nfs4_stateid * new,const struct cred * cred,bool update_barrier)1069 pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
1070 			const struct cred *cred, bool update_barrier)
1071 {
1072 	u32 oldseq = be32_to_cpu(lo->plh_stateid.seqid);
1073 	u32 newseq = be32_to_cpu(new->seqid);
1074 
1075 	if (!pnfs_layout_is_valid(lo)) {
1076 		pnfs_set_layout_cred(lo, cred);
1077 		nfs4_stateid_copy(&lo->plh_stateid, new);
1078 		lo->plh_barrier = newseq;
1079 		pnfs_clear_layoutreturn_info(lo);
1080 		clear_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
1081 		return;
1082 	}
1083 
1084 	if (pnfs_seqid_is_newer(newseq, oldseq))
1085 		nfs4_stateid_copy(&lo->plh_stateid, new);
1086 
1087 	if (update_barrier) {
1088 		pnfs_barrier_update(lo, newseq);
1089 		return;
1090 	}
1091 	/*
1092 	 * Because of wraparound, we want to keep the barrier
1093 	 * "close" to the current seqids. We really only want to
1094 	 * get here from a layoutget call.
1095 	 */
1096 	if (atomic_read(&lo->plh_outstanding) == 1)
1097 		 pnfs_barrier_update(lo, be32_to_cpu(lo->plh_stateid.seqid));
1098 }
1099 
1100 static bool
pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr * lo,const nfs4_stateid * stateid)1101 pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo,
1102 		const nfs4_stateid *stateid)
1103 {
1104 	u32 seqid = be32_to_cpu(stateid->seqid);
1105 
1106 	return lo->plh_barrier && pnfs_seqid_is_newer(lo->plh_barrier, seqid);
1107 }
1108 
1109 /* lget is set to 1 if called from inside send_layoutget call chain */
1110 static bool
pnfs_layoutgets_blocked(const struct pnfs_layout_hdr * lo)1111 pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo)
1112 {
1113 	return lo->plh_block_lgets ||
1114 		test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
1115 }
1116 
1117 static struct nfs_server *
pnfs_find_server(struct inode * inode,struct nfs_open_context * ctx)1118 pnfs_find_server(struct inode *inode, struct nfs_open_context *ctx)
1119 {
1120 	struct nfs_server *server;
1121 
1122 	if (inode) {
1123 		server = NFS_SERVER(inode);
1124 	} else {
1125 		struct dentry *parent_dir = dget_parent(ctx->dentry);
1126 		server = NFS_SERVER(parent_dir->d_inode);
1127 		dput(parent_dir);
1128 	}
1129 	return server;
1130 }
1131 
nfs4_free_pages(struct page ** pages,size_t size)1132 static void nfs4_free_pages(struct page **pages, size_t size)
1133 {
1134 	int i;
1135 
1136 	if (!pages)
1137 		return;
1138 
1139 	for (i = 0; i < size; i++) {
1140 		if (!pages[i])
1141 			break;
1142 		__free_page(pages[i]);
1143 	}
1144 	kfree(pages);
1145 }
1146 
nfs4_alloc_pages(size_t size,gfp_t gfp_flags)1147 static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
1148 {
1149 	struct page **pages;
1150 	int i;
1151 
1152 	pages = kmalloc_array(size, sizeof(struct page *), gfp_flags);
1153 	if (!pages) {
1154 		dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
1155 		return NULL;
1156 	}
1157 
1158 	for (i = 0; i < size; i++) {
1159 		pages[i] = alloc_page(gfp_flags);
1160 		if (!pages[i]) {
1161 			dprintk("%s: failed to allocate page\n", __func__);
1162 			nfs4_free_pages(pages, i);
1163 			return NULL;
1164 		}
1165 	}
1166 
1167 	return pages;
1168 }
1169 
1170 static struct nfs4_layoutget *
pnfs_alloc_init_layoutget_args(struct inode * ino,struct nfs_open_context * ctx,const nfs4_stateid * stateid,const struct pnfs_layout_range * range,gfp_t gfp_flags)1171 pnfs_alloc_init_layoutget_args(struct inode *ino,
1172 	   struct nfs_open_context *ctx,
1173 	   const nfs4_stateid *stateid,
1174 	   const struct pnfs_layout_range *range,
1175 	   gfp_t gfp_flags)
1176 {
1177 	struct nfs_server *server = pnfs_find_server(ino, ctx);
1178 	size_t max_reply_sz = server->pnfs_curr_ld->max_layoutget_response;
1179 	size_t max_pages = max_response_pages(server);
1180 	struct nfs4_layoutget *lgp;
1181 
1182 	dprintk("--> %s\n", __func__);
1183 
1184 	lgp = kzalloc(sizeof(*lgp), gfp_flags);
1185 	if (lgp == NULL)
1186 		return NULL;
1187 
1188 	if (max_reply_sz) {
1189 		size_t npages = (max_reply_sz + PAGE_SIZE - 1) >> PAGE_SHIFT;
1190 		if (npages < max_pages)
1191 			max_pages = npages;
1192 	}
1193 
1194 	lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
1195 	if (!lgp->args.layout.pages) {
1196 		kfree(lgp);
1197 		return NULL;
1198 	}
1199 	lgp->args.layout.pglen = max_pages * PAGE_SIZE;
1200 	lgp->res.layoutp = &lgp->args.layout;
1201 
1202 	/* Don't confuse uninitialised result and success */
1203 	lgp->res.status = -NFS4ERR_DELAY;
1204 
1205 	lgp->args.minlength = PAGE_SIZE;
1206 	if (lgp->args.minlength > range->length)
1207 		lgp->args.minlength = range->length;
1208 	if (ino) {
1209 		loff_t i_size = i_size_read(ino);
1210 
1211 		if (range->iomode == IOMODE_READ) {
1212 			if (range->offset >= i_size)
1213 				lgp->args.minlength = 0;
1214 			else if (i_size - range->offset < lgp->args.minlength)
1215 				lgp->args.minlength = i_size - range->offset;
1216 		}
1217 	}
1218 	lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
1219 	pnfs_copy_range(&lgp->args.range, range);
1220 	lgp->args.type = server->pnfs_curr_ld->id;
1221 	lgp->args.inode = ino;
1222 	lgp->args.ctx = get_nfs_open_context(ctx);
1223 	nfs4_stateid_copy(&lgp->args.stateid, stateid);
1224 	lgp->gfp_flags = gfp_flags;
1225 	lgp->cred = ctx->cred;
1226 	return lgp;
1227 }
1228 
pnfs_layoutget_free(struct nfs4_layoutget * lgp)1229 void pnfs_layoutget_free(struct nfs4_layoutget *lgp)
1230 {
1231 	size_t max_pages = lgp->args.layout.pglen / PAGE_SIZE;
1232 
1233 	nfs4_free_pages(lgp->args.layout.pages, max_pages);
1234 	pnfs_put_layout_hdr(lgp->lo);
1235 	put_nfs_open_context(lgp->args.ctx);
1236 	kfree(lgp);
1237 }
1238 
pnfs_clear_layoutcommit(struct inode * inode,struct list_head * head)1239 static void pnfs_clear_layoutcommit(struct inode *inode,
1240 		struct list_head *head)
1241 {
1242 	struct nfs_inode *nfsi = NFS_I(inode);
1243 	struct pnfs_layout_segment *lseg, *tmp;
1244 
1245 	if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
1246 		return;
1247 	list_for_each_entry_safe(lseg, tmp, &nfsi->layout->plh_segs, pls_list) {
1248 		if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
1249 			continue;
1250 		pnfs_lseg_dec_and_remove_zero(lseg, head);
1251 	}
1252 }
1253 
1254 static void
pnfs_layoutreturn_retry_later_locked(struct pnfs_layout_hdr * lo,const nfs4_stateid * arg_stateid,const struct pnfs_layout_range * range)1255 pnfs_layoutreturn_retry_later_locked(struct pnfs_layout_hdr *lo,
1256 				     const nfs4_stateid *arg_stateid,
1257 				     const struct pnfs_layout_range *range)
1258 {
1259 	const struct pnfs_layout_segment *lseg;
1260 	u32 seq = be32_to_cpu(arg_stateid->seqid);
1261 
1262 	if (pnfs_layout_is_valid(lo) &&
1263 	    nfs4_stateid_match_other(&lo->plh_stateid, arg_stateid)) {
1264 		list_for_each_entry(lseg, &lo->plh_return_segs, pls_list) {
1265 			if (pnfs_seqid_is_newer(lseg->pls_seq, seq) ||
1266 			    !pnfs_should_free_range(&lseg->pls_range, range))
1267 				continue;
1268 			pnfs_set_plh_return_info(lo, range->iomode, seq);
1269 			break;
1270 		}
1271 	}
1272 }
1273 
pnfs_layoutreturn_retry_later(struct pnfs_layout_hdr * lo,const nfs4_stateid * arg_stateid,const struct pnfs_layout_range * range)1274 void pnfs_layoutreturn_retry_later(struct pnfs_layout_hdr *lo,
1275 				   const nfs4_stateid *arg_stateid,
1276 				   const struct pnfs_layout_range *range)
1277 {
1278 	struct inode *inode = lo->plh_inode;
1279 
1280 	spin_lock(&inode->i_lock);
1281 	pnfs_layoutreturn_retry_later_locked(lo, arg_stateid, range);
1282 	pnfs_clear_layoutreturn_waitbit(lo);
1283 	spin_unlock(&inode->i_lock);
1284 }
1285 
pnfs_layoutreturn_free_lsegs(struct pnfs_layout_hdr * lo,const nfs4_stateid * arg_stateid,const struct pnfs_layout_range * range,const nfs4_stateid * stateid)1286 void pnfs_layoutreturn_free_lsegs(struct pnfs_layout_hdr *lo,
1287 		const nfs4_stateid *arg_stateid,
1288 		const struct pnfs_layout_range *range,
1289 		const nfs4_stateid *stateid)
1290 {
1291 	struct inode *inode = lo->plh_inode;
1292 	LIST_HEAD(freeme);
1293 
1294 	spin_lock(&inode->i_lock);
1295 	if (!nfs4_stateid_match_other(&lo->plh_stateid, arg_stateid))
1296 		goto out_unlock;
1297 	if (stateid && pnfs_layout_is_valid(lo)) {
1298 		u32 seq = be32_to_cpu(arg_stateid->seqid);
1299 
1300 		pnfs_mark_matching_lsegs_invalid(lo, &freeme, range, seq);
1301 		pnfs_free_returned_lsegs(lo, &freeme, range, seq);
1302 		pnfs_set_layout_stateid(lo, stateid, NULL, true);
1303 		pnfs_reset_return_info(lo);
1304 	} else
1305 		pnfs_mark_layout_stateid_invalid(lo, &freeme);
1306 out_unlock:
1307 	pnfs_clear_layoutreturn_waitbit(lo);
1308 	spin_unlock(&inode->i_lock);
1309 	pnfs_free_lseg_list(&freeme);
1310 
1311 }
1312 
1313 static bool
pnfs_prepare_layoutreturn(struct pnfs_layout_hdr * lo,nfs4_stateid * stateid,const struct cred ** cred,enum pnfs_iomode * iomode)1314 pnfs_prepare_layoutreturn(struct pnfs_layout_hdr *lo,
1315 		nfs4_stateid *stateid,
1316 		const struct cred **cred,
1317 		enum pnfs_iomode *iomode)
1318 {
1319 	/* Serialise LAYOUTGET/LAYOUTRETURN */
1320 	if (atomic_read(&lo->plh_outstanding) != 0 && lo->plh_return_seq == 0)
1321 		return false;
1322 	if (test_and_set_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags))
1323 		return false;
1324 	set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags);
1325 	pnfs_get_layout_hdr(lo);
1326 	nfs4_stateid_copy(stateid, &lo->plh_stateid);
1327 	*cred = get_cred(lo->plh_lc_cred);
1328 	if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) {
1329 		if (lo->plh_return_seq != 0)
1330 			stateid->seqid = cpu_to_be32(lo->plh_return_seq);
1331 		if (iomode != NULL)
1332 			*iomode = lo->plh_return_iomode;
1333 		pnfs_clear_layoutreturn_info(lo);
1334 	} else if (iomode != NULL)
1335 		*iomode = IOMODE_ANY;
1336 	pnfs_barrier_update(lo, be32_to_cpu(stateid->seqid));
1337 	return true;
1338 }
1339 
1340 static void
pnfs_init_layoutreturn_args(struct nfs4_layoutreturn_args * args,struct pnfs_layout_hdr * lo,const nfs4_stateid * stateid,enum pnfs_iomode iomode)1341 pnfs_init_layoutreturn_args(struct nfs4_layoutreturn_args *args,
1342 		struct pnfs_layout_hdr *lo,
1343 		const nfs4_stateid *stateid,
1344 		enum pnfs_iomode iomode)
1345 {
1346 	struct inode *inode = lo->plh_inode;
1347 
1348 	args->layout_type = NFS_SERVER(inode)->pnfs_curr_ld->id;
1349 	args->inode = inode;
1350 	args->range.iomode = iomode;
1351 	args->range.offset = 0;
1352 	args->range.length = NFS4_MAX_UINT64;
1353 	args->layout = lo;
1354 	nfs4_stateid_copy(&args->stateid, stateid);
1355 }
1356 
1357 static int
pnfs_send_layoutreturn(struct pnfs_layout_hdr * lo,const nfs4_stateid * stateid,const struct cred ** pcred,enum pnfs_iomode iomode,unsigned int flags)1358 pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo,
1359 		       const nfs4_stateid *stateid,
1360 		       const struct cred **pcred,
1361 		       enum pnfs_iomode iomode,
1362 		       unsigned int flags)
1363 {
1364 	struct inode *ino = lo->plh_inode;
1365 	struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
1366 	struct nfs4_layoutreturn *lrp;
1367 	const struct cred *cred = *pcred;
1368 	int status = 0;
1369 
1370 	*pcred = NULL;
1371 	lrp = kzalloc(sizeof(*lrp), nfs_io_gfp_mask());
1372 	if (unlikely(lrp == NULL)) {
1373 		status = -ENOMEM;
1374 		spin_lock(&ino->i_lock);
1375 		pnfs_clear_layoutreturn_waitbit(lo);
1376 		spin_unlock(&ino->i_lock);
1377 		put_cred(cred);
1378 		pnfs_put_layout_hdr(lo);
1379 		goto out;
1380 	}
1381 
1382 	pnfs_init_layoutreturn_args(&lrp->args, lo, stateid, iomode);
1383 	lrp->args.ld_private = &lrp->ld_private;
1384 	lrp->clp = NFS_SERVER(ino)->nfs_client;
1385 	lrp->cred = cred;
1386 	if (ld->prepare_layoutreturn)
1387 		ld->prepare_layoutreturn(&lrp->args);
1388 
1389 	status = nfs4_proc_layoutreturn(lrp, flags);
1390 out:
1391 	dprintk("<-- %s status: %d\n", __func__, status);
1392 	return status;
1393 }
1394 
1395 /* Return true if layoutreturn is needed */
1396 static bool
pnfs_layout_need_return(struct pnfs_layout_hdr * lo)1397 pnfs_layout_need_return(struct pnfs_layout_hdr *lo)
1398 {
1399 	if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
1400 		return false;
1401 	return pnfs_mark_layout_stateid_return(lo, &lo->plh_return_segs,
1402 					       lo->plh_return_iomode,
1403 					       lo->plh_return_seq) != EBUSY;
1404 }
1405 
pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr * lo)1406 static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo)
1407 {
1408 	struct inode *inode= lo->plh_inode;
1409 
1410 	if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
1411 		return;
1412 	spin_lock(&inode->i_lock);
1413 	if (pnfs_layout_need_return(lo)) {
1414 		const struct cred *cred;
1415 		nfs4_stateid stateid;
1416 		enum pnfs_iomode iomode;
1417 		bool send;
1418 
1419 		send = pnfs_prepare_layoutreturn(lo, &stateid, &cred, &iomode);
1420 		spin_unlock(&inode->i_lock);
1421 		if (send) {
1422 			/* Send an async layoutreturn so we dont deadlock */
1423 			pnfs_send_layoutreturn(lo, &stateid, &cred, iomode,
1424 					       PNFS_FL_LAYOUTRETURN_ASYNC);
1425 		}
1426 	} else
1427 		spin_unlock(&inode->i_lock);
1428 }
1429 
1430 /*
1431  * Initiates a LAYOUTRETURN(FILE), and removes the pnfs_layout_hdr
1432  * when the layout segment list is empty.
1433  *
1434  * Note that a pnfs_layout_hdr can exist with an empty layout segment
1435  * list when LAYOUTGET has failed, or when LAYOUTGET succeeded, but the
1436  * deviceid is marked invalid.
1437  */
1438 int
_pnfs_return_layout(struct inode * ino)1439 _pnfs_return_layout(struct inode *ino)
1440 {
1441 	struct pnfs_layout_hdr *lo = NULL;
1442 	struct nfs_inode *nfsi = NFS_I(ino);
1443 	struct pnfs_layout_range range = {
1444 		.iomode		= IOMODE_ANY,
1445 		.offset		= 0,
1446 		.length		= NFS4_MAX_UINT64,
1447 	};
1448 	LIST_HEAD(tmp_list);
1449 	const struct cred *cred;
1450 	nfs4_stateid stateid;
1451 	int status = 0;
1452 	bool send, valid_layout;
1453 
1454 	dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
1455 
1456 	spin_lock(&ino->i_lock);
1457 	lo = nfsi->layout;
1458 	if (!lo) {
1459 		spin_unlock(&ino->i_lock);
1460 		dprintk("NFS: %s no layout to return\n", __func__);
1461 		goto out;
1462 	}
1463 	/* Reference matched in nfs4_layoutreturn_release */
1464 	pnfs_get_layout_hdr(lo);
1465 	/* Is there an outstanding layoutreturn ? */
1466 	if (test_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags)) {
1467 		spin_unlock(&ino->i_lock);
1468 		if (wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN,
1469 					TASK_UNINTERRUPTIBLE))
1470 			goto out_put_layout_hdr;
1471 		spin_lock(&ino->i_lock);
1472 	}
1473 	valid_layout = pnfs_layout_is_valid(lo);
1474 	pnfs_clear_layoutcommit(ino, &tmp_list);
1475 	pnfs_mark_matching_lsegs_return(lo, &tmp_list, &range, 0);
1476 
1477 	if (NFS_SERVER(ino)->pnfs_curr_ld->return_range)
1478 		NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range);
1479 
1480 	/* Don't send a LAYOUTRETURN if list was initially empty */
1481 	if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags) ||
1482 			!valid_layout) {
1483 		spin_unlock(&ino->i_lock);
1484 		dprintk("NFS: %s no layout segments to return\n", __func__);
1485 		goto out_wait_layoutreturn;
1486 	}
1487 
1488 	send = pnfs_prepare_layoutreturn(lo, &stateid, &cred, NULL);
1489 	spin_unlock(&ino->i_lock);
1490 	if (send)
1491 		status = pnfs_send_layoutreturn(lo, &stateid, &cred, IOMODE_ANY,
1492 						0);
1493 out_wait_layoutreturn:
1494 	wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN, TASK_UNINTERRUPTIBLE);
1495 out_put_layout_hdr:
1496 	pnfs_free_lseg_list(&tmp_list);
1497 	pnfs_put_layout_hdr(lo);
1498 out:
1499 	dprintk("<-- %s status: %d\n", __func__, status);
1500 	return status;
1501 }
1502 
1503 int
pnfs_commit_and_return_layout(struct inode * inode)1504 pnfs_commit_and_return_layout(struct inode *inode)
1505 {
1506 	struct pnfs_layout_hdr *lo;
1507 	int ret;
1508 
1509 	spin_lock(&inode->i_lock);
1510 	lo = NFS_I(inode)->layout;
1511 	if (lo == NULL) {
1512 		spin_unlock(&inode->i_lock);
1513 		return 0;
1514 	}
1515 	pnfs_get_layout_hdr(lo);
1516 	/* Block new layoutgets and read/write to ds */
1517 	lo->plh_block_lgets++;
1518 	spin_unlock(&inode->i_lock);
1519 	filemap_fdatawait(inode->i_mapping);
1520 	ret = pnfs_layoutcommit_inode(inode, true);
1521 	if (ret == 0)
1522 		ret = _pnfs_return_layout(inode);
1523 	spin_lock(&inode->i_lock);
1524 	lo->plh_block_lgets--;
1525 	spin_unlock(&inode->i_lock);
1526 	pnfs_put_layout_hdr(lo);
1527 	return ret;
1528 }
1529 
pnfs_layout_return_on_reboot(struct pnfs_layout_hdr * lo)1530 static int pnfs_layout_return_on_reboot(struct pnfs_layout_hdr *lo)
1531 {
1532 	struct inode *inode = lo->plh_inode;
1533 	const struct cred *cred;
1534 
1535 	spin_lock(&inode->i_lock);
1536 	if (!pnfs_layout_is_valid(lo)) {
1537 		spin_unlock(&inode->i_lock);
1538 		return 0;
1539 	}
1540 	cred = get_cred(lo->plh_lc_cred);
1541 	pnfs_get_layout_hdr(lo);
1542 	spin_unlock(&inode->i_lock);
1543 
1544 	return pnfs_send_layoutreturn(lo, &zero_stateid, &cred, IOMODE_ANY,
1545 				      PNFS_FL_LAYOUTRETURN_PRIVILEGED);
1546 }
1547 
pnfs_roc(struct inode * ino,struct nfs4_layoutreturn_args * args,struct nfs4_layoutreturn_res * res,const struct cred * cred)1548 bool pnfs_roc(struct inode *ino,
1549 		struct nfs4_layoutreturn_args *args,
1550 		struct nfs4_layoutreturn_res *res,
1551 		const struct cred *cred)
1552 {
1553 	struct nfs_inode *nfsi = NFS_I(ino);
1554 	struct nfs_open_context *ctx;
1555 	struct nfs4_state *state;
1556 	struct pnfs_layout_hdr *lo;
1557 	struct pnfs_layout_segment *lseg, *next;
1558 	const struct cred *lc_cred;
1559 	nfs4_stateid stateid;
1560 	enum pnfs_iomode iomode = 0;
1561 	bool layoutreturn = false, roc = false;
1562 	bool skip_read = false;
1563 
1564 	if (!nfs_have_layout(ino))
1565 		return false;
1566 retry:
1567 	rcu_read_lock();
1568 	spin_lock(&ino->i_lock);
1569 	lo = nfsi->layout;
1570 	if (!lo || !pnfs_layout_is_valid(lo) ||
1571 	    test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1572 		lo = NULL;
1573 		goto out_noroc;
1574 	}
1575 	pnfs_get_layout_hdr(lo);
1576 	if (test_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags)) {
1577 		spin_unlock(&ino->i_lock);
1578 		rcu_read_unlock();
1579 		wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN,
1580 				TASK_UNINTERRUPTIBLE);
1581 		pnfs_put_layout_hdr(lo);
1582 		goto retry;
1583 	}
1584 
1585 	/* no roc if we hold a delegation */
1586 	if (nfs4_check_delegation(ino, FMODE_READ)) {
1587 		if (nfs4_check_delegation(ino, FMODE_WRITE))
1588 			goto out_noroc;
1589 		skip_read = true;
1590 	}
1591 
1592 	list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
1593 		state = ctx->state;
1594 		if (state == NULL)
1595 			continue;
1596 		/* Don't return layout if there is open file state */
1597 		if (state->state & FMODE_WRITE)
1598 			goto out_noroc;
1599 		if (state->state & FMODE_READ)
1600 			skip_read = true;
1601 	}
1602 
1603 
1604 	list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) {
1605 		if (skip_read && lseg->pls_range.iomode == IOMODE_READ)
1606 			continue;
1607 		/* If we are sending layoutreturn, invalidate all valid lsegs */
1608 		if (!test_and_clear_bit(NFS_LSEG_ROC, &lseg->pls_flags))
1609 			continue;
1610 		/*
1611 		 * Note: mark lseg for return so pnfs_layout_remove_lseg
1612 		 * doesn't invalidate the layout for us.
1613 		 */
1614 		set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
1615 		if (!mark_lseg_invalid(lseg, &lo->plh_return_segs))
1616 			continue;
1617 		pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
1618 	}
1619 
1620 	if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
1621 		goto out_noroc;
1622 
1623 	/* ROC in two conditions:
1624 	 * 1. there are ROC lsegs
1625 	 * 2. we don't send layoutreturn
1626 	 */
1627 	/* lo ref dropped in pnfs_roc_release() */
1628 	layoutreturn = pnfs_prepare_layoutreturn(lo, &stateid, &lc_cred, &iomode);
1629 	/* If the creds don't match, we can't compound the layoutreturn */
1630 	if (!layoutreturn || cred_fscmp(cred, lc_cred) != 0)
1631 		goto out_noroc;
1632 
1633 	roc = layoutreturn;
1634 	pnfs_init_layoutreturn_args(args, lo, &stateid, iomode);
1635 	res->lrs_present = 0;
1636 	layoutreturn = false;
1637 	put_cred(lc_cred);
1638 
1639 out_noroc:
1640 	spin_unlock(&ino->i_lock);
1641 	rcu_read_unlock();
1642 	pnfs_layoutcommit_inode(ino, true);
1643 	if (roc) {
1644 		struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
1645 		if (ld->prepare_layoutreturn)
1646 			ld->prepare_layoutreturn(args);
1647 		pnfs_put_layout_hdr(lo);
1648 		return true;
1649 	}
1650 	if (layoutreturn)
1651 		pnfs_send_layoutreturn(lo, &stateid, &lc_cred, iomode, 0);
1652 	pnfs_put_layout_hdr(lo);
1653 	return false;
1654 }
1655 
pnfs_roc_done(struct rpc_task * task,struct nfs4_layoutreturn_args ** argpp,struct nfs4_layoutreturn_res ** respp,int * ret)1656 int pnfs_roc_done(struct rpc_task *task, struct nfs4_layoutreturn_args **argpp,
1657 		  struct nfs4_layoutreturn_res **respp, int *ret)
1658 {
1659 	struct nfs4_layoutreturn_args *arg = *argpp;
1660 	int retval = -EAGAIN;
1661 
1662 	if (!arg)
1663 		return 0;
1664 	/* Handle Layoutreturn errors */
1665 	switch (*ret) {
1666 	case 0:
1667 		retval = 0;
1668 		break;
1669 	case -NFS4ERR_NOMATCHING_LAYOUT:
1670 		/* Was there an RPC level error? If not, retry */
1671 		if (task->tk_rpc_status == 0)
1672 			break;
1673 		/* If the call was not sent, let caller handle it */
1674 		if (!RPC_WAS_SENT(task))
1675 			return 0;
1676 		/*
1677 		 * Otherwise, assume the call succeeded and
1678 		 * that we need to release the layout
1679 		 */
1680 		*ret = 0;
1681 		(*respp)->lrs_present = 0;
1682 		retval = 0;
1683 		break;
1684 	case -NFS4ERR_DELAY:
1685 		/* Let the caller handle the retry */
1686 		*ret = -NFS4ERR_NOMATCHING_LAYOUT;
1687 		return 0;
1688 	case -NFS4ERR_OLD_STATEID:
1689 		if (!nfs4_layout_refresh_old_stateid(&arg->stateid,
1690 						     &arg->range, arg->inode))
1691 			break;
1692 		*ret = -NFS4ERR_NOMATCHING_LAYOUT;
1693 		return -EAGAIN;
1694 	}
1695 	*argpp = NULL;
1696 	*respp = NULL;
1697 	return retval;
1698 }
1699 
pnfs_roc_release(struct nfs4_layoutreturn_args * args,struct nfs4_layoutreturn_res * res,int ret)1700 void pnfs_roc_release(struct nfs4_layoutreturn_args *args,
1701 		      struct nfs4_layoutreturn_res *res, int ret)
1702 {
1703 	struct pnfs_layout_hdr *lo = args->layout;
1704 	struct inode *inode = args->inode;
1705 	const nfs4_stateid *res_stateid = NULL;
1706 	struct nfs4_xdr_opaque_data *ld_private = args->ld_private;
1707 
1708 	switch (ret) {
1709 	case -NFS4ERR_BADSESSION:
1710 	case -NFS4ERR_DEADSESSION:
1711 	case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1712 	case -NFS4ERR_NOMATCHING_LAYOUT:
1713 		spin_lock(&inode->i_lock);
1714 		pnfs_layoutreturn_retry_later_locked(lo, &args->stateid,
1715 						     &args->range);
1716 		pnfs_clear_layoutreturn_waitbit(lo);
1717 		spin_unlock(&inode->i_lock);
1718 		break;
1719 	case 0:
1720 		if (res->lrs_present)
1721 			res_stateid = &res->stateid;
1722 		fallthrough;
1723 	default:
1724 		pnfs_layoutreturn_free_lsegs(lo, &args->stateid, &args->range,
1725 					     res_stateid);
1726 	}
1727 	trace_nfs4_layoutreturn_on_close(args->inode, &args->stateid, ret);
1728 	if (ld_private && ld_private->ops && ld_private->ops->free)
1729 		ld_private->ops->free(ld_private);
1730 	pnfs_put_layout_hdr(lo);
1731 }
1732 
pnfs_wait_on_layoutreturn(struct inode * ino,struct rpc_task * task)1733 bool pnfs_wait_on_layoutreturn(struct inode *ino, struct rpc_task *task)
1734 {
1735 	struct nfs_inode *nfsi = NFS_I(ino);
1736         struct pnfs_layout_hdr *lo;
1737         bool sleep = false;
1738 
1739 	/* we might not have grabbed lo reference. so need to check under
1740 	 * i_lock */
1741         spin_lock(&ino->i_lock);
1742         lo = nfsi->layout;
1743         if (lo && test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
1744                 rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
1745                 sleep = true;
1746 	}
1747         spin_unlock(&ino->i_lock);
1748         return sleep;
1749 }
1750 
1751 /*
1752  * Compare two layout segments for sorting into layout cache.
1753  * We want to preferentially return RW over RO layouts, so ensure those
1754  * are seen first.
1755  */
1756 static s64
pnfs_lseg_range_cmp(const struct pnfs_layout_range * l1,const struct pnfs_layout_range * l2)1757 pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1,
1758 	   const struct pnfs_layout_range *l2)
1759 {
1760 	s64 d;
1761 
1762 	/* high offset > low offset */
1763 	d = l1->offset - l2->offset;
1764 	if (d)
1765 		return d;
1766 
1767 	/* short length > long length */
1768 	d = l2->length - l1->length;
1769 	if (d)
1770 		return d;
1771 
1772 	/* read > read/write */
1773 	return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
1774 }
1775 
1776 static bool
pnfs_lseg_range_is_after(const struct pnfs_layout_range * l1,const struct pnfs_layout_range * l2)1777 pnfs_lseg_range_is_after(const struct pnfs_layout_range *l1,
1778 		const struct pnfs_layout_range *l2)
1779 {
1780 	return pnfs_lseg_range_cmp(l1, l2) > 0;
1781 }
1782 
1783 static bool
pnfs_lseg_no_merge(struct pnfs_layout_segment * lseg,struct pnfs_layout_segment * old)1784 pnfs_lseg_no_merge(struct pnfs_layout_segment *lseg,
1785 		struct pnfs_layout_segment *old)
1786 {
1787 	return false;
1788 }
1789 
1790 void
pnfs_generic_layout_insert_lseg(struct pnfs_layout_hdr * lo,struct pnfs_layout_segment * lseg,bool (* is_after)(const struct pnfs_layout_range *,const struct pnfs_layout_range *),bool (* do_merge)(struct pnfs_layout_segment *,struct pnfs_layout_segment *),struct list_head * free_me)1791 pnfs_generic_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1792 		   struct pnfs_layout_segment *lseg,
1793 		   bool (*is_after)(const struct pnfs_layout_range *,
1794 			   const struct pnfs_layout_range *),
1795 		   bool (*do_merge)(struct pnfs_layout_segment *,
1796 			   struct pnfs_layout_segment *),
1797 		   struct list_head *free_me)
1798 {
1799 	struct pnfs_layout_segment *lp, *tmp;
1800 
1801 	dprintk("%s:Begin\n", __func__);
1802 
1803 	list_for_each_entry_safe(lp, tmp, &lo->plh_segs, pls_list) {
1804 		if (test_bit(NFS_LSEG_VALID, &lp->pls_flags) == 0)
1805 			continue;
1806 		if (do_merge(lseg, lp)) {
1807 			mark_lseg_invalid(lp, free_me);
1808 			continue;
1809 		}
1810 		if (is_after(&lseg->pls_range, &lp->pls_range))
1811 			continue;
1812 		list_add_tail(&lseg->pls_list, &lp->pls_list);
1813 		dprintk("%s: inserted lseg %p "
1814 			"iomode %d offset %llu length %llu before "
1815 			"lp %p iomode %d offset %llu length %llu\n",
1816 			__func__, lseg, lseg->pls_range.iomode,
1817 			lseg->pls_range.offset, lseg->pls_range.length,
1818 			lp, lp->pls_range.iomode, lp->pls_range.offset,
1819 			lp->pls_range.length);
1820 		goto out;
1821 	}
1822 	list_add_tail(&lseg->pls_list, &lo->plh_segs);
1823 	dprintk("%s: inserted lseg %p "
1824 		"iomode %d offset %llu length %llu at tail\n",
1825 		__func__, lseg, lseg->pls_range.iomode,
1826 		lseg->pls_range.offset, lseg->pls_range.length);
1827 out:
1828 	pnfs_get_layout_hdr(lo);
1829 
1830 	dprintk("%s:Return\n", __func__);
1831 }
1832 EXPORT_SYMBOL_GPL(pnfs_generic_layout_insert_lseg);
1833 
1834 static void
pnfs_layout_insert_lseg(struct pnfs_layout_hdr * lo,struct pnfs_layout_segment * lseg,struct list_head * free_me)1835 pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1836 		   struct pnfs_layout_segment *lseg,
1837 		   struct list_head *free_me)
1838 {
1839 	struct inode *inode = lo->plh_inode;
1840 	struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
1841 
1842 	if (ld->add_lseg != NULL)
1843 		ld->add_lseg(lo, lseg, free_me);
1844 	else
1845 		pnfs_generic_layout_insert_lseg(lo, lseg,
1846 				pnfs_lseg_range_is_after,
1847 				pnfs_lseg_no_merge,
1848 				free_me);
1849 }
1850 
1851 static struct pnfs_layout_hdr *
alloc_init_layout_hdr(struct inode * ino,struct nfs_open_context * ctx,gfp_t gfp_flags)1852 alloc_init_layout_hdr(struct inode *ino,
1853 		      struct nfs_open_context *ctx,
1854 		      gfp_t gfp_flags)
1855 {
1856 	struct pnfs_layout_hdr *lo;
1857 
1858 	lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
1859 	if (!lo)
1860 		return NULL;
1861 	refcount_set(&lo->plh_refcount, 1);
1862 	INIT_LIST_HEAD(&lo->plh_layouts);
1863 	INIT_LIST_HEAD(&lo->plh_segs);
1864 	INIT_LIST_HEAD(&lo->plh_return_segs);
1865 	INIT_LIST_HEAD(&lo->plh_bulk_destroy);
1866 	lo->plh_inode = ino;
1867 	lo->plh_lc_cred = get_cred(ctx->cred);
1868 	lo->plh_flags |= 1 << NFS_LAYOUT_INVALID_STID;
1869 	return lo;
1870 }
1871 
1872 static struct pnfs_layout_hdr *
pnfs_find_alloc_layout(struct inode * ino,struct nfs_open_context * ctx,gfp_t gfp_flags)1873 pnfs_find_alloc_layout(struct inode *ino,
1874 		       struct nfs_open_context *ctx,
1875 		       gfp_t gfp_flags)
1876 	__releases(&ino->i_lock)
1877 	__acquires(&ino->i_lock)
1878 {
1879 	struct nfs_inode *nfsi = NFS_I(ino);
1880 	struct pnfs_layout_hdr *new = NULL;
1881 
1882 	dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
1883 
1884 	if (nfsi->layout != NULL)
1885 		goto out_existing;
1886 	spin_unlock(&ino->i_lock);
1887 	new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
1888 	spin_lock(&ino->i_lock);
1889 
1890 	if (likely(nfsi->layout == NULL)) {	/* Won the race? */
1891 		nfsi->layout = new;
1892 		return new;
1893 	} else if (new != NULL)
1894 		pnfs_free_layout_hdr(new);
1895 out_existing:
1896 	pnfs_get_layout_hdr(nfsi->layout);
1897 	return nfsi->layout;
1898 }
1899 
1900 /*
1901  * iomode matching rules:
1902  * iomode	lseg	strict match
1903  *                      iomode
1904  * -----	-----	------ -----
1905  * ANY		READ	N/A    true
1906  * ANY		RW	N/A    true
1907  * RW		READ	N/A    false
1908  * RW		RW	N/A    true
1909  * READ		READ	N/A    true
1910  * READ		RW	true   false
1911  * READ		RW	false  true
1912  */
1913 static bool
pnfs_lseg_range_match(const struct pnfs_layout_range * ls_range,const struct pnfs_layout_range * range,bool strict_iomode)1914 pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range,
1915 		 const struct pnfs_layout_range *range,
1916 		 bool strict_iomode)
1917 {
1918 	struct pnfs_layout_range range1;
1919 
1920 	if ((range->iomode == IOMODE_RW &&
1921 	     ls_range->iomode != IOMODE_RW) ||
1922 	    (range->iomode != ls_range->iomode &&
1923 	     strict_iomode) ||
1924 	    !pnfs_lseg_range_intersecting(ls_range, range))
1925 		return false;
1926 
1927 	/* range1 covers only the first byte in the range */
1928 	range1 = *range;
1929 	range1.length = 1;
1930 	return pnfs_lseg_range_contained(ls_range, &range1);
1931 }
1932 
1933 /*
1934  * lookup range in layout
1935  */
1936 static struct pnfs_layout_segment *
pnfs_find_lseg(struct pnfs_layout_hdr * lo,struct pnfs_layout_range * range,bool strict_iomode)1937 pnfs_find_lseg(struct pnfs_layout_hdr *lo,
1938 		struct pnfs_layout_range *range,
1939 		bool strict_iomode)
1940 {
1941 	struct pnfs_layout_segment *lseg, *ret = NULL;
1942 
1943 	dprintk("%s:Begin\n", __func__);
1944 
1945 	list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
1946 		if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
1947 		    pnfs_lseg_range_match(&lseg->pls_range, range,
1948 					  strict_iomode)) {
1949 			ret = pnfs_get_lseg(lseg);
1950 			break;
1951 		}
1952 	}
1953 
1954 	dprintk("%s:Return lseg %p ref %d\n",
1955 		__func__, ret, ret ? refcount_read(&ret->pls_refcount) : 0);
1956 	return ret;
1957 }
1958 
1959 /*
1960  * Use mdsthreshold hints set at each OPEN to determine if I/O should go
1961  * to the MDS or over pNFS
1962  *
1963  * The nfs_inode read_io and write_io fields are cumulative counters reset
1964  * when there are no layout segments. Note that in pnfs_update_layout iomode
1965  * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
1966  * WRITE request.
1967  *
1968  * A return of true means use MDS I/O.
1969  *
1970  * From rfc 5661:
1971  * If a file's size is smaller than the file size threshold, data accesses
1972  * SHOULD be sent to the metadata server.  If an I/O request has a length that
1973  * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
1974  * server.  If both file size and I/O size are provided, the client SHOULD
1975  * reach or exceed  both thresholds before sending its read or write
1976  * requests to the data server.
1977  */
pnfs_within_mdsthreshold(struct nfs_open_context * ctx,struct inode * ino,int iomode)1978 static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
1979 				     struct inode *ino, int iomode)
1980 {
1981 	struct nfs4_threshold *t = ctx->mdsthreshold;
1982 	struct nfs_inode *nfsi = NFS_I(ino);
1983 	loff_t fsize = i_size_read(ino);
1984 	bool size = false, size_set = false, io = false, io_set = false, ret = false;
1985 
1986 	if (t == NULL)
1987 		return ret;
1988 
1989 	dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
1990 		__func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
1991 
1992 	switch (iomode) {
1993 	case IOMODE_READ:
1994 		if (t->bm & THRESHOLD_RD) {
1995 			dprintk("%s fsize %llu\n", __func__, fsize);
1996 			size_set = true;
1997 			if (fsize < t->rd_sz)
1998 				size = true;
1999 		}
2000 		if (t->bm & THRESHOLD_RD_IO) {
2001 			dprintk("%s nfsi->read_io %llu\n", __func__,
2002 				nfsi->read_io);
2003 			io_set = true;
2004 			if (nfsi->read_io < t->rd_io_sz)
2005 				io = true;
2006 		}
2007 		break;
2008 	case IOMODE_RW:
2009 		if (t->bm & THRESHOLD_WR) {
2010 			dprintk("%s fsize %llu\n", __func__, fsize);
2011 			size_set = true;
2012 			if (fsize < t->wr_sz)
2013 				size = true;
2014 		}
2015 		if (t->bm & THRESHOLD_WR_IO) {
2016 			dprintk("%s nfsi->write_io %llu\n", __func__,
2017 				nfsi->write_io);
2018 			io_set = true;
2019 			if (nfsi->write_io < t->wr_io_sz)
2020 				io = true;
2021 		}
2022 		break;
2023 	}
2024 	if (size_set && io_set) {
2025 		if (size && io)
2026 			ret = true;
2027 	} else if (size || io)
2028 		ret = true;
2029 
2030 	dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
2031 	return ret;
2032 }
2033 
pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr * lo)2034 static int pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo)
2035 {
2036 	/*
2037 	 * send layoutcommit as it can hold up layoutreturn due to lseg
2038 	 * reference
2039 	 */
2040 	pnfs_layoutcommit_inode(lo->plh_inode, false);
2041 	return wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN,
2042 				   nfs_wait_bit_killable,
2043 				   TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
2044 }
2045 
nfs_layoutget_begin(struct pnfs_layout_hdr * lo)2046 static void nfs_layoutget_begin(struct pnfs_layout_hdr *lo)
2047 {
2048 	atomic_inc(&lo->plh_outstanding);
2049 }
2050 
nfs_layoutget_end(struct pnfs_layout_hdr * lo)2051 static void nfs_layoutget_end(struct pnfs_layout_hdr *lo)
2052 {
2053 	if (atomic_dec_and_test(&lo->plh_outstanding) &&
2054 	    test_and_clear_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags)) {
2055 		smp_mb__after_atomic();
2056 		wake_up_bit(&lo->plh_flags, NFS_LAYOUT_DRAIN);
2057 	}
2058 }
2059 
pnfs_is_first_layoutget(struct pnfs_layout_hdr * lo)2060 static bool pnfs_is_first_layoutget(struct pnfs_layout_hdr *lo)
2061 {
2062 	return test_bit(NFS_LAYOUT_FIRST_LAYOUTGET, &lo->plh_flags);
2063 }
2064 
pnfs_clear_first_layoutget(struct pnfs_layout_hdr * lo)2065 static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo)
2066 {
2067 	unsigned long *bitlock = &lo->plh_flags;
2068 
2069 	clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock);
2070 	smp_mb__after_atomic();
2071 	wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET);
2072 }
2073 
_add_to_server_list(struct pnfs_layout_hdr * lo,struct nfs_server * server)2074 static void _add_to_server_list(struct pnfs_layout_hdr *lo,
2075 				struct nfs_server *server)
2076 {
2077 	if (!test_and_set_bit(NFS_LAYOUT_HASHED, &lo->plh_flags)) {
2078 		struct nfs_client *clp = server->nfs_client;
2079 
2080 		/* The lo must be on the clp list if there is any
2081 		 * chance of a CB_LAYOUTRECALL(FILE) coming in.
2082 		 */
2083 		spin_lock(&clp->cl_lock);
2084 		list_add_tail_rcu(&lo->plh_layouts, &server->layouts);
2085 		spin_unlock(&clp->cl_lock);
2086 	}
2087 }
2088 
2089 /*
2090  * Layout segment is retreived from the server if not cached.
2091  * The appropriate layout segment is referenced and returned to the caller.
2092  */
2093 struct pnfs_layout_segment *
pnfs_update_layout(struct inode * ino,struct nfs_open_context * ctx,loff_t pos,u64 count,enum pnfs_iomode iomode,bool strict_iomode,gfp_t gfp_flags)2094 pnfs_update_layout(struct inode *ino,
2095 		   struct nfs_open_context *ctx,
2096 		   loff_t pos,
2097 		   u64 count,
2098 		   enum pnfs_iomode iomode,
2099 		   bool strict_iomode,
2100 		   gfp_t gfp_flags)
2101 {
2102 	struct pnfs_layout_range arg = {
2103 		.iomode = iomode,
2104 		.offset = pos,
2105 		.length = count,
2106 	};
2107 	unsigned pg_offset;
2108 	struct nfs_server *server = NFS_SERVER(ino);
2109 	struct nfs_client *clp = server->nfs_client;
2110 	struct pnfs_layout_hdr *lo = NULL;
2111 	struct pnfs_layout_segment *lseg = NULL;
2112 	struct nfs4_layoutget *lgp;
2113 	nfs4_stateid stateid;
2114 	struct nfs4_exception exception = {
2115 		.inode = ino,
2116 	};
2117 	unsigned long giveup = jiffies + (clp->cl_lease_time << 1);
2118 	bool first;
2119 
2120 	if (!pnfs_enabled_sb(NFS_SERVER(ino))) {
2121 		trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2122 				 PNFS_UPDATE_LAYOUT_NO_PNFS);
2123 		goto out;
2124 	}
2125 
2126 	if (pnfs_within_mdsthreshold(ctx, ino, iomode)) {
2127 		trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2128 				 PNFS_UPDATE_LAYOUT_MDSTHRESH);
2129 		goto out;
2130 	}
2131 
2132 lookup_again:
2133 	if (!nfs4_valid_open_stateid(ctx->state)) {
2134 		trace_pnfs_update_layout(ino, pos, count,
2135 					 iomode, lo, lseg,
2136 					 PNFS_UPDATE_LAYOUT_INVALID_OPEN);
2137 		lseg = ERR_PTR(-EIO);
2138 		goto out;
2139 	}
2140 
2141 	lseg = ERR_PTR(nfs4_client_recover_expired_lease(clp));
2142 	if (IS_ERR(lseg))
2143 		goto out;
2144 	first = false;
2145 	spin_lock(&ino->i_lock);
2146 	lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
2147 	if (lo == NULL) {
2148 		spin_unlock(&ino->i_lock);
2149 		lseg = ERR_PTR(-ENOMEM);
2150 		trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2151 				 PNFS_UPDATE_LAYOUT_NOMEM);
2152 		goto out;
2153 	}
2154 
2155 	/* Do we even need to bother with this? */
2156 	if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
2157 		trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2158 				 PNFS_UPDATE_LAYOUT_BULK_RECALL);
2159 		dprintk("%s matches recall, use MDS\n", __func__);
2160 		goto out_unlock;
2161 	}
2162 
2163 	/* if LAYOUTGET already failed once we don't try again */
2164 	if (pnfs_layout_io_test_failed(lo, iomode)) {
2165 		trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2166 				 PNFS_UPDATE_LAYOUT_IO_TEST_FAIL);
2167 		goto out_unlock;
2168 	}
2169 
2170 	/*
2171 	 * If the layout segment list is empty, but there are outstanding
2172 	 * layoutget calls, then they might be subject to a layoutrecall.
2173 	 */
2174 	if (test_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags) &&
2175 	    atomic_read(&lo->plh_outstanding) != 0) {
2176 		spin_unlock(&ino->i_lock);
2177 		lseg = ERR_PTR(wait_on_bit(&lo->plh_flags, NFS_LAYOUT_DRAIN,
2178 					   TASK_KILLABLE));
2179 		if (IS_ERR(lseg))
2180 			goto out_put_layout_hdr;
2181 		pnfs_put_layout_hdr(lo);
2182 		goto lookup_again;
2183 	}
2184 
2185 	/*
2186 	 * Because we free lsegs when sending LAYOUTRETURN, we need to wait
2187 	 * for LAYOUTRETURN.
2188 	 */
2189 	if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
2190 		spin_unlock(&ino->i_lock);
2191 		dprintk("%s wait for layoutreturn\n", __func__);
2192 		lseg = ERR_PTR(pnfs_prepare_to_retry_layoutget(lo));
2193 		if (!IS_ERR(lseg)) {
2194 			pnfs_put_layout_hdr(lo);
2195 			dprintk("%s retrying\n", __func__);
2196 			trace_pnfs_update_layout(ino, pos, count, iomode, lo,
2197 						 lseg,
2198 						 PNFS_UPDATE_LAYOUT_RETRY);
2199 			goto lookup_again;
2200 		}
2201 		trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2202 					 PNFS_UPDATE_LAYOUT_RETURN);
2203 		goto out_put_layout_hdr;
2204 	}
2205 
2206 	lseg = pnfs_find_lseg(lo, &arg, strict_iomode);
2207 	if (lseg) {
2208 		trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2209 				PNFS_UPDATE_LAYOUT_FOUND_CACHED);
2210 		goto out_unlock;
2211 	}
2212 
2213 	/*
2214 	 * Choose a stateid for the LAYOUTGET. If we don't have a layout
2215 	 * stateid, or it has been invalidated, then we must use the open
2216 	 * stateid.
2217 	 */
2218 	if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) {
2219 		int status;
2220 
2221 		/*
2222 		 * The first layoutget for the file. Need to serialize per
2223 		 * RFC 5661 Errata 3208.
2224 		 */
2225 		if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET,
2226 				     &lo->plh_flags)) {
2227 			spin_unlock(&ino->i_lock);
2228 			lseg = ERR_PTR(wait_on_bit(&lo->plh_flags,
2229 						NFS_LAYOUT_FIRST_LAYOUTGET,
2230 						TASK_KILLABLE));
2231 			if (IS_ERR(lseg))
2232 				goto out_put_layout_hdr;
2233 			pnfs_put_layout_hdr(lo);
2234 			dprintk("%s retrying\n", __func__);
2235 			goto lookup_again;
2236 		}
2237 
2238 		spin_unlock(&ino->i_lock);
2239 		first = true;
2240 		status = nfs4_select_rw_stateid(ctx->state,
2241 					iomode == IOMODE_RW ? FMODE_WRITE : FMODE_READ,
2242 					NULL, &stateid, NULL);
2243 		if (status != 0) {
2244 			lseg = ERR_PTR(status);
2245 			trace_pnfs_update_layout(ino, pos, count,
2246 					iomode, lo, lseg,
2247 					PNFS_UPDATE_LAYOUT_INVALID_OPEN);
2248 			nfs4_schedule_stateid_recovery(server, ctx->state);
2249 			pnfs_clear_first_layoutget(lo);
2250 			pnfs_put_layout_hdr(lo);
2251 			goto lookup_again;
2252 		}
2253 		spin_lock(&ino->i_lock);
2254 	} else {
2255 		nfs4_stateid_copy(&stateid, &lo->plh_stateid);
2256 	}
2257 
2258 	if (pnfs_layoutgets_blocked(lo)) {
2259 		trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2260 				PNFS_UPDATE_LAYOUT_BLOCKED);
2261 		goto out_unlock;
2262 	}
2263 	nfs_layoutget_begin(lo);
2264 	spin_unlock(&ino->i_lock);
2265 
2266 	_add_to_server_list(lo, server);
2267 
2268 	pg_offset = arg.offset & ~PAGE_MASK;
2269 	if (pg_offset) {
2270 		arg.offset -= pg_offset;
2271 		arg.length += pg_offset;
2272 	}
2273 	if (arg.length != NFS4_MAX_UINT64)
2274 		arg.length = PAGE_ALIGN(arg.length);
2275 
2276 	lgp = pnfs_alloc_init_layoutget_args(ino, ctx, &stateid, &arg, gfp_flags);
2277 	if (!lgp) {
2278 		lseg = ERR_PTR(-ENOMEM);
2279 		trace_pnfs_update_layout(ino, pos, count, iomode, lo, NULL,
2280 					 PNFS_UPDATE_LAYOUT_NOMEM);
2281 		nfs_layoutget_end(lo);
2282 		goto out_put_layout_hdr;
2283 	}
2284 
2285 	lgp->lo = lo;
2286 	pnfs_get_layout_hdr(lo);
2287 
2288 	lseg = nfs4_proc_layoutget(lgp, &exception);
2289 	trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2290 				 PNFS_UPDATE_LAYOUT_SEND_LAYOUTGET);
2291 	nfs_layoutget_end(lo);
2292 	if (IS_ERR(lseg)) {
2293 		switch(PTR_ERR(lseg)) {
2294 		case -EBUSY:
2295 			if (time_after(jiffies, giveup))
2296 				lseg = NULL;
2297 			break;
2298 		case -ERECALLCONFLICT:
2299 		case -EAGAIN:
2300 			break;
2301 		case -ENODATA:
2302 			/* The server returned NFS4ERR_LAYOUTUNAVAILABLE */
2303 			pnfs_layout_set_fail_bit(
2304 				lo, pnfs_iomode_to_fail_bit(iomode));
2305 			lseg = NULL;
2306 			goto out_put_layout_hdr;
2307 		default:
2308 			if (!nfs_error_is_fatal(PTR_ERR(lseg))) {
2309 				pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
2310 				lseg = NULL;
2311 			}
2312 			goto out_put_layout_hdr;
2313 		}
2314 		if (lseg) {
2315 			if (!exception.retry)
2316 				goto out_put_layout_hdr;
2317 			if (first)
2318 				pnfs_clear_first_layoutget(lo);
2319 			trace_pnfs_update_layout(ino, pos, count,
2320 				iomode, lo, lseg, PNFS_UPDATE_LAYOUT_RETRY);
2321 			pnfs_put_layout_hdr(lo);
2322 			goto lookup_again;
2323 		}
2324 	} else {
2325 		pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
2326 	}
2327 
2328 out_put_layout_hdr:
2329 	if (first)
2330 		pnfs_clear_first_layoutget(lo);
2331 	trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
2332 				 PNFS_UPDATE_LAYOUT_EXIT);
2333 	pnfs_put_layout_hdr(lo);
2334 out:
2335 	dprintk("%s: inode %s/%llu pNFS layout segment %s for "
2336 			"(%s, offset: %llu, length: %llu)\n",
2337 			__func__, ino->i_sb->s_id,
2338 			(unsigned long long)NFS_FILEID(ino),
2339 			IS_ERR_OR_NULL(lseg) ? "not found" : "found",
2340 			iomode==IOMODE_RW ?  "read/write" : "read-only",
2341 			(unsigned long long)pos,
2342 			(unsigned long long)count);
2343 	return lseg;
2344 out_unlock:
2345 	spin_unlock(&ino->i_lock);
2346 	goto out_put_layout_hdr;
2347 }
2348 EXPORT_SYMBOL_GPL(pnfs_update_layout);
2349 
2350 static bool
pnfs_sanity_check_layout_range(struct pnfs_layout_range * range)2351 pnfs_sanity_check_layout_range(struct pnfs_layout_range *range)
2352 {
2353 	switch (range->iomode) {
2354 	case IOMODE_READ:
2355 	case IOMODE_RW:
2356 		break;
2357 	default:
2358 		return false;
2359 	}
2360 	if (range->offset == NFS4_MAX_UINT64)
2361 		return false;
2362 	if (range->length == 0)
2363 		return false;
2364 	if (range->length != NFS4_MAX_UINT64 &&
2365 	    range->length > NFS4_MAX_UINT64 - range->offset)
2366 		return false;
2367 	return true;
2368 }
2369 
2370 static struct pnfs_layout_hdr *
_pnfs_grab_empty_layout(struct inode * ino,struct nfs_open_context * ctx)2371 _pnfs_grab_empty_layout(struct inode *ino, struct nfs_open_context *ctx)
2372 {
2373 	struct pnfs_layout_hdr *lo;
2374 
2375 	spin_lock(&ino->i_lock);
2376 	lo = pnfs_find_alloc_layout(ino, ctx, nfs_io_gfp_mask());
2377 	if (!lo)
2378 		goto out_unlock;
2379 	if (!test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags))
2380 		goto out_unlock;
2381 	if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags))
2382 		goto out_unlock;
2383 	if (pnfs_layoutgets_blocked(lo))
2384 		goto out_unlock;
2385 	if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET, &lo->plh_flags))
2386 		goto out_unlock;
2387 	nfs_layoutget_begin(lo);
2388 	spin_unlock(&ino->i_lock);
2389 	_add_to_server_list(lo, NFS_SERVER(ino));
2390 	return lo;
2391 
2392 out_unlock:
2393 	spin_unlock(&ino->i_lock);
2394 	pnfs_put_layout_hdr(lo);
2395 	return NULL;
2396 }
2397 
_lgopen_prepare_attached(struct nfs4_opendata * data,struct nfs_open_context * ctx)2398 static void _lgopen_prepare_attached(struct nfs4_opendata *data,
2399 				     struct nfs_open_context *ctx)
2400 {
2401 	struct inode *ino = data->dentry->d_inode;
2402 	struct pnfs_layout_range rng = {
2403 		.iomode = (data->o_arg.fmode & FMODE_WRITE) ?
2404 			  IOMODE_RW: IOMODE_READ,
2405 		.offset = 0,
2406 		.length = NFS4_MAX_UINT64,
2407 	};
2408 	struct nfs4_layoutget *lgp;
2409 	struct pnfs_layout_hdr *lo;
2410 
2411 	/* Heuristic: don't send layoutget if we have cached data */
2412 	if (rng.iomode == IOMODE_READ &&
2413 	   (i_size_read(ino) == 0 || ino->i_mapping->nrpages != 0))
2414 		return;
2415 
2416 	lo = _pnfs_grab_empty_layout(ino, ctx);
2417 	if (!lo)
2418 		return;
2419 	lgp = pnfs_alloc_init_layoutget_args(ino, ctx, ¤t_stateid, &rng,
2420 					     nfs_io_gfp_mask());
2421 	if (!lgp) {
2422 		pnfs_clear_first_layoutget(lo);
2423 		nfs_layoutget_end(lo);
2424 		pnfs_put_layout_hdr(lo);
2425 		return;
2426 	}
2427 	lgp->lo = lo;
2428 	data->lgp = lgp;
2429 	data->o_arg.lg_args = &lgp->args;
2430 	data->o_res.lg_res = &lgp->res;
2431 }
2432 
_lgopen_prepare_floating(struct nfs4_opendata * data,struct nfs_open_context * ctx)2433 static void _lgopen_prepare_floating(struct nfs4_opendata *data,
2434 				     struct nfs_open_context *ctx)
2435 {
2436 	struct inode *ino = data->dentry->d_inode;
2437 	struct pnfs_layout_range rng = {
2438 		.iomode = (data->o_arg.fmode & FMODE_WRITE) ?
2439 			  IOMODE_RW: IOMODE_READ,
2440 		.offset = 0,
2441 		.length = NFS4_MAX_UINT64,
2442 	};
2443 	struct nfs4_layoutget *lgp;
2444 
2445 	lgp = pnfs_alloc_init_layoutget_args(ino, ctx, ¤t_stateid, &rng,
2446 					     nfs_io_gfp_mask());
2447 	if (!lgp)
2448 		return;
2449 	data->lgp = lgp;
2450 	data->o_arg.lg_args = &lgp->args;
2451 	data->o_res.lg_res = &lgp->res;
2452 }
2453 
pnfs_lgopen_prepare(struct nfs4_opendata * data,struct nfs_open_context * ctx)2454 void pnfs_lgopen_prepare(struct nfs4_opendata *data,
2455 			 struct nfs_open_context *ctx)
2456 {
2457 	struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
2458 
2459 	if (!(pnfs_enabled_sb(server) &&
2460 	      server->pnfs_curr_ld->flags & PNFS_LAYOUTGET_ON_OPEN))
2461 		return;
2462 	/* Could check on max_ops, but currently hardcoded high enough */
2463 	if (!nfs_server_capable(data->dir->d_inode, NFS_CAP_LGOPEN))
2464 		return;
2465 	if (data->lgp)
2466 		return;
2467 	if (data->state)
2468 		_lgopen_prepare_attached(data, ctx);
2469 	else
2470 		_lgopen_prepare_floating(data, ctx);
2471 }
2472 
pnfs_parse_lgopen(struct inode * ino,struct nfs4_layoutget * lgp,struct nfs_open_context * ctx)2473 void pnfs_parse_lgopen(struct inode *ino, struct nfs4_layoutget *lgp,
2474 		       struct nfs_open_context *ctx)
2475 {
2476 	struct pnfs_layout_hdr *lo;
2477 	struct pnfs_layout_segment *lseg;
2478 	struct nfs_server *srv = NFS_SERVER(ino);
2479 	u32 iomode;
2480 
2481 	if (!lgp)
2482 		return;
2483 	dprintk("%s: entered with status %i\n", __func__, lgp->res.status);
2484 	if (lgp->res.status) {
2485 		switch (lgp->res.status) {
2486 		default:
2487 			break;
2488 		/*
2489 		 * Halt lgopen attempts if the server doesn't recognise
2490 		 * the "current stateid" value, the layout type, or the
2491 		 * layoutget operation as being valid.
2492 		 * Also if it complains about too many ops in the compound
2493 		 * or of the request/reply being too big.
2494 		 */
2495 		case -NFS4ERR_BAD_STATEID:
2496 		case -NFS4ERR_NOTSUPP:
2497 		case -NFS4ERR_REP_TOO_BIG:
2498 		case -NFS4ERR_REP_TOO_BIG_TO_CACHE:
2499 		case -NFS4ERR_REQ_TOO_BIG:
2500 		case -NFS4ERR_TOO_MANY_OPS:
2501 		case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
2502 			srv->caps &= ~NFS_CAP_LGOPEN;
2503 		}
2504 		return;
2505 	}
2506 	if (!lgp->lo) {
2507 		lo = _pnfs_grab_empty_layout(ino, ctx);
2508 		if (!lo)
2509 			return;
2510 		lgp->lo = lo;
2511 	} else
2512 		lo = lgp->lo;
2513 
2514 	lseg = pnfs_layout_process(lgp);
2515 	if (!IS_ERR(lseg)) {
2516 		iomode = lgp->args.range.iomode;
2517 		pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
2518 		pnfs_put_lseg(lseg);
2519 	}
2520 }
2521 
nfs4_lgopen_release(struct nfs4_layoutget * lgp)2522 void nfs4_lgopen_release(struct nfs4_layoutget *lgp)
2523 {
2524 	if (lgp != NULL) {
2525 		if (lgp->lo) {
2526 			pnfs_clear_first_layoutget(lgp->lo);
2527 			nfs_layoutget_end(lgp->lo);
2528 		}
2529 		pnfs_layoutget_free(lgp);
2530 	}
2531 }
2532 
2533 struct pnfs_layout_segment *
pnfs_layout_process(struct nfs4_layoutget * lgp)2534 pnfs_layout_process(struct nfs4_layoutget *lgp)
2535 {
2536 	struct pnfs_layout_hdr *lo = lgp->lo;
2537 	struct nfs4_layoutget_res *res = &lgp->res;
2538 	struct pnfs_layout_segment *lseg;
2539 	struct inode *ino = lo->plh_inode;
2540 	LIST_HEAD(free_me);
2541 
2542 	if (!pnfs_sanity_check_layout_range(&res->range))
2543 		return ERR_PTR(-EINVAL);
2544 
2545 	/* Inject layout blob into I/O device driver */
2546 	lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
2547 	if (IS_ERR_OR_NULL(lseg)) {
2548 		if (!lseg)
2549 			lseg = ERR_PTR(-ENOMEM);
2550 
2551 		dprintk("%s: Could not allocate layout: error %ld\n",
2552 		       __func__, PTR_ERR(lseg));
2553 		return lseg;
2554 	}
2555 
2556 	pnfs_init_lseg(lo, lseg, &res->range, &res->stateid);
2557 
2558 	spin_lock(&ino->i_lock);
2559 	if (pnfs_layoutgets_blocked(lo)) {
2560 		dprintk("%s forget reply due to state\n", __func__);
2561 		goto out_forget;
2562 	}
2563 
2564 	if (test_bit(NFS_LAYOUT_DRAIN, &lo->plh_flags) &&
2565 	    !pnfs_is_first_layoutget(lo))
2566 		goto out_forget;
2567 
2568 	if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) {
2569 		/* existing state ID, make sure the sequence number matches. */
2570 		if (pnfs_layout_stateid_blocked(lo, &res->stateid)) {
2571 			if (!pnfs_layout_is_valid(lo))
2572 				lo->plh_barrier = 0;
2573 			dprintk("%s forget reply due to sequence\n", __func__);
2574 			goto out_forget;
2575 		}
2576 		pnfs_set_layout_stateid(lo, &res->stateid, lgp->cred, false);
2577 	} else if (pnfs_layout_is_valid(lo)) {
2578 		/*
2579 		 * We got an entirely new state ID.  Mark all segments for the
2580 		 * inode invalid, and retry the layoutget
2581 		 */
2582 		struct pnfs_layout_range range = {
2583 			.iomode = IOMODE_ANY,
2584 			.length = NFS4_MAX_UINT64,
2585 		};
2586 		pnfs_mark_matching_lsegs_return(lo, &free_me, &range, 0);
2587 		goto out_forget;
2588 	} else {
2589 		/* We have a completely new layout */
2590 		pnfs_set_layout_stateid(lo, &res->stateid, lgp->cred, true);
2591 	}
2592 
2593 	pnfs_get_lseg(lseg);
2594 	pnfs_layout_insert_lseg(lo, lseg, &free_me);
2595 
2596 
2597 	if (res->return_on_close)
2598 		set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
2599 
2600 	spin_unlock(&ino->i_lock);
2601 	pnfs_free_lseg_list(&free_me);
2602 	return lseg;
2603 
2604 out_forget:
2605 	spin_unlock(&ino->i_lock);
2606 	lseg->pls_layout = lo;
2607 	NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
2608 	return ERR_PTR(-EAGAIN);
2609 }
2610 
2611 /**
2612  * pnfs_mark_matching_lsegs_return - Free or return matching layout segments
2613  * @lo: pointer to layout header
2614  * @tmp_list: list header to be used with pnfs_free_lseg_list()
2615  * @return_range: describe layout segment ranges to be returned
2616  * @seq: stateid seqid to match
2617  *
2618  * This function is mainly intended for use by layoutrecall. It attempts
2619  * to free the layout segment immediately, or else to mark it for return
2620  * as soon as its reference count drops to zero.
2621  *
2622  * Returns
2623  * - 0: a layoutreturn needs to be scheduled.
2624  * - EBUSY: there are layout segment that are still in use.
2625  * - ENOENT: there are no layout segments that need to be returned.
2626  */
2627 int
pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr * lo,struct list_head * tmp_list,const struct pnfs_layout_range * return_range,u32 seq)2628 pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo,
2629 				struct list_head *tmp_list,
2630 				const struct pnfs_layout_range *return_range,
2631 				u32 seq)
2632 {
2633 	struct pnfs_layout_segment *lseg, *next;
2634 	struct nfs_server *server = NFS_SERVER(lo->plh_inode);
2635 	int remaining = 0;
2636 
2637 	dprintk("%s:Begin lo %p\n", __func__, lo);
2638 
2639 	assert_spin_locked(&lo->plh_inode->i_lock);
2640 
2641 	if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
2642 		tmp_list = &lo->plh_return_segs;
2643 
2644 	list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
2645 		if (pnfs_match_lseg_recall(lseg, return_range, seq)) {
2646 			dprintk("%s: marking lseg %p iomode %d "
2647 				"offset %llu length %llu\n", __func__,
2648 				lseg, lseg->pls_range.iomode,
2649 				lseg->pls_range.offset,
2650 				lseg->pls_range.length);
2651 			if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
2652 				tmp_list = &lo->plh_return_segs;
2653 			if (mark_lseg_invalid(lseg, tmp_list))
2654 				continue;
2655 			remaining++;
2656 			set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
2657 			pnfs_lseg_cancel_io(server, lseg);
2658 		}
2659 
2660 	if (remaining) {
2661 		pnfs_set_plh_return_info(lo, return_range->iomode, seq);
2662 		return -EBUSY;
2663 	}
2664 
2665 	if (!list_empty(&lo->plh_return_segs)) {
2666 		pnfs_set_plh_return_info(lo, return_range->iomode, seq);
2667 		return 0;
2668 	}
2669 
2670 	return -ENOENT;
2671 }
2672 
2673 static void
pnfs_mark_layout_for_return(struct inode * inode,const struct pnfs_layout_range * range)2674 pnfs_mark_layout_for_return(struct inode *inode,
2675 			    const struct pnfs_layout_range *range)
2676 {
2677 	struct pnfs_layout_hdr *lo;
2678 	bool return_now = false;
2679 
2680 	spin_lock(&inode->i_lock);
2681 	lo = NFS_I(inode)->layout;
2682 	if (!pnfs_layout_is_valid(lo)) {
2683 		spin_unlock(&inode->i_lock);
2684 		return;
2685 	}
2686 	pnfs_set_plh_return_info(lo, range->iomode, 0);
2687 	/*
2688 	 * mark all matching lsegs so that we are sure to have no live
2689 	 * segments at hand when sending layoutreturn. See pnfs_put_lseg()
2690 	 * for how it works.
2691 	 */
2692 	if (pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs, range, 0) != -EBUSY) {
2693 		const struct cred *cred;
2694 		nfs4_stateid stateid;
2695 		enum pnfs_iomode iomode;
2696 
2697 		return_now = pnfs_prepare_layoutreturn(lo, &stateid, &cred, &iomode);
2698 		spin_unlock(&inode->i_lock);
2699 		if (return_now)
2700 			pnfs_send_layoutreturn(lo, &stateid, &cred, iomode,
2701 					       PNFS_FL_LAYOUTRETURN_ASYNC);
2702 	} else {
2703 		spin_unlock(&inode->i_lock);
2704 		nfs_commit_inode(inode, 0);
2705 	}
2706 }
2707 
pnfs_error_mark_layout_for_return(struct inode * inode,struct pnfs_layout_segment * lseg)2708 void pnfs_error_mark_layout_for_return(struct inode *inode,
2709 				       struct pnfs_layout_segment *lseg)
2710 {
2711 	struct pnfs_layout_range range = {
2712 		.iomode = lseg->pls_range.iomode,
2713 		.offset = 0,
2714 		.length = NFS4_MAX_UINT64,
2715 	};
2716 
2717 	pnfs_mark_layout_for_return(inode, &range);
2718 }
2719 EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return);
2720 
2721 static bool
pnfs_layout_can_be_returned(struct pnfs_layout_hdr * lo)2722 pnfs_layout_can_be_returned(struct pnfs_layout_hdr *lo)
2723 {
2724 	return pnfs_layout_is_valid(lo) &&
2725 		!test_bit(NFS_LAYOUT_INODE_FREEING, &lo->plh_flags) &&
2726 		!test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags);
2727 }
2728 
2729 static struct pnfs_layout_segment *
pnfs_find_first_lseg(struct pnfs_layout_hdr * lo,const struct pnfs_layout_range * range,enum pnfs_iomode iomode)2730 pnfs_find_first_lseg(struct pnfs_layout_hdr *lo,
2731 		     const struct pnfs_layout_range *range,
2732 		     enum pnfs_iomode iomode)
2733 {
2734 	struct pnfs_layout_segment *lseg;
2735 
2736 	list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
2737 		if (!test_bit(NFS_LSEG_VALID, &lseg->pls_flags))
2738 			continue;
2739 		if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
2740 			continue;
2741 		if (lseg->pls_range.iomode != iomode && iomode != IOMODE_ANY)
2742 			continue;
2743 		if (pnfs_lseg_range_intersecting(&lseg->pls_range, range))
2744 			return lseg;
2745 	}
2746 	return NULL;
2747 }
2748 
2749 /* Find open file states whose mode matches that of the range */
2750 static bool
pnfs_should_return_unused_layout(struct pnfs_layout_hdr * lo,const struct pnfs_layout_range * range)2751 pnfs_should_return_unused_layout(struct pnfs_layout_hdr *lo,
2752 				 const struct pnfs_layout_range *range)
2753 {
2754 	struct list_head *head;
2755 	struct nfs_open_context *ctx;
2756 	fmode_t mode = 0;
2757 
2758 	if (!pnfs_layout_can_be_returned(lo) ||
2759 	    !pnfs_find_first_lseg(lo, range, range->iomode))
2760 		return false;
2761 
2762 	head = &NFS_I(lo->plh_inode)->open_files;
2763 	list_for_each_entry_rcu(ctx, head, list) {
2764 		if (ctx->state)
2765 			mode |= ctx->state->state & (FMODE_READ|FMODE_WRITE);
2766 	}
2767 
2768 	switch (range->iomode) {
2769 	default:
2770 		break;
2771 	case IOMODE_READ:
2772 		mode &= ~FMODE_WRITE;
2773 		break;
2774 	case IOMODE_RW:
2775 		if (pnfs_find_first_lseg(lo, range, IOMODE_READ))
2776 			mode &= ~FMODE_READ;
2777 	}
2778 	return mode == 0;
2779 }
2780 
pnfs_layout_return_unused_byserver(struct nfs_server * server,void * data)2781 static int pnfs_layout_return_unused_byserver(struct nfs_server *server,
2782 					      void *data)
2783 {
2784 	const struct pnfs_layout_range *range = data;
2785 	const struct cred *cred;
2786 	struct pnfs_layout_hdr *lo;
2787 	struct inode *inode;
2788 	nfs4_stateid stateid;
2789 	enum pnfs_iomode iomode;
2790 
2791 restart:
2792 	rcu_read_lock();
2793 	list_for_each_entry_rcu(lo, &server->layouts, plh_layouts) {
2794 		inode = lo->plh_inode;
2795 		if (!inode || !pnfs_layout_can_be_returned(lo) ||
2796 		    test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
2797 			continue;
2798 		spin_lock(&inode->i_lock);
2799 		if (!lo->plh_inode ||
2800 		    !pnfs_should_return_unused_layout(lo, range)) {
2801 			spin_unlock(&inode->i_lock);
2802 			continue;
2803 		}
2804 		pnfs_get_layout_hdr(lo);
2805 		pnfs_set_plh_return_info(lo, range->iomode, 0);
2806 		if (pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs,
2807 						    range, 0) != 0 ||
2808 		    !pnfs_prepare_layoutreturn(lo, &stateid, &cred, &iomode)) {
2809 			spin_unlock(&inode->i_lock);
2810 			rcu_read_unlock();
2811 			pnfs_put_layout_hdr(lo);
2812 			cond_resched();
2813 			goto restart;
2814 		}
2815 		spin_unlock(&inode->i_lock);
2816 		rcu_read_unlock();
2817 		pnfs_send_layoutreturn(lo, &stateid, &cred, iomode,
2818 				       PNFS_FL_LAYOUTRETURN_ASYNC);
2819 		pnfs_put_layout_hdr(lo);
2820 		cond_resched();
2821 		goto restart;
2822 	}
2823 	rcu_read_unlock();
2824 	return 0;
2825 }
2826 
2827 void
pnfs_layout_return_unused_byclid(struct nfs_client * clp,enum pnfs_iomode iomode)2828 pnfs_layout_return_unused_byclid(struct nfs_client *clp,
2829 				 enum pnfs_iomode iomode)
2830 {
2831 	struct pnfs_layout_range range = {
2832 		.iomode = iomode,
2833 		.offset = 0,
2834 		.length = NFS4_MAX_UINT64,
2835 	};
2836 
2837 	nfs_client_for_each_server(clp, pnfs_layout_return_unused_byserver,
2838 			&range);
2839 }
2840 
2841 /* Check if we have we have a valid layout but if there isn't an intersection
2842  * between the request and the pgio->pg_lseg, put this pgio->pg_lseg away.
2843  */
2844 void
pnfs_generic_pg_check_layout(struct nfs_pageio_descriptor * pgio,struct nfs_page * req)2845 pnfs_generic_pg_check_layout(struct nfs_pageio_descriptor *pgio,
2846 			     struct nfs_page *req)
2847 {
2848 	if (pgio->pg_lseg == NULL ||
2849 	    (test_bit(NFS_LSEG_VALID, &pgio->pg_lseg->pls_flags) &&
2850 	    pnfs_lseg_request_intersecting(pgio->pg_lseg, req)))
2851 		return;
2852 	pnfs_put_lseg(pgio->pg_lseg);
2853 	pgio->pg_lseg = NULL;
2854 }
2855 EXPORT_SYMBOL_GPL(pnfs_generic_pg_check_layout);
2856 
2857 void
pnfs_generic_pg_init_read(struct nfs_pageio_descriptor * pgio,struct nfs_page * req)2858 pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
2859 {
2860 	u64 rd_size;
2861 
2862 	pnfs_generic_pg_check_layout(pgio, req);
2863 	if (pgio->pg_lseg == NULL) {
2864 		if (pgio->pg_dreq == NULL)
2865 			rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
2866 		else
2867 			rd_size = nfs_dreq_bytes_left(pgio->pg_dreq,
2868 						      req_offset(req));
2869 
2870 		pgio->pg_lseg =
2871 			pnfs_update_layout(pgio->pg_inode, nfs_req_openctx(req),
2872 					   req_offset(req), rd_size,
2873 					   IOMODE_READ, false,
2874 					   nfs_io_gfp_mask());
2875 		if (IS_ERR(pgio->pg_lseg)) {
2876 			pgio->pg_error = PTR_ERR(pgio->pg_lseg);
2877 			pgio->pg_lseg = NULL;
2878 			return;
2879 		}
2880 	}
2881 	/* If no lseg, fall back to read through mds */
2882 	if (pgio->pg_lseg == NULL)
2883 		nfs_pageio_reset_read_mds(pgio);
2884 
2885 }
2886 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
2887 
2888 void
pnfs_generic_pg_init_write(struct nfs_pageio_descriptor * pgio,struct nfs_page * req,u64 wb_size)2889 pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
2890 			   struct nfs_page *req, u64 wb_size)
2891 {
2892 	pnfs_generic_pg_check_layout(pgio, req);
2893 	if (pgio->pg_lseg == NULL) {
2894 		pgio->pg_lseg =
2895 			pnfs_update_layout(pgio->pg_inode, nfs_req_openctx(req),
2896 					   req_offset(req), wb_size, IOMODE_RW,
2897 					   false, nfs_io_gfp_mask());
2898 		if (IS_ERR(pgio->pg_lseg)) {
2899 			pgio->pg_error = PTR_ERR(pgio->pg_lseg);
2900 			pgio->pg_lseg = NULL;
2901 			return;
2902 		}
2903 	}
2904 	/* If no lseg, fall back to write through mds */
2905 	if (pgio->pg_lseg == NULL)
2906 		nfs_pageio_reset_write_mds(pgio);
2907 }
2908 EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
2909 
2910 void
pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor * desc)2911 pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc)
2912 {
2913 	if (desc->pg_lseg) {
2914 		pnfs_put_lseg(desc->pg_lseg);
2915 		desc->pg_lseg = NULL;
2916 	}
2917 }
2918 EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup);
2919 
2920 /*
2921  * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
2922  * of bytes (maximum @req->wb_bytes) that can be coalesced.
2923  */
2924 size_t
pnfs_generic_pg_test(struct nfs_pageio_descriptor * pgio,struct nfs_page * prev,struct nfs_page * req)2925 pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio,
2926 		     struct nfs_page *prev, struct nfs_page *req)
2927 {
2928 	unsigned int size;
2929 	u64 seg_end, req_start, seg_left;
2930 
2931 	size = nfs_generic_pg_test(pgio, prev, req);
2932 	if (!size)
2933 		return 0;
2934 
2935 	/*
2936 	 * 'size' contains the number of bytes left in the current page (up
2937 	 * to the original size asked for in @req->wb_bytes).
2938 	 *
2939 	 * Calculate how many bytes are left in the layout segment
2940 	 * and if there are less bytes than 'size', return that instead.
2941 	 *
2942 	 * Please also note that 'end_offset' is actually the offset of the
2943 	 * first byte that lies outside the pnfs_layout_range. FIXME?
2944 	 *
2945 	 */
2946 	if (pgio->pg_lseg) {
2947 		seg_end = pnfs_end_offset(pgio->pg_lseg->pls_range.offset,
2948 				     pgio->pg_lseg->pls_range.length);
2949 		req_start = req_offset(req);
2950 
2951 		/* start of request is past the last byte of this segment */
2952 		if (req_start >= seg_end)
2953 			return 0;
2954 
2955 		/* adjust 'size' iff there are fewer bytes left in the
2956 		 * segment than what nfs_generic_pg_test returned */
2957 		seg_left = seg_end - req_start;
2958 		if (seg_left < size)
2959 			size = (unsigned int)seg_left;
2960 	}
2961 
2962 	return size;
2963 }
2964 EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
2965 
pnfs_write_done_resend_to_mds(struct nfs_pgio_header * hdr)2966 int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr)
2967 {
2968 	struct nfs_pageio_descriptor pgio;
2969 
2970 	/* Resend all requests through the MDS */
2971 	nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true,
2972 			      hdr->completion_ops);
2973 	return nfs_pageio_resend(&pgio, hdr);
2974 }
2975 EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
2976 
pnfs_ld_handle_write_error(struct nfs_pgio_header * hdr)2977 static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr)
2978 {
2979 
2980 	dprintk("pnfs write error = %d\n", hdr->pnfs_error);
2981 	if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
2982 	    PNFS_LAYOUTRET_ON_ERROR) {
2983 		pnfs_return_layout(hdr->inode);
2984 	}
2985 	if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
2986 		hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr);
2987 }
2988 
2989 /*
2990  * Called by non rpc-based layout drivers
2991  */
pnfs_ld_write_done(struct nfs_pgio_header * hdr)2992 void pnfs_ld_write_done(struct nfs_pgio_header *hdr)
2993 {
2994 	if (likely(!hdr->pnfs_error)) {
2995 		pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
2996 				hdr->mds_offset + hdr->res.count);
2997 		hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
2998 	}
2999 	trace_nfs4_pnfs_write(hdr, hdr->pnfs_error);
3000 	if (unlikely(hdr->pnfs_error))
3001 		pnfs_ld_handle_write_error(hdr);
3002 	hdr->mds_ops->rpc_release(hdr);
3003 }
3004 EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
3005 
3006 static void
pnfs_write_through_mds(struct nfs_pageio_descriptor * desc,struct nfs_pgio_header * hdr)3007 pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
3008 		struct nfs_pgio_header *hdr)
3009 {
3010 	struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
3011 
3012 	if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
3013 		list_splice_tail_init(&hdr->pages, &mirror->pg_list);
3014 		nfs_pageio_reset_write_mds(desc);
3015 		mirror->pg_recoalesce = 1;
3016 	}
3017 	hdr->completion_ops->completion(hdr);
3018 }
3019 
3020 static enum pnfs_try_status
pnfs_try_to_write_data(struct nfs_pgio_header * hdr,const struct rpc_call_ops * call_ops,struct pnfs_layout_segment * lseg,int how)3021 pnfs_try_to_write_data(struct nfs_pgio_header *hdr,
3022 			const struct rpc_call_ops *call_ops,
3023 			struct pnfs_layout_segment *lseg,
3024 			int how)
3025 {
3026 	struct inode *inode = hdr->inode;
3027 	enum pnfs_try_status trypnfs;
3028 	struct nfs_server *nfss = NFS_SERVER(inode);
3029 
3030 	hdr->mds_ops = call_ops;
3031 
3032 	dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
3033 		inode->i_ino, hdr->args.count, hdr->args.offset, how);
3034 	trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how);
3035 	if (trypnfs != PNFS_NOT_ATTEMPTED)
3036 		nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
3037 	dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
3038 	return trypnfs;
3039 }
3040 
3041 static void
pnfs_do_write(struct nfs_pageio_descriptor * desc,struct nfs_pgio_header * hdr,int how)3042 pnfs_do_write(struct nfs_pageio_descriptor *desc,
3043 	      struct nfs_pgio_header *hdr, int how)
3044 {
3045 	const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
3046 	struct pnfs_layout_segment *lseg = desc->pg_lseg;
3047 	enum pnfs_try_status trypnfs;
3048 
3049 	trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how);
3050 	switch (trypnfs) {
3051 	case PNFS_NOT_ATTEMPTED:
3052 		pnfs_write_through_mds(desc, hdr);
3053 		break;
3054 	case PNFS_ATTEMPTED:
3055 		break;
3056 	case PNFS_TRY_AGAIN:
3057 		/* cleanup hdr and prepare to redo pnfs */
3058 		if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
3059 			struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
3060 			list_splice_init(&hdr->pages, &mirror->pg_list);
3061 			mirror->pg_recoalesce = 1;
3062 		}
3063 		hdr->mds_ops->rpc_release(hdr);
3064 	}
3065 }
3066 
pnfs_writehdr_free(struct nfs_pgio_header * hdr)3067 static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
3068 {
3069 	pnfs_put_lseg(hdr->lseg);
3070 	nfs_pgio_header_free(hdr);
3071 }
3072 
3073 int
pnfs_generic_pg_writepages(struct nfs_pageio_descriptor * desc)3074 pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
3075 {
3076 	struct nfs_pgio_header *hdr;
3077 	int ret;
3078 
3079 	hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
3080 	if (!hdr) {
3081 		desc->pg_error = -ENOMEM;
3082 		return desc->pg_error;
3083 	}
3084 	nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
3085 
3086 	hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
3087 	ret = nfs_generic_pgio(desc, hdr);
3088 	if (!ret)
3089 		pnfs_do_write(desc, hdr, desc->pg_ioflags);
3090 
3091 	return ret;
3092 }
3093 EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
3094 
pnfs_read_done_resend_to_mds(struct nfs_pgio_header * hdr)3095 int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr)
3096 {
3097 	struct nfs_pageio_descriptor pgio;
3098 
3099 	/* Resend all requests through the MDS */
3100 	nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops);
3101 	return nfs_pageio_resend(&pgio, hdr);
3102 }
3103 EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
3104 
pnfs_ld_handle_read_error(struct nfs_pgio_header * hdr)3105 static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr)
3106 {
3107 	dprintk("pnfs read error = %d\n", hdr->pnfs_error);
3108 	if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
3109 	    PNFS_LAYOUTRET_ON_ERROR) {
3110 		pnfs_return_layout(hdr->inode);
3111 	}
3112 	if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
3113 		hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr);
3114 }
3115 
3116 /*
3117  * Called by non rpc-based layout drivers
3118  */
pnfs_ld_read_done(struct nfs_pgio_header * hdr)3119 void pnfs_ld_read_done(struct nfs_pgio_header *hdr)
3120 {
3121 	if (likely(!hdr->pnfs_error))
3122 		hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
3123 	trace_nfs4_pnfs_read(hdr, hdr->pnfs_error);
3124 	if (unlikely(hdr->pnfs_error))
3125 		pnfs_ld_handle_read_error(hdr);
3126 	hdr->mds_ops->rpc_release(hdr);
3127 }
3128 EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
3129 
3130 static void
pnfs_read_through_mds(struct nfs_pageio_descriptor * desc,struct nfs_pgio_header * hdr)3131 pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
3132 		struct nfs_pgio_header *hdr)
3133 {
3134 	struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
3135 
3136 	if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
3137 		list_splice_tail_init(&hdr->pages, &mirror->pg_list);
3138 		nfs_pageio_reset_read_mds(desc);
3139 		mirror->pg_recoalesce = 1;
3140 	}
3141 	hdr->completion_ops->completion(hdr);
3142 }
3143 
3144 /*
3145  * Call the appropriate parallel I/O subsystem read function.
3146  */
3147 static enum pnfs_try_status
pnfs_try_to_read_data(struct nfs_pgio_header * hdr,const struct rpc_call_ops * call_ops,struct pnfs_layout_segment * lseg)3148 pnfs_try_to_read_data(struct nfs_pgio_header *hdr,
3149 		       const struct rpc_call_ops *call_ops,
3150 		       struct pnfs_layout_segment *lseg)
3151 {
3152 	struct inode *inode = hdr->inode;
3153 	struct nfs_server *nfss = NFS_SERVER(inode);
3154 	enum pnfs_try_status trypnfs;
3155 
3156 	hdr->mds_ops = call_ops;
3157 
3158 	dprintk("%s: Reading ino:%lu %u@%llu\n",
3159 		__func__, inode->i_ino, hdr->args.count, hdr->args.offset);
3160 
3161 	trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr);
3162 	if (trypnfs != PNFS_NOT_ATTEMPTED)
3163 		nfs_inc_stats(inode, NFSIOS_PNFS_READ);
3164 	dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
3165 	return trypnfs;
3166 }
3167 
3168 /* Resend all requests through pnfs. */
pnfs_read_resend_pnfs(struct nfs_pgio_header * hdr,unsigned int mirror_idx)3169 void pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr,
3170 			   unsigned int mirror_idx)
3171 {
3172 	struct nfs_pageio_descriptor pgio;
3173 
3174 	if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
3175 		/* Prevent deadlocks with layoutreturn! */
3176 		pnfs_put_lseg(hdr->lseg);
3177 		hdr->lseg = NULL;
3178 
3179 		nfs_pageio_init_read(&pgio, hdr->inode, false,
3180 					hdr->completion_ops);
3181 		pgio.pg_mirror_idx = mirror_idx;
3182 		hdr->task.tk_status = nfs_pageio_resend(&pgio, hdr);
3183 	}
3184 }
3185 EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs);
3186 
3187 static void
pnfs_do_read(struct nfs_pageio_descriptor * desc,struct nfs_pgio_header * hdr)3188 pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr)
3189 {
3190 	const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
3191 	struct pnfs_layout_segment *lseg = desc->pg_lseg;
3192 	enum pnfs_try_status trypnfs;
3193 
3194 	trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg);
3195 	switch (trypnfs) {
3196 	case PNFS_NOT_ATTEMPTED:
3197 		pnfs_read_through_mds(desc, hdr);
3198 		break;
3199 	case PNFS_ATTEMPTED:
3200 		break;
3201 	case PNFS_TRY_AGAIN:
3202 		/* cleanup hdr and prepare to redo pnfs */
3203 		if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
3204 			struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
3205 			list_splice_init(&hdr->pages, &mirror->pg_list);
3206 			mirror->pg_recoalesce = 1;
3207 		}
3208 		hdr->mds_ops->rpc_release(hdr);
3209 	}
3210 }
3211 
pnfs_readhdr_free(struct nfs_pgio_header * hdr)3212 static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
3213 {
3214 	pnfs_put_lseg(hdr->lseg);
3215 	nfs_pgio_header_free(hdr);
3216 }
3217 
3218 int
pnfs_generic_pg_readpages(struct nfs_pageio_descriptor * desc)3219 pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
3220 {
3221 	struct nfs_pgio_header *hdr;
3222 	int ret;
3223 
3224 	hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
3225 	if (!hdr) {
3226 		desc->pg_error = -ENOMEM;
3227 		return desc->pg_error;
3228 	}
3229 	nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
3230 	hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
3231 	ret = nfs_generic_pgio(desc, hdr);
3232 	if (!ret)
3233 		pnfs_do_read(desc, hdr);
3234 	return ret;
3235 }
3236 EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
3237 
pnfs_clear_layoutcommitting(struct inode * inode)3238 static void pnfs_clear_layoutcommitting(struct inode *inode)
3239 {
3240 	unsigned long *bitlock = &NFS_I(inode)->flags;
3241 
3242 	clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
3243 	smp_mb__after_atomic();
3244 	wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
3245 }
3246 
3247 /*
3248  * There can be multiple RW segments.
3249  */
pnfs_list_write_lseg(struct inode * inode,struct list_head * listp)3250 static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
3251 {
3252 	struct pnfs_layout_segment *lseg;
3253 
3254 	list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
3255 		if (lseg->pls_range.iomode == IOMODE_RW &&
3256 		    test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
3257 			list_add(&lseg->pls_lc_list, listp);
3258 	}
3259 }
3260 
pnfs_list_write_lseg_done(struct inode * inode,struct list_head * listp)3261 static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp)
3262 {
3263 	struct pnfs_layout_segment *lseg, *tmp;
3264 
3265 	/* Matched by references in pnfs_set_layoutcommit */
3266 	list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) {
3267 		list_del_init(&lseg->pls_lc_list);
3268 		pnfs_put_lseg(lseg);
3269 	}
3270 
3271 	pnfs_clear_layoutcommitting(inode);
3272 }
3273 
pnfs_set_lo_fail(struct pnfs_layout_segment * lseg)3274 void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
3275 {
3276 	pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
3277 }
3278 EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
3279 
3280 void
pnfs_set_layoutcommit(struct inode * inode,struct pnfs_layout_segment * lseg,loff_t end_pos)3281 pnfs_set_layoutcommit(struct inode *inode, struct pnfs_layout_segment *lseg,
3282 		loff_t end_pos)
3283 {
3284 	struct nfs_inode *nfsi = NFS_I(inode);
3285 	bool mark_as_dirty = false;
3286 
3287 	spin_lock(&inode->i_lock);
3288 	if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
3289 		nfsi->layout->plh_lwb = end_pos;
3290 		mark_as_dirty = true;
3291 		dprintk("%s: Set layoutcommit for inode %lu ",
3292 			__func__, inode->i_ino);
3293 	} else if (end_pos > nfsi->layout->plh_lwb)
3294 		nfsi->layout->plh_lwb = end_pos;
3295 	if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) {
3296 		/* references matched in nfs4_layoutcommit_release */
3297 		pnfs_get_lseg(lseg);
3298 	}
3299 	spin_unlock(&inode->i_lock);
3300 	dprintk("%s: lseg %p end_pos %llu\n",
3301 		__func__, lseg, nfsi->layout->plh_lwb);
3302 
3303 	/* if pnfs_layoutcommit_inode() runs between inode locks, the next one
3304 	 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
3305 	if (mark_as_dirty)
3306 		mark_inode_dirty_sync(inode);
3307 }
3308 EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
3309 
pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data * data)3310 void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
3311 {
3312 	struct nfs_server *nfss = NFS_SERVER(data->args.inode);
3313 
3314 	if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
3315 		nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
3316 	pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
3317 }
3318 
3319 /*
3320  * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
3321  * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
3322  * data to disk to allow the server to recover the data if it crashes.
3323  * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
3324  * is off, and a COMMIT is sent to a data server, or
3325  * if WRITEs to a data server return NFS_DATA_SYNC.
3326  */
3327 int
pnfs_layoutcommit_inode(struct inode * inode,bool sync)3328 pnfs_layoutcommit_inode(struct inode *inode, bool sync)
3329 {
3330 	struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
3331 	struct nfs4_layoutcommit_data *data;
3332 	struct nfs_inode *nfsi = NFS_I(inode);
3333 	loff_t end_pos;
3334 	int status;
3335 	bool mark_as_dirty = false;
3336 
3337 	if (!pnfs_layoutcommit_outstanding(inode))
3338 		return 0;
3339 
3340 	dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
3341 
3342 	status = -EAGAIN;
3343 	if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
3344 		if (!sync)
3345 			goto out;
3346 		status = wait_on_bit_lock_action(&nfsi->flags,
3347 				NFS_INO_LAYOUTCOMMITTING,
3348 				nfs_wait_bit_killable,
3349 				TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
3350 		if (status)
3351 			goto out;
3352 	}
3353 
3354 	status = -ENOMEM;
3355 	/* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
3356 	data = kzalloc(sizeof(*data), nfs_io_gfp_mask());
3357 	if (!data)
3358 		goto clear_layoutcommitting;
3359 
3360 	status = 0;
3361 	spin_lock(&inode->i_lock);
3362 	if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
3363 		goto out_unlock;
3364 
3365 	INIT_LIST_HEAD(&data->lseg_list);
3366 	pnfs_list_write_lseg(inode, &data->lseg_list);
3367 
3368 	end_pos = nfsi->layout->plh_lwb;
3369 
3370 	nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
3371 	data->cred = get_cred(nfsi->layout->plh_lc_cred);
3372 	spin_unlock(&inode->i_lock);
3373 
3374 	data->args.inode = inode;
3375 	nfs_fattr_init(&data->fattr);
3376 	data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3377 	data->res.fattr = &data->fattr;
3378 	if (end_pos != 0)
3379 		data->args.lastbytewritten = end_pos - 1;
3380 	else
3381 		data->args.lastbytewritten = U64_MAX;
3382 	data->res.server = NFS_SERVER(inode);
3383 
3384 	if (ld->prepare_layoutcommit) {
3385 		status = ld->prepare_layoutcommit(&data->args);
3386 		if (status) {
3387 			if (status != -ENOSPC)
3388 				put_cred(data->cred);
3389 			spin_lock(&inode->i_lock);
3390 			set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags);
3391 			if (end_pos > nfsi->layout->plh_lwb)
3392 				nfsi->layout->plh_lwb = end_pos;
3393 			if (status != -ENOSPC)
3394 				goto out_unlock;
3395 			spin_unlock(&inode->i_lock);
3396 			mark_as_dirty = true;
3397 		}
3398 	}
3399 
3400 
3401 	status = nfs4_proc_layoutcommit(data, sync);
3402 out:
3403 	if (status || mark_as_dirty)
3404 		mark_inode_dirty_sync(inode);
3405 	dprintk("<-- %s status %d\n", __func__, status);
3406 	return status;
3407 out_unlock:
3408 	spin_unlock(&inode->i_lock);
3409 	kfree(data);
3410 clear_layoutcommitting:
3411 	pnfs_clear_layoutcommitting(inode);
3412 	goto out;
3413 }
3414 EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode);
3415 
3416 int
pnfs_generic_sync(struct inode * inode,bool datasync)3417 pnfs_generic_sync(struct inode *inode, bool datasync)
3418 {
3419 	return pnfs_layoutcommit_inode(inode, true);
3420 }
3421 EXPORT_SYMBOL_GPL(pnfs_generic_sync);
3422 
pnfs_mdsthreshold_alloc(void)3423 struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
3424 {
3425 	struct nfs4_threshold *thp;
3426 
3427 	thp = kzalloc(sizeof(*thp), nfs_io_gfp_mask());
3428 	if (!thp) {
3429 		dprintk("%s mdsthreshold allocation failed\n", __func__);
3430 		return NULL;
3431 	}
3432 	return thp;
3433 }
3434 
3435 #if IS_ENABLED(CONFIG_NFS_V4_2)
3436 int
pnfs_report_layoutstat(struct inode * inode,gfp_t gfp_flags)3437 pnfs_report_layoutstat(struct inode *inode, gfp_t gfp_flags)
3438 {
3439 	struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
3440 	struct nfs_server *server = NFS_SERVER(inode);
3441 	struct nfs_inode *nfsi = NFS_I(inode);
3442 	struct nfs42_layoutstat_data *data;
3443 	struct pnfs_layout_hdr *hdr;
3444 	int status = 0;
3445 
3446 	if (!pnfs_enabled_sb(server) || !ld->prepare_layoutstats)
3447 		goto out;
3448 
3449 	if (!nfs_server_capable(inode, NFS_CAP_LAYOUTSTATS))
3450 		goto out;
3451 
3452 	if (test_and_set_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags))
3453 		goto out;
3454 
3455 	spin_lock(&inode->i_lock);
3456 	if (!NFS_I(inode)->layout) {
3457 		spin_unlock(&inode->i_lock);
3458 		goto out_clear_layoutstats;
3459 	}
3460 	hdr = NFS_I(inode)->layout;
3461 	pnfs_get_layout_hdr(hdr);
3462 	spin_unlock(&inode->i_lock);
3463 
3464 	data = kzalloc(sizeof(*data), gfp_flags);
3465 	if (!data) {
3466 		status = -ENOMEM;
3467 		goto out_put;
3468 	}
3469 
3470 	data->args.fh = NFS_FH(inode);
3471 	data->args.inode = inode;
3472 	status = ld->prepare_layoutstats(&data->args);
3473 	if (status)
3474 		goto out_free;
3475 
3476 	status = nfs42_proc_layoutstats_generic(NFS_SERVER(inode), data);
3477 
3478 out:
3479 	dprintk("%s returns %d\n", __func__, status);
3480 	return status;
3481 
3482 out_free:
3483 	kfree(data);
3484 out_put:
3485 	pnfs_put_layout_hdr(hdr);
3486 out_clear_layoutstats:
3487 	smp_mb__before_atomic();
3488 	clear_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags);
3489 	smp_mb__after_atomic();
3490 	goto out;
3491 }
3492 EXPORT_SYMBOL_GPL(pnfs_report_layoutstat);
3493 #endif
3494 
3495 unsigned int layoutstats_timer;
3496 module_param(layoutstats_timer, uint, 0644);
3497 EXPORT_SYMBOL_GPL(layoutstats_timer);
3498