blob: ab2cb04f8a284c805d58825d8b85663ae6112193 [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;
Peng Tao9fa40752011-07-30 20:52:32 -0400193 put_rpccred(lo->plh_lc_cred);
Benny Halevy636fb9c2011-05-22 19:51:33 +0300194 return ld->alloc_layout_hdr ? ld->free_layout_hdr(lo) : kfree(lo);
195}
196
Fred Isamancc6e5342011-01-06 11:36:28 +0000197static void
198destroy_layout_hdr(struct pnfs_layout_hdr *lo)
199{
200 dprintk("%s: freeing layout cache %p\n", __func__, lo);
201 BUG_ON(!list_empty(&lo->plh_layouts));
202 NFS_I(lo->plh_inode)->layout = NULL;
Benny Halevy636fb9c2011-05-22 19:51:33 +0300203 pnfs_free_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400204}
205
206static void
207put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
208{
Fred Isamancc6e5342011-01-06 11:36:28 +0000209 if (atomic_dec_and_test(&lo->plh_refcount))
210 destroy_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400211}
212
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400213void
Fred Isamancc6e5342011-01-06 11:36:28 +0000214put_layout_hdr(struct pnfs_layout_hdr *lo)
Andy Adamson974cec82010-10-20 00:18:02 -0400215{
Fred Isamancc6e5342011-01-06 11:36:28 +0000216 struct inode *inode = lo->plh_inode;
217
218 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
219 destroy_layout_hdr(lo);
220 spin_unlock(&inode->i_lock);
221 }
Andy Adamson974cec82010-10-20 00:18:02 -0400222}
223
224static void
225init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
226{
Fred Isaman566052c2011-01-06 11:36:20 +0000227 INIT_LIST_HEAD(&lseg->pls_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000228 atomic_set(&lseg->pls_refcount, 1);
229 smp_mb();
230 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
Fred Isaman566052c2011-01-06 11:36:20 +0000231 lseg->pls_layout = lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400232}
233
Fred Isaman4541d162011-01-06 11:36:23 +0000234static void free_lseg(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400235{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000236 struct inode *ino = lseg->pls_layout->plh_inode;
Andy Adamson974cec82010-10-20 00:18:02 -0400237
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400238 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
Fred Isaman52fabd72011-01-06 11:36:18 +0000239 /* Matched by get_layout_hdr in pnfs_insert_layout */
Fred Isamancc6e5342011-01-06 11:36:28 +0000240 put_layout_hdr(NFS_I(ino)->layout);
Andy Adamson974cec82010-10-20 00:18:02 -0400241}
242
Fred Isamand684d2a2011-03-01 01:34:13 +0000243static void
244put_lseg_common(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400245{
Fred Isamand684d2a2011-03-01 01:34:13 +0000246 struct inode *inode = lseg->pls_layout->plh_inode;
247
Benny Halevyd20581a2011-05-22 19:52:03 +0300248 WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Fred Isamand684d2a2011-03-01 01:34:13 +0000249 list_del_init(&lseg->pls_list);
250 if (list_empty(&lseg->pls_layout->plh_segs)) {
251 set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags);
252 /* Matched by initial refcount set in alloc_init_layout_hdr */
253 put_layout_hdr_locked(lseg->pls_layout);
254 }
255 rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
256}
257
Fred Isamanbae724e2011-03-01 01:34:15 +0000258void
Fred Isamand684d2a2011-03-01 01:34:13 +0000259put_lseg(struct pnfs_layout_segment *lseg)
260{
261 struct inode *inode;
262
263 if (!lseg)
264 return;
265
Fred Isaman4541d162011-01-06 11:36:23 +0000266 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
267 atomic_read(&lseg->pls_refcount),
268 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Fred Isamand684d2a2011-03-01 01:34:13 +0000269 inode = lseg->pls_layout->plh_inode;
270 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
271 LIST_HEAD(free_me);
Andy Adamson974cec82010-10-20 00:18:02 -0400272
Fred Isamand684d2a2011-03-01 01:34:13 +0000273 put_lseg_common(lseg);
274 list_add(&lseg->pls_list, &free_me);
275 spin_unlock(&inode->i_lock);
276 pnfs_free_lseg_list(&free_me);
Fred Isaman4541d162011-01-06 11:36:23 +0000277 }
Andy Adamson974cec82010-10-20 00:18:02 -0400278}
Fred Isamane0c2b382011-03-23 13:27:53 +0000279EXPORT_SYMBOL_GPL(put_lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400280
Benny Halevyfb3296e2011-05-22 19:47:26 +0300281static inline u64
282end_offset(u64 start, u64 len)
Fred Isaman4541d162011-01-06 11:36:23 +0000283{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300284 u64 end;
285
286 end = start + len;
287 return end >= start ? end : NFS4_MAX_UINT64;
288}
289
290/* last octet in a range */
291static inline u64
292last_byte_offset(u64 start, u64 len)
293{
294 u64 end;
295
296 BUG_ON(!len);
297 end = start + len;
298 return end > start ? end - 1 : NFS4_MAX_UINT64;
299}
300
301/*
302 * is l2 fully contained in l1?
303 * start1 end1
304 * [----------------------------------)
305 * start2 end2
306 * [----------------)
307 */
308static inline int
309lo_seg_contained(struct pnfs_layout_range *l1,
310 struct pnfs_layout_range *l2)
311{
312 u64 start1 = l1->offset;
313 u64 end1 = end_offset(start1, l1->length);
314 u64 start2 = l2->offset;
315 u64 end2 = end_offset(start2, l2->length);
316
317 return (start1 <= start2) && (end1 >= end2);
318}
319
320/*
321 * is l1 and l2 intersecting?
322 * start1 end1
323 * [----------------------------------)
324 * start2 end2
325 * [----------------)
326 */
327static inline int
328lo_seg_intersecting(struct pnfs_layout_range *l1,
329 struct pnfs_layout_range *l2)
330{
331 u64 start1 = l1->offset;
332 u64 end1 = end_offset(start1, l1->length);
333 u64 start2 = l2->offset;
334 u64 end2 = end_offset(start2, l2->length);
335
336 return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
337 (end2 == NFS4_MAX_UINT64 || end2 > start1);
338}
339
Fred Isaman4541d162011-01-06 11:36:23 +0000340static bool
Benny Halevy778b5502011-05-22 19:48:02 +0300341should_free_lseg(struct pnfs_layout_range *lseg_range,
342 struct pnfs_layout_range *recall_range)
Fred Isaman4541d162011-01-06 11:36:23 +0000343{
Benny Halevy778b5502011-05-22 19:48:02 +0300344 return (recall_range->iomode == IOMODE_ANY ||
345 lseg_range->iomode == recall_range->iomode) &&
346 lo_seg_intersecting(lseg_range, recall_range);
Fred Isaman4541d162011-01-06 11:36:23 +0000347}
348
349/* Returns 1 if lseg is removed from list, 0 otherwise */
350static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
351 struct list_head *tmp_list)
352{
353 int rv = 0;
354
355 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
356 /* Remove the reference keeping the lseg in the
357 * list. It will now be removed when all
358 * outstanding io is finished.
359 */
Fred Isamand684d2a2011-03-01 01:34:13 +0000360 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
361 atomic_read(&lseg->pls_refcount));
362 if (atomic_dec_and_test(&lseg->pls_refcount)) {
363 put_lseg_common(lseg);
364 list_add(&lseg->pls_list, tmp_list);
365 rv = 1;
366 }
Fred Isaman4541d162011-01-06 11:36:23 +0000367 }
368 return rv;
369}
370
371/* Returns count of number of matching invalid lsegs remaining in list
372 * after call.
373 */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000374int
Fred Isaman4541d162011-01-06 11:36:23 +0000375mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
376 struct list_head *tmp_list,
Benny Halevy778b5502011-05-22 19:48:02 +0300377 struct pnfs_layout_range *recall_range)
Andy Adamson974cec82010-10-20 00:18:02 -0400378{
379 struct pnfs_layout_segment *lseg, *next;
Fred Isaman4541d162011-01-06 11:36:23 +0000380 int invalid = 0, removed = 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400381
382 dprintk("%s:Begin lo %p\n", __func__, lo);
383
Fred Isaman38511722011-02-03 18:28:50 +0000384 if (list_empty(&lo->plh_segs)) {
385 if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags))
386 put_layout_hdr_locked(lo);
387 return 0;
388 }
Fred Isaman4541d162011-01-06 11:36:23 +0000389 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
Benny Halevy778b5502011-05-22 19:48:02 +0300390 if (!recall_range ||
391 should_free_lseg(&lseg->pls_range, recall_range)) {
Fred Isaman4541d162011-01-06 11:36:23 +0000392 dprintk("%s: freeing lseg %p iomode %d "
393 "offset %llu length %llu\n", __func__,
394 lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
395 lseg->pls_range.length);
396 invalid++;
397 removed += mark_lseg_invalid(lseg, tmp_list);
398 }
399 dprintk("%s:Return %i\n", __func__, invalid - removed);
400 return invalid - removed;
Andy Adamson974cec82010-10-20 00:18:02 -0400401}
402
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000403/* note free_me must contain lsegs from a single layout_hdr */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000404void
Fred Isaman4541d162011-01-06 11:36:23 +0000405pnfs_free_lseg_list(struct list_head *free_me)
Andy Adamson974cec82010-10-20 00:18:02 -0400406{
Fred Isaman4541d162011-01-06 11:36:23 +0000407 struct pnfs_layout_segment *lseg, *tmp;
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000408 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400409
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000410 if (list_empty(free_me))
411 return;
412
413 lo = list_first_entry(free_me, struct pnfs_layout_segment,
414 pls_list)->pls_layout;
415
416 if (test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) {
417 struct nfs_client *clp;
418
419 clp = NFS_SERVER(lo->plh_inode)->nfs_client;
420 spin_lock(&clp->cl_lock);
421 list_del_init(&lo->plh_layouts);
422 spin_unlock(&clp->cl_lock);
423 }
Fred Isaman4541d162011-01-06 11:36:23 +0000424 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
Fred Isaman566052c2011-01-06 11:36:20 +0000425 list_del(&lseg->pls_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000426 free_lseg(lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400427 }
428}
429
Benny Halevye5e94012010-10-20 00:18:01 -0400430void
431pnfs_destroy_layout(struct nfs_inode *nfsi)
432{
433 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400434 LIST_HEAD(tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400435
436 spin_lock(&nfsi->vfs_inode.i_lock);
437 lo = nfsi->layout;
438 if (lo) {
Fred Isaman38511722011-02-03 18:28:50 +0000439 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
Benny Halevy778b5502011-05-22 19:48:02 +0300440 mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
Benny Halevye5e94012010-10-20 00:18:01 -0400441 }
442 spin_unlock(&nfsi->vfs_inode.i_lock);
Andy Adamson974cec82010-10-20 00:18:02 -0400443 pnfs_free_lseg_list(&tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400444}
445
Andy Adamson974cec82010-10-20 00:18:02 -0400446/*
447 * Called by the state manger to remove all layouts established under an
448 * expired lease.
449 */
450void
451pnfs_destroy_all_layouts(struct nfs_client *clp)
452{
Weston Andros Adamson6382a442011-06-01 16:44:44 -0400453 struct nfs_server *server;
Andy Adamson974cec82010-10-20 00:18:02 -0400454 struct pnfs_layout_hdr *lo;
455 LIST_HEAD(tmp_list);
456
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400457 nfs4_deviceid_mark_client_invalid(clp);
458 nfs4_deviceid_purge_client(clp);
459
Andy Adamson974cec82010-10-20 00:18:02 -0400460 spin_lock(&clp->cl_lock);
Weston Andros Adamson6382a442011-06-01 16:44:44 -0400461 rcu_read_lock();
462 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
463 if (!list_empty(&server->layouts))
464 list_splice_init(&server->layouts, &tmp_list);
465 }
466 rcu_read_unlock();
Andy Adamson974cec82010-10-20 00:18:02 -0400467 spin_unlock(&clp->cl_lock);
468
469 while (!list_empty(&tmp_list)) {
470 lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000471 plh_layouts);
Andy Adamson974cec82010-10-20 00:18:02 -0400472 dprintk("%s freeing layout for inode %lu\n", __func__,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000473 lo->plh_inode->i_ino);
Andy Adamson2887fe42011-05-11 01:19:58 -0400474 list_del_init(&lo->plh_layouts);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000475 pnfs_destroy_layout(NFS_I(lo->plh_inode));
Andy Adamson974cec82010-10-20 00:18:02 -0400476 }
477}
478
Fred Isamanfd6002e2011-01-06 11:36:22 +0000479/* update lo->plh_stateid with new if is more recent */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000480void
481pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
482 bool update_barrier)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400483{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000484 u32 oldseq, newseq;
Andy Adamson974cec82010-10-20 00:18:02 -0400485
Fred Isamanfd6002e2011-01-06 11:36:22 +0000486 oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid);
487 newseq = be32_to_cpu(new->stateid.seqid);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000488 if ((int)(newseq - oldseq) > 0) {
Fred Isamanfd6002e2011-01-06 11:36:22 +0000489 memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid));
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000490 if (update_barrier) {
491 u32 new_barrier = be32_to_cpu(new->stateid.seqid);
492
493 if ((int)(new_barrier - lo->plh_barrier))
494 lo->plh_barrier = new_barrier;
495 } else {
496 /* Because of wraparound, we want to keep the barrier
497 * "close" to the current seqids. It needs to be
498 * within 2**31 to count as "behind", so if it
499 * gets too near that limit, give us a litle leeway
500 * and bring it to within 2**30.
501 * NOTE - and yes, this is all unsigned arithmetic.
502 */
503 if (unlikely((newseq - lo->plh_barrier) > (3 << 29)))
504 lo->plh_barrier = newseq - (1 << 30);
505 }
506 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400507}
508
Fred Isamancf7d63f2011-01-06 11:36:25 +0000509/* lget is set to 1 if called from inside send_layoutget call chain */
510static bool
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000511pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid,
512 int lget)
Fred Isamancf7d63f2011-01-06 11:36:25 +0000513{
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000514 if ((stateid) &&
515 (int)(lo->plh_barrier - be32_to_cpu(stateid->stateid.seqid)) >= 0)
516 return true;
Fred Isamanf7e89172011-01-06 11:36:32 +0000517 return lo->plh_block_lgets ||
Fred Isaman38511722011-02-03 18:28:50 +0000518 test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) ||
Fred Isamanf7e89172011-01-06 11:36:32 +0000519 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000520 (list_empty(&lo->plh_segs) &&
Fred Isamancf7d63f2011-01-06 11:36:25 +0000521 (atomic_read(&lo->plh_outstanding) > lget));
522}
523
Fred Isamanfd6002e2011-01-06 11:36:22 +0000524int
525pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
526 struct nfs4_state *open_state)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400527{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000528 int status = 0;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400529
530 dprintk("--> %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000531 spin_lock(&lo->plh_inode->i_lock);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000532 if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
Fred Isamancf7d63f2011-01-06 11:36:25 +0000533 status = -EAGAIN;
534 } else if (list_empty(&lo->plh_segs)) {
Fred Isamanfd6002e2011-01-06 11:36:22 +0000535 int seq;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400536
Fred Isamanfd6002e2011-01-06 11:36:22 +0000537 do {
538 seq = read_seqbegin(&open_state->seqlock);
539 memcpy(dst->data, open_state->stateid.data,
540 sizeof(open_state->stateid.data));
541 } while (read_seqretry(&open_state->seqlock, seq));
542 } else
543 memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data));
544 spin_unlock(&lo->plh_inode->i_lock);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400545 dprintk("<-- %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000546 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400547}
548
549/*
550* Get layout from server.
551* for now, assume that whole file layouts are requested.
552* arg->offset: 0
553* arg->length: all ones
554*/
Benny Halevye5e94012010-10-20 00:18:01 -0400555static struct pnfs_layout_segment *
556send_layoutget(struct pnfs_layout_hdr *lo,
557 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +0300558 struct pnfs_layout_range *range,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400559 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400560{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000561 struct inode *ino = lo->plh_inode;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400562 struct nfs_server *server = NFS_SERVER(ino);
563 struct nfs4_layoutget *lgp;
564 struct pnfs_layout_segment *lseg = NULL;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400565 struct page **pages = NULL;
566 int i;
567 u32 max_resp_sz, max_pages;
Benny Halevye5e94012010-10-20 00:18:01 -0400568
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400569 dprintk("--> %s\n", __func__);
570
571 BUG_ON(ctx == NULL);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400572 lgp = kzalloc(sizeof(*lgp), gfp_flags);
Fred Isamancf7d63f2011-01-06 11:36:25 +0000573 if (lgp == NULL)
Andy Adamson974cec82010-10-20 00:18:02 -0400574 return NULL;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400575
576 /* allocate pages for xdr post processing */
577 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
578 max_pages = max_resp_sz >> PAGE_SHIFT;
579
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400580 pages = kzalloc(max_pages * sizeof(struct page *), gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400581 if (!pages)
582 goto out_err_free;
583
584 for (i = 0; i < max_pages; i++) {
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400585 pages[i] = alloc_page(gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400586 if (!pages[i])
587 goto out_err_free;
588 }
589
Benny Halevyfb3296e2011-05-22 19:47:26 +0300590 lgp->args.minlength = PAGE_CACHE_SIZE;
591 if (lgp->args.minlength > range->length)
592 lgp->args.minlength = range->length;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400593 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
Benny Halevyfb3296e2011-05-22 19:47:26 +0300594 lgp->args.range = *range;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400595 lgp->args.type = server->pnfs_curr_ld->id;
596 lgp->args.inode = ino;
597 lgp->args.ctx = get_nfs_open_context(ctx);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400598 lgp->args.layout.pages = pages;
599 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400600 lgp->lsegpp = &lseg;
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400601 lgp->gfp_flags = gfp_flags;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400602
603 /* Synchronously retrieve layout information from server and
604 * store in lseg.
605 */
606 nfs4_proc_layoutget(lgp);
607 if (!lseg) {
608 /* remember that LAYOUTGET failed and suspend trying */
Benny Halevyfb3296e2011-05-22 19:47:26 +0300609 set_bit(lo_fail_bit(range->iomode), &lo->plh_flags);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400610 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400611
612 /* free xdr pages */
613 for (i = 0; i < max_pages; i++)
614 __free_page(pages[i]);
615 kfree(pages);
616
Andy Adamson974cec82010-10-20 00:18:02 -0400617 return lseg;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400618
619out_err_free:
620 /* free any allocated xdr pages, lgp as it's not used */
621 if (pages) {
622 for (i = 0; i < max_pages; i++) {
623 if (!pages[i])
624 break;
625 __free_page(pages[i]);
626 }
627 kfree(pages);
628 }
629 kfree(lgp);
630 return NULL;
Andy Adamson974cec82010-10-20 00:18:02 -0400631}
632
Benny Halevycbe82602011-05-22 19:52:37 +0300633/* Initiates a LAYOUTRETURN(FILE) */
634int
635_pnfs_return_layout(struct inode *ino)
636{
637 struct pnfs_layout_hdr *lo = NULL;
638 struct nfs_inode *nfsi = NFS_I(ino);
639 LIST_HEAD(tmp_list);
640 struct nfs4_layoutreturn *lrp;
641 nfs4_stateid stateid;
642 int status = 0;
643
644 dprintk("--> %s\n", __func__);
645
646 spin_lock(&ino->i_lock);
647 lo = nfsi->layout;
Fred Isamana2e1d4f2011-06-13 18:54:53 -0400648 if (!lo) {
Benny Halevycbe82602011-05-22 19:52:37 +0300649 spin_unlock(&ino->i_lock);
Fred Isamana2e1d4f2011-06-13 18:54:53 -0400650 dprintk("%s: no layout to return\n", __func__);
651 return status;
Benny Halevycbe82602011-05-22 19:52:37 +0300652 }
653 stateid = nfsi->layout->plh_stateid;
654 /* Reference matched in nfs4_layoutreturn_release */
655 get_layout_hdr(lo);
Fred Isamanea0ded72011-06-15 12:31:02 -0400656 mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
657 lo->plh_block_lgets++;
Benny Halevycbe82602011-05-22 19:52:37 +0300658 spin_unlock(&ino->i_lock);
659 pnfs_free_lseg_list(&tmp_list);
660
661 WARN_ON(test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags));
662
663 lrp = kzalloc(sizeof(*lrp), GFP_KERNEL);
664 if (unlikely(lrp == NULL)) {
665 status = -ENOMEM;
Fred Isaman9e2dfdb2011-06-15 14:32:02 -0400666 set_bit(NFS_LAYOUT_RW_FAILED, &lo->plh_flags);
667 set_bit(NFS_LAYOUT_RO_FAILED, &lo->plh_flags);
Benny Halevy1ed3a852011-06-15 11:39:57 -0400668 put_layout_hdr(lo);
Benny Halevycbe82602011-05-22 19:52:37 +0300669 goto out;
670 }
671
672 lrp->args.stateid = stateid;
673 lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
674 lrp->args.inode = ino;
Trond Myklebusta56aaa02011-06-15 11:59:10 -0400675 lrp->args.layout = lo;
Benny Halevycbe82602011-05-22 19:52:37 +0300676 lrp->clp = NFS_SERVER(ino)->nfs_client;
677
678 status = nfs4_proc_layoutreturn(lrp);
679out:
680 dprintk("<-- %s status: %d\n", __func__, status);
681 return status;
682}
683
Fred Isamanf7e89172011-01-06 11:36:32 +0000684bool pnfs_roc(struct inode *ino)
685{
686 struct pnfs_layout_hdr *lo;
687 struct pnfs_layout_segment *lseg, *tmp;
688 LIST_HEAD(tmp_list);
689 bool found = false;
690
691 spin_lock(&ino->i_lock);
692 lo = NFS_I(ino)->layout;
693 if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
694 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
695 goto out_nolayout;
696 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
697 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
698 mark_lseg_invalid(lseg, &tmp_list);
699 found = true;
700 }
701 if (!found)
702 goto out_nolayout;
703 lo->plh_block_lgets++;
704 get_layout_hdr(lo); /* matched in pnfs_roc_release */
705 spin_unlock(&ino->i_lock);
706 pnfs_free_lseg_list(&tmp_list);
707 return true;
708
709out_nolayout:
710 spin_unlock(&ino->i_lock);
711 return false;
712}
713
714void pnfs_roc_release(struct inode *ino)
715{
716 struct pnfs_layout_hdr *lo;
717
718 spin_lock(&ino->i_lock);
719 lo = NFS_I(ino)->layout;
720 lo->plh_block_lgets--;
721 put_layout_hdr_locked(lo);
722 spin_unlock(&ino->i_lock);
723}
724
725void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
726{
727 struct pnfs_layout_hdr *lo;
728
729 spin_lock(&ino->i_lock);
730 lo = NFS_I(ino)->layout;
731 if ((int)(barrier - lo->plh_barrier) > 0)
732 lo->plh_barrier = barrier;
733 spin_unlock(&ino->i_lock);
734}
735
736bool pnfs_roc_drain(struct inode *ino, u32 *barrier)
737{
738 struct nfs_inode *nfsi = NFS_I(ino);
739 struct pnfs_layout_segment *lseg;
740 bool found = false;
741
742 spin_lock(&ino->i_lock);
743 list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
744 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
745 found = true;
746 break;
747 }
748 if (!found) {
749 struct pnfs_layout_hdr *lo = nfsi->layout;
750 u32 current_seqid = be32_to_cpu(lo->plh_stateid.stateid.seqid);
751
752 /* Since close does not return a layout stateid for use as
753 * a barrier, we choose the worst-case barrier.
754 */
755 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
756 }
757 spin_unlock(&ino->i_lock);
758 return found;
759}
760
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400761/*
762 * Compare two layout segments for sorting into layout cache.
763 * We want to preferentially return RW over RO layouts, so ensure those
764 * are seen first.
765 */
766static s64
Benny Halevyfb3296e2011-05-22 19:47:26 +0300767cmp_layout(struct pnfs_layout_range *l1,
768 struct pnfs_layout_range *l2)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400769{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300770 s64 d;
771
772 /* high offset > low offset */
773 d = l1->offset - l2->offset;
774 if (d)
775 return d;
776
777 /* short length > long length */
778 d = l2->length - l1->length;
779 if (d)
780 return d;
781
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400782 /* read > read/write */
Benny Halevyfb3296e2011-05-22 19:47:26 +0300783 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400784}
785
Andy Adamson974cec82010-10-20 00:18:02 -0400786static void
787pnfs_insert_layout(struct pnfs_layout_hdr *lo,
788 struct pnfs_layout_segment *lseg)
789{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400790 struct pnfs_layout_segment *lp;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400791
Andy Adamson974cec82010-10-20 00:18:02 -0400792 dprintk("%s:Begin\n", __func__);
793
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000794 assert_spin_locked(&lo->plh_inode->i_lock);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000795 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
Benny Halevyfb3296e2011-05-22 19:47:26 +0300796 if (cmp_layout(&lseg->pls_range, &lp->pls_range) > 0)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400797 continue;
Fred Isaman566052c2011-01-06 11:36:20 +0000798 list_add_tail(&lseg->pls_list, &lp->pls_list);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400799 dprintk("%s: inserted lseg %p "
800 "iomode %d offset %llu length %llu before "
801 "lp %p iomode %d offset %llu length %llu\n",
Fred Isaman566052c2011-01-06 11:36:20 +0000802 __func__, lseg, lseg->pls_range.iomode,
803 lseg->pls_range.offset, lseg->pls_range.length,
804 lp, lp->pls_range.iomode, lp->pls_range.offset,
805 lp->pls_range.length);
Benny Halevyfb3296e2011-05-22 19:47:26 +0300806 goto out;
Andy Adamson974cec82010-10-20 00:18:02 -0400807 }
Benny Halevyfb3296e2011-05-22 19:47:26 +0300808 list_add_tail(&lseg->pls_list, &lo->plh_segs);
809 dprintk("%s: inserted lseg %p "
810 "iomode %d offset %llu length %llu at tail\n",
811 __func__, lseg, lseg->pls_range.iomode,
812 lseg->pls_range.offset, lseg->pls_range.length);
813out:
Fred Isamancc6e5342011-01-06 11:36:28 +0000814 get_layout_hdr(lo);
Andy Adamson974cec82010-10-20 00:18:02 -0400815
816 dprintk("%s:Return\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -0400817}
818
819static struct pnfs_layout_hdr *
Peng Tao9fa40752011-07-30 20:52:32 -0400820alloc_init_layout_hdr(struct inode *ino,
821 struct nfs_open_context *ctx,
822 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400823{
824 struct pnfs_layout_hdr *lo;
825
Benny Halevy636fb9c2011-05-22 19:51:33 +0300826 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400827 if (!lo)
828 return NULL;
Fred Isamancc6e5342011-01-06 11:36:28 +0000829 atomic_set(&lo->plh_refcount, 1);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000830 INIT_LIST_HEAD(&lo->plh_layouts);
831 INIT_LIST_HEAD(&lo->plh_segs);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000832 INIT_LIST_HEAD(&lo->plh_bulk_recall);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000833 lo->plh_inode = ino;
Peng Tao9fa40752011-07-30 20:52:32 -0400834 lo->plh_lc_cred = get_rpccred(ctx->state->owner->so_cred);
Benny Halevye5e94012010-10-20 00:18:01 -0400835 return lo;
836}
837
838static struct pnfs_layout_hdr *
Peng Tao9fa40752011-07-30 20:52:32 -0400839pnfs_find_alloc_layout(struct inode *ino,
840 struct nfs_open_context *ctx,
841 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400842{
843 struct nfs_inode *nfsi = NFS_I(ino);
844 struct pnfs_layout_hdr *new = NULL;
845
846 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
847
848 assert_spin_locked(&ino->i_lock);
Fred Isaman4541d162011-01-06 11:36:23 +0000849 if (nfsi->layout) {
850 if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
851 return NULL;
852 else
853 return nfsi->layout;
854 }
Benny Halevye5e94012010-10-20 00:18:01 -0400855 spin_unlock(&ino->i_lock);
Peng Tao9fa40752011-07-30 20:52:32 -0400856 new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400857 spin_lock(&ino->i_lock);
858
859 if (likely(nfsi->layout == NULL)) /* Won the race? */
860 nfsi->layout = new;
861 else
Benny Halevy636fb9c2011-05-22 19:51:33 +0300862 pnfs_free_layout_hdr(new);
Benny Halevye5e94012010-10-20 00:18:01 -0400863 return nfsi->layout;
864}
865
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400866/*
867 * iomode matching rules:
868 * iomode lseg match
869 * ----- ----- -----
870 * ANY READ true
871 * ANY RW true
872 * RW READ false
873 * RW RW true
874 * READ READ true
875 * READ RW true
876 */
877static int
Benny Halevyfb3296e2011-05-22 19:47:26 +0300878is_matching_lseg(struct pnfs_layout_range *ls_range,
879 struct pnfs_layout_range *range)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400880{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300881 struct pnfs_layout_range range1;
882
883 if ((range->iomode == IOMODE_RW &&
884 ls_range->iomode != IOMODE_RW) ||
885 !lo_seg_intersecting(ls_range, range))
886 return 0;
887
888 /* range1 covers only the first byte in the range */
889 range1 = *range;
890 range1.length = 1;
891 return lo_seg_contained(ls_range, &range1);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400892}
893
894/*
895 * lookup range in layout
896 */
Benny Halevye5e94012010-10-20 00:18:01 -0400897static struct pnfs_layout_segment *
Benny Halevyfb3296e2011-05-22 19:47:26 +0300898pnfs_find_lseg(struct pnfs_layout_hdr *lo,
899 struct pnfs_layout_range *range)
Benny Halevye5e94012010-10-20 00:18:01 -0400900{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400901 struct pnfs_layout_segment *lseg, *ret = NULL;
902
903 dprintk("%s:Begin\n", __func__);
904
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000905 assert_spin_locked(&lo->plh_inode->i_lock);
906 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
Fred Isaman4541d162011-01-06 11:36:23 +0000907 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
Benny Halevyfb3296e2011-05-22 19:47:26 +0300908 is_matching_lseg(&lseg->pls_range, range)) {
Fred Isamand684d2a2011-03-01 01:34:13 +0000909 ret = get_lseg(lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400910 break;
911 }
Benny Halevyd771e3a2011-06-14 16:30:16 -0400912 if (lseg->pls_range.offset > range->offset)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400913 break;
914 }
915
916 dprintk("%s:Return lseg %p ref %d\n",
Fred Isaman4541d162011-01-06 11:36:23 +0000917 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400918 return ret;
Benny Halevye5e94012010-10-20 00:18:01 -0400919}
920
921/*
922 * Layout segment is retreived from the server if not cached.
923 * The appropriate layout segment is referenced and returned to the caller.
924 */
Andy Adamson7c24d942011-06-13 18:22:38 -0400925struct pnfs_layout_segment *
Benny Halevye5e94012010-10-20 00:18:01 -0400926pnfs_update_layout(struct inode *ino,
927 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +0300928 loff_t pos,
929 u64 count,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400930 enum pnfs_iomode iomode,
931 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400932{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300933 struct pnfs_layout_range arg = {
934 .iomode = iomode,
935 .offset = pos,
936 .length = count,
937 };
Benny Halevy707ed5f2011-05-22 19:47:46 +0300938 unsigned pg_offset;
Benny Halevye5e94012010-10-20 00:18:01 -0400939 struct nfs_inode *nfsi = NFS_I(ino);
Weston Andros Adamson6382a442011-06-01 16:44:44 -0400940 struct nfs_server *server = NFS_SERVER(ino);
941 struct nfs_client *clp = server->nfs_client;
Benny Halevye5e94012010-10-20 00:18:01 -0400942 struct pnfs_layout_hdr *lo;
943 struct pnfs_layout_segment *lseg = NULL;
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000944 bool first = false;
Benny Halevye5e94012010-10-20 00:18:01 -0400945
946 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
947 return NULL;
948 spin_lock(&ino->i_lock);
Peng Tao9fa40752011-07-30 20:52:32 -0400949 lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400950 if (lo == NULL) {
951 dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
952 goto out_unlock;
953 }
954
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000955 /* Do we even need to bother with this? */
956 if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
957 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
958 dprintk("%s matches recall, use MDS\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -0400959 goto out_unlock;
960 }
961
962 /* if LAYOUTGET already failed once we don't try again */
Fred Isaman566052c2011-01-06 11:36:20 +0000963 if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
Benny Halevye5e94012010-10-20 00:18:01 -0400964 goto out_unlock;
965
Andy Adamson568e8c42011-03-01 01:34:22 +0000966 /* Check to see if the layout for the given range already exists */
Benny Halevyfb3296e2011-05-22 19:47:26 +0300967 lseg = pnfs_find_lseg(lo, &arg);
Andy Adamson568e8c42011-03-01 01:34:22 +0000968 if (lseg)
969 goto out_unlock;
970
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000971 if (pnfs_layoutgets_blocked(lo, NULL, 0))
Fred Isamancf7d63f2011-01-06 11:36:25 +0000972 goto out_unlock;
973 atomic_inc(&lo->plh_outstanding);
974
Fred Isamancc6e5342011-01-06 11:36:28 +0000975 get_layout_hdr(lo);
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000976 if (list_empty(&lo->plh_segs))
977 first = true;
978 spin_unlock(&ino->i_lock);
979 if (first) {
Fred Isaman2130ff62011-01-06 11:36:26 +0000980 /* The lo must be on the clp list if there is any
981 * chance of a CB_LAYOUTRECALL(FILE) coming in.
982 */
983 spin_lock(&clp->cl_lock);
984 BUG_ON(!list_empty(&lo->plh_layouts));
Weston Andros Adamson6382a442011-06-01 16:44:44 -0400985 list_add_tail(&lo->plh_layouts, &server->layouts);
Fred Isaman2130ff62011-01-06 11:36:26 +0000986 spin_unlock(&clp->cl_lock);
987 }
Benny Halevye5e94012010-10-20 00:18:01 -0400988
Benny Halevy707ed5f2011-05-22 19:47:46 +0300989 pg_offset = arg.offset & ~PAGE_CACHE_MASK;
990 if (pg_offset) {
991 arg.offset -= pg_offset;
992 arg.length += pg_offset;
993 }
Andy Adamson7c24d942011-06-13 18:22:38 -0400994 if (arg.length != NFS4_MAX_UINT64)
995 arg.length = PAGE_CACHE_ALIGN(arg.length);
Benny Halevy707ed5f2011-05-22 19:47:46 +0300996
Benny Halevyfb3296e2011-05-22 19:47:26 +0300997 lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000998 if (!lseg && first) {
999 spin_lock(&clp->cl_lock);
1000 list_del_init(&lo->plh_layouts);
1001 spin_unlock(&clp->cl_lock);
Fred Isaman2130ff62011-01-06 11:36:26 +00001002 }
Fred Isamancf7d63f2011-01-06 11:36:25 +00001003 atomic_dec(&lo->plh_outstanding);
Fred Isamancc6e5342011-01-06 11:36:28 +00001004 put_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -04001005out:
1006 dprintk("%s end, state 0x%lx lseg %p\n", __func__,
Andy Adamsonbf9c1382011-03-01 01:34:07 +00001007 nfsi->layout ? nfsi->layout->plh_flags : -1, lseg);
Benny Halevye5e94012010-10-20 00:18:01 -04001008 return lseg;
1009out_unlock:
1010 spin_unlock(&ino->i_lock);
1011 goto out;
1012}
Andy Adamson7c24d942011-06-13 18:22:38 -04001013EXPORT_SYMBOL_GPL(pnfs_update_layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001014
1015int
1016pnfs_layout_process(struct nfs4_layoutget *lgp)
1017{
1018 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
1019 struct nfs4_layoutget_res *res = &lgp->res;
1020 struct pnfs_layout_segment *lseg;
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001021 struct inode *ino = lo->plh_inode;
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001022 struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001023 int status = 0;
1024
1025 /* Inject layout blob into I/O device driver */
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001026 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001027 if (!lseg || IS_ERR(lseg)) {
1028 if (!lseg)
1029 status = -ENOMEM;
1030 else
1031 status = PTR_ERR(lseg);
1032 dprintk("%s: Could not allocate layout: error %d\n",
1033 __func__, status);
1034 goto out;
1035 }
1036
1037 spin_lock(&ino->i_lock);
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001038 if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
1039 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1040 dprintk("%s forget reply due to recall\n", __func__);
1041 goto out_forget_reply;
1042 }
1043
1044 if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) {
1045 dprintk("%s forget reply due to state\n", __func__);
1046 goto out_forget_reply;
1047 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001048 init_lseg(lo, lseg);
Fred Isaman566052c2011-01-06 11:36:20 +00001049 lseg->pls_range = res->range;
Fred Isamand684d2a2011-03-01 01:34:13 +00001050 *lgp->lsegpp = get_lseg(lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001051 pnfs_insert_layout(lo, lseg);
1052
Fred Isamanf7e89172011-01-06 11:36:32 +00001053 if (res->return_on_close) {
1054 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
1055 set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
1056 }
1057
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001058 /* Done processing layoutget. Set the layout stateid */
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001059 pnfs_set_layout_stateid(lo, &res->stateid, false);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001060 spin_unlock(&ino->i_lock);
1061out:
1062 return status;
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001063
1064out_forget_reply:
1065 spin_unlock(&ino->i_lock);
1066 lseg->pls_layout = lo;
1067 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
1068 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001069}
1070
Trond Myklebustd8007d42011-06-10 13:30:23 -04001071void
1072pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1073{
1074 BUG_ON(pgio->pg_lseg != NULL);
1075
1076 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1077 req->wb_context,
1078 req_offset(req),
1079 req->wb_bytes,
1080 IOMODE_READ,
1081 GFP_KERNEL);
Trond Myklebuste885de12011-06-10 13:30:23 -04001082 /* If no lseg, fall back to read through mds */
1083 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -04001084 nfs_pageio_reset_read_mds(pgio);
Trond Myklebuste885de12011-06-10 13:30:23 -04001085
Trond Myklebustd8007d42011-06-10 13:30:23 -04001086}
1087EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
1088
1089void
1090pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1091{
1092 BUG_ON(pgio->pg_lseg != NULL);
1093
1094 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1095 req->wb_context,
1096 req_offset(req),
1097 req->wb_bytes,
1098 IOMODE_RW,
1099 GFP_NOFS);
Trond Myklebuste885de12011-06-10 13:30:23 -04001100 /* If no lseg, fall back to write through mds */
1101 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -04001102 nfs_pageio_reset_write_mds(pgio);
Trond Myklebustd8007d42011-06-10 13:30:23 -04001103}
1104EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
1105
Benny Halevy18ad0a92011-05-25 21:03:56 +03001106bool
Trond Myklebust1751c362011-06-10 13:30:23 -04001107pnfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, struct inode *inode)
1108{
1109 struct nfs_server *server = NFS_SERVER(inode);
1110 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
1111
1112 if (ld == NULL)
1113 return false;
1114 nfs_pageio_init(pgio, inode, ld->pg_read_ops, server->rsize, 0);
1115 return true;
1116}
1117
1118bool
1119pnfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, struct inode *inode, int ioflags)
1120{
1121 struct nfs_server *server = NFS_SERVER(inode);
1122 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
1123
1124 if (ld == NULL)
1125 return false;
1126 nfs_pageio_init(pgio, inode, ld->pg_write_ops, server->wsize, ioflags);
1127 return true;
1128}
1129
1130bool
Benny Halevydfed2062011-05-25 20:25:22 +03001131pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
1132 struct nfs_page *req)
Fred Isamanbae724e2011-03-01 01:34:15 +00001133{
Trond Myklebustd8007d42011-06-10 13:30:23 -04001134 if (pgio->pg_lseg == NULL)
1135 return nfs_generic_pg_test(pgio, prev, req);
Benny Halevy89a58e32011-05-25 20:54:40 +03001136
Trond Myklebust19982ba2011-06-10 13:30:23 -04001137 /*
1138 * Test if a nfs_page is fully contained in the pnfs_layout_range.
1139 * Note that this test makes several assumptions:
1140 * - that the previous nfs_page in the struct nfs_pageio_descriptor
1141 * is known to lie within the range.
1142 * - that the nfs_page being tested is known to be contiguous with the
1143 * previous nfs_page.
1144 * - Layout ranges are page aligned, so we only have to test the
1145 * start offset of the request.
1146 *
1147 * Please also note that 'end_offset' is actually the offset of the
1148 * first byte that lies outside the pnfs_layout_range. FIXME?
1149 *
1150 */
1151 return req_offset(req) < end_offset(pgio->pg_lseg->pls_range.offset,
1152 pgio->pg_lseg->pls_range.length);
Fred Isamanbae724e2011-03-01 01:34:15 +00001153}
Benny Halevy89a58e32011-05-25 20:54:40 +03001154EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
Fred Isamanbae724e2011-03-01 01:34:15 +00001155
Benny Halevyd20581a2011-05-22 19:52:03 +03001156/*
1157 * Called by non rpc-based layout drivers
1158 */
1159int
1160pnfs_ld_write_done(struct nfs_write_data *data)
Fred Isaman94ad1c82011-03-01 01:34:14 +00001161{
Benny Halevyd20581a2011-05-22 19:52:03 +03001162 int status;
Fred Isaman94ad1c82011-03-01 01:34:14 +00001163
Benny Halevyd20581a2011-05-22 19:52:03 +03001164 if (!data->pnfs_error) {
1165 pnfs_set_layoutcommit(data);
1166 data->mds_ops->rpc_call_done(&data->task, data);
1167 data->mds_ops->rpc_release(data);
1168 return 0;
Fred Isaman44b83792011-03-03 15:13:44 +00001169 }
Fred Isaman44b83792011-03-03 15:13:44 +00001170
Benny Halevyd20581a2011-05-22 19:52:03 +03001171 dprintk("%s: pnfs_error=%d, retry via MDS\n", __func__,
1172 data->pnfs_error);
1173 status = nfs_initiate_write(data, NFS_CLIENT(data->inode),
1174 data->mds_ops, NFS_FILE_SYNC);
1175 return status ? : -EAGAIN;
Fred Isaman44b83792011-03-03 15:13:44 +00001176}
Benny Halevyd20581a2011-05-22 19:52:03 +03001177EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
Fred Isaman44b83792011-03-03 15:13:44 +00001178
Trond Myklebustdce81292011-07-13 15:59:19 -04001179static void
1180pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
1181 struct nfs_write_data *data)
1182{
1183 list_splice_tail_init(&data->pages, &desc->pg_list);
1184 if (data->req && list_empty(&data->req->wb_list))
1185 nfs_list_add_request(data->req, &desc->pg_list);
1186 nfs_pageio_reset_write_mds(desc);
1187 desc->pg_recoalesce = 1;
1188 nfs_writedata_release(data);
1189}
1190
1191static enum pnfs_try_status
Andy Adamson0382b742011-03-03 15:13:45 +00001192pnfs_try_to_write_data(struct nfs_write_data *wdata,
Trond Myklebustdce81292011-07-13 15:59:19 -04001193 const struct rpc_call_ops *call_ops,
1194 struct pnfs_layout_segment *lseg,
1195 int how)
Andy Adamson0382b742011-03-03 15:13:45 +00001196{
1197 struct inode *inode = wdata->inode;
1198 enum pnfs_try_status trypnfs;
1199 struct nfs_server *nfss = NFS_SERVER(inode);
1200
1201 wdata->mds_ops = call_ops;
Trond Myklebustdce81292011-07-13 15:59:19 -04001202 wdata->lseg = get_lseg(lseg);
Andy Adamson0382b742011-03-03 15:13:45 +00001203
1204 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
1205 inode->i_ino, wdata->args.count, wdata->args.offset, how);
1206
1207 trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how);
1208 if (trypnfs == PNFS_NOT_ATTEMPTED) {
1209 put_lseg(wdata->lseg);
1210 wdata->lseg = NULL;
1211 } else
1212 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
1213
1214 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1215 return trypnfs;
1216}
1217
Trond Myklebustdce81292011-07-13 15:59:19 -04001218static void
1219pnfs_do_multiple_writes(struct nfs_pageio_descriptor *desc, struct list_head *head, int how)
1220{
1221 struct nfs_write_data *data;
1222 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1223 struct pnfs_layout_segment *lseg = desc->pg_lseg;
1224
1225 desc->pg_lseg = NULL;
1226 while (!list_empty(head)) {
1227 enum pnfs_try_status trypnfs;
1228
1229 data = list_entry(head->next, struct nfs_write_data, list);
1230 list_del_init(&data->list);
1231
1232 trypnfs = pnfs_try_to_write_data(data, call_ops, lseg, how);
1233 if (trypnfs == PNFS_NOT_ATTEMPTED)
1234 pnfs_write_through_mds(desc, data);
1235 }
1236 put_lseg(lseg);
1237}
1238
1239int
1240pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
1241{
1242 LIST_HEAD(head);
1243 int ret;
1244
1245 ret = nfs_generic_flush(desc, &head);
1246 if (ret != 0) {
1247 put_lseg(desc->pg_lseg);
1248 desc->pg_lseg = NULL;
1249 return ret;
1250 }
1251 pnfs_do_multiple_writes(desc, &head, desc->pg_ioflags);
1252 return 0;
1253}
1254EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
1255
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001256/*
Benny Halevyd20581a2011-05-22 19:52:03 +03001257 * Called by non rpc-based layout drivers
1258 */
1259int
1260pnfs_ld_read_done(struct nfs_read_data *data)
1261{
1262 int status;
1263
1264 if (!data->pnfs_error) {
1265 __nfs4_read_done_cb(data);
1266 data->mds_ops->rpc_call_done(&data->task, data);
1267 data->mds_ops->rpc_release(data);
1268 return 0;
1269 }
1270
1271 dprintk("%s: pnfs_error=%d, retry via MDS\n", __func__,
1272 data->pnfs_error);
1273 status = nfs_initiate_read(data, NFS_CLIENT(data->inode),
1274 data->mds_ops);
1275 return status ? : -EAGAIN;
1276}
1277EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
1278
Trond Myklebust493292d2011-07-13 15:58:28 -04001279static void
1280pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
1281 struct nfs_read_data *data)
1282{
1283 list_splice_tail_init(&data->pages, &desc->pg_list);
1284 if (data->req && list_empty(&data->req->wb_list))
1285 nfs_list_add_request(data->req, &desc->pg_list);
1286 nfs_pageio_reset_read_mds(desc);
1287 desc->pg_recoalesce = 1;
1288 nfs_readdata_release(data);
1289}
1290
Benny Halevyd20581a2011-05-22 19:52:03 +03001291/*
Andy Adamson64419a92011-03-01 01:34:16 +00001292 * Call the appropriate parallel I/O subsystem read function.
1293 */
Trond Myklebust493292d2011-07-13 15:58:28 -04001294static enum pnfs_try_status
Andy Adamson64419a92011-03-01 01:34:16 +00001295pnfs_try_to_read_data(struct nfs_read_data *rdata,
Trond Myklebust493292d2011-07-13 15:58:28 -04001296 const struct rpc_call_ops *call_ops,
1297 struct pnfs_layout_segment *lseg)
Andy Adamson64419a92011-03-01 01:34:16 +00001298{
1299 struct inode *inode = rdata->inode;
1300 struct nfs_server *nfss = NFS_SERVER(inode);
1301 enum pnfs_try_status trypnfs;
1302
1303 rdata->mds_ops = call_ops;
Trond Myklebust493292d2011-07-13 15:58:28 -04001304 rdata->lseg = get_lseg(lseg);
Andy Adamson64419a92011-03-01 01:34:16 +00001305
1306 dprintk("%s: Reading ino:%lu %u@%llu\n",
1307 __func__, inode->i_ino, rdata->args.count, rdata->args.offset);
1308
1309 trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata);
1310 if (trypnfs == PNFS_NOT_ATTEMPTED) {
1311 put_lseg(rdata->lseg);
1312 rdata->lseg = NULL;
1313 } else {
1314 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
1315 }
1316 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1317 return trypnfs;
1318}
Andy Adamson863a3c62011-03-23 13:27:54 +00001319
Trond Myklebust493292d2011-07-13 15:58:28 -04001320static void
1321pnfs_do_multiple_reads(struct nfs_pageio_descriptor *desc, struct list_head *head)
1322{
1323 struct nfs_read_data *data;
1324 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1325 struct pnfs_layout_segment *lseg = desc->pg_lseg;
1326
1327 desc->pg_lseg = NULL;
1328 while (!list_empty(head)) {
1329 enum pnfs_try_status trypnfs;
1330
1331 data = list_entry(head->next, struct nfs_read_data, list);
1332 list_del_init(&data->list);
1333
1334 trypnfs = pnfs_try_to_read_data(data, call_ops, lseg);
1335 if (trypnfs == PNFS_NOT_ATTEMPTED)
1336 pnfs_read_through_mds(desc, data);
1337 }
1338 put_lseg(lseg);
1339}
1340
1341int
1342pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
1343{
1344 LIST_HEAD(head);
1345 int ret;
1346
1347 ret = nfs_generic_pagein(desc, &head);
1348 if (ret != 0) {
1349 put_lseg(desc->pg_lseg);
1350 desc->pg_lseg = NULL;
1351 return ret;
1352 }
1353 pnfs_do_multiple_reads(desc, &head);
1354 return 0;
1355}
1356EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
1357
Andy Adamson863a3c62011-03-23 13:27:54 +00001358/*
1359 * Currently there is only one (whole file) write lseg.
1360 */
1361static struct pnfs_layout_segment *pnfs_list_write_lseg(struct inode *inode)
1362{
1363 struct pnfs_layout_segment *lseg, *rv = NULL;
1364
1365 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list)
1366 if (lseg->pls_range.iomode == IOMODE_RW)
1367 rv = lseg;
1368 return rv;
1369}
1370
1371void
1372pnfs_set_layoutcommit(struct nfs_write_data *wdata)
1373{
1374 struct nfs_inode *nfsi = NFS_I(wdata->inode);
Vitaliy Gusev4b8ee2b2011-05-20 01:34:46 +04001375 loff_t end_pos = wdata->mds_offset + wdata->res.count;
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001376 bool mark_as_dirty = false;
Andy Adamson863a3c62011-03-23 13:27:54 +00001377
1378 spin_lock(&nfsi->vfs_inode.i_lock);
1379 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1380 /* references matched in nfs4_layoutcommit_release */
1381 get_lseg(wdata->lseg);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001382 mark_as_dirty = true;
Andy Adamson863a3c62011-03-23 13:27:54 +00001383 dprintk("%s: Set layoutcommit for inode %lu ",
1384 __func__, wdata->inode->i_ino);
1385 }
Peng Taoacff58802011-07-30 20:52:31 -04001386 if (end_pos > nfsi->layout->plh_lwb)
1387 nfsi->layout->plh_lwb = end_pos;
Andy Adamson863a3c62011-03-23 13:27:54 +00001388 spin_unlock(&nfsi->vfs_inode.i_lock);
Peng Taoacff58802011-07-30 20:52:31 -04001389 dprintk("%s: lseg %p end_pos %llu\n",
1390 __func__, wdata->lseg, nfsi->layout->plh_lwb);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001391
1392 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
1393 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
1394 if (mark_as_dirty)
1395 mark_inode_dirty_sync(wdata->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00001396}
1397EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
1398
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001399/*
1400 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
1401 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
1402 * data to disk to allow the server to recover the data if it crashes.
1403 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
1404 * is off, and a COMMIT is sent to a data server, or
1405 * if WRITEs to a data server return NFS_DATA_SYNC.
1406 */
Andy Adamson863a3c62011-03-23 13:27:54 +00001407int
Andy Adamsonef311532011-03-12 02:58:10 -05001408pnfs_layoutcommit_inode(struct inode *inode, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00001409{
1410 struct nfs4_layoutcommit_data *data;
1411 struct nfs_inode *nfsi = NFS_I(inode);
1412 struct pnfs_layout_segment *lseg;
Andy Adamson863a3c62011-03-23 13:27:54 +00001413 loff_t end_pos;
1414 int status = 0;
1415
1416 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
1417
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001418 if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
1419 return 0;
1420
Andy Adamson863a3c62011-03-23 13:27:54 +00001421 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
1422 data = kzalloc(sizeof(*data), GFP_NOFS);
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001423 if (!data) {
1424 mark_inode_dirty_sync(inode);
1425 status = -ENOMEM;
1426 goto out;
1427 }
Andy Adamson863a3c62011-03-23 13:27:54 +00001428
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001429 spin_lock(&inode->i_lock);
Andy Adamson863a3c62011-03-23 13:27:54 +00001430 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1431 spin_unlock(&inode->i_lock);
1432 kfree(data);
1433 goto out;
1434 }
1435 /*
1436 * Currently only one (whole file) write lseg which is referenced
1437 * in pnfs_set_layoutcommit and will be found.
1438 */
1439 lseg = pnfs_list_write_lseg(inode);
1440
Peng Taoacff58802011-07-30 20:52:31 -04001441 end_pos = nfsi->layout->plh_lwb;
Peng Taoacff58802011-07-30 20:52:31 -04001442 nfsi->layout->plh_lwb = 0;
Andy Adamson863a3c62011-03-23 13:27:54 +00001443
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001444 memcpy(&data->args.stateid.data, nfsi->layout->plh_stateid.data,
1445 sizeof(nfsi->layout->plh_stateid.data));
Andy Adamson863a3c62011-03-23 13:27:54 +00001446 spin_unlock(&inode->i_lock);
1447
1448 data->args.inode = inode;
1449 data->lseg = lseg;
Peng Tao9fa40752011-07-30 20:52:32 -04001450 data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
Andy Adamson863a3c62011-03-23 13:27:54 +00001451 nfs_fattr_init(&data->fattr);
1452 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
1453 data->res.fattr = &data->fattr;
1454 data->args.lastbytewritten = end_pos - 1;
1455 data->res.server = NFS_SERVER(inode);
1456
1457 status = nfs4_proc_layoutcommit(data, sync);
1458out:
1459 dprintk("<-- %s status %d\n", __func__, status);
1460 return status;
1461}