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