1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
41#define NFSDBG_FACILITY NFSDBG_PNFS
42#define PNFS_LAYOUTGET_RETRY_TIMEOUT (120*HZ)
43
44
45
46
47
48
49static DEFINE_SPINLOCK(pnfs_spinlock);
50
51
52
53
54static LIST_HEAD(pnfs_modules_tbl);
55
56static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo);
57static void pnfs_free_returned_lsegs(struct pnfs_layout_hdr *lo,
58 struct list_head *free_me,
59 const struct pnfs_layout_range *range,
60 u32 seq);
61static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg,
62 struct list_head *tmp_list);
63
64
65static struct pnfs_layoutdriver_type *
66find_pnfs_driver_locked(u32 id)
67{
68 struct pnfs_layoutdriver_type *local;
69
70 list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
71 if (local->id == id)
72 goto out;
73 local = NULL;
74out:
75 dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
76 return local;
77}
78
79static struct pnfs_layoutdriver_type *
80find_pnfs_driver(u32 id)
81{
82 struct pnfs_layoutdriver_type *local;
83
84 spin_lock(&pnfs_spinlock);
85 local = find_pnfs_driver_locked(id);
86 if (local != NULL && !try_module_get(local->owner)) {
87 dprintk("%s: Could not grab reference on module\n", __func__);
88 local = NULL;
89 }
90 spin_unlock(&pnfs_spinlock);
91 return local;
92}
93
94void
95unset_pnfs_layoutdriver(struct nfs_server *nfss)
96{
97 if (nfss->pnfs_curr_ld) {
98 if (nfss->pnfs_curr_ld->clear_layoutdriver)
99 nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
100
101 if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count))
102 nfs4_deviceid_purge_client(nfss->nfs_client);
103 module_put(nfss->pnfs_curr_ld->owner);
104 }
105 nfss->pnfs_curr_ld = NULL;
106}
107
108
109
110
111
112
113
114
115static const u32 ld_prefs[] = {
116 LAYOUT_SCSI,
117 LAYOUT_BLOCK_VOLUME,
118 LAYOUT_OSD2_OBJECTS,
119 LAYOUT_FLEX_FILES,
120 LAYOUT_NFSV4_1_FILES,
121 0
122};
123
124static int
125ld_cmp(const void *e1, const void *e2)
126{
127 u32 ld1 = *((u32 *)e1);
128 u32 ld2 = *((u32 *)e2);
129 int i;
130
131 for (i = 0; ld_prefs[i] != 0; i++) {
132 if (ld1 == ld_prefs[i])
133 return -1;
134
135 if (ld2 == ld_prefs[i])
136 return 1;
137 }
138 return 0;
139}
140
141
142
143
144
145
146
147void
148set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
149 struct nfs_fsinfo *fsinfo)
150{
151 struct pnfs_layoutdriver_type *ld_type = NULL;
152 u32 id;
153 int i;
154
155 if (fsinfo->nlayouttypes == 0)
156 goto out_no_driver;
157 if (!(server->nfs_client->cl_exchange_flags &
158 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
159 printk(KERN_ERR "NFS: %s: cl_exchange_flags 0x%x\n",
160 __func__, server->nfs_client->cl_exchange_flags);
161 goto out_no_driver;
162 }
163
164 sort(fsinfo->layouttype, fsinfo->nlayouttypes,
165 sizeof(*fsinfo->layouttype), ld_cmp, NULL);
166
167 for (i = 0; i < fsinfo->nlayouttypes; i++) {
168 id = fsinfo->layouttype[i];
169 ld_type = find_pnfs_driver(id);
170 if (!ld_type) {
171 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX,
172 id);
173 ld_type = find_pnfs_driver(id);
174 }
175 if (ld_type)
176 break;
177 }
178
179 if (!ld_type) {
180 dprintk("%s: No pNFS module found!\n", __func__);
181 goto out_no_driver;
182 }
183
184 server->pnfs_curr_ld = ld_type;
185 if (ld_type->set_layoutdriver
186 && ld_type->set_layoutdriver(server, mntfh)) {
187 printk(KERN_ERR "NFS: %s: Error initializing pNFS layout "
188 "driver %u.\n", __func__, id);
189 module_put(ld_type->owner);
190 goto out_no_driver;
191 }
192
193 atomic_inc(&server->nfs_client->cl_mds_count);
194
195 dprintk("%s: pNFS module for %u set\n", __func__, id);
196 return;
197
198out_no_driver:
199 dprintk("%s: Using NFSv4 I/O\n", __func__);
200 server->pnfs_curr_ld = NULL;
201}
202
203int
204pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
205{
206 int status = -EINVAL;
207 struct pnfs_layoutdriver_type *tmp;
208
209 if (ld_type->id == 0) {
210 printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__);
211 return status;
212 }
213 if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
214 printk(KERN_ERR "NFS: %s Layout driver must provide "
215 "alloc_lseg and free_lseg.\n", __func__);
216 return status;
217 }
218
219 spin_lock(&pnfs_spinlock);
220 tmp = find_pnfs_driver_locked(ld_type->id);
221 if (!tmp) {
222 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
223 status = 0;
224 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
225 ld_type->name);
226 } else {
227 printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n",
228 __func__, ld_type->id);
229 }
230 spin_unlock(&pnfs_spinlock);
231
232 return status;
233}
234EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
235
236void
237pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
238{
239 dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
240 spin_lock(&pnfs_spinlock);
241 list_del(&ld_type->pnfs_tblid);
242 spin_unlock(&pnfs_spinlock);
243}
244EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
245
246
247
248
249
250
251void
252pnfs_get_layout_hdr(struct pnfs_layout_hdr *lo)
253{
254 atomic_inc(&lo->plh_refcount);
255}
256
257static struct pnfs_layout_hdr *
258pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
259{
260 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
261 return ld->alloc_layout_hdr(ino, gfp_flags);
262}
263
264static void
265pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
266{
267 struct nfs_server *server = NFS_SERVER(lo->plh_inode);
268 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
269
270 if (!list_empty(&lo->plh_layouts)) {
271 struct nfs_client *clp = server->nfs_client;
272
273 spin_lock(&clp->cl_lock);
274 list_del_init(&lo->plh_layouts);
275 spin_unlock(&clp->cl_lock);
276 }
277 put_rpccred(lo->plh_lc_cred);
278 return ld->free_layout_hdr(lo);
279}
280
281static void
282pnfs_detach_layout_hdr(struct pnfs_layout_hdr *lo)
283{
284 struct nfs_inode *nfsi = NFS_I(lo->plh_inode);
285 dprintk("%s: freeing layout cache %p\n", __func__, lo);
286 nfsi->layout = NULL;
287
288 nfsi->write_io = 0;
289 nfsi->read_io = 0;
290}
291
292void
293pnfs_put_layout_hdr(struct pnfs_layout_hdr *lo)
294{
295 struct inode *inode;
296
297 if (!lo)
298 return;
299 inode = lo->plh_inode;
300 pnfs_layoutreturn_before_put_layout_hdr(lo);
301
302 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
303 if (!list_empty(&lo->plh_segs))
304 WARN_ONCE(1, "NFS: BUG unfreed layout segments.\n");
305 pnfs_detach_layout_hdr(lo);
306 spin_unlock(&inode->i_lock);
307 pnfs_free_layout_hdr(lo);
308 }
309}
310
311static void
312pnfs_set_plh_return_info(struct pnfs_layout_hdr *lo, enum pnfs_iomode iomode,
313 u32 seq)
314{
315 if (lo->plh_return_iomode != 0 && lo->plh_return_iomode != iomode)
316 iomode = IOMODE_ANY;
317 lo->plh_return_iomode = iomode;
318 set_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
319 if (seq != 0) {
320 WARN_ON_ONCE(lo->plh_return_seq != 0 && lo->plh_return_seq != seq);
321 lo->plh_return_seq = seq;
322 }
323}
324
325static void
326pnfs_clear_layoutreturn_info(struct pnfs_layout_hdr *lo)
327{
328 struct pnfs_layout_segment *lseg;
329 lo->plh_return_iomode = 0;
330 lo->plh_return_seq = 0;
331 clear_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags);
332 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
333 if (!test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
334 continue;
335 pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
336 }
337}
338
339static void pnfs_clear_layoutreturn_waitbit(struct pnfs_layout_hdr *lo)
340{
341 clear_bit_unlock(NFS_LAYOUT_RETURN, &lo->plh_flags);
342 clear_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags);
343 smp_mb__after_atomic();
344 wake_up_bit(&lo->plh_flags, NFS_LAYOUT_RETURN);
345 rpc_wake_up(&NFS_SERVER(lo->plh_inode)->roc_rpcwaitq);
346}
347
348static void
349pnfs_clear_lseg_state(struct pnfs_layout_segment *lseg,
350 struct list_head *free_me)
351{
352 clear_bit(NFS_LSEG_ROC, &lseg->pls_flags);
353 clear_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
354 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags))
355 pnfs_lseg_dec_and_remove_zero(lseg, free_me);
356 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
357 pnfs_lseg_dec_and_remove_zero(lseg, free_me);
358}
359
360
361
362
363
364
365
366
367int
368pnfs_mark_layout_stateid_invalid(struct pnfs_layout_hdr *lo,
369 struct list_head *lseg_list)
370{
371 struct pnfs_layout_range range = {
372 .iomode = IOMODE_ANY,
373 .offset = 0,
374 .length = NFS4_MAX_UINT64,
375 };
376 struct pnfs_layout_segment *lseg, *next;
377
378 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
379 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
380 pnfs_clear_lseg_state(lseg, lseg_list);
381 pnfs_clear_layoutreturn_info(lo);
382 pnfs_free_returned_lsegs(lo, lseg_list, &range, 0);
383 if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags) &&
384 !test_and_set_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags))
385 pnfs_clear_layoutreturn_waitbit(lo);
386 return !list_empty(&lo->plh_segs);
387}
388
389static int
390pnfs_iomode_to_fail_bit(u32 iomode)
391{
392 return iomode == IOMODE_RW ?
393 NFS_LAYOUT_RW_FAILED : NFS_LAYOUT_RO_FAILED;
394}
395
396static void
397pnfs_layout_set_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
398{
399 lo->plh_retry_timestamp = jiffies;
400 if (!test_and_set_bit(fail_bit, &lo->plh_flags))
401 atomic_inc(&lo->plh_refcount);
402}
403
404static void
405pnfs_layout_clear_fail_bit(struct pnfs_layout_hdr *lo, int fail_bit)
406{
407 if (test_and_clear_bit(fail_bit, &lo->plh_flags))
408 atomic_dec(&lo->plh_refcount);
409}
410
411static void
412pnfs_layout_io_set_failed(struct pnfs_layout_hdr *lo, u32 iomode)
413{
414 struct inode *inode = lo->plh_inode;
415 struct pnfs_layout_range range = {
416 .iomode = iomode,
417 .offset = 0,
418 .length = NFS4_MAX_UINT64,
419 };
420 LIST_HEAD(head);
421
422 spin_lock(&inode->i_lock);
423 pnfs_layout_set_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
424 pnfs_mark_matching_lsegs_invalid(lo, &head, &range, 0);
425 spin_unlock(&inode->i_lock);
426 pnfs_free_lseg_list(&head);
427 dprintk("%s Setting layout IOMODE_%s fail bit\n", __func__,
428 iomode == IOMODE_RW ? "RW" : "READ");
429}
430
431static bool
432pnfs_layout_io_test_failed(struct pnfs_layout_hdr *lo, u32 iomode)
433{
434 unsigned long start, end;
435 int fail_bit = pnfs_iomode_to_fail_bit(iomode);
436
437 if (test_bit(fail_bit, &lo->plh_flags) == 0)
438 return false;
439 end = jiffies;
440 start = end - PNFS_LAYOUTGET_RETRY_TIMEOUT;
441 if (!time_in_range(lo->plh_retry_timestamp, start, end)) {
442
443 pnfs_layout_clear_fail_bit(lo, fail_bit);
444 return false;
445 }
446 return true;
447}
448
449static void
450pnfs_init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg,
451 const struct pnfs_layout_range *range,
452 const nfs4_stateid *stateid)
453{
454 INIT_LIST_HEAD(&lseg->pls_list);
455 INIT_LIST_HEAD(&lseg->pls_lc_list);
456 atomic_set(&lseg->pls_refcount, 1);
457 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
458 lseg->pls_layout = lo;
459 lseg->pls_range = *range;
460 lseg->pls_seq = be32_to_cpu(stateid->seqid);
461}
462
463static void pnfs_free_lseg(struct pnfs_layout_segment *lseg)
464{
465 if (lseg != NULL) {
466 struct inode *inode = lseg->pls_layout->plh_inode;
467 NFS_SERVER(inode)->pnfs_curr_ld->free_lseg(lseg);
468 }
469}
470
471static void
472pnfs_layout_remove_lseg(struct pnfs_layout_hdr *lo,
473 struct pnfs_layout_segment *lseg)
474{
475 WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
476 list_del_init(&lseg->pls_list);
477
478 atomic_dec(&lo->plh_refcount);
479 if (test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags))
480 return;
481 if (list_empty(&lo->plh_segs) &&
482 !test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags) &&
483 !test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
484 if (atomic_read(&lo->plh_outstanding) == 0)
485 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
486 clear_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
487 }
488}
489
490static bool
491pnfs_cache_lseg_for_layoutreturn(struct pnfs_layout_hdr *lo,
492 struct pnfs_layout_segment *lseg)
493{
494 if (test_and_clear_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) &&
495 pnfs_layout_is_valid(lo)) {
496 pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
497 list_move_tail(&lseg->pls_list, &lo->plh_return_segs);
498 return true;
499 }
500 return false;
501}
502
503void
504pnfs_put_lseg(struct pnfs_layout_segment *lseg)
505{
506 struct pnfs_layout_hdr *lo;
507 struct inode *inode;
508
509 if (!lseg)
510 return;
511
512 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
513 atomic_read(&lseg->pls_refcount),
514 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
515
516 lo = lseg->pls_layout;
517 inode = lo->plh_inode;
518
519 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
520 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
521 spin_unlock(&inode->i_lock);
522 return;
523 }
524 pnfs_get_layout_hdr(lo);
525 pnfs_layout_remove_lseg(lo, lseg);
526 if (pnfs_cache_lseg_for_layoutreturn(lo, lseg))
527 lseg = NULL;
528 spin_unlock(&inode->i_lock);
529 pnfs_free_lseg(lseg);
530 pnfs_put_layout_hdr(lo);
531 }
532}
533EXPORT_SYMBOL_GPL(pnfs_put_lseg);
534
535static void pnfs_free_lseg_async_work(struct work_struct *work)
536{
537 struct pnfs_layout_segment *lseg;
538 struct pnfs_layout_hdr *lo;
539
540 lseg = container_of(work, struct pnfs_layout_segment, pls_work);
541 lo = lseg->pls_layout;
542
543 pnfs_free_lseg(lseg);
544 pnfs_put_layout_hdr(lo);
545}
546
547static void pnfs_free_lseg_async(struct pnfs_layout_segment *lseg)
548{
549 INIT_WORK(&lseg->pls_work, pnfs_free_lseg_async_work);
550 schedule_work(&lseg->pls_work);
551}
552
553void
554pnfs_put_lseg_locked(struct pnfs_layout_segment *lseg)
555{
556 if (!lseg)
557 return;
558
559 assert_spin_locked(&lseg->pls_layout->plh_inode->i_lock);
560
561 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
562 atomic_read(&lseg->pls_refcount),
563 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
564 if (atomic_dec_and_test(&lseg->pls_refcount)) {
565 struct pnfs_layout_hdr *lo = lseg->pls_layout;
566 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags))
567 return;
568 pnfs_layout_remove_lseg(lo, lseg);
569 if (!pnfs_cache_lseg_for_layoutreturn(lo, lseg)) {
570 pnfs_get_layout_hdr(lo);
571 pnfs_free_lseg_async(lseg);
572 }
573 }
574}
575EXPORT_SYMBOL_GPL(pnfs_put_lseg_locked);
576
577
578
579
580
581
582
583
584static bool
585pnfs_lseg_range_contained(const struct pnfs_layout_range *l1,
586 const struct pnfs_layout_range *l2)
587{
588 u64 start1 = l1->offset;
589 u64 end1 = pnfs_end_offset(start1, l1->length);
590 u64 start2 = l2->offset;
591 u64 end2 = pnfs_end_offset(start2, l2->length);
592
593 return (start1 <= start2) && (end1 >= end2);
594}
595
596static bool pnfs_lseg_dec_and_remove_zero(struct pnfs_layout_segment *lseg,
597 struct list_head *tmp_list)
598{
599 if (!atomic_dec_and_test(&lseg->pls_refcount))
600 return false;
601 pnfs_layout_remove_lseg(lseg->pls_layout, lseg);
602 list_add(&lseg->pls_list, tmp_list);
603 return true;
604}
605
606
607static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
608 struct list_head *tmp_list)
609{
610 int rv = 0;
611
612 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
613
614
615
616
617 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
618 atomic_read(&lseg->pls_refcount));
619 if (pnfs_lseg_dec_and_remove_zero(lseg, tmp_list))
620 rv = 1;
621 }
622 return rv;
623}
624
625
626
627
628
629static bool pnfs_seqid_is_newer(u32 s1, u32 s2)
630{
631 return (s32)(s1 - s2) > 0;
632}
633
634static bool
635pnfs_should_free_range(const struct pnfs_layout_range *lseg_range,
636 const struct pnfs_layout_range *recall_range)
637{
638 return (recall_range->iomode == IOMODE_ANY ||
639 lseg_range->iomode == recall_range->iomode) &&
640 pnfs_lseg_range_intersecting(lseg_range, recall_range);
641}
642
643static bool
644pnfs_match_lseg_recall(const struct pnfs_layout_segment *lseg,
645 const struct pnfs_layout_range *recall_range,
646 u32 seq)
647{
648 if (seq != 0 && pnfs_seqid_is_newer(lseg->pls_seq, seq))
649 return false;
650 if (recall_range == NULL)
651 return true;
652 return pnfs_should_free_range(&lseg->pls_range, recall_range);
653}
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670int
671pnfs_mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
672 struct list_head *tmp_list,
673 const struct pnfs_layout_range *recall_range,
674 u32 seq)
675{
676 struct pnfs_layout_segment *lseg, *next;
677 int remaining = 0;
678
679 dprintk("%s:Begin lo %p\n", __func__, lo);
680
681 if (list_empty(&lo->plh_segs))
682 return 0;
683 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
684 if (pnfs_match_lseg_recall(lseg, recall_range, seq)) {
685 dprintk("%s: freeing lseg %p iomode %d seq %u "
686 "offset %llu length %llu\n", __func__,
687 lseg, lseg->pls_range.iomode, lseg->pls_seq,
688 lseg->pls_range.offset, lseg->pls_range.length);
689 if (!mark_lseg_invalid(lseg, tmp_list))
690 remaining++;
691 }
692 dprintk("%s:Return %i\n", __func__, remaining);
693 return remaining;
694}
695
696static void
697pnfs_free_returned_lsegs(struct pnfs_layout_hdr *lo,
698 struct list_head *free_me,
699 const struct pnfs_layout_range *range,
700 u32 seq)
701{
702 struct pnfs_layout_segment *lseg, *next;
703
704 list_for_each_entry_safe(lseg, next, &lo->plh_return_segs, pls_list) {
705 if (pnfs_match_lseg_recall(lseg, range, seq))
706 list_move_tail(&lseg->pls_list, free_me);
707 }
708}
709
710
711void
712pnfs_free_lseg_list(struct list_head *free_me)
713{
714 struct pnfs_layout_segment *lseg, *tmp;
715
716 if (list_empty(free_me))
717 return;
718
719 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
720 list_del(&lseg->pls_list);
721 pnfs_free_lseg(lseg);
722 }
723}
724
725void
726pnfs_destroy_layout(struct nfs_inode *nfsi)
727{
728 struct pnfs_layout_hdr *lo;
729 LIST_HEAD(tmp_list);
730
731 spin_lock(&nfsi->vfs_inode.i_lock);
732 lo = nfsi->layout;
733 if (lo) {
734 pnfs_get_layout_hdr(lo);
735 pnfs_mark_layout_stateid_invalid(lo, &tmp_list);
736 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RO_FAILED);
737 pnfs_layout_clear_fail_bit(lo, NFS_LAYOUT_RW_FAILED);
738 spin_unlock(&nfsi->vfs_inode.i_lock);
739 pnfs_free_lseg_list(&tmp_list);
740 nfs_commit_inode(&nfsi->vfs_inode, 0);
741 pnfs_put_layout_hdr(lo);
742 } else
743 spin_unlock(&nfsi->vfs_inode.i_lock);
744}
745EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
746
747static bool
748pnfs_layout_add_bulk_destroy_list(struct inode *inode,
749 struct list_head *layout_list)
750{
751 struct pnfs_layout_hdr *lo;
752 bool ret = false;
753
754 spin_lock(&inode->i_lock);
755 lo = NFS_I(inode)->layout;
756 if (lo != NULL && list_empty(&lo->plh_bulk_destroy)) {
757 pnfs_get_layout_hdr(lo);
758 list_add(&lo->plh_bulk_destroy, layout_list);
759 ret = true;
760 }
761 spin_unlock(&inode->i_lock);
762 return ret;
763}
764
765
766static int
767pnfs_layout_bulk_destroy_byserver_locked(struct nfs_client *clp,
768 struct nfs_server *server,
769 struct list_head *layout_list)
770{
771 struct pnfs_layout_hdr *lo, *next;
772 struct inode *inode;
773
774 list_for_each_entry_safe(lo, next, &server->layouts, plh_layouts) {
775 inode = igrab(lo->plh_inode);
776 if (inode == NULL)
777 continue;
778 list_del_init(&lo->plh_layouts);
779 if (pnfs_layout_add_bulk_destroy_list(inode, layout_list))
780 continue;
781 rcu_read_unlock();
782 spin_unlock(&clp->cl_lock);
783 iput(inode);
784 spin_lock(&clp->cl_lock);
785 rcu_read_lock();
786 return -EAGAIN;
787 }
788 return 0;
789}
790
791static int
792pnfs_layout_free_bulk_destroy_list(struct list_head *layout_list,
793 bool is_bulk_recall)
794{
795 struct pnfs_layout_hdr *lo;
796 struct inode *inode;
797 LIST_HEAD(lseg_list);
798 int ret = 0;
799
800 while (!list_empty(layout_list)) {
801 lo = list_entry(layout_list->next, struct pnfs_layout_hdr,
802 plh_bulk_destroy);
803 dprintk("%s freeing layout for inode %lu\n", __func__,
804 lo->plh_inode->i_ino);
805 inode = lo->plh_inode;
806
807 pnfs_layoutcommit_inode(inode, false);
808
809 spin_lock(&inode->i_lock);
810 list_del_init(&lo->plh_bulk_destroy);
811 if (pnfs_mark_layout_stateid_invalid(lo, &lseg_list)) {
812 if (is_bulk_recall)
813 set_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
814 ret = -EAGAIN;
815 }
816 spin_unlock(&inode->i_lock);
817 pnfs_free_lseg_list(&lseg_list);
818
819 nfs_commit_inode(inode, 0);
820 pnfs_put_layout_hdr(lo);
821 iput(inode);
822 }
823 return ret;
824}
825
826int
827pnfs_destroy_layouts_byfsid(struct nfs_client *clp,
828 struct nfs_fsid *fsid,
829 bool is_recall)
830{
831 struct nfs_server *server;
832 LIST_HEAD(layout_list);
833
834 spin_lock(&clp->cl_lock);
835 rcu_read_lock();
836restart:
837 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
838 if (memcmp(&server->fsid, fsid, sizeof(*fsid)) != 0)
839 continue;
840 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
841 server,
842 &layout_list) != 0)
843 goto restart;
844 }
845 rcu_read_unlock();
846 spin_unlock(&clp->cl_lock);
847
848 if (list_empty(&layout_list))
849 return 0;
850 return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
851}
852
853int
854pnfs_destroy_layouts_byclid(struct nfs_client *clp,
855 bool is_recall)
856{
857 struct nfs_server *server;
858 LIST_HEAD(layout_list);
859
860 spin_lock(&clp->cl_lock);
861 rcu_read_lock();
862restart:
863 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
864 if (pnfs_layout_bulk_destroy_byserver_locked(clp,
865 server,
866 &layout_list) != 0)
867 goto restart;
868 }
869 rcu_read_unlock();
870 spin_unlock(&clp->cl_lock);
871
872 if (list_empty(&layout_list))
873 return 0;
874 return pnfs_layout_free_bulk_destroy_list(&layout_list, is_recall);
875}
876
877
878
879
880
881void
882pnfs_destroy_all_layouts(struct nfs_client *clp)
883{
884 nfs4_deviceid_mark_client_invalid(clp);
885 nfs4_deviceid_purge_client(clp);
886
887 pnfs_destroy_layouts_byclid(clp, false);
888}
889
890
891void
892pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
893 bool update_barrier)
894{
895 u32 oldseq, newseq, new_barrier = 0;
896
897 oldseq = be32_to_cpu(lo->plh_stateid.seqid);
898 newseq = be32_to_cpu(new->seqid);
899
900 if (!pnfs_layout_is_valid(lo)) {
901 nfs4_stateid_copy(&lo->plh_stateid, new);
902 lo->plh_barrier = newseq;
903 pnfs_clear_layoutreturn_info(lo);
904 clear_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
905 return;
906 }
907 if (pnfs_seqid_is_newer(newseq, oldseq)) {
908 nfs4_stateid_copy(&lo->plh_stateid, new);
909
910
911
912
913 new_barrier = newseq - atomic_read(&lo->plh_outstanding);
914 }
915 if (update_barrier)
916 new_barrier = be32_to_cpu(new->seqid);
917 else if (new_barrier == 0)
918 return;
919 if (pnfs_seqid_is_newer(new_barrier, lo->plh_barrier))
920 lo->plh_barrier = new_barrier;
921}
922
923static bool
924pnfs_layout_stateid_blocked(const struct pnfs_layout_hdr *lo,
925 const nfs4_stateid *stateid)
926{
927 u32 seqid = be32_to_cpu(stateid->seqid);
928
929 return !pnfs_seqid_is_newer(seqid, lo->plh_barrier);
930}
931
932
933static bool
934pnfs_layoutgets_blocked(const struct pnfs_layout_hdr *lo)
935{
936 return lo->plh_block_lgets ||
937 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags);
938}
939
940
941
942
943
944
945
946static struct pnfs_layout_segment *
947send_layoutget(struct pnfs_layout_hdr *lo,
948 struct nfs_open_context *ctx,
949 nfs4_stateid *stateid,
950 const struct pnfs_layout_range *range,
951 long *timeout, gfp_t gfp_flags)
952{
953 struct inode *ino = lo->plh_inode;
954 struct nfs_server *server = NFS_SERVER(ino);
955 struct nfs4_layoutget *lgp;
956 loff_t i_size;
957
958 dprintk("--> %s\n", __func__);
959
960
961
962
963
964
965 lgp = kzalloc(sizeof(*lgp), gfp_flags);
966 if (lgp == NULL)
967 return ERR_PTR(-ENOMEM);
968
969 i_size = i_size_read(ino);
970
971 lgp->args.minlength = PAGE_CACHE_SIZE;
972 if (lgp->args.minlength > range->length)
973 lgp->args.minlength = range->length;
974 if (range->iomode == IOMODE_READ) {
975 if (range->offset >= i_size)
976 lgp->args.minlength = 0;
977 else if (i_size - range->offset < lgp->args.minlength)
978 lgp->args.minlength = i_size - range->offset;
979 }
980 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
981 pnfs_copy_range(&lgp->args.range, range);
982 lgp->args.type = server->pnfs_curr_ld->id;
983 lgp->args.inode = ino;
984 lgp->args.ctx = get_nfs_open_context(ctx);
985 nfs4_stateid_copy(&lgp->args.stateid, stateid);
986 lgp->gfp_flags = gfp_flags;
987 lgp->cred = lo->plh_lc_cred;
988
989 return nfs4_proc_layoutget(lgp, timeout, gfp_flags);
990}
991
992static void pnfs_clear_layoutcommit(struct inode *inode,
993 struct list_head *head)
994{
995 struct nfs_inode *nfsi = NFS_I(inode);
996 struct pnfs_layout_segment *lseg, *tmp;
997
998 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
999 return;
1000 list_for_each_entry_safe(lseg, tmp, &nfsi->layout->plh_segs, pls_list) {
1001 if (!test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
1002 continue;
1003 pnfs_lseg_dec_and_remove_zero(lseg, head);
1004 }
1005}
1006
1007void pnfs_layoutreturn_free_lsegs(struct pnfs_layout_hdr *lo,
1008 const nfs4_stateid *arg_stateid,
1009 const struct pnfs_layout_range *range,
1010 const nfs4_stateid *stateid)
1011{
1012 struct inode *inode = lo->plh_inode;
1013 LIST_HEAD(freeme);
1014
1015 spin_lock(&inode->i_lock);
1016 if (!pnfs_layout_is_valid(lo) || !arg_stateid ||
1017 !nfs4_stateid_match_other(&lo->plh_stateid, arg_stateid))
1018 goto out_unlock;
1019 if (stateid) {
1020 u32 seq = be32_to_cpu(arg_stateid->seqid);
1021
1022 pnfs_mark_matching_lsegs_invalid(lo, &freeme, range, seq);
1023 pnfs_free_returned_lsegs(lo, &freeme, range, seq);
1024 pnfs_set_layout_stateid(lo, stateid, true);
1025 } else
1026 pnfs_mark_layout_stateid_invalid(lo, &freeme);
1027out_unlock:
1028 pnfs_clear_layoutreturn_waitbit(lo);
1029 spin_unlock(&inode->i_lock);
1030 pnfs_free_lseg_list(&freeme);
1031
1032}
1033
1034static bool
1035pnfs_prepare_layoutreturn(struct pnfs_layout_hdr *lo,
1036 nfs4_stateid *stateid,
1037 enum pnfs_iomode *iomode)
1038{
1039
1040 if (atomic_read(&lo->plh_outstanding) != 0)
1041 return false;
1042 if (test_and_set_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags))
1043 return false;
1044 set_bit(NFS_LAYOUT_RETURN, &lo->plh_flags);
1045 pnfs_get_layout_hdr(lo);
1046 if (test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags)) {
1047 if (stateid != NULL) {
1048 nfs4_stateid_copy(stateid, &lo->plh_stateid);
1049 if (lo->plh_return_seq != 0)
1050 stateid->seqid = cpu_to_be32(lo->plh_return_seq);
1051 }
1052 if (iomode != NULL)
1053 *iomode = lo->plh_return_iomode;
1054 pnfs_clear_layoutreturn_info(lo);
1055 return true;
1056 }
1057 if (stateid != NULL)
1058 nfs4_stateid_copy(stateid, &lo->plh_stateid);
1059 if (iomode != NULL)
1060 *iomode = IOMODE_ANY;
1061 return true;
1062}
1063
1064static void
1065pnfs_init_layoutreturn_args(struct nfs4_layoutreturn_args *args,
1066 struct pnfs_layout_hdr *lo,
1067 const nfs4_stateid *stateid,
1068 enum pnfs_iomode iomode)
1069{
1070 struct inode *inode = lo->plh_inode;
1071
1072 args->layout_type = NFS_SERVER(inode)->pnfs_curr_ld->id;
1073 args->inode = inode;
1074 args->range.iomode = iomode;
1075 args->range.offset = 0;
1076 args->range.length = NFS4_MAX_UINT64;
1077 args->layout = lo;
1078 nfs4_stateid_copy(&args->stateid, stateid);
1079}
1080
1081static int
1082pnfs_send_layoutreturn(struct pnfs_layout_hdr *lo, const nfs4_stateid *stateid,
1083 enum pnfs_iomode iomode, bool sync)
1084{
1085 struct inode *ino = lo->plh_inode;
1086 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
1087 struct nfs4_layoutreturn *lrp;
1088 int status = 0;
1089
1090 lrp = kzalloc(sizeof(*lrp), GFP_NOFS);
1091 if (unlikely(lrp == NULL)) {
1092 status = -ENOMEM;
1093 spin_lock(&ino->i_lock);
1094 pnfs_clear_layoutreturn_waitbit(lo);
1095 spin_unlock(&ino->i_lock);
1096 pnfs_put_layout_hdr(lo);
1097 goto out;
1098 }
1099
1100 pnfs_init_layoutreturn_args(&lrp->args, lo, stateid, iomode);
1101 lrp->args.ld_private = &lrp->ld_private;
1102 lrp->clp = NFS_SERVER(ino)->nfs_client;
1103 lrp->cred = lo->plh_lc_cred;
1104 if (ld->prepare_layoutreturn)
1105 ld->prepare_layoutreturn(&lrp->args);
1106
1107 status = nfs4_proc_layoutreturn(lrp, sync);
1108out:
1109 dprintk("<-- %s status: %d\n", __func__, status);
1110 return status;
1111}
1112
1113
1114static bool
1115pnfs_layout_need_return(struct pnfs_layout_hdr *lo)
1116{
1117 struct pnfs_layout_segment *s;
1118
1119 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
1120 return false;
1121
1122
1123 list_for_each_entry(s, &lo->plh_segs, pls_list) {
1124 if (test_bit(NFS_LSEG_LAYOUTRETURN, &s->pls_flags))
1125 return false;
1126 }
1127
1128 return true;
1129}
1130
1131static void pnfs_layoutreturn_before_put_layout_hdr(struct pnfs_layout_hdr *lo)
1132{
1133 struct inode *inode= lo->plh_inode;
1134
1135 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
1136 return;
1137 spin_lock(&inode->i_lock);
1138 if (pnfs_layout_need_return(lo)) {
1139 nfs4_stateid stateid;
1140 enum pnfs_iomode iomode;
1141 bool send;
1142
1143 send = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
1144 spin_unlock(&inode->i_lock);
1145 if (send) {
1146
1147 pnfs_send_layoutreturn(lo, &stateid, iomode, false);
1148 }
1149 } else
1150 spin_unlock(&inode->i_lock);
1151}
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161int
1162_pnfs_return_layout(struct inode *ino)
1163{
1164 struct pnfs_layout_hdr *lo = NULL;
1165 struct nfs_inode *nfsi = NFS_I(ino);
1166 LIST_HEAD(tmp_list);
1167 nfs4_stateid stateid;
1168 int status = 0;
1169 bool send, valid_layout;
1170
1171 dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
1172
1173 spin_lock(&ino->i_lock);
1174 lo = nfsi->layout;
1175 if (!lo) {
1176 spin_unlock(&ino->i_lock);
1177 dprintk("NFS: %s no layout to return\n", __func__);
1178 goto out;
1179 }
1180
1181 pnfs_get_layout_hdr(lo);
1182
1183 if (test_bit(NFS_LAYOUT_RETURN_LOCK, &lo->plh_flags)) {
1184 spin_unlock(&ino->i_lock);
1185 if (wait_on_bit(&lo->plh_flags, NFS_LAYOUT_RETURN,
1186 TASK_UNINTERRUPTIBLE))
1187 goto out_put_layout_hdr;
1188 spin_lock(&ino->i_lock);
1189 }
1190 valid_layout = pnfs_layout_is_valid(lo);
1191 pnfs_clear_layoutcommit(ino, &tmp_list);
1192 pnfs_mark_matching_lsegs_invalid(lo, &tmp_list, NULL, 0);
1193
1194 if (NFS_SERVER(ino)->pnfs_curr_ld->return_range) {
1195 struct pnfs_layout_range range = {
1196 .iomode = IOMODE_ANY,
1197 .offset = 0,
1198 .length = NFS4_MAX_UINT64,
1199 };
1200 NFS_SERVER(ino)->pnfs_curr_ld->return_range(lo, &range);
1201 }
1202
1203
1204 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags) ||
1205 !valid_layout) {
1206 spin_unlock(&ino->i_lock);
1207 dprintk("NFS: %s no layout segments to return\n", __func__);
1208 goto out_put_layout_hdr;
1209 }
1210
1211 send = pnfs_prepare_layoutreturn(lo, &stateid, NULL);
1212 spin_unlock(&ino->i_lock);
1213 if (send)
1214 status = pnfs_send_layoutreturn(lo, &stateid, IOMODE_ANY, true);
1215out_put_layout_hdr:
1216 pnfs_free_lseg_list(&tmp_list);
1217 pnfs_put_layout_hdr(lo);
1218out:
1219 dprintk("<-- %s status: %d\n", __func__, status);
1220 return status;
1221}
1222EXPORT_SYMBOL_GPL(_pnfs_return_layout);
1223
1224int
1225pnfs_commit_and_return_layout(struct inode *inode)
1226{
1227 struct pnfs_layout_hdr *lo;
1228 int ret;
1229
1230 spin_lock(&inode->i_lock);
1231 lo = NFS_I(inode)->layout;
1232 if (lo == NULL) {
1233 spin_unlock(&inode->i_lock);
1234 return 0;
1235 }
1236 pnfs_get_layout_hdr(lo);
1237
1238 lo->plh_block_lgets++;
1239 spin_unlock(&inode->i_lock);
1240 filemap_fdatawait(inode->i_mapping);
1241 ret = pnfs_layoutcommit_inode(inode, true);
1242 if (ret == 0)
1243 ret = _pnfs_return_layout(inode);
1244 spin_lock(&inode->i_lock);
1245 lo->plh_block_lgets--;
1246 spin_unlock(&inode->i_lock);
1247 pnfs_put_layout_hdr(lo);
1248 return ret;
1249}
1250
1251bool pnfs_roc(struct inode *ino,
1252 struct nfs4_layoutreturn_args *args,
1253 struct nfs4_layoutreturn_res *res,
1254 const struct rpc_cred *cred)
1255{
1256 struct nfs_inode *nfsi = NFS_I(ino);
1257 struct nfs_open_context *ctx;
1258 struct nfs4_state *state;
1259 struct pnfs_layout_hdr *lo;
1260 struct pnfs_layout_segment *lseg, *next;
1261 nfs4_stateid stateid;
1262 enum pnfs_iomode iomode = 0;
1263 bool layoutreturn = false, roc = false;
1264
1265 if (!nfs_have_layout(ino))
1266 return false;
1267 spin_lock(&ino->i_lock);
1268 lo = nfsi->layout;
1269 if (!lo || !pnfs_layout_is_valid(lo) ||
1270 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1271 lo = NULL;
1272 goto out_noroc;
1273 }
1274 pnfs_get_layout_hdr(lo);
1275
1276
1277 if (nfs4_check_delegation(ino, FMODE_READ))
1278 goto out_noroc;
1279
1280 list_for_each_entry(ctx, &nfsi->open_files, list) {
1281 state = ctx->state;
1282
1283 if (state != NULL && state->state != 0)
1284 goto out_noroc;
1285 }
1286
1287
1288 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list) {
1289
1290 if (!test_and_clear_bit(NFS_LSEG_ROC, &lseg->pls_flags))
1291 continue;
1292
1293
1294
1295
1296 set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
1297 if (!mark_lseg_invalid(lseg, &lo->plh_return_segs))
1298 continue;
1299 pnfs_set_plh_return_info(lo, lseg->pls_range.iomode, 0);
1300 }
1301
1302 if (!test_bit(NFS_LAYOUT_RETURN_REQUESTED, &lo->plh_flags))
1303 goto out_noroc;
1304
1305
1306
1307
1308
1309
1310 layoutreturn = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
1311
1312 if (!layoutreturn || cred != lo->plh_lc_cred)
1313 goto out_noroc;
1314
1315 roc = layoutreturn;
1316 pnfs_init_layoutreturn_args(args, lo, &stateid, iomode);
1317 res->lrs_present = 0;
1318 layoutreturn = false;
1319
1320out_noroc:
1321 spin_unlock(&ino->i_lock);
1322 pnfs_layoutcommit_inode(ino, true);
1323 if (roc) {
1324 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
1325 if (ld->prepare_layoutreturn)
1326 ld->prepare_layoutreturn(args);
1327 pnfs_put_layout_hdr(lo);
1328 return true;
1329 }
1330 if (layoutreturn)
1331 pnfs_send_layoutreturn(lo, &stateid, iomode, true);
1332 pnfs_put_layout_hdr(lo);
1333 return false;
1334}
1335
1336void pnfs_roc_release(struct nfs4_layoutreturn_args *args,
1337 struct nfs4_layoutreturn_res *res,
1338 int ret)
1339{
1340 struct pnfs_layout_hdr *lo = args->layout;
1341 const nfs4_stateid *arg_stateid = NULL;
1342 const nfs4_stateid *res_stateid = NULL;
1343 struct nfs4_xdr_opaque_data *ld_private = args->ld_private;
1344
1345 if (ret == 0) {
1346 arg_stateid = &args->stateid;
1347 if (res->lrs_present)
1348 res_stateid = &res->stateid;
1349 }
1350 pnfs_layoutreturn_free_lsegs(lo, arg_stateid, &args->range,
1351 res_stateid);
1352 if (ld_private && ld_private->ops && ld_private->ops->free)
1353 ld_private->ops->free(ld_private);
1354 pnfs_put_layout_hdr(lo);
1355 trace_nfs4_layoutreturn_on_close(args->inode, 0);
1356}
1357
1358bool pnfs_wait_on_layoutreturn(struct inode *ino, struct rpc_task *task)
1359{
1360 struct nfs_inode *nfsi = NFS_I(ino);
1361 struct pnfs_layout_hdr *lo;
1362 bool sleep = false;
1363
1364
1365
1366 spin_lock(&ino->i_lock);
1367 lo = nfsi->layout;
1368 if (lo && test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
1369 rpc_sleep_on(&NFS_SERVER(ino)->roc_rpcwaitq, task, NULL);
1370 sleep = true;
1371 }
1372 spin_unlock(&ino->i_lock);
1373 return sleep;
1374}
1375
1376
1377
1378
1379
1380
1381static s64
1382pnfs_lseg_range_cmp(const struct pnfs_layout_range *l1,
1383 const struct pnfs_layout_range *l2)
1384{
1385 s64 d;
1386
1387
1388 d = l1->offset - l2->offset;
1389 if (d)
1390 return d;
1391
1392
1393 d = l2->length - l1->length;
1394 if (d)
1395 return d;
1396
1397
1398 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
1399}
1400
1401static bool
1402pnfs_lseg_range_is_after(const struct pnfs_layout_range *l1,
1403 const struct pnfs_layout_range *l2)
1404{
1405 return pnfs_lseg_range_cmp(l1, l2) > 0;
1406}
1407
1408static bool
1409pnfs_lseg_no_merge(struct pnfs_layout_segment *lseg,
1410 struct pnfs_layout_segment *old)
1411{
1412 return false;
1413}
1414
1415void
1416pnfs_generic_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1417 struct pnfs_layout_segment *lseg,
1418 bool (*is_after)(const struct pnfs_layout_range *,
1419 const struct pnfs_layout_range *),
1420 bool (*do_merge)(struct pnfs_layout_segment *,
1421 struct pnfs_layout_segment *),
1422 struct list_head *free_me)
1423{
1424 struct pnfs_layout_segment *lp, *tmp;
1425
1426 dprintk("%s:Begin\n", __func__);
1427
1428 list_for_each_entry_safe(lp, tmp, &lo->plh_segs, pls_list) {
1429 if (test_bit(NFS_LSEG_VALID, &lp->pls_flags) == 0)
1430 continue;
1431 if (do_merge(lseg, lp)) {
1432 mark_lseg_invalid(lp, free_me);
1433 continue;
1434 }
1435 if (is_after(&lseg->pls_range, &lp->pls_range))
1436 continue;
1437 list_add_tail(&lseg->pls_list, &lp->pls_list);
1438 dprintk("%s: inserted lseg %p "
1439 "iomode %d offset %llu length %llu before "
1440 "lp %p iomode %d offset %llu length %llu\n",
1441 __func__, lseg, lseg->pls_range.iomode,
1442 lseg->pls_range.offset, lseg->pls_range.length,
1443 lp, lp->pls_range.iomode, lp->pls_range.offset,
1444 lp->pls_range.length);
1445 goto out;
1446 }
1447 list_add_tail(&lseg->pls_list, &lo->plh_segs);
1448 dprintk("%s: inserted lseg %p "
1449 "iomode %d offset %llu length %llu at tail\n",
1450 __func__, lseg, lseg->pls_range.iomode,
1451 lseg->pls_range.offset, lseg->pls_range.length);
1452out:
1453 pnfs_get_layout_hdr(lo);
1454
1455 dprintk("%s:Return\n", __func__);
1456}
1457EXPORT_SYMBOL_GPL(pnfs_generic_layout_insert_lseg);
1458
1459static void
1460pnfs_layout_insert_lseg(struct pnfs_layout_hdr *lo,
1461 struct pnfs_layout_segment *lseg,
1462 struct list_head *free_me)
1463{
1464 struct inode *inode = lo->plh_inode;
1465 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
1466
1467 if (ld->add_lseg != NULL)
1468 ld->add_lseg(lo, lseg, free_me);
1469 else
1470 pnfs_generic_layout_insert_lseg(lo, lseg,
1471 pnfs_lseg_range_is_after,
1472 pnfs_lseg_no_merge,
1473 free_me);
1474}
1475
1476static struct pnfs_layout_hdr *
1477alloc_init_layout_hdr(struct inode *ino,
1478 struct nfs_open_context *ctx,
1479 gfp_t gfp_flags)
1480{
1481 struct pnfs_layout_hdr *lo;
1482
1483 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
1484 if (!lo)
1485 return NULL;
1486 atomic_set(&lo->plh_refcount, 1);
1487 INIT_LIST_HEAD(&lo->plh_layouts);
1488 INIT_LIST_HEAD(&lo->plh_segs);
1489 INIT_LIST_HEAD(&lo->plh_return_segs);
1490 INIT_LIST_HEAD(&lo->plh_bulk_destroy);
1491 lo->plh_inode = ino;
1492 lo->plh_lc_cred = get_rpccred(ctx->cred);
1493 lo->plh_flags |= 1 << NFS_LAYOUT_INVALID_STID;
1494 return lo;
1495}
1496
1497static struct pnfs_layout_hdr *
1498pnfs_find_alloc_layout(struct inode *ino,
1499 struct nfs_open_context *ctx,
1500 gfp_t gfp_flags)
1501 __releases(&ino->i_lock)
1502 __acquires(&ino->i_lock)
1503{
1504 struct nfs_inode *nfsi = NFS_I(ino);
1505 struct pnfs_layout_hdr *new = NULL;
1506
1507 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
1508
1509 if (nfsi->layout != NULL)
1510 goto out_existing;
1511 spin_unlock(&ino->i_lock);
1512 new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
1513 spin_lock(&ino->i_lock);
1514
1515 if (likely(nfsi->layout == NULL)) {
1516 nfsi->layout = new;
1517 return new;
1518 } else if (new != NULL)
1519 pnfs_free_layout_hdr(new);
1520out_existing:
1521 pnfs_get_layout_hdr(nfsi->layout);
1522 return nfsi->layout;
1523}
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538static bool
1539pnfs_lseg_range_match(const struct pnfs_layout_range *ls_range,
1540 const struct pnfs_layout_range *range,
1541 bool strict_iomode)
1542{
1543 struct pnfs_layout_range range1;
1544
1545 if ((range->iomode == IOMODE_RW &&
1546 ls_range->iomode != IOMODE_RW) ||
1547 (range->iomode != ls_range->iomode &&
1548 strict_iomode == true) ||
1549 !pnfs_lseg_range_intersecting(ls_range, range))
1550 return 0;
1551
1552
1553 range1 = *range;
1554 range1.length = 1;
1555 return pnfs_lseg_range_contained(ls_range, &range1);
1556}
1557
1558
1559
1560
1561static struct pnfs_layout_segment *
1562pnfs_find_lseg(struct pnfs_layout_hdr *lo,
1563 struct pnfs_layout_range *range,
1564 bool strict_iomode)
1565{
1566 struct pnfs_layout_segment *lseg, *ret = NULL;
1567
1568 dprintk("%s:Begin\n", __func__);
1569
1570 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
1571 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
1572 !test_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags) &&
1573 pnfs_lseg_range_match(&lseg->pls_range, range,
1574 strict_iomode)) {
1575 ret = pnfs_get_lseg(lseg);
1576 break;
1577 }
1578 }
1579
1580 dprintk("%s:Return lseg %p ref %d\n",
1581 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
1582 return ret;
1583}
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
1605 struct inode *ino, int iomode)
1606{
1607 struct nfs4_threshold *t = ctx->mdsthreshold;
1608 struct nfs_inode *nfsi = NFS_I(ino);
1609 loff_t fsize = i_size_read(ino);
1610 bool size = false, size_set = false, io = false, io_set = false, ret = false;
1611
1612 if (t == NULL)
1613 return ret;
1614
1615 dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
1616 __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
1617
1618 switch (iomode) {
1619 case IOMODE_READ:
1620 if (t->bm & THRESHOLD_RD) {
1621 dprintk("%s fsize %llu\n", __func__, fsize);
1622 size_set = true;
1623 if (fsize < t->rd_sz)
1624 size = true;
1625 }
1626 if (t->bm & THRESHOLD_RD_IO) {
1627 dprintk("%s nfsi->read_io %llu\n", __func__,
1628 nfsi->read_io);
1629 io_set = true;
1630 if (nfsi->read_io < t->rd_io_sz)
1631 io = true;
1632 }
1633 break;
1634 case IOMODE_RW:
1635 if (t->bm & THRESHOLD_WR) {
1636 dprintk("%s fsize %llu\n", __func__, fsize);
1637 size_set = true;
1638 if (fsize < t->wr_sz)
1639 size = true;
1640 }
1641 if (t->bm & THRESHOLD_WR_IO) {
1642 dprintk("%s nfsi->write_io %llu\n", __func__,
1643 nfsi->write_io);
1644 io_set = true;
1645 if (nfsi->write_io < t->wr_io_sz)
1646 io = true;
1647 }
1648 break;
1649 }
1650 if (size_set && io_set) {
1651 if (size && io)
1652 ret = true;
1653 } else if (size || io)
1654 ret = true;
1655
1656 dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
1657 return ret;
1658}
1659
1660static bool pnfs_prepare_to_retry_layoutget(struct pnfs_layout_hdr *lo)
1661{
1662
1663
1664
1665
1666 pnfs_layoutcommit_inode(lo->plh_inode, false);
1667 return !wait_on_bit_action(&lo->plh_flags, NFS_LAYOUT_RETURN,
1668 nfs_wait_bit_killable,
1669 TASK_UNINTERRUPTIBLE);
1670}
1671
1672static void pnfs_clear_first_layoutget(struct pnfs_layout_hdr *lo)
1673{
1674 unsigned long *bitlock = &lo->plh_flags;
1675
1676 clear_bit_unlock(NFS_LAYOUT_FIRST_LAYOUTGET, bitlock);
1677 smp_mb__after_atomic();
1678 wake_up_bit(bitlock, NFS_LAYOUT_FIRST_LAYOUTGET);
1679}
1680
1681
1682
1683
1684
1685struct pnfs_layout_segment *
1686pnfs_update_layout(struct inode *ino,
1687 struct nfs_open_context *ctx,
1688 loff_t pos,
1689 u64 count,
1690 enum pnfs_iomode iomode,
1691 bool strict_iomode,
1692 gfp_t gfp_flags)
1693{
1694 struct pnfs_layout_range arg = {
1695 .iomode = iomode,
1696 .offset = pos,
1697 .length = count,
1698 };
1699 unsigned pg_offset;
1700 struct nfs_server *server = NFS_SERVER(ino);
1701 struct nfs_client *clp = server->nfs_client;
1702 struct pnfs_layout_hdr *lo = NULL;
1703 struct pnfs_layout_segment *lseg = NULL;
1704 nfs4_stateid stateid;
1705 long timeout = 0;
1706 unsigned long giveup = jiffies + (clp->cl_lease_time << 1);
1707 bool first;
1708
1709 if (!pnfs_enabled_sb(NFS_SERVER(ino))) {
1710 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1711 PNFS_UPDATE_LAYOUT_NO_PNFS);
1712 goto out;
1713 }
1714
1715 if (iomode == IOMODE_READ && i_size_read(ino) == 0) {
1716 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1717 PNFS_UPDATE_LAYOUT_RD_ZEROLEN);
1718 goto out;
1719 }
1720
1721 if (pnfs_within_mdsthreshold(ctx, ino, iomode)) {
1722 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1723 PNFS_UPDATE_LAYOUT_MDSTHRESH);
1724 goto out;
1725 }
1726
1727lookup_again:
1728 nfs4_client_recover_expired_lease(clp);
1729 first = false;
1730 spin_lock(&ino->i_lock);
1731 lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
1732 if (lo == NULL) {
1733 spin_unlock(&ino->i_lock);
1734 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1735 PNFS_UPDATE_LAYOUT_NOMEM);
1736 goto out;
1737 }
1738
1739
1740 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1741 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1742 PNFS_UPDATE_LAYOUT_BULK_RECALL);
1743 dprintk("%s matches recall, use MDS\n", __func__);
1744 goto out_unlock;
1745 }
1746
1747
1748 if (pnfs_layout_io_test_failed(lo, iomode)) {
1749 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1750 PNFS_UPDATE_LAYOUT_IO_TEST_FAIL);
1751 goto out_unlock;
1752 }
1753
1754 lseg = pnfs_find_lseg(lo, &arg, strict_iomode);
1755 if (lseg) {
1756 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1757 PNFS_UPDATE_LAYOUT_FOUND_CACHED);
1758 goto out_unlock;
1759 }
1760
1761 if (!nfs4_valid_open_stateid(ctx->state)) {
1762 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1763 PNFS_UPDATE_LAYOUT_INVALID_OPEN);
1764 goto out_unlock;
1765 }
1766
1767
1768
1769
1770
1771
1772 if (test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags)) {
1773
1774
1775
1776
1777
1778 if (test_and_set_bit(NFS_LAYOUT_FIRST_LAYOUTGET,
1779 &lo->plh_flags)) {
1780 spin_unlock(&ino->i_lock);
1781 wait_on_bit(&lo->plh_flags, NFS_LAYOUT_FIRST_LAYOUTGET,
1782 TASK_UNINTERRUPTIBLE);
1783 pnfs_put_layout_hdr(lo);
1784 dprintk("%s retrying\n", __func__);
1785 goto lookup_again;
1786 }
1787
1788 first = true;
1789 if (nfs4_select_rw_stateid(ctx->state,
1790 iomode == IOMODE_RW ? FMODE_WRITE : FMODE_READ,
1791 NULL, &stateid, NULL) != 0) {
1792 trace_pnfs_update_layout(ino, pos, count,
1793 iomode, lo, lseg,
1794 PNFS_UPDATE_LAYOUT_INVALID_OPEN);
1795 goto out_unlock;
1796 }
1797 } else {
1798 nfs4_stateid_copy(&stateid, &lo->plh_stateid);
1799 }
1800
1801
1802
1803
1804
1805 if (test_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) {
1806 spin_unlock(&ino->i_lock);
1807 dprintk("%s wait for layoutreturn\n", __func__);
1808 if (pnfs_prepare_to_retry_layoutget(lo)) {
1809 if (first)
1810 pnfs_clear_first_layoutget(lo);
1811 pnfs_put_layout_hdr(lo);
1812 dprintk("%s retrying\n", __func__);
1813 trace_pnfs_update_layout(ino, pos, count, iomode, lo,
1814 lseg, PNFS_UPDATE_LAYOUT_RETRY);
1815 goto lookup_again;
1816 }
1817 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1818 PNFS_UPDATE_LAYOUT_RETURN);
1819 goto out_put_layout_hdr;
1820 }
1821
1822 if (pnfs_layoutgets_blocked(lo)) {
1823 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1824 PNFS_UPDATE_LAYOUT_BLOCKED);
1825 goto out_unlock;
1826 }
1827 atomic_inc(&lo->plh_outstanding);
1828 spin_unlock(&ino->i_lock);
1829
1830 if (list_empty(&lo->plh_layouts)) {
1831
1832
1833
1834 spin_lock(&clp->cl_lock);
1835 if (list_empty(&lo->plh_layouts))
1836 list_add_tail(&lo->plh_layouts, &server->layouts);
1837 spin_unlock(&clp->cl_lock);
1838 }
1839
1840 pg_offset = arg.offset & ~PAGE_CACHE_MASK;
1841 if (pg_offset) {
1842 arg.offset -= pg_offset;
1843 arg.length += pg_offset;
1844 }
1845 if (arg.length != NFS4_MAX_UINT64)
1846 arg.length = PAGE_CACHE_ALIGN(arg.length);
1847
1848 lseg = send_layoutget(lo, ctx, &stateid, &arg, &timeout, gfp_flags);
1849 trace_pnfs_update_layout(ino, pos, count, iomode, lo, lseg,
1850 PNFS_UPDATE_LAYOUT_SEND_LAYOUTGET);
1851 atomic_dec(&lo->plh_outstanding);
1852 if (IS_ERR(lseg)) {
1853 switch(PTR_ERR(lseg)) {
1854 case -EBUSY:
1855 if (time_after(jiffies, giveup))
1856 lseg = NULL;
1857 break;
1858 case -ERECALLCONFLICT:
1859
1860 if (first) {
1861 lseg = NULL;
1862 break;
1863 }
1864
1865 if (time_after(jiffies, giveup))
1866 pnfs_destroy_layout(NFS_I(ino));
1867
1868 case -EAGAIN:
1869 break;
1870 default:
1871 if (!nfs_error_is_fatal(PTR_ERR(lseg))) {
1872 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
1873 lseg = NULL;
1874 }
1875 goto out_put_layout_hdr;
1876 }
1877 if (lseg) {
1878 if (first)
1879 pnfs_clear_first_layoutget(lo);
1880 trace_pnfs_update_layout(ino, pos, count,
1881 iomode, lo, lseg, PNFS_UPDATE_LAYOUT_RETRY);
1882 pnfs_put_layout_hdr(lo);
1883 goto lookup_again;
1884 }
1885 } else {
1886 pnfs_layout_clear_fail_bit(lo, pnfs_iomode_to_fail_bit(iomode));
1887 }
1888
1889out_put_layout_hdr:
1890 if (first)
1891 pnfs_clear_first_layoutget(lo);
1892 pnfs_put_layout_hdr(lo);
1893out:
1894 dprintk("%s: inode %s/%llu pNFS layout segment %s for "
1895 "(%s, offset: %llu, length: %llu)\n",
1896 __func__, ino->i_sb->s_id,
1897 (unsigned long long)NFS_FILEID(ino),
1898 IS_ERR_OR_NULL(lseg) ? "not found" : "found",
1899 iomode==IOMODE_RW ? "read/write" : "read-only",
1900 (unsigned long long)pos,
1901 (unsigned long long)count);
1902 return lseg;
1903out_unlock:
1904 spin_unlock(&ino->i_lock);
1905 goto out_put_layout_hdr;
1906}
1907EXPORT_SYMBOL_GPL(pnfs_update_layout);
1908
1909static bool
1910pnfs_sanity_check_layout_range(struct pnfs_layout_range *range)
1911{
1912 switch (range->iomode) {
1913 case IOMODE_READ:
1914 case IOMODE_RW:
1915 break;
1916 default:
1917 return false;
1918 }
1919 if (range->offset == NFS4_MAX_UINT64)
1920 return false;
1921 if (range->length == 0)
1922 return false;
1923 if (range->length != NFS4_MAX_UINT64 &&
1924 range->length > NFS4_MAX_UINT64 - range->offset)
1925 return false;
1926 return true;
1927}
1928
1929struct pnfs_layout_segment *
1930pnfs_layout_process(struct nfs4_layoutget *lgp)
1931{
1932 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
1933 struct nfs4_layoutget_res *res = &lgp->res;
1934 struct pnfs_layout_segment *lseg;
1935 struct inode *ino = lo->plh_inode;
1936 LIST_HEAD(free_me);
1937
1938 if (!pnfs_sanity_check_layout_range(&res->range))
1939 return ERR_PTR(-EINVAL);
1940
1941
1942 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
1943 if (IS_ERR_OR_NULL(lseg)) {
1944 if (!lseg)
1945 lseg = ERR_PTR(-ENOMEM);
1946
1947 dprintk("%s: Could not allocate layout: error %ld\n",
1948 __func__, PTR_ERR(lseg));
1949 return lseg;
1950 }
1951
1952 pnfs_init_lseg(lo, lseg, &res->range, &res->stateid);
1953
1954 spin_lock(&ino->i_lock);
1955 if (pnfs_layoutgets_blocked(lo)) {
1956 dprintk("%s forget reply due to state\n", __func__);
1957 goto out_forget;
1958 }
1959
1960 if (!pnfs_layout_is_valid(lo)) {
1961
1962 pnfs_set_layout_stateid(lo, &res->stateid, true);
1963 } else if (nfs4_stateid_match_other(&lo->plh_stateid, &res->stateid)) {
1964
1965 if (pnfs_layout_stateid_blocked(lo, &res->stateid)) {
1966 dprintk("%s forget reply due to sequence\n", __func__);
1967 goto out_forget;
1968 }
1969 pnfs_set_layout_stateid(lo, &res->stateid, false);
1970 } else {
1971
1972
1973
1974
1975 pnfs_mark_layout_stateid_invalid(lo, &free_me);
1976 goto out_forget;
1977 }
1978
1979 pnfs_get_lseg(lseg);
1980 pnfs_layout_insert_lseg(lo, lseg, &free_me);
1981
1982
1983 if (res->return_on_close)
1984 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
1985
1986 spin_unlock(&ino->i_lock);
1987 pnfs_free_lseg_list(&free_me);
1988 return lseg;
1989
1990out_forget:
1991 spin_unlock(&ino->i_lock);
1992 lseg->pls_layout = lo;
1993 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
1994 if (!pnfs_layout_is_valid(lo))
1995 nfs_commit_inode(ino, 0);
1996 return ERR_PTR(-EAGAIN);
1997}
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009int
2010pnfs_mark_matching_lsegs_return(struct pnfs_layout_hdr *lo,
2011 struct list_head *tmp_list,
2012 const struct pnfs_layout_range *return_range,
2013 u32 seq)
2014{
2015 struct pnfs_layout_segment *lseg, *next;
2016 int remaining = 0;
2017
2018 dprintk("%s:Begin lo %p\n", __func__, lo);
2019
2020 if (list_empty(&lo->plh_segs))
2021 return 0;
2022
2023 assert_spin_locked(&lo->plh_inode->i_lock);
2024
2025 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
2026 if (pnfs_match_lseg_recall(lseg, return_range, seq)) {
2027 dprintk("%s: marking lseg %p iomode %d "
2028 "offset %llu length %llu\n", __func__,
2029 lseg, lseg->pls_range.iomode,
2030 lseg->pls_range.offset,
2031 lseg->pls_range.length);
2032 if (mark_lseg_invalid(lseg, tmp_list))
2033 continue;
2034 remaining++;
2035 set_bit(NFS_LSEG_LAYOUTRETURN, &lseg->pls_flags);
2036 }
2037
2038 if (remaining)
2039 pnfs_set_plh_return_info(lo, return_range->iomode, seq);
2040
2041 return remaining;
2042}
2043
2044void pnfs_error_mark_layout_for_return(struct inode *inode,
2045 struct pnfs_layout_segment *lseg)
2046{
2047 struct pnfs_layout_hdr *lo = NFS_I(inode)->layout;
2048 struct pnfs_layout_range range = {
2049 .iomode = lseg->pls_range.iomode,
2050 .offset = 0,
2051 .length = NFS4_MAX_UINT64,
2052 };
2053 bool return_now = false;
2054
2055 spin_lock(&inode->i_lock);
2056 pnfs_set_plh_return_info(lo, range.iomode, 0);
2057
2058
2059
2060
2061
2062 if (!pnfs_mark_matching_lsegs_return(lo, &lo->plh_return_segs, &range, 0)) {
2063 nfs4_stateid stateid;
2064 enum pnfs_iomode iomode;
2065
2066 return_now = pnfs_prepare_layoutreturn(lo, &stateid, &iomode);
2067 spin_unlock(&inode->i_lock);
2068 if (return_now)
2069 pnfs_send_layoutreturn(lo, &stateid, iomode, false);
2070 } else {
2071 spin_unlock(&inode->i_lock);
2072 nfs_commit_inode(inode, 0);
2073 }
2074}
2075EXPORT_SYMBOL_GPL(pnfs_error_mark_layout_for_return);
2076
2077
2078
2079
2080
2081static void
2082pnfs_generic_pg_check_range(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
2083{
2084 if (pgio->pg_lseg && !pnfs_lseg_request_intersecting(pgio->pg_lseg, req)) {
2085 pnfs_put_lseg(pgio->pg_lseg);
2086 pgio->pg_lseg = NULL;
2087 }
2088}
2089
2090void
2091pnfs_generic_pg_check_layout(struct nfs_pageio_descriptor *pgio)
2092{
2093 if (pgio->pg_lseg == NULL ||
2094 test_bit(NFS_LSEG_VALID, &pgio->pg_lseg->pls_flags))
2095 return;
2096 pnfs_put_lseg(pgio->pg_lseg);
2097 pgio->pg_lseg = NULL;
2098}
2099EXPORT_SYMBOL_GPL(pnfs_generic_pg_check_layout);
2100
2101void
2102pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
2103{
2104 u64 rd_size = req->wb_bytes;
2105
2106 pnfs_generic_pg_check_layout(pgio);
2107 pnfs_generic_pg_check_range(pgio, req);
2108 if (pgio->pg_lseg == NULL) {
2109 if (pgio->pg_dreq == NULL)
2110 rd_size = i_size_read(pgio->pg_inode) - req_offset(req);
2111 else
2112 rd_size = nfs_dreq_bytes_left(pgio->pg_dreq);
2113
2114 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
2115 req->wb_context,
2116 req_offset(req),
2117 rd_size,
2118 IOMODE_READ,
2119 false,
2120 GFP_KERNEL);
2121 if (IS_ERR(pgio->pg_lseg)) {
2122 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
2123 pgio->pg_lseg = NULL;
2124 return;
2125 }
2126 }
2127
2128 if (pgio->pg_lseg == NULL)
2129 nfs_pageio_reset_read_mds(pgio);
2130
2131}
2132EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
2133
2134void
2135pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio,
2136 struct nfs_page *req, u64 wb_size)
2137{
2138 pnfs_generic_pg_check_layout(pgio);
2139 pnfs_generic_pg_check_range(pgio, req);
2140 if (pgio->pg_lseg == NULL) {
2141 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
2142 req->wb_context,
2143 req_offset(req),
2144 wb_size,
2145 IOMODE_RW,
2146 false,
2147 GFP_NOFS);
2148 if (IS_ERR(pgio->pg_lseg)) {
2149 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
2150 pgio->pg_lseg = NULL;
2151 return;
2152 }
2153 }
2154
2155 if (pgio->pg_lseg == NULL)
2156 nfs_pageio_reset_write_mds(pgio);
2157}
2158EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
2159
2160void
2161pnfs_generic_pg_cleanup(struct nfs_pageio_descriptor *desc)
2162{
2163 if (desc->pg_lseg) {
2164 pnfs_put_lseg(desc->pg_lseg);
2165 desc->pg_lseg = NULL;
2166 }
2167}
2168EXPORT_SYMBOL_GPL(pnfs_generic_pg_cleanup);
2169
2170
2171
2172
2173
2174size_t
2175pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio,
2176 struct nfs_page *prev, struct nfs_page *req)
2177{
2178 unsigned int size;
2179 u64 seg_end, req_start, seg_left;
2180
2181 size = nfs_generic_pg_test(pgio, prev, req);
2182 if (!size)
2183 return 0;
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196 if (pgio->pg_lseg) {
2197 seg_end = pnfs_end_offset(pgio->pg_lseg->pls_range.offset,
2198 pgio->pg_lseg->pls_range.length);
2199 req_start = req_offset(req);
2200
2201
2202 if (req_start >= seg_end)
2203 return 0;
2204
2205
2206
2207 seg_left = seg_end - req_start;
2208 if (seg_left < size)
2209 size = (unsigned int)seg_left;
2210 }
2211
2212 return size;
2213}
2214EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
2215
2216int pnfs_write_done_resend_to_mds(struct nfs_pgio_header *hdr)
2217{
2218 struct nfs_pageio_descriptor pgio;
2219
2220
2221 nfs_pageio_init_write(&pgio, hdr->inode, FLUSH_STABLE, true,
2222 hdr->completion_ops);
2223 set_bit(NFS_CONTEXT_RESEND_WRITES, &hdr->args.context->flags);
2224 return nfs_pageio_resend(&pgio, hdr);
2225}
2226EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
2227
2228static void pnfs_ld_handle_write_error(struct nfs_pgio_header *hdr)
2229{
2230
2231 dprintk("pnfs write error = %d\n", hdr->pnfs_error);
2232 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
2233 PNFS_LAYOUTRET_ON_ERROR) {
2234 pnfs_return_layout(hdr->inode);
2235 }
2236 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
2237 hdr->task.tk_status = pnfs_write_done_resend_to_mds(hdr);
2238}
2239
2240
2241
2242
2243void pnfs_ld_write_done(struct nfs_pgio_header *hdr)
2244{
2245 if (likely(!hdr->pnfs_error)) {
2246 pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
2247 hdr->mds_offset + hdr->res.count);
2248 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
2249 }
2250 trace_nfs4_pnfs_write(hdr, hdr->pnfs_error);
2251 if (unlikely(hdr->pnfs_error))
2252 pnfs_ld_handle_write_error(hdr);
2253 hdr->mds_ops->rpc_release(hdr);
2254}
2255EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
2256
2257static void
2258pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
2259 struct nfs_pgio_header *hdr)
2260{
2261 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
2262
2263 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2264 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
2265 nfs_pageio_reset_write_mds(desc);
2266 mirror->pg_recoalesce = 1;
2267 }
2268 hdr->completion_ops->completion(hdr);
2269}
2270
2271static enum pnfs_try_status
2272pnfs_try_to_write_data(struct nfs_pgio_header *hdr,
2273 const struct rpc_call_ops *call_ops,
2274 struct pnfs_layout_segment *lseg,
2275 int how)
2276{
2277 struct inode *inode = hdr->inode;
2278 enum pnfs_try_status trypnfs;
2279 struct nfs_server *nfss = NFS_SERVER(inode);
2280
2281 hdr->mds_ops = call_ops;
2282
2283 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
2284 inode->i_ino, hdr->args.count, hdr->args.offset, how);
2285 trypnfs = nfss->pnfs_curr_ld->write_pagelist(hdr, how);
2286 if (trypnfs != PNFS_NOT_ATTEMPTED)
2287 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
2288 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
2289 return trypnfs;
2290}
2291
2292static void
2293pnfs_do_write(struct nfs_pageio_descriptor *desc,
2294 struct nfs_pgio_header *hdr, int how)
2295{
2296 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
2297 struct pnfs_layout_segment *lseg = desc->pg_lseg;
2298 enum pnfs_try_status trypnfs;
2299
2300 trypnfs = pnfs_try_to_write_data(hdr, call_ops, lseg, how);
2301 switch (trypnfs) {
2302 case PNFS_NOT_ATTEMPTED:
2303 pnfs_write_through_mds(desc, hdr);
2304 case PNFS_ATTEMPTED:
2305 break;
2306 case PNFS_TRY_AGAIN:
2307
2308 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2309 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
2310 list_splice_init(&hdr->pages, &mirror->pg_list);
2311 mirror->pg_recoalesce = 1;
2312 }
2313 hdr->mds_ops->rpc_release(hdr);
2314 }
2315}
2316
2317static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
2318{
2319 pnfs_put_lseg(hdr->lseg);
2320 nfs_pgio_header_free(hdr);
2321}
2322
2323int
2324pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
2325{
2326 struct nfs_pgio_header *hdr;
2327 int ret;
2328
2329 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2330 if (!hdr) {
2331 desc->pg_error = -ENOMEM;
2332 return desc->pg_error;
2333 }
2334 nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
2335
2336 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
2337 ret = nfs_generic_pgio(desc, hdr);
2338 if (!ret)
2339 pnfs_do_write(desc, hdr, desc->pg_ioflags);
2340
2341 return ret;
2342}
2343EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
2344
2345int pnfs_read_done_resend_to_mds(struct nfs_pgio_header *hdr)
2346{
2347 struct nfs_pageio_descriptor pgio;
2348
2349
2350 nfs_pageio_init_read(&pgio, hdr->inode, true, hdr->completion_ops);
2351 return nfs_pageio_resend(&pgio, hdr);
2352}
2353EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
2354
2355static void pnfs_ld_handle_read_error(struct nfs_pgio_header *hdr)
2356{
2357 dprintk("pnfs read error = %d\n", hdr->pnfs_error);
2358 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
2359 PNFS_LAYOUTRET_ON_ERROR) {
2360 pnfs_return_layout(hdr->inode);
2361 }
2362 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
2363 hdr->task.tk_status = pnfs_read_done_resend_to_mds(hdr);
2364}
2365
2366
2367
2368
2369void pnfs_ld_read_done(struct nfs_pgio_header *hdr)
2370{
2371 if (likely(!hdr->pnfs_error))
2372 hdr->mds_ops->rpc_call_done(&hdr->task, hdr);
2373 trace_nfs4_pnfs_read(hdr, hdr->pnfs_error);
2374 if (unlikely(hdr->pnfs_error))
2375 pnfs_ld_handle_read_error(hdr);
2376 hdr->mds_ops->rpc_release(hdr);
2377}
2378EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
2379
2380static void
2381pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
2382 struct nfs_pgio_header *hdr)
2383{
2384 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
2385
2386 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2387 list_splice_tail_init(&hdr->pages, &mirror->pg_list);
2388 nfs_pageio_reset_read_mds(desc);
2389 mirror->pg_recoalesce = 1;
2390 }
2391 hdr->completion_ops->completion(hdr);
2392}
2393
2394
2395
2396
2397static enum pnfs_try_status
2398pnfs_try_to_read_data(struct nfs_pgio_header *hdr,
2399 const struct rpc_call_ops *call_ops,
2400 struct pnfs_layout_segment *lseg)
2401{
2402 struct inode *inode = hdr->inode;
2403 struct nfs_server *nfss = NFS_SERVER(inode);
2404 enum pnfs_try_status trypnfs;
2405
2406 hdr->mds_ops = call_ops;
2407
2408 dprintk("%s: Reading ino:%lu %u@%llu\n",
2409 __func__, inode->i_ino, hdr->args.count, hdr->args.offset);
2410
2411 trypnfs = nfss->pnfs_curr_ld->read_pagelist(hdr);
2412 if (trypnfs != PNFS_NOT_ATTEMPTED)
2413 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
2414 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
2415 return trypnfs;
2416}
2417
2418
2419void pnfs_read_resend_pnfs(struct nfs_pgio_header *hdr)
2420{
2421 struct nfs_pageio_descriptor pgio;
2422
2423 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2424
2425 pnfs_put_lseg(hdr->lseg);
2426 hdr->lseg = NULL;
2427
2428 nfs_pageio_init_read(&pgio, hdr->inode, false,
2429 hdr->completion_ops);
2430 hdr->task.tk_status = nfs_pageio_resend(&pgio, hdr);
2431 }
2432}
2433EXPORT_SYMBOL_GPL(pnfs_read_resend_pnfs);
2434
2435static void
2436pnfs_do_read(struct nfs_pageio_descriptor *desc, struct nfs_pgio_header *hdr)
2437{
2438 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
2439 struct pnfs_layout_segment *lseg = desc->pg_lseg;
2440 enum pnfs_try_status trypnfs;
2441
2442 trypnfs = pnfs_try_to_read_data(hdr, call_ops, lseg);
2443 switch (trypnfs) {
2444 case PNFS_NOT_ATTEMPTED:
2445 pnfs_read_through_mds(desc, hdr);
2446 case PNFS_ATTEMPTED:
2447 break;
2448 case PNFS_TRY_AGAIN:
2449
2450 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
2451 struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(desc);
2452 list_splice_init(&hdr->pages, &mirror->pg_list);
2453 mirror->pg_recoalesce = 1;
2454 }
2455 hdr->mds_ops->rpc_release(hdr);
2456 }
2457}
2458
2459static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
2460{
2461 pnfs_put_lseg(hdr->lseg);
2462 nfs_pgio_header_free(hdr);
2463}
2464
2465int
2466pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
2467{
2468 struct nfs_pgio_header *hdr;
2469 int ret;
2470
2471 hdr = nfs_pgio_header_alloc(desc->pg_rw_ops);
2472 if (!hdr) {
2473 desc->pg_error = -ENOMEM;
2474 return desc->pg_error;
2475 }
2476 nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
2477 hdr->lseg = pnfs_get_lseg(desc->pg_lseg);
2478 ret = nfs_generic_pgio(desc, hdr);
2479 if (!ret)
2480 pnfs_do_read(desc, hdr);
2481 return ret;
2482}
2483EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
2484
2485static void pnfs_clear_layoutcommitting(struct inode *inode)
2486{
2487 unsigned long *bitlock = &NFS_I(inode)->flags;
2488
2489 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
2490 smp_mb__after_clear_bit();
2491 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
2492}
2493
2494
2495
2496
2497static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
2498{
2499 struct pnfs_layout_segment *lseg;
2500
2501 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
2502 if (lseg->pls_range.iomode == IOMODE_RW &&
2503 test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
2504 list_add(&lseg->pls_lc_list, listp);
2505 }
2506}
2507
2508static void pnfs_list_write_lseg_done(struct inode *inode, struct list_head *listp)
2509{
2510 struct pnfs_layout_segment *lseg, *tmp;
2511
2512
2513 list_for_each_entry_safe(lseg, tmp, listp, pls_lc_list) {
2514 list_del_init(&lseg->pls_lc_list);
2515 pnfs_put_lseg(lseg);
2516 }
2517
2518 pnfs_clear_layoutcommitting(inode);
2519}
2520
2521void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
2522{
2523 pnfs_layout_io_set_failed(lseg->pls_layout, lseg->pls_range.iomode);
2524}
2525EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
2526
2527void
2528pnfs_set_layoutcommit(struct inode *inode, struct pnfs_layout_segment *lseg,
2529 loff_t end_pos)
2530{
2531 struct nfs_inode *nfsi = NFS_I(inode);
2532 bool mark_as_dirty = false;
2533
2534 spin_lock(&inode->i_lock);
2535 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
2536 nfsi->layout->plh_lwb = end_pos;
2537 mark_as_dirty = true;
2538 dprintk("%s: Set layoutcommit for inode %lu ",
2539 __func__, inode->i_ino);
2540 } else if (end_pos > nfsi->layout->plh_lwb)
2541 nfsi->layout->plh_lwb = end_pos;
2542 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags)) {
2543
2544 pnfs_get_lseg(lseg);
2545 }
2546 spin_unlock(&inode->i_lock);
2547 dprintk("%s: lseg %p end_pos %llu\n",
2548 __func__, lseg, nfsi->layout->plh_lwb);
2549
2550
2551
2552 if (mark_as_dirty)
2553 mark_inode_dirty_sync(inode);
2554}
2555EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
2556
2557void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
2558{
2559 struct nfs_server *nfss = NFS_SERVER(data->args.inode);
2560
2561 if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
2562 nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
2563 pnfs_list_write_lseg_done(data->args.inode, &data->lseg_list);
2564}
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574int
2575pnfs_layoutcommit_inode(struct inode *inode, bool sync)
2576{
2577 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
2578 struct nfs4_layoutcommit_data *data;
2579 struct nfs_inode *nfsi = NFS_I(inode);
2580 loff_t end_pos;
2581 int status;
2582
2583 if (!pnfs_layoutcommit_outstanding(inode))
2584 return 0;
2585
2586 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
2587
2588 status = -EAGAIN;
2589 if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
2590 if (!sync)
2591 goto out;
2592 status = wait_on_bit_lock_action(&nfsi->flags,
2593 NFS_INO_LAYOUTCOMMITTING,
2594 nfs_wait_bit_killable,
2595 TASK_KILLABLE);
2596 if (status)
2597 goto out;
2598 }
2599
2600 status = -ENOMEM;
2601
2602 data = kzalloc(sizeof(*data), GFP_NOFS);
2603 if (!data)
2604 goto clear_layoutcommitting;
2605
2606 status = 0;
2607 spin_lock(&inode->i_lock);
2608 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
2609 goto out_unlock;
2610
2611 INIT_LIST_HEAD(&data->lseg_list);
2612 pnfs_list_write_lseg(inode, &data->lseg_list);
2613
2614 end_pos = nfsi->layout->plh_lwb;
2615
2616 nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
2617 spin_unlock(&inode->i_lock);
2618
2619 data->args.inode = inode;
2620 data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
2621 nfs_fattr_init(&data->fattr);
2622 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
2623 data->res.fattr = &data->fattr;
2624 if (end_pos != 0)
2625 data->args.lastbytewritten = end_pos - 1;
2626 else
2627 data->args.lastbytewritten = U64_MAX;
2628 data->res.server = NFS_SERVER(inode);
2629
2630 if (ld->prepare_layoutcommit) {
2631 status = ld->prepare_layoutcommit(&data->args);
2632 if (status) {
2633 put_rpccred(data->cred);
2634 spin_lock(&inode->i_lock);
2635 set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags);
2636 if (end_pos > nfsi->layout->plh_lwb)
2637 nfsi->layout->plh_lwb = end_pos;
2638 goto out_unlock;
2639 }
2640 }
2641
2642
2643 status = nfs4_proc_layoutcommit(data, sync);
2644out:
2645 if (status)
2646 mark_inode_dirty_sync(inode);
2647 dprintk("<-- %s status %d\n", __func__, status);
2648 return status;
2649out_unlock:
2650 spin_unlock(&inode->i_lock);
2651 kfree(data);
2652clear_layoutcommitting:
2653 pnfs_clear_layoutcommitting(inode);
2654 goto out;
2655}
2656EXPORT_SYMBOL_GPL(pnfs_layoutcommit_inode);
2657
2658int
2659pnfs_generic_sync(struct inode *inode, bool datasync)
2660{
2661 return pnfs_layoutcommit_inode(inode, true);
2662}
2663EXPORT_SYMBOL_GPL(pnfs_generic_sync);
2664
2665struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
2666{
2667 struct nfs4_threshold *thp;
2668
2669 thp = kzalloc(sizeof(*thp), GFP_NOFS);
2670 if (!thp) {
2671 dprintk("%s mdsthreshold allocation failed\n", __func__);
2672 return NULL;
2673 }
2674 return thp;
2675}
2676
2677#if IS_ENABLED(CONFIG_NFS_V4_2)
2678int
2679pnfs_report_layoutstat(struct inode *inode, gfp_t gfp_flags)
2680{
2681 struct pnfs_layoutdriver_type *ld = NFS_SERVER(inode)->pnfs_curr_ld;
2682 struct nfs_server *server = NFS_SERVER(inode);
2683 struct nfs_inode *nfsi = NFS_I(inode);
2684 struct nfs42_layoutstat_data *data;
2685 struct pnfs_layout_hdr *hdr;
2686 int status = 0;
2687
2688 if (!pnfs_enabled_sb(server) || !ld->prepare_layoutstats)
2689 goto out;
2690
2691 if (!nfs_server_capable(inode, NFS_CAP_LAYOUTSTATS))
2692 goto out;
2693
2694 if (test_and_set_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags))
2695 goto out;
2696
2697 spin_lock(&inode->i_lock);
2698 if (!NFS_I(inode)->layout) {
2699 spin_unlock(&inode->i_lock);
2700 goto out_clear_layoutstats;
2701 }
2702 hdr = NFS_I(inode)->layout;
2703 pnfs_get_layout_hdr(hdr);
2704 spin_unlock(&inode->i_lock);
2705
2706 data = kzalloc(sizeof(*data), gfp_flags);
2707 if (!data) {
2708 status = -ENOMEM;
2709 goto out_put;
2710 }
2711
2712 data->args.fh = NFS_FH(inode);
2713 data->args.inode = inode;
2714 status = ld->prepare_layoutstats(&data->args);
2715 if (status)
2716 goto out_free;
2717
2718 status = nfs42_proc_layoutstats_generic(NFS_SERVER(inode), data);
2719
2720out:
2721 dprintk("%s returns %d\n", __func__, status);
2722 return status;
2723
2724out_free:
2725 kfree(data);
2726out_put:
2727 pnfs_put_layout_hdr(hdr);
2728out_clear_layoutstats:
2729 smp_mb__before_atomic();
2730 clear_bit(NFS_INO_LAYOUTSTATS, &nfsi->flags);
2731 smp_mb__after_atomic();
2732 goto out;
2733}
2734EXPORT_SYMBOL_GPL(pnfs_report_layoutstat);
2735#endif
2736
2737unsigned int layoutstats_timer;
2738module_param(layoutstats_timer, uint, 0644);
2739EXPORT_SYMBOL_GPL(layoutstats_timer);
2740