blob: c2f09e9b670e8a147c086ae42cc8280cd687b81c [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>
Andy Adamson974cec82010-10-20 00:18:02 -040031#include "internal.h"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040032#include "pnfs.h"
Andy Adamson64419a92011-03-01 01:34:16 +000033#include "iostat.h"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040034
35#define NFSDBG_FACILITY NFSDBG_PNFS
36
Fred Isaman02c35fc2010-10-20 00:17:59 -040037/* Locking:
38 *
39 * pnfs_spinlock:
40 * protects pnfs_modules_tbl.
41 */
42static DEFINE_SPINLOCK(pnfs_spinlock);
43
44/*
45 * pnfs_modules_tbl holds all pnfs modules
46 */
47static LIST_HEAD(pnfs_modules_tbl);
48
49/* Return the registered pnfs layout driver module matching given id */
50static struct pnfs_layoutdriver_type *
51find_pnfs_driver_locked(u32 id)
52{
53 struct pnfs_layoutdriver_type *local;
54
55 list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
56 if (local->id == id)
57 goto out;
58 local = NULL;
59out:
60 dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
61 return local;
62}
63
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040064static struct pnfs_layoutdriver_type *
65find_pnfs_driver(u32 id)
66{
Fred Isaman02c35fc2010-10-20 00:17:59 -040067 struct pnfs_layoutdriver_type *local;
68
69 spin_lock(&pnfs_spinlock);
70 local = find_pnfs_driver_locked(id);
71 spin_unlock(&pnfs_spinlock);
72 return local;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040073}
74
75void
76unset_pnfs_layoutdriver(struct nfs_server *nfss)
77{
Christoph Hellwigea8eecd2011-03-01 01:34:21 +000078 if (nfss->pnfs_curr_ld)
Fred Isaman02c35fc2010-10-20 00:17:59 -040079 module_put(nfss->pnfs_curr_ld->owner);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040080 nfss->pnfs_curr_ld = NULL;
81}
82
83/*
84 * Try to set the server's pnfs module to the pnfs layout type specified by id.
85 * Currently only one pNFS layout driver per filesystem is supported.
86 *
87 * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
88 */
89void
90set_pnfs_layoutdriver(struct nfs_server *server, u32 id)
91{
92 struct pnfs_layoutdriver_type *ld_type = NULL;
93
94 if (id == 0)
95 goto out_no_driver;
96 if (!(server->nfs_client->cl_exchange_flags &
97 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
98 printk(KERN_ERR "%s: id %u cl_exchange_flags 0x%x\n", __func__,
99 id, server->nfs_client->cl_exchange_flags);
100 goto out_no_driver;
101 }
102 ld_type = find_pnfs_driver(id);
103 if (!ld_type) {
104 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
105 ld_type = find_pnfs_driver(id);
106 if (!ld_type) {
107 dprintk("%s: No pNFS module found for %u.\n",
108 __func__, id);
109 goto out_no_driver;
110 }
111 }
Fred Isaman02c35fc2010-10-20 00:17:59 -0400112 if (!try_module_get(ld_type->owner)) {
113 dprintk("%s: Could not grab reference on module\n", __func__);
114 goto out_no_driver;
115 }
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400116 server->pnfs_curr_ld = ld_type;
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000117
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400118 dprintk("%s: pNFS module for %u set\n", __func__, id);
119 return;
120
121out_no_driver:
122 dprintk("%s: Using NFSv4 I/O\n", __func__);
123 server->pnfs_curr_ld = NULL;
124}
Fred Isaman02c35fc2010-10-20 00:17:59 -0400125
126int
127pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
128{
129 int status = -EINVAL;
130 struct pnfs_layoutdriver_type *tmp;
131
132 if (ld_type->id == 0) {
133 printk(KERN_ERR "%s id 0 is reserved\n", __func__);
134 return status;
135 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400136 if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
137 printk(KERN_ERR "%s Layout driver must provide "
138 "alloc_lseg and free_lseg.\n", __func__);
139 return status;
140 }
Fred Isaman02c35fc2010-10-20 00:17:59 -0400141
142 spin_lock(&pnfs_spinlock);
143 tmp = find_pnfs_driver_locked(ld_type->id);
144 if (!tmp) {
145 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
146 status = 0;
147 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
148 ld_type->name);
149 } else {
150 printk(KERN_ERR "%s Module with id %d already loaded!\n",
151 __func__, ld_type->id);
152 }
153 spin_unlock(&pnfs_spinlock);
154
155 return status;
156}
157EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
158
159void
160pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
161{
162 dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
163 spin_lock(&pnfs_spinlock);
164 list_del(&ld_type->pnfs_tblid);
165 spin_unlock(&pnfs_spinlock);
166}
167EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
Benny Halevye5e94012010-10-20 00:18:01 -0400168
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400169/*
170 * pNFS client layout cache
171 */
172
Fred Isamancc6e5342011-01-06 11:36:28 +0000173/* Need to hold i_lock if caller does not already hold reference */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000174void
Fred Isamancc6e5342011-01-06 11:36:28 +0000175get_layout_hdr(struct pnfs_layout_hdr *lo)
Benny Halevye5e94012010-10-20 00:18:01 -0400176{
Fred Isamancc6e5342011-01-06 11:36:28 +0000177 atomic_inc(&lo->plh_refcount);
178}
179
180static void
181destroy_layout_hdr(struct pnfs_layout_hdr *lo)
182{
183 dprintk("%s: freeing layout cache %p\n", __func__, lo);
184 BUG_ON(!list_empty(&lo->plh_layouts));
185 NFS_I(lo->plh_inode)->layout = NULL;
186 kfree(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400187}
188
189static void
190put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
191{
Fred Isamancc6e5342011-01-06 11:36:28 +0000192 if (atomic_dec_and_test(&lo->plh_refcount))
193 destroy_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400194}
195
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400196void
Fred Isamancc6e5342011-01-06 11:36:28 +0000197put_layout_hdr(struct pnfs_layout_hdr *lo)
Andy Adamson974cec82010-10-20 00:18:02 -0400198{
Fred Isamancc6e5342011-01-06 11:36:28 +0000199 struct inode *inode = lo->plh_inode;
200
201 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
202 destroy_layout_hdr(lo);
203 spin_unlock(&inode->i_lock);
204 }
Andy Adamson974cec82010-10-20 00:18:02 -0400205}
206
207static void
208init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
209{
Fred Isaman566052c2011-01-06 11:36:20 +0000210 INIT_LIST_HEAD(&lseg->pls_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000211 atomic_set(&lseg->pls_refcount, 1);
212 smp_mb();
213 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
Fred Isaman566052c2011-01-06 11:36:20 +0000214 lseg->pls_layout = lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400215}
216
Fred Isaman4541d162011-01-06 11:36:23 +0000217static void free_lseg(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400218{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000219 struct inode *ino = lseg->pls_layout->plh_inode;
Andy Adamson974cec82010-10-20 00:18:02 -0400220
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400221 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
Fred Isaman52fabd72011-01-06 11:36:18 +0000222 /* Matched by get_layout_hdr in pnfs_insert_layout */
Fred Isamancc6e5342011-01-06 11:36:28 +0000223 put_layout_hdr(NFS_I(ino)->layout);
Andy Adamson974cec82010-10-20 00:18:02 -0400224}
225
Fred Isamand684d2a2011-03-01 01:34:13 +0000226static void
227put_lseg_common(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400228{
Fred Isamand684d2a2011-03-01 01:34:13 +0000229 struct inode *inode = lseg->pls_layout->plh_inode;
230
231 BUG_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
232 list_del_init(&lseg->pls_list);
233 if (list_empty(&lseg->pls_layout->plh_segs)) {
234 set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags);
235 /* Matched by initial refcount set in alloc_init_layout_hdr */
236 put_layout_hdr_locked(lseg->pls_layout);
237 }
238 rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
239}
240
Fred Isamanbae724e2011-03-01 01:34:15 +0000241void
Fred Isamand684d2a2011-03-01 01:34:13 +0000242put_lseg(struct pnfs_layout_segment *lseg)
243{
244 struct inode *inode;
245
246 if (!lseg)
247 return;
248
Fred Isaman4541d162011-01-06 11:36:23 +0000249 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
250 atomic_read(&lseg->pls_refcount),
251 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Fred Isamand684d2a2011-03-01 01:34:13 +0000252 inode = lseg->pls_layout->plh_inode;
253 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
254 LIST_HEAD(free_me);
Andy Adamson974cec82010-10-20 00:18:02 -0400255
Fred Isamand684d2a2011-03-01 01:34:13 +0000256 put_lseg_common(lseg);
257 list_add(&lseg->pls_list, &free_me);
258 spin_unlock(&inode->i_lock);
259 pnfs_free_lseg_list(&free_me);
Fred Isaman4541d162011-01-06 11:36:23 +0000260 }
Andy Adamson974cec82010-10-20 00:18:02 -0400261}
Fred Isamane0c2b382011-03-23 13:27:53 +0000262EXPORT_SYMBOL_GPL(put_lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400263
Benny Halevyfb3296e2011-05-22 19:47:26 +0300264static inline u64
265end_offset(u64 start, u64 len)
266{
267 u64 end;
268
269 end = start + len;
270 return end >= start ? end : NFS4_MAX_UINT64;
271}
272
273/* last octet in a range */
274static inline u64
275last_byte_offset(u64 start, u64 len)
276{
277 u64 end;
278
279 BUG_ON(!len);
280 end = start + len;
281 return end > start ? end - 1 : NFS4_MAX_UINT64;
282}
283
284/*
285 * is l2 fully contained in l1?
286 * start1 end1
287 * [----------------------------------)
288 * start2 end2
289 * [----------------)
290 */
291static inline int
292lo_seg_contained(struct pnfs_layout_range *l1,
293 struct pnfs_layout_range *l2)
294{
295 u64 start1 = l1->offset;
296 u64 end1 = end_offset(start1, l1->length);
297 u64 start2 = l2->offset;
298 u64 end2 = end_offset(start2, l2->length);
299
300 return (start1 <= start2) && (end1 >= end2);
301}
302
303/*
304 * is l1 and l2 intersecting?
305 * start1 end1
306 * [----------------------------------)
307 * start2 end2
308 * [----------------)
309 */
310static inline int
311lo_seg_intersecting(struct pnfs_layout_range *l1,
312 struct pnfs_layout_range *l2)
313{
314 u64 start1 = l1->offset;
315 u64 end1 = end_offset(start1, l1->length);
316 u64 start2 = l2->offset;
317 u64 end2 = end_offset(start2, l2->length);
318
319 return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
320 (end2 == NFS4_MAX_UINT64 || end2 > start1);
321}
322
Fred Isaman4541d162011-01-06 11:36:23 +0000323static bool
324should_free_lseg(u32 lseg_iomode, u32 recall_iomode)
325{
326 return (recall_iomode == IOMODE_ANY ||
327 lseg_iomode == recall_iomode);
328}
329
330/* Returns 1 if lseg is removed from list, 0 otherwise */
331static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
332 struct list_head *tmp_list)
333{
334 int rv = 0;
335
336 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
337 /* Remove the reference keeping the lseg in the
338 * list. It will now be removed when all
339 * outstanding io is finished.
340 */
Fred Isamand684d2a2011-03-01 01:34:13 +0000341 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
342 atomic_read(&lseg->pls_refcount));
343 if (atomic_dec_and_test(&lseg->pls_refcount)) {
344 put_lseg_common(lseg);
345 list_add(&lseg->pls_list, tmp_list);
346 rv = 1;
347 }
Fred Isaman4541d162011-01-06 11:36:23 +0000348 }
349 return rv;
350}
351
352/* Returns count of number of matching invalid lsegs remaining in list
353 * after call.
354 */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000355int
Fred Isaman4541d162011-01-06 11:36:23 +0000356mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
357 struct list_head *tmp_list,
358 u32 iomode)
Andy Adamson974cec82010-10-20 00:18:02 -0400359{
360 struct pnfs_layout_segment *lseg, *next;
Fred Isaman4541d162011-01-06 11:36:23 +0000361 int invalid = 0, removed = 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400362
363 dprintk("%s:Begin lo %p\n", __func__, lo);
364
Fred Isaman38511722011-02-03 18:28:50 +0000365 if (list_empty(&lo->plh_segs)) {
366 if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags))
367 put_layout_hdr_locked(lo);
368 return 0;
369 }
Fred Isaman4541d162011-01-06 11:36:23 +0000370 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
371 if (should_free_lseg(lseg->pls_range.iomode, iomode)) {
372 dprintk("%s: freeing lseg %p iomode %d "
373 "offset %llu length %llu\n", __func__,
374 lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
375 lseg->pls_range.length);
376 invalid++;
377 removed += mark_lseg_invalid(lseg, tmp_list);
378 }
379 dprintk("%s:Return %i\n", __func__, invalid - removed);
380 return invalid - removed;
Andy Adamson974cec82010-10-20 00:18:02 -0400381}
382
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000383/* note free_me must contain lsegs from a single layout_hdr */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000384void
Fred Isaman4541d162011-01-06 11:36:23 +0000385pnfs_free_lseg_list(struct list_head *free_me)
Andy Adamson974cec82010-10-20 00:18:02 -0400386{
Fred Isaman4541d162011-01-06 11:36:23 +0000387 struct pnfs_layout_segment *lseg, *tmp;
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000388 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400389
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000390 if (list_empty(free_me))
391 return;
392
393 lo = list_first_entry(free_me, struct pnfs_layout_segment,
394 pls_list)->pls_layout;
395
396 if (test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) {
397 struct nfs_client *clp;
398
399 clp = NFS_SERVER(lo->plh_inode)->nfs_client;
400 spin_lock(&clp->cl_lock);
401 list_del_init(&lo->plh_layouts);
402 spin_unlock(&clp->cl_lock);
403 }
Fred Isaman4541d162011-01-06 11:36:23 +0000404 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
Fred Isaman566052c2011-01-06 11:36:20 +0000405 list_del(&lseg->pls_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000406 free_lseg(lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400407 }
408}
409
Benny Halevye5e94012010-10-20 00:18:01 -0400410void
411pnfs_destroy_layout(struct nfs_inode *nfsi)
412{
413 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400414 LIST_HEAD(tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400415
416 spin_lock(&nfsi->vfs_inode.i_lock);
417 lo = nfsi->layout;
418 if (lo) {
Fred Isaman38511722011-02-03 18:28:50 +0000419 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
Fred Isaman4541d162011-01-06 11:36:23 +0000420 mark_matching_lsegs_invalid(lo, &tmp_list, IOMODE_ANY);
Benny Halevye5e94012010-10-20 00:18:01 -0400421 }
422 spin_unlock(&nfsi->vfs_inode.i_lock);
Andy Adamson974cec82010-10-20 00:18:02 -0400423 pnfs_free_lseg_list(&tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400424}
425
Andy Adamson974cec82010-10-20 00:18:02 -0400426/*
427 * Called by the state manger to remove all layouts established under an
428 * expired lease.
429 */
430void
431pnfs_destroy_all_layouts(struct nfs_client *clp)
432{
433 struct pnfs_layout_hdr *lo;
434 LIST_HEAD(tmp_list);
435
436 spin_lock(&clp->cl_lock);
437 list_splice_init(&clp->cl_layouts, &tmp_list);
438 spin_unlock(&clp->cl_lock);
439
440 while (!list_empty(&tmp_list)) {
441 lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000442 plh_layouts);
Andy Adamson974cec82010-10-20 00:18:02 -0400443 dprintk("%s freeing layout for inode %lu\n", __func__,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000444 lo->plh_inode->i_ino);
Andy Adamson2887fe42011-05-11 01:19:58 -0400445 list_del_init(&lo->plh_layouts);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000446 pnfs_destroy_layout(NFS_I(lo->plh_inode));
Andy Adamson974cec82010-10-20 00:18:02 -0400447 }
448}
449
Fred Isamanfd6002e2011-01-06 11:36:22 +0000450/* update lo->plh_stateid with new if is more recent */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000451void
452pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
453 bool update_barrier)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400454{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000455 u32 oldseq, newseq;
Andy Adamson974cec82010-10-20 00:18:02 -0400456
Fred Isamanfd6002e2011-01-06 11:36:22 +0000457 oldseq = be32_to_cpu(lo->plh_stateid.stateid.seqid);
458 newseq = be32_to_cpu(new->stateid.seqid);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000459 if ((int)(newseq - oldseq) > 0) {
Fred Isamanfd6002e2011-01-06 11:36:22 +0000460 memcpy(&lo->plh_stateid, &new->stateid, sizeof(new->stateid));
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000461 if (update_barrier) {
462 u32 new_barrier = be32_to_cpu(new->stateid.seqid);
463
464 if ((int)(new_barrier - lo->plh_barrier))
465 lo->plh_barrier = new_barrier;
466 } else {
467 /* Because of wraparound, we want to keep the barrier
468 * "close" to the current seqids. It needs to be
469 * within 2**31 to count as "behind", so if it
470 * gets too near that limit, give us a litle leeway
471 * and bring it to within 2**30.
472 * NOTE - and yes, this is all unsigned arithmetic.
473 */
474 if (unlikely((newseq - lo->plh_barrier) > (3 << 29)))
475 lo->plh_barrier = newseq - (1 << 30);
476 }
477 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400478}
479
Fred Isamancf7d63f2011-01-06 11:36:25 +0000480/* lget is set to 1 if called from inside send_layoutget call chain */
481static bool
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000482pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid,
483 int lget)
Fred Isamancf7d63f2011-01-06 11:36:25 +0000484{
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000485 if ((stateid) &&
486 (int)(lo->plh_barrier - be32_to_cpu(stateid->stateid.seqid)) >= 0)
487 return true;
Fred Isamanf7e89172011-01-06 11:36:32 +0000488 return lo->plh_block_lgets ||
Fred Isaman38511722011-02-03 18:28:50 +0000489 test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) ||
Fred Isamanf7e89172011-01-06 11:36:32 +0000490 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000491 (list_empty(&lo->plh_segs) &&
Fred Isamancf7d63f2011-01-06 11:36:25 +0000492 (atomic_read(&lo->plh_outstanding) > lget));
493}
494
Fred Isamanfd6002e2011-01-06 11:36:22 +0000495int
496pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
497 struct nfs4_state *open_state)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400498{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000499 int status = 0;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400500
501 dprintk("--> %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000502 spin_lock(&lo->plh_inode->i_lock);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000503 if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
Fred Isamancf7d63f2011-01-06 11:36:25 +0000504 status = -EAGAIN;
505 } else if (list_empty(&lo->plh_segs)) {
Fred Isamanfd6002e2011-01-06 11:36:22 +0000506 int seq;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400507
Fred Isamanfd6002e2011-01-06 11:36:22 +0000508 do {
509 seq = read_seqbegin(&open_state->seqlock);
510 memcpy(dst->data, open_state->stateid.data,
511 sizeof(open_state->stateid.data));
512 } while (read_seqretry(&open_state->seqlock, seq));
513 } else
514 memcpy(dst->data, lo->plh_stateid.data, sizeof(lo->plh_stateid.data));
515 spin_unlock(&lo->plh_inode->i_lock);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400516 dprintk("<-- %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000517 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400518}
519
520/*
521* Get layout from server.
522* for now, assume that whole file layouts are requested.
523* arg->offset: 0
524* arg->length: all ones
525*/
Benny Halevye5e94012010-10-20 00:18:01 -0400526static struct pnfs_layout_segment *
527send_layoutget(struct pnfs_layout_hdr *lo,
528 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +0300529 struct pnfs_layout_range *range,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400530 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400531{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000532 struct inode *ino = lo->plh_inode;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400533 struct nfs_server *server = NFS_SERVER(ino);
534 struct nfs4_layoutget *lgp;
535 struct pnfs_layout_segment *lseg = NULL;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400536 struct page **pages = NULL;
537 int i;
538 u32 max_resp_sz, max_pages;
Benny Halevye5e94012010-10-20 00:18:01 -0400539
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400540 dprintk("--> %s\n", __func__);
541
542 BUG_ON(ctx == NULL);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400543 lgp = kzalloc(sizeof(*lgp), gfp_flags);
Fred Isamancf7d63f2011-01-06 11:36:25 +0000544 if (lgp == NULL)
Andy Adamson974cec82010-10-20 00:18:02 -0400545 return NULL;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400546
547 /* allocate pages for xdr post processing */
548 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
549 max_pages = max_resp_sz >> PAGE_SHIFT;
550
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400551 pages = kzalloc(max_pages * sizeof(struct page *), gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400552 if (!pages)
553 goto out_err_free;
554
555 for (i = 0; i < max_pages; i++) {
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400556 pages[i] = alloc_page(gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400557 if (!pages[i])
558 goto out_err_free;
559 }
560
Benny Halevyfb3296e2011-05-22 19:47:26 +0300561 lgp->args.minlength = PAGE_CACHE_SIZE;
562 if (lgp->args.minlength > range->length)
563 lgp->args.minlength = range->length;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400564 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
Benny Halevyfb3296e2011-05-22 19:47:26 +0300565 lgp->args.range = *range;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400566 lgp->args.type = server->pnfs_curr_ld->id;
567 lgp->args.inode = ino;
568 lgp->args.ctx = get_nfs_open_context(ctx);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400569 lgp->args.layout.pages = pages;
570 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400571 lgp->lsegpp = &lseg;
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400572 lgp->gfp_flags = gfp_flags;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400573
574 /* Synchronously retrieve layout information from server and
575 * store in lseg.
576 */
577 nfs4_proc_layoutget(lgp);
578 if (!lseg) {
579 /* remember that LAYOUTGET failed and suspend trying */
Benny Halevyfb3296e2011-05-22 19:47:26 +0300580 set_bit(lo_fail_bit(range->iomode), &lo->plh_flags);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400581 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400582
583 /* free xdr pages */
584 for (i = 0; i < max_pages; i++)
585 __free_page(pages[i]);
586 kfree(pages);
587
Andy Adamson974cec82010-10-20 00:18:02 -0400588 return lseg;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400589
590out_err_free:
591 /* free any allocated xdr pages, lgp as it's not used */
592 if (pages) {
593 for (i = 0; i < max_pages; i++) {
594 if (!pages[i])
595 break;
596 __free_page(pages[i]);
597 }
598 kfree(pages);
599 }
600 kfree(lgp);
601 return NULL;
Andy Adamson974cec82010-10-20 00:18:02 -0400602}
603
Fred Isamanf7e89172011-01-06 11:36:32 +0000604bool pnfs_roc(struct inode *ino)
605{
606 struct pnfs_layout_hdr *lo;
607 struct pnfs_layout_segment *lseg, *tmp;
608 LIST_HEAD(tmp_list);
609 bool found = false;
610
611 spin_lock(&ino->i_lock);
612 lo = NFS_I(ino)->layout;
613 if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
614 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
615 goto out_nolayout;
616 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
617 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
618 mark_lseg_invalid(lseg, &tmp_list);
619 found = true;
620 }
621 if (!found)
622 goto out_nolayout;
623 lo->plh_block_lgets++;
624 get_layout_hdr(lo); /* matched in pnfs_roc_release */
625 spin_unlock(&ino->i_lock);
626 pnfs_free_lseg_list(&tmp_list);
627 return true;
628
629out_nolayout:
630 spin_unlock(&ino->i_lock);
631 return false;
632}
633
634void pnfs_roc_release(struct inode *ino)
635{
636 struct pnfs_layout_hdr *lo;
637
638 spin_lock(&ino->i_lock);
639 lo = NFS_I(ino)->layout;
640 lo->plh_block_lgets--;
641 put_layout_hdr_locked(lo);
642 spin_unlock(&ino->i_lock);
643}
644
645void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
646{
647 struct pnfs_layout_hdr *lo;
648
649 spin_lock(&ino->i_lock);
650 lo = NFS_I(ino)->layout;
651 if ((int)(barrier - lo->plh_barrier) > 0)
652 lo->plh_barrier = barrier;
653 spin_unlock(&ino->i_lock);
654}
655
656bool pnfs_roc_drain(struct inode *ino, u32 *barrier)
657{
658 struct nfs_inode *nfsi = NFS_I(ino);
659 struct pnfs_layout_segment *lseg;
660 bool found = false;
661
662 spin_lock(&ino->i_lock);
663 list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
664 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
665 found = true;
666 break;
667 }
668 if (!found) {
669 struct pnfs_layout_hdr *lo = nfsi->layout;
670 u32 current_seqid = be32_to_cpu(lo->plh_stateid.stateid.seqid);
671
672 /* Since close does not return a layout stateid for use as
673 * a barrier, we choose the worst-case barrier.
674 */
675 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
676 }
677 spin_unlock(&ino->i_lock);
678 return found;
679}
680
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400681/*
682 * Compare two layout segments for sorting into layout cache.
683 * We want to preferentially return RW over RO layouts, so ensure those
684 * are seen first.
685 */
686static s64
Benny Halevyfb3296e2011-05-22 19:47:26 +0300687cmp_layout(struct pnfs_layout_range *l1,
688 struct pnfs_layout_range *l2)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400689{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300690 s64 d;
691
692 /* high offset > low offset */
693 d = l1->offset - l2->offset;
694 if (d)
695 return d;
696
697 /* short length > long length */
698 d = l2->length - l1->length;
699 if (d)
700 return d;
701
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400702 /* read > read/write */
Benny Halevyfb3296e2011-05-22 19:47:26 +0300703 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400704}
705
Andy Adamson974cec82010-10-20 00:18:02 -0400706static void
707pnfs_insert_layout(struct pnfs_layout_hdr *lo,
708 struct pnfs_layout_segment *lseg)
709{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400710 struct pnfs_layout_segment *lp;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400711
Andy Adamson974cec82010-10-20 00:18:02 -0400712 dprintk("%s:Begin\n", __func__);
713
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000714 assert_spin_locked(&lo->plh_inode->i_lock);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000715 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
Benny Halevyfb3296e2011-05-22 19:47:26 +0300716 if (cmp_layout(&lseg->pls_range, &lp->pls_range) > 0)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400717 continue;
Fred Isaman566052c2011-01-06 11:36:20 +0000718 list_add_tail(&lseg->pls_list, &lp->pls_list);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400719 dprintk("%s: inserted lseg %p "
720 "iomode %d offset %llu length %llu before "
721 "lp %p iomode %d offset %llu length %llu\n",
Fred Isaman566052c2011-01-06 11:36:20 +0000722 __func__, lseg, lseg->pls_range.iomode,
723 lseg->pls_range.offset, lseg->pls_range.length,
724 lp, lp->pls_range.iomode, lp->pls_range.offset,
725 lp->pls_range.length);
Benny Halevyfb3296e2011-05-22 19:47:26 +0300726 goto out;
Andy Adamson974cec82010-10-20 00:18:02 -0400727 }
Benny Halevyfb3296e2011-05-22 19:47:26 +0300728 list_add_tail(&lseg->pls_list, &lo->plh_segs);
729 dprintk("%s: inserted lseg %p "
730 "iomode %d offset %llu length %llu at tail\n",
731 __func__, lseg, lseg->pls_range.iomode,
732 lseg->pls_range.offset, lseg->pls_range.length);
733out:
Fred Isamancc6e5342011-01-06 11:36:28 +0000734 get_layout_hdr(lo);
Andy Adamson974cec82010-10-20 00:18:02 -0400735
736 dprintk("%s:Return\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -0400737}
738
739static struct pnfs_layout_hdr *
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400740alloc_init_layout_hdr(struct inode *ino, gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400741{
742 struct pnfs_layout_hdr *lo;
743
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400744 lo = kzalloc(sizeof(struct pnfs_layout_hdr), gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400745 if (!lo)
746 return NULL;
Fred Isamancc6e5342011-01-06 11:36:28 +0000747 atomic_set(&lo->plh_refcount, 1);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000748 INIT_LIST_HEAD(&lo->plh_layouts);
749 INIT_LIST_HEAD(&lo->plh_segs);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000750 INIT_LIST_HEAD(&lo->plh_bulk_recall);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000751 lo->plh_inode = ino;
Benny Halevye5e94012010-10-20 00:18:01 -0400752 return lo;
753}
754
755static struct pnfs_layout_hdr *
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400756pnfs_find_alloc_layout(struct inode *ino, gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400757{
758 struct nfs_inode *nfsi = NFS_I(ino);
759 struct pnfs_layout_hdr *new = NULL;
760
761 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
762
763 assert_spin_locked(&ino->i_lock);
Fred Isaman4541d162011-01-06 11:36:23 +0000764 if (nfsi->layout) {
765 if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
766 return NULL;
767 else
768 return nfsi->layout;
769 }
Benny Halevye5e94012010-10-20 00:18:01 -0400770 spin_unlock(&ino->i_lock);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400771 new = alloc_init_layout_hdr(ino, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400772 spin_lock(&ino->i_lock);
773
774 if (likely(nfsi->layout == NULL)) /* Won the race? */
775 nfsi->layout = new;
776 else
777 kfree(new);
778 return nfsi->layout;
779}
780
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400781/*
782 * iomode matching rules:
783 * iomode lseg match
784 * ----- ----- -----
785 * ANY READ true
786 * ANY RW true
787 * RW READ false
788 * RW RW true
789 * READ READ true
790 * READ RW true
791 */
792static int
Benny Halevyfb3296e2011-05-22 19:47:26 +0300793is_matching_lseg(struct pnfs_layout_range *ls_range,
794 struct pnfs_layout_range *range)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400795{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300796 struct pnfs_layout_range range1;
797
798 if ((range->iomode == IOMODE_RW &&
799 ls_range->iomode != IOMODE_RW) ||
800 !lo_seg_intersecting(ls_range, range))
801 return 0;
802
803 /* range1 covers only the first byte in the range */
804 range1 = *range;
805 range1.length = 1;
806 return lo_seg_contained(ls_range, &range1);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400807}
808
809/*
810 * lookup range in layout
811 */
Benny Halevye5e94012010-10-20 00:18:01 -0400812static struct pnfs_layout_segment *
Benny Halevyfb3296e2011-05-22 19:47:26 +0300813pnfs_find_lseg(struct pnfs_layout_hdr *lo,
814 struct pnfs_layout_range *range)
Benny Halevye5e94012010-10-20 00:18:01 -0400815{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400816 struct pnfs_layout_segment *lseg, *ret = NULL;
817
818 dprintk("%s:Begin\n", __func__);
819
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000820 assert_spin_locked(&lo->plh_inode->i_lock);
821 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
Fred Isaman4541d162011-01-06 11:36:23 +0000822 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
Benny Halevyfb3296e2011-05-22 19:47:26 +0300823 is_matching_lseg(&lseg->pls_range, range)) {
Fred Isamand684d2a2011-03-01 01:34:13 +0000824 ret = get_lseg(lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400825 break;
826 }
Benny Halevyfb3296e2011-05-22 19:47:26 +0300827 if (cmp_layout(range, &lseg->pls_range) > 0)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400828 break;
829 }
830
831 dprintk("%s:Return lseg %p ref %d\n",
Fred Isaman4541d162011-01-06 11:36:23 +0000832 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400833 return ret;
Benny Halevye5e94012010-10-20 00:18:01 -0400834}
835
836/*
837 * Layout segment is retreived from the server if not cached.
838 * The appropriate layout segment is referenced and returned to the caller.
839 */
840struct pnfs_layout_segment *
841pnfs_update_layout(struct inode *ino,
842 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +0300843 loff_t pos,
844 u64 count,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400845 enum pnfs_iomode iomode,
846 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400847{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300848 struct pnfs_layout_range arg = {
849 .iomode = iomode,
850 .offset = pos,
851 .length = count,
852 };
Benny Halevye5e94012010-10-20 00:18:01 -0400853 struct nfs_inode *nfsi = NFS_I(ino);
Fred Isaman2130ff62011-01-06 11:36:26 +0000854 struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
Benny Halevye5e94012010-10-20 00:18:01 -0400855 struct pnfs_layout_hdr *lo;
856 struct pnfs_layout_segment *lseg = NULL;
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000857 bool first = false;
Benny Halevye5e94012010-10-20 00:18:01 -0400858
859 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
860 return NULL;
861 spin_lock(&ino->i_lock);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400862 lo = pnfs_find_alloc_layout(ino, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400863 if (lo == NULL) {
864 dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
865 goto out_unlock;
866 }
867
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000868 /* Do we even need to bother with this? */
869 if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
870 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
871 dprintk("%s matches recall, use MDS\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -0400872 goto out_unlock;
873 }
874
875 /* if LAYOUTGET already failed once we don't try again */
Fred Isaman566052c2011-01-06 11:36:20 +0000876 if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
Benny Halevye5e94012010-10-20 00:18:01 -0400877 goto out_unlock;
878
Andy Adamson568e8c42011-03-01 01:34:22 +0000879 /* Check to see if the layout for the given range already exists */
Benny Halevyfb3296e2011-05-22 19:47:26 +0300880 lseg = pnfs_find_lseg(lo, &arg);
Andy Adamson568e8c42011-03-01 01:34:22 +0000881 if (lseg)
882 goto out_unlock;
883
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000884 if (pnfs_layoutgets_blocked(lo, NULL, 0))
Fred Isamancf7d63f2011-01-06 11:36:25 +0000885 goto out_unlock;
886 atomic_inc(&lo->plh_outstanding);
887
Fred Isamancc6e5342011-01-06 11:36:28 +0000888 get_layout_hdr(lo);
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000889 if (list_empty(&lo->plh_segs))
890 first = true;
891 spin_unlock(&ino->i_lock);
892 if (first) {
Fred Isaman2130ff62011-01-06 11:36:26 +0000893 /* The lo must be on the clp list if there is any
894 * chance of a CB_LAYOUTRECALL(FILE) coming in.
895 */
896 spin_lock(&clp->cl_lock);
897 BUG_ON(!list_empty(&lo->plh_layouts));
898 list_add_tail(&lo->plh_layouts, &clp->cl_layouts);
899 spin_unlock(&clp->cl_lock);
900 }
Benny Halevye5e94012010-10-20 00:18:01 -0400901
Benny Halevyfb3296e2011-05-22 19:47:26 +0300902 lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000903 if (!lseg && first) {
904 spin_lock(&clp->cl_lock);
905 list_del_init(&lo->plh_layouts);
906 spin_unlock(&clp->cl_lock);
Fred Isaman2130ff62011-01-06 11:36:26 +0000907 }
Fred Isamancf7d63f2011-01-06 11:36:25 +0000908 atomic_dec(&lo->plh_outstanding);
Fred Isamancc6e5342011-01-06 11:36:28 +0000909 put_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400910out:
911 dprintk("%s end, state 0x%lx lseg %p\n", __func__,
Andy Adamsonbf9c1382011-03-01 01:34:07 +0000912 nfsi->layout ? nfsi->layout->plh_flags : -1, lseg);
Benny Halevye5e94012010-10-20 00:18:01 -0400913 return lseg;
914out_unlock:
915 spin_unlock(&ino->i_lock);
916 goto out;
917}
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400918
919int
920pnfs_layout_process(struct nfs4_layoutget *lgp)
921{
922 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
923 struct nfs4_layoutget_res *res = &lgp->res;
924 struct pnfs_layout_segment *lseg;
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000925 struct inode *ino = lo->plh_inode;
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000926 struct nfs_client *clp = NFS_SERVER(ino)->nfs_client;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400927 int status = 0;
928
929 /* Inject layout blob into I/O device driver */
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400930 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400931 if (!lseg || IS_ERR(lseg)) {
932 if (!lseg)
933 status = -ENOMEM;
934 else
935 status = PTR_ERR(lseg);
936 dprintk("%s: Could not allocate layout: error %d\n",
937 __func__, status);
938 goto out;
939 }
940
941 spin_lock(&ino->i_lock);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000942 if (test_bit(NFS4CLNT_LAYOUTRECALL, &clp->cl_state) ||
943 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
944 dprintk("%s forget reply due to recall\n", __func__);
945 goto out_forget_reply;
946 }
947
948 if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) {
949 dprintk("%s forget reply due to state\n", __func__);
950 goto out_forget_reply;
951 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400952 init_lseg(lo, lseg);
Fred Isaman566052c2011-01-06 11:36:20 +0000953 lseg->pls_range = res->range;
Fred Isamand684d2a2011-03-01 01:34:13 +0000954 *lgp->lsegpp = get_lseg(lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400955 pnfs_insert_layout(lo, lseg);
956
Fred Isamanf7e89172011-01-06 11:36:32 +0000957 if (res->return_on_close) {
958 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
959 set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
960 }
961
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400962 /* Done processing layoutget. Set the layout stateid */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000963 pnfs_set_layout_stateid(lo, &res->stateid, false);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400964 spin_unlock(&ino->i_lock);
965out:
966 return status;
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000967
968out_forget_reply:
969 spin_unlock(&ino->i_lock);
970 lseg->pls_layout = lo;
971 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
972 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400973}
974
Fred Isamanbae724e2011-03-01 01:34:15 +0000975static int pnfs_read_pg_test(struct nfs_pageio_descriptor *pgio,
976 struct nfs_page *prev,
977 struct nfs_page *req)
978{
979 if (pgio->pg_count == prev->wb_bytes) {
980 /* This is first coelesce call for a series of nfs_pages */
981 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
982 prev->wb_context,
Benny Halevyfb3296e2011-05-22 19:47:26 +0300983 req_offset(req),
984 pgio->pg_count,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400985 IOMODE_READ,
986 GFP_KERNEL);
Benny Halevyfb3296e2011-05-22 19:47:26 +0300987 } else if (pgio->pg_lseg &&
988 req_offset(req) > end_offset(pgio->pg_lseg->pls_range.offset,
989 pgio->pg_lseg->pls_range.length))
990 return 0;
Fred Isamanbae724e2011-03-01 01:34:15 +0000991 return NFS_SERVER(pgio->pg_inode)->pnfs_curr_ld->pg_test(pgio, prev, req);
992}
993
994void
995pnfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, struct inode *inode)
Fred Isaman94ad1c82011-03-01 01:34:14 +0000996{
997 struct pnfs_layoutdriver_type *ld;
998
999 ld = NFS_SERVER(inode)->pnfs_curr_ld;
Fred Isamanbae724e2011-03-01 01:34:15 +00001000 pgio->pg_test = (ld && ld->pg_test) ? pnfs_read_pg_test : NULL;
Fred Isaman94ad1c82011-03-01 01:34:14 +00001001}
1002
Fred Isaman44b83792011-03-03 15:13:44 +00001003static int pnfs_write_pg_test(struct nfs_pageio_descriptor *pgio,
1004 struct nfs_page *prev,
1005 struct nfs_page *req)
1006{
1007 if (pgio->pg_count == prev->wb_bytes) {
1008 /* This is first coelesce call for a series of nfs_pages */
1009 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1010 prev->wb_context,
Benny Halevyfb3296e2011-05-22 19:47:26 +03001011 req_offset(req),
1012 pgio->pg_count,
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001013 IOMODE_RW,
1014 GFP_NOFS);
Benny Halevyfb3296e2011-05-22 19:47:26 +03001015 } else if (pgio->pg_lseg &&
1016 req_offset(req) > end_offset(pgio->pg_lseg->pls_range.offset,
1017 pgio->pg_lseg->pls_range.length))
1018 return 0;
Fred Isaman44b83792011-03-03 15:13:44 +00001019 return NFS_SERVER(pgio->pg_inode)->pnfs_curr_ld->pg_test(pgio, prev, req);
1020}
1021
1022void
1023pnfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, struct inode *inode)
1024{
1025 struct pnfs_layoutdriver_type *ld;
1026
1027 ld = NFS_SERVER(inode)->pnfs_curr_ld;
1028 pgio->pg_test = (ld && ld->pg_test) ? pnfs_write_pg_test : NULL;
1029}
1030
Andy Adamson0382b742011-03-03 15:13:45 +00001031enum pnfs_try_status
1032pnfs_try_to_write_data(struct nfs_write_data *wdata,
1033 const struct rpc_call_ops *call_ops, int how)
1034{
1035 struct inode *inode = wdata->inode;
1036 enum pnfs_try_status trypnfs;
1037 struct nfs_server *nfss = NFS_SERVER(inode);
1038
1039 wdata->mds_ops = call_ops;
1040
1041 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
1042 inode->i_ino, wdata->args.count, wdata->args.offset, how);
1043
1044 trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how);
1045 if (trypnfs == PNFS_NOT_ATTEMPTED) {
1046 put_lseg(wdata->lseg);
1047 wdata->lseg = NULL;
1048 } else
1049 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
1050
1051 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1052 return trypnfs;
1053}
1054
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001055/*
Andy Adamson64419a92011-03-01 01:34:16 +00001056 * Call the appropriate parallel I/O subsystem read function.
1057 */
1058enum pnfs_try_status
1059pnfs_try_to_read_data(struct nfs_read_data *rdata,
1060 const struct rpc_call_ops *call_ops)
1061{
1062 struct inode *inode = rdata->inode;
1063 struct nfs_server *nfss = NFS_SERVER(inode);
1064 enum pnfs_try_status trypnfs;
1065
1066 rdata->mds_ops = call_ops;
1067
1068 dprintk("%s: Reading ino:%lu %u@%llu\n",
1069 __func__, inode->i_ino, rdata->args.count, rdata->args.offset);
1070
1071 trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata);
1072 if (trypnfs == PNFS_NOT_ATTEMPTED) {
1073 put_lseg(rdata->lseg);
1074 rdata->lseg = NULL;
1075 } else {
1076 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
1077 }
1078 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1079 return trypnfs;
1080}
Andy Adamson863a3c62011-03-23 13:27:54 +00001081
1082/*
1083 * Currently there is only one (whole file) write lseg.
1084 */
1085static struct pnfs_layout_segment *pnfs_list_write_lseg(struct inode *inode)
1086{
1087 struct pnfs_layout_segment *lseg, *rv = NULL;
1088
1089 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list)
1090 if (lseg->pls_range.iomode == IOMODE_RW)
1091 rv = lseg;
1092 return rv;
1093}
1094
1095void
1096pnfs_set_layoutcommit(struct nfs_write_data *wdata)
1097{
1098 struct nfs_inode *nfsi = NFS_I(wdata->inode);
1099 loff_t end_pos = wdata->args.offset + wdata->res.count;
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001100 bool mark_as_dirty = false;
Andy Adamson863a3c62011-03-23 13:27:54 +00001101
1102 spin_lock(&nfsi->vfs_inode.i_lock);
1103 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1104 /* references matched in nfs4_layoutcommit_release */
1105 get_lseg(wdata->lseg);
1106 wdata->lseg->pls_lc_cred =
1107 get_rpccred(wdata->args.context->state->owner->so_cred);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001108 mark_as_dirty = true;
Andy Adamson863a3c62011-03-23 13:27:54 +00001109 dprintk("%s: Set layoutcommit for inode %lu ",
1110 __func__, wdata->inode->i_ino);
1111 }
1112 if (end_pos > wdata->lseg->pls_end_pos)
1113 wdata->lseg->pls_end_pos = end_pos;
1114 spin_unlock(&nfsi->vfs_inode.i_lock);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001115
1116 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
1117 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
1118 if (mark_as_dirty)
1119 mark_inode_dirty_sync(wdata->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00001120}
1121EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
1122
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001123/*
1124 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
1125 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
1126 * data to disk to allow the server to recover the data if it crashes.
1127 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
1128 * is off, and a COMMIT is sent to a data server, or
1129 * if WRITEs to a data server return NFS_DATA_SYNC.
1130 */
Andy Adamson863a3c62011-03-23 13:27:54 +00001131int
Andy Adamsonef311532011-03-12 02:58:10 -05001132pnfs_layoutcommit_inode(struct inode *inode, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00001133{
1134 struct nfs4_layoutcommit_data *data;
1135 struct nfs_inode *nfsi = NFS_I(inode);
1136 struct pnfs_layout_segment *lseg;
1137 struct rpc_cred *cred;
1138 loff_t end_pos;
1139 int status = 0;
1140
1141 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
1142
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001143 if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
1144 return 0;
1145
Andy Adamson863a3c62011-03-23 13:27:54 +00001146 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
1147 data = kzalloc(sizeof(*data), GFP_NOFS);
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001148 if (!data) {
1149 mark_inode_dirty_sync(inode);
1150 status = -ENOMEM;
1151 goto out;
1152 }
Andy Adamson863a3c62011-03-23 13:27:54 +00001153
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001154 spin_lock(&inode->i_lock);
Andy Adamson863a3c62011-03-23 13:27:54 +00001155 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
1156 spin_unlock(&inode->i_lock);
1157 kfree(data);
1158 goto out;
1159 }
1160 /*
1161 * Currently only one (whole file) write lseg which is referenced
1162 * in pnfs_set_layoutcommit and will be found.
1163 */
1164 lseg = pnfs_list_write_lseg(inode);
1165
1166 end_pos = lseg->pls_end_pos;
1167 cred = lseg->pls_lc_cred;
1168 lseg->pls_end_pos = 0;
1169 lseg->pls_lc_cred = NULL;
1170
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001171 memcpy(&data->args.stateid.data, nfsi->layout->plh_stateid.data,
1172 sizeof(nfsi->layout->plh_stateid.data));
Andy Adamson863a3c62011-03-23 13:27:54 +00001173 spin_unlock(&inode->i_lock);
1174
1175 data->args.inode = inode;
1176 data->lseg = lseg;
1177 data->cred = cred;
1178 nfs_fattr_init(&data->fattr);
1179 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
1180 data->res.fattr = &data->fattr;
1181 data->args.lastbytewritten = end_pos - 1;
1182 data->res.server = NFS_SERVER(inode);
1183
1184 status = nfs4_proc_layoutcommit(data, sync);
1185out:
1186 dprintk("<-- %s status %d\n", __func__, status);
1187 return status;
1188}