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