blob: 9eca5a8cdbddeba654ba8980642deae8435acb70 [file] [log] [blame]
Ricardo Labiaga85e174b2010-10-20 00:17:58 -04001/*
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>
Trond Myklebust493292d2011-07-13 15:58:28 -040031#include <linux/nfs_page.h>
Andy Adamson974cec82010-10-20 00:18:02 -040032#include "internal.h"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040033#include "pnfs.h"
Andy Adamson64419a92011-03-01 01:34:16 +000034#include "iostat.h"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040035
36#define NFSDBG_FACILITY NFSDBG_PNFS
37
Fred Isaman02c35fc2010-10-20 00:17:59 -040038/* 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
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040065static struct pnfs_layoutdriver_type *
66find_pnfs_driver(u32 id)
67{
Fred Isaman02c35fc2010-10-20 00:17:59 -040068 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;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040074}
75
76void
77unset_pnfs_layoutdriver(struct nfs_server *nfss)
78{
Christoph Hellwigea8eecd2011-03-01 01:34:21 +000079 if (nfss->pnfs_curr_ld)
Fred Isaman02c35fc2010-10-20 00:17:59 -040080 module_put(nfss->pnfs_curr_ld->owner);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040081 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 }
Fred Isaman02c35fc2010-10-20 00:17:59 -0400113 if (!try_module_get(ld_type->owner)) {
114 dprintk("%s: Could not grab reference on module\n", __func__);
115 goto out_no_driver;
116 }
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400117 server->pnfs_curr_ld = ld_type;
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000118
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400119 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}
Fred Isaman02c35fc2010-10-20 00:17:59 -0400126
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 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400137 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 }
Fred Isaman02c35fc2010-10-20 00:17:59 -0400142
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);
Benny Halevye5e94012010-10-20 00:18:01 -0400169
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400170/*
171 * pNFS client layout cache
172 */
173
Fred Isamancc6e5342011-01-06 11:36:28 +0000174/* Need to hold i_lock if caller does not already hold reference */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000175void
Fred Isamancc6e5342011-01-06 11:36:28 +0000176get_layout_hdr(struct pnfs_layout_hdr *lo)
Benny Halevye5e94012010-10-20 00:18:01 -0400177{
Fred Isamancc6e5342011-01-06 11:36:28 +0000178 atomic_inc(&lo->plh_refcount);
179}
180
Benny Halevy636fb9c2011-05-22 19:51:33 +0300181static 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
Fred Isamancc6e5342011-01-06 11:36:28 +0000196static void
197destroy_layout_hdr(struct pnfs_layout_hdr *lo)
198{
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;
Benny Halevy636fb9c2011-05-22 19:51:33 +0300202 pnfs_free_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400203}
204
205static void
206put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
207{
Fred Isamancc6e5342011-01-06 11:36:28 +0000208 if (atomic_dec_and_test(&lo->plh_refcount))
209 destroy_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400210}
211
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400212void
Fred Isamancc6e5342011-01-06 11:36:28 +0000213put_layout_hdr(struct pnfs_layout_hdr *lo)
Andy Adamson974cec82010-10-20 00:18:02 -0400214{
Fred Isamancc6e5342011-01-06 11:36:28 +0000215 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 }
Andy Adamson974cec82010-10-20 00:18:02 -0400221}
222
223static void
224init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
225{
Fred Isaman566052c2011-01-06 11:36:20 +0000226 INIT_LIST_HEAD(&lseg->pls_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000227 atomic_set(&lseg->pls_refcount, 1);
228 smp_mb();
229 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
Fred Isaman566052c2011-01-06 11:36:20 +0000230 lseg->pls_layout = lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400231}
232
Fred Isaman4541d162011-01-06 11:36:23 +0000233static void free_lseg(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400234{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000235 struct inode *ino = lseg->pls_layout->plh_inode;
Andy Adamson974cec82010-10-20 00:18:02 -0400236
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400237 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
Fred Isaman52fabd72011-01-06 11:36:18 +0000238 /* Matched by get_layout_hdr in pnfs_insert_layout */
Fred Isamancc6e5342011-01-06 11:36:28 +0000239 put_layout_hdr(NFS_I(ino)->layout);
Andy Adamson974cec82010-10-20 00:18:02 -0400240}
241
Fred Isamand684d2a2011-03-01 01:34:13 +0000242static void
243put_lseg_common(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400244{
Fred Isamand684d2a2011-03-01 01:34:13 +0000245 struct inode *inode = lseg->pls_layout->plh_inode;
246
Benny Halevyd20581a2011-05-22 19:52:03 +0300247 WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Fred Isamand684d2a2011-03-01 01:34:13 +0000248 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
Fred Isamanbae724e2011-03-01 01:34:15 +0000257void
Fred Isamand684d2a2011-03-01 01:34:13 +0000258put_lseg(struct pnfs_layout_segment *lseg)
259{
260 struct inode *inode;
261
262 if (!lseg)
263 return;
264
Fred Isaman4541d162011-01-06 11:36:23 +0000265 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));
Fred Isamand684d2a2011-03-01 01:34:13 +0000268 inode = lseg->pls_layout->plh_inode;
269 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
270 LIST_HEAD(free_me);
Andy Adamson974cec82010-10-20 00:18:02 -0400271
Fred Isamand684d2a2011-03-01 01:34:13 +0000272 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);
Fred Isaman4541d162011-01-06 11:36:23 +0000276 }
Andy Adamson974cec82010-10-20 00:18:02 -0400277}
Fred Isamane0c2b382011-03-23 13:27:53 +0000278EXPORT_SYMBOL_GPL(put_lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400279
Benny Halevyfb3296e2011-05-22 19:47:26 +0300280static inline u64
281end_offset(u64 start, u64 len)
Fred Isaman4541d162011-01-06 11:36:23 +0000282{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300283 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
Fred Isaman4541d162011-01-06 11:36:23 +0000339static bool
Benny Halevy778b5502011-05-22 19:48:02 +0300340should_free_lseg(struct pnfs_layout_range *lseg_range,
341 struct pnfs_layout_range *recall_range)
Fred Isaman4541d162011-01-06 11:36:23 +0000342{
Benny Halevy778b5502011-05-22 19:48:02 +0300343 return (recall_range->iomode == IOMODE_ANY ||
344 lseg_range->iomode == recall_range->iomode) &&
345 lo_seg_intersecting(lseg_range, recall_range);
Fred Isaman4541d162011-01-06 11:36:23 +0000346}
347
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 */
Fred Isamand684d2a2011-03-01 01:34:13 +0000359 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 }
Fred Isaman4541d162011-01-06 11:36:23 +0000366 }
367 return rv;
368}
369
370/* Returns count of number of matching invalid lsegs remaining in list
371 * after call.
372 */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000373int
Fred Isaman4541d162011-01-06 11:36:23 +0000374mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
375 struct list_head *tmp_list,
Benny Halevy778b5502011-05-22 19:48:02 +0300376 struct pnfs_layout_range *recall_range)
Andy Adamson974cec82010-10-20 00:18:02 -0400377{
378 struct pnfs_layout_segment *lseg, *next;
Fred Isaman4541d162011-01-06 11:36:23 +0000379 int invalid = 0, removed = 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400380
381 dprintk("%s:Begin lo %p\n", __func__, lo);
382
Fred Isaman38511722011-02-03 18:28:50 +0000383 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 }
Fred Isaman4541d162011-01-06 11:36:23 +0000388 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
Benny Halevy778b5502011-05-22 19:48:02 +0300389 if (!recall_range ||
390 should_free_lseg(&lseg->pls_range, recall_range)) {
Fred Isaman4541d162011-01-06 11:36:23 +0000391 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;
Andy Adamson974cec82010-10-20 00:18:02 -0400400}
401
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000402/* note free_me must contain lsegs from a single layout_hdr */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000403void
Fred Isaman4541d162011-01-06 11:36:23 +0000404pnfs_free_lseg_list(struct list_head *free_me)
Andy Adamson974cec82010-10-20 00:18:02 -0400405{
Fred Isaman4541d162011-01-06 11:36:23 +0000406 struct pnfs_layout_segment *lseg, *tmp;
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000407 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400408
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000409 if (list_empty(free_me))
410 return;
411
412 lo = list_first_entry(free_me, struct pnfs_layout_segment,
413 pls_list)->pls_layout;
414
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 }
Fred Isaman4541d162011-01-06 11:36:23 +0000423 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
Fred Isaman566052c2011-01-06 11:36:20 +0000424 list_del(&lseg->pls_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000425 free_lseg(lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400426 }
427}
428
Benny Halevye5e94012010-10-20 00:18:01 -0400429void
430pnfs_destroy_layout(struct nfs_inode *nfsi)
431{
432 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400433 LIST_HEAD(tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400434
435 spin_lock(&nfsi->vfs_inode.i_lock);
436 lo = nfsi->layout;
437 if (lo) {
Fred Isaman38511722011-02-03 18:28:50 +0000438 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
Benny Halevy778b5502011-05-22 19:48:02 +0300439 mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
Benny Halevye5e94012010-10-20 00:18:01 -0400440 }
441 spin_unlock(&nfsi->vfs_inode.i_lock);
Andy Adamson974cec82010-10-20 00:18:02 -0400442 pnfs_free_lseg_list(&tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400443}
444
Andy Adamson974cec82010-10-20 00:18:02 -0400445/*
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{
Weston Andros Adamson6382a442011-06-01 16:44:44 -0400452 struct nfs_server *server;
Andy Adamson974cec82010-10-20 00:18:02 -0400453 struct pnfs_layout_hdr *lo;
454 LIST_HEAD(tmp_list);
455
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400456 nfs4_deviceid_mark_client_invalid(clp);
457 nfs4_deviceid_purge_client(clp);
458
Andy Adamson974cec82010-10-20 00:18:02 -0400459 spin_lock(&clp->cl_lock);
Weston Andros Adamson6382a442011-06-01 16:44:44 -0400460 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();
Andy Adamson974cec82010-10-20 00:18:02 -0400466 spin_unlock(&clp->cl_lock);
467
468 while (!list_empty(&tmp_list)) {
469 lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000470 plh_layouts);
Andy Adamson974cec82010-10-20 00:18:02 -0400471 dprintk("%s freeing layout for inode %lu\n", __func__,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000472 lo->plh_inode->i_ino);
Andy Adamson2887fe42011-05-11 01:19:58 -0400473 list_del_init(&lo->plh_layouts);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000474 pnfs_destroy_layout(NFS_I(lo->plh_inode));
Andy Adamson974cec82010-10-20 00:18:02 -0400475 }
476}
477
Fred Isamanfd6002e2011-01-06 11:36:22 +0000478/* update lo->plh_stateid with new if is more recent */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000479void
480pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
481 bool update_barrier)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400482{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000483 u32 oldseq, newseq;
Andy Adamson974cec82010-10-20 00:18:02 -0400484
Fred Isamanfd6002e2011-01-06 11:36:22 +0000485 oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid);
486 newseq = be32_to_cpu(new->stateid.seqid);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000487 if ((int)(newseq - oldseq) > 0) {
Fred Isamanfd6002e2011-01-06 11:36:22 +0000488 memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid));
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000489 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 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400506}
507
Fred Isamancf7d63f2011-01-06 11:36:25 +0000508/* lget is set to 1 if called from inside send_layoutget call chain */
509static bool
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000510pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid,
511 int lget)
Fred Isamancf7d63f2011-01-06 11:36:25 +0000512{
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000513 if ((stateid) &&
514 (int)(lo->plh_barrier - be32_to_cpu(stateid->stateid.seqid)) >= 0)
515 return true;
Fred Isamanf7e89172011-01-06 11:36:32 +0000516 return lo->plh_block_lgets ||
Fred Isaman38511722011-02-03 18:28:50 +0000517 test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) ||
Fred Isamanf7e89172011-01-06 11:36:32 +0000518 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000519 (list_empty(&lo->plh_segs) &&
Fred Isamancf7d63f2011-01-06 11:36:25 +0000520 (atomic_read(&lo->plh_outstanding) > lget));
521}
522
Fred Isamanfd6002e2011-01-06 11:36:22 +0000523int
524pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
525 struct nfs4_state *open_state)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400526{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000527 int status = 0;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400528
529 dprintk("--> %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000530 spin_lock(&lo->plh_inode->i_lock);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000531 if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
Fred Isamancf7d63f2011-01-06 11:36:25 +0000532 status = -EAGAIN;
533 } else if (list_empty(&lo->plh_segs)) {
Fred Isamanfd6002e2011-01-06 11:36:22 +0000534 int seq;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400535
Fred Isamanfd6002e2011-01-06 11:36:22 +0000536 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);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400544 dprintk("<-- %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000545 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400546}
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*/
Benny Halevye5e94012010-10-20 00:18:01 -0400554static struct pnfs_layout_segment *
555send_layoutget(struct pnfs_layout_hdr *lo,
556 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +0300557 struct pnfs_layout_range *range,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400558 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400559{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000560 struct inode *ino = lo->plh_inode;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400561 struct nfs_server *server = NFS_SERVER(ino);
562 struct nfs4_layoutget *lgp;
563 struct pnfs_layout_segment *lseg = NULL;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400564 struct page **pages = NULL;
565 int i;
566 u32 max_resp_sz, max_pages;
Benny Halevye5e94012010-10-20 00:18:01 -0400567
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400568 dprintk("--> %s\n", __func__);
569
570 BUG_ON(ctx == NULL);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400571 lgp = kzalloc(sizeof(*lgp), gfp_flags);
Fred Isamancf7d63f2011-01-06 11:36:25 +0000572 if (lgp == NULL)
Andy Adamson974cec82010-10-20 00:18:02 -0400573 return NULL;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400574
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
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400579 pages = kzalloc(max_pages * sizeof(struct page *), gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400580 if (!pages)
581 goto out_err_free;
582
583 for (i = 0; i < max_pages; i++) {
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400584 pages[i] = alloc_page(gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400585 if (!pages[i])
586 goto out_err_free;
587 }
588
Benny Halevyfb3296e2011-05-22 19:47:26 +0300589 lgp->args.minlength = PAGE_CACHE_SIZE;
590 if (lgp->args.minlength > range->length)
591 lgp->args.minlength = range->length;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400592 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
Benny Halevyfb3296e2011-05-22 19:47:26 +0300593 lgp->args.range = *range;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400594 lgp->args.type = server->pnfs_curr_ld->id;
595 lgp->args.inode = ino;
596 lgp->args.ctx = get_nfs_open_context(ctx);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400597 lgp->args.layout.pages = pages;
598 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400599 lgp->lsegpp = &lseg;
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400600 lgp->gfp_flags = gfp_flags;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400601
602 /* Synchronously retrieve layout information from server and
603 * store in lseg.
604 */
605 nfs4_proc_layoutget(lgp);
606 if (!lseg) {
607 /* remember that LAYOUTGET failed and suspend trying */
Benny Halevyfb3296e2011-05-22 19:47:26 +0300608 set_bit(lo_fail_bit(range->iomode), &lo->plh_flags);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400609 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400610
611 /* free xdr pages */
612 for (i = 0; i < max_pages; i++)
613 __free_page(pages[i]);
614 kfree(pages);
615
Andy Adamson974cec82010-10-20 00:18:02 -0400616 return lseg;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400617
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;
Andy Adamson974cec82010-10-20 00:18:02 -0400630}
631
Benny Halevycbe82602011-05-22 19:52:37 +0300632/* 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;
Fred Isamana2e1d4f2011-06-13 18:54:53 -0400647 if (!lo) {
Benny Halevycbe82602011-05-22 19:52:37 +0300648 spin_unlock(&ino->i_lock);
Fred Isamana2e1d4f2011-06-13 18:54:53 -0400649 dprintk("%s: no layout to return\n", __func__);
650 return status;
Benny Halevycbe82602011-05-22 19:52:37 +0300651 }
652 stateid = nfsi->layout->plh_stateid;
653 /* Reference matched in nfs4_layoutreturn_release */
654 get_layout_hdr(lo);
Fred Isamanea0ded72011-06-15 12:31:02 -0400655 mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
656 lo->plh_block_lgets++;
Benny Halevycbe82602011-05-22 19:52:37 +0300657 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;
Fred Isaman9e2dfdb2011-06-15 14:32:02 -0400665 set_bit(NFS_LAYOUT_RW_FAILED, &lo->plh_flags);
666 set_bit(NFS_LAYOUT_RO_FAILED, &lo->plh_flags);
Benny Halevy1ed3a852011-06-15 11:39:57 -0400667 put_layout_hdr(lo);
Benny Halevycbe82602011-05-22 19:52:37 +0300668 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;
Trond Myklebusta56aaa02011-06-15 11:59:10 -0400674 lrp->args.layout = lo;
Benny Halevycbe82602011-05-22 19:52:37 +0300675 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
Fred Isamanf7e89172011-01-06 11:36:32 +0000683bool 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
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400760/*
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
Benny Halevyfb3296e2011-05-22 19:47:26 +0300766cmp_layout(struct pnfs_layout_range *l1,
767 struct pnfs_layout_range *l2)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400768{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300769 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
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400781 /* read > read/write */
Benny Halevyfb3296e2011-05-22 19:47:26 +0300782 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400783}
784
Andy Adamson974cec82010-10-20 00:18:02 -0400785static void
786pnfs_insert_layout(struct pnfs_layout_hdr *lo,
787 struct pnfs_layout_segment *lseg)
788{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400789 struct pnfs_layout_segment *lp;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400790
Andy Adamson974cec82010-10-20 00:18:02 -0400791 dprintk("%s:Begin\n", __func__);
792
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000793 assert_spin_locked(&lo->plh_inode->i_lock);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000794 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
Benny Halevyfb3296e2011-05-22 19:47:26 +0300795 if (cmp_layout(&lseg->pls_range, &lp->pls_range) > 0)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400796 continue;
Fred Isaman566052c2011-01-06 11:36:20 +0000797 list_add_tail(&lseg->pls_list, &lp->pls_list);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400798 dprintk("%s: inserted lseg %p "
799 "iomode %d offset %llu length %llu before "
800 "lp %p iomode %d offset %llu length %llu\n",
Fred Isaman566052c2011-01-06 11:36:20 +0000801 __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);
Benny Halevyfb3296e2011-05-22 19:47:26 +0300805 goto out;
Andy Adamson974cec82010-10-20 00:18:02 -0400806 }
Benny Halevyfb3296e2011-05-22 19:47:26 +0300807 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:
Fred Isamancc6e5342011-01-06 11:36:28 +0000813 get_layout_hdr(lo);
Andy Adamson974cec82010-10-20 00:18:02 -0400814
815 dprintk("%s:Return\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -0400816}
817
818static struct pnfs_layout_hdr *
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400819alloc_init_layout_hdr(struct inode *ino, gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400820{
821 struct pnfs_layout_hdr *lo;
822
Benny Halevy636fb9c2011-05-22 19:51:33 +0300823 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400824 if (!lo)
825 return NULL;
Fred Isamancc6e5342011-01-06 11:36:28 +0000826 atomic_set(&lo->plh_refcount, 1);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000827 INIT_LIST_HEAD(&lo->plh_layouts);
828 INIT_LIST_HEAD(&lo->plh_segs);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000829 INIT_LIST_HEAD(&lo->plh_bulk_recall);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000830 lo->plh_inode = ino;
Benny Halevye5e94012010-10-20 00:18:01 -0400831 return lo;
832}
833
834static struct pnfs_layout_hdr *
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400835pnfs_find_alloc_layout(struct inode *ino, gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400836{
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);
Fred Isaman4541d162011-01-06 11:36:23 +0000843 if (nfsi->layout) {
844 if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
845 return NULL;
846 else
847 return nfsi->layout;
848 }
Benny Halevye5e94012010-10-20 00:18:01 -0400849 spin_unlock(&ino->i_lock);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400850 new = alloc_init_layout_hdr(ino, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400851 spin_lock(&ino->i_lock);
852
853 if (likely(nfsi->layout == NULL)) /* Won the race? */
854 nfsi->layout = new;
855 else
Benny Halevy636fb9c2011-05-22 19:51:33 +0300856 pnfs_free_layout_hdr(new);
Benny Halevye5e94012010-10-20 00:18:01 -0400857 return nfsi->layout;
858}
859
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400860/*
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
Benny Halevyfb3296e2011-05-22 19:47:26 +0300872is_matching_lseg(struct pnfs_layout_range *ls_range,
873 struct pnfs_layout_range *range)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400874{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300875 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);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400886}
887
888/*
889 * lookup range in layout
890 */
Benny Halevye5e94012010-10-20 00:18:01 -0400891static struct pnfs_layout_segment *
Benny Halevyfb3296e2011-05-22 19:47:26 +0300892pnfs_find_lseg(struct pnfs_layout_hdr *lo,
893 struct pnfs_layout_range *range)
Benny Halevye5e94012010-10-20 00:18:01 -0400894{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400895 struct pnfs_layout_segment *lseg, *ret = NULL;
896
897 dprintk("%s:Begin\n", __func__);
898
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000899 assert_spin_locked(&lo->plh_inode->i_lock);
900 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
Fred Isaman4541d162011-01-06 11:36:23 +0000901 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
Benny Halevyfb3296e2011-05-22 19:47:26 +0300902 is_matching_lseg(&lseg->pls_range, range)) {
Fred Isamand684d2a2011-03-01 01:34:13 +0000903 ret = get_lseg(lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400904 break;
905 }
Benny Halevyd771e3a2011-06-14 16:30:16 -0400906 if (lseg->pls_range.offset > range->offset)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400907 break;
908 }
909
910 dprintk("%s:Return lseg %p ref %d\n",
Fred Isaman4541d162011-01-06 11:36:23 +0000911 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400912 return ret;
Benny Halevye5e94012010-10-20 00:18:01 -0400913}
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 */
Andy Adamson7c24d942011-06-13 18:22:38 -0400919struct pnfs_layout_segment *
Benny Halevye5e94012010-10-20 00:18:01 -0400920pnfs_update_layout(struct inode *ino,
921 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +0300922 loff_t pos,
923 u64 count,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400924 enum pnfs_iomode iomode,
925 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400926{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300927 struct pnfs_layout_range arg = {
928 .iomode = iomode,
929 .offset = pos,
930 .length = count,
931 };
Benny Halevy707ed5f2011-05-22 19:47:46 +0300932 unsigned pg_offset;
Benny Halevye5e94012010-10-20 00:18:01 -0400933 struct nfs_inode *nfsi = NFS_I(ino);
Weston Andros Adamson6382a442011-06-01 16:44:44 -0400934 struct nfs_server *server = NFS_SERVER(ino);
935 struct nfs_client *clp = server->nfs_client;
Benny Halevye5e94012010-10-20 00:18:01 -0400936 struct pnfs_layout_hdr *lo;
937 struct pnfs_layout_segment *lseg = NULL;
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000938 bool first = false;
Benny Halevye5e94012010-10-20 00:18:01 -0400939
940 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
941 return NULL;
942 spin_lock(&ino->i_lock);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400943 lo = pnfs_find_alloc_layout(ino, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400944 if (lo == NULL) {
945 dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
946 goto out_unlock;
947 }
948
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000949 /* 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__);
Benny Halevye5e94012010-10-20 00:18:01 -0400953 goto out_unlock;
954 }
955
956 /* if LAYOUTGET already failed once we don't try again */
Fred Isaman566052c2011-01-06 11:36:20 +0000957 if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
Benny Halevye5e94012010-10-20 00:18:01 -0400958 goto out_unlock;
959
Andy Adamson568e8c42011-03-01 01:34:22 +0000960 /* Check to see if the layout for the given range already exists */
Benny Halevyfb3296e2011-05-22 19:47:26 +0300961 lseg = pnfs_find_lseg(lo, &arg);
Andy Adamson568e8c42011-03-01 01:34:22 +0000962 if (lseg)
963 goto out_unlock;
964
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000965 if (pnfs_layoutgets_blocked(lo, NULL, 0))
Fred Isamancf7d63f2011-01-06 11:36:25 +0000966 goto out_unlock;
967 atomic_inc(&lo->plh_outstanding);
968
Fred Isamancc6e5342011-01-06 11:36:28 +0000969 get_layout_hdr(lo);
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000970 if (list_empty(&lo->plh_segs))
971 first = true;
972 spin_unlock(&ino->i_lock);
973 if (first) {
Fred Isaman2130ff62011-01-06 11:36:26 +0000974 /* 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));
Weston Andros Adamson6382a442011-06-01 16:44:44 -0400979 list_add_tail(&lo->plh_layouts, &server->layouts);
Fred Isaman2130ff62011-01-06 11:36:26 +0000980 spin_unlock(&clp->cl_lock);
981 }
Benny Halevye5e94012010-10-20 00:18:01 -0400982
Benny Halevy707ed5f2011-05-22 19:47:46 +0300983 pg_offset = arg.offset & ~PAGE_CACHE_MASK;
984 if (pg_offset) {
985 arg.offset -= pg_offset;
986 arg.length += pg_offset;
987 }
Andy Adamson7c24d942011-06-13 18:22:38 -0400988 if (arg.length != NFS4_MAX_UINT64)
989 arg.length = PAGE_CACHE_ALIGN(arg.length);
Benny Halevy707ed5f2011-05-22 19:47:46 +0300990
Benny Halevyfb3296e2011-05-22 19:47:26 +0300991 lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000992 if (!lseg && first) {
993 spin_lock(&clp->cl_lock);
994 list_del_init(&lo->plh_layouts);
995 spin_unlock(&clp->cl_lock);
Fred Isaman2130ff62011-01-06 11:36:26 +0000996 }
Fred Isamancf7d63f2011-01-06 11:36:25 +0000997 atomic_dec(&lo->plh_outstanding);
Fred Isamancc6e5342011-01-06 11:36:28 +0000998 put_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400999out:
1000 dprintk("%s end, state 0x%lx lseg %p\n", __func__,
Andy Adamsonbf9c1382011-03-01 01:34:07 +00001001 nfsi->layout ? nfsi->layout->plh_flags : -1, lseg);
Benny Halevye5e94012010-10-20 00:18:01 -04001002 return lseg;
1003out_unlock:
1004 spin_unlock(&ino->i_lock);
1005 goto out;
1006}
Andy Adamson7c24d942011-06-13 18:22:38 -04001007EXPORT_SYMBOL_GPL(pnfs_update_layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001008
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;
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001015 struct inode *ino = lo->plh_inode;
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001016 struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001017 int status = 0;
1018
1019 /* Inject layout blob into I/O device driver */
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001020 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001021 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);
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001032 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 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001042 init_lseg(lo, lseg);
Fred Isaman566052c2011-01-06 11:36:20 +00001043 lseg->pls_range = res->range;
Fred Isamand684d2a2011-03-01 01:34:13 +00001044 *lgp->lsegpp = get_lseg(lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001045 pnfs_insert_layout(lo, lseg);
1046
Fred Isamanf7e89172011-01-06 11:36:32 +00001047 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
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001052 /* Done processing layoutget. Set the layout stateid */
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001053 pnfs_set_layout_stateid(lo, &res->stateid, false);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001054 spin_unlock(&ino->i_lock);
1055out:
1056 return status;
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001057
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;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001063}
1064
Trond Myklebustd8007d42011-06-10 13:30:23 -04001065void
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);
Trond Myklebuste885de12011-06-10 13:30:23 -04001076 /* 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
Trond Myklebustd8007d42011-06-10 13:30:23 -04001080}
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);
Trond Myklebuste885de12011-06-10 13:30:23 -04001094 /* 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);
Trond Myklebustd8007d42011-06-10 13:30:23 -04001097}
1098EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
1099
Benny Halevy18ad0a92011-05-25 21:03:56 +03001100bool
Trond Myklebust1751c362011-06-10 13:30:23 -04001101pnfs_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
1124bool
Benny Halevydfed2062011-05-25 20:25:22 +03001125pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
1126 struct nfs_page *req)
Fred Isamanbae724e2011-03-01 01:34:15 +00001127{
Trond Myklebustd8007d42011-06-10 13:30:23 -04001128 if (pgio->pg_lseg == NULL)
1129 return nfs_generic_pg_test(pgio, prev, req);
Benny Halevy89a58e32011-05-25 20:54:40 +03001130
Trond Myklebust19982ba2011-06-10 13:30:23 -04001131 /*
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);
Fred Isamanbae724e2011-03-01 01:34:15 +00001147}
Benny Halevy89a58e32011-05-25 20:54:40 +03001148EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
Fred Isamanbae724e2011-03-01 01:34:15 +00001149
Benny Halevyd20581a2011-05-22 19:52:03 +03001150/*
1151 * Called by non rpc-based layout drivers
1152 */
1153int
1154pnfs_ld_write_done(struct nfs_write_data *data)
Fred Isaman94ad1c82011-03-01 01:34:14 +00001155{
Benny Halevyd20581a2011-05-22 19:52:03 +03001156 int status;
Fred Isaman94ad1c82011-03-01 01:34:14 +00001157
Benny Halevyd20581a2011-05-22 19:52:03 +03001158 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;
Fred Isaman44b83792011-03-03 15:13:44 +00001163 }
Fred Isaman44b83792011-03-03 15:13:44 +00001164
Benny Halevyd20581a2011-05-22 19:52:03 +03001165 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;
Fred Isaman44b83792011-03-03 15:13:44 +00001170}
Benny Halevyd20581a2011-05-22 19:52:03 +03001171EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
Fred Isaman44b83792011-03-03 15:13:44 +00001172
Andy Adamson0382b742011-03-03 15:13:45 +00001173enum pnfs_try_status
1174pnfs_try_to_write_data(struct nfs_write_data *wdata,
1175 const struct rpc_call_ops *call_ops, int how)
1176{
1177 struct inode *inode = wdata->inode;
1178 enum pnfs_try_status trypnfs;
1179 struct nfs_server *nfss = NFS_SERVER(inode);
1180
1181 wdata->mds_ops = call_ops;
1182
1183 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
1184 inode->i_ino, wdata->args.count, wdata->args.offset, how);
1185
1186 trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how);
1187 if (trypnfs == PNFS_NOT_ATTEMPTED) {
1188 put_lseg(wdata->lseg);
1189 wdata->lseg = NULL;
1190 } else
1191 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
1192
1193 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1194 return trypnfs;
1195}
1196
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001197/*
Benny Halevyd20581a2011-05-22 19:52:03 +03001198 * Called by non rpc-based layout drivers
1199 */
1200int
1201pnfs_ld_read_done(struct nfs_read_data *data)
1202{
1203 int status;
1204
1205 if (!data->pnfs_error) {
1206 __nfs4_read_done_cb(data);
1207 data->mds_ops->rpc_call_done(&data->task, data);
1208 data->mds_ops->rpc_release(data);
1209 return 0;
1210 }
1211
1212 dprintk("%s: pnfs_error=%d, retry via MDS\n", __func__,
1213 data->pnfs_error);
1214 status = nfs_initiate_read(data, NFS_CLIENT(data->inode),
1215 data->mds_ops);
1216 return status ? : -EAGAIN;
1217}
1218EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
1219
Trond Myklebust493292d2011-07-13 15:58:28 -04001220static void
1221pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
1222 struct nfs_read_data *data)
1223{
1224 list_splice_tail_init(&data->pages, &desc->pg_list);
1225 if (data->req && list_empty(&data->req->wb_list))
1226 nfs_list_add_request(data->req, &desc->pg_list);
1227 nfs_pageio_reset_read_mds(desc);
1228 desc->pg_recoalesce = 1;
1229 nfs_readdata_release(data);
1230}
1231
Benny Halevyd20581a2011-05-22 19:52:03 +03001232/*
Andy Adamson64419a92011-03-01 01:34:16 +00001233 * Call the appropriate parallel I/O subsystem read function.
1234 */
Trond Myklebust493292d2011-07-13 15:58:28 -04001235static enum pnfs_try_status
Andy Adamson64419a92011-03-01 01:34:16 +00001236pnfs_try_to_read_data(struct nfs_read_data *rdata,
Trond Myklebust493292d2011-07-13 15:58:28 -04001237 const struct rpc_call_ops *call_ops,
1238 struct pnfs_layout_segment *lseg)
Andy Adamson64419a92011-03-01 01:34:16 +00001239{
1240 struct inode *inode = rdata->inode;
1241 struct nfs_server *nfss = NFS_SERVER(inode);
1242 enum pnfs_try_status trypnfs;
1243
1244 rdata->mds_ops = call_ops;
Trond Myklebust493292d2011-07-13 15:58:28 -04001245 rdata->lseg = get_lseg(lseg);
Andy Adamson64419a92011-03-01 01:34:16 +00001246
1247 dprintk("%s: Reading ino:%lu %u@%llu\n",
1248 __func__, inode->i_ino, rdata->args.count, rdata->args.offset);
1249
1250 trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata);
1251 if (trypnfs == PNFS_NOT_ATTEMPTED) {
1252 put_lseg(rdata->lseg);
1253 rdata->lseg = NULL;
1254 } else {
1255 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
1256 }
1257 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1258 return trypnfs;
1259}
Andy Adamson863a3c62011-03-23 13:27:54 +00001260
Trond Myklebust493292d2011-07-13 15:58:28 -04001261static void
1262pnfs_do_multiple_reads(struct nfs_pageio_descriptor *desc, struct list_head *head)
1263{
1264 struct nfs_read_data *data;
1265 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1266 struct pnfs_layout_segment *lseg = desc->pg_lseg;
1267
1268 desc->pg_lseg = NULL;
1269 while (!list_empty(head)) {
1270 enum pnfs_try_status trypnfs;
1271
1272 data = list_entry(head->next, struct nfs_read_data, list);
1273 list_del_init(&data->list);
1274
1275 trypnfs = pnfs_try_to_read_data(data, call_ops, lseg);
1276 if (trypnfs == PNFS_NOT_ATTEMPTED)
1277 pnfs_read_through_mds(desc, data);
1278 }
1279 put_lseg(lseg);
1280}
1281
1282int
1283pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
1284{
1285 LIST_HEAD(head);
1286 int ret;
1287
1288 ret = nfs_generic_pagein(desc, &head);
1289 if (ret != 0) {
1290 put_lseg(desc->pg_lseg);
1291 desc->pg_lseg = NULL;
1292 return ret;
1293 }
1294 pnfs_do_multiple_reads(desc, &head);
1295 return 0;
1296}
1297EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
1298
Andy Adamson863a3c62011-03-23 13:27:54 +00001299/*
1300 * Currently there is only one (whole file) write lseg.
1301 */
1302static struct pnfs_layout_segment *pnfs_list_write_lseg(struct inode *inode)
1303{
1304 struct pnfs_layout_segment *lseg, *rv = NULL;
1305
1306 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list)
1307 if (lseg->pls_range.iomode == IOMODE_RW)
1308 rv = lseg;
1309 return rv;
1310}
1311
1312void
1313pnfs_set_layoutcommit(struct nfs_write_data *wdata)
1314{
1315 struct nfs_inode *nfsi = NFS_I(wdata->inode);
Vitaliy Gusev4b8ee2b2011-05-20 01:34:46 +04001316 loff_t end_pos = wdata->mds_offset + wdata->res.count;
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001317 bool mark_as_dirty = false;
Andy Adamson863a3c62011-03-23 13:27:54 +00001318
1319 spin_lock(&nfsi->vfs_inode.i_lock);
1320 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1321 /* references matched in nfs4_layoutcommit_release */
1322 get_lseg(wdata->lseg);
1323 wdata->lseg->pls_lc_cred =
1324 get_rpccred(wdata->args.context->state->owner->so_cred);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001325 mark_as_dirty = true;
Andy Adamson863a3c62011-03-23 13:27:54 +00001326 dprintk("%s: Set layoutcommit for inode %lu ",
1327 __func__, wdata->inode->i_ino);
1328 }
1329 if (end_pos > wdata->lseg->pls_end_pos)
1330 wdata->lseg->pls_end_pos = end_pos;
1331 spin_unlock(&nfsi->vfs_inode.i_lock);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001332
1333 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
1334 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
1335 if (mark_as_dirty)
1336 mark_inode_dirty_sync(wdata->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00001337}
1338EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
1339
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001340/*
1341 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
1342 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
1343 * data to disk to allow the server to recover the data if it crashes.
1344 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
1345 * is off, and a COMMIT is sent to a data server, or
1346 * if WRITEs to a data server return NFS_DATA_SYNC.
1347 */
Andy Adamson863a3c62011-03-23 13:27:54 +00001348int
Andy Adamsonef311532011-03-12 02:58:10 -05001349pnfs_layoutcommit_inode(struct inode *inode, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00001350{
1351 struct nfs4_layoutcommit_data *data;
1352 struct nfs_inode *nfsi = NFS_I(inode);
1353 struct pnfs_layout_segment *lseg;
1354 struct rpc_cred *cred;
1355 loff_t end_pos;
1356 int status = 0;
1357
1358 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
1359
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001360 if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
1361 return 0;
1362
Andy Adamson863a3c62011-03-23 13:27:54 +00001363 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
1364 data = kzalloc(sizeof(*data), GFP_NOFS);
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001365 if (!data) {
1366 mark_inode_dirty_sync(inode);
1367 status = -ENOMEM;
1368 goto out;
1369 }
Andy Adamson863a3c62011-03-23 13:27:54 +00001370
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001371 spin_lock(&inode->i_lock);
Andy Adamson863a3c62011-03-23 13:27:54 +00001372 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1373 spin_unlock(&inode->i_lock);
1374 kfree(data);
1375 goto out;
1376 }
1377 /*
1378 * Currently only one (whole file) write lseg which is referenced
1379 * in pnfs_set_layoutcommit and will be found.
1380 */
1381 lseg = pnfs_list_write_lseg(inode);
1382
1383 end_pos = lseg->pls_end_pos;
1384 cred = lseg->pls_lc_cred;
1385 lseg->pls_end_pos = 0;
1386 lseg->pls_lc_cred = NULL;
1387
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001388 memcpy(&data->args.stateid.data, nfsi->layout->plh_stateid.data,
1389 sizeof(nfsi->layout->plh_stateid.data));
Andy Adamson863a3c62011-03-23 13:27:54 +00001390 spin_unlock(&inode->i_lock);
1391
1392 data->args.inode = inode;
1393 data->lseg = lseg;
1394 data->cred = cred;
1395 nfs_fattr_init(&data->fattr);
1396 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
1397 data->res.fattr = &data->fattr;
1398 data->args.lastbytewritten = end_pos - 1;
1399 data->res.server = NFS_SERVER(inode);
1400
1401 status = nfs4_proc_layoutcommit(data, sync);
1402out:
1403 dprintk("<-- %s status %d\n", __func__, status);
1404 return status;
1405}