blob: a7e5f17f77763d9db7dd8373df65cea70ba7ba7c [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);
Peng Taoa9bae562011-07-30 20:52:33 -0400228 INIT_LIST_HEAD(&lseg->pls_lc_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000229 atomic_set(&lseg->pls_refcount, 1);
230 smp_mb();
231 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
Fred Isaman566052c2011-01-06 11:36:20 +0000232 lseg->pls_layout = lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400233}
234
Fred Isaman4541d162011-01-06 11:36:23 +0000235static void free_lseg(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400236{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000237 struct inode *ino = lseg->pls_layout->plh_inode;
Andy Adamson974cec82010-10-20 00:18:02 -0400238
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400239 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
Fred Isaman52fabd72011-01-06 11:36:18 +0000240 /* Matched by get_layout_hdr in pnfs_insert_layout */
Fred Isamancc6e5342011-01-06 11:36:28 +0000241 put_layout_hdr(NFS_I(ino)->layout);
Andy Adamson974cec82010-10-20 00:18:02 -0400242}
243
Fred Isamand684d2a2011-03-01 01:34:13 +0000244static void
245put_lseg_common(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400246{
Fred Isamand684d2a2011-03-01 01:34:13 +0000247 struct inode *inode = lseg->pls_layout->plh_inode;
248
Benny Halevyd20581a2011-05-22 19:52:03 +0300249 WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Fred Isamand684d2a2011-03-01 01:34:13 +0000250 list_del_init(&lseg->pls_list);
251 if (list_empty(&lseg->pls_layout->plh_segs)) {
252 set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags);
253 /* Matched by initial refcount set in alloc_init_layout_hdr */
254 put_layout_hdr_locked(lseg->pls_layout);
255 }
256 rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
257}
258
Fred Isamanbae724e2011-03-01 01:34:15 +0000259void
Fred Isamand684d2a2011-03-01 01:34:13 +0000260put_lseg(struct pnfs_layout_segment *lseg)
261{
262 struct inode *inode;
263
264 if (!lseg)
265 return;
266
Fred Isaman4541d162011-01-06 11:36:23 +0000267 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
268 atomic_read(&lseg->pls_refcount),
269 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Fred Isamand684d2a2011-03-01 01:34:13 +0000270 inode = lseg->pls_layout->plh_inode;
271 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
272 LIST_HEAD(free_me);
Andy Adamson974cec82010-10-20 00:18:02 -0400273
Fred Isamand684d2a2011-03-01 01:34:13 +0000274 put_lseg_common(lseg);
275 list_add(&lseg->pls_list, &free_me);
276 spin_unlock(&inode->i_lock);
277 pnfs_free_lseg_list(&free_me);
Fred Isaman4541d162011-01-06 11:36:23 +0000278 }
Andy Adamson974cec82010-10-20 00:18:02 -0400279}
Fred Isamane0c2b382011-03-23 13:27:53 +0000280EXPORT_SYMBOL_GPL(put_lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400281
Benny Halevyfb3296e2011-05-22 19:47:26 +0300282static inline u64
283end_offset(u64 start, u64 len)
Fred Isaman4541d162011-01-06 11:36:23 +0000284{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300285 u64 end;
286
287 end = start + len;
288 return end >= start ? end : NFS4_MAX_UINT64;
289}
290
291/* last octet in a range */
292static inline u64
293last_byte_offset(u64 start, u64 len)
294{
295 u64 end;
296
297 BUG_ON(!len);
298 end = start + len;
299 return end > start ? end - 1 : NFS4_MAX_UINT64;
300}
301
302/*
303 * is l2 fully contained in l1?
304 * start1 end1
305 * [----------------------------------)
306 * start2 end2
307 * [----------------)
308 */
309static inline int
310lo_seg_contained(struct pnfs_layout_range *l1,
311 struct pnfs_layout_range *l2)
312{
313 u64 start1 = l1->offset;
314 u64 end1 = end_offset(start1, l1->length);
315 u64 start2 = l2->offset;
316 u64 end2 = end_offset(start2, l2->length);
317
318 return (start1 <= start2) && (end1 >= end2);
319}
320
321/*
322 * is l1 and l2 intersecting?
323 * start1 end1
324 * [----------------------------------)
325 * start2 end2
326 * [----------------)
327 */
328static inline int
329lo_seg_intersecting(struct pnfs_layout_range *l1,
330 struct pnfs_layout_range *l2)
331{
332 u64 start1 = l1->offset;
333 u64 end1 = end_offset(start1, l1->length);
334 u64 start2 = l2->offset;
335 u64 end2 = end_offset(start2, l2->length);
336
337 return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
338 (end2 == NFS4_MAX_UINT64 || end2 > start1);
339}
340
Fred Isaman4541d162011-01-06 11:36:23 +0000341static bool
Benny Halevy778b5502011-05-22 19:48:02 +0300342should_free_lseg(struct pnfs_layout_range *lseg_range,
343 struct pnfs_layout_range *recall_range)
Fred Isaman4541d162011-01-06 11:36:23 +0000344{
Benny Halevy778b5502011-05-22 19:48:02 +0300345 return (recall_range->iomode == IOMODE_ANY ||
346 lseg_range->iomode == recall_range->iomode) &&
347 lo_seg_intersecting(lseg_range, recall_range);
Fred Isaman4541d162011-01-06 11:36:23 +0000348}
349
350/* Returns 1 if lseg is removed from list, 0 otherwise */
351static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
352 struct list_head *tmp_list)
353{
354 int rv = 0;
355
356 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
357 /* Remove the reference keeping the lseg in the
358 * list. It will now be removed when all
359 * outstanding io is finished.
360 */
Fred Isamand684d2a2011-03-01 01:34:13 +0000361 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
362 atomic_read(&lseg->pls_refcount));
363 if (atomic_dec_and_test(&lseg->pls_refcount)) {
364 put_lseg_common(lseg);
365 list_add(&lseg->pls_list, tmp_list);
366 rv = 1;
367 }
Fred Isaman4541d162011-01-06 11:36:23 +0000368 }
369 return rv;
370}
371
372/* Returns count of number of matching invalid lsegs remaining in list
373 * after call.
374 */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000375int
Fred Isaman4541d162011-01-06 11:36:23 +0000376mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
377 struct list_head *tmp_list,
Benny Halevy778b5502011-05-22 19:48:02 +0300378 struct pnfs_layout_range *recall_range)
Andy Adamson974cec82010-10-20 00:18:02 -0400379{
380 struct pnfs_layout_segment *lseg, *next;
Fred Isaman4541d162011-01-06 11:36:23 +0000381 int invalid = 0, removed = 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400382
383 dprintk("%s:Begin lo %p\n", __func__, lo);
384
Fred Isaman38511722011-02-03 18:28:50 +0000385 if (list_empty(&lo->plh_segs)) {
386 if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags))
387 put_layout_hdr_locked(lo);
388 return 0;
389 }
Fred Isaman4541d162011-01-06 11:36:23 +0000390 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
Benny Halevy778b5502011-05-22 19:48:02 +0300391 if (!recall_range ||
392 should_free_lseg(&lseg->pls_range, recall_range)) {
Fred Isaman4541d162011-01-06 11:36:23 +0000393 dprintk("%s: freeing lseg %p iomode %d "
394 "offset %llu length %llu\n", __func__,
395 lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
396 lseg->pls_range.length);
397 invalid++;
398 removed += mark_lseg_invalid(lseg, tmp_list);
399 }
400 dprintk("%s:Return %i\n", __func__, invalid - removed);
401 return invalid - removed;
Andy Adamson974cec82010-10-20 00:18:02 -0400402}
403
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000404/* note free_me must contain lsegs from a single layout_hdr */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000405void
Fred Isaman4541d162011-01-06 11:36:23 +0000406pnfs_free_lseg_list(struct list_head *free_me)
Andy Adamson974cec82010-10-20 00:18:02 -0400407{
Fred Isaman4541d162011-01-06 11:36:23 +0000408 struct pnfs_layout_segment *lseg, *tmp;
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000409 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400410
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000411 if (list_empty(free_me))
412 return;
413
414 lo = list_first_entry(free_me, struct pnfs_layout_segment,
415 pls_list)->pls_layout;
416
417 if (test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) {
418 struct nfs_client *clp;
419
420 clp = NFS_SERVER(lo->plh_inode)->nfs_client;
421 spin_lock(&clp->cl_lock);
422 list_del_init(&lo->plh_layouts);
423 spin_unlock(&clp->cl_lock);
424 }
Fred Isaman4541d162011-01-06 11:36:23 +0000425 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
Fred Isaman566052c2011-01-06 11:36:20 +0000426 list_del(&lseg->pls_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000427 free_lseg(lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400428 }
429}
430
Benny Halevye5e94012010-10-20 00:18:01 -0400431void
432pnfs_destroy_layout(struct nfs_inode *nfsi)
433{
434 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400435 LIST_HEAD(tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400436
437 spin_lock(&nfsi->vfs_inode.i_lock);
438 lo = nfsi->layout;
439 if (lo) {
Fred Isaman38511722011-02-03 18:28:50 +0000440 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
Benny Halevy778b5502011-05-22 19:48:02 +0300441 mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
Benny Halevye5e94012010-10-20 00:18:01 -0400442 }
443 spin_unlock(&nfsi->vfs_inode.i_lock);
Andy Adamson974cec82010-10-20 00:18:02 -0400444 pnfs_free_lseg_list(&tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400445}
446
Andy Adamson974cec82010-10-20 00:18:02 -0400447/*
448 * Called by the state manger to remove all layouts established under an
449 * expired lease.
450 */
451void
452pnfs_destroy_all_layouts(struct nfs_client *clp)
453{
Weston Andros Adamson6382a442011-06-01 16:44:44 -0400454 struct nfs_server *server;
Andy Adamson974cec82010-10-20 00:18:02 -0400455 struct pnfs_layout_hdr *lo;
456 LIST_HEAD(tmp_list);
457
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400458 nfs4_deviceid_mark_client_invalid(clp);
459 nfs4_deviceid_purge_client(clp);
460
Andy Adamson974cec82010-10-20 00:18:02 -0400461 spin_lock(&clp->cl_lock);
Weston Andros Adamson6382a442011-06-01 16:44:44 -0400462 rcu_read_lock();
463 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
464 if (!list_empty(&server->layouts))
465 list_splice_init(&server->layouts, &tmp_list);
466 }
467 rcu_read_unlock();
Andy Adamson974cec82010-10-20 00:18:02 -0400468 spin_unlock(&clp->cl_lock);
469
470 while (!list_empty(&tmp_list)) {
471 lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000472 plh_layouts);
Andy Adamson974cec82010-10-20 00:18:02 -0400473 dprintk("%s freeing layout for inode %lu\n", __func__,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000474 lo->plh_inode->i_ino);
Andy Adamson2887fe42011-05-11 01:19:58 -0400475 list_del_init(&lo->plh_layouts);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000476 pnfs_destroy_layout(NFS_I(lo->plh_inode));
Andy Adamson974cec82010-10-20 00:18:02 -0400477 }
478}
479
Fred Isamanfd6002e2011-01-06 11:36:22 +0000480/* update lo->plh_stateid with new if is more recent */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000481void
482pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
483 bool update_barrier)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400484{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000485 u32 oldseq, newseq;
Andy Adamson974cec82010-10-20 00:18:02 -0400486
Fred Isamanfd6002e2011-01-06 11:36:22 +0000487 oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid);
488 newseq = be32_to_cpu(new->stateid.seqid);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000489 if ((int)(newseq - oldseq) > 0) {
Fred Isamanfd6002e2011-01-06 11:36:22 +0000490 memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid));
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000491 if (update_barrier) {
492 u32 new_barrier = be32_to_cpu(new->stateid.seqid);
493
494 if ((int)(new_barrier - lo->plh_barrier))
495 lo->plh_barrier = new_barrier;
496 } else {
497 /* Because of wraparound, we want to keep the barrier
498 * "close" to the current seqids. It needs to be
499 * within 2**31 to count as "behind", so if it
500 * gets too near that limit, give us a litle leeway
501 * and bring it to within 2**30.
502 * NOTE - and yes, this is all unsigned arithmetic.
503 */
504 if (unlikely((newseq - lo->plh_barrier) > (3 << 29)))
505 lo->plh_barrier = newseq - (1 << 30);
506 }
507 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400508}
509
Fred Isamancf7d63f2011-01-06 11:36:25 +0000510/* lget is set to 1 if called from inside send_layoutget call chain */
511static bool
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000512pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid,
513 int lget)
Fred Isamancf7d63f2011-01-06 11:36:25 +0000514{
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000515 if ((stateid) &&
516 (int)(lo->plh_barrier - be32_to_cpu(stateid->stateid.seqid)) >= 0)
517 return true;
Fred Isamanf7e89172011-01-06 11:36:32 +0000518 return lo->plh_block_lgets ||
Fred Isaman38511722011-02-03 18:28:50 +0000519 test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) ||
Fred Isamanf7e89172011-01-06 11:36:32 +0000520 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000521 (list_empty(&lo->plh_segs) &&
Fred Isamancf7d63f2011-01-06 11:36:25 +0000522 (atomic_read(&lo->plh_outstanding) > lget));
523}
524
Fred Isamanfd6002e2011-01-06 11:36:22 +0000525int
526pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
527 struct nfs4_state *open_state)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400528{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000529 int status = 0;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400530
531 dprintk("--> %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000532 spin_lock(&lo->plh_inode->i_lock);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000533 if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
Fred Isamancf7d63f2011-01-06 11:36:25 +0000534 status = -EAGAIN;
535 } else if (list_empty(&lo->plh_segs)) {
Fred Isamanfd6002e2011-01-06 11:36:22 +0000536 int seq;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400537
Fred Isamanfd6002e2011-01-06 11:36:22 +0000538 do {
539 seq = read_seqbegin(&open_state->seqlock);
540 memcpy(dst->data, open_state->stateid.data,
541 sizeof(open_state->stateid.data));
542 } while (read_seqretry(&open_state->seqlock, seq));
543 } else
544 memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data));
545 spin_unlock(&lo->plh_inode->i_lock);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400546 dprintk("<-- %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000547 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400548}
549
550/*
551* Get layout from server.
552* for now, assume that whole file layouts are requested.
553* arg->offset: 0
554* arg->length: all ones
555*/
Benny Halevye5e94012010-10-20 00:18:01 -0400556static struct pnfs_layout_segment *
557send_layoutget(struct pnfs_layout_hdr *lo,
558 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +0300559 struct pnfs_layout_range *range,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400560 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400561{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000562 struct inode *ino = lo->plh_inode;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400563 struct nfs_server *server = NFS_SERVER(ino);
564 struct nfs4_layoutget *lgp;
565 struct pnfs_layout_segment *lseg = NULL;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400566 struct page **pages = NULL;
567 int i;
568 u32 max_resp_sz, max_pages;
Benny Halevye5e94012010-10-20 00:18:01 -0400569
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400570 dprintk("--> %s\n", __func__);
571
572 BUG_ON(ctx == NULL);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400573 lgp = kzalloc(sizeof(*lgp), gfp_flags);
Fred Isamancf7d63f2011-01-06 11:36:25 +0000574 if (lgp == NULL)
Andy Adamson974cec82010-10-20 00:18:02 -0400575 return NULL;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400576
577 /* allocate pages for xdr post processing */
578 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
579 max_pages = max_resp_sz >> PAGE_SHIFT;
580
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400581 pages = kzalloc(max_pages * sizeof(struct page *), gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400582 if (!pages)
583 goto out_err_free;
584
585 for (i = 0; i < max_pages; i++) {
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400586 pages[i] = alloc_page(gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400587 if (!pages[i])
588 goto out_err_free;
589 }
590
Benny Halevyfb3296e2011-05-22 19:47:26 +0300591 lgp->args.minlength = PAGE_CACHE_SIZE;
592 if (lgp->args.minlength > range->length)
593 lgp->args.minlength = range->length;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400594 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
Benny Halevyfb3296e2011-05-22 19:47:26 +0300595 lgp->args.range = *range;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400596 lgp->args.type = server->pnfs_curr_ld->id;
597 lgp->args.inode = ino;
598 lgp->args.ctx = get_nfs_open_context(ctx);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400599 lgp->args.layout.pages = pages;
600 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400601 lgp->lsegpp = &lseg;
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400602 lgp->gfp_flags = gfp_flags;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400603
604 /* Synchronously retrieve layout information from server and
605 * store in lseg.
606 */
607 nfs4_proc_layoutget(lgp);
608 if (!lseg) {
609 /* remember that LAYOUTGET failed and suspend trying */
Benny Halevyfb3296e2011-05-22 19:47:26 +0300610 set_bit(lo_fail_bit(range->iomode), &lo->plh_flags);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400611 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400612
613 /* free xdr pages */
614 for (i = 0; i < max_pages; i++)
615 __free_page(pages[i]);
616 kfree(pages);
617
Andy Adamson974cec82010-10-20 00:18:02 -0400618 return lseg;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400619
620out_err_free:
621 /* free any allocated xdr pages, lgp as it's not used */
622 if (pages) {
623 for (i = 0; i < max_pages; i++) {
624 if (!pages[i])
625 break;
626 __free_page(pages[i]);
627 }
628 kfree(pages);
629 }
630 kfree(lgp);
631 return NULL;
Andy Adamson974cec82010-10-20 00:18:02 -0400632}
633
Benny Halevycbe82602011-05-22 19:52:37 +0300634/* Initiates a LAYOUTRETURN(FILE) */
635int
636_pnfs_return_layout(struct inode *ino)
637{
638 struct pnfs_layout_hdr *lo = NULL;
639 struct nfs_inode *nfsi = NFS_I(ino);
640 LIST_HEAD(tmp_list);
641 struct nfs4_layoutreturn *lrp;
642 nfs4_stateid stateid;
643 int status = 0;
644
645 dprintk("--> %s\n", __func__);
646
647 spin_lock(&ino->i_lock);
648 lo = nfsi->layout;
Fred Isamana2e1d4f2011-06-13 18:54:53 -0400649 if (!lo) {
Benny Halevycbe82602011-05-22 19:52:37 +0300650 spin_unlock(&ino->i_lock);
Fred Isamana2e1d4f2011-06-13 18:54:53 -0400651 dprintk("%s: no layout to return\n", __func__);
652 return status;
Benny Halevycbe82602011-05-22 19:52:37 +0300653 }
654 stateid = nfsi->layout->plh_stateid;
655 /* Reference matched in nfs4_layoutreturn_release */
656 get_layout_hdr(lo);
Fred Isamanea0ded72011-06-15 12:31:02 -0400657 mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
658 lo->plh_block_lgets++;
Benny Halevycbe82602011-05-22 19:52:37 +0300659 spin_unlock(&ino->i_lock);
660 pnfs_free_lseg_list(&tmp_list);
661
662 WARN_ON(test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags));
663
664 lrp = kzalloc(sizeof(*lrp), GFP_KERNEL);
665 if (unlikely(lrp == NULL)) {
666 status = -ENOMEM;
Fred Isaman9e2dfdb2011-06-15 14:32:02 -0400667 set_bit(NFS_LAYOUT_RW_FAILED, &lo->plh_flags);
668 set_bit(NFS_LAYOUT_RO_FAILED, &lo->plh_flags);
Benny Halevy1ed3a852011-06-15 11:39:57 -0400669 put_layout_hdr(lo);
Benny Halevycbe82602011-05-22 19:52:37 +0300670 goto out;
671 }
672
673 lrp->args.stateid = stateid;
674 lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
675 lrp->args.inode = ino;
Trond Myklebusta56aaa02011-06-15 11:59:10 -0400676 lrp->args.layout = lo;
Benny Halevycbe82602011-05-22 19:52:37 +0300677 lrp->clp = NFS_SERVER(ino)->nfs_client;
678
679 status = nfs4_proc_layoutreturn(lrp);
680out:
681 dprintk("<-- %s status: %d\n", __func__, status);
682 return status;
683}
684
Fred Isamanf7e89172011-01-06 11:36:32 +0000685bool pnfs_roc(struct inode *ino)
686{
687 struct pnfs_layout_hdr *lo;
688 struct pnfs_layout_segment *lseg, *tmp;
689 LIST_HEAD(tmp_list);
690 bool found = false;
691
692 spin_lock(&ino->i_lock);
693 lo = NFS_I(ino)->layout;
694 if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
695 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
696 goto out_nolayout;
697 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
698 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
699 mark_lseg_invalid(lseg, &tmp_list);
700 found = true;
701 }
702 if (!found)
703 goto out_nolayout;
704 lo->plh_block_lgets++;
705 get_layout_hdr(lo); /* matched in pnfs_roc_release */
706 spin_unlock(&ino->i_lock);
707 pnfs_free_lseg_list(&tmp_list);
708 return true;
709
710out_nolayout:
711 spin_unlock(&ino->i_lock);
712 return false;
713}
714
715void pnfs_roc_release(struct inode *ino)
716{
717 struct pnfs_layout_hdr *lo;
718
719 spin_lock(&ino->i_lock);
720 lo = NFS_I(ino)->layout;
721 lo->plh_block_lgets--;
722 put_layout_hdr_locked(lo);
723 spin_unlock(&ino->i_lock);
724}
725
726void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
727{
728 struct pnfs_layout_hdr *lo;
729
730 spin_lock(&ino->i_lock);
731 lo = NFS_I(ino)->layout;
732 if ((int)(barrier - lo->plh_barrier) > 0)
733 lo->plh_barrier = barrier;
734 spin_unlock(&ino->i_lock);
735}
736
737bool pnfs_roc_drain(struct inode *ino, u32 *barrier)
738{
739 struct nfs_inode *nfsi = NFS_I(ino);
740 struct pnfs_layout_segment *lseg;
741 bool found = false;
742
743 spin_lock(&ino->i_lock);
744 list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
745 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
746 found = true;
747 break;
748 }
749 if (!found) {
750 struct pnfs_layout_hdr *lo = nfsi->layout;
751 u32 current_seqid = be32_to_cpu(lo->plh_stateid.stateid.seqid);
752
753 /* Since close does not return a layout stateid for use as
754 * a barrier, we choose the worst-case barrier.
755 */
756 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
757 }
758 spin_unlock(&ino->i_lock);
759 return found;
760}
761
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400762/*
763 * Compare two layout segments for sorting into layout cache.
764 * We want to preferentially return RW over RO layouts, so ensure those
765 * are seen first.
766 */
767static s64
Benny Halevyfb3296e2011-05-22 19:47:26 +0300768cmp_layout(struct pnfs_layout_range *l1,
769 struct pnfs_layout_range *l2)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400770{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300771 s64 d;
772
773 /* high offset > low offset */
774 d = l1->offset - l2->offset;
775 if (d)
776 return d;
777
778 /* short length > long length */
779 d = l2->length - l1->length;
780 if (d)
781 return d;
782
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400783 /* read > read/write */
Benny Halevyfb3296e2011-05-22 19:47:26 +0300784 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400785}
786
Andy Adamson974cec82010-10-20 00:18:02 -0400787static void
788pnfs_insert_layout(struct pnfs_layout_hdr *lo,
789 struct pnfs_layout_segment *lseg)
790{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400791 struct pnfs_layout_segment *lp;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400792
Andy Adamson974cec82010-10-20 00:18:02 -0400793 dprintk("%s:Begin\n", __func__);
794
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000795 assert_spin_locked(&lo->plh_inode->i_lock);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000796 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
Benny Halevyfb3296e2011-05-22 19:47:26 +0300797 if (cmp_layout(&lseg->pls_range, &lp->pls_range) > 0)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400798 continue;
Fred Isaman566052c2011-01-06 11:36:20 +0000799 list_add_tail(&lseg->pls_list, &lp->pls_list);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400800 dprintk("%s: inserted lseg %p "
801 "iomode %d offset %llu length %llu before "
802 "lp %p iomode %d offset %llu length %llu\n",
Fred Isaman566052c2011-01-06 11:36:20 +0000803 __func__, lseg, lseg->pls_range.iomode,
804 lseg->pls_range.offset, lseg->pls_range.length,
805 lp, lp->pls_range.iomode, lp->pls_range.offset,
806 lp->pls_range.length);
Benny Halevyfb3296e2011-05-22 19:47:26 +0300807 goto out;
Andy Adamson974cec82010-10-20 00:18:02 -0400808 }
Benny Halevyfb3296e2011-05-22 19:47:26 +0300809 list_add_tail(&lseg->pls_list, &lo->plh_segs);
810 dprintk("%s: inserted lseg %p "
811 "iomode %d offset %llu length %llu at tail\n",
812 __func__, lseg, lseg->pls_range.iomode,
813 lseg->pls_range.offset, lseg->pls_range.length);
814out:
Fred Isamancc6e5342011-01-06 11:36:28 +0000815 get_layout_hdr(lo);
Andy Adamson974cec82010-10-20 00:18:02 -0400816
817 dprintk("%s:Return\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -0400818}
819
820static struct pnfs_layout_hdr *
Peng Tao9fa40752011-07-30 20:52:32 -0400821alloc_init_layout_hdr(struct inode *ino,
822 struct nfs_open_context *ctx,
823 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400824{
825 struct pnfs_layout_hdr *lo;
826
Benny Halevy636fb9c2011-05-22 19:51:33 +0300827 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400828 if (!lo)
829 return NULL;
Fred Isamancc6e5342011-01-06 11:36:28 +0000830 atomic_set(&lo->plh_refcount, 1);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000831 INIT_LIST_HEAD(&lo->plh_layouts);
832 INIT_LIST_HEAD(&lo->plh_segs);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000833 INIT_LIST_HEAD(&lo->plh_bulk_recall);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000834 lo->plh_inode = ino;
Peng Tao9fa40752011-07-30 20:52:32 -0400835 lo->plh_lc_cred = get_rpccred(ctx->state->owner->so_cred);
Benny Halevye5e94012010-10-20 00:18:01 -0400836 return lo;
837}
838
839static struct pnfs_layout_hdr *
Peng Tao9fa40752011-07-30 20:52:32 -0400840pnfs_find_alloc_layout(struct inode *ino,
841 struct nfs_open_context *ctx,
842 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400843{
844 struct nfs_inode *nfsi = NFS_I(ino);
845 struct pnfs_layout_hdr *new = NULL;
846
847 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
848
849 assert_spin_locked(&ino->i_lock);
Fred Isaman4541d162011-01-06 11:36:23 +0000850 if (nfsi->layout) {
851 if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
852 return NULL;
853 else
854 return nfsi->layout;
855 }
Benny Halevye5e94012010-10-20 00:18:01 -0400856 spin_unlock(&ino->i_lock);
Peng Tao9fa40752011-07-30 20:52:32 -0400857 new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400858 spin_lock(&ino->i_lock);
859
860 if (likely(nfsi->layout == NULL)) /* Won the race? */
861 nfsi->layout = new;
862 else
Benny Halevy636fb9c2011-05-22 19:51:33 +0300863 pnfs_free_layout_hdr(new);
Benny Halevye5e94012010-10-20 00:18:01 -0400864 return nfsi->layout;
865}
866
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400867/*
868 * iomode matching rules:
869 * iomode lseg match
870 * ----- ----- -----
871 * ANY READ true
872 * ANY RW true
873 * RW READ false
874 * RW RW true
875 * READ READ true
876 * READ RW true
877 */
878static int
Benny Halevyfb3296e2011-05-22 19:47:26 +0300879is_matching_lseg(struct pnfs_layout_range *ls_range,
880 struct pnfs_layout_range *range)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400881{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300882 struct pnfs_layout_range range1;
883
884 if ((range->iomode == IOMODE_RW &&
885 ls_range->iomode != IOMODE_RW) ||
886 !lo_seg_intersecting(ls_range, range))
887 return 0;
888
889 /* range1 covers only the first byte in the range */
890 range1 = *range;
891 range1.length = 1;
892 return lo_seg_contained(ls_range, &range1);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400893}
894
895/*
896 * lookup range in layout
897 */
Benny Halevye5e94012010-10-20 00:18:01 -0400898static struct pnfs_layout_segment *
Benny Halevyfb3296e2011-05-22 19:47:26 +0300899pnfs_find_lseg(struct pnfs_layout_hdr *lo,
900 struct pnfs_layout_range *range)
Benny Halevye5e94012010-10-20 00:18:01 -0400901{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400902 struct pnfs_layout_segment *lseg, *ret = NULL;
903
904 dprintk("%s:Begin\n", __func__);
905
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000906 assert_spin_locked(&lo->plh_inode->i_lock);
907 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
Fred Isaman4541d162011-01-06 11:36:23 +0000908 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
Benny Halevyfb3296e2011-05-22 19:47:26 +0300909 is_matching_lseg(&lseg->pls_range, range)) {
Fred Isamand684d2a2011-03-01 01:34:13 +0000910 ret = get_lseg(lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400911 break;
912 }
Benny Halevyd771e3a2011-06-14 16:30:16 -0400913 if (lseg->pls_range.offset > range->offset)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400914 break;
915 }
916
917 dprintk("%s:Return lseg %p ref %d\n",
Fred Isaman4541d162011-01-06 11:36:23 +0000918 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400919 return ret;
Benny Halevye5e94012010-10-20 00:18:01 -0400920}
921
922/*
923 * Layout segment is retreived from the server if not cached.
924 * The appropriate layout segment is referenced and returned to the caller.
925 */
Andy Adamson7c24d942011-06-13 18:22:38 -0400926struct pnfs_layout_segment *
Benny Halevye5e94012010-10-20 00:18:01 -0400927pnfs_update_layout(struct inode *ino,
928 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +0300929 loff_t pos,
930 u64 count,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400931 enum pnfs_iomode iomode,
932 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400933{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300934 struct pnfs_layout_range arg = {
935 .iomode = iomode,
936 .offset = pos,
937 .length = count,
938 };
Benny Halevy707ed5f2011-05-22 19:47:46 +0300939 unsigned pg_offset;
Benny Halevye5e94012010-10-20 00:18:01 -0400940 struct nfs_inode *nfsi = NFS_I(ino);
Weston Andros Adamson6382a442011-06-01 16:44:44 -0400941 struct nfs_server *server = NFS_SERVER(ino);
942 struct nfs_client *clp = server->nfs_client;
Benny Halevye5e94012010-10-20 00:18:01 -0400943 struct pnfs_layout_hdr *lo;
944 struct pnfs_layout_segment *lseg = NULL;
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000945 bool first = false;
Benny Halevye5e94012010-10-20 00:18:01 -0400946
947 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
948 return NULL;
949 spin_lock(&ino->i_lock);
Peng Tao9fa40752011-07-30 20:52:32 -0400950 lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400951 if (lo == NULL) {
952 dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
953 goto out_unlock;
954 }
955
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000956 /* Do we even need to bother with this? */
957 if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
958 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
959 dprintk("%s matches recall, use MDS\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -0400960 goto out_unlock;
961 }
962
963 /* if LAYOUTGET already failed once we don't try again */
Fred Isaman566052c2011-01-06 11:36:20 +0000964 if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
Benny Halevye5e94012010-10-20 00:18:01 -0400965 goto out_unlock;
966
Andy Adamson568e8c42011-03-01 01:34:22 +0000967 /* Check to see if the layout for the given range already exists */
Benny Halevyfb3296e2011-05-22 19:47:26 +0300968 lseg = pnfs_find_lseg(lo, &arg);
Andy Adamson568e8c42011-03-01 01:34:22 +0000969 if (lseg)
970 goto out_unlock;
971
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000972 if (pnfs_layoutgets_blocked(lo, NULL, 0))
Fred Isamancf7d63f2011-01-06 11:36:25 +0000973 goto out_unlock;
974 atomic_inc(&lo->plh_outstanding);
975
Fred Isamancc6e5342011-01-06 11:36:28 +0000976 get_layout_hdr(lo);
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000977 if (list_empty(&lo->plh_segs))
978 first = true;
979 spin_unlock(&ino->i_lock);
980 if (first) {
Fred Isaman2130ff62011-01-06 11:36:26 +0000981 /* The lo must be on the clp list if there is any
982 * chance of a CB_LAYOUTRECALL(FILE) coming in.
983 */
984 spin_lock(&clp->cl_lock);
985 BUG_ON(!list_empty(&lo->plh_layouts));
Weston Andros Adamson6382a442011-06-01 16:44:44 -0400986 list_add_tail(&lo->plh_layouts, &server->layouts);
Fred Isaman2130ff62011-01-06 11:36:26 +0000987 spin_unlock(&clp->cl_lock);
988 }
Benny Halevye5e94012010-10-20 00:18:01 -0400989
Benny Halevy707ed5f2011-05-22 19:47:46 +0300990 pg_offset = arg.offset & ~PAGE_CACHE_MASK;
991 if (pg_offset) {
992 arg.offset -= pg_offset;
993 arg.length += pg_offset;
994 }
Andy Adamson7c24d942011-06-13 18:22:38 -0400995 if (arg.length != NFS4_MAX_UINT64)
996 arg.length = PAGE_CACHE_ALIGN(arg.length);
Benny Halevy707ed5f2011-05-22 19:47:46 +0300997
Benny Halevyfb3296e2011-05-22 19:47:26 +0300998 lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000999 if (!lseg && first) {
1000 spin_lock(&clp->cl_lock);
1001 list_del_init(&lo->plh_layouts);
1002 spin_unlock(&clp->cl_lock);
Fred Isaman2130ff62011-01-06 11:36:26 +00001003 }
Fred Isamancf7d63f2011-01-06 11:36:25 +00001004 atomic_dec(&lo->plh_outstanding);
Fred Isamancc6e5342011-01-06 11:36:28 +00001005 put_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -04001006out:
1007 dprintk("%s end, state 0x%lx lseg %p\n", __func__,
Andy Adamsonbf9c1382011-03-01 01:34:07 +00001008 nfsi->layout ? nfsi->layout->plh_flags : -1, lseg);
Benny Halevye5e94012010-10-20 00:18:01 -04001009 return lseg;
1010out_unlock:
1011 spin_unlock(&ino->i_lock);
1012 goto out;
1013}
Andy Adamson7c24d942011-06-13 18:22:38 -04001014EXPORT_SYMBOL_GPL(pnfs_update_layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001015
1016int
1017pnfs_layout_process(struct nfs4_layoutget *lgp)
1018{
1019 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
1020 struct nfs4_layoutget_res *res = &lgp->res;
1021 struct pnfs_layout_segment *lseg;
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001022 struct inode *ino = lo->plh_inode;
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001023 struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001024 int status = 0;
1025
1026 /* Inject layout blob into I/O device driver */
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001027 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001028 if (!lseg || IS_ERR(lseg)) {
1029 if (!lseg)
1030 status = -ENOMEM;
1031 else
1032 status = PTR_ERR(lseg);
1033 dprintk("%s: Could not allocate layout: error %d\n",
1034 __func__, status);
1035 goto out;
1036 }
1037
1038 spin_lock(&ino->i_lock);
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001039 if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
1040 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
1041 dprintk("%s forget reply due to recall\n", __func__);
1042 goto out_forget_reply;
1043 }
1044
1045 if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) {
1046 dprintk("%s forget reply due to state\n", __func__);
1047 goto out_forget_reply;
1048 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001049 init_lseg(lo, lseg);
Fred Isaman566052c2011-01-06 11:36:20 +00001050 lseg->pls_range = res->range;
Fred Isamand684d2a2011-03-01 01:34:13 +00001051 *lgp->lsegpp = get_lseg(lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001052 pnfs_insert_layout(lo, lseg);
1053
Fred Isamanf7e89172011-01-06 11:36:32 +00001054 if (res->return_on_close) {
1055 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
1056 set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
1057 }
1058
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001059 /* Done processing layoutget. Set the layout stateid */
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001060 pnfs_set_layout_stateid(lo, &res->stateid, false);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001061 spin_unlock(&ino->i_lock);
1062out:
1063 return status;
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001064
1065out_forget_reply:
1066 spin_unlock(&ino->i_lock);
1067 lseg->pls_layout = lo;
1068 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
1069 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001070}
1071
Trond Myklebustd8007d42011-06-10 13:30:23 -04001072void
1073pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1074{
1075 BUG_ON(pgio->pg_lseg != NULL);
1076
1077 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1078 req->wb_context,
1079 req_offset(req),
1080 req->wb_bytes,
1081 IOMODE_READ,
1082 GFP_KERNEL);
Trond Myklebuste885de12011-06-10 13:30:23 -04001083 /* If no lseg, fall back to read through mds */
1084 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -04001085 nfs_pageio_reset_read_mds(pgio);
Trond Myklebuste885de12011-06-10 13:30:23 -04001086
Trond Myklebustd8007d42011-06-10 13:30:23 -04001087}
1088EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
1089
1090void
1091pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1092{
1093 BUG_ON(pgio->pg_lseg != NULL);
1094
1095 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1096 req->wb_context,
1097 req_offset(req),
1098 req->wb_bytes,
1099 IOMODE_RW,
1100 GFP_NOFS);
Trond Myklebuste885de12011-06-10 13:30:23 -04001101 /* If no lseg, fall back to write through mds */
1102 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -04001103 nfs_pageio_reset_write_mds(pgio);
Trond Myklebustd8007d42011-06-10 13:30:23 -04001104}
1105EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
1106
Benny Halevy18ad0a92011-05-25 21:03:56 +03001107bool
Trond Myklebust1751c362011-06-10 13:30:23 -04001108pnfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, struct inode *inode)
1109{
1110 struct nfs_server *server = NFS_SERVER(inode);
1111 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
1112
1113 if (ld == NULL)
1114 return false;
1115 nfs_pageio_init(pgio, inode, ld->pg_read_ops, server->rsize, 0);
1116 return true;
1117}
1118
1119bool
1120pnfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, struct inode *inode, int ioflags)
1121{
1122 struct nfs_server *server = NFS_SERVER(inode);
1123 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
1124
1125 if (ld == NULL)
1126 return false;
1127 nfs_pageio_init(pgio, inode, ld->pg_write_ops, server->wsize, ioflags);
1128 return true;
1129}
1130
1131bool
Benny Halevydfed2062011-05-25 20:25:22 +03001132pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
1133 struct nfs_page *req)
Fred Isamanbae724e2011-03-01 01:34:15 +00001134{
Trond Myklebustd8007d42011-06-10 13:30:23 -04001135 if (pgio->pg_lseg == NULL)
1136 return nfs_generic_pg_test(pgio, prev, req);
Benny Halevy89a58e32011-05-25 20:54:40 +03001137
Trond Myklebust19982ba2011-06-10 13:30:23 -04001138 /*
1139 * Test if a nfs_page is fully contained in the pnfs_layout_range.
1140 * Note that this test makes several assumptions:
1141 * - that the previous nfs_page in the struct nfs_pageio_descriptor
1142 * is known to lie within the range.
1143 * - that the nfs_page being tested is known to be contiguous with the
1144 * previous nfs_page.
1145 * - Layout ranges are page aligned, so we only have to test the
1146 * start offset of the request.
1147 *
1148 * Please also note that 'end_offset' is actually the offset of the
1149 * first byte that lies outside the pnfs_layout_range. FIXME?
1150 *
1151 */
1152 return req_offset(req) < end_offset(pgio->pg_lseg->pls_range.offset,
1153 pgio->pg_lseg->pls_range.length);
Fred Isamanbae724e2011-03-01 01:34:15 +00001154}
Benny Halevy89a58e32011-05-25 20:54:40 +03001155EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
Fred Isamanbae724e2011-03-01 01:34:15 +00001156
Benny Halevyd20581a2011-05-22 19:52:03 +03001157/*
1158 * Called by non rpc-based layout drivers
1159 */
1160int
1161pnfs_ld_write_done(struct nfs_write_data *data)
Fred Isaman94ad1c82011-03-01 01:34:14 +00001162{
Benny Halevyd20581a2011-05-22 19:52:03 +03001163 int status;
Fred Isaman94ad1c82011-03-01 01:34:14 +00001164
Benny Halevyd20581a2011-05-22 19:52:03 +03001165 if (!data->pnfs_error) {
1166 pnfs_set_layoutcommit(data);
1167 data->mds_ops->rpc_call_done(&data->task, data);
1168 data->mds_ops->rpc_release(data);
1169 return 0;
Fred Isaman44b83792011-03-03 15:13:44 +00001170 }
Fred Isaman44b83792011-03-03 15:13:44 +00001171
Benny Halevyd20581a2011-05-22 19:52:03 +03001172 dprintk("%s: pnfs_error=%d, retry via MDS\n", __func__,
1173 data->pnfs_error);
1174 status = nfs_initiate_write(data, NFS_CLIENT(data->inode),
1175 data->mds_ops, NFS_FILE_SYNC);
1176 return status ? : -EAGAIN;
Fred Isaman44b83792011-03-03 15:13:44 +00001177}
Benny Halevyd20581a2011-05-22 19:52:03 +03001178EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
Fred Isaman44b83792011-03-03 15:13:44 +00001179
Trond Myklebustdce81292011-07-13 15:59:19 -04001180static void
1181pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
1182 struct nfs_write_data *data)
1183{
1184 list_splice_tail_init(&data->pages, &desc->pg_list);
1185 if (data->req && list_empty(&data->req->wb_list))
1186 nfs_list_add_request(data->req, &desc->pg_list);
1187 nfs_pageio_reset_write_mds(desc);
1188 desc->pg_recoalesce = 1;
1189 nfs_writedata_release(data);
1190}
1191
1192static enum pnfs_try_status
Andy Adamson0382b742011-03-03 15:13:45 +00001193pnfs_try_to_write_data(struct nfs_write_data *wdata,
Trond Myklebustdce81292011-07-13 15:59:19 -04001194 const struct rpc_call_ops *call_ops,
1195 struct pnfs_layout_segment *lseg,
1196 int how)
Andy Adamson0382b742011-03-03 15:13:45 +00001197{
1198 struct inode *inode = wdata->inode;
1199 enum pnfs_try_status trypnfs;
1200 struct nfs_server *nfss = NFS_SERVER(inode);
1201
1202 wdata->mds_ops = call_ops;
Trond Myklebustdce81292011-07-13 15:59:19 -04001203 wdata->lseg = get_lseg(lseg);
Andy Adamson0382b742011-03-03 15:13:45 +00001204
1205 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
1206 inode->i_ino, wdata->args.count, wdata->args.offset, how);
1207
1208 trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how);
1209 if (trypnfs == PNFS_NOT_ATTEMPTED) {
1210 put_lseg(wdata->lseg);
1211 wdata->lseg = NULL;
1212 } else
1213 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
1214
1215 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1216 return trypnfs;
1217}
1218
Trond Myklebustdce81292011-07-13 15:59:19 -04001219static void
1220pnfs_do_multiple_writes(struct nfs_pageio_descriptor *desc, struct list_head *head, int how)
1221{
1222 struct nfs_write_data *data;
1223 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1224 struct pnfs_layout_segment *lseg = desc->pg_lseg;
1225
1226 desc->pg_lseg = NULL;
1227 while (!list_empty(head)) {
1228 enum pnfs_try_status trypnfs;
1229
1230 data = list_entry(head->next, struct nfs_write_data, list);
1231 list_del_init(&data->list);
1232
1233 trypnfs = pnfs_try_to_write_data(data, call_ops, lseg, how);
1234 if (trypnfs == PNFS_NOT_ATTEMPTED)
1235 pnfs_write_through_mds(desc, data);
1236 }
1237 put_lseg(lseg);
1238}
1239
1240int
1241pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
1242{
1243 LIST_HEAD(head);
1244 int ret;
1245
1246 ret = nfs_generic_flush(desc, &head);
1247 if (ret != 0) {
1248 put_lseg(desc->pg_lseg);
1249 desc->pg_lseg = NULL;
1250 return ret;
1251 }
1252 pnfs_do_multiple_writes(desc, &head, desc->pg_ioflags);
1253 return 0;
1254}
1255EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
1256
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001257/*
Benny Halevyd20581a2011-05-22 19:52:03 +03001258 * Called by non rpc-based layout drivers
1259 */
1260int
1261pnfs_ld_read_done(struct nfs_read_data *data)
1262{
1263 int status;
1264
1265 if (!data->pnfs_error) {
1266 __nfs4_read_done_cb(data);
1267 data->mds_ops->rpc_call_done(&data->task, data);
1268 data->mds_ops->rpc_release(data);
1269 return 0;
1270 }
1271
1272 dprintk("%s: pnfs_error=%d, retry via MDS\n", __func__,
1273 data->pnfs_error);
1274 status = nfs_initiate_read(data, NFS_CLIENT(data->inode),
1275 data->mds_ops);
1276 return status ? : -EAGAIN;
1277}
1278EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
1279
Trond Myklebust493292d2011-07-13 15:58:28 -04001280static void
1281pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
1282 struct nfs_read_data *data)
1283{
1284 list_splice_tail_init(&data->pages, &desc->pg_list);
1285 if (data->req && list_empty(&data->req->wb_list))
1286 nfs_list_add_request(data->req, &desc->pg_list);
1287 nfs_pageio_reset_read_mds(desc);
1288 desc->pg_recoalesce = 1;
1289 nfs_readdata_release(data);
1290}
1291
Benny Halevyd20581a2011-05-22 19:52:03 +03001292/*
Andy Adamson64419a92011-03-01 01:34:16 +00001293 * Call the appropriate parallel I/O subsystem read function.
1294 */
Trond Myklebust493292d2011-07-13 15:58:28 -04001295static enum pnfs_try_status
Andy Adamson64419a92011-03-01 01:34:16 +00001296pnfs_try_to_read_data(struct nfs_read_data *rdata,
Trond Myklebust493292d2011-07-13 15:58:28 -04001297 const struct rpc_call_ops *call_ops,
1298 struct pnfs_layout_segment *lseg)
Andy Adamson64419a92011-03-01 01:34:16 +00001299{
1300 struct inode *inode = rdata->inode;
1301 struct nfs_server *nfss = NFS_SERVER(inode);
1302 enum pnfs_try_status trypnfs;
1303
1304 rdata->mds_ops = call_ops;
Trond Myklebust493292d2011-07-13 15:58:28 -04001305 rdata->lseg = get_lseg(lseg);
Andy Adamson64419a92011-03-01 01:34:16 +00001306
1307 dprintk("%s: Reading ino:%lu %u@%llu\n",
1308 __func__, inode->i_ino, rdata->args.count, rdata->args.offset);
1309
1310 trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata);
1311 if (trypnfs == PNFS_NOT_ATTEMPTED) {
1312 put_lseg(rdata->lseg);
1313 rdata->lseg = NULL;
1314 } else {
1315 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
1316 }
1317 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1318 return trypnfs;
1319}
Andy Adamson863a3c62011-03-23 13:27:54 +00001320
Trond Myklebust493292d2011-07-13 15:58:28 -04001321static void
1322pnfs_do_multiple_reads(struct nfs_pageio_descriptor *desc, struct list_head *head)
1323{
1324 struct nfs_read_data *data;
1325 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1326 struct pnfs_layout_segment *lseg = desc->pg_lseg;
1327
1328 desc->pg_lseg = NULL;
1329 while (!list_empty(head)) {
1330 enum pnfs_try_status trypnfs;
1331
1332 data = list_entry(head->next, struct nfs_read_data, list);
1333 list_del_init(&data->list);
1334
1335 trypnfs = pnfs_try_to_read_data(data, call_ops, lseg);
1336 if (trypnfs == PNFS_NOT_ATTEMPTED)
1337 pnfs_read_through_mds(desc, data);
1338 }
1339 put_lseg(lseg);
1340}
1341
1342int
1343pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
1344{
1345 LIST_HEAD(head);
1346 int ret;
1347
1348 ret = nfs_generic_pagein(desc, &head);
1349 if (ret != 0) {
1350 put_lseg(desc->pg_lseg);
1351 desc->pg_lseg = NULL;
1352 return ret;
1353 }
1354 pnfs_do_multiple_reads(desc, &head);
1355 return 0;
1356}
1357EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
1358
Andy Adamson863a3c62011-03-23 13:27:54 +00001359/*
Peng Taoa9bae562011-07-30 20:52:33 -04001360 * There can be multiple RW segments.
Andy Adamson863a3c62011-03-23 13:27:54 +00001361 */
Peng Taoa9bae562011-07-30 20:52:33 -04001362static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
Andy Adamson863a3c62011-03-23 13:27:54 +00001363{
Peng Taoa9bae562011-07-30 20:52:33 -04001364 struct pnfs_layout_segment *lseg;
Andy Adamson863a3c62011-03-23 13:27:54 +00001365
Peng Taoa9bae562011-07-30 20:52:33 -04001366 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
1367 if (lseg->pls_range.iomode == IOMODE_RW &&
1368 test_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
1369 list_add(&lseg->pls_lc_list, listp);
1370 }
Andy Adamson863a3c62011-03-23 13:27:54 +00001371}
1372
1373void
1374pnfs_set_layoutcommit(struct nfs_write_data *wdata)
1375{
1376 struct nfs_inode *nfsi = NFS_I(wdata->inode);
Vitaliy Gusev4b8ee2b2011-05-20 01:34:46 +04001377 loff_t end_pos = wdata->mds_offset + wdata->res.count;
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001378 bool mark_as_dirty = false;
Andy Adamson863a3c62011-03-23 13:27:54 +00001379
1380 spin_lock(&nfsi->vfs_inode.i_lock);
1381 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
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 Taoa9bae562011-07-30 20:52:33 -04001386 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &wdata->lseg->pls_flags)) {
1387 /* references matched in nfs4_layoutcommit_release */
1388 get_lseg(wdata->lseg);
1389 }
Peng Taoacff58802011-07-30 20:52:31 -04001390 if (end_pos > nfsi->layout->plh_lwb)
1391 nfsi->layout->plh_lwb = end_pos;
Andy Adamson863a3c62011-03-23 13:27:54 +00001392 spin_unlock(&nfsi->vfs_inode.i_lock);
Peng Taoacff58802011-07-30 20:52:31 -04001393 dprintk("%s: lseg %p end_pos %llu\n",
1394 __func__, wdata->lseg, nfsi->layout->plh_lwb);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001395
1396 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
1397 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
1398 if (mark_as_dirty)
1399 mark_inode_dirty_sync(wdata->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00001400}
1401EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
1402
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001403/*
1404 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
1405 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
1406 * data to disk to allow the server to recover the data if it crashes.
1407 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
1408 * is off, and a COMMIT is sent to a data server, or
1409 * if WRITEs to a data server return NFS_DATA_SYNC.
1410 */
Andy Adamson863a3c62011-03-23 13:27:54 +00001411int
Andy Adamsonef311532011-03-12 02:58:10 -05001412pnfs_layoutcommit_inode(struct inode *inode, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00001413{
1414 struct nfs4_layoutcommit_data *data;
1415 struct nfs_inode *nfsi = NFS_I(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00001416 loff_t end_pos;
1417 int status = 0;
1418
1419 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
1420
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001421 if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
1422 return 0;
1423
Andy Adamson863a3c62011-03-23 13:27:54 +00001424 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
1425 data = kzalloc(sizeof(*data), GFP_NOFS);
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001426 if (!data) {
1427 mark_inode_dirty_sync(inode);
1428 status = -ENOMEM;
1429 goto out;
1430 }
Andy Adamson863a3c62011-03-23 13:27:54 +00001431
Peng Taoa9bae562011-07-30 20:52:33 -04001432 INIT_LIST_HEAD(&data->lseg_list);
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001433 spin_lock(&inode->i_lock);
Andy Adamson863a3c62011-03-23 13:27:54 +00001434 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1435 spin_unlock(&inode->i_lock);
1436 kfree(data);
1437 goto out;
1438 }
Peng Taoa9bae562011-07-30 20:52:33 -04001439
1440 pnfs_list_write_lseg(inode, &data->lseg_list);
Andy Adamson863a3c62011-03-23 13:27:54 +00001441
Peng Taoacff58802011-07-30 20:52:31 -04001442 end_pos = nfsi->layout->plh_lwb;
Peng Taoacff58802011-07-30 20:52:31 -04001443 nfsi->layout->plh_lwb = 0;
Andy Adamson863a3c62011-03-23 13:27:54 +00001444
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001445 memcpy(&data->args.stateid.data, nfsi->layout->plh_stateid.data,
1446 sizeof(nfsi->layout->plh_stateid.data));
Andy Adamson863a3c62011-03-23 13:27:54 +00001447 spin_unlock(&inode->i_lock);
1448
1449 data->args.inode = inode;
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}