blob: 3ad768f2cef4064d6728b72fdf0c4aef09164bb0 [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>
Paul Gortmaker143cb492011-07-01 14:23:34 -040032#include <linux/module.h>
Andy Adamson974cec82010-10-20 00:18:02 -040033#include "internal.h"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040034#include "pnfs.h"
Andy Adamson64419a92011-03-01 01:34:16 +000035#include "iostat.h"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040036
37#define NFSDBG_FACILITY NFSDBG_PNFS
38
Fred Isaman02c35fc2010-10-20 00:17:59 -040039/* Locking:
40 *
41 * pnfs_spinlock:
42 * protects pnfs_modules_tbl.
43 */
44static DEFINE_SPINLOCK(pnfs_spinlock);
45
46/*
47 * pnfs_modules_tbl holds all pnfs modules
48 */
49static LIST_HEAD(pnfs_modules_tbl);
50
51/* Return the registered pnfs layout driver module matching given id */
52static struct pnfs_layoutdriver_type *
53find_pnfs_driver_locked(u32 id)
54{
55 struct pnfs_layoutdriver_type *local;
56
57 list_for_each_entry(local, &pnfs_modules_tbl, pnfs_tblid)
58 if (local->id == id)
59 goto out;
60 local = NULL;
61out:
62 dprintk("%s: Searching for id %u, found %p\n", __func__, id, local);
63 return local;
64}
65
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040066static struct pnfs_layoutdriver_type *
67find_pnfs_driver(u32 id)
68{
Fred Isaman02c35fc2010-10-20 00:17:59 -040069 struct pnfs_layoutdriver_type *local;
70
71 spin_lock(&pnfs_spinlock);
72 local = find_pnfs_driver_locked(id);
Trond Myklebust0a9c63f2012-06-15 13:02:58 -040073 if (local != NULL && !try_module_get(local->owner)) {
74 dprintk("%s: Could not grab reference on module\n", __func__);
75 local = NULL;
76 }
Fred Isaman02c35fc2010-10-20 00:17:59 -040077 spin_unlock(&pnfs_spinlock);
78 return local;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040079}
80
81void
82unset_pnfs_layoutdriver(struct nfs_server *nfss)
83{
Benny Halevy738fd0f32011-07-30 20:52:36 -040084 if (nfss->pnfs_curr_ld) {
85 if (nfss->pnfs_curr_ld->clear_layoutdriver)
86 nfss->pnfs_curr_ld->clear_layoutdriver(nfss);
Trond Myklebust2a4c8992012-06-14 13:08:38 -040087 /* Decrement the MDS count. Purge the deviceid cache if zero */
88 if (atomic_dec_and_test(&nfss->nfs_client->cl_mds_count))
89 nfs4_deviceid_purge_client(nfss->nfs_client);
Fred Isaman02c35fc2010-10-20 00:17:59 -040090 module_put(nfss->pnfs_curr_ld->owner);
Benny Halevy738fd0f32011-07-30 20:52:36 -040091 }
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040092 nfss->pnfs_curr_ld = NULL;
93}
94
95/*
96 * Try to set the server's pnfs module to the pnfs layout type specified by id.
97 * Currently only one pNFS layout driver per filesystem is supported.
98 *
99 * @id layout type. Zero (illegal layout type) indicates pNFS not in use.
100 */
101void
Benny Halevy738fd0f32011-07-30 20:52:36 -0400102set_pnfs_layoutdriver(struct nfs_server *server, const struct nfs_fh *mntfh,
103 u32 id)
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400104{
105 struct pnfs_layoutdriver_type *ld_type = NULL;
106
107 if (id == 0)
108 goto out_no_driver;
109 if (!(server->nfs_client->cl_exchange_flags &
110 (EXCHGID4_FLAG_USE_NON_PNFS | EXCHGID4_FLAG_USE_PNFS_MDS))) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500111 printk(KERN_ERR "NFS: %s: id %u cl_exchange_flags 0x%x\n",
112 __func__, id, server->nfs_client->cl_exchange_flags);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400113 goto out_no_driver;
114 }
115 ld_type = find_pnfs_driver(id);
116 if (!ld_type) {
117 request_module("%s-%u", LAYOUT_NFSV4_1_MODULE_PREFIX, id);
118 ld_type = find_pnfs_driver(id);
119 if (!ld_type) {
120 dprintk("%s: No pNFS module found for %u.\n",
121 __func__, id);
122 goto out_no_driver;
123 }
124 }
125 server->pnfs_curr_ld = ld_type;
Benny Halevy738fd0f32011-07-30 20:52:36 -0400126 if (ld_type->set_layoutdriver
127 && ld_type->set_layoutdriver(server, mntfh)) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500128 printk(KERN_ERR "NFS: %s: Error initializing pNFS layout "
129 "driver %u.\n", __func__, id);
Benny Halevy738fd0f32011-07-30 20:52:36 -0400130 module_put(ld_type->owner);
131 goto out_no_driver;
132 }
Trond Myklebust2a4c8992012-06-14 13:08:38 -0400133 /* Bump the MDS count */
134 atomic_inc(&server->nfs_client->cl_mds_count);
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000135
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400136 dprintk("%s: pNFS module for %u set\n", __func__, id);
137 return;
138
139out_no_driver:
140 dprintk("%s: Using NFSv4 I/O\n", __func__);
141 server->pnfs_curr_ld = NULL;
142}
Fred Isaman02c35fc2010-10-20 00:17:59 -0400143
144int
145pnfs_register_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
146{
147 int status = -EINVAL;
148 struct pnfs_layoutdriver_type *tmp;
149
150 if (ld_type->id == 0) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500151 printk(KERN_ERR "NFS: %s id 0 is reserved\n", __func__);
Fred Isaman02c35fc2010-10-20 00:17:59 -0400152 return status;
153 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400154 if (!ld_type->alloc_lseg || !ld_type->free_lseg) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500155 printk(KERN_ERR "NFS: %s Layout driver must provide "
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400156 "alloc_lseg and free_lseg.\n", __func__);
157 return status;
158 }
Fred Isaman02c35fc2010-10-20 00:17:59 -0400159
160 spin_lock(&pnfs_spinlock);
161 tmp = find_pnfs_driver_locked(ld_type->id);
162 if (!tmp) {
163 list_add(&ld_type->pnfs_tblid, &pnfs_modules_tbl);
164 status = 0;
165 dprintk("%s Registering id:%u name:%s\n", __func__, ld_type->id,
166 ld_type->name);
167 } else {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500168 printk(KERN_ERR "NFS: %s Module with id %d already loaded!\n",
Fred Isaman02c35fc2010-10-20 00:17:59 -0400169 __func__, ld_type->id);
170 }
171 spin_unlock(&pnfs_spinlock);
172
173 return status;
174}
175EXPORT_SYMBOL_GPL(pnfs_register_layoutdriver);
176
177void
178pnfs_unregister_layoutdriver(struct pnfs_layoutdriver_type *ld_type)
179{
180 dprintk("%s Deregistering id:%u\n", __func__, ld_type->id);
181 spin_lock(&pnfs_spinlock);
182 list_del(&ld_type->pnfs_tblid);
183 spin_unlock(&pnfs_spinlock);
184}
185EXPORT_SYMBOL_GPL(pnfs_unregister_layoutdriver);
Benny Halevye5e94012010-10-20 00:18:01 -0400186
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400187/*
188 * pNFS client layout cache
189 */
190
Fred Isamancc6e5342011-01-06 11:36:28 +0000191/* Need to hold i_lock if caller does not already hold reference */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000192void
Fred Isamancc6e5342011-01-06 11:36:28 +0000193get_layout_hdr(struct pnfs_layout_hdr *lo)
Benny Halevye5e94012010-10-20 00:18:01 -0400194{
Fred Isamancc6e5342011-01-06 11:36:28 +0000195 atomic_inc(&lo->plh_refcount);
196}
197
Benny Halevy636fb9c2011-05-22 19:51:33 +0300198static struct pnfs_layout_hdr *
199pnfs_alloc_layout_hdr(struct inode *ino, gfp_t gfp_flags)
200{
201 struct pnfs_layoutdriver_type *ld = NFS_SERVER(ino)->pnfs_curr_ld;
202 return ld->alloc_layout_hdr ? ld->alloc_layout_hdr(ino, gfp_flags) :
203 kzalloc(sizeof(struct pnfs_layout_hdr), gfp_flags);
204}
205
206static void
207pnfs_free_layout_hdr(struct pnfs_layout_hdr *lo)
208{
209 struct pnfs_layoutdriver_type *ld = NFS_SERVER(lo->plh_inode)->pnfs_curr_ld;
Peng Tao9fa40752011-07-30 20:52:32 -0400210 put_rpccred(lo->plh_lc_cred);
Benny Halevy636fb9c2011-05-22 19:51:33 +0300211 return ld->alloc_layout_hdr ? ld->free_layout_hdr(lo) : kfree(lo);
212}
213
Fred Isamancc6e5342011-01-06 11:36:28 +0000214static void
215destroy_layout_hdr(struct pnfs_layout_hdr *lo)
216{
217 dprintk("%s: freeing layout cache %p\n", __func__, lo);
218 BUG_ON(!list_empty(&lo->plh_layouts));
219 NFS_I(lo->plh_inode)->layout = NULL;
Benny Halevy636fb9c2011-05-22 19:51:33 +0300220 pnfs_free_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400221}
222
223static void
224put_layout_hdr_locked(struct pnfs_layout_hdr *lo)
225{
Fred Isamancc6e5342011-01-06 11:36:28 +0000226 if (atomic_dec_and_test(&lo->plh_refcount))
227 destroy_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -0400228}
229
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400230void
Fred Isamancc6e5342011-01-06 11:36:28 +0000231put_layout_hdr(struct pnfs_layout_hdr *lo)
Andy Adamson974cec82010-10-20 00:18:02 -0400232{
Fred Isamancc6e5342011-01-06 11:36:28 +0000233 struct inode *inode = lo->plh_inode;
234
235 if (atomic_dec_and_lock(&lo->plh_refcount, &inode->i_lock)) {
236 destroy_layout_hdr(lo);
237 spin_unlock(&inode->i_lock);
238 }
Andy Adamson974cec82010-10-20 00:18:02 -0400239}
240
241static void
242init_lseg(struct pnfs_layout_hdr *lo, struct pnfs_layout_segment *lseg)
243{
Fred Isaman566052c2011-01-06 11:36:20 +0000244 INIT_LIST_HEAD(&lseg->pls_list);
Peng Taoa9bae562011-07-30 20:52:33 -0400245 INIT_LIST_HEAD(&lseg->pls_lc_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000246 atomic_set(&lseg->pls_refcount, 1);
247 smp_mb();
248 set_bit(NFS_LSEG_VALID, &lseg->pls_flags);
Fred Isaman566052c2011-01-06 11:36:20 +0000249 lseg->pls_layout = lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400250}
251
Fred Isaman4541d162011-01-06 11:36:23 +0000252static void free_lseg(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400253{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000254 struct inode *ino = lseg->pls_layout->plh_inode;
Andy Adamson974cec82010-10-20 00:18:02 -0400255
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400256 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
Fred Isaman52fabd72011-01-06 11:36:18 +0000257 /* Matched by get_layout_hdr in pnfs_insert_layout */
Fred Isamancc6e5342011-01-06 11:36:28 +0000258 put_layout_hdr(NFS_I(ino)->layout);
Andy Adamson974cec82010-10-20 00:18:02 -0400259}
260
Fred Isamand684d2a2011-03-01 01:34:13 +0000261static void
262put_lseg_common(struct pnfs_layout_segment *lseg)
Andy Adamson974cec82010-10-20 00:18:02 -0400263{
Fred Isamand684d2a2011-03-01 01:34:13 +0000264 struct inode *inode = lseg->pls_layout->plh_inode;
265
Benny Halevyd20581a2011-05-22 19:52:03 +0300266 WARN_ON(test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Fred Isamand684d2a2011-03-01 01:34:13 +0000267 list_del_init(&lseg->pls_list);
268 if (list_empty(&lseg->pls_layout->plh_segs)) {
269 set_bit(NFS_LAYOUT_DESTROYED, &lseg->pls_layout->plh_flags);
270 /* Matched by initial refcount set in alloc_init_layout_hdr */
271 put_layout_hdr_locked(lseg->pls_layout);
272 }
273 rpc_wake_up(&NFS_SERVER(inode)->roc_rpcwaitq);
274}
275
Fred Isamanbae724e2011-03-01 01:34:15 +0000276void
Fred Isamand684d2a2011-03-01 01:34:13 +0000277put_lseg(struct pnfs_layout_segment *lseg)
278{
279 struct inode *inode;
280
281 if (!lseg)
282 return;
283
Fred Isaman4541d162011-01-06 11:36:23 +0000284 dprintk("%s: lseg %p ref %d valid %d\n", __func__, lseg,
285 atomic_read(&lseg->pls_refcount),
286 test_bit(NFS_LSEG_VALID, &lseg->pls_flags));
Fred Isamand684d2a2011-03-01 01:34:13 +0000287 inode = lseg->pls_layout->plh_inode;
288 if (atomic_dec_and_lock(&lseg->pls_refcount, &inode->i_lock)) {
289 LIST_HEAD(free_me);
Andy Adamson974cec82010-10-20 00:18:02 -0400290
Fred Isamand684d2a2011-03-01 01:34:13 +0000291 put_lseg_common(lseg);
292 list_add(&lseg->pls_list, &free_me);
293 spin_unlock(&inode->i_lock);
294 pnfs_free_lseg_list(&free_me);
Fred Isaman4541d162011-01-06 11:36:23 +0000295 }
Andy Adamson974cec82010-10-20 00:18:02 -0400296}
Fred Isamane0c2b382011-03-23 13:27:53 +0000297EXPORT_SYMBOL_GPL(put_lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400298
Benny Halevyfb3296e2011-05-22 19:47:26 +0300299static inline u64
300end_offset(u64 start, u64 len)
Fred Isaman4541d162011-01-06 11:36:23 +0000301{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300302 u64 end;
303
304 end = start + len;
305 return end >= start ? end : NFS4_MAX_UINT64;
306}
307
308/* last octet in a range */
309static inline u64
310last_byte_offset(u64 start, u64 len)
311{
312 u64 end;
313
314 BUG_ON(!len);
315 end = start + len;
316 return end > start ? end - 1 : NFS4_MAX_UINT64;
317}
318
319/*
320 * is l2 fully contained in l1?
321 * start1 end1
322 * [----------------------------------)
323 * start2 end2
324 * [----------------)
325 */
326static inline int
327lo_seg_contained(struct pnfs_layout_range *l1,
328 struct pnfs_layout_range *l2)
329{
330 u64 start1 = l1->offset;
331 u64 end1 = end_offset(start1, l1->length);
332 u64 start2 = l2->offset;
333 u64 end2 = end_offset(start2, l2->length);
334
335 return (start1 <= start2) && (end1 >= end2);
336}
337
338/*
339 * is l1 and l2 intersecting?
340 * start1 end1
341 * [----------------------------------)
342 * start2 end2
343 * [----------------)
344 */
345static inline int
346lo_seg_intersecting(struct pnfs_layout_range *l1,
347 struct pnfs_layout_range *l2)
348{
349 u64 start1 = l1->offset;
350 u64 end1 = end_offset(start1, l1->length);
351 u64 start2 = l2->offset;
352 u64 end2 = end_offset(start2, l2->length);
353
354 return (end1 == NFS4_MAX_UINT64 || end1 > start2) &&
355 (end2 == NFS4_MAX_UINT64 || end2 > start1);
356}
357
Fred Isaman4541d162011-01-06 11:36:23 +0000358static bool
Benny Halevy778b5502011-05-22 19:48:02 +0300359should_free_lseg(struct pnfs_layout_range *lseg_range,
360 struct pnfs_layout_range *recall_range)
Fred Isaman4541d162011-01-06 11:36:23 +0000361{
Benny Halevy778b5502011-05-22 19:48:02 +0300362 return (recall_range->iomode == IOMODE_ANY ||
363 lseg_range->iomode == recall_range->iomode) &&
364 lo_seg_intersecting(lseg_range, recall_range);
Fred Isaman4541d162011-01-06 11:36:23 +0000365}
366
367/* Returns 1 if lseg is removed from list, 0 otherwise */
368static int mark_lseg_invalid(struct pnfs_layout_segment *lseg,
369 struct list_head *tmp_list)
370{
371 int rv = 0;
372
373 if (test_and_clear_bit(NFS_LSEG_VALID, &lseg->pls_flags)) {
374 /* Remove the reference keeping the lseg in the
375 * list. It will now be removed when all
376 * outstanding io is finished.
377 */
Fred Isamand684d2a2011-03-01 01:34:13 +0000378 dprintk("%s: lseg %p ref %d\n", __func__, lseg,
379 atomic_read(&lseg->pls_refcount));
380 if (atomic_dec_and_test(&lseg->pls_refcount)) {
381 put_lseg_common(lseg);
382 list_add(&lseg->pls_list, tmp_list);
383 rv = 1;
384 }
Fred Isaman4541d162011-01-06 11:36:23 +0000385 }
386 return rv;
387}
388
389/* Returns count of number of matching invalid lsegs remaining in list
390 * after call.
391 */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000392int
Fred Isaman4541d162011-01-06 11:36:23 +0000393mark_matching_lsegs_invalid(struct pnfs_layout_hdr *lo,
394 struct list_head *tmp_list,
Benny Halevy778b5502011-05-22 19:48:02 +0300395 struct pnfs_layout_range *recall_range)
Andy Adamson974cec82010-10-20 00:18:02 -0400396{
397 struct pnfs_layout_segment *lseg, *next;
Fred Isaman4541d162011-01-06 11:36:23 +0000398 int invalid = 0, removed = 0;
Andy Adamson974cec82010-10-20 00:18:02 -0400399
400 dprintk("%s:Begin lo %p\n", __func__, lo);
401
Fred Isaman38511722011-02-03 18:28:50 +0000402 if (list_empty(&lo->plh_segs)) {
Andy Adamson2701d082012-05-24 13:13:24 -0400403 /* Reset MDS Threshold I/O counters */
404 NFS_I(lo->plh_inode)->write_io = 0;
405 NFS_I(lo->plh_inode)->read_io = 0;
Fred Isaman38511722011-02-03 18:28:50 +0000406 if (!test_and_set_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags))
407 put_layout_hdr_locked(lo);
408 return 0;
409 }
Fred Isaman4541d162011-01-06 11:36:23 +0000410 list_for_each_entry_safe(lseg, next, &lo->plh_segs, pls_list)
Benny Halevy778b5502011-05-22 19:48:02 +0300411 if (!recall_range ||
412 should_free_lseg(&lseg->pls_range, recall_range)) {
Fred Isaman4541d162011-01-06 11:36:23 +0000413 dprintk("%s: freeing lseg %p iomode %d "
414 "offset %llu length %llu\n", __func__,
415 lseg, lseg->pls_range.iomode, lseg->pls_range.offset,
416 lseg->pls_range.length);
417 invalid++;
418 removed += mark_lseg_invalid(lseg, tmp_list);
419 }
420 dprintk("%s:Return %i\n", __func__, invalid - removed);
421 return invalid - removed;
Andy Adamson974cec82010-10-20 00:18:02 -0400422}
423
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000424/* note free_me must contain lsegs from a single layout_hdr */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000425void
Fred Isaman4541d162011-01-06 11:36:23 +0000426pnfs_free_lseg_list(struct list_head *free_me)
Andy Adamson974cec82010-10-20 00:18:02 -0400427{
Fred Isaman4541d162011-01-06 11:36:23 +0000428 struct pnfs_layout_segment *lseg, *tmp;
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000429 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400430
Fred Isamanf49f9ba2011-02-03 18:28:52 +0000431 if (list_empty(free_me))
432 return;
433
434 lo = list_first_entry(free_me, struct pnfs_layout_segment,
435 pls_list)->pls_layout;
436
437 if (test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags)) {
438 struct nfs_client *clp;
439
440 clp = NFS_SERVER(lo->plh_inode)->nfs_client;
441 spin_lock(&clp->cl_lock);
442 list_del_init(&lo->plh_layouts);
443 spin_unlock(&clp->cl_lock);
444 }
Fred Isaman4541d162011-01-06 11:36:23 +0000445 list_for_each_entry_safe(lseg, tmp, free_me, pls_list) {
Fred Isaman566052c2011-01-06 11:36:20 +0000446 list_del(&lseg->pls_list);
Fred Isaman4541d162011-01-06 11:36:23 +0000447 free_lseg(lseg);
Andy Adamson974cec82010-10-20 00:18:02 -0400448 }
449}
450
Benny Halevye5e94012010-10-20 00:18:01 -0400451void
452pnfs_destroy_layout(struct nfs_inode *nfsi)
453{
454 struct pnfs_layout_hdr *lo;
Andy Adamson974cec82010-10-20 00:18:02 -0400455 LIST_HEAD(tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400456
457 spin_lock(&nfsi->vfs_inode.i_lock);
458 lo = nfsi->layout;
459 if (lo) {
Fred Isaman38511722011-02-03 18:28:50 +0000460 lo->plh_block_lgets++; /* permanently block new LAYOUTGETs */
Benny Halevy778b5502011-05-22 19:48:02 +0300461 mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
Benny Halevye5e94012010-10-20 00:18:01 -0400462 }
463 spin_unlock(&nfsi->vfs_inode.i_lock);
Andy Adamson974cec82010-10-20 00:18:02 -0400464 pnfs_free_lseg_list(&tmp_list);
Benny Halevye5e94012010-10-20 00:18:01 -0400465}
Andy Adamson041245c2012-04-27 17:53:53 -0400466EXPORT_SYMBOL_GPL(pnfs_destroy_layout);
Benny Halevye5e94012010-10-20 00:18:01 -0400467
Andy Adamson974cec82010-10-20 00:18:02 -0400468/*
469 * Called by the state manger to remove all layouts established under an
470 * expired lease.
471 */
472void
473pnfs_destroy_all_layouts(struct nfs_client *clp)
474{
Weston Andros Adamson6382a442011-06-01 16:44:44 -0400475 struct nfs_server *server;
Andy Adamson974cec82010-10-20 00:18:02 -0400476 struct pnfs_layout_hdr *lo;
477 LIST_HEAD(tmp_list);
478
Andy Adamsonc47abcf2011-06-15 17:52:40 -0400479 nfs4_deviceid_mark_client_invalid(clp);
480 nfs4_deviceid_purge_client(clp);
481
Andy Adamson974cec82010-10-20 00:18:02 -0400482 spin_lock(&clp->cl_lock);
Weston Andros Adamson6382a442011-06-01 16:44:44 -0400483 rcu_read_lock();
484 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
485 if (!list_empty(&server->layouts))
486 list_splice_init(&server->layouts, &tmp_list);
487 }
488 rcu_read_unlock();
Andy Adamson974cec82010-10-20 00:18:02 -0400489 spin_unlock(&clp->cl_lock);
490
491 while (!list_empty(&tmp_list)) {
492 lo = list_entry(tmp_list.next, struct pnfs_layout_hdr,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000493 plh_layouts);
Andy Adamson974cec82010-10-20 00:18:02 -0400494 dprintk("%s freeing layout for inode %lu\n", __func__,
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000495 lo->plh_inode->i_ino);
Andy Adamson2887fe42011-05-11 01:19:58 -0400496 list_del_init(&lo->plh_layouts);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000497 pnfs_destroy_layout(NFS_I(lo->plh_inode));
Andy Adamson974cec82010-10-20 00:18:02 -0400498 }
499}
500
Fred Isamanfd6002e2011-01-06 11:36:22 +0000501/* update lo->plh_stateid with new if is more recent */
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000502void
503pnfs_set_layout_stateid(struct pnfs_layout_hdr *lo, const nfs4_stateid *new,
504 bool update_barrier)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400505{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000506 u32 oldseq, newseq;
Andy Adamson974cec82010-10-20 00:18:02 -0400507
Trond Myklebust2d2f24a2012-03-04 18:13:57 -0500508 oldseq = be32_to_cpu(lo->plh_stateid.seqid);
509 newseq = be32_to_cpu(new->seqid);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000510 if ((int)(newseq - oldseq) > 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500511 nfs4_stateid_copy(&lo->plh_stateid, new);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000512 if (update_barrier) {
Trond Myklebust2d2f24a2012-03-04 18:13:57 -0500513 u32 new_barrier = be32_to_cpu(new->seqid);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000514
515 if ((int)(new_barrier - lo->plh_barrier))
516 lo->plh_barrier = new_barrier;
517 } else {
518 /* Because of wraparound, we want to keep the barrier
519 * "close" to the current seqids. It needs to be
520 * within 2**31 to count as "behind", so if it
521 * gets too near that limit, give us a litle leeway
522 * and bring it to within 2**30.
523 * NOTE - and yes, this is all unsigned arithmetic.
524 */
525 if (unlikely((newseq - lo->plh_barrier) > (3 << 29)))
526 lo->plh_barrier = newseq - (1 << 30);
527 }
528 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400529}
530
Fred Isamancf7d63f2011-01-06 11:36:25 +0000531/* lget is set to 1 if called from inside send_layoutget call chain */
532static bool
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000533pnfs_layoutgets_blocked(struct pnfs_layout_hdr *lo, nfs4_stateid *stateid,
534 int lget)
Fred Isamancf7d63f2011-01-06 11:36:25 +0000535{
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000536 if ((stateid) &&
Trond Myklebust2d2f24a2012-03-04 18:13:57 -0500537 (int)(lo->plh_barrier - be32_to_cpu(stateid->seqid)) >= 0)
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000538 return true;
Fred Isamanf7e89172011-01-06 11:36:32 +0000539 return lo->plh_block_lgets ||
Fred Isaman38511722011-02-03 18:28:50 +0000540 test_bit(NFS_LAYOUT_DESTROYED, &lo->plh_flags) ||
Fred Isamanf7e89172011-01-06 11:36:32 +0000541 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags) ||
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000542 (list_empty(&lo->plh_segs) &&
Fred Isamancf7d63f2011-01-06 11:36:25 +0000543 (atomic_read(&lo->plh_outstanding) > lget));
544}
545
Fred Isamanfd6002e2011-01-06 11:36:22 +0000546int
547pnfs_choose_layoutget_stateid(nfs4_stateid *dst, struct pnfs_layout_hdr *lo,
548 struct nfs4_state *open_state)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400549{
Fred Isamanfd6002e2011-01-06 11:36:22 +0000550 int status = 0;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400551
552 dprintk("--> %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000553 spin_lock(&lo->plh_inode->i_lock);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000554 if (pnfs_layoutgets_blocked(lo, NULL, 1)) {
Fred Isamancf7d63f2011-01-06 11:36:25 +0000555 status = -EAGAIN;
556 } else if (list_empty(&lo->plh_segs)) {
Fred Isamanfd6002e2011-01-06 11:36:22 +0000557 int seq;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400558
Fred Isamanfd6002e2011-01-06 11:36:22 +0000559 do {
560 seq = read_seqbegin(&open_state->seqlock);
Trond Myklebustf597c532012-03-04 18:13:56 -0500561 nfs4_stateid_copy(dst, &open_state->stateid);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000562 } while (read_seqretry(&open_state->seqlock, seq));
563 } else
Trond Myklebustf597c532012-03-04 18:13:56 -0500564 nfs4_stateid_copy(dst, &lo->plh_stateid);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000565 spin_unlock(&lo->plh_inode->i_lock);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400566 dprintk("<-- %s\n", __func__);
Fred Isamanfd6002e2011-01-06 11:36:22 +0000567 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400568}
569
570/*
571* Get layout from server.
572* for now, assume that whole file layouts are requested.
573* arg->offset: 0
574* arg->length: all ones
575*/
Benny Halevye5e94012010-10-20 00:18:01 -0400576static struct pnfs_layout_segment *
577send_layoutget(struct pnfs_layout_hdr *lo,
578 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +0300579 struct pnfs_layout_range *range,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400580 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400581{
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000582 struct inode *ino = lo->plh_inode;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400583 struct nfs_server *server = NFS_SERVER(ino);
584 struct nfs4_layoutget *lgp;
585 struct pnfs_layout_segment *lseg = NULL;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400586 struct page **pages = NULL;
587 int i;
588 u32 max_resp_sz, max_pages;
Benny Halevye5e94012010-10-20 00:18:01 -0400589
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400590 dprintk("--> %s\n", __func__);
591
592 BUG_ON(ctx == NULL);
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400593 lgp = kzalloc(sizeof(*lgp), gfp_flags);
Fred Isamancf7d63f2011-01-06 11:36:25 +0000594 if (lgp == NULL)
Andy Adamson974cec82010-10-20 00:18:02 -0400595 return NULL;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400596
597 /* allocate pages for xdr post processing */
598 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
Andy Adamsone5265a02012-04-14 03:56:35 -0400599 max_pages = nfs_page_array_len(0, max_resp_sz);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400600
Trond Myklebust7d9dea92012-01-20 18:57:02 -0500601 pages = kcalloc(max_pages, sizeof(struct page *), gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400602 if (!pages)
603 goto out_err_free;
604
605 for (i = 0; i < max_pages; i++) {
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400606 pages[i] = alloc_page(gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400607 if (!pages[i])
608 goto out_err_free;
609 }
610
Benny Halevyfb3296e2011-05-22 19:47:26 +0300611 lgp->args.minlength = PAGE_CACHE_SIZE;
612 if (lgp->args.minlength > range->length)
613 lgp->args.minlength = range->length;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400614 lgp->args.maxcount = PNFS_LAYOUT_MAXSIZE;
Benny Halevyfb3296e2011-05-22 19:47:26 +0300615 lgp->args.range = *range;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400616 lgp->args.type = server->pnfs_curr_ld->id;
617 lgp->args.inode = ino;
618 lgp->args.ctx = get_nfs_open_context(ctx);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400619 lgp->args.layout.pages = pages;
620 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400621 lgp->lsegpp = &lseg;
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400622 lgp->gfp_flags = gfp_flags;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400623
624 /* Synchronously retrieve layout information from server and
625 * store in lseg.
626 */
627 nfs4_proc_layoutget(lgp);
628 if (!lseg) {
629 /* remember that LAYOUTGET failed and suspend trying */
Benny Halevyfb3296e2011-05-22 19:47:26 +0300630 set_bit(lo_fail_bit(range->iomode), &lo->plh_flags);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400631 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400632
633 /* free xdr pages */
634 for (i = 0; i < max_pages; i++)
635 __free_page(pages[i]);
636 kfree(pages);
637
Andy Adamson974cec82010-10-20 00:18:02 -0400638 return lseg;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400639
640out_err_free:
641 /* free any allocated xdr pages, lgp as it's not used */
642 if (pages) {
643 for (i = 0; i < max_pages; i++) {
644 if (!pages[i])
645 break;
646 __free_page(pages[i]);
647 }
648 kfree(pages);
649 }
650 kfree(lgp);
651 return NULL;
Andy Adamson974cec82010-10-20 00:18:02 -0400652}
653
Benny Halevycbe82602011-05-22 19:52:37 +0300654/* Initiates a LAYOUTRETURN(FILE) */
655int
656_pnfs_return_layout(struct inode *ino)
657{
658 struct pnfs_layout_hdr *lo = NULL;
659 struct nfs_inode *nfsi = NFS_I(ino);
660 LIST_HEAD(tmp_list);
661 struct nfs4_layoutreturn *lrp;
662 nfs4_stateid stateid;
663 int status = 0;
664
Andy Adamson366d5052012-06-20 15:03:33 -0400665 dprintk("NFS: %s for inode %lu\n", __func__, ino->i_ino);
Benny Halevycbe82602011-05-22 19:52:37 +0300666
667 spin_lock(&ino->i_lock);
668 lo = nfsi->layout;
Andy Adamson366d5052012-06-20 15:03:33 -0400669 if (!lo || pnfs_test_layout_returned(lo)) {
Benny Halevycbe82602011-05-22 19:52:37 +0300670 spin_unlock(&ino->i_lock);
Fred Isamana2e1d4f2011-06-13 18:54:53 -0400671 dprintk("%s: no layout to return\n", __func__);
672 return status;
Benny Halevycbe82602011-05-22 19:52:37 +0300673 }
674 stateid = nfsi->layout->plh_stateid;
675 /* Reference matched in nfs4_layoutreturn_release */
676 get_layout_hdr(lo);
Fred Isamanea0ded72011-06-15 12:31:02 -0400677 mark_matching_lsegs_invalid(lo, &tmp_list, NULL);
678 lo->plh_block_lgets++;
Andy Adamson366d5052012-06-20 15:03:33 -0400679 pnfs_mark_layout_returned(lo);
Benny Halevycbe82602011-05-22 19:52:37 +0300680 spin_unlock(&ino->i_lock);
681 pnfs_free_lseg_list(&tmp_list);
682
683 WARN_ON(test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags));
684
685 lrp = kzalloc(sizeof(*lrp), GFP_KERNEL);
686 if (unlikely(lrp == NULL)) {
687 status = -ENOMEM;
Fred Isaman9e2dfdb2011-06-15 14:32:02 -0400688 set_bit(NFS_LAYOUT_RW_FAILED, &lo->plh_flags);
689 set_bit(NFS_LAYOUT_RO_FAILED, &lo->plh_flags);
Andy Adamson366d5052012-06-20 15:03:33 -0400690 pnfs_clear_layout_returned(lo);
Benny Halevy1ed3a852011-06-15 11:39:57 -0400691 put_layout_hdr(lo);
Benny Halevycbe82602011-05-22 19:52:37 +0300692 goto out;
693 }
694
695 lrp->args.stateid = stateid;
696 lrp->args.layout_type = NFS_SERVER(ino)->pnfs_curr_ld->id;
697 lrp->args.inode = ino;
Trond Myklebusta56aaa02011-06-15 11:59:10 -0400698 lrp->args.layout = lo;
Benny Halevycbe82602011-05-22 19:52:37 +0300699 lrp->clp = NFS_SERVER(ino)->nfs_client;
700
701 status = nfs4_proc_layoutreturn(lrp);
702out:
703 dprintk("<-- %s status: %d\n", __func__, status);
704 return status;
705}
Andy Adamson0a57cda2012-04-27 17:53:50 -0400706EXPORT_SYMBOL_GPL(_pnfs_return_layout);
Benny Halevycbe82602011-05-22 19:52:37 +0300707
Fred Isamanf7e89172011-01-06 11:36:32 +0000708bool pnfs_roc(struct inode *ino)
709{
710 struct pnfs_layout_hdr *lo;
711 struct pnfs_layout_segment *lseg, *tmp;
712 LIST_HEAD(tmp_list);
713 bool found = false;
714
715 spin_lock(&ino->i_lock);
716 lo = NFS_I(ino)->layout;
717 if (!lo || !test_and_clear_bit(NFS_LAYOUT_ROC, &lo->plh_flags) ||
718 test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags))
719 goto out_nolayout;
720 list_for_each_entry_safe(lseg, tmp, &lo->plh_segs, pls_list)
721 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
722 mark_lseg_invalid(lseg, &tmp_list);
723 found = true;
724 }
725 if (!found)
726 goto out_nolayout;
727 lo->plh_block_lgets++;
728 get_layout_hdr(lo); /* matched in pnfs_roc_release */
729 spin_unlock(&ino->i_lock);
730 pnfs_free_lseg_list(&tmp_list);
731 return true;
732
733out_nolayout:
734 spin_unlock(&ino->i_lock);
735 return false;
736}
737
738void pnfs_roc_release(struct inode *ino)
739{
740 struct pnfs_layout_hdr *lo;
741
742 spin_lock(&ino->i_lock);
743 lo = NFS_I(ino)->layout;
744 lo->plh_block_lgets--;
745 put_layout_hdr_locked(lo);
746 spin_unlock(&ino->i_lock);
747}
748
749void pnfs_roc_set_barrier(struct inode *ino, u32 barrier)
750{
751 struct pnfs_layout_hdr *lo;
752
753 spin_lock(&ino->i_lock);
754 lo = NFS_I(ino)->layout;
755 if ((int)(barrier - lo->plh_barrier) > 0)
756 lo->plh_barrier = barrier;
757 spin_unlock(&ino->i_lock);
758}
759
760bool pnfs_roc_drain(struct inode *ino, u32 *barrier)
761{
762 struct nfs_inode *nfsi = NFS_I(ino);
763 struct pnfs_layout_segment *lseg;
764 bool found = false;
765
766 spin_lock(&ino->i_lock);
767 list_for_each_entry(lseg, &nfsi->layout->plh_segs, pls_list)
768 if (test_bit(NFS_LSEG_ROC, &lseg->pls_flags)) {
769 found = true;
770 break;
771 }
772 if (!found) {
773 struct pnfs_layout_hdr *lo = nfsi->layout;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -0500774 u32 current_seqid = be32_to_cpu(lo->plh_stateid.seqid);
Fred Isamanf7e89172011-01-06 11:36:32 +0000775
776 /* Since close does not return a layout stateid for use as
777 * a barrier, we choose the worst-case barrier.
778 */
779 *barrier = current_seqid + atomic_read(&lo->plh_outstanding);
780 }
781 spin_unlock(&ino->i_lock);
782 return found;
783}
784
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400785/*
786 * Compare two layout segments for sorting into layout cache.
787 * We want to preferentially return RW over RO layouts, so ensure those
788 * are seen first.
789 */
790static s64
Benny Halevyfb3296e2011-05-22 19:47:26 +0300791cmp_layout(struct pnfs_layout_range *l1,
792 struct pnfs_layout_range *l2)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400793{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300794 s64 d;
795
796 /* high offset > low offset */
797 d = l1->offset - l2->offset;
798 if (d)
799 return d;
800
801 /* short length > long length */
802 d = l2->length - l1->length;
803 if (d)
804 return d;
805
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400806 /* read > read/write */
Benny Halevyfb3296e2011-05-22 19:47:26 +0300807 return (int)(l1->iomode == IOMODE_READ) - (int)(l2->iomode == IOMODE_READ);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400808}
809
Andy Adamson974cec82010-10-20 00:18:02 -0400810static void
811pnfs_insert_layout(struct pnfs_layout_hdr *lo,
812 struct pnfs_layout_segment *lseg)
813{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400814 struct pnfs_layout_segment *lp;
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400815
Andy Adamson974cec82010-10-20 00:18:02 -0400816 dprintk("%s:Begin\n", __func__);
817
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000818 assert_spin_locked(&lo->plh_inode->i_lock);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000819 list_for_each_entry(lp, &lo->plh_segs, pls_list) {
Benny Halevyfb3296e2011-05-22 19:47:26 +0300820 if (cmp_layout(&lseg->pls_range, &lp->pls_range) > 0)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400821 continue;
Fred Isaman566052c2011-01-06 11:36:20 +0000822 list_add_tail(&lseg->pls_list, &lp->pls_list);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400823 dprintk("%s: inserted lseg %p "
824 "iomode %d offset %llu length %llu before "
825 "lp %p iomode %d offset %llu length %llu\n",
Fred Isaman566052c2011-01-06 11:36:20 +0000826 __func__, lseg, lseg->pls_range.iomode,
827 lseg->pls_range.offset, lseg->pls_range.length,
828 lp, lp->pls_range.iomode, lp->pls_range.offset,
829 lp->pls_range.length);
Benny Halevyfb3296e2011-05-22 19:47:26 +0300830 goto out;
Andy Adamson974cec82010-10-20 00:18:02 -0400831 }
Benny Halevyfb3296e2011-05-22 19:47:26 +0300832 list_add_tail(&lseg->pls_list, &lo->plh_segs);
833 dprintk("%s: inserted lseg %p "
834 "iomode %d offset %llu length %llu at tail\n",
835 __func__, lseg, lseg->pls_range.iomode,
836 lseg->pls_range.offset, lseg->pls_range.length);
837out:
Fred Isamancc6e5342011-01-06 11:36:28 +0000838 get_layout_hdr(lo);
Andy Adamson974cec82010-10-20 00:18:02 -0400839
840 dprintk("%s:Return\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -0400841}
842
843static struct pnfs_layout_hdr *
Peng Tao9fa40752011-07-30 20:52:32 -0400844alloc_init_layout_hdr(struct inode *ino,
845 struct nfs_open_context *ctx,
846 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400847{
848 struct pnfs_layout_hdr *lo;
849
Benny Halevy636fb9c2011-05-22 19:51:33 +0300850 lo = pnfs_alloc_layout_hdr(ino, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400851 if (!lo)
852 return NULL;
Fred Isamancc6e5342011-01-06 11:36:28 +0000853 atomic_set(&lo->plh_refcount, 1);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000854 INIT_LIST_HEAD(&lo->plh_layouts);
855 INIT_LIST_HEAD(&lo->plh_segs);
Fred Isaman43f1b3d2011-01-06 11:36:30 +0000856 INIT_LIST_HEAD(&lo->plh_bulk_recall);
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000857 lo->plh_inode = ino;
Peng Tao9fa40752011-07-30 20:52:32 -0400858 lo->plh_lc_cred = get_rpccred(ctx->state->owner->so_cred);
Benny Halevye5e94012010-10-20 00:18:01 -0400859 return lo;
860}
861
862static struct pnfs_layout_hdr *
Peng Tao9fa40752011-07-30 20:52:32 -0400863pnfs_find_alloc_layout(struct inode *ino,
864 struct nfs_open_context *ctx,
865 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -0400866{
867 struct nfs_inode *nfsi = NFS_I(ino);
868 struct pnfs_layout_hdr *new = NULL;
869
870 dprintk("%s Begin ino=%p layout=%p\n", __func__, ino, nfsi->layout);
871
872 assert_spin_locked(&ino->i_lock);
Fred Isaman4541d162011-01-06 11:36:23 +0000873 if (nfsi->layout) {
874 if (test_bit(NFS_LAYOUT_DESTROYED, &nfsi->layout->plh_flags))
875 return NULL;
876 else
877 return nfsi->layout;
878 }
Benny Halevye5e94012010-10-20 00:18:01 -0400879 spin_unlock(&ino->i_lock);
Peng Tao9fa40752011-07-30 20:52:32 -0400880 new = alloc_init_layout_hdr(ino, ctx, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -0400881 spin_lock(&ino->i_lock);
882
883 if (likely(nfsi->layout == NULL)) /* Won the race? */
884 nfsi->layout = new;
885 else
Benny Halevy636fb9c2011-05-22 19:51:33 +0300886 pnfs_free_layout_hdr(new);
Benny Halevye5e94012010-10-20 00:18:01 -0400887 return nfsi->layout;
888}
889
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400890/*
891 * iomode matching rules:
892 * iomode lseg match
893 * ----- ----- -----
894 * ANY READ true
895 * ANY RW true
896 * RW READ false
897 * RW RW true
898 * READ READ true
899 * READ RW true
900 */
901static int
Benny Halevyfb3296e2011-05-22 19:47:26 +0300902is_matching_lseg(struct pnfs_layout_range *ls_range,
903 struct pnfs_layout_range *range)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400904{
Benny Halevyfb3296e2011-05-22 19:47:26 +0300905 struct pnfs_layout_range range1;
906
907 if ((range->iomode == IOMODE_RW &&
908 ls_range->iomode != IOMODE_RW) ||
909 !lo_seg_intersecting(ls_range, range))
910 return 0;
911
912 /* range1 covers only the first byte in the range */
913 range1 = *range;
914 range1.length = 1;
915 return lo_seg_contained(ls_range, &range1);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400916}
917
918/*
919 * lookup range in layout
920 */
Benny Halevye5e94012010-10-20 00:18:01 -0400921static struct pnfs_layout_segment *
Benny Halevyfb3296e2011-05-22 19:47:26 +0300922pnfs_find_lseg(struct pnfs_layout_hdr *lo,
923 struct pnfs_layout_range *range)
Benny Halevye5e94012010-10-20 00:18:01 -0400924{
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400925 struct pnfs_layout_segment *lseg, *ret = NULL;
926
927 dprintk("%s:Begin\n", __func__);
928
Fred Isamanb7edfaa2011-01-06 11:36:21 +0000929 assert_spin_locked(&lo->plh_inode->i_lock);
930 list_for_each_entry(lseg, &lo->plh_segs, pls_list) {
Fred Isaman4541d162011-01-06 11:36:23 +0000931 if (test_bit(NFS_LSEG_VALID, &lseg->pls_flags) &&
Benny Halevyfb3296e2011-05-22 19:47:26 +0300932 is_matching_lseg(&lseg->pls_range, range)) {
Fred Isamand684d2a2011-03-01 01:34:13 +0000933 ret = get_lseg(lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400934 break;
935 }
Benny Halevyd771e3a2011-06-14 16:30:16 -0400936 if (lseg->pls_range.offset > range->offset)
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400937 break;
938 }
939
940 dprintk("%s:Return lseg %p ref %d\n",
Fred Isaman4541d162011-01-06 11:36:23 +0000941 __func__, ret, ret ? atomic_read(&ret->pls_refcount) : 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -0400942 return ret;
Benny Halevye5e94012010-10-20 00:18:01 -0400943}
944
945/*
Andy Adamsond23d61c2012-05-23 05:02:37 -0400946 * Use mdsthreshold hints set at each OPEN to determine if I/O should go
947 * to the MDS or over pNFS
948 *
949 * The nfs_inode read_io and write_io fields are cumulative counters reset
950 * when there are no layout segments. Note that in pnfs_update_layout iomode
951 * is set to IOMODE_READ for a READ request, and set to IOMODE_RW for a
952 * WRITE request.
953 *
954 * A return of true means use MDS I/O.
955 *
956 * From rfc 5661:
957 * If a file's size is smaller than the file size threshold, data accesses
958 * SHOULD be sent to the metadata server. If an I/O request has a length that
959 * is below the I/O size threshold, the I/O SHOULD be sent to the metadata
960 * server. If both file size and I/O size are provided, the client SHOULD
961 * reach or exceed both thresholds before sending its read or write
962 * requests to the data server.
963 */
964static bool pnfs_within_mdsthreshold(struct nfs_open_context *ctx,
965 struct inode *ino, int iomode)
966{
967 struct nfs4_threshold *t = ctx->mdsthreshold;
968 struct nfs_inode *nfsi = NFS_I(ino);
969 loff_t fsize = i_size_read(ino);
970 bool size = false, size_set = false, io = false, io_set = false, ret = false;
971
972 if (t == NULL)
973 return ret;
974
975 dprintk("%s bm=0x%x rd_sz=%llu wr_sz=%llu rd_io=%llu wr_io=%llu\n",
976 __func__, t->bm, t->rd_sz, t->wr_sz, t->rd_io_sz, t->wr_io_sz);
977
978 switch (iomode) {
979 case IOMODE_READ:
980 if (t->bm & THRESHOLD_RD) {
981 dprintk("%s fsize %llu\n", __func__, fsize);
982 size_set = true;
983 if (fsize < t->rd_sz)
984 size = true;
985 }
986 if (t->bm & THRESHOLD_RD_IO) {
987 dprintk("%s nfsi->read_io %llu\n", __func__,
988 nfsi->read_io);
989 io_set = true;
990 if (nfsi->read_io < t->rd_io_sz)
991 io = true;
992 }
993 break;
994 case IOMODE_RW:
995 if (t->bm & THRESHOLD_WR) {
996 dprintk("%s fsize %llu\n", __func__, fsize);
997 size_set = true;
998 if (fsize < t->wr_sz)
999 size = true;
1000 }
1001 if (t->bm & THRESHOLD_WR_IO) {
1002 dprintk("%s nfsi->write_io %llu\n", __func__,
1003 nfsi->write_io);
1004 io_set = true;
1005 if (nfsi->write_io < t->wr_io_sz)
1006 io = true;
1007 }
1008 break;
1009 }
1010 if (size_set && io_set) {
1011 if (size && io)
1012 ret = true;
1013 } else if (size || io)
1014 ret = true;
1015
1016 dprintk("<-- %s size %d io %d ret %d\n", __func__, size, io, ret);
1017 return ret;
1018}
1019
1020/*
Benny Halevye5e94012010-10-20 00:18:01 -04001021 * Layout segment is retreived from the server if not cached.
1022 * The appropriate layout segment is referenced and returned to the caller.
1023 */
Andy Adamson7c24d942011-06-13 18:22:38 -04001024struct pnfs_layout_segment *
Benny Halevye5e94012010-10-20 00:18:01 -04001025pnfs_update_layout(struct inode *ino,
1026 struct nfs_open_context *ctx,
Benny Halevyfb3296e2011-05-22 19:47:26 +03001027 loff_t pos,
1028 u64 count,
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001029 enum pnfs_iomode iomode,
1030 gfp_t gfp_flags)
Benny Halevye5e94012010-10-20 00:18:01 -04001031{
Benny Halevyfb3296e2011-05-22 19:47:26 +03001032 struct pnfs_layout_range arg = {
1033 .iomode = iomode,
1034 .offset = pos,
1035 .length = count,
1036 };
Benny Halevy707ed5f2011-05-22 19:47:46 +03001037 unsigned pg_offset;
Benny Halevye5e94012010-10-20 00:18:01 -04001038 struct nfs_inode *nfsi = NFS_I(ino);
Weston Andros Adamson6382a442011-06-01 16:44:44 -04001039 struct nfs_server *server = NFS_SERVER(ino);
1040 struct nfs_client *clp = server->nfs_client;
Benny Halevye5e94012010-10-20 00:18:01 -04001041 struct pnfs_layout_hdr *lo;
1042 struct pnfs_layout_segment *lseg = NULL;
Fred Isamanf49f9ba2011-02-03 18:28:52 +00001043 bool first = false;
Benny Halevye5e94012010-10-20 00:18:01 -04001044
1045 if (!pnfs_enabled_sb(NFS_SERVER(ino)))
1046 return NULL;
Andy Adamsond23d61c2012-05-23 05:02:37 -04001047
1048 if (pnfs_within_mdsthreshold(ctx, ino, iomode))
1049 return NULL;
1050
Benny Halevye5e94012010-10-20 00:18:01 -04001051 spin_lock(&ino->i_lock);
Peng Tao9fa40752011-07-30 20:52:32 -04001052 lo = pnfs_find_alloc_layout(ino, ctx, gfp_flags);
Benny Halevye5e94012010-10-20 00:18:01 -04001053 if (lo == NULL) {
1054 dprintk("%s ERROR: can't get pnfs_layout_hdr\n", __func__);
1055 goto out_unlock;
1056 }
1057
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001058 /* Do we even need to bother with this? */
Trond Myklebusta59c30a2012-03-01 11:17:47 -05001059 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001060 dprintk("%s matches recall, use MDS\n", __func__);
Benny Halevye5e94012010-10-20 00:18:01 -04001061 goto out_unlock;
1062 }
1063
1064 /* if LAYOUTGET already failed once we don't try again */
Fred Isaman566052c2011-01-06 11:36:20 +00001065 if (test_bit(lo_fail_bit(iomode), &nfsi->layout->plh_flags))
Benny Halevye5e94012010-10-20 00:18:01 -04001066 goto out_unlock;
1067
Andy Adamson568e8c42011-03-01 01:34:22 +00001068 /* Check to see if the layout for the given range already exists */
Benny Halevyfb3296e2011-05-22 19:47:26 +03001069 lseg = pnfs_find_lseg(lo, &arg);
Andy Adamson568e8c42011-03-01 01:34:22 +00001070 if (lseg)
1071 goto out_unlock;
1072
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001073 if (pnfs_layoutgets_blocked(lo, NULL, 0))
Fred Isamancf7d63f2011-01-06 11:36:25 +00001074 goto out_unlock;
1075 atomic_inc(&lo->plh_outstanding);
1076
Fred Isamancc6e5342011-01-06 11:36:28 +00001077 get_layout_hdr(lo);
Fred Isamanf49f9ba2011-02-03 18:28:52 +00001078 if (list_empty(&lo->plh_segs))
1079 first = true;
Andy Adamson366d5052012-06-20 15:03:33 -04001080
1081 /* Enable LAYOUTRETURNs */
1082 pnfs_clear_layout_returned(lo);
1083
Fred Isamanf49f9ba2011-02-03 18:28:52 +00001084 spin_unlock(&ino->i_lock);
1085 if (first) {
Fred Isaman2130ff62011-01-06 11:36:26 +00001086 /* The lo must be on the clp list if there is any
1087 * chance of a CB_LAYOUTRECALL(FILE) coming in.
1088 */
1089 spin_lock(&clp->cl_lock);
1090 BUG_ON(!list_empty(&lo->plh_layouts));
Weston Andros Adamson6382a442011-06-01 16:44:44 -04001091 list_add_tail(&lo->plh_layouts, &server->layouts);
Fred Isaman2130ff62011-01-06 11:36:26 +00001092 spin_unlock(&clp->cl_lock);
1093 }
Benny Halevye5e94012010-10-20 00:18:01 -04001094
Benny Halevy707ed5f2011-05-22 19:47:46 +03001095 pg_offset = arg.offset & ~PAGE_CACHE_MASK;
1096 if (pg_offset) {
1097 arg.offset -= pg_offset;
1098 arg.length += pg_offset;
1099 }
Andy Adamson7c24d942011-06-13 18:22:38 -04001100 if (arg.length != NFS4_MAX_UINT64)
1101 arg.length = PAGE_CACHE_ALIGN(arg.length);
Benny Halevy707ed5f2011-05-22 19:47:46 +03001102
Benny Halevyfb3296e2011-05-22 19:47:26 +03001103 lseg = send_layoutget(lo, ctx, &arg, gfp_flags);
Fred Isamanf49f9ba2011-02-03 18:28:52 +00001104 if (!lseg && first) {
1105 spin_lock(&clp->cl_lock);
1106 list_del_init(&lo->plh_layouts);
1107 spin_unlock(&clp->cl_lock);
Fred Isaman2130ff62011-01-06 11:36:26 +00001108 }
Fred Isamancf7d63f2011-01-06 11:36:25 +00001109 atomic_dec(&lo->plh_outstanding);
Fred Isamancc6e5342011-01-06 11:36:28 +00001110 put_layout_hdr(lo);
Benny Halevye5e94012010-10-20 00:18:01 -04001111out:
1112 dprintk("%s end, state 0x%lx lseg %p\n", __func__,
Andy Adamsonbf9c1382011-03-01 01:34:07 +00001113 nfsi->layout ? nfsi->layout->plh_flags : -1, lseg);
Benny Halevye5e94012010-10-20 00:18:01 -04001114 return lseg;
1115out_unlock:
1116 spin_unlock(&ino->i_lock);
1117 goto out;
1118}
Andy Adamson7c24d942011-06-13 18:22:38 -04001119EXPORT_SYMBOL_GPL(pnfs_update_layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001120
1121int
1122pnfs_layout_process(struct nfs4_layoutget *lgp)
1123{
1124 struct pnfs_layout_hdr *lo = NFS_I(lgp->args.inode)->layout;
1125 struct nfs4_layoutget_res *res = &lgp->res;
1126 struct pnfs_layout_segment *lseg;
Fred Isamanb7edfaa2011-01-06 11:36:21 +00001127 struct inode *ino = lo->plh_inode;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001128 int status = 0;
1129
1130 /* Inject layout blob into I/O device driver */
Trond Myklebusta75b9df2011-05-11 18:00:51 -04001131 lseg = NFS_SERVER(ino)->pnfs_curr_ld->alloc_lseg(lo, res, lgp->gfp_flags);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001132 if (!lseg || IS_ERR(lseg)) {
1133 if (!lseg)
1134 status = -ENOMEM;
1135 else
1136 status = PTR_ERR(lseg);
1137 dprintk("%s: Could not allocate layout: error %d\n",
1138 __func__, status);
1139 goto out;
1140 }
1141
1142 spin_lock(&ino->i_lock);
Trond Myklebusta59c30a2012-03-01 11:17:47 -05001143 if (test_bit(NFS_LAYOUT_BULK_RECALL, &lo->plh_flags)) {
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001144 dprintk("%s forget reply due to recall\n", __func__);
1145 goto out_forget_reply;
1146 }
1147
1148 if (pnfs_layoutgets_blocked(lo, &res->stateid, 1)) {
1149 dprintk("%s forget reply due to state\n", __func__);
1150 goto out_forget_reply;
1151 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001152 init_lseg(lo, lseg);
Fred Isaman566052c2011-01-06 11:36:20 +00001153 lseg->pls_range = res->range;
Fred Isamand684d2a2011-03-01 01:34:13 +00001154 *lgp->lsegpp = get_lseg(lseg);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001155 pnfs_insert_layout(lo, lseg);
1156
Fred Isamanf7e89172011-01-06 11:36:32 +00001157 if (res->return_on_close) {
1158 set_bit(NFS_LSEG_ROC, &lseg->pls_flags);
1159 set_bit(NFS_LAYOUT_ROC, &lo->plh_flags);
1160 }
1161
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001162 /* Done processing layoutget. Set the layout stateid */
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001163 pnfs_set_layout_stateid(lo, &res->stateid, false);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001164 spin_unlock(&ino->i_lock);
1165out:
1166 return status;
Fred Isaman43f1b3d2011-01-06 11:36:30 +00001167
1168out_forget_reply:
1169 spin_unlock(&ino->i_lock);
1170 lseg->pls_layout = lo;
1171 NFS_SERVER(ino)->pnfs_curr_ld->free_lseg(lseg);
1172 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001173}
1174
Trond Myklebustd8007d42011-06-10 13:30:23 -04001175void
1176pnfs_generic_pg_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1177{
1178 BUG_ON(pgio->pg_lseg != NULL);
1179
Fred Isaman1825a0d2012-04-20 19:55:31 -04001180 if (req->wb_offset != req->wb_pgbase) {
1181 nfs_pageio_reset_read_mds(pgio);
1182 return;
1183 }
Trond Myklebustd8007d42011-06-10 13:30:23 -04001184 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1185 req->wb_context,
1186 req_offset(req),
1187 req->wb_bytes,
1188 IOMODE_READ,
1189 GFP_KERNEL);
Trond Myklebuste885de12011-06-10 13:30:23 -04001190 /* If no lseg, fall back to read through mds */
1191 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -04001192 nfs_pageio_reset_read_mds(pgio);
Trond Myklebuste885de12011-06-10 13:30:23 -04001193
Trond Myklebustd8007d42011-06-10 13:30:23 -04001194}
1195EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_read);
1196
1197void
1198pnfs_generic_pg_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
1199{
1200 BUG_ON(pgio->pg_lseg != NULL);
1201
Fred Isaman1825a0d2012-04-20 19:55:31 -04001202 if (req->wb_offset != req->wb_pgbase) {
1203 nfs_pageio_reset_write_mds(pgio);
1204 return;
1205 }
Trond Myklebustd8007d42011-06-10 13:30:23 -04001206 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
1207 req->wb_context,
1208 req_offset(req),
1209 req->wb_bytes,
1210 IOMODE_RW,
1211 GFP_NOFS);
Trond Myklebuste885de12011-06-10 13:30:23 -04001212 /* If no lseg, fall back to write through mds */
1213 if (pgio->pg_lseg == NULL)
Trond Myklebust1f945352011-07-13 15:59:57 -04001214 nfs_pageio_reset_write_mds(pgio);
Trond Myklebustd8007d42011-06-10 13:30:23 -04001215}
1216EXPORT_SYMBOL_GPL(pnfs_generic_pg_init_write);
1217
Bryan Schumaker1abb50882012-06-20 15:53:47 -04001218void
Fred Isaman061ae2e2012-04-20 14:47:48 -04001219pnfs_pageio_init_read(struct nfs_pageio_descriptor *pgio, struct inode *inode,
1220 const struct nfs_pgio_completion_ops *compl_ops)
Trond Myklebust1751c362011-06-10 13:30:23 -04001221{
1222 struct nfs_server *server = NFS_SERVER(inode);
1223 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
1224
1225 if (ld == NULL)
Bryan Schumaker1abb50882012-06-20 15:53:47 -04001226 nfs_pageio_init_read(pgio, inode, compl_ops);
1227 else
1228 nfs_pageio_init(pgio, inode, ld->pg_read_ops, compl_ops, server->rsize, 0);
Trond Myklebust1751c362011-06-10 13:30:23 -04001229}
1230
Bryan Schumaker57208fa2012-06-20 15:53:48 -04001231void
Fred Isaman061ae2e2012-04-20 14:47:48 -04001232pnfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, struct inode *inode,
1233 int ioflags,
1234 const struct nfs_pgio_completion_ops *compl_ops)
Trond Myklebust1751c362011-06-10 13:30:23 -04001235{
1236 struct nfs_server *server = NFS_SERVER(inode);
1237 struct pnfs_layoutdriver_type *ld = server->pnfs_curr_ld;
1238
1239 if (ld == NULL)
Bryan Schumaker57208fa2012-06-20 15:53:48 -04001240 nfs_pageio_init_write(pgio, inode, ioflags, compl_ops);
1241 else
1242 nfs_pageio_init(pgio, inode, ld->pg_write_ops, compl_ops, server->wsize, ioflags);
Trond Myklebust1751c362011-06-10 13:30:23 -04001243}
1244
1245bool
Benny Halevydfed2062011-05-25 20:25:22 +03001246pnfs_generic_pg_test(struct nfs_pageio_descriptor *pgio, struct nfs_page *prev,
1247 struct nfs_page *req)
Fred Isamanbae724e2011-03-01 01:34:15 +00001248{
Trond Myklebustd8007d42011-06-10 13:30:23 -04001249 if (pgio->pg_lseg == NULL)
1250 return nfs_generic_pg_test(pgio, prev, req);
Benny Halevy89a58e32011-05-25 20:54:40 +03001251
Trond Myklebust19982ba2011-06-10 13:30:23 -04001252 /*
1253 * Test if a nfs_page is fully contained in the pnfs_layout_range.
1254 * Note that this test makes several assumptions:
1255 * - that the previous nfs_page in the struct nfs_pageio_descriptor
1256 * is known to lie within the range.
1257 * - that the nfs_page being tested is known to be contiguous with the
1258 * previous nfs_page.
1259 * - Layout ranges are page aligned, so we only have to test the
1260 * start offset of the request.
1261 *
1262 * Please also note that 'end_offset' is actually the offset of the
1263 * first byte that lies outside the pnfs_layout_range. FIXME?
1264 *
1265 */
1266 return req_offset(req) < end_offset(pgio->pg_lseg->pls_range.offset,
1267 pgio->pg_lseg->pls_range.length);
Fred Isamanbae724e2011-03-01 01:34:15 +00001268}
Benny Halevy89a58e32011-05-25 20:54:40 +03001269EXPORT_SYMBOL_GPL(pnfs_generic_pg_test);
Fred Isamanbae724e2011-03-01 01:34:15 +00001270
Andy Adamsone7dd79af2012-04-27 17:53:46 -04001271int pnfs_write_done_resend_to_mds(struct inode *inode,
Fred Isaman061ae2e2012-04-20 14:47:48 -04001272 struct list_head *head,
1273 const struct nfs_pgio_completion_ops *compl_ops)
Trond Myklebuste2fecb22012-01-06 08:57:46 -05001274{
1275 struct nfs_pageio_descriptor pgio;
1276 LIST_HEAD(failed);
1277
1278 /* Resend all requests through the MDS */
Bryan Schumaker57208fa2012-06-20 15:53:48 -04001279 nfs_pageio_init_write(&pgio, inode, FLUSH_STABLE, compl_ops);
Trond Myklebuste2fecb22012-01-06 08:57:46 -05001280 while (!list_empty(head)) {
1281 struct nfs_page *req = nfs_list_entry(head->next);
1282
1283 nfs_list_remove_request(req);
1284 if (!nfs_pageio_add_request(&pgio, req))
1285 nfs_list_add_request(req, &failed);
1286 }
1287 nfs_pageio_complete(&pgio);
1288
1289 if (!list_empty(&failed)) {
1290 /* For some reason our attempt to resend pages. Mark the
1291 * overall send request as having failed, and let
1292 * nfs_writeback_release_full deal with the error.
1293 */
1294 list_move(&failed, head);
1295 return -EIO;
1296 }
1297 return 0;
1298}
Andy Adamsone7dd79af2012-04-27 17:53:46 -04001299EXPORT_SYMBOL_GPL(pnfs_write_done_resend_to_mds);
Trond Myklebuste2fecb22012-01-06 08:57:46 -05001300
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001301static void pnfs_ld_handle_write_error(struct nfs_write_data *data)
1302{
Fred Isamancd841602012-04-20 14:47:44 -04001303 struct nfs_pgio_header *hdr = data->header;
1304
1305 dprintk("pnfs write error = %d\n", hdr->pnfs_error);
1306 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001307 PNFS_LAYOUTRET_ON_ERROR) {
Fred Isamancd841602012-04-20 14:47:44 -04001308 clear_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(hdr->inode)->flags);
1309 pnfs_return_layout(hdr->inode);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001310 }
Fred Isaman6c75dc02012-04-20 14:47:47 -04001311 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
1312 data->task.tk_status = pnfs_write_done_resend_to_mds(hdr->inode,
Fred Isaman061ae2e2012-04-20 14:47:48 -04001313 &hdr->pages,
1314 hdr->completion_ops);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001315}
1316
Benny Halevyd20581a2011-05-22 19:52:03 +03001317/*
1318 * Called by non rpc-based layout drivers
1319 */
Peng Tao8ce160c2011-09-22 21:50:14 -04001320void pnfs_ld_write_done(struct nfs_write_data *data)
Fred Isaman94ad1c82011-03-01 01:34:14 +00001321{
Fred Isamancd841602012-04-20 14:47:44 -04001322 struct nfs_pgio_header *hdr = data->header;
1323
1324 if (!hdr->pnfs_error) {
Benny Halevyd20581a2011-05-22 19:52:03 +03001325 pnfs_set_layoutcommit(data);
Fred Isamancd841602012-04-20 14:47:44 -04001326 hdr->mds_ops->rpc_call_done(&data->task, data);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001327 } else
1328 pnfs_ld_handle_write_error(data);
Fred Isamancd841602012-04-20 14:47:44 -04001329 hdr->mds_ops->rpc_release(data);
Fred Isaman44b83792011-03-03 15:13:44 +00001330}
Benny Halevyd20581a2011-05-22 19:52:03 +03001331EXPORT_SYMBOL_GPL(pnfs_ld_write_done);
Fred Isaman44b83792011-03-03 15:13:44 +00001332
Trond Myklebustdce81292011-07-13 15:59:19 -04001333static void
1334pnfs_write_through_mds(struct nfs_pageio_descriptor *desc,
1335 struct nfs_write_data *data)
1336{
Fred Isamancd841602012-04-20 14:47:44 -04001337 struct nfs_pgio_header *hdr = data->header;
1338
Fred Isaman6c75dc02012-04-20 14:47:47 -04001339 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
1340 list_splice_tail_init(&hdr->pages, &desc->pg_list);
1341 nfs_pageio_reset_write_mds(desc);
1342 desc->pg_recoalesce = 1;
1343 }
Trond Myklebustdce81292011-07-13 15:59:19 -04001344 nfs_writedata_release(data);
1345}
1346
1347static enum pnfs_try_status
Andy Adamson0382b742011-03-03 15:13:45 +00001348pnfs_try_to_write_data(struct nfs_write_data *wdata,
Trond Myklebustdce81292011-07-13 15:59:19 -04001349 const struct rpc_call_ops *call_ops,
1350 struct pnfs_layout_segment *lseg,
1351 int how)
Andy Adamson0382b742011-03-03 15:13:45 +00001352{
Fred Isamancd841602012-04-20 14:47:44 -04001353 struct nfs_pgio_header *hdr = wdata->header;
1354 struct inode *inode = hdr->inode;
Andy Adamson0382b742011-03-03 15:13:45 +00001355 enum pnfs_try_status trypnfs;
1356 struct nfs_server *nfss = NFS_SERVER(inode);
1357
Fred Isamancd841602012-04-20 14:47:44 -04001358 hdr->mds_ops = call_ops;
Andy Adamson0382b742011-03-03 15:13:45 +00001359
1360 dprintk("%s: Writing ino:%lu %u@%llu (how %d)\n", __func__,
1361 inode->i_ino, wdata->args.count, wdata->args.offset, how);
Andy Adamson0382b742011-03-03 15:13:45 +00001362 trypnfs = nfss->pnfs_curr_ld->write_pagelist(wdata, how);
Fred Isaman6c75dc02012-04-20 14:47:47 -04001363 if (trypnfs != PNFS_NOT_ATTEMPTED)
Andy Adamson0382b742011-03-03 15:13:45 +00001364 nfs_inc_stats(inode, NFSIOS_PNFS_WRITE);
Andy Adamson0382b742011-03-03 15:13:45 +00001365 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1366 return trypnfs;
1367}
1368
Trond Myklebustdce81292011-07-13 15:59:19 -04001369static void
1370pnfs_do_multiple_writes(struct nfs_pageio_descriptor *desc, struct list_head *head, int how)
1371{
1372 struct nfs_write_data *data;
1373 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1374 struct pnfs_layout_segment *lseg = desc->pg_lseg;
1375
1376 desc->pg_lseg = NULL;
1377 while (!list_empty(head)) {
1378 enum pnfs_try_status trypnfs;
1379
Fred Isaman6c75dc02012-04-20 14:47:47 -04001380 data = list_first_entry(head, struct nfs_write_data, list);
Trond Myklebustdce81292011-07-13 15:59:19 -04001381 list_del_init(&data->list);
1382
1383 trypnfs = pnfs_try_to_write_data(data, call_ops, lseg, how);
1384 if (trypnfs == PNFS_NOT_ATTEMPTED)
1385 pnfs_write_through_mds(desc, data);
1386 }
1387 put_lseg(lseg);
1388}
1389
Fred Isaman6c75dc02012-04-20 14:47:47 -04001390static void pnfs_writehdr_free(struct nfs_pgio_header *hdr)
1391{
1392 put_lseg(hdr->lseg);
1393 nfs_writehdr_free(hdr);
1394}
1395
Trond Myklebustdce81292011-07-13 15:59:19 -04001396int
1397pnfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc)
1398{
Fred Isaman6c75dc02012-04-20 14:47:47 -04001399 struct nfs_write_header *whdr;
1400 struct nfs_pgio_header *hdr;
Trond Myklebustdce81292011-07-13 15:59:19 -04001401 int ret;
1402
Fred Isaman6c75dc02012-04-20 14:47:47 -04001403 whdr = nfs_writehdr_alloc();
1404 if (!whdr) {
Trond Myklebust9b5415b2012-04-27 14:31:47 -04001405 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
Fred Isaman6c75dc02012-04-20 14:47:47 -04001406 put_lseg(desc->pg_lseg);
1407 desc->pg_lseg = NULL;
1408 return -ENOMEM;
1409 }
1410 hdr = &whdr->header;
1411 nfs_pgheader_init(desc, hdr, pnfs_writehdr_free);
1412 hdr->lseg = get_lseg(desc->pg_lseg);
1413 atomic_inc(&hdr->refcnt);
1414 ret = nfs_generic_flush(desc, hdr);
Trond Myklebustdce81292011-07-13 15:59:19 -04001415 if (ret != 0) {
1416 put_lseg(desc->pg_lseg);
1417 desc->pg_lseg = NULL;
Fred Isaman6c75dc02012-04-20 14:47:47 -04001418 } else
1419 pnfs_do_multiple_writes(desc, &hdr->rpc_list, desc->pg_ioflags);
1420 if (atomic_dec_and_test(&hdr->refcnt))
Fred Isaman061ae2e2012-04-20 14:47:48 -04001421 hdr->completion_ops->completion(hdr);
Fred Isaman6c75dc02012-04-20 14:47:47 -04001422 return ret;
Trond Myklebustdce81292011-07-13 15:59:19 -04001423}
1424EXPORT_SYMBOL_GPL(pnfs_generic_pg_writepages);
1425
Andy Adamsone7dd79af2012-04-27 17:53:46 -04001426int pnfs_read_done_resend_to_mds(struct inode *inode,
Fred Isaman061ae2e2012-04-20 14:47:48 -04001427 struct list_head *head,
1428 const struct nfs_pgio_completion_ops *compl_ops)
Trond Myklebust62e4a762011-11-10 14:30:37 -05001429{
1430 struct nfs_pageio_descriptor pgio;
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001431 LIST_HEAD(failed);
Trond Myklebust62e4a762011-11-10 14:30:37 -05001432
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001433 /* Resend all requests through the MDS */
Bryan Schumaker1abb50882012-06-20 15:53:47 -04001434 nfs_pageio_init_read(&pgio, inode, compl_ops);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001435 while (!list_empty(head)) {
1436 struct nfs_page *req = nfs_list_entry(head->next);
Trond Myklebust62e4a762011-11-10 14:30:37 -05001437
1438 nfs_list_remove_request(req);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001439 if (!nfs_pageio_add_request(&pgio, req))
1440 nfs_list_add_request(req, &failed);
Trond Myklebust62e4a762011-11-10 14:30:37 -05001441 }
1442 nfs_pageio_complete(&pgio);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001443
1444 if (!list_empty(&failed)) {
1445 list_move(&failed, head);
1446 return -EIO;
1447 }
1448 return 0;
1449}
Andy Adamsone7dd79af2012-04-27 17:53:46 -04001450EXPORT_SYMBOL_GPL(pnfs_read_done_resend_to_mds);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001451
1452static void pnfs_ld_handle_read_error(struct nfs_read_data *data)
1453{
Fred Isamancd841602012-04-20 14:47:44 -04001454 struct nfs_pgio_header *hdr = data->header;
1455
1456 dprintk("pnfs read error = %d\n", hdr->pnfs_error);
1457 if (NFS_SERVER(hdr->inode)->pnfs_curr_ld->flags &
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001458 PNFS_LAYOUTRET_ON_ERROR) {
Fred Isamancd841602012-04-20 14:47:44 -04001459 clear_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(hdr->inode)->flags);
1460 pnfs_return_layout(hdr->inode);
Fred Isaman1acbbb4e12012-04-20 14:47:37 -04001461 }
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001462 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags))
1463 data->task.tk_status = pnfs_read_done_resend_to_mds(hdr->inode,
Fred Isaman061ae2e2012-04-20 14:47:48 -04001464 &hdr->pages,
1465 hdr->completion_ops);
Trond Myklebust62e4a762011-11-10 14:30:37 -05001466}
1467
Andy Adamsonb1f69b72010-10-20 00:18:03 -04001468/*
Benny Halevyd20581a2011-05-22 19:52:03 +03001469 * Called by non rpc-based layout drivers
1470 */
Peng Tao9b7eecd2011-09-22 21:50:15 -04001471void pnfs_ld_read_done(struct nfs_read_data *data)
Benny Halevyd20581a2011-05-22 19:52:03 +03001472{
Fred Isamancd841602012-04-20 14:47:44 -04001473 struct nfs_pgio_header *hdr = data->header;
1474
1475 if (likely(!hdr->pnfs_error)) {
Benny Halevyd20581a2011-05-22 19:52:03 +03001476 __nfs4_read_done_cb(data);
Fred Isamancd841602012-04-20 14:47:44 -04001477 hdr->mds_ops->rpc_call_done(&data->task, data);
Trond Myklebust62e4a762011-11-10 14:30:37 -05001478 } else
1479 pnfs_ld_handle_read_error(data);
Fred Isamancd841602012-04-20 14:47:44 -04001480 hdr->mds_ops->rpc_release(data);
Benny Halevyd20581a2011-05-22 19:52:03 +03001481}
1482EXPORT_SYMBOL_GPL(pnfs_ld_read_done);
1483
Trond Myklebust493292d2011-07-13 15:58:28 -04001484static void
1485pnfs_read_through_mds(struct nfs_pageio_descriptor *desc,
1486 struct nfs_read_data *data)
1487{
Fred Isamancd841602012-04-20 14:47:44 -04001488 struct nfs_pgio_header *hdr = data->header;
1489
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001490 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
1491 list_splice_tail_init(&hdr->pages, &desc->pg_list);
1492 nfs_pageio_reset_read_mds(desc);
1493 desc->pg_recoalesce = 1;
1494 }
Trond Myklebust493292d2011-07-13 15:58:28 -04001495 nfs_readdata_release(data);
1496}
1497
Benny Halevyd20581a2011-05-22 19:52:03 +03001498/*
Andy Adamson64419a92011-03-01 01:34:16 +00001499 * Call the appropriate parallel I/O subsystem read function.
1500 */
Trond Myklebust493292d2011-07-13 15:58:28 -04001501static enum pnfs_try_status
Andy Adamson64419a92011-03-01 01:34:16 +00001502pnfs_try_to_read_data(struct nfs_read_data *rdata,
Trond Myklebust493292d2011-07-13 15:58:28 -04001503 const struct rpc_call_ops *call_ops,
1504 struct pnfs_layout_segment *lseg)
Andy Adamson64419a92011-03-01 01:34:16 +00001505{
Fred Isamancd841602012-04-20 14:47:44 -04001506 struct nfs_pgio_header *hdr = rdata->header;
1507 struct inode *inode = hdr->inode;
Andy Adamson64419a92011-03-01 01:34:16 +00001508 struct nfs_server *nfss = NFS_SERVER(inode);
1509 enum pnfs_try_status trypnfs;
1510
Fred Isamancd841602012-04-20 14:47:44 -04001511 hdr->mds_ops = call_ops;
Andy Adamson64419a92011-03-01 01:34:16 +00001512
1513 dprintk("%s: Reading ino:%lu %u@%llu\n",
1514 __func__, inode->i_ino, rdata->args.count, rdata->args.offset);
1515
1516 trypnfs = nfss->pnfs_curr_ld->read_pagelist(rdata);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001517 if (trypnfs != PNFS_NOT_ATTEMPTED)
Andy Adamson64419a92011-03-01 01:34:16 +00001518 nfs_inc_stats(inode, NFSIOS_PNFS_READ);
Andy Adamson64419a92011-03-01 01:34:16 +00001519 dprintk("%s End (trypnfs:%d)\n", __func__, trypnfs);
1520 return trypnfs;
1521}
Andy Adamson863a3c62011-03-23 13:27:54 +00001522
Trond Myklebust493292d2011-07-13 15:58:28 -04001523static void
1524pnfs_do_multiple_reads(struct nfs_pageio_descriptor *desc, struct list_head *head)
1525{
1526 struct nfs_read_data *data;
1527 const struct rpc_call_ops *call_ops = desc->pg_rpc_callops;
1528 struct pnfs_layout_segment *lseg = desc->pg_lseg;
1529
1530 desc->pg_lseg = NULL;
1531 while (!list_empty(head)) {
1532 enum pnfs_try_status trypnfs;
1533
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001534 data = list_first_entry(head, struct nfs_read_data, list);
Trond Myklebust493292d2011-07-13 15:58:28 -04001535 list_del_init(&data->list);
1536
1537 trypnfs = pnfs_try_to_read_data(data, call_ops, lseg);
1538 if (trypnfs == PNFS_NOT_ATTEMPTED)
1539 pnfs_read_through_mds(desc, data);
1540 }
1541 put_lseg(lseg);
1542}
1543
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001544static void pnfs_readhdr_free(struct nfs_pgio_header *hdr)
1545{
1546 put_lseg(hdr->lseg);
1547 nfs_readhdr_free(hdr);
1548}
1549
Trond Myklebust493292d2011-07-13 15:58:28 -04001550int
1551pnfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
1552{
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001553 struct nfs_read_header *rhdr;
1554 struct nfs_pgio_header *hdr;
Trond Myklebust493292d2011-07-13 15:58:28 -04001555 int ret;
1556
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001557 rhdr = nfs_readhdr_alloc();
1558 if (!rhdr) {
Fred Isaman061ae2e2012-04-20 14:47:48 -04001559 desc->pg_completion_ops->error_cleanup(&desc->pg_list);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001560 ret = -ENOMEM;
Trond Myklebust493292d2011-07-13 15:58:28 -04001561 put_lseg(desc->pg_lseg);
1562 desc->pg_lseg = NULL;
1563 return ret;
1564 }
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001565 hdr = &rhdr->header;
1566 nfs_pgheader_init(desc, hdr, pnfs_readhdr_free);
1567 hdr->lseg = get_lseg(desc->pg_lseg);
1568 atomic_inc(&hdr->refcnt);
1569 ret = nfs_generic_pagein(desc, hdr);
1570 if (ret != 0) {
1571 put_lseg(desc->pg_lseg);
1572 desc->pg_lseg = NULL;
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001573 } else
1574 pnfs_do_multiple_reads(desc, &hdr->rpc_list);
1575 if (atomic_dec_and_test(&hdr->refcnt))
Fred Isaman061ae2e2012-04-20 14:47:48 -04001576 hdr->completion_ops->completion(hdr);
Fred Isaman4db6e0b2012-04-20 14:47:46 -04001577 return ret;
Trond Myklebust493292d2011-07-13 15:58:28 -04001578}
1579EXPORT_SYMBOL_GPL(pnfs_generic_pg_readpages);
1580
Andy Adamson863a3c62011-03-23 13:27:54 +00001581/*
Peng Taoa9bae562011-07-30 20:52:33 -04001582 * There can be multiple RW segments.
Andy Adamson863a3c62011-03-23 13:27:54 +00001583 */
Peng Taoa9bae562011-07-30 20:52:33 -04001584static void pnfs_list_write_lseg(struct inode *inode, struct list_head *listp)
Andy Adamson863a3c62011-03-23 13:27:54 +00001585{
Peng Taoa9bae562011-07-30 20:52:33 -04001586 struct pnfs_layout_segment *lseg;
Andy Adamson863a3c62011-03-23 13:27:54 +00001587
Peng Taoa9bae562011-07-30 20:52:33 -04001588 list_for_each_entry(lseg, &NFS_I(inode)->layout->plh_segs, pls_list) {
1589 if (lseg->pls_range.iomode == IOMODE_RW &&
1590 test_bit(NFS_LSEG_LAYOUTCOMMIT, &lseg->pls_flags))
1591 list_add(&lseg->pls_lc_list, listp);
1592 }
Andy Adamson863a3c62011-03-23 13:27:54 +00001593}
1594
Peng Tao1b0ae062011-09-22 21:50:12 -04001595void pnfs_set_lo_fail(struct pnfs_layout_segment *lseg)
1596{
1597 if (lseg->pls_range.iomode == IOMODE_RW) {
1598 dprintk("%s Setting layout IOMODE_RW fail bit\n", __func__);
1599 set_bit(lo_fail_bit(IOMODE_RW), &lseg->pls_layout->plh_flags);
1600 } else {
1601 dprintk("%s Setting layout IOMODE_READ fail bit\n", __func__);
1602 set_bit(lo_fail_bit(IOMODE_READ), &lseg->pls_layout->plh_flags);
1603 }
1604}
1605EXPORT_SYMBOL_GPL(pnfs_set_lo_fail);
1606
Andy Adamson863a3c62011-03-23 13:27:54 +00001607void
1608pnfs_set_layoutcommit(struct nfs_write_data *wdata)
1609{
Fred Isamancd841602012-04-20 14:47:44 -04001610 struct nfs_pgio_header *hdr = wdata->header;
1611 struct inode *inode = hdr->inode;
1612 struct nfs_inode *nfsi = NFS_I(inode);
Vitaliy Gusev4b8ee2b2011-05-20 01:34:46 +04001613 loff_t end_pos = wdata->mds_offset + wdata->res.count;
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001614 bool mark_as_dirty = false;
Andy Adamson863a3c62011-03-23 13:27:54 +00001615
Fred Isamancd841602012-04-20 14:47:44 -04001616 spin_lock(&inode->i_lock);
Andy Adamson863a3c62011-03-23 13:27:54 +00001617 if (!test_and_set_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001618 mark_as_dirty = true;
Andy Adamson863a3c62011-03-23 13:27:54 +00001619 dprintk("%s: Set layoutcommit for inode %lu ",
Fred Isamancd841602012-04-20 14:47:44 -04001620 __func__, inode->i_ino);
Andy Adamson863a3c62011-03-23 13:27:54 +00001621 }
Fred Isamancd841602012-04-20 14:47:44 -04001622 if (!test_and_set_bit(NFS_LSEG_LAYOUTCOMMIT, &hdr->lseg->pls_flags)) {
Peng Taoa9bae562011-07-30 20:52:33 -04001623 /* references matched in nfs4_layoutcommit_release */
Fred Isamancd841602012-04-20 14:47:44 -04001624 get_lseg(hdr->lseg);
Peng Taoa9bae562011-07-30 20:52:33 -04001625 }
Peng Taoacff58802011-07-30 20:52:31 -04001626 if (end_pos > nfsi->layout->plh_lwb)
1627 nfsi->layout->plh_lwb = end_pos;
Fred Isamancd841602012-04-20 14:47:44 -04001628 spin_unlock(&inode->i_lock);
Peng Taoacff58802011-07-30 20:52:31 -04001629 dprintk("%s: lseg %p end_pos %llu\n",
Fred Isamancd841602012-04-20 14:47:44 -04001630 __func__, hdr->lseg, nfsi->layout->plh_lwb);
Weston Andros Adamson79a48a12011-04-13 10:53:51 -04001631
1632 /* if pnfs_layoutcommit_inode() runs between inode locks, the next one
1633 * will be a noop because NFS_INO_LAYOUTCOMMIT will not be set */
1634 if (mark_as_dirty)
Fred Isamancd841602012-04-20 14:47:44 -04001635 mark_inode_dirty_sync(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00001636}
1637EXPORT_SYMBOL_GPL(pnfs_set_layoutcommit);
1638
Andy Adamsondb29c082011-07-30 20:52:38 -04001639void pnfs_cleanup_layoutcommit(struct nfs4_layoutcommit_data *data)
1640{
1641 struct nfs_server *nfss = NFS_SERVER(data->args.inode);
1642
1643 if (nfss->pnfs_curr_ld->cleanup_layoutcommit)
1644 nfss->pnfs_curr_ld->cleanup_layoutcommit(data);
1645}
1646
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001647/*
1648 * For the LAYOUT4_NFSV4_1_FILES layout type, NFS_DATA_SYNC WRITEs and
1649 * NFS_UNSTABLE WRITEs with a COMMIT to data servers must store enough
1650 * data to disk to allow the server to recover the data if it crashes.
1651 * LAYOUTCOMMIT is only needed when the NFL4_UFLG_COMMIT_THRU_MDS flag
1652 * is off, and a COMMIT is sent to a data server, or
1653 * if WRITEs to a data server return NFS_DATA_SYNC.
1654 */
Andy Adamson863a3c62011-03-23 13:27:54 +00001655int
Andy Adamsonef311532011-03-12 02:58:10 -05001656pnfs_layoutcommit_inode(struct inode *inode, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00001657{
1658 struct nfs4_layoutcommit_data *data;
1659 struct nfs_inode *nfsi = NFS_I(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00001660 loff_t end_pos;
1661 int status = 0;
1662
1663 dprintk("--> %s inode %lu\n", __func__, inode->i_ino);
1664
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001665 if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
1666 return 0;
1667
Andy Adamson863a3c62011-03-23 13:27:54 +00001668 /* Note kzalloc ensures data->res.seq_res.sr_slot == NULL */
1669 data = kzalloc(sizeof(*data), GFP_NOFS);
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001670 if (!data) {
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001671 status = -ENOMEM;
1672 goto out;
1673 }
Andy Adamson863a3c62011-03-23 13:27:54 +00001674
Peng Tao92407e72011-10-23 20:21:17 -07001675 if (!test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags))
1676 goto out_free;
1677
1678 if (test_and_set_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags)) {
1679 if (!sync) {
1680 status = -EAGAIN;
1681 goto out_free;
1682 }
1683 status = wait_on_bit_lock(&nfsi->flags, NFS_INO_LAYOUTCOMMITTING,
1684 nfs_wait_bit_killable, TASK_KILLABLE);
1685 if (status)
1686 goto out_free;
1687 }
1688
Peng Taoa9bae562011-07-30 20:52:33 -04001689 INIT_LIST_HEAD(&data->lseg_list);
Andy Adamsonde4b15c2011-03-12 02:58:09 -05001690 spin_lock(&inode->i_lock);
Andy Adamson863a3c62011-03-23 13:27:54 +00001691 if (!test_and_clear_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) {
Peng Tao92407e72011-10-23 20:21:17 -07001692 clear_bit(NFS_INO_LAYOUTCOMMITTING, &nfsi->flags);
Andy Adamson863a3c62011-03-23 13:27:54 +00001693 spin_unlock(&inode->i_lock);
Peng Tao92407e72011-10-23 20:21:17 -07001694 wake_up_bit(&nfsi->flags, NFS_INO_LAYOUTCOMMITTING);
1695 goto out_free;
Andy Adamson863a3c62011-03-23 13:27:54 +00001696 }
Peng Taoa9bae562011-07-30 20:52:33 -04001697
1698 pnfs_list_write_lseg(inode, &data->lseg_list);
Andy Adamson863a3c62011-03-23 13:27:54 +00001699
Peng Taoacff58802011-07-30 20:52:31 -04001700 end_pos = nfsi->layout->plh_lwb;
Peng Taoacff58802011-07-30 20:52:31 -04001701 nfsi->layout->plh_lwb = 0;
Andy Adamson863a3c62011-03-23 13:27:54 +00001702
Trond Myklebustf597c532012-03-04 18:13:56 -05001703 nfs4_stateid_copy(&data->args.stateid, &nfsi->layout->plh_stateid);
Andy Adamson863a3c62011-03-23 13:27:54 +00001704 spin_unlock(&inode->i_lock);
1705
1706 data->args.inode = inode;
Peng Tao9fa40752011-07-30 20:52:32 -04001707 data->cred = get_rpccred(nfsi->layout->plh_lc_cred);
Andy Adamson863a3c62011-03-23 13:27:54 +00001708 nfs_fattr_init(&data->fattr);
1709 data->args.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
1710 data->res.fattr = &data->fattr;
1711 data->args.lastbytewritten = end_pos - 1;
1712 data->res.server = NFS_SERVER(inode);
1713
1714 status = nfs4_proc_layoutcommit(data, sync);
1715out:
Peng Tao92407e72011-10-23 20:21:17 -07001716 if (status)
1717 mark_inode_dirty_sync(inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00001718 dprintk("<-- %s status %d\n", __func__, status);
1719 return status;
Peng Tao92407e72011-10-23 20:21:17 -07001720out_free:
1721 kfree(data);
1722 goto out;
Andy Adamson863a3c62011-03-23 13:27:54 +00001723}
Andy Adamson82be4172012-05-23 05:02:35 -04001724
1725struct nfs4_threshold *pnfs_mdsthreshold_alloc(void)
1726{
1727 struct nfs4_threshold *thp;
1728
1729 thp = kzalloc(sizeof(*thp), GFP_NOFS);
1730 if (!thp) {
1731 dprintk("%s mdsthreshold allocation failed\n", __func__);
1732 return NULL;
1733 }
1734 return thp;
1735}