NFS: Move the pnfs write code into pnfs.c
[linux-2.6-block.git] / fs / nfs / pnfs.c
CommitLineData
85e174ba
RL
1/*
2 * pNFS functions to call and manage layout drivers.
3 *
4 * Copyright (c) 2002 [year of first publication]
5 * The Regents of the University of Michigan
6 * All Rights Reserved
7 *
8 * Dean Hildebrand <dhildebz@umich.edu>
9 *
10 * Permission is granted to use, copy, create derivative works, and
11 * redistribute this software and such derivative works for any purpose,
12 * so long as the name of the University of Michigan is not used in
13 * any advertising or publicity pertaining to the use or distribution
14 * of this software without specific, written prior authorization. If
15 * the above copyright notice or any other identification of the
16 * University of Michigan is included in any copy of any portion of
17 * this software, then the disclaimer below must also be included.
18 *
19 * This software is provided as is, without representation or warranty
20 * of any kind either express or implied, including without limitation
21 * the implied warranties of merchantability, fitness for a particular
22 * purpose, or noninfringement. The Regents of the University of
23 * Michigan shall not be liable for any damages, including special,
24 * indirect, incidental, or consequential damages, with respect to any
25 * claim arising out of or in connection with the use of the software,
26 * even if it has been or is hereafter advised of the possibility of
27 * such damages.
28 */
29
30#include <linux/nfs_fs.h>
493292dd 31#include <linux/nfs_page.h>
974cec8c 32#include "internal.h"
85e174ba 33#include "pnfs.h"
64419a9b 34#include "iostat.h"
85e174ba
RL
35
36#define NFSDBG_FACILITY NFSDBG_PNFS
37
02c35fca
FI
38/* Locking:
39 *
40 * pnfs_spinlock:
41 * protects pnfs_modules_tbl.
42 */
43static DEFINE_SPINLOCK(pnfs_spinlock);
44
45/*
46 * pnfs_modules_tbl holds all pnfs modules
47 */
48static LIST_HEAD(pnfs_modules_tbl);
49
50/* Return the registered pnfs layout driver module matching given id */
51static struct pnfs_layoutdriver_type *
52find_pnfs_driver_locked(u32 id)
53{
54 struct pnfs_layoutdriver_type *local;
55
56 list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
57 if (local->id == id)
58 goto out;
59 local = NULL;
60out:
61 dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
62 return local;
63}
64
85e174ba
RL
65static struct pnfs_layoutdriver_type *
66find_pnfs_driver(u32 id)
67{
02c35fca
FI
68 struct pnfs_layoutdriver_type *local;
69
70 spin_lock(&pnfs_spinlock);
71 local = find_pnfs_driver_locked(id);
72 spin_unlock(&pnfs_spinlock);
73 return local;
85e174ba
RL
74}
75
76void
77unset_pnfs_layoutdriver(struct nfs_server *nfss)
78{
ea8eecdd 79 if (nfss->pnfs_curr_ld)
02c35fca 80 module_put(nfss->pnfs_curr_ld->owner);
85e174ba
RL
81 nfss->pnfs_curr_ld = NULL;
82}
83
84/*
85 * Try to set the server's pnfs module to the pnfs layout type specified by id.
86 * Currently only one pNFS layout driver per filesystem is supported.
87 *
88 * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
89 */
90void
91set_pnfs_layoutdriver(struct nfs_server *server, u32 id)
92{
93 struct pnfs_layoutdriver_type *ld_type = NULL;
94
95 if (id == 0)
96 goto out_no_driver;
97 if (!(server->nfs_client->cl_exchange_flags &
98 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
99 printk(KERN_ERR "%s: id %u cl_exchange_flags 0x%x\n", __func__,
100 id, server->nfs_client->cl_exchange_flags);
101 goto out_no_driver;
102 }
103 ld_type = find_pnfs_driver(id);
104 if (!ld_type) {
105 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
106 ld_type = find_pnfs_driver(id);
107 if (!ld_type) {
108 dprintk("%s: No pNFS module found for %u.\n",
109 __func__, id);
110 goto out_no_driver;
111 }
112 }
02c35fca
FI
113 if (!try_module_get(ld_type->owner)) {
114 dprintk("%s: Could not grab reference on module\n", __func__);
115 goto out_no_driver;
116 }
85e174ba 117 server->pnfs_curr_ld = ld_type;
ea8eecdd 118
85e174ba
RL
119 dprintk("%s: pNFS module for %u set\n", __func__, id);
120 return;
121
122out_no_driver:
123 dprintk("%s: Using NFSv4 I/O\n", __func__);
124 server->pnfs_curr_ld = NULL;
125}
02c35fca
FI
126
127int
128pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
129{
130 int status = -EINVAL;
131 struct pnfs_layoutdriver_type *tmp;
132
133 if (ld_type->id == 0) {
134 printk(KERN_ERR "%s id 0 is reserved\n", __func__);
135 return status;
136 }
b1f69b75
AA
137 if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
138 printk(KERN_ERR "%s Layout driver must provide "
139 "alloc_lseg and free_lseg.\n", __func__);
140 return status;
141 }
02c35fca
FI
142
143 spin_lock(&pnfs_spinlock);
144 tmp = find_pnfs_driver_locked(ld_type->id);
145 if (!tmp) {
146 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
147 status = 0;
148 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
149 ld_type->name);
150 } else {
151 printk(KERN_ERR "%s Module with id %d already loaded!\n",
152 __func__, ld_type->id);
153 }
154 spin_unlock(&pnfs_spinlock);
155
156 return status;
157}
158EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
159
160void
161pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
162{
163 dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
164 spin_lock(&pnfs_spinlock);
165 list_del(&ld_type->pnfs_tblid);
166 spin_unlock(&pnfs_spinlock);
167}
168EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
e5e94017 169
b1f69b75
AA
170/*
171 * pNFS client layout cache
172 */
173
cc6e5340 174/* Need to hold i_lock if caller does not already hold reference */
43f1b3da 175void
cc6e5340 176get_layout_hdr(struct pnfs_layout_hdr *lo)
e5e94017 177{
cc6e5340 178 atomic_inc(&lo->plh_refcount);
e5e94017
BH
179}
180
636fb9c8
BH
181static struct pnfs_layout_hdr *
182pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
183{
184 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
185 return ld->alloc_layout_hdr ? ld->alloc_layout_hdr(ino, gfp_flags) :
186 kzalloc(sizeof(struct pnfs_layout_hdr), gfp_flags);
187}
188
189static void
190pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
191{
192 struct pnfs_layoutdriver_type *ld = NFS_SERVER(lo->plh_inode)->pnfs_curr_ld;
193 return ld->alloc_layout_hdr ? ld->free_layout_hdr(lo) : kfree(lo);
194}
195
e5e94017 196static void
cc6e5340 197destroy_layout_hdr(struct pnfs_layout_hdr *lo)
e5e94017 198{
cc6e5340
FI
199 dprintk("%s: freeing layout cache %p\n", __func__, lo);
200 BUG_ON(!list_empty(&lo->plh_layouts));
201 NFS_I(lo->plh_inode)->layout = NULL;
636fb9c8 202 pnfs_free_layout_hdr(lo);
cc6e5340 203}
e5e94017 204
cc6e5340
FI
205static void
206put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
207{
208 if (atomic_dec_and_test(&lo->plh_refcount))
209 destroy_layout_hdr(lo);
e5e94017
BH
210}
211
b1f69b75 212void
cc6e5340 213put_layout_hdr(struct pnfs_layout_hdr *lo)
974cec8c 214{
cc6e5340
FI
215 struct inode *inode = lo->plh_inode;
216
217 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
218 destroy_layout_hdr(lo);
219 spin_unlock(&inode->i_lock);
220 }
974cec8c
AA
221}
222
223static void
224init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
225{
566052c5 226 INIT_LIST_HEAD(&lseg->pls_list);
4541d16c
FI
227 atomic_set(&lseg->pls_refcount, 1);
228 smp_mb();
229 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
566052c5 230 lseg->pls_layout = lo;
974cec8c
AA
231}
232
4541d16c 233static void free_lseg(struct pnfs_layout_segment *lseg)
974cec8c 234{
b7edfaa1 235 struct inode *ino = lseg->pls_layout->plh_inode;
974cec8c 236
b1f69b75 237 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
52fabd73 238 /* Matched by get_layout_hdr in pnfs_insert_layout */
cc6e5340 239 put_layout_hdr(NFS_I(ino)->layout);
974cec8c
AA
240}
241
d684d2ae
FI
242static void
243put_lseg_common(struct pnfs_layout_segment *lseg)
244{
245 struct inode *inode = lseg->pls_layout->plh_inode;
246
d20581aa 247 WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
d684d2ae
FI
248 list_del_init(&lseg->pls_list);
249 if (list_empty(&lseg->pls_layout->plh_segs)) {
250 set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags);
251 /* Matched by initial refcount set in alloc_init_layout_hdr */
252 put_layout_hdr_locked(lseg->pls_layout);
253 }
254 rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
255}
256
bae724ef 257void
d684d2ae 258put_lseg(struct pnfs_layout_segment *lseg)
974cec8c 259{
d684d2ae
FI
260 struct inode *inode;
261
262 if (!lseg)
263 return;
264
4541d16c
FI
265 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
266 atomic_read(&lseg->pls_refcount),
267 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
d684d2ae
FI
268 inode = lseg->pls_layout->plh_inode;
269 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
270 LIST_HEAD(free_me);
4541d16c 271
d684d2ae
FI
272 put_lseg_common(lseg);
273 list_add(&lseg->pls_list, &free_me);
274 spin_unlock(&inode->i_lock);
275 pnfs_free_lseg_list(&free_me);
4541d16c 276 }
4541d16c 277}
e0c2b380 278EXPORT_SYMBOL_GPL(put_lseg);
974cec8c 279
fb3296eb
BH
280static inline u64
281end_offset(u64 start, u64 len)
282{
283 u64 end;
284
285 end = start + len;
286 return end >= start ? end : NFS4_MAX_UINT64;
287}
288
289/* last octet in a range */
290static inline u64
291last_byte_offset(u64 start, u64 len)
292{
293 u64 end;
294
295 BUG_ON(!len);
296 end = start + len;
297 return end > start ? end - 1 : NFS4_MAX_UINT64;
298}
299
300/*
301 * is l2 fully contained in l1?
302 * start1 end1
303 * [----------------------------------)
304 * start2 end2
305 * [----------------)
306 */
307static inline int
308lo_seg_contained(struct pnfs_layout_range *l1,
309 struct pnfs_layout_range *l2)
310{
311 u64 start1 = l1->offset;
312 u64 end1 = end_offset(start1, l1->length);
313 u64 start2 = l2->offset;
314 u64 end2 = end_offset(start2, l2->length);
315
316 return (start1 <= start2) && (end1 >= end2);
317}
318
319/*
320 * is l1 and l2 intersecting?
321 * start1 end1
322 * [----------------------------------)
323 * start2 end2
324 * [----------------)
325 */
326static inline int
327lo_seg_intersecting(struct pnfs_layout_range *l1,
328 struct pnfs_layout_range *l2)
329{
330 u64 start1 = l1->offset;
331 u64 end1 = end_offset(start1, l1->length);
332 u64 start2 = l2->offset;
333 u64 end2 = end_offset(start2, l2->length);
334
335 return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
336 (end2 == NFS4_MAX_UINT64 || end2 > start1);
337}
338
4541d16c 339static bool
778b5502
BH
340should_free_lseg(struct pnfs_layout_range *lseg_range,
341 struct pnfs_layout_range *recall_range)
4541d16c 342{
778b5502
BH
343 return (recall_range->iomode == IOMODE_ANY ||
344 lseg_range->iomode == recall_range->iomode) &&
345 lo_seg_intersecting(lseg_range, recall_range);
974cec8c
AA
346}
347
4541d16c
FI
348/* Returns 1 if lseg is removed from list, 0 otherwise */
349static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
350 struct list_head *tmp_list)
351{
352 int rv = 0;
353
354 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
355 /* Remove the reference keeping the lseg in the
356 * list. It will now be removed when all
357 * outstanding io is finished.
358 */
d684d2ae
FI
359 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
360 atomic_read(&lseg->pls_refcount));
361 if (atomic_dec_and_test(&lseg->pls_refcount)) {
362 put_lseg_common(lseg);
363 list_add(&lseg->pls_list, tmp_list);
364 rv = 1;
365 }
4541d16c
FI
366 }
367 return rv;
368}
369
370/* Returns count of number of matching invalid lsegs remaining in list
371 * after call.
372 */
43f1b3da 373int
4541d16c
FI
374mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
375 struct list_head *tmp_list,
778b5502 376 struct pnfs_layout_range *recall_range)
974cec8c
AA
377{
378 struct pnfs_layout_segment *lseg, *next;
4541d16c 379 int invalid = 0, removed = 0;
974cec8c
AA
380
381 dprintk("%s:Begin lo %p\n", __func__, lo);
382
38511722
FI
383 if (list_empty(&lo->plh_segs)) {
384 if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags))
385 put_layout_hdr_locked(lo);
386 return 0;
387 }
4541d16c 388 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
778b5502
BH
389 if (!recall_range ||
390 should_free_lseg(&lseg->pls_range, recall_range)) {
4541d16c
FI
391 dprintk("%s: freeing lseg %p iomode %d "
392 "offset %llu length %llu\n", __func__,
393 lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
394 lseg->pls_range.length);
395 invalid++;
396 removed += mark_lseg_invalid(lseg, tmp_list);
397 }
398 dprintk("%s:Return %i\n", __func__, invalid - removed);
399 return invalid - removed;
974cec8c
AA
400}
401
f49f9baa 402/* note free_me must contain lsegs from a single layout_hdr */
43f1b3da 403void
4541d16c 404pnfs_free_lseg_list(struct list_head *free_me)
974cec8c 405{
4541d16c 406 struct pnfs_layout_segment *lseg, *tmp;
f49f9baa
FI
407 struct pnfs_layout_hdr *lo;
408
409 if (list_empty(free_me))
410 return;
411
412 lo = list_first_entry(free_me, struct pnfs_layout_segment,
413 pls_list)->pls_layout;
974cec8c 414
f49f9baa
FI
415 if (test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) {
416 struct nfs_client *clp;
417
418 clp = NFS_SERVER(lo->plh_inode)->nfs_client;
419 spin_lock(&clp->cl_lock);
420 list_del_init(&lo->plh_layouts);
421 spin_unlock(&clp->cl_lock);
422 }
4541d16c 423 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
566052c5 424 list_del(&lseg->pls_list);
4541d16c 425 free_lseg(lseg);
974cec8c
AA
426 }
427}
428
e5e94017
BH
429void
430pnfs_destroy_layout(struct nfs_inode *nfsi)
431{
432 struct pnfs_layout_hdr *lo;
974cec8c 433 LIST_HEAD(tmp_list);
e5e94017
BH
434
435 spin_lock(&nfsi->vfs_inode.i_lock);
436 lo = nfsi->layout;
437 if (lo) {
38511722 438 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
778b5502 439 mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
e5e94017
BH
440 }
441 spin_unlock(&nfsi->vfs_inode.i_lock);
974cec8c
AA
442 pnfs_free_lseg_list(&tmp_list);
443}
444
445/*
446 * Called by the state manger to remove all layouts established under an
447 * expired lease.
448 */
449void
450pnfs_destroy_all_layouts(struct nfs_client *clp)
451{
6382a441 452 struct nfs_server *server;
974cec8c
AA
453 struct pnfs_layout_hdr *lo;
454 LIST_HEAD(tmp_list);
455
c47abcf8
AA
456 nfs4_deviceid_mark_client_invalid(clp);
457 nfs4_deviceid_purge_client(clp);
458
974cec8c 459 spin_lock(&clp->cl_lock);
6382a441
WAA
460 rcu_read_lock();
461 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
462 if (!list_empty(&server->layouts))
463 list_splice_init(&server->layouts, &tmp_list);
464 }
465 rcu_read_unlock();
974cec8c
AA
466 spin_unlock(&clp->cl_lock);
467
468 while (!list_empty(&tmp_list)) {
469 lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
b7edfaa1 470 plh_layouts);
974cec8c 471 dprintk("%s freeing layout for inode %lu\n", __func__,
b7edfaa1 472 lo->plh_inode->i_ino);
2887fe45 473 list_del_init(&lo->plh_layouts);
b7edfaa1 474 pnfs_destroy_layout(NFS_I(lo->plh_inode));
974cec8c 475 }
e5e94017
BH
476}
477
fd6002e9 478/* update lo->plh_stateid with new if is more recent */
43f1b3da
FI
479void
480pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
481 bool update_barrier)
b1f69b75 482{
fd6002e9 483 u32 oldseq, newseq;
b1f69b75 484
fd6002e9
FI
485 oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid);
486 newseq = be32_to_cpu(new->stateid.seqid);
43f1b3da 487 if ((int)(newseq - oldseq) > 0) {
fd6002e9 488 memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid));
43f1b3da
FI
489 if (update_barrier) {
490 u32 new_barrier = be32_to_cpu(new->stateid.seqid);
491
492 if ((int)(new_barrier - lo->plh_barrier))
493 lo->plh_barrier = new_barrier;
494 } else {
495 /* Because of wraparound, we want to keep the barrier
496 * "close" to the current seqids. It needs to be
497 * within 2**31 to count as "behind", so if it
498 * gets too near that limit, give us a litle leeway
499 * and bring it to within 2**30.
500 * NOTE - and yes, this is all unsigned arithmetic.
501 */
502 if (unlikely((newseq - lo->plh_barrier) > (3 << 29)))
503 lo->plh_barrier = newseq - (1 << 30);
504 }
505 }
b1f69b75
AA
506}
507
cf7d63f1
FI
508/* lget is set to 1 if called from inside send_layoutget call chain */
509static bool
43f1b3da
FI
510pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid,
511 int lget)
512{
513 if ((stateid) &&
514 (int)(lo->plh_barrier - be32_to_cpu(stateid->stateid.seqid)) >= 0)
515 return true;
f7e8917a 516 return lo->plh_block_lgets ||
38511722 517 test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) ||
f7e8917a 518 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
43f1b3da 519 (list_empty(&lo->plh_segs) &&
cf7d63f1
FI
520 (atomic_read(&lo->plh_outstanding) > lget));
521}
522
fd6002e9
FI
523int
524pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
525 struct nfs4_state *open_state)
b1f69b75 526{
fd6002e9 527 int status = 0;
974cec8c 528
b1f69b75 529 dprintk("--> %s\n", __func__);
fd6002e9 530 spin_lock(&lo->plh_inode->i_lock);
43f1b3da 531 if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
cf7d63f1
FI
532 status = -EAGAIN;
533 } else if (list_empty(&lo->plh_segs)) {
fd6002e9
FI
534 int seq;
535
536 do {
537 seq = read_seqbegin(&open_state->seqlock);
538 memcpy(dst->data, open_state->stateid.data,
539 sizeof(open_state->stateid.data));
540 } while (read_seqretry(&open_state->seqlock, seq));
541 } else
542 memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data));
543 spin_unlock(&lo->plh_inode->i_lock);
b1f69b75 544 dprintk("<-- %s\n", __func__);
fd6002e9 545 return status;
b1f69b75
AA
546}
547
548/*
549* Get layout from server.
550* for now, assume that whole file layouts are requested.
551* arg->offset: 0
552* arg->length: all ones
553*/
e5e94017
BH
554static struct pnfs_layout_segment *
555send_layoutget(struct pnfs_layout_hdr *lo,
556 struct nfs_open_context *ctx,
fb3296eb 557 struct pnfs_layout_range *range,
a75b9df9 558 gfp_t gfp_flags)
e5e94017 559{
b7edfaa1 560 struct inode *ino = lo->plh_inode;
b1f69b75
AA
561 struct nfs_server *server = NFS_SERVER(ino);
562 struct nfs4_layoutget *lgp;
563 struct pnfs_layout_segment *lseg = NULL;
35124a09
WAA
564 struct page **pages = NULL;
565 int i;
566 u32 max_resp_sz, max_pages;
b1f69b75
AA
567
568 dprintk("--> %s\n", __func__);
e5e94017 569
b1f69b75 570 BUG_ON(ctx == NULL);
a75b9df9 571 lgp = kzalloc(sizeof(*lgp), gfp_flags);
cf7d63f1 572 if (lgp == NULL)
b1f69b75 573 return NULL;
35124a09
WAA
574
575 /* allocate pages for xdr post processing */
576 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
577 max_pages = max_resp_sz >> PAGE_SHIFT;
578
a75b9df9 579 pages = kzalloc(max_pages * sizeof(struct page *), gfp_flags);
35124a09
WAA
580 if (!pages)
581 goto out_err_free;
582
583 for (i = 0; i < max_pages; i++) {
a75b9df9 584 pages[i] = alloc_page(gfp_flags);
35124a09
WAA
585 if (!pages[i])
586 goto out_err_free;
587 }
588
fb3296eb
BH
589 lgp->args.minlength = PAGE_CACHE_SIZE;
590 if (lgp->args.minlength > range->length)
591 lgp->args.minlength = range->length;
b1f69b75 592 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
fb3296eb 593 lgp->args.range = *range;
b1f69b75
AA
594 lgp->args.type = server->pnfs_curr_ld->id;
595 lgp->args.inode = ino;
596 lgp->args.ctx = get_nfs_open_context(ctx);
35124a09
WAA
597 lgp->args.layout.pages = pages;
598 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
b1f69b75 599 lgp->lsegpp = &lseg;
a75b9df9 600 lgp->gfp_flags = gfp_flags;
b1f69b75
AA
601
602 /* Synchronously retrieve layout information from server and
603 * store in lseg.
604 */
605 nfs4_proc_layoutget(lgp);
974cec8c 606 if (!lseg) {
b1f69b75 607 /* remember that LAYOUTGET failed and suspend trying */
fb3296eb 608 set_bit(lo_fail_bit(range->iomode), &lo->plh_flags);
974cec8c 609 }
35124a09
WAA
610
611 /* free xdr pages */
612 for (i = 0; i < max_pages; i++)
613 __free_page(pages[i]);
614 kfree(pages);
615
974cec8c 616 return lseg;
35124a09
WAA
617
618out_err_free:
619 /* free any allocated xdr pages, lgp as it's not used */
620 if (pages) {
621 for (i = 0; i < max_pages; i++) {
622 if (!pages[i])
623 break;
624 __free_page(pages[i]);
625 }
626 kfree(pages);
627 }
628 kfree(lgp);
629 return NULL;
974cec8c
AA
630}
631
cbe82603
BH
632/* Initiates a LAYOUTRETURN(FILE) */
633int
634_pnfs_return_layout(struct inode *ino)
635{
636 struct pnfs_layout_hdr *lo = NULL;
637 struct nfs_inode *nfsi = NFS_I(ino);
638 LIST_HEAD(tmp_list);
639 struct nfs4_layoutreturn *lrp;
640 nfs4_stateid stateid;
641 int status = 0;
642
643 dprintk("--> %s\n", __func__);
644
645 spin_lock(&ino->i_lock);
646 lo = nfsi->layout;
a2e1d4f2 647 if (!lo) {
cbe82603 648 spin_unlock(&ino->i_lock);
a2e1d4f2
FI
649 dprintk("%s: no layout to return\n", __func__);
650 return status;
cbe82603
BH
651 }
652 stateid = nfsi->layout->plh_stateid;
653 /* Reference matched in nfs4_layoutreturn_release */
654 get_layout_hdr(lo);
ea0ded74
FI
655 mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
656 lo->plh_block_lgets++;
cbe82603
BH
657 spin_unlock(&ino->i_lock);
658 pnfs_free_lseg_list(&tmp_list);
659
660 WARN_ON(test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags));
661
662 lrp = kzalloc(sizeof(*lrp), GFP_KERNEL);
663 if (unlikely(lrp == NULL)) {
664 status = -ENOMEM;
9e2dfdb3
FI
665 set_bit(NFS_LAYOUT_RW_FAILED, &lo->plh_flags);
666 set_bit(NFS_LAYOUT_RO_FAILED, &lo->plh_flags);
1ed3a853 667 put_layout_hdr(lo);
cbe82603
BH
668 goto out;
669 }
670
671 lrp->args.stateid = stateid;
672 lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
673 lrp->args.inode = ino;
a56aaa02 674 lrp->args.layout = lo;
cbe82603
BH
675 lrp->clp = NFS_SERVER(ino)->nfs_client;
676
677 status = nfs4_proc_layoutreturn(lrp);
678out:
679 dprintk("<-- %s status: %d\n", __func__, status);
680 return status;
681}
682
f7e8917a
FI
683bool pnfs_roc(struct inode *ino)
684{
685 struct pnfs_layout_hdr *lo;
686 struct pnfs_layout_segment *lseg, *tmp;
687 LIST_HEAD(tmp_list);
688 bool found = false;
689
690 spin_lock(&ino->i_lock);
691 lo = NFS_I(ino)->layout;
692 if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
693 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
694 goto out_nolayout;
695 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
696 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
697 mark_lseg_invalid(lseg, &tmp_list);
698 found = true;
699 }
700 if (!found)
701 goto out_nolayout;
702 lo->plh_block_lgets++;
703 get_layout_hdr(lo); /* matched in pnfs_roc_release */
704 spin_unlock(&ino->i_lock);
705 pnfs_free_lseg_list(&tmp_list);
706 return true;
707
708out_nolayout:
709 spin_unlock(&ino->i_lock);
710 return false;
711}
712
713void pnfs_roc_release(struct inode *ino)
714{
715 struct pnfs_layout_hdr *lo;
716
717 spin_lock(&ino->i_lock);
718 lo = NFS_I(ino)->layout;
719 lo->plh_block_lgets--;
720 put_layout_hdr_locked(lo);
721 spin_unlock(&ino->i_lock);
722}
723
724void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
725{
726 struct pnfs_layout_hdr *lo;
727
728 spin_lock(&ino->i_lock);
729 lo = NFS_I(ino)->layout;
730 if ((int)(barrier - lo->plh_barrier) > 0)
731 lo->plh_barrier = barrier;
732 spin_unlock(&ino->i_lock);
733}
734
735bool pnfs_roc_drain(struct inode *ino, u32 *barrier)
736{
737 struct nfs_inode *nfsi = NFS_I(ino);
738 struct pnfs_layout_segment *lseg;
739 bool found = false;
740
741 spin_lock(&ino->i_lock);
742 list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
743 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
744 found = true;
745 break;
746 }
747 if (!found) {
748 struct pnfs_layout_hdr *lo = nfsi->layout;
749 u32 current_seqid = be32_to_cpu(lo->plh_stateid.stateid.seqid);
750
751 /* Since close does not return a layout stateid for use as
752 * a barrier, we choose the worst-case barrier.
753 */
754 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
755 }
756 spin_unlock(&ino->i_lock);
757 return found;
758}
759
b1f69b75
AA
760/*
761 * Compare two layout segments for sorting into layout cache.
762 * We want to preferentially return RW over RO layouts, so ensure those
763 * are seen first.
764 */
765static s64
fb3296eb
BH
766cmp_layout(struct pnfs_layout_range *l1,
767 struct pnfs_layout_range *l2)
b1f69b75 768{
fb3296eb
BH
769 s64 d;
770
771 /* high offset > low offset */
772 d = l1->offset - l2->offset;
773 if (d)
774 return d;
775
776 /* short length > long length */
777 d = l2->length - l1->length;
778 if (d)
779 return d;
780
b1f69b75 781 /* read > read/write */
fb3296eb 782 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
b1f69b75
AA
783}
784
974cec8c
AA
785static void
786pnfs_insert_layout(struct pnfs_layout_hdr *lo,
787 struct pnfs_layout_segment *lseg)
788{
b1f69b75 789 struct pnfs_layout_segment *lp;
b1f69b75 790
974cec8c
AA
791 dprintk("%s:Begin\n", __func__);
792
b7edfaa1 793 assert_spin_locked(&lo->plh_inode->i_lock);
b7edfaa1 794 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
fb3296eb 795 if (cmp_layout(&lseg->pls_range, &lp->pls_range) > 0)
b1f69b75 796 continue;
566052c5 797 list_add_tail(&lseg->pls_list, &lp->pls_list);
b1f69b75
AA
798 dprintk("%s: inserted lseg %p "
799 "iomode %d offset %llu length %llu before "
800 "lp %p iomode %d offset %llu length %llu\n",
566052c5
FI
801 __func__, lseg, lseg->pls_range.iomode,
802 lseg->pls_range.offset, lseg->pls_range.length,
803 lp, lp->pls_range.iomode, lp->pls_range.offset,
804 lp->pls_range.length);
fb3296eb 805 goto out;
974cec8c 806 }
fb3296eb
BH
807 list_add_tail(&lseg->pls_list, &lo->plh_segs);
808 dprintk("%s: inserted lseg %p "
809 "iomode %d offset %llu length %llu at tail\n",
810 __func__, lseg, lseg->pls_range.iomode,
811 lseg->pls_range.offset, lseg->pls_range.length);
812out:
cc6e5340 813 get_layout_hdr(lo);
974cec8c
AA
814
815 dprintk("%s:Return\n", __func__);
e5e94017
BH
816}
817
818static struct pnfs_layout_hdr *
a75b9df9 819alloc_init_layout_hdr(struct inode *ino, gfp_t gfp_flags)
e5e94017
BH
820{
821 struct pnfs_layout_hdr *lo;
822
636fb9c8 823 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
e5e94017
BH
824 if (!lo)
825 return NULL;
cc6e5340 826 atomic_set(&lo->plh_refcount, 1);
b7edfaa1
FI
827 INIT_LIST_HEAD(&lo->plh_layouts);
828 INIT_LIST_HEAD(&lo->plh_segs);
43f1b3da 829 INIT_LIST_HEAD(&lo->plh_bulk_recall);
b7edfaa1 830 lo->plh_inode = ino;
e5e94017
BH
831 return lo;
832}
833
834static struct pnfs_layout_hdr *
a75b9df9 835pnfs_find_alloc_layout(struct inode *ino, gfp_t gfp_flags)
e5e94017
BH
836{
837 struct nfs_inode *nfsi = NFS_I(ino);
838 struct pnfs_layout_hdr *new = NULL;
839
840 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
841
842 assert_spin_locked(&ino->i_lock);
4541d16c
FI
843 if (nfsi->layout) {
844 if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
845 return NULL;
846 else
847 return nfsi->layout;
848 }
e5e94017 849 spin_unlock(&ino->i_lock);
a75b9df9 850 new = alloc_init_layout_hdr(ino, gfp_flags);
e5e94017
BH
851 spin_lock(&ino->i_lock);
852
853 if (likely(nfsi->layout == NULL)) /* Won the race? */
854 nfsi->layout = new;
855 else
636fb9c8 856 pnfs_free_layout_hdr(new);
e5e94017
BH
857 return nfsi->layout;
858}
859
b1f69b75
AA
860/*
861 * iomode matching rules:
862 * iomode lseg match
863 * ----- ----- -----
864 * ANY READ true
865 * ANY RW true
866 * RW READ false
867 * RW RW true
868 * READ READ true
869 * READ RW true
870 */
871static int
fb3296eb
BH
872is_matching_lseg(struct pnfs_layout_range *ls_range,
873 struct pnfs_layout_range *range)
b1f69b75 874{
fb3296eb
BH
875 struct pnfs_layout_range range1;
876
877 if ((range->iomode == IOMODE_RW &&
878 ls_range->iomode != IOMODE_RW) ||
879 !lo_seg_intersecting(ls_range, range))
880 return 0;
881
882 /* range1 covers only the first byte in the range */
883 range1 = *range;
884 range1.length = 1;
885 return lo_seg_contained(ls_range, &range1);
b1f69b75
AA
886}
887
888/*
889 * lookup range in layout
890 */
e5e94017 891static struct pnfs_layout_segment *
fb3296eb
BH
892pnfs_find_lseg(struct pnfs_layout_hdr *lo,
893 struct pnfs_layout_range *range)
e5e94017 894{
b1f69b75
AA
895 struct pnfs_layout_segment *lseg, *ret = NULL;
896
897 dprintk("%s:Begin\n", __func__);
898
b7edfaa1
FI
899 assert_spin_locked(&lo->plh_inode->i_lock);
900 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
4541d16c 901 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
fb3296eb 902 is_matching_lseg(&lseg->pls_range, range)) {
d684d2ae 903 ret = get_lseg(lseg);
b1f69b75
AA
904 break;
905 }
d771e3a4 906 if (lseg->pls_range.offset > range->offset)
b1f69b75
AA
907 break;
908 }
909
910 dprintk("%s:Return lseg %p ref %d\n",
4541d16c 911 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
b1f69b75 912 return ret;
e5e94017
BH
913}
914
915/*
916 * Layout segment is retreived from the server if not cached.
917 * The appropriate layout segment is referenced and returned to the caller.
918 */
7c24d948 919struct pnfs_layout_segment *
e5e94017
BH
920pnfs_update_layout(struct inode *ino,
921 struct nfs_open_context *ctx,
fb3296eb
BH
922 loff_t pos,
923 u64 count,
a75b9df9
TM
924 enum pnfs_iomode iomode,
925 gfp_t gfp_flags)
e5e94017 926{
fb3296eb
BH
927 struct pnfs_layout_range arg = {
928 .iomode = iomode,
929 .offset = pos,
930 .length = count,
931 };
707ed5fd 932 unsigned pg_offset;
e5e94017 933 struct nfs_inode *nfsi = NFS_I(ino);
6382a441
WAA
934 struct nfs_server *server = NFS_SERVER(ino);
935 struct nfs_client *clp = server->nfs_client;
e5e94017
BH
936 struct pnfs_layout_hdr *lo;
937 struct pnfs_layout_segment *lseg = NULL;
f49f9baa 938 bool first = false;
e5e94017
BH
939
940 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
941 return NULL;
942 spin_lock(&ino->i_lock);
a75b9df9 943 lo = pnfs_find_alloc_layout(ino, gfp_flags);
e5e94017
BH
944 if (lo == NULL) {
945 dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
946 goto out_unlock;
947 }
948
43f1b3da
FI
949 /* Do we even need to bother with this? */
950 if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
951 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
952 dprintk("%s matches recall, use MDS\n", __func__);
e5e94017
BH
953 goto out_unlock;
954 }
955
956 /* if LAYOUTGET already failed once we don't try again */
566052c5 957 if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
e5e94017
BH
958 goto out_unlock;
959
568e8c49 960 /* Check to see if the layout for the given range already exists */
fb3296eb 961 lseg = pnfs_find_lseg(lo, &arg);
568e8c49
AA
962 if (lseg)
963 goto out_unlock;
964
43f1b3da 965 if (pnfs_layoutgets_blocked(lo, NULL, 0))
cf7d63f1
FI
966 goto out_unlock;
967 atomic_inc(&lo->plh_outstanding);
968
cc6e5340 969 get_layout_hdr(lo);
f49f9baa
FI
970 if (list_empty(&lo->plh_segs))
971 first = true;
972 spin_unlock(&ino->i_lock);
973 if (first) {
2130ff66
FI
974 /* The lo must be on the clp list if there is any
975 * chance of a CB_LAYOUTRECALL(FILE) coming in.
976 */
977 spin_lock(&clp->cl_lock);
978 BUG_ON(!list_empty(&lo->plh_layouts));
6382a441 979 list_add_tail(&lo->plh_layouts, &server->layouts);
2130ff66
FI
980 spin_unlock(&clp->cl_lock);
981 }
e5e94017 982
707ed5fd
BH
983 pg_offset = arg.offset & ~PAGE_CACHE_MASK;
984 if (pg_offset) {
985 arg.offset -= pg_offset;
986 arg.length += pg_offset;
987 }
7c24d948
AA
988 if (arg.length != NFS4_MAX_UINT64)
989 arg.length = PAGE_CACHE_ALIGN(arg.length);
707ed5fd 990
fb3296eb 991 lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
f49f9baa
FI
992 if (!lseg && first) {
993 spin_lock(&clp->cl_lock);
994 list_del_init(&lo->plh_layouts);
995 spin_unlock(&clp->cl_lock);
2130ff66 996 }
cf7d63f1 997 atomic_dec(&lo->plh_outstanding);
cc6e5340 998 put_layout_hdr(lo);
e5e94017
BH
999out:
1000 dprintk("%s end, state 0x%lx lseg %p\n", __func__,
bf9c1387 1001 nfsi->layout ? nfsi->layout->plh_flags : -1, lseg);
e5e94017
BH
1002 return lseg;
1003out_unlock:
1004 spin_unlock(&ino->i_lock);
1005 goto out;
1006}
7c24d948 1007EXPORT_SYMBOL_GPL(pnfs_update_layout);
b1f69b75
AA
1008
1009int
1010pnfs_layout_process(struct nfs4_layoutget *lgp)
1011{
1012 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
1013 struct nfs4_layoutget_res *res = &lgp->res;
1014 struct pnfs_layout_segment *lseg;
b7edfaa1 1015 struct inode *ino = lo->plh_inode;
43f1b3da 1016 struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
b1f69b75
AA
1017 int status = 0;
1018
1019 /* Inject layout blob into I/O device driver */
a75b9df9 1020 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
b1f69b75
AA
1021 if (!lseg || IS_ERR(lseg)) {
1022 if (!lseg)
1023 status = -ENOMEM;
1024 else
1025 status = PTR_ERR(lseg);
1026 dprintk("%s: Could not allocate layout: error %d\n",
1027 __func__, status);
1028 goto out;
1029 }
1030
1031 spin_lock(&ino->i_lock);
43f1b3da
FI
1032 if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
1033 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1034 dprintk("%s forget reply due to recall\n", __func__);
1035 goto out_forget_reply;
1036 }
1037
1038 if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) {
1039 dprintk("%s forget reply due to state\n", __func__);
1040 goto out_forget_reply;
1041 }
b1f69b75 1042 init_lseg(lo, lseg);
566052c5 1043 lseg->pls_range = res->range;
d684d2ae 1044 *lgp->lsegpp = get_lseg(lseg);
b1f69b75
AA
1045 pnfs_insert_layout(lo, lseg);
1046
f7e8917a
FI
1047 if (res->return_on_close) {
1048 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
1049 set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
1050 }
1051
b1f69b75 1052 /* Done processing layoutget. Set the layout stateid */
43f1b3da 1053 pnfs_set_layout_stateid(lo, &res->stateid, false);
b1f69b75
AA
1054 spin_unlock(&ino->i_lock);
1055out:
1056 return status;
43f1b3da
FI
1057
1058out_forget_reply:
1059 spin_unlock(&ino->i_lock);
1060 lseg->pls_layout = lo;
1061 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
1062 goto out;
b1f69b75
AA
1063}
1064
d8007d4d
TM
1065void
1066pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1067{
1068 BUG_ON(pgio->pg_lseg != NULL);
1069
1070 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1071 req->wb_context,
1072 req_offset(req),
1073 req->wb_bytes,
1074 IOMODE_READ,
1075 GFP_KERNEL);
e885de1a
TM
1076 /* If no lseg, fall back to read through mds */
1077 if (pgio->pg_lseg == NULL)
1078 nfs_pageio_init_read_mds(pgio, pgio->pg_inode);
1079
d8007d4d
TM
1080}
1081EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
1082
1083void
1084pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1085{
1086 BUG_ON(pgio->pg_lseg != NULL);
1087
1088 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1089 req->wb_context,
1090 req_offset(req),
1091 req->wb_bytes,
1092 IOMODE_RW,
1093 GFP_NOFS);
e885de1a
TM
1094 /* If no lseg, fall back to write through mds */
1095 if (pgio->pg_lseg == NULL)
1096 nfs_pageio_init_write_mds(pgio, pgio->pg_inode, pgio->pg_ioflags);
d8007d4d
TM
1097}
1098EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
1099
1751c363
TM
1100bool
1101pnfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, struct inode *inode)
1102{
1103 struct nfs_server *server = NFS_SERVER(inode);
1104 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
1105
1106 if (ld == NULL)
1107 return false;
1108 nfs_pageio_init(pgio, inode, ld->pg_read_ops, server->rsize, 0);
1109 return true;
1110}
1111
1112bool
1113pnfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, struct inode *inode, int ioflags)
1114{
1115 struct nfs_server *server = NFS_SERVER(inode);
1116 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
1117
1118 if (ld == NULL)
1119 return false;
1120 nfs_pageio_init(pgio, inode, ld->pg_write_ops, server->wsize, ioflags);
1121 return true;
1122}
1123
18ad0a9f 1124bool
dfed206b
BH
1125pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
1126 struct nfs_page *req)
94ad1c80 1127{
d8007d4d
TM
1128 if (pgio->pg_lseg == NULL)
1129 return nfs_generic_pg_test(pgio, prev, req);
94ad1c80 1130
19982ba8
TM
1131 /*
1132 * Test if a nfs_page is fully contained in the pnfs_layout_range.
1133 * Note that this test makes several assumptions:
1134 * - that the previous nfs_page in the struct nfs_pageio_descriptor
1135 * is known to lie within the range.
1136 * - that the nfs_page being tested is known to be contiguous with the
1137 * previous nfs_page.
1138 * - Layout ranges are page aligned, so we only have to test the
1139 * start offset of the request.
1140 *
1141 * Please also note that 'end_offset' is actually the offset of the
1142 * first byte that lies outside the pnfs_layout_range. FIXME?
1143 *
1144 */
1145 return req_offset(req) < end_offset(pgio->pg_lseg->pls_range.offset,
1146 pgio->pg_lseg->pls_range.length);
94ad1c80 1147}
89a58e32 1148EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
94ad1c80 1149
d20581aa
BH
1150/*
1151 * Called by non rpc-based layout drivers
1152 */
1153int
1154pnfs_ld_write_done(struct nfs_write_data *data)
44b83799 1155{
d20581aa 1156 int status;
44b83799 1157
d20581aa
BH
1158 if (!data->pnfs_error) {
1159 pnfs_set_layoutcommit(data);
1160 data->mds_ops->rpc_call_done(&data->task, data);
1161 data->mds_ops->rpc_release(data);
1162 return 0;
1163 }
44b83799 1164
d20581aa
BH
1165 dprintk("%s: pnfs_error=%d, retry via MDS\n", __func__,
1166 data->pnfs_error);
1167 status = nfs_initiate_write(data, NFS_CLIENT(data->inode),
1168 data->mds_ops, NFS_FILE_SYNC);
1169 return status ? : -EAGAIN;
44b83799 1170}
d20581aa 1171EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
44b83799 1172
dce81290
TM
1173static void
1174pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
1175 struct nfs_write_data *data)
1176{
1177 list_splice_tail_init(&data->pages, &desc->pg_list);
1178 if (data->req && list_empty(&data->req->wb_list))
1179 nfs_list_add_request(data->req, &desc->pg_list);
1180 nfs_pageio_reset_write_mds(desc);
1181 desc->pg_recoalesce = 1;
1182 nfs_writedata_release(data);
1183}
1184
1185static enum pnfs_try_status
0382b744 1186pnfs_try_to_write_data(struct nfs_write_data *wdata,
dce81290
TM
1187 const struct rpc_call_ops *call_ops,
1188 struct pnfs_layout_segment *lseg,
1189 int how)
0382b744
AA
1190{
1191 struct inode *inode = wdata->inode;
1192 enum pnfs_try_status trypnfs;
1193 struct nfs_server *nfss = NFS_SERVER(inode);
1194
1195 wdata->mds_ops = call_ops;
dce81290 1196 wdata->lseg = get_lseg(lseg);
0382b744
AA
1197
1198 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
1199 inode->i_ino, wdata->args.count, wdata->args.offset, how);
1200
1201 trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how);
1202 if (trypnfs == PNFS_NOT_ATTEMPTED) {
1203 put_lseg(wdata->lseg);
1204 wdata->lseg = NULL;
1205 } else
1206 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
1207
1208 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1209 return trypnfs;
1210}
1211
dce81290
TM
1212static void
1213pnfs_do_multiple_writes(struct nfs_pageio_descriptor *desc, struct list_head *head, int how)
1214{
1215 struct nfs_write_data *data;
1216 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1217 struct pnfs_layout_segment *lseg = desc->pg_lseg;
1218
1219 desc->pg_lseg = NULL;
1220 while (!list_empty(head)) {
1221 enum pnfs_try_status trypnfs;
1222
1223 data = list_entry(head->next, struct nfs_write_data, list);
1224 list_del_init(&data->list);
1225
1226 trypnfs = pnfs_try_to_write_data(data, call_ops, lseg, how);
1227 if (trypnfs == PNFS_NOT_ATTEMPTED)
1228 pnfs_write_through_mds(desc, data);
1229 }
1230 put_lseg(lseg);
1231}
1232
1233int
1234pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
1235{
1236 LIST_HEAD(head);
1237 int ret;
1238
1239 ret = nfs_generic_flush(desc, &head);
1240 if (ret != 0) {
1241 put_lseg(desc->pg_lseg);
1242 desc->pg_lseg = NULL;
1243 return ret;
1244 }
1245 pnfs_do_multiple_writes(desc, &head, desc->pg_ioflags);
1246 return 0;
1247}
1248EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
1249
d20581aa
BH
1250/*
1251 * Called by non rpc-based layout drivers
1252 */
1253int
1254pnfs_ld_read_done(struct nfs_read_data *data)
1255{
1256 int status;
1257
1258 if (!data->pnfs_error) {
1259 __nfs4_read_done_cb(data);
1260 data->mds_ops->rpc_call_done(&data->task, data);
1261 data->mds_ops->rpc_release(data);
1262 return 0;
1263 }
1264
1265 dprintk("%s: pnfs_error=%d, retry via MDS\n", __func__,
1266 data->pnfs_error);
1267 status = nfs_initiate_read(data, NFS_CLIENT(data->inode),
1268 data->mds_ops);
1269 return status ? : -EAGAIN;
1270}
1271EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
1272
493292dd
TM
1273static void
1274pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
1275 struct nfs_read_data *data)
1276{
1277 list_splice_tail_init(&data->pages, &desc->pg_list);
1278 if (data->req && list_empty(&data->req->wb_list))
1279 nfs_list_add_request(data->req, &desc->pg_list);
1280 nfs_pageio_reset_read_mds(desc);
1281 desc->pg_recoalesce = 1;
1282 nfs_readdata_release(data);
1283}
1284
64419a9b
AA
1285/*
1286 * Call the appropriate parallel I/O subsystem read function.
1287 */
493292dd 1288static enum pnfs_try_status
64419a9b 1289pnfs_try_to_read_data(struct nfs_read_data *rdata,
493292dd
TM
1290 const struct rpc_call_ops *call_ops,
1291 struct pnfs_layout_segment *lseg)
64419a9b
AA
1292{
1293 struct inode *inode = rdata->inode;
1294 struct nfs_server *nfss = NFS_SERVER(inode);
1295 enum pnfs_try_status trypnfs;
1296
1297 rdata->mds_ops = call_ops;
493292dd 1298 rdata->lseg = get_lseg(lseg);
64419a9b
AA
1299
1300 dprintk("%s: Reading ino:%lu %u@%llu\n",
1301 __func__, inode->i_ino, rdata->args.count, rdata->args.offset);
1302
1303 trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata);
1304 if (trypnfs == PNFS_NOT_ATTEMPTED) {
1305 put_lseg(rdata->lseg);
1306 rdata->lseg = NULL;
1307 } else {
1308 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
1309 }
1310 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1311 return trypnfs;
1312}
863a3c6c 1313
493292dd
TM
1314static void
1315pnfs_do_multiple_reads(struct nfs_pageio_descriptor *desc, struct list_head *head)
1316{
1317 struct nfs_read_data *data;
1318 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1319 struct pnfs_layout_segment *lseg = desc->pg_lseg;
1320
1321 desc->pg_lseg = NULL;
1322 while (!list_empty(head)) {
1323 enum pnfs_try_status trypnfs;
1324
1325 data = list_entry(head->next, struct nfs_read_data, list);
1326 list_del_init(&data->list);
1327
1328 trypnfs = pnfs_try_to_read_data(data, call_ops, lseg);
1329 if (trypnfs == PNFS_NOT_ATTEMPTED)
1330 pnfs_read_through_mds(desc, data);
1331 }
1332 put_lseg(lseg);
1333}
1334
1335int
1336pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
1337{
1338 LIST_HEAD(head);
1339 int ret;
1340
1341 ret = nfs_generic_pagein(desc, &head);
1342 if (ret != 0) {
1343 put_lseg(desc->pg_lseg);
1344 desc->pg_lseg = NULL;
1345 return ret;
1346 }
1347 pnfs_do_multiple_reads(desc, &head);
1348 return 0;
1349}
1350EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
1351
863a3c6c
AA
1352/*
1353 * Currently there is only one (whole file) write lseg.
1354 */
1355static struct pnfs_layout_segment *pnfs_list_write_lseg(struct inode *inode)
1356{
1357 struct pnfs_layout_segment *lseg, *rv = NULL;
1358
1359 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list)
1360 if (lseg->pls_range.iomode == IOMODE_RW)
1361 rv = lseg;
1362 return rv;
1363}
1364
1365void
1366pnfs_set_layoutcommit(struct nfs_write_data *wdata)
1367{
1368 struct nfs_inode *nfsi = NFS_I(wdata->inode);
4b8ee2b8 1369 loff_t end_pos = wdata->mds_offset + wdata->res.count;
79a48a1f 1370 bool mark_as_dirty = false;
863a3c6c
AA
1371
1372 spin_lock(&nfsi->vfs_inode.i_lock);
1373 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1374 /* references matched in nfs4_layoutcommit_release */
1375 get_lseg(wdata->lseg);
1376 wdata->lseg->pls_lc_cred =
1377 get_rpccred(wdata->args.context->state->owner->so_cred);
79a48a1f 1378 mark_as_dirty = true;
863a3c6c
AA
1379 dprintk("%s: Set layoutcommit for inode %lu ",
1380 __func__, wdata->inode->i_ino);
1381 }
1382 if (end_pos > wdata->lseg->pls_end_pos)
1383 wdata->lseg->pls_end_pos = end_pos;
1384 spin_unlock(&nfsi->vfs_inode.i_lock);
79a48a1f
WAA
1385
1386 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
1387 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
1388 if (mark_as_dirty)
1389 mark_inode_dirty_sync(wdata->inode);
863a3c6c
AA
1390}
1391EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
1392
de4b15c7
AA
1393/*
1394 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
1395 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
1396 * data to disk to allow the server to recover the data if it crashes.
1397 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
1398 * is off, and a COMMIT is sent to a data server, or
1399 * if WRITEs to a data server return NFS_DATA_SYNC.
1400 */
863a3c6c 1401int
ef311537 1402pnfs_layoutcommit_inode(struct inode *inode, bool sync)
863a3c6c
AA
1403{
1404 struct nfs4_layoutcommit_data *data;
1405 struct nfs_inode *nfsi = NFS_I(inode);
1406 struct pnfs_layout_segment *lseg;
1407 struct rpc_cred *cred;
1408 loff_t end_pos;
1409 int status = 0;
1410
1411 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
1412
de4b15c7
AA
1413 if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
1414 return 0;
1415
863a3c6c
AA
1416 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
1417 data = kzalloc(sizeof(*data), GFP_NOFS);
de4b15c7
AA
1418 if (!data) {
1419 mark_inode_dirty_sync(inode);
1420 status = -ENOMEM;
1421 goto out;
1422 }
863a3c6c 1423
de4b15c7 1424 spin_lock(&inode->i_lock);
863a3c6c
AA
1425 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1426 spin_unlock(&inode->i_lock);
1427 kfree(data);
1428 goto out;
1429 }
1430 /*
1431 * Currently only one (whole file) write lseg which is referenced
1432 * in pnfs_set_layoutcommit and will be found.
1433 */
1434 lseg = pnfs_list_write_lseg(inode);
1435
1436 end_pos = lseg->pls_end_pos;
1437 cred = lseg->pls_lc_cred;
1438 lseg->pls_end_pos = 0;
1439 lseg->pls_lc_cred = NULL;
1440
de4b15c7
AA
1441 memcpy(&data->args.stateid.data, nfsi->layout->plh_stateid.data,
1442 sizeof(nfsi->layout->plh_stateid.data));
863a3c6c
AA
1443 spin_unlock(&inode->i_lock);
1444
1445 data->args.inode = inode;
1446 data->lseg = lseg;
1447 data->cred = cred;
1448 nfs_fattr_init(&data->fattr);
1449 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
1450 data->res.fattr = &data->fattr;
1451 data->args.lastbytewritten = end_pos - 1;
1452 data->res.server = NFS_SERVER(inode);
1453
1454 status = nfs4_proc_layoutcommit(data, sync);
1455out:
1456 dprintk("<-- %s status %d\n", __func__, status);
1457 return status;
1458}