]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/nfs/pnfs.c
pnfs-obj: define per-inode private structure
[mirror_ubuntu-bionic-kernel.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>
974cec8c 31#include "internal.h"
85e174ba 32#include "pnfs.h"
64419a9b 33#include "iostat.h"
85e174ba
RL
34
35#define NFSDBG_FACILITY NFSDBG_PNFS
36
02c35fca
FI
37/* Locking:
38 *
39 * pnfs_spinlock:
40 * protects pnfs_modules_tbl.
41 */
42static DEFINE_SPINLOCK(pnfs_spinlock);
43
44/*
45 * pnfs_modules_tbl holds all pnfs modules
46 */
47static LIST_HEAD(pnfs_modules_tbl);
48
49/* Return the registered pnfs layout driver module matching given id */
50static struct pnfs_layoutdriver_type *
51find_pnfs_driver_locked(u32 id)
52{
53 struct pnfs_layoutdriver_type *local;
54
55 list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
56 if (local->id == id)
57 goto out;
58 local = NULL;
59out:
60 dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
61 return local;
62}
63
85e174ba
RL
64static struct pnfs_layoutdriver_type *
65find_pnfs_driver(u32 id)
66{
02c35fca
FI
67 struct pnfs_layoutdriver_type *local;
68
69 spin_lock(&pnfs_spinlock);
70 local = find_pnfs_driver_locked(id);
71 spin_unlock(&pnfs_spinlock);
72 return local;
85e174ba
RL
73}
74
75void
76unset_pnfs_layoutdriver(struct nfs_server *nfss)
77{
ea8eecdd 78 if (nfss->pnfs_curr_ld)
02c35fca 79 module_put(nfss->pnfs_curr_ld->owner);
85e174ba
RL
80 nfss->pnfs_curr_ld = NULL;
81}
82
83/*
84 * Try to set the server's pnfs module to the pnfs layout type specified by id.
85 * Currently only one pNFS layout driver per filesystem is supported.
86 *
87 * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
88 */
89void
90set_pnfs_layoutdriver(struct nfs_server *server, u32 id)
91{
92 struct pnfs_layoutdriver_type *ld_type = NULL;
93
94 if (id == 0)
95 goto out_no_driver;
96 if (!(server->nfs_client->cl_exchange_flags &
97 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
98 printk(KERN_ERR "%s: id %u cl_exchange_flags 0x%x\n", __func__,
99 id, server->nfs_client->cl_exchange_flags);
100 goto out_no_driver;
101 }
102 ld_type = find_pnfs_driver(id);
103 if (!ld_type) {
104 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
105 ld_type = find_pnfs_driver(id);
106 if (!ld_type) {
107 dprintk("%s: No pNFS module found for %u.\n",
108 __func__, id);
109 goto out_no_driver;
110 }
111 }
02c35fca
FI
112 if (!try_module_get(ld_type->owner)) {
113 dprintk("%s: Could not grab reference on module\n", __func__);
114 goto out_no_driver;
115 }
85e174ba 116 server->pnfs_curr_ld = ld_type;
ea8eecdd 117
85e174ba
RL
118 dprintk("%s: pNFS module for %u set\n", __func__, id);
119 return;
120
121out_no_driver:
122 dprintk("%s: Using NFSv4 I/O\n", __func__);
123 server->pnfs_curr_ld = NULL;
124}
02c35fca
FI
125
126int
127pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
128{
129 int status = -EINVAL;
130 struct pnfs_layoutdriver_type *tmp;
131
132 if (ld_type->id == 0) {
133 printk(KERN_ERR "%s id 0 is reserved\n", __func__);
134 return status;
135 }
b1f69b75
AA
136 if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
137 printk(KERN_ERR "%s Layout driver must provide "
138 "alloc_lseg and free_lseg.\n", __func__);
139 return status;
140 }
02c35fca
FI
141
142 spin_lock(&pnfs_spinlock);
143 tmp = find_pnfs_driver_locked(ld_type->id);
144 if (!tmp) {
145 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
146 status = 0;
147 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
148 ld_type->name);
149 } else {
150 printk(KERN_ERR "%s Module with id %d already loaded!\n",
151 __func__, ld_type->id);
152 }
153 spin_unlock(&pnfs_spinlock);
154
155 return status;
156}
157EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
158
159void
160pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
161{
162 dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
163 spin_lock(&pnfs_spinlock);
164 list_del(&ld_type->pnfs_tblid);
165 spin_unlock(&pnfs_spinlock);
166}
167EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
e5e94017 168
b1f69b75
AA
169/*
170 * pNFS client layout cache
171 */
172
cc6e5340 173/* Need to hold i_lock if caller does not already hold reference */
43f1b3da 174void
cc6e5340 175get_layout_hdr(struct pnfs_layout_hdr *lo)
e5e94017 176{
cc6e5340 177 atomic_inc(&lo->plh_refcount);
e5e94017
BH
178}
179
636fb9c8
BH
180static struct pnfs_layout_hdr *
181pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
182{
183 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
184 return ld->alloc_layout_hdr ? ld->alloc_layout_hdr(ino, gfp_flags) :
185 kzalloc(sizeof(struct pnfs_layout_hdr), gfp_flags);
186}
187
188static void
189pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
190{
191 struct pnfs_layoutdriver_type *ld = NFS_SERVER(lo->plh_inode)->pnfs_curr_ld;
192 return ld->alloc_layout_hdr ? ld->free_layout_hdr(lo) : kfree(lo);
193}
194
e5e94017 195static void
cc6e5340 196destroy_layout_hdr(struct pnfs_layout_hdr *lo)
e5e94017 197{
cc6e5340
FI
198 dprintk("%s: freeing layout cache %p\n", __func__, lo);
199 BUG_ON(!list_empty(&lo->plh_layouts));
200 NFS_I(lo->plh_inode)->layout = NULL;
636fb9c8 201 pnfs_free_layout_hdr(lo);
cc6e5340 202}
e5e94017 203
cc6e5340
FI
204static void
205put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
206{
207 if (atomic_dec_and_test(&lo->plh_refcount))
208 destroy_layout_hdr(lo);
e5e94017
BH
209}
210
b1f69b75 211void
cc6e5340 212put_layout_hdr(struct pnfs_layout_hdr *lo)
974cec8c 213{
cc6e5340
FI
214 struct inode *inode = lo->plh_inode;
215
216 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
217 destroy_layout_hdr(lo);
218 spin_unlock(&inode->i_lock);
219 }
974cec8c
AA
220}
221
222static void
223init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
224{
566052c5 225 INIT_LIST_HEAD(&lseg->pls_list);
4541d16c
FI
226 atomic_set(&lseg->pls_refcount, 1);
227 smp_mb();
228 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
566052c5 229 lseg->pls_layout = lo;
974cec8c
AA
230}
231
4541d16c 232static void free_lseg(struct pnfs_layout_segment *lseg)
974cec8c 233{
b7edfaa1 234 struct inode *ino = lseg->pls_layout->plh_inode;
974cec8c 235
b1f69b75 236 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
52fabd73 237 /* Matched by get_layout_hdr in pnfs_insert_layout */
cc6e5340 238 put_layout_hdr(NFS_I(ino)->layout);
974cec8c
AA
239}
240
d684d2ae
FI
241static void
242put_lseg_common(struct pnfs_layout_segment *lseg)
243{
244 struct inode *inode = lseg->pls_layout->plh_inode;
245
246 BUG_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
247 list_del_init(&lseg->pls_list);
248 if (list_empty(&lseg->pls_layout->plh_segs)) {
249 set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags);
250 /* Matched by initial refcount set in alloc_init_layout_hdr */
251 put_layout_hdr_locked(lseg->pls_layout);
252 }
253 rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
254}
255
bae724ef 256void
d684d2ae 257put_lseg(struct pnfs_layout_segment *lseg)
974cec8c 258{
d684d2ae
FI
259 struct inode *inode;
260
261 if (!lseg)
262 return;
263
4541d16c
FI
264 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
265 atomic_read(&lseg->pls_refcount),
266 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
d684d2ae
FI
267 inode = lseg->pls_layout->plh_inode;
268 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
269 LIST_HEAD(free_me);
4541d16c 270
d684d2ae
FI
271 put_lseg_common(lseg);
272 list_add(&lseg->pls_list, &free_me);
273 spin_unlock(&inode->i_lock);
274 pnfs_free_lseg_list(&free_me);
4541d16c 275 }
4541d16c 276}
e0c2b380 277EXPORT_SYMBOL_GPL(put_lseg);
974cec8c 278
fb3296eb
BH
279static inline u64
280end_offset(u64 start, u64 len)
281{
282 u64 end;
283
284 end = start + len;
285 return end >= start ? end : NFS4_MAX_UINT64;
286}
287
288/* last octet in a range */
289static inline u64
290last_byte_offset(u64 start, u64 len)
291{
292 u64 end;
293
294 BUG_ON(!len);
295 end = start + len;
296 return end > start ? end - 1 : NFS4_MAX_UINT64;
297}
298
299/*
300 * is l2 fully contained in l1?
301 * start1 end1
302 * [----------------------------------)
303 * start2 end2
304 * [----------------)
305 */
306static inline int
307lo_seg_contained(struct pnfs_layout_range *l1,
308 struct pnfs_layout_range *l2)
309{
310 u64 start1 = l1->offset;
311 u64 end1 = end_offset(start1, l1->length);
312 u64 start2 = l2->offset;
313 u64 end2 = end_offset(start2, l2->length);
314
315 return (start1 <= start2) && (end1 >= end2);
316}
317
318/*
319 * is l1 and l2 intersecting?
320 * start1 end1
321 * [----------------------------------)
322 * start2 end2
323 * [----------------)
324 */
325static inline int
326lo_seg_intersecting(struct pnfs_layout_range *l1,
327 struct pnfs_layout_range *l2)
328{
329 u64 start1 = l1->offset;
330 u64 end1 = end_offset(start1, l1->length);
331 u64 start2 = l2->offset;
332 u64 end2 = end_offset(start2, l2->length);
333
334 return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
335 (end2 == NFS4_MAX_UINT64 || end2 > start1);
336}
337
4541d16c 338static bool
778b5502
BH
339should_free_lseg(struct pnfs_layout_range *lseg_range,
340 struct pnfs_layout_range *recall_range)
4541d16c 341{
778b5502
BH
342 return (recall_range->iomode == IOMODE_ANY ||
343 lseg_range->iomode == recall_range->iomode) &&
344 lo_seg_intersecting(lseg_range, recall_range);
974cec8c
AA
345}
346
4541d16c
FI
347/* Returns 1 if lseg is removed from list, 0 otherwise */
348static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
349 struct list_head *tmp_list)
350{
351 int rv = 0;
352
353 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
354 /* Remove the reference keeping the lseg in the
355 * list. It will now be removed when all
356 * outstanding io is finished.
357 */
d684d2ae
FI
358 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
359 atomic_read(&lseg->pls_refcount));
360 if (atomic_dec_and_test(&lseg->pls_refcount)) {
361 put_lseg_common(lseg);
362 list_add(&lseg->pls_list, tmp_list);
363 rv = 1;
364 }
4541d16c
FI
365 }
366 return rv;
367}
368
369/* Returns count of number of matching invalid lsegs remaining in list
370 * after call.
371 */
43f1b3da 372int
4541d16c
FI
373mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
374 struct list_head *tmp_list,
778b5502 375 struct pnfs_layout_range *recall_range)
974cec8c
AA
376{
377 struct pnfs_layout_segment *lseg, *next;
4541d16c 378 int invalid = 0, removed = 0;
974cec8c
AA
379
380 dprintk("%s:Begin lo %p\n", __func__, lo);
381
38511722
FI
382 if (list_empty(&lo->plh_segs)) {
383 if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags))
384 put_layout_hdr_locked(lo);
385 return 0;
386 }
4541d16c 387 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
778b5502
BH
388 if (!recall_range ||
389 should_free_lseg(&lseg->pls_range, recall_range)) {
4541d16c
FI
390 dprintk("%s: freeing lseg %p iomode %d "
391 "offset %llu length %llu\n", __func__,
392 lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
393 lseg->pls_range.length);
394 invalid++;
395 removed += mark_lseg_invalid(lseg, tmp_list);
396 }
397 dprintk("%s:Return %i\n", __func__, invalid - removed);
398 return invalid - removed;
974cec8c
AA
399}
400
f49f9baa 401/* note free_me must contain lsegs from a single layout_hdr */
43f1b3da 402void
4541d16c 403pnfs_free_lseg_list(struct list_head *free_me)
974cec8c 404{
4541d16c 405 struct pnfs_layout_segment *lseg, *tmp;
f49f9baa
FI
406 struct pnfs_layout_hdr *lo;
407
408 if (list_empty(free_me))
409 return;
410
411 lo = list_first_entry(free_me, struct pnfs_layout_segment,
412 pls_list)->pls_layout;
974cec8c 413
f49f9baa
FI
414 if (test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) {
415 struct nfs_client *clp;
416
417 clp = NFS_SERVER(lo->plh_inode)->nfs_client;
418 spin_lock(&clp->cl_lock);
419 list_del_init(&lo->plh_layouts);
420 spin_unlock(&clp->cl_lock);
421 }
4541d16c 422 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
566052c5 423 list_del(&lseg->pls_list);
4541d16c 424 free_lseg(lseg);
974cec8c
AA
425 }
426}
427
e5e94017
BH
428void
429pnfs_destroy_layout(struct nfs_inode *nfsi)
430{
431 struct pnfs_layout_hdr *lo;
974cec8c 432 LIST_HEAD(tmp_list);
e5e94017
BH
433
434 spin_lock(&nfsi->vfs_inode.i_lock);
435 lo = nfsi->layout;
436 if (lo) {
38511722 437 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
778b5502 438 mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
e5e94017
BH
439 }
440 spin_unlock(&nfsi->vfs_inode.i_lock);
974cec8c
AA
441 pnfs_free_lseg_list(&tmp_list);
442}
443
444/*
445 * Called by the state manger to remove all layouts established under an
446 * expired lease.
447 */
448void
449pnfs_destroy_all_layouts(struct nfs_client *clp)
450{
451 struct pnfs_layout_hdr *lo;
452 LIST_HEAD(tmp_list);
453
454 spin_lock(&clp->cl_lock);
455 list_splice_init(&clp->cl_layouts, &tmp_list);
456 spin_unlock(&clp->cl_lock);
457
458 while (!list_empty(&tmp_list)) {
459 lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
b7edfaa1 460 plh_layouts);
974cec8c 461 dprintk("%s freeing layout for inode %lu\n", __func__,
b7edfaa1 462 lo->plh_inode->i_ino);
2887fe45 463 list_del_init(&lo->plh_layouts);
b7edfaa1 464 pnfs_destroy_layout(NFS_I(lo->plh_inode));
974cec8c 465 }
e5e94017
BH
466}
467
fd6002e9 468/* update lo->plh_stateid with new if is more recent */
43f1b3da
FI
469void
470pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
471 bool update_barrier)
b1f69b75 472{
fd6002e9 473 u32 oldseq, newseq;
b1f69b75 474
fd6002e9
FI
475 oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid);
476 newseq = be32_to_cpu(new->stateid.seqid);
43f1b3da 477 if ((int)(newseq - oldseq) > 0) {
fd6002e9 478 memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid));
43f1b3da
FI
479 if (update_barrier) {
480 u32 new_barrier = be32_to_cpu(new->stateid.seqid);
481
482 if ((int)(new_barrier - lo->plh_barrier))
483 lo->plh_barrier = new_barrier;
484 } else {
485 /* Because of wraparound, we want to keep the barrier
486 * "close" to the current seqids. It needs to be
487 * within 2**31 to count as "behind", so if it
488 * gets too near that limit, give us a litle leeway
489 * and bring it to within 2**30.
490 * NOTE - and yes, this is all unsigned arithmetic.
491 */
492 if (unlikely((newseq - lo->plh_barrier) > (3 << 29)))
493 lo->plh_barrier = newseq - (1 << 30);
494 }
495 }
b1f69b75
AA
496}
497
cf7d63f1
FI
498/* lget is set to 1 if called from inside send_layoutget call chain */
499static bool
43f1b3da
FI
500pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid,
501 int lget)
502{
503 if ((stateid) &&
504 (int)(lo->plh_barrier - be32_to_cpu(stateid->stateid.seqid)) >= 0)
505 return true;
f7e8917a 506 return lo->plh_block_lgets ||
38511722 507 test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) ||
f7e8917a 508 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
43f1b3da 509 (list_empty(&lo->plh_segs) &&
cf7d63f1
FI
510 (atomic_read(&lo->plh_outstanding) > lget));
511}
512
fd6002e9
FI
513int
514pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
515 struct nfs4_state *open_state)
b1f69b75 516{
fd6002e9 517 int status = 0;
974cec8c 518
b1f69b75 519 dprintk("--> %s\n", __func__);
fd6002e9 520 spin_lock(&lo->plh_inode->i_lock);
43f1b3da 521 if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
cf7d63f1
FI
522 status = -EAGAIN;
523 } else if (list_empty(&lo->plh_segs)) {
fd6002e9
FI
524 int seq;
525
526 do {
527 seq = read_seqbegin(&open_state->seqlock);
528 memcpy(dst->data, open_state->stateid.data,
529 sizeof(open_state->stateid.data));
530 } while (read_seqretry(&open_state->seqlock, seq));
531 } else
532 memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data));
533 spin_unlock(&lo->plh_inode->i_lock);
b1f69b75 534 dprintk("<-- %s\n", __func__);
fd6002e9 535 return status;
b1f69b75
AA
536}
537
538/*
539* Get layout from server.
540* for now, assume that whole file layouts are requested.
541* arg->offset: 0
542* arg->length: all ones
543*/
e5e94017
BH
544static struct pnfs_layout_segment *
545send_layoutget(struct pnfs_layout_hdr *lo,
546 struct nfs_open_context *ctx,
fb3296eb 547 struct pnfs_layout_range *range,
a75b9df9 548 gfp_t gfp_flags)
e5e94017 549{
b7edfaa1 550 struct inode *ino = lo->plh_inode;
b1f69b75
AA
551 struct nfs_server *server = NFS_SERVER(ino);
552 struct nfs4_layoutget *lgp;
553 struct pnfs_layout_segment *lseg = NULL;
35124a09
WAA
554 struct page **pages = NULL;
555 int i;
556 u32 max_resp_sz, max_pages;
b1f69b75
AA
557
558 dprintk("--> %s\n", __func__);
e5e94017 559
b1f69b75 560 BUG_ON(ctx == NULL);
a75b9df9 561 lgp = kzalloc(sizeof(*lgp), gfp_flags);
cf7d63f1 562 if (lgp == NULL)
b1f69b75 563 return NULL;
35124a09
WAA
564
565 /* allocate pages for xdr post processing */
566 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
567 max_pages = max_resp_sz >> PAGE_SHIFT;
568
a75b9df9 569 pages = kzalloc(max_pages * sizeof(struct page *), gfp_flags);
35124a09
WAA
570 if (!pages)
571 goto out_err_free;
572
573 for (i = 0; i < max_pages; i++) {
a75b9df9 574 pages[i] = alloc_page(gfp_flags);
35124a09
WAA
575 if (!pages[i])
576 goto out_err_free;
577 }
578
fb3296eb
BH
579 lgp->args.minlength = PAGE_CACHE_SIZE;
580 if (lgp->args.minlength > range->length)
581 lgp->args.minlength = range->length;
b1f69b75 582 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
fb3296eb 583 lgp->args.range = *range;
b1f69b75
AA
584 lgp->args.type = server->pnfs_curr_ld->id;
585 lgp->args.inode = ino;
586 lgp->args.ctx = get_nfs_open_context(ctx);
35124a09
WAA
587 lgp->args.layout.pages = pages;
588 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
b1f69b75 589 lgp->lsegpp = &lseg;
a75b9df9 590 lgp->gfp_flags = gfp_flags;
b1f69b75
AA
591
592 /* Synchronously retrieve layout information from server and
593 * store in lseg.
594 */
595 nfs4_proc_layoutget(lgp);
974cec8c 596 if (!lseg) {
b1f69b75 597 /* remember that LAYOUTGET failed and suspend trying */
fb3296eb 598 set_bit(lo_fail_bit(range->iomode), &lo->plh_flags);
974cec8c 599 }
35124a09
WAA
600
601 /* free xdr pages */
602 for (i = 0; i < max_pages; i++)
603 __free_page(pages[i]);
604 kfree(pages);
605
974cec8c 606 return lseg;
35124a09
WAA
607
608out_err_free:
609 /* free any allocated xdr pages, lgp as it's not used */
610 if (pages) {
611 for (i = 0; i < max_pages; i++) {
612 if (!pages[i])
613 break;
614 __free_page(pages[i]);
615 }
616 kfree(pages);
617 }
618 kfree(lgp);
619 return NULL;
974cec8c
AA
620}
621
f7e8917a
FI
622bool pnfs_roc(struct inode *ino)
623{
624 struct pnfs_layout_hdr *lo;
625 struct pnfs_layout_segment *lseg, *tmp;
626 LIST_HEAD(tmp_list);
627 bool found = false;
628
629 spin_lock(&ino->i_lock);
630 lo = NFS_I(ino)->layout;
631 if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
632 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
633 goto out_nolayout;
634 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
635 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
636 mark_lseg_invalid(lseg, &tmp_list);
637 found = true;
638 }
639 if (!found)
640 goto out_nolayout;
641 lo->plh_block_lgets++;
642 get_layout_hdr(lo); /* matched in pnfs_roc_release */
643 spin_unlock(&ino->i_lock);
644 pnfs_free_lseg_list(&tmp_list);
645 return true;
646
647out_nolayout:
648 spin_unlock(&ino->i_lock);
649 return false;
650}
651
652void pnfs_roc_release(struct inode *ino)
653{
654 struct pnfs_layout_hdr *lo;
655
656 spin_lock(&ino->i_lock);
657 lo = NFS_I(ino)->layout;
658 lo->plh_block_lgets--;
659 put_layout_hdr_locked(lo);
660 spin_unlock(&ino->i_lock);
661}
662
663void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
664{
665 struct pnfs_layout_hdr *lo;
666
667 spin_lock(&ino->i_lock);
668 lo = NFS_I(ino)->layout;
669 if ((int)(barrier - lo->plh_barrier) > 0)
670 lo->plh_barrier = barrier;
671 spin_unlock(&ino->i_lock);
672}
673
674bool pnfs_roc_drain(struct inode *ino, u32 *barrier)
675{
676 struct nfs_inode *nfsi = NFS_I(ino);
677 struct pnfs_layout_segment *lseg;
678 bool found = false;
679
680 spin_lock(&ino->i_lock);
681 list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
682 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
683 found = true;
684 break;
685 }
686 if (!found) {
687 struct pnfs_layout_hdr *lo = nfsi->layout;
688 u32 current_seqid = be32_to_cpu(lo->plh_stateid.stateid.seqid);
689
690 /* Since close does not return a layout stateid for use as
691 * a barrier, we choose the worst-case barrier.
692 */
693 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
694 }
695 spin_unlock(&ino->i_lock);
696 return found;
697}
698
b1f69b75
AA
699/*
700 * Compare two layout segments for sorting into layout cache.
701 * We want to preferentially return RW over RO layouts, so ensure those
702 * are seen first.
703 */
704static s64
fb3296eb
BH
705cmp_layout(struct pnfs_layout_range *l1,
706 struct pnfs_layout_range *l2)
b1f69b75 707{
fb3296eb
BH
708 s64 d;
709
710 /* high offset > low offset */
711 d = l1->offset - l2->offset;
712 if (d)
713 return d;
714
715 /* short length > long length */
716 d = l2->length - l1->length;
717 if (d)
718 return d;
719
b1f69b75 720 /* read > read/write */
fb3296eb 721 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
b1f69b75
AA
722}
723
974cec8c
AA
724static void
725pnfs_insert_layout(struct pnfs_layout_hdr *lo,
726 struct pnfs_layout_segment *lseg)
727{
b1f69b75 728 struct pnfs_layout_segment *lp;
b1f69b75 729
974cec8c
AA
730 dprintk("%s:Begin\n", __func__);
731
b7edfaa1 732 assert_spin_locked(&lo->plh_inode->i_lock);
b7edfaa1 733 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
fb3296eb 734 if (cmp_layout(&lseg->pls_range, &lp->pls_range) > 0)
b1f69b75 735 continue;
566052c5 736 list_add_tail(&lseg->pls_list, &lp->pls_list);
b1f69b75
AA
737 dprintk("%s: inserted lseg %p "
738 "iomode %d offset %llu length %llu before "
739 "lp %p iomode %d offset %llu length %llu\n",
566052c5
FI
740 __func__, lseg, lseg->pls_range.iomode,
741 lseg->pls_range.offset, lseg->pls_range.length,
742 lp, lp->pls_range.iomode, lp->pls_range.offset,
743 lp->pls_range.length);
fb3296eb 744 goto out;
974cec8c 745 }
fb3296eb
BH
746 list_add_tail(&lseg->pls_list, &lo->plh_segs);
747 dprintk("%s: inserted lseg %p "
748 "iomode %d offset %llu length %llu at tail\n",
749 __func__, lseg, lseg->pls_range.iomode,
750 lseg->pls_range.offset, lseg->pls_range.length);
751out:
cc6e5340 752 get_layout_hdr(lo);
974cec8c
AA
753
754 dprintk("%s:Return\n", __func__);
e5e94017
BH
755}
756
757static struct pnfs_layout_hdr *
a75b9df9 758alloc_init_layout_hdr(struct inode *ino, gfp_t gfp_flags)
e5e94017
BH
759{
760 struct pnfs_layout_hdr *lo;
761
636fb9c8 762 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
e5e94017
BH
763 if (!lo)
764 return NULL;
cc6e5340 765 atomic_set(&lo->plh_refcount, 1);
b7edfaa1
FI
766 INIT_LIST_HEAD(&lo->plh_layouts);
767 INIT_LIST_HEAD(&lo->plh_segs);
43f1b3da 768 INIT_LIST_HEAD(&lo->plh_bulk_recall);
b7edfaa1 769 lo->plh_inode = ino;
e5e94017
BH
770 return lo;
771}
772
773static struct pnfs_layout_hdr *
a75b9df9 774pnfs_find_alloc_layout(struct inode *ino, gfp_t gfp_flags)
e5e94017
BH
775{
776 struct nfs_inode *nfsi = NFS_I(ino);
777 struct pnfs_layout_hdr *new = NULL;
778
779 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
780
781 assert_spin_locked(&ino->i_lock);
4541d16c
FI
782 if (nfsi->layout) {
783 if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
784 return NULL;
785 else
786 return nfsi->layout;
787 }
e5e94017 788 spin_unlock(&ino->i_lock);
a75b9df9 789 new = alloc_init_layout_hdr(ino, gfp_flags);
e5e94017
BH
790 spin_lock(&ino->i_lock);
791
792 if (likely(nfsi->layout == NULL)) /* Won the race? */
793 nfsi->layout = new;
794 else
636fb9c8 795 pnfs_free_layout_hdr(new);
e5e94017
BH
796 return nfsi->layout;
797}
798
b1f69b75
AA
799/*
800 * iomode matching rules:
801 * iomode lseg match
802 * ----- ----- -----
803 * ANY READ true
804 * ANY RW true
805 * RW READ false
806 * RW RW true
807 * READ READ true
808 * READ RW true
809 */
810static int
fb3296eb
BH
811is_matching_lseg(struct pnfs_layout_range *ls_range,
812 struct pnfs_layout_range *range)
b1f69b75 813{
fb3296eb
BH
814 struct pnfs_layout_range range1;
815
816 if ((range->iomode == IOMODE_RW &&
817 ls_range->iomode != IOMODE_RW) ||
818 !lo_seg_intersecting(ls_range, range))
819 return 0;
820
821 /* range1 covers only the first byte in the range */
822 range1 = *range;
823 range1.length = 1;
824 return lo_seg_contained(ls_range, &range1);
b1f69b75
AA
825}
826
827/*
828 * lookup range in layout
829 */
e5e94017 830static struct pnfs_layout_segment *
fb3296eb
BH
831pnfs_find_lseg(struct pnfs_layout_hdr *lo,
832 struct pnfs_layout_range *range)
e5e94017 833{
b1f69b75
AA
834 struct pnfs_layout_segment *lseg, *ret = NULL;
835
836 dprintk("%s:Begin\n", __func__);
837
b7edfaa1
FI
838 assert_spin_locked(&lo->plh_inode->i_lock);
839 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
4541d16c 840 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
fb3296eb 841 is_matching_lseg(&lseg->pls_range, range)) {
d684d2ae 842 ret = get_lseg(lseg);
b1f69b75
AA
843 break;
844 }
fb3296eb 845 if (cmp_layout(range, &lseg->pls_range) > 0)
b1f69b75
AA
846 break;
847 }
848
849 dprintk("%s:Return lseg %p ref %d\n",
4541d16c 850 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
b1f69b75 851 return ret;
e5e94017
BH
852}
853
854/*
855 * Layout segment is retreived from the server if not cached.
856 * The appropriate layout segment is referenced and returned to the caller.
857 */
858struct pnfs_layout_segment *
859pnfs_update_layout(struct inode *ino,
860 struct nfs_open_context *ctx,
fb3296eb
BH
861 loff_t pos,
862 u64 count,
a75b9df9
TM
863 enum pnfs_iomode iomode,
864 gfp_t gfp_flags)
e5e94017 865{
fb3296eb
BH
866 struct pnfs_layout_range arg = {
867 .iomode = iomode,
868 .offset = pos,
869 .length = count,
870 };
707ed5fd 871 unsigned pg_offset;
e5e94017 872 struct nfs_inode *nfsi = NFS_I(ino);
2130ff66 873 struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
e5e94017
BH
874 struct pnfs_layout_hdr *lo;
875 struct pnfs_layout_segment *lseg = NULL;
f49f9baa 876 bool first = false;
e5e94017
BH
877
878 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
879 return NULL;
880 spin_lock(&ino->i_lock);
a75b9df9 881 lo = pnfs_find_alloc_layout(ino, gfp_flags);
e5e94017
BH
882 if (lo == NULL) {
883 dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
884 goto out_unlock;
885 }
886
43f1b3da
FI
887 /* Do we even need to bother with this? */
888 if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
889 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
890 dprintk("%s matches recall, use MDS\n", __func__);
e5e94017
BH
891 goto out_unlock;
892 }
893
894 /* if LAYOUTGET already failed once we don't try again */
566052c5 895 if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
e5e94017
BH
896 goto out_unlock;
897
568e8c49 898 /* Check to see if the layout for the given range already exists */
fb3296eb 899 lseg = pnfs_find_lseg(lo, &arg);
568e8c49
AA
900 if (lseg)
901 goto out_unlock;
902
43f1b3da 903 if (pnfs_layoutgets_blocked(lo, NULL, 0))
cf7d63f1
FI
904 goto out_unlock;
905 atomic_inc(&lo->plh_outstanding);
906
cc6e5340 907 get_layout_hdr(lo);
f49f9baa
FI
908 if (list_empty(&lo->plh_segs))
909 first = true;
910 spin_unlock(&ino->i_lock);
911 if (first) {
2130ff66
FI
912 /* The lo must be on the clp list if there is any
913 * chance of a CB_LAYOUTRECALL(FILE) coming in.
914 */
915 spin_lock(&clp->cl_lock);
916 BUG_ON(!list_empty(&lo->plh_layouts));
917 list_add_tail(&lo->plh_layouts, &clp->cl_layouts);
918 spin_unlock(&clp->cl_lock);
919 }
e5e94017 920
707ed5fd
BH
921 pg_offset = arg.offset & ~PAGE_CACHE_MASK;
922 if (pg_offset) {
923 arg.offset -= pg_offset;
924 arg.length += pg_offset;
925 }
926 arg.length = PAGE_CACHE_ALIGN(arg.length);
927
fb3296eb 928 lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
f49f9baa
FI
929 if (!lseg && first) {
930 spin_lock(&clp->cl_lock);
931 list_del_init(&lo->plh_layouts);
932 spin_unlock(&clp->cl_lock);
2130ff66 933 }
cf7d63f1 934 atomic_dec(&lo->plh_outstanding);
cc6e5340 935 put_layout_hdr(lo);
e5e94017
BH
936out:
937 dprintk("%s end, state 0x%lx lseg %p\n", __func__,
bf9c1387 938 nfsi->layout ? nfsi->layout->plh_flags : -1, lseg);
e5e94017
BH
939 return lseg;
940out_unlock:
941 spin_unlock(&ino->i_lock);
942 goto out;
943}
b1f69b75
AA
944
945int
946pnfs_layout_process(struct nfs4_layoutget *lgp)
947{
948 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
949 struct nfs4_layoutget_res *res = &lgp->res;
950 struct pnfs_layout_segment *lseg;
b7edfaa1 951 struct inode *ino = lo->plh_inode;
43f1b3da 952 struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
b1f69b75
AA
953 int status = 0;
954
955 /* Inject layout blob into I/O device driver */
a75b9df9 956 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
b1f69b75
AA
957 if (!lseg || IS_ERR(lseg)) {
958 if (!lseg)
959 status = -ENOMEM;
960 else
961 status = PTR_ERR(lseg);
962 dprintk("%s: Could not allocate layout: error %d\n",
963 __func__, status);
964 goto out;
965 }
966
967 spin_lock(&ino->i_lock);
43f1b3da
FI
968 if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
969 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
970 dprintk("%s forget reply due to recall\n", __func__);
971 goto out_forget_reply;
972 }
973
974 if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) {
975 dprintk("%s forget reply due to state\n", __func__);
976 goto out_forget_reply;
977 }
b1f69b75 978 init_lseg(lo, lseg);
566052c5 979 lseg->pls_range = res->range;
d684d2ae 980 *lgp->lsegpp = get_lseg(lseg);
b1f69b75
AA
981 pnfs_insert_layout(lo, lseg);
982
f7e8917a
FI
983 if (res->return_on_close) {
984 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
985 set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
986 }
987
b1f69b75 988 /* Done processing layoutget. Set the layout stateid */
43f1b3da 989 pnfs_set_layout_stateid(lo, &res->stateid, false);
b1f69b75
AA
990 spin_unlock(&ino->i_lock);
991out:
992 return status;
43f1b3da
FI
993
994out_forget_reply:
995 spin_unlock(&ino->i_lock);
996 lseg->pls_layout = lo;
997 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
998 goto out;
b1f69b75
AA
999}
1000
bae724ef
FI
1001static int pnfs_read_pg_test(struct nfs_pageio_descriptor *pgio,
1002 struct nfs_page *prev,
1003 struct nfs_page *req)
94ad1c80 1004{
bae724ef
FI
1005 if (pgio->pg_count == prev->wb_bytes) {
1006 /* This is first coelesce call for a series of nfs_pages */
1007 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1008 prev->wb_context,
fb3296eb
BH
1009 req_offset(req),
1010 pgio->pg_count,
a75b9df9
TM
1011 IOMODE_READ,
1012 GFP_KERNEL);
fb3296eb
BH
1013 } else if (pgio->pg_lseg &&
1014 req_offset(req) > end_offset(pgio->pg_lseg->pls_range.offset,
1015 pgio->pg_lseg->pls_range.length))
1016 return 0;
bae724ef 1017 return NFS_SERVER(pgio->pg_inode)->pnfs_curr_ld->pg_test(pgio, prev, req);
94ad1c80
FI
1018}
1019
1020void
bae724ef 1021pnfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, struct inode *inode)
94ad1c80 1022{
bae724ef
FI
1023 struct pnfs_layoutdriver_type *ld;
1024
1025 ld = NFS_SERVER(inode)->pnfs_curr_ld;
1026 pgio->pg_test = (ld && ld->pg_test) ? pnfs_read_pg_test : NULL;
94ad1c80
FI
1027}
1028
44b83799
FI
1029static int pnfs_write_pg_test(struct nfs_pageio_descriptor *pgio,
1030 struct nfs_page *prev,
1031 struct nfs_page *req)
1032{
1033 if (pgio->pg_count == prev->wb_bytes) {
1034 /* This is first coelesce call for a series of nfs_pages */
1035 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1036 prev->wb_context,
fb3296eb
BH
1037 req_offset(req),
1038 pgio->pg_count,
a75b9df9
TM
1039 IOMODE_RW,
1040 GFP_NOFS);
fb3296eb
BH
1041 } else if (pgio->pg_lseg &&
1042 req_offset(req) > end_offset(pgio->pg_lseg->pls_range.offset,
1043 pgio->pg_lseg->pls_range.length))
1044 return 0;
44b83799
FI
1045 return NFS_SERVER(pgio->pg_inode)->pnfs_curr_ld->pg_test(pgio, prev, req);
1046}
1047
1048void
1049pnfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, struct inode *inode)
1050{
1051 struct pnfs_layoutdriver_type *ld;
1052
1053 ld = NFS_SERVER(inode)->pnfs_curr_ld;
1054 pgio->pg_test = (ld && ld->pg_test) ? pnfs_write_pg_test : NULL;
1055}
1056
0382b744
AA
1057enum pnfs_try_status
1058pnfs_try_to_write_data(struct nfs_write_data *wdata,
1059 const struct rpc_call_ops *call_ops, int how)
1060{
1061 struct inode *inode = wdata->inode;
1062 enum pnfs_try_status trypnfs;
1063 struct nfs_server *nfss = NFS_SERVER(inode);
1064
1065 wdata->mds_ops = call_ops;
1066
1067 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
1068 inode->i_ino, wdata->args.count, wdata->args.offset, how);
1069
1070 trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how);
1071 if (trypnfs == PNFS_NOT_ATTEMPTED) {
1072 put_lseg(wdata->lseg);
1073 wdata->lseg = NULL;
1074 } else
1075 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
1076
1077 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1078 return trypnfs;
1079}
1080
64419a9b
AA
1081/*
1082 * Call the appropriate parallel I/O subsystem read function.
1083 */
1084enum pnfs_try_status
1085pnfs_try_to_read_data(struct nfs_read_data *rdata,
1086 const struct rpc_call_ops *call_ops)
1087{
1088 struct inode *inode = rdata->inode;
1089 struct nfs_server *nfss = NFS_SERVER(inode);
1090 enum pnfs_try_status trypnfs;
1091
1092 rdata->mds_ops = call_ops;
1093
1094 dprintk("%s: Reading ino:%lu %u@%llu\n",
1095 __func__, inode->i_ino, rdata->args.count, rdata->args.offset);
1096
1097 trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata);
1098 if (trypnfs == PNFS_NOT_ATTEMPTED) {
1099 put_lseg(rdata->lseg);
1100 rdata->lseg = NULL;
1101 } else {
1102 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
1103 }
1104 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1105 return trypnfs;
1106}
863a3c6c
AA
1107
1108/*
1109 * Currently there is only one (whole file) write lseg.
1110 */
1111static struct pnfs_layout_segment *pnfs_list_write_lseg(struct inode *inode)
1112{
1113 struct pnfs_layout_segment *lseg, *rv = NULL;
1114
1115 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list)
1116 if (lseg->pls_range.iomode == IOMODE_RW)
1117 rv = lseg;
1118 return rv;
1119}
1120
1121void
1122pnfs_set_layoutcommit(struct nfs_write_data *wdata)
1123{
1124 struct nfs_inode *nfsi = NFS_I(wdata->inode);
1125 loff_t end_pos = wdata->args.offset + wdata->res.count;
79a48a1f 1126 bool mark_as_dirty = false;
863a3c6c
AA
1127
1128 spin_lock(&nfsi->vfs_inode.i_lock);
1129 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1130 /* references matched in nfs4_layoutcommit_release */
1131 get_lseg(wdata->lseg);
1132 wdata->lseg->pls_lc_cred =
1133 get_rpccred(wdata->args.context->state->owner->so_cred);
79a48a1f 1134 mark_as_dirty = true;
863a3c6c
AA
1135 dprintk("%s: Set layoutcommit for inode %lu ",
1136 __func__, wdata->inode->i_ino);
1137 }
1138 if (end_pos > wdata->lseg->pls_end_pos)
1139 wdata->lseg->pls_end_pos = end_pos;
1140 spin_unlock(&nfsi->vfs_inode.i_lock);
79a48a1f
WAA
1141
1142 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
1143 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
1144 if (mark_as_dirty)
1145 mark_inode_dirty_sync(wdata->inode);
863a3c6c
AA
1146}
1147EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
1148
de4b15c7
AA
1149/*
1150 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
1151 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
1152 * data to disk to allow the server to recover the data if it crashes.
1153 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
1154 * is off, and a COMMIT is sent to a data server, or
1155 * if WRITEs to a data server return NFS_DATA_SYNC.
1156 */
863a3c6c 1157int
ef311537 1158pnfs_layoutcommit_inode(struct inode *inode, bool sync)
863a3c6c
AA
1159{
1160 struct nfs4_layoutcommit_data *data;
1161 struct nfs_inode *nfsi = NFS_I(inode);
1162 struct pnfs_layout_segment *lseg;
1163 struct rpc_cred *cred;
1164 loff_t end_pos;
1165 int status = 0;
1166
1167 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
1168
de4b15c7
AA
1169 if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
1170 return 0;
1171
863a3c6c
AA
1172 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
1173 data = kzalloc(sizeof(*data), GFP_NOFS);
de4b15c7
AA
1174 if (!data) {
1175 mark_inode_dirty_sync(inode);
1176 status = -ENOMEM;
1177 goto out;
1178 }
863a3c6c 1179
de4b15c7 1180 spin_lock(&inode->i_lock);
863a3c6c
AA
1181 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1182 spin_unlock(&inode->i_lock);
1183 kfree(data);
1184 goto out;
1185 }
1186 /*
1187 * Currently only one (whole file) write lseg which is referenced
1188 * in pnfs_set_layoutcommit and will be found.
1189 */
1190 lseg = pnfs_list_write_lseg(inode);
1191
1192 end_pos = lseg->pls_end_pos;
1193 cred = lseg->pls_lc_cred;
1194 lseg->pls_end_pos = 0;
1195 lseg->pls_lc_cred = NULL;
1196
de4b15c7
AA
1197 memcpy(&data->args.stateid.data, nfsi->layout->plh_stateid.data,
1198 sizeof(nfsi->layout->plh_stateid.data));
863a3c6c
AA
1199 spin_unlock(&inode->i_lock);
1200
1201 data->args.inode = inode;
1202 data->lseg = lseg;
1203 data->cred = cred;
1204 nfs_fattr_init(&data->fattr);
1205 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
1206 data->res.fattr = &data->fattr;
1207 data->args.lastbytewritten = end_pos - 1;
1208 data->res.server = NFS_SERVER(inode);
1209
1210 status = nfs4_proc_layoutcommit(data, sync);
1211out:
1212 dprintk("<-- %s status %d\n", __func__, status);
1213 return status;
1214}